src/model/Component.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "Component.hpp" | ||
| 7 | #include "Component_Impl.hpp" | ||
| 8 | |||
| 9 | #include "Version.hpp" | ||
| 10 | #include "Version_Impl.hpp" | ||
| 11 | #include "ComponentData.hpp" | ||
| 12 | #include "ComponentData_Impl.hpp" | ||
| 13 | |||
| 14 | #include "../utilities/idf/IdfFile.hpp" | ||
| 15 | #include "../utilities/idf/WorkspaceObject.hpp" | ||
| 16 | |||
| 17 | #include <utilities/idd/OS_ComponentData_FieldEnums.hxx> | ||
| 18 | #include <utilities/idd/IddEnums.hxx> | ||
| 19 | |||
| 20 | #include "../utilities/core/Assert.hpp" | ||
| 21 | #include "../utilities/core/PathHelpers.hpp" | ||
| 22 | |||
| 23 | namespace openstudio { | ||
| 24 | namespace model { | ||
| 25 | |||
| 26 | namespace detail { | ||
| 27 | |||
| 28 | 4 | Component_Impl::Component_Impl(const IdfFile& idfFile) : Model_Impl(idfFile) {} | |
| 29 | |||
| 30 | ✗ | Component_Impl::Component_Impl(const Component_Impl& other, bool keepHandles) : Model_Impl(other, keepHandles) {} | |
| 31 | |||
| 32 |
2/2✓ Branch 1 taken 129 times.
✓ Branch 4 taken 129 times.
|
129 | Component_Impl::Component_Impl(const Model_Impl& model, const std::vector<Handle>& hs) : Model_Impl(model, hs, false, StrictnessLevel::Draft) {} |
| 33 | |||
| 34 | ✗ | Workspace Component_Impl::clone(bool keepHandles) const { | |
| 35 | // copy everything but objects | ||
| 36 | ✗ | std::shared_ptr<Component_Impl> cloneImpl(new Component_Impl(*this, keepHandles)); | |
| 37 | // clone objects | ||
| 38 | ✗ | createAndAddClonedObjects(getWorkspace<Component>().getImpl<Component_Impl>(), cloneImpl, keepHandles); | |
| 39 | // wrap impl and return | ||
| 40 | ✗ | Component result(cloneImpl); | |
| 41 | ✗ | return result.cast<Workspace>(); | |
| 42 | ✗ | } | |
| 43 | |||
| 44 | ✗ | Workspace Component_Impl::cloneSubset(const std::vector<Handle>& handles, bool keepHandles, StrictnessLevel level) const { | |
| 45 | ✗ | if (!handles.empty()) { | |
| 46 | ✗ | LOG_AND_THROW("Components::cloneSubset is not supported. A workaround may be to clone() and " << "then remove the unwanted objects."); | |
| 47 | } | ||
| 48 | // create Model with same characteristics as this Component | ||
| 49 | ✗ | return Model_Impl::cloneSubset(handles, keepHandles, level); | |
| 50 | } | ||
| 51 | |||
| 52 | 57 | ComponentData Component_Impl::componentData() const { | |
| 53 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
|
57 | ComponentDataVector candidates = model().getConcreteModelObjects<ComponentData>(); |
| 54 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✗ Branch 4 not taken.
|
57 | OS_ASSERT(candidates.size() == 1); |
| 55 |
1/1✓ Branch 2 taken 57 times.
|
114 | return candidates[0]; |
| 56 | 57 | } | |
| 57 | |||
| 58 | 6 | ModelObject Component_Impl::primaryObject() const { | |
| 59 |
1/1✓ Branch 1 taken 6 times.
|
6 | ComponentData metaDataAndContents = componentData(); |
| 60 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 6 not taken.
|
6 | OS_ASSERT(metaDataAndContents.numComponentObjects() > 0); |
| 61 |
1/1✓ Branch 1 taken 6 times.
|
12 | return metaDataAndContents.primaryComponentObject(); |
| 62 | 6 | } | |
| 63 | |||
| 64 | // SETTERS (override to do nothing) | ||
| 65 | |||
| 66 | ✗ | bool Component_Impl::setStrictnessLevel(StrictnessLevel /*level*/) { | |
| 67 | ✗ | return false; | |
| 68 | } | ||
| 69 | |||
| 70 | ✗ | boost::optional<WorkspaceObject> Component_Impl::addObject(const IdfObject& /*idfObject*/) { | |
| 71 | ✗ | return boost::none; | |
| 72 | } | ||
| 73 | |||
| 74 | ✗ | boost::optional<WorkspaceObject> Component_Impl::insertObject(const IdfObject& /*idfObject*/) { | |
| 75 | ✗ | return boost::none; | |
| 76 | } | ||
| 77 | |||
| 78 | ✗ | std::vector<WorkspaceObject> Component_Impl::addObjects(const IdfObjectVector& /*idfObjects*/, bool /*checkNames*/) { | |
| 79 | ✗ | return {}; | |
| 80 | } | ||
| 81 | |||
| 82 | ✗ | std::vector<WorkspaceObject> Component_Impl::insertObjects(const IdfObjectVector& /*idfObjects*/) { | |
| 83 | ✗ | return {}; | |
| 84 | } | ||
| 85 | |||
| 86 | ✗ | std::vector<WorkspaceObject> Component_Impl::addAndInsertObjects(const std::vector<IdfObject>& /*objectsToAdd*/, | |
| 87 | const std::vector<IdfObject>& /*objectsToInsert*/) { | ||
| 88 | ✗ | return {}; | |
| 89 | } | ||
| 90 | |||
| 91 | ✗ | std::vector<WorkspaceObject> Component_Impl::addObjects(const std::vector<WorkspaceObject>& /*objects*/, bool /*checkNames*/) { | |
| 92 | ✗ | return {}; | |
| 93 | } | ||
| 94 | |||
| 95 | ✗ | std::vector<WorkspaceObject> Component_Impl::insertObjects(const std::vector<WorkspaceObject>& /*objects*/) { | |
| 96 | ✗ | return {}; | |
| 97 | } | ||
| 98 | |||
| 99 | ✗ | std::vector<WorkspaceObject> Component_Impl::addAndInsertObjects(const std::vector<WorkspaceObject>& /*objectsToAdd*/, | |
| 100 | const std::vector<WorkspaceObject>& /*objectsToInsert*/) { | ||
| 101 | ✗ | return {}; | |
| 102 | } | ||
| 103 | |||
| 104 | ✗ | std::vector<WorkspaceObject> Component_Impl::addAndInsertObjects(const std::vector<WorkspaceObject>& /*objectsToAdd*/, | |
| 105 | const std::vector<std::vector<WorkspaceObject>>& /*objectsToInsert*/) { | ||
| 106 | ✗ | return {}; | |
| 107 | } | ||
| 108 | |||
| 109 | ✗ | bool Component_Impl::setSqlFile(const openstudio::SqlFile& /*sqlFile*/) { | |
| 110 | ✗ | return false; | |
| 111 | } | ||
| 112 | |||
| 113 | ✗ | boost::optional<ComponentData> Component_Impl::insertComponent(const Component& /*component*/) { | |
| 114 | ✗ | return boost::none; | |
| 115 | } | ||
| 116 | |||
| 117 | ✗ | std::vector<openstudio::IdfObject> Component_Impl::purgeUnusedResourceObjects() { | |
| 118 | ✗ | return {}; | |
| 119 | } | ||
| 120 | |||
| 121 | ✗ | std::vector<openstudio::IdfObject> Component_Impl::purgeUnusedResourceObjects(IddObjectType /*iddObjectType*/) { | |
| 122 | ✗ | return {}; | |
| 123 | } | ||
| 124 | |||
| 125 | ✗ | void Component_Impl::obsoleteComponentWatcher(const ComponentWatcher& watcher) { | |
| 126 | ✗ | Model_Impl::obsoleteComponentWatcher(watcher); | |
| 127 | ✗ | LOG_AND_THROW("The ComponentData object or primaryComponentObject has been removed. " | |
| 128 | << "This Component is no longer valid. (The ComponentData object may have been removed " | ||
| 129 | << "because a relationship change called into question whether the Component was still " << "valid.)"); | ||
| 130 | } | ||
| 131 | |||
| 132 | 113 | bool Component_Impl::save(const openstudio::path& p, bool overwrite) { | |
| 133 |
3/3✓ Branch 1 taken 113 times.
✓ Branch 4 taken 113 times.
✓ Branch 7 taken 113 times.
|
113 | return openstudio::detail::Workspace_Impl::save(setFileExtension(p, componentFileExtension(), true, true), overwrite); |
| 134 | } | ||
| 135 | |||
| 136 | } // namespace detail | ||
| 137 | |||
| 138 |
4/6✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 11 taken 4 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
|
4 | Component::Component(const openstudio::IdfFile& idfFile) : Model(std::shared_ptr<detail::Component_Impl>(new detail::Component_Impl(idfFile))) { |
| 139 | // construct WorkspaceObject_ImplPtrs | ||
| 140 | 4 | openstudio::detail::WorkspaceObject_ImplPtrVector objectImplPtrs; | |
| 141 |
2/3✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
8 | if (OptionalIdfObject vo = idfFile.versionObject()) { |
| 142 |
3/3✓ Branch 3 taken 4 times.
✓ Branch 6 taken 4 times.
✓ Branch 9 taken 4 times.
|
4 | objectImplPtrs.push_back(getImpl<detail::Model_Impl>()->createObject(*vo, true)); |
| 143 | 4 | } | |
| 144 |
3/3✓ Branch 1 taken 4 times.
✓ Branch 7 taken 44 times.
✓ Branch 8 taken 4 times.
|
48 | for (const IdfObject& idfObject : idfFile.objects()) { |
| 145 |
2/2✓ Branch 3 taken 44 times.
✓ Branch 6 taken 44 times.
|
44 | objectImplPtrs.push_back(getImpl<detail::Component_Impl>()->createObject(idfObject, true)); |
| 146 | 4 | } | |
| 147 | // add Object_ImplPtrs to Workspace_Impl | ||
| 148 |
1/1✓ Branch 5 taken 4 times.
|
4 | getImpl<openstudio::detail::Workspace_Impl>()->addObjects(objectImplPtrs); |
| 149 | |||
| 150 | // check that this really is a component | ||
| 151 | // 1 Version object | ||
| 152 |
1/1✓ Branch 1 taken 4 times.
|
4 | boost::optional<Version> versionObject = getOptionalUniqueModelObject<Version>(); |
| 153 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if (!versionObject) { |
| 154 | ✗ | LOG_AND_THROW("Cannot construct Component because it does not contain a version object.") | |
| 155 | } | ||
| 156 | |||
| 157 | // 1 ComponentData object | ||
| 158 |
1/1✓ Branch 1 taken 4 times.
|
4 | ComponentDataVector componentDataObjects = getConcreteModelObjects<ComponentData>(); |
| 159 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if (componentDataObjects.size() != 1) { |
| 160 | ✗ | LOG_AND_THROW("Cannot construct Component from IdfFile because the file contains " << componentDataObjects.size() << " ComponentData objects."); | |
| 161 | } | ||
| 162 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 4 not taken.
|
4 | OS_ASSERT(componentDataObjects.size() == 1); |
| 163 |
1/1✓ Branch 2 taken 4 times.
|
4 | ComponentData componentData = componentDataObjects[0]; |
| 164 | |||
| 165 | // All other objects are Component contents | ||
| 166 |
2/3✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
|
4 | if (componentData.numComponentObjects() == 0) { |
| 167 | ✗ | LOG_AND_THROW("Cannot construct Component from IdfFile because there are no objects listed " << "in the contents."); | |
| 168 | } | ||
| 169 |
3/3✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
|
4 | int nNonTrivialObjects = numObjects() - getObjectsByType(IddObjectType::CommentOnly).size(); |
| 170 |
2/3✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
|
4 | if (nNonTrivialObjects != static_cast<int>(componentData.numComponentObjects()) + 1) { |
| 171 | ✗ | LOG_AND_THROW("Cannot construct Component from IdfFile because the file contains " << nNonTrivialObjects << " non-trivial objects, not " | |
| 172 | << componentData.numComponentObjects() + 1u | ||
| 173 | << ", as expected. The file: " << '\n' | ||
| 174 | << idfFile); | ||
| 175 | } | ||
| 176 | try { | ||
| 177 |
1/1✓ Branch 1 taken 4 times.
|
4 | ModelObjectVector componentContents = componentData.componentObjects(); |
| 178 | 4 | } catch (...) { | |
| 179 | ✗ | LOG_AND_THROW("Cannot construct Component from IdfFile because at least one of the " << "comprising objects cannot be located."); | |
| 180 | ✗ | } | |
| 181 | |||
| 182 |
1/1✓ Branch 3 taken 4 times.
|
4 | getImpl<detail::Model_Impl>()->createComponentWatchers(); |
| 183 | 4 | } | |
| 184 | |||
| 185 | ✗ | boost::optional<Component> Component::load(const path& p) { | |
| 186 | ✗ | OptionalComponent result; | |
| 187 | ✗ | OptionalIdfFile oIdfFile = IdfFile::load(p, IddFileType::OpenStudio); | |
| 188 | ✗ | if (oIdfFile) { | |
| 189 | try { | ||
| 190 | ✗ | result = Component(*oIdfFile); | |
| 191 | ✗ | } catch (...) { | |
| 192 | ✗ | } | |
| 193 | } | ||
| 194 | ✗ | return result; | |
| 195 | ✗ | } | |
| 196 | |||
| 197 | 51 | ComponentData Component::componentData() const { | |
| 198 |
1/1✓ Branch 3 taken 51 times.
|
51 | return getImpl<detail::Component_Impl>()->componentData(); |
| 199 | } | ||
| 200 | |||
| 201 | 6 | ModelObject Component::primaryObject() const { | |
| 202 |
1/1✓ Branch 3 taken 6 times.
|
6 | return getImpl<detail::Component_Impl>()->primaryObject(); |
| 203 | } | ||
| 204 | |||
| 205 | 113 | bool Component::save(const openstudio::path& p, bool overwrite) { | |
| 206 |
1/1✓ Branch 3 taken 113 times.
|
113 | return getImpl<detail::Component_Impl>()->save(p, overwrite); |
| 207 | } | ||
| 208 | |||
| 209 | /// @cond | ||
| 210 |
1/1✓ Branch 3 taken 4 times.
|
4 | Component::Component(std::shared_ptr<detail::Component_Impl> impl) : Model(std::move(impl)) {} |
| 211 | /// @endcond | ||
| 212 | |||
| 213 | 129 | Component::Component(const std::vector<ModelObject>& contents) | |
| 214 |
1/3✓ Branch 1 taken 129 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
258 | : Model(std::shared_ptr<detail::Component_Impl>( |
| 215 |
5/5✓ Branch 1 taken 129 times.
✓ Branch 4 taken 129 times.
✓ Branch 8 taken 129 times.
✓ Branch 13 taken 129 times.
✓ Branch 17 taken 129 times.
|
387 | new detail::Component_Impl(*(contents[0].model().getImpl<detail::Model_Impl>()), getHandles<ModelObject>(contents)))) { |
| 216 | // create Version object | ||
| 217 |
1/1✓ Branch 1 taken 129 times.
|
129 | this->addVersionObject(); |
| 218 | |||
| 219 | // create ComponentData object | ||
| 220 | 129 | openstudio::detail::WorkspaceObject_ImplPtrVector objectImplPtrs; | |
| 221 |
4/4✓ Branch 3 taken 129 times.
✓ Branch 6 taken 129 times.
✓ Branch 9 taken 129 times.
✓ Branch 12 taken 129 times.
|
129 | objectImplPtrs.push_back(getImpl<detail::Component_Impl>()->createObject(IdfObject(ComponentData::iddObjectType()), false)); |
| 222 |
1/1✓ Branch 5 taken 129 times.
|
129 | WorkspaceObjectVector objects = getImpl<openstudio::detail::Workspace_Impl>()->addObjects(objectImplPtrs); |
| 223 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 129 times.
✗ Branch 4 not taken.
|
129 | OS_ASSERT(objects.size() == static_cast<unsigned>(1)); |
| 224 | |||
| 225 |
1/1✓ Branch 1 taken 129 times.
|
129 | ComponentDataVector cdTemp = getConcreteModelObjects<ComponentData>(); |
| 226 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 129 times.
✗ Branch 4 not taken.
|
129 | OS_ASSERT(cdTemp.size() == 1); |
| 227 |
1/1✓ Branch 2 taken 129 times.
|
129 | ComponentData componentData = cdTemp[0]; |
| 228 |
3/3✓ Branch 1 taken 129 times.
✓ Branch 4 taken 129 times.
✓ Branch 7 taken 129 times.
|
129 | componentData.setString(OS_ComponentDataFields::UUID, toString(createUUID())); |
| 229 |
1/1✓ Branch 2 taken 129 times.
|
129 | componentData.setInt(OS_ComponentDataFields::CreationTimestamp, (int)time(nullptr)); |
| 230 |
1/1✓ Branch 1 taken 129 times.
|
129 | componentData.createVersionUUID(); |
| 231 | |||
| 232 | // clone objects | ||
| 233 | 129 | openstudio::detail::WorkspaceObject_ImplPtrVector newObjectImplPtrs; | |
| 234 | 129 | HandleMap oldNewHandleMap; | |
| 235 |
1/1✓ Branch 2 taken 129 times.
|
129 | Model model = contents[0].model(); |
| 236 |
2/2✓ Branch 4 taken 315 times.
✓ Branch 5 taken 129 times.
|
444 | for (const ModelObject& mo : contents) { |
| 237 |
3/5✓ Branch 1 taken 315 times.
✓ Branch 4 taken 315 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 315 times.
✗ Branch 9 not taken.
|
315 | OS_ASSERT(mo.model() == model); |
| 238 |
2/2✓ Branch 5 taken 315 times.
✓ Branch 8 taken 315 times.
|
315 | newObjectImplPtrs.push_back(getImpl<ImplType>()->createObject(mo.getImpl<detail::ModelObject_Impl>(), false)); |
| 239 |
3/3✓ Branch 3 taken 315 times.
✓ Branch 6 taken 315 times.
✓ Branch 10 taken 315 times.
|
315 | oldNewHandleMap.insert(HandleMap::value_type(mo.handle(), newObjectImplPtrs.back()->handle())); |
| 240 | } | ||
| 241 |
1/1✓ Branch 5 taken 129 times.
|
129 | WorkspaceObjectVector newObjects = getImpl<detail::Model_Impl>()->addClones(newObjectImplPtrs, oldNewHandleMap, true); |
| 242 |
1/3✗ Branch 2 not taken.
✓ Branch 3 taken 129 times.
✗ Branch 5 not taken.
|
129 | OS_ASSERT(newObjects.size() == newObjectImplPtrs.size()); |
| 243 | |||
| 244 | // populate ComponentData | ||
| 245 |
1/1✓ Branch 1 taken 129 times.
|
129 | componentData.clearExtensibleGroups(); |
| 246 |
2/2✓ Branch 5 taken 315 times.
✓ Branch 6 taken 129 times.
|
444 | for (WorkspaceObject& newObject : newObjects) { |
| 247 |
1/1✓ Branch 1 taken 315 times.
|
315 | auto mo = newObject.cast<ModelObject>(); |
| 248 |
1/1✓ Branch 3 taken 315 times.
|
315 | bool ok = componentData.getImpl<detail::ComponentData_Impl>()->registerObject(mo); |
| 249 |
1/3✗ Branch 0 not taken.
✓ Branch 1 taken 315 times.
✗ Branch 3 not taken.
|
315 | OS_ASSERT(ok); |
| 250 | 315 | } | |
| 251 | |||
| 252 |
1/1✓ Branch 3 taken 129 times.
|
129 | getImpl<detail::Model_Impl>()->createComponentWatchers(); |
| 253 | 129 | } | |
| 254 | |||
| 255 | } // namespace model | ||
| 256 | } // namespace openstudio | ||
| 257 |