src/model/ModelObjectList.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 "ModelObjectList.hpp" | ||
| 7 | #include "ModelObjectList_Impl.hpp" | ||
| 8 | #include "Model.hpp" | ||
| 9 | #include "Model_Impl.hpp" | ||
| 10 | |||
| 11 | #include <utilities/idd/OS_ModelObjectList_FieldEnums.hxx> | ||
| 12 | #include <utilities/idd/IddEnums.hxx> | ||
| 13 | |||
| 14 | #include "../utilities/core/Assert.hpp" | ||
| 15 | #include "../utilities/idf/WorkspaceExtensibleGroup.hpp" | ||
| 16 | |||
| 17 | namespace openstudio { | ||
| 18 | namespace model { | ||
| 19 | |||
| 20 | namespace detail { | ||
| 21 | |||
| 22 | 1062 | ModelObjectList_Impl::ModelObjectList_Impl(const IdfObject& idfObject, Model_Impl* model, bool keepHandle) | |
| 23 | 1062 | : ModelObject_Impl(idfObject, model, keepHandle) { | |
| 24 |
4/6✓ Branch 1 taken 1062 times.
✓ Branch 4 taken 1062 times.
✓ Branch 7 taken 1062 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 1062 times.
✗ Branch 13 not taken.
|
1062 | OS_ASSERT(idfObject.iddObject().type() == ModelObjectList::iddObjectType()); |
| 25 | 1062 | } | |
| 26 | |||
| 27 | 41 | ModelObjectList_Impl::ModelObjectList_Impl(const openstudio::detail::WorkspaceObject_Impl& other, Model_Impl* model, bool keepHandle) | |
| 28 | 41 | : ModelObject_Impl(other, model, keepHandle) { | |
| 29 |
4/6✓ Branch 1 taken 41 times.
✓ Branch 4 taken 41 times.
✓ Branch 7 taken 41 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 41 times.
✗ Branch 13 not taken.
|
41 | OS_ASSERT(other.iddObject().type() == ModelObjectList::iddObjectType()); |
| 30 | 41 | } | |
| 31 | |||
| 32 | 363 | ModelObjectList_Impl::ModelObjectList_Impl(const ModelObjectList_Impl& other, Model_Impl* model, bool keepHandle) | |
| 33 | 363 | : ModelObject_Impl(other, model, keepHandle) {} | |
| 34 | |||
| 35 | ✗ | const std::vector<std::string>& ModelObjectList_Impl::outputVariableNames() const { | |
| 36 | ✗ | static const std::vector<std::string> result; | |
| 37 | ✗ | return result; | |
| 38 | } | ||
| 39 | |||
| 40 | ✗ | IddObjectType ModelObjectList_Impl::iddObjectType() const { | |
| 41 | ✗ | return ModelObjectList::iddObjectType(); | |
| 42 | } | ||
| 43 | |||
| 44 | 110 | std::vector<IdfObject> ModelObjectList_Impl::remove() { | |
| 45 | 110 | std::vector<IdfObject> result; | |
| 46 | |||
| 47 |
1/1✓ Branch 1 taken 110 times.
|
110 | std::vector<ModelObject> modelObjects = this->modelObjects(); |
| 48 | |||
| 49 |
2/2✓ Branch 1 taken 19 times.
✓ Branch 2 taken 91 times.
|
110 | if (!modelObjects.empty()) { |
| 50 |
2/2✓ Branch 5 taken 34 times.
✓ Branch 6 taken 19 times.
|
53 | for (auto& modelObject : modelObjects) { |
| 51 |
1/1✓ Branch 1 taken 34 times.
|
34 | std::vector<IdfObject> removedModelObject = modelObject.remove(); |
| 52 |
1/1✓ Branch 5 taken 34 times.
|
34 | result.insert(result.end(), removedModelObject.begin(), removedModelObject.end()); |
| 53 | 34 | } | |
| 54 | } | ||
| 55 | |||
| 56 |
1/1✓ Branch 1 taken 110 times.
|
110 | std::vector<IdfObject> removedModelObjectList = ModelObject_Impl::remove(); |
| 57 |
1/1✓ Branch 5 taken 110 times.
|
110 | result.insert(result.end(), removedModelObjectList.begin(), removedModelObjectList.end()); |
| 58 | |||
| 59 | 220 | return result; | |
| 60 | 110 | } | |
| 61 | |||
| 62 | 119 | ModelObject ModelObjectList_Impl::clone(Model model) const { | |
| 63 |
3/3✓ Branch 1 taken 119 times.
✓ Branch 4 taken 119 times.
✓ Branch 7 taken 119 times.
|
119 | auto modelObjectListClone = ModelObject_Impl::clone(model).cast<ModelObjectList>(); |
| 64 | |||
| 65 |
1/1✓ Branch 1 taken 119 times.
|
119 | std::vector<ModelObject> modelObjects = this->modelObjects(); |
| 66 | |||
| 67 |
2/2✓ Branch 1 taken 27 times.
✓ Branch 2 taken 92 times.
|
119 | if (!modelObjects.empty()) { |
| 68 |
1/1✓ Branch 1 taken 27 times.
|
27 | modelObjectListClone.removeAllModelObjects(); |
| 69 |
2/2✓ Branch 5 taken 44 times.
✓ Branch 6 taken 27 times.
|
71 | for (const auto& modelObject : modelObjects) { |
| 70 |
2/2✓ Branch 1 taken 44 times.
✓ Branch 4 taken 44 times.
|
44 | ModelObject modelObjectClone = modelObject.clone(model); |
| 71 |
1/1✓ Branch 1 taken 44 times.
|
44 | modelObjectListClone.addModelObject(modelObjectClone); |
| 72 | 44 | } | |
| 73 | } | ||
| 74 | |||
| 75 | 238 | return std::move(modelObjectListClone); | |
| 76 | 119 | } | |
| 77 | |||
| 78 | 1832 | std::vector<ModelObject> ModelObjectList_Impl::modelObjects() const { | |
| 79 | 1832 | std::vector<ModelObject> result; | |
| 80 | |||
| 81 |
1/1✓ Branch 1 taken 1832 times.
|
1832 | std::vector<IdfExtensibleGroup> groups = extensibleGroups(); |
| 82 | |||
| 83 |
2/2✓ Branch 5 taken 2255 times.
✓ Branch 6 taken 1832 times.
|
4087 | for (const auto& group : groups) { |
| 84 |
2/2✓ Branch 1 taken 2255 times.
✓ Branch 4 taken 2255 times.
|
2255 | boost::optional<WorkspaceObject> wo = group.cast<WorkspaceExtensibleGroup>().getTarget(OS_ModelObjectListExtensibleFields::ModelObject); |
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 2244 times.
✓ Branch 1 taken 11 times.
|
2255 | if (wo) { |
| 87 |
3/3✓ Branch 1 taken 2244 times.
✓ Branch 4 taken 2244 times.
✓ Branch 7 taken 2244 times.
|
2244 | result.push_back(wo->cast<ModelObject>()); |
| 88 | } | ||
| 89 | 2255 | } | |
| 90 | |||
| 91 | 3664 | return result; | |
| 92 | 1832 | } | |
| 93 | |||
| 94 | 76 | unsigned ModelObjectList_Impl::size() const { | |
| 95 |
1/1✓ Branch 1 taken 76 times.
|
76 | return extensibleGroups().size(); |
| 96 | } | ||
| 97 | |||
| 98 | 700 | bool ModelObjectList_Impl::addModelObject(const ModelObject& modelObject) { | |
| 99 | |||
| 100 | // DLM: should this class prevent duplicates in the list? | ||
| 101 | |||
| 102 |
3/3✓ Branch 1 taken 700 times.
✓ Branch 4 taken 700 times.
✓ Branch 7 taken 700 times.
|
700 | auto eg = getObject<ModelObject>().pushExtensibleGroup().cast<WorkspaceExtensibleGroup>(); |
| 103 | |||
| 104 |
2/2✓ Branch 1 taken 700 times.
✓ Branch 4 taken 700 times.
|
700 | bool ok = eg.setPointer(OS_ModelObjectListExtensibleFields::ModelObject, modelObject.handle()); |
| 105 | |||
| 106 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 698 times.
|
700 | if (!ok) { |
| 107 |
3/3✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
|
2 | getObject<ModelObject>().eraseExtensibleGroup(eg.groupIndex()); |
| 108 | } | ||
| 109 | 700 | return ok; | |
| 110 | 700 | } | |
| 111 | |||
| 112 | 220 | bool ModelObjectList_Impl::hasModelObject(const ModelObject& modelObject) const { | |
| 113 |
1/1✓ Branch 1 taken 220 times.
|
220 | std::vector<IdfExtensibleGroup> groups = extensibleGroups(); |
| 114 | |||
| 115 |
2/2✓ Branch 5 taken 235 times.
✓ Branch 6 taken 44 times.
|
279 | for (const auto& group : groups) { |
| 116 | // DLM: if an object was pointed to by this list but that object was deleted from the model, this list may have an empty field | ||
| 117 | // in that case getTarget may fail to return an object | ||
| 118 |
2/2✓ Branch 1 taken 235 times.
✓ Branch 4 taken 235 times.
|
235 | boost::optional<WorkspaceObject> wo = group.cast<WorkspaceExtensibleGroup>().getTarget(OS_ModelObjectListExtensibleFields::ModelObject); |
| 119 | |||
| 120 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 235 times.
✗ Branch 4 not taken.
|
235 | OS_ASSERT(wo); |
| 121 | |||
| 122 |
6/6✓ Branch 1 taken 235 times.
✓ Branch 4 taken 235 times.
✓ Branch 7 taken 235 times.
✓ Branch 10 taken 235 times.
✓ Branch 12 taken 176 times.
✓ Branch 13 taken 59 times.
|
235 | if (wo->handle() == modelObject.handle()) { |
| 123 | 176 | return true; | |
| 124 | } | ||
| 125 | 235 | } | |
| 126 | |||
| 127 | 44 | return false; | |
| 128 | 220 | } | |
| 129 | |||
| 130 | 159 | void ModelObjectList_Impl::removeModelObject(const ModelObject& modelObject) { | |
| 131 |
2/2✓ Branch 1 taken 156 times.
✓ Branch 2 taken 3 times.
|
159 | if (hasModelObject(modelObject)) { |
| 132 |
1/1✓ Branch 1 taken 156 times.
|
156 | WorkspaceExtensibleGroup eg = getGroupForModelObject(modelObject); |
| 133 |
3/3✓ Branch 1 taken 156 times.
✓ Branch 4 taken 156 times.
✓ Branch 7 taken 156 times.
|
156 | getObject<ModelObject>().eraseExtensibleGroup(eg.groupIndex()); |
| 134 | 156 | } | |
| 135 | 159 | } | |
| 136 | |||
| 137 | 74 | void ModelObjectList_Impl::removeAllModelObjects() { | |
| 138 |
2/2✓ Branch 1 taken 74 times.
✓ Branch 4 taken 74 times.
|
74 | getObject<ModelObject>().clearExtensibleGroups(); |
| 139 | 74 | } | |
| 140 | |||
| 141 | 156 | WorkspaceExtensibleGroup ModelObjectList_Impl::getGroupForModelObject(const ModelObject& modelObject) { | |
| 142 | 156 | boost::optional<WorkspaceExtensibleGroup> result; | |
| 143 | |||
| 144 |
1/1✓ Branch 1 taken 156 times.
|
156 | std::vector<IdfExtensibleGroup> groups = extensibleGroups(); |
| 145 | |||
| 146 |
1/2✓ Branch 5 taken 193 times.
✗ Branch 6 not taken.
|
193 | for (const auto& group : groups) { |
| 147 |
2/2✓ Branch 1 taken 193 times.
✓ Branch 4 taken 193 times.
|
193 | boost::optional<WorkspaceObject> wo = group.cast<WorkspaceExtensibleGroup>().getTarget(OS_ModelObjectListExtensibleFields::ModelObject); |
| 148 | |||
| 149 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 193 times.
✗ Branch 4 not taken.
|
193 | OS_ASSERT(wo); |
| 150 | |||
| 151 |
6/6✓ Branch 1 taken 193 times.
✓ Branch 4 taken 193 times.
✓ Branch 7 taken 193 times.
✓ Branch 10 taken 193 times.
✓ Branch 12 taken 156 times.
✓ Branch 13 taken 37 times.
|
193 | if (wo->handle() == modelObject.handle()) { |
| 152 |
2/2✓ Branch 1 taken 156 times.
✓ Branch 4 taken 156 times.
|
156 | result = group.cast<WorkspaceExtensibleGroup>(); |
| 153 | |||
| 154 | 156 | break; | |
| 155 | } | ||
| 156 | 193 | } | |
| 157 | |||
| 158 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 156 times.
✗ Branch 4 not taken.
|
156 | OS_ASSERT(result); |
| 159 | |||
| 160 |
1/1✓ Branch 1 taken 156 times.
|
312 | return result.get(); |
| 161 | 156 | } | |
| 162 | |||
| 163 | } // namespace detail | ||
| 164 | |||
| 165 | 1021 | ModelObjectList::ModelObjectList(const Model& model) : ModelObject(ModelObjectList::iddObjectType(), model, true) { | |
| 166 |
1/3✗ Branch 2 not taken.
✓ Branch 3 taken 1021 times.
✗ Branch 5 not taken.
|
1021 | OS_ASSERT(getImpl<detail::ModelObjectList_Impl>()); |
| 167 | 1021 | } | |
| 168 | |||
| 169 | 9902 | IddObjectType ModelObjectList::iddObjectType() { | |
| 170 |
1/1✓ Branch 1 taken 9902 times.
|
9902 | return {IddObjectType::OS_ModelObjectList}; |
| 171 | } | ||
| 172 | |||
| 173 | 110 | std::vector<IdfObject> ModelObjectList::remove() { | |
| 174 |
1/1✓ Branch 3 taken 110 times.
|
110 | return getImpl<detail::ModelObjectList_Impl>()->remove(); |
| 175 | } | ||
| 176 | |||
| 177 | 119 | ModelObject ModelObjectList::clone(Model model) const { | |
| 178 |
2/2✓ Branch 3 taken 119 times.
✓ Branch 6 taken 119 times.
|
119 | return getImpl<detail::ModelObjectList_Impl>()->clone(model); |
| 179 | } | ||
| 180 | |||
| 181 | 1603 | std::vector<ModelObject> ModelObjectList::modelObjects() const { | |
| 182 |
1/1✓ Branch 3 taken 1603 times.
|
1603 | return getImpl<detail::ModelObjectList_Impl>()->modelObjects(); |
| 183 | } | ||
| 184 | |||
| 185 | 76 | unsigned ModelObjectList::size() const { | |
| 186 |
1/1✓ Branch 3 taken 76 times.
|
76 | return getImpl<detail::ModelObjectList_Impl>()->size(); |
| 187 | } | ||
| 188 | |||
| 189 | 700 | bool ModelObjectList::addModelObject(const ModelObject& modelObject) { | |
| 190 |
1/1✓ Branch 3 taken 700 times.
|
700 | return getImpl<detail::ModelObjectList_Impl>()->addModelObject(modelObject); |
| 191 | } | ||
| 192 | |||
| 193 | 61 | bool ModelObjectList::hasModelObject(const ModelObject& modelObject) const { | |
| 194 |
1/1✓ Branch 3 taken 61 times.
|
61 | return getImpl<detail::ModelObjectList_Impl>()->hasModelObject(modelObject); |
| 195 | } | ||
| 196 | |||
| 197 | 159 | void ModelObjectList::removeModelObject(const ModelObject& modelObject) { | |
| 198 |
1/1✓ Branch 3 taken 159 times.
|
159 | getImpl<detail::ModelObjectList_Impl>()->removeModelObject(modelObject); |
| 199 | 159 | } | |
| 200 | |||
| 201 | 74 | void ModelObjectList::removeAllModelObjects() { | |
| 202 |
1/1✓ Branch 3 taken 74 times.
|
74 | getImpl<detail::ModelObjectList_Impl>()->removeAllModelObjects(); |
| 203 | 74 | } | |
| 204 | |||
| 205 | /// @cond | ||
| 206 |
1/1✓ Branch 3 taken 3155 times.
|
3155 | ModelObjectList::ModelObjectList(std::shared_ptr<detail::ModelObjectList_Impl> impl) : ModelObject(std::move(impl)) {} |
| 207 | /// @endcond | ||
| 208 | |||
| 209 | } // namespace model | ||
| 210 | } // namespace openstudio | ||
| 211 |