GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 93.0% 159 / 0 / 171
Functions: 100.0% 18 / 0 / 18
Branches: 71.8% 204 / 0 / 284

src/utilities/data/EndUses.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 "EndUses.hpp"
7 #include "../core/Assert.hpp"
8
9 namespace openstudio {
10
11 struct FuelTypeAttributeSorter
12 {
13 29 bool operator()(const Attribute& leftFuelTypeAttribute, const Attribute& rightFuelTypeAttribute) const {
14
2/2
✓ Branch 1 taken 29 times.
✓ Branch 4 taken 29 times.
29 EndUseFuelType leftFuelType(leftFuelTypeAttribute.name());
15
2/2
✓ Branch 1 taken 29 times.
✓ Branch 4 taken 29 times.
29 EndUseFuelType rightFuelType(rightFuelTypeAttribute.name());
16
2/2
✓ Branch 1 taken 29 times.
✓ Branch 4 taken 29 times.
58 return (leftFuelType.value() < rightFuelType.value());
17 }
18 };
19
20 struct CategoryAttributeSorter
21 {
22 846 bool operator()(const Attribute& leftCategoryAttribute, const Attribute& rightCategoryAttribute) const {
23
2/2
✓ Branch 1 taken 846 times.
✓ Branch 4 taken 846 times.
846 EndUseCategoryType leftCategory(leftCategoryAttribute.name());
24
2/2
✓ Branch 1 taken 846 times.
✓ Branch 4 taken 846 times.
846 EndUseCategoryType rightCategory(rightCategoryAttribute.name());
25
2/2
✓ Branch 1 taken 846 times.
✓ Branch 4 taken 846 times.
1692 return (leftCategory.value() < rightCategory.value());
26 }
27 };
28
29 struct SubCategoryAttributeSorter
30 {
31 2 bool operator()(const Attribute& leftSubCategoryAttribute, const Attribute& rightSubCategoryAttribute) const {
32
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 return (leftSubCategoryAttribute.name() < rightSubCategoryAttribute.name());
33 }
34 };
35
36 20 std::string EndUses::attributeName() {
37
1/1
✓ Branch 1 taken 20 times.
40 return "EndUses";
38 }
39
40 158 std::string EndUses::getUnitsForFuelType(const EndUseFuelType& fuelType) {
41
3/3
✓ Branch 1 taken 158 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 156 times.
158 if (fuelType == EndUseFuelType::Water) {
42
1/1
✓ Branch 1 taken 2 times.
4 return "m3";
43 }
44
1/1
✓ Branch 1 taken 156 times.
312 return "GJ";
45 }
46
47 1 std::vector<EndUseFuelType> EndUses::fuelTypes() {
48 1 std::vector<EndUseFuelType> result;
49
50
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::map<int, std::string> endUseFuelTypes = EndUseFuelType::getNames();
51 1 auto endUseFuelTypesIt = endUseFuelTypes.begin();
52 1 auto endUseFuelTypesItEnd = endUseFuelTypes.end();
53
54
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 1 time.
15 for (; endUseFuelTypesIt != endUseFuelTypesItEnd; ++endUseFuelTypesIt) {
55
2/2
✓ Branch 2 taken 14 times.
✓ Branch 5 taken 14 times.
14 result.push_back(EndUseFuelType(endUseFuelTypesIt->first));
56 }
57
58 2 return result;
59 1 }
60
61 1 std::vector<EndUseCategoryType> EndUses::categories() {
62 1 std::vector<EndUseCategoryType> result;
63
64
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::map<int, std::string> endUseCategoryTypes = EndUseCategoryType::getNames();
65 1 auto endUseCategoryTypesIt = endUseCategoryTypes.begin();
66 1 auto endUseCategoryTypesItEnd = endUseCategoryTypes.end();
67
68
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 1 time.
15 for (; endUseCategoryTypesIt != endUseCategoryTypesItEnd; ++endUseCategoryTypesIt) {
69
2/2
✓ Branch 2 taken 14 times.
✓ Branch 5 taken 14 times.
14 result.push_back(EndUseCategoryType(endUseCategoryTypesIt->first));
70 }
71
72 2 return result;
73 1 }
74
75 5 boost::optional<EndUses> EndUses::fromAttribute(const Attribute& attribute) {
76
4/4
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 4 times.
5 if (attribute.name() != EndUses::attributeName()) {
77
8/8
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 17 taken 1 time.
✓ Branch 20 taken 1 time.
✓ Branch 23 taken 1 time.
1 LOG(Warn, "Cannot create EndUses from Attribute because attribute name is '" << attribute.name() << "'.");
78 1 return boost::none;
79 }
80
81
4/4
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 3 times.
4 if (attribute.valueType() != AttributeValueType::AttributeVector) {
82
9/9
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 20 taken 1 time.
✓ Branch 23 taken 1 time.
✓ Branch 26 taken 1 time.
1 LOG(Warn,
83 "Cannot create EndUses from Attribute because attribute is a " << attribute.valueType().valueDescription() << ", not an AttributeVector.");
84 1 return boost::none;
85 }
86
87
2/3
✓ Branch 1 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
6 if (attribute.units()) {
88 ✗ LOG(Warn, "Cannot create EndUses from Attribute because attribute has units '" << attribute.units().get() << "'.");
89 ✗ return boost::none;
90 }
91
92
3/3
✓ Branch 1 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 3 times.
6 for (const Attribute& fuelTypeAttribute : attribute.valueAsAttributeVector()) {
93
94
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 EndUseFuelType endUseFuelType(fuelTypeAttribute.name());
95
3/4
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
3 if (fuelTypeAttribute.valueType() != AttributeValueType::AttributeVector) {
96 ✗ LOG(Warn, "Cannot create EndUses from Attribute because fuel type attribute '" << fuelTypeAttribute.name() << "' is a "
97 << fuelTypeAttribute.valueType() << ", not an AttributeVector.");
98 ✗ return boost::none;
99 }
100
101
3/3
✓ Branch 1 taken 3 times.
✓ Branch 8 taken 5 times.
✓ Branch 9 taken 3 times.
8 for (const Attribute& categoryAttribute : fuelTypeAttribute.valueAsAttributeVector()) {
102
103
2/2
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
5 EndUseCategoryType endUseCategoryType(categoryAttribute.name());
104
3/4
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5 times.
5 if (categoryAttribute.valueType() != AttributeValueType::AttributeVector) {
105 ✗ LOG(Warn, "Cannot create EndUses from Attribute because category attribute '"
106 << categoryAttribute.name() << "' is a " << categoryAttribute.valueType() << ", not an AttributeVector.");
107 ✗ return boost::none;
108 }
109
110
3/3
✓ Branch 1 taken 5 times.
✓ Branch 8 taken 5 times.
✓ Branch 9 taken 5 times.
10 for (const Attribute& subCategoryAttribute : categoryAttribute.valueAsAttributeVector()) {
111
3/4
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5 times.
5 if (subCategoryAttribute.valueType() != AttributeValueType::Double) {
112 ✗ LOG(Warn, "Cannot create EndUses from Attribute because sub-category attribute '" << categoryAttribute.name() << "' is a "
113 << subCategoryAttribute.valueType() << ", not a Double.");
114 ✗ return boost::none;
115 }
116
2/3
✓ Branch 1 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
5 if (!subCategoryAttribute.units()) {
117 ✗ LOG(Warn, "Cannot create EndUses from Attribute because sub-category attribute '" << categoryAttribute.name() << "' does not have units.");
118 ✗ return boost::none;
119 }
120
4/5
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 5 times.
5 if (subCategoryAttribute.units().get() != getUnitsForFuelType(endUseFuelType)) {
121 ✗ LOG(Warn, "Cannot create EndUses from Attribute because sub-category attribute '"
122 << categoryAttribute.name() << "' has units '" << subCategoryAttribute.units().get() << "', not '"
123 << getUnitsForFuelType(endUseFuelType) << "', as expected for fuel type " << endUseFuelType.valueDescription() << ".");
124 ✗ return boost::none;
125 }
126 5 }
127 3 }
128 3 }
129
130
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 return EndUses(attribute);
131 }
132
133
2/2
✓ Branch 3 taken 15 times.
✓ Branch 6 taken 15 times.
15 EndUses::EndUses() : m_attribute(EndUses::attributeName(), std::vector<openstudio::Attribute>(), boost::none) {}
134
135 3 EndUses::EndUses(const Attribute& attribute) : m_attribute(attribute) {}
136
137 3 Attribute EndUses::attribute() const {
138 3 return m_attribute;
139 }
140
141 153 void EndUses::addEndUse(double value, const EndUseFuelType& fuelType, const EndUseCategoryType& category, const std::string& subCategory) {
142
1/1
✓ Branch 1 taken 153 times.
153 std::string units = this->getUnitsForFuelType(fuelType);
143
144 // Try to find an existing fuelType attribute
145
2/2
✓ Branch 1 taken 153 times.
✓ Branch 4 taken 153 times.
153 boost::optional<Attribute> fuelTypeAttribute = m_attribute.findChildByName(fuelType.valueName());
146
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 125 times.
153 if (!fuelTypeAttribute) {
147 // If you can't, then add a new Attribute to the std::vector of Attribute
148
1/1
✓ Branch 1 taken 28 times.
28 std::vector<Attribute> fuelTypeAttributes = m_attribute.valueAsAttributeVector();
149
3/3
✓ Branch 2 taken 28 times.
✓ Branch 5 taken 28 times.
✓ Branch 8 taken 28 times.
28 fuelTypeAttributes.push_back(Attribute(fuelType.valueName(), std::vector<Attribute>()));
150
1/1
✓ Branch 3 taken 28 times.
28 std::sort(fuelTypeAttributes.begin(), fuelTypeAttributes.end(), FuelTypeAttributeSorter());
151
1/1
✓ Branch 1 taken 28 times.
28 m_attribute.setValue(fuelTypeAttributes);
152
3/3
✓ Branch 1 taken 28 times.
✓ Branch 4 taken 28 times.
✓ Branch 7 taken 28 times.
28 fuelTypeAttribute = m_attribute.findChildByName(fuelType.valueName());
153 28 }
154
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
✗ Branch 4 not taken.
153 OS_ASSERT(fuelTypeAttribute);
155
156 // Within the FuelTypeAttribute, we look if the EndUseCategory actually exists
157
3/3
✓ Branch 1 taken 153 times.
✓ Branch 4 taken 153 times.
✓ Branch 7 taken 153 times.
153 boost::optional<Attribute> categoryAttribute = fuelTypeAttribute->findChildByName(category.valueName());
158
2/2
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 3 times.
153 if (!categoryAttribute) {
159 // Otherwise we add it
160
2/2
✓ Branch 1 taken 150 times.
✓ Branch 4 taken 150 times.
150 std::vector<Attribute> categoryAttributes = fuelTypeAttribute->valueAsAttributeVector();
161
3/3
✓ Branch 2 taken 150 times.
✓ Branch 5 taken 150 times.
✓ Branch 8 taken 150 times.
150 categoryAttributes.push_back(Attribute(category.valueName(), std::vector<Attribute>()));
162
1/1
✓ Branch 3 taken 150 times.
150 std::sort(categoryAttributes.begin(), categoryAttributes.end(), CategoryAttributeSorter());
163
2/2
✓ Branch 1 taken 150 times.
✓ Branch 4 taken 150 times.
150 fuelTypeAttribute->setValue(categoryAttributes);
164
4/4
✓ Branch 1 taken 150 times.
✓ Branch 4 taken 150 times.
✓ Branch 7 taken 150 times.
✓ Branch 10 taken 150 times.
150 categoryAttribute = fuelTypeAttribute->findChildByName(category.valueName());
165 150 }
166
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
✗ Branch 4 not taken.
153 OS_ASSERT(categoryAttribute);
167
168 // Within that category, if we find an existing SubCategory, then we add our value that category
169 153 bool found = false;
170
2/2
✓ Branch 1 taken 153 times.
✓ Branch 4 taken 153 times.
153 std::vector<Attribute> subCategories = categoryAttribute->valueAsAttributeVector();
171
1/1
✓ Branch 1 taken 153 times.
153 std::vector<Attribute> newSubCategories = subCategories;
172
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 153 times.
157 for (unsigned i = 0; i < subCategories.size(); ++i) {
173
3/3
✓ Branch 2 taken 4 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 2 times.
4 if (subCategories[i].name() == subCategory) {
174
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
2 OS_ASSERT(!found);
175
3/3
✓ Branch 2 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 newSubCategories[i] = Attribute(subCategory, subCategories[i].valueAsDouble() + value, subCategories[i].units());
176 2 found = true;
177 }
178 }
179
180 // If we didn't find the proper Subcategory, we create it
181
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 2 times.
153 if (!found) {
182
2/2
✓ Branch 1 taken 151 times.
✓ Branch 4 taken 151 times.
151 newSubCategories.push_back(Attribute(subCategory, value, units));
183
1/1
✓ Branch 3 taken 151 times.
151 std::sort(newSubCategories.begin(), newSubCategories.end(), SubCategoryAttributeSorter());
184 }
185
186
2/2
✓ Branch 1 taken 153 times.
✓ Branch 4 taken 153 times.
153 categoryAttribute->setValue(newSubCategories);
187 153 }
188
189 407 double EndUses::getEndUse(const EndUseFuelType& fuelType, const EndUseCategoryType& category, const std::string& subCategory) const {
190 407 double result = 0;
191
192
2/2
✓ Branch 1 taken 407 times.
✓ Branch 4 taken 407 times.
407 boost::optional<Attribute> fuelTypeAttribute = m_attribute.findChildByName(fuelType.valueName());
193
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 317 times.
407 if (fuelTypeAttribute) {
194
3/3
✓ Branch 1 taken 90 times.
✓ Branch 4 taken 90 times.
✓ Branch 7 taken 90 times.
90 boost::optional<Attribute> categoryAttribute = fuelTypeAttribute->findChildByName(category.valueName());
195
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 42 times.
90 if (categoryAttribute) {
196
2/2
✓ Branch 1 taken 48 times.
✓ Branch 4 taken 48 times.
48 std::vector<Attribute> subCategories = categoryAttribute->valueAsAttributeVector();
197
2/2
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 19 times.
75 for (unsigned i = 0; i < subCategories.size(); ++i) {
198
3/3
✓ Branch 2 taken 56 times.
✓ Branch 6 taken 29 times.
✓ Branch 7 taken 27 times.
56 if (subCategories[i].name() == subCategory) {
199
1/1
✓ Branch 2 taken 29 times.
29 result = subCategories[i].valueAsDouble();
200 29 break;
201 }
202 }
203 48 }
204 90 }
205
206 407 return result;
207 407 }
208
209 909 double EndUses::getEndUse(const EndUseFuelType& fuelType, const EndUseCategoryType& category) const {
210 909 double result = 0;
211
212
2/2
✓ Branch 1 taken 909 times.
✓ Branch 4 taken 909 times.
909 boost::optional<Attribute> fuelTypeAttribute = m_attribute.findChildByName(fuelType.valueName());
213
2/2
✓ Branch 0 taken 377 times.
✓ Branch 1 taken 532 times.
909 if (fuelTypeAttribute) {
214
3/3
✓ Branch 1 taken 377 times.
✓ Branch 4 taken 377 times.
✓ Branch 7 taken 377 times.
377 boost::optional<Attribute> categoryAttribute = fuelTypeAttribute->findChildByName(category.valueName());
215
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 191 times.
377 if (categoryAttribute) {
216
2/2
✓ Branch 1 taken 186 times.
✓ Branch 4 taken 186 times.
186 std::vector<Attribute> subCategories = categoryAttribute->valueAsAttributeVector();
217
2/2
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 186 times.
378 for (unsigned i = 0; i < subCategories.size(); ++i) {
218
1/1
✓ Branch 2 taken 192 times.
192 result += subCategories[i].valueAsDouble();
219 }
220 186 }
221 377 }
222
223 909 return result;
224 909 }
225
226 25 double EndUses::getEndUseByCategory(const EndUseCategoryType& category, const std::string& subCategory) const {
227 25 double result = 0;
228
2/2
✓ Branch 1 taken 25 times.
✓ Branch 4 taken 25 times.
25 std::map<int, std::string> endUseFuelTypes = EndUseFuelType::getNames();
229 25 auto endUseFuelTypesIt = endUseFuelTypes.begin();
230 25 auto endUseFuelTypesItEnd = endUseFuelTypes.end();
231
232
2/2
✓ Branch 1 taken 350 times.
✓ Branch 2 taken 25 times.
375 for (; endUseFuelTypesIt != endUseFuelTypesItEnd; ++endUseFuelTypesIt) {
233
2/2
✓ Branch 2 taken 350 times.
✓ Branch 5 taken 350 times.
350 result += getEndUse(EndUseFuelType(endUseFuelTypesIt->second), category, subCategory);
234 }
235
236 25 return result;
237 25 }
238
239 29 double EndUses::getEndUseByCategory(const EndUseCategoryType& category) const {
240 29 double result = 0;
241
2/2
✓ Branch 1 taken 29 times.
✓ Branch 4 taken 29 times.
29 std::map<int, std::string> endUseFuelTypes = EndUseFuelType::getNames();
242 29 auto endUseFuelTypesIt = endUseFuelTypes.begin();
243 29 auto endUseFuelTypesItEnd = endUseFuelTypes.end();
244
245
2/2
✓ Branch 1 taken 406 times.
✓ Branch 2 taken 29 times.
435 for (; endUseFuelTypesIt != endUseFuelTypesItEnd; ++endUseFuelTypesIt) {
246
2/2
✓ Branch 2 taken 406 times.
✓ Branch 5 taken 406 times.
406 result += getEndUse(EndUseFuelType(endUseFuelTypesIt->second), category);
247 }
248
249 29 return result;
250 29 }
251
252 25 double EndUses::getEndUseByFuelType(const EndUseFuelType& fuelType) const {
253 25 double result = 0;
254
2/2
✓ Branch 1 taken 25 times.
✓ Branch 4 taken 25 times.
25 std::map<int, std::string> endUseCategoryTypes = EndUseCategoryType::getNames();
255 25 auto endUseCategoryTypesIt = endUseCategoryTypes.begin();
256 25 auto endUseCategoryTypesItEnd = endUseCategoryTypes.end();
257
258
2/2
✓ Branch 1 taken 350 times.
✓ Branch 2 taken 25 times.
375 for (; endUseCategoryTypesIt != endUseCategoryTypesItEnd; ++endUseCategoryTypesIt) {
259
2/2
✓ Branch 2 taken 350 times.
✓ Branch 5 taken 350 times.
350 result += getEndUse(fuelType, EndUseCategoryType(endUseCategoryTypesIt->second));
260 }
261
262 25 return result;
263 25 }
264
265 1 std::vector<std::string> EndUses::subCategories() const {
266 1 std::set<std::string> subCategories;
267
3/3
✓ Branch 1 taken 1 time.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 1 time.
4 for (const Attribute& fuelType : m_attribute.valueAsAttributeVector()) {
268
3/3
✓ Branch 1 taken 3 times.
✓ Branch 8 taken 5 times.
✓ Branch 9 taken 3 times.
8 for (const Attribute& category : fuelType.valueAsAttributeVector()) {
269
3/3
✓ Branch 1 taken 5 times.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 5 times.
10 for (const Attribute& subCategory : category.valueAsAttributeVector()) {
270
2/2
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
5 subCategories.insert(subCategory.name());
271 5 }
272 3 }
273 1 }
274
275
1/1
✓ Branch 3 taken 1 time.
3 return {subCategories.begin(), subCategories.end()};
276 1 }
277
278 } // namespace openstudio
279