GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 64.7% 473 / 0 / 731
Functions: 45.0% 36 / 0 / 80
Branches: 57.6% 774 / 0 / 1343

src/utilities/bcl/BCLMeasure.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 "BCLMeasure.hpp"
7 #include "LocalBCL.hpp"
8 #include "BCLFileReference.hpp"
9
10 #include "../core/Filesystem.hpp"
11 #include "../core/PathHelpers.hpp"
12 #include "../core/FilesystemHelpers.hpp"
13 #include "../core/StringHelpers.hpp"
14 #include "../core/FileReference.hpp"
15 #include "../core/Assert.hpp"
16
17 #include <OpenStudio.hxx>
18
19 #include <boost/regex.hpp>
20 #include <boost/algorithm/string.hpp>
21 #include <boost/algorithm/string/split.hpp>
22 #include <boost/algorithm/string/replace.hpp>
23 #include <boost/algorithm/string/classification.hpp>
24
25 #include <src/utilities/embedded_files.hxx>
26
27 #include <array>
28 #include <algorithm>
29 #include <cctype> // std::isalpha, std::isdigit
30 #include <string_view>
31 #include <vector>
32
33 #include <json/json.h>
34
35 namespace openstudio {
36
37 /*****************************************************************************************************************************************************
38 * F I L T E R I N G R U L E S F O R W H A T I S A L L O W E D *
39 *****************************************************************************************************************************************************/
40
41 /***************
42 * R U L E S *
43 ***************/
44
45 static constexpr std::array<std::pair<std::string_view, std::string_view>, 5> rootToUsageTypeMap{{
46 // fileName, usageType
47 {"measure.rb", "script"},
48 {"measure.py", "script"},
49 {"LICENSE.md", "license"},
50 {"README.md", "readme"},
51 {"README.md.erb", "readmeerb"}
52 // ".gitkeep" // assuming .gitkeep outside a subfolder makes zero sense...
53 // "measure.xml" // not included in itself!
54 }};
55
56 static constexpr std::array<std::pair<std::string_view, std::string_view>, 3> approvedSubFolderToUsageMap{{
57 {"docs", "doc"},
58 {"tests", "test"},
59 {"resources", "resource"},
60 }};
61
62 static constexpr std::array<std::string_view, 1> ignoredSubFolders{
63 "tests/output",
64 };
65
66 // TODO: do we want to keep ignoring the docs/ directory?
67 static constexpr std::array<std::string_view, 1> usageTypesIgnoredOnClone{"doc"};
68
69 /******************************************************************
70 * C A L C U L A T E D T H I N G S A N D H E L P E R S *
71 ******************************************************************/
72
73 static constexpr std::array<std::string_view, rootToUsageTypeMap.size()> approvedRootFiles = []() {
74 std::array<std::string_view, rootToUsageTypeMap.size()> result;
75 for (size_t i = 0; i < rootToUsageTypeMap.size(); ++i) {
76 result[i] = rootToUsageTypeMap[i].first;
77 }
78 return result;
79 }();
80
81 static constexpr std::array<std::string_view, approvedSubFolderToUsageMap.size()> approvedSubFolders = []() {
82 std::array<std::string_view, approvedSubFolderToUsageMap.size()> result;
83 for (size_t i = 0; i < approvedSubFolderToUsageMap.size(); ++i) {
84 result[i] = approvedSubFolderToUsageMap[i].first;
85 }
86 return result;
87 }();
88
89 97 bool BCLMeasure::isIgnoredFileName(const std::string& fileName) {
90
6/6
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 86 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 7 times.
97 return (fileName.empty() || (boost::starts_with(fileName, ".") && (fileName != ".gitkeep")));
91 }
92
93
1/1
✓ Branch 1 taken 91 times.
91 bool BCLMeasure::isApprovedFile(const openstudio::path& absoluteFilePath, const openstudio::path& measureDir) {
94
95
1/1
✓ Branch 1 taken 91 times.
91 std::string fileName = toString(absoluteFilePath.filename());
96
2/3
✓ Branch 1 taken 91 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 91 times.
91 if (isIgnoredFileName(fileName)) {
97 ✗ return false;
98 }
99
100
1/1
✓ Branch 1 taken 91 times.
91 openstudio::path parentPath = absoluteFilePath.parent_path();
101
102
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 73 times.
91 if (parentPath == measureDir) {
103
2/2
✓ Branch 3 taken 18 times.
✓ Branch 6 taken 56 times.
186 if (std::find_if(approvedRootFiles.cbegin(), approvedRootFiles.cend(), [&fileName](const auto& sv) { return fileName == std::string(sv); })
104
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 16 times.
18 == approvedRootFiles.cend()) {
105
6/6
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
2 LOG(Warn, absoluteFilePath << " is not one of the approved top level files");
106 2 return false;
107 }
108 } else {
109 73 bool ignored = true;
110 // Keep going back up the directory structure, until you either find one excluded folder or an approved one
111
112 174 auto isPathInContainer = [&measureDir, &parentPath](const auto& container) -> bool {
113 174 return std::find_if(container.begin(), container.end(),
114 [&measureDir, &parentPath](const auto& subFolderPath) {
115 auto fullSubFolderPath = measureDir / toPath(subFolderPath);
116 return parentPath == fullSubFolderPath;
117 })
118 174 != container.end();
119 };
120
121 // Don't go back up further than the measure directory root
122
5/6
✓ Branch 1 taken 94 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 90 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 90 times.
✓ Branch 6 taken 4 times.
188 while (!parentPath.empty() && (parentPath != measureDir)) {
123
3/3
✓ Branch 1 taken 90 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 84 times.
90 if (isPathInContainer(ignoredSubFolders)) {
124 6 ignored = true;
125 6 break;
126
3/3
✓ Branch 1 taken 84 times.
✓ Branch 3 taken 63 times.
✓ Branch 4 taken 21 times.
84 } else if (isPathInContainer(approvedSubFolders)) {
127 63 ignored = false;
128 63 break;
129 }
130
1/1
✓ Branch 1 taken 21 times.
21 parentPath = parentPath.parent_path();
131 }
132
133
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 63 times.
73 if (ignored) {
134
6/6
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
✓ Branch 13 taken 10 times.
✓ Branch 16 taken 10 times.
10 LOG(Warn, absoluteFilePath << " is not inside an approved subfolder.");
135 10 return false;
136 }
137 }
138
139 79 return true;
140 91 }
141 /****************************************************************************************************************************************************/
142
143 46 void BCLMeasure::createDirectory(const openstudio::path& dir) {
144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if (exists(dir)) {
145 ✗ if (!isEmptyDirectory(dir)) {
146 ✗ LOG_AND_THROW("'" << toString(dir) << "' exists but is not an empty directory");
147 }
148 } else {
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if (!openstudio::filesystem::create_directories(dir)) {
150 ✗ LOG_AND_THROW("'" << toString(dir) << "' cannot be created as an empty directory");
151 }
152 }
153 46 }
154
155 14 BCLMeasure::BCLMeasure(const std::string& name, const std::string& className, const openstudio::path& dir, const std::string& taxonomyTag,
156 MeasureType measureType, const std::string& description, const std::string& modelerDescription,
157 14 MeasureLanguage measureLanguage)
158
2/2
✓ Branch 2 taken 14 times.
✓ Branch 5 taken 14 times.
14 : m_directory(openstudio::filesystem::system_complete(dir)), m_bclXML(BCLXMLType::MeasureXML) {
159
160 // Avoid potential problems, since toPath("/path/to/dir/") != toPath("/path/to/dir")
161
1/1
✓ Branch 1 taken 14 times.
14 m_directory.remove_trailing_separator();
162
163
1/1
✓ Branch 1 taken 14 times.
14 openstudio::path measureDocRelativeDir = toPath("docs");
164
1/1
✓ Branch 1 taken 14 times.
14 openstudio::path measureTestRelativeDir = toPath("tests");
165
1/1
✓ Branch 1 taken 14 times.
14 std::string lowerClassName = toUnderscoreCase(className);
166
167
1/1
✓ Branch 1 taken 14 times.
14 createDirectory(m_directory);
168
2/2
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
28 createDirectory(m_directory / measureDocRelativeDir);
169
2/2
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
28 createDirectory(m_directory / measureTestRelativeDir);
170
171 // read in template files
172 14 std::string measureTemplate;
173
1/1
✓ Branch 1 taken 14 times.
14 std::string licenseTemplate = ":/templates/common/LICENSE.md";
174 14 std::string readmeTemplate;
175
3/3
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 6 times.
14 if (measureLanguage == MeasureLanguage::Ruby) {
176
1/1
✓ Branch 1 taken 8 times.
8 readmeTemplate = ":/templates/common/README.md.erb";
177 }
178
1/1
✓ Branch 1 taken 14 times.
14 std::string docTemplate = ":/templates/common/docs/.gitkeep";
179 14 std::string testTemplate;
180
181 14 std::string templateClassName;
182 14 std::vector<BCLMeasureArgument> arguments;
183 14 std::vector<BCLMeasureOutput> outputs;
184 14 std::string testOSM;
185 14 std::string testEPW;
186 14 std::string resourceFile;
187
188 14 openstudio::path testOSMRelativePath;
189 14 openstudio::path testEPWRelativePath;
190 14 openstudio::path resourceFileRelativePath;
191
3/3
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 7 times.
14 if (measureType == MeasureType::ModelMeasure) {
192
3/3
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 2 times.
7 if (measureLanguage == MeasureLanguage::Ruby) {
193
1/1
✓ Branch 1 taken 5 times.
5 measureTemplate = ":/templates/ModelMeasure/measure.rb";
194
1/1
✓ Branch 1 taken 5 times.
5 testTemplate = ":/templates/ModelMeasure/tests/model_measure_test.rb";
195
2/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 } else if (measureLanguage == MeasureLanguage::Python) {
196
1/1
✓ Branch 1 taken 2 times.
2 measureTemplate = ":/templates/ModelMeasure/measure.py";
197
1/1
✓ Branch 1 taken 2 times.
2 testTemplate = ":/templates/ModelMeasure/tests/test_model_measure.py";
198 }
199
1/1
✓ Branch 1 taken 7 times.
7 testOSM = ":/templates/ModelMeasure/tests/example_model.osm";
200
1/1
✓ Branch 1 taken 7 times.
7 templateClassName = "ModelMeasureName";
201
202
1/1
✓ Branch 1 taken 7 times.
7 testOSMRelativePath = toPath("tests/example_model.osm");
203
204
1/1
✓ Branch 1 taken 7 times.
14 std::string argName("space_name");
205
1/1
✓ Branch 1 taken 7 times.
14 std::string argDisplayName("New space name");
206
1/1
✓ Branch 1 taken 7 times.
14 std::string argDescription("This name will be used as the name of the new space.");
207
1/1
✓ Branch 1 taken 7 times.
7 std::string argType("String");
208 ✗ BCLMeasureArgument arg(argName, argDisplayName, argDescription, argType, boost::none, true, false, boost::none, std::vector<std::string>(),
209
2/2
✓ Branch 7 taken 7 times.
✓ Branch 10 taken 7 times.
7 std::vector<std::string>(), boost::none, boost::none);
210
1/1
✓ Branch 1 taken 7 times.
7 arguments.push_back(arg);
211
212
3/3
✓ Branch 6 taken 7 times.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 4 times.
14 } else if (measureType == MeasureType::EnergyPlusMeasure) {
213
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 2 times.
3 if (measureLanguage == MeasureLanguage::Ruby) {
214
1/1
✓ Branch 1 taken 1 time.
1 measureTemplate = ":/templates/EnergyPlusMeasure/measure.rb";
215
1/1
✓ Branch 1 taken 1 time.
1 testTemplate = ":/templates/EnergyPlusMeasure/tests/energyplus_measure_test.rb";
216
2/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 } else if (measureLanguage == MeasureLanguage::Python) {
217
1/1
✓ Branch 1 taken 2 times.
2 measureTemplate = ":/templates/EnergyPlusMeasure/measure.py";
218
1/1
✓ Branch 1 taken 2 times.
2 testTemplate = ":/templates/EnergyPlusMeasure/tests/test_energyplus_measure.py";
219 }
220
1/1
✓ Branch 1 taken 3 times.
3 templateClassName = "EnergyPlusMeasureName";
221
222
1/1
✓ Branch 1 taken 3 times.
6 std::string argName("zone_name");
223
1/1
✓ Branch 1 taken 3 times.
6 std::string argDisplayName("New zone name");
224
1/1
✓ Branch 1 taken 3 times.
6 std::string argDescription("This name will be used as the name of the new zone.");
225
1/1
✓ Branch 1 taken 3 times.
3 std::string argType("String");
226 ✗ BCLMeasureArgument arg(argName, argDisplayName, argDescription, argType, boost::none, true, false, boost::none, std::vector<std::string>(),
227
2/2
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
3 std::vector<std::string>(), boost::none, boost::none);
228
1/1
✓ Branch 1 taken 3 times.
3 arguments.push_back(arg);
229
230
2/3
✓ Branch 6 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 4 times.
7 } else if (measureType == MeasureType::UtilityMeasure) {
231 ✗ measureTemplate = ":/templates/UtilityMeasure/measure.rb";
232 ✗ testTemplate = ":/templates/UtilityMeasure/tests/utility_measure_test.rb";
233 ✗ templateClassName = "UtilityMeasureName";
234
235
2/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 } else if (measureType == MeasureType::ReportingMeasure) {
236
3/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2 times.
4 if (measureLanguage == MeasureLanguage::Ruby) {
237
1/1
✓ Branch 1 taken 2 times.
2 measureTemplate = ":/templates/ReportingMeasure/measure.rb";
238
1/1
✓ Branch 1 taken 2 times.
2 testTemplate = ":/templates/ReportingMeasure/tests/reporting_measure_test.rb";
239
1/1
✓ Branch 1 taken 2 times.
2 resourceFile = ":/templates/ReportingMeasure/resources/report.html.in";
240
2/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 } else if (measureLanguage == MeasureLanguage::Python) {
241
1/1
✓ Branch 1 taken 2 times.
2 measureTemplate = ":/templates/ReportingMeasure/measure.py";
242
1/1
✓ Branch 1 taken 2 times.
2 testTemplate = ":/templates/ReportingMeasure/tests/test_reporting_measure.py";
243 }
244
1/1
✓ Branch 1 taken 4 times.
4 testOSM = ":/templates/ReportingMeasure/tests/example_model.osm";
245
1/1
✓ Branch 1 taken 4 times.
4 testEPW = ":/templates/ReportingMeasure/tests/USA_CO_Golden-NREL.724666_TMY3.epw";
246
1/1
✓ Branch 1 taken 4 times.
4 templateClassName = "ReportingMeasureName";
247
248
1/1
✓ Branch 1 taken 4 times.
4 testOSMRelativePath = toPath("tests/example_model.osm");
249
1/1
✓ Branch 1 taken 4 times.
4 testEPWRelativePath = toPath("tests/USA_CO_Golden-NREL.724666_TMY3.epw");
250
251
3/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
8 createDirectory(m_directory / toPath("resources"));
252
1/1
✓ Branch 1 taken 4 times.
4 resourceFileRelativePath = toPath("resources/report.html.in");
253 }
254
255 14 std::string measureString;
256
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (!measureTemplate.empty()) {
257
1/1
✓ Branch 1 taken 14 times.
14 measureString = ::openstudio::embedded_files::getFileAsString(measureTemplate);
258
1/1
✓ Branch 1 taken 14 times.
14 boost::replace_all(measureString, templateClassName, className);
259
1/1
✓ Branch 1 taken 14 times.
28 std::string templateName = "NAME_TEXT";
260
1/1
✓ Branch 1 taken 14 times.
28 std::string templateDescription = "DESCRIPTION_TEXT";
261
1/1
✓ Branch 1 taken 14 times.
14 std::string templateModelerDescription = "MODELER_DESCRIPTION_TEXT";
262
1/1
✓ Branch 1 taken 14 times.
14 boost::replace_all(measureString, templateName, name);
263
1/1
✓ Branch 1 taken 14 times.
14 boost::replace_all(measureString, templateModelerDescription, modelerDescription); // put first as this includes description tag
264
1/1
✓ Branch 1 taken 14 times.
14 boost::replace_all(measureString, templateDescription, description);
265 14 }
266
267 14 std::string licenseString;
268 // cppcheck-suppress knownConditionTrueFalse
269
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (!licenseTemplate.empty()) {
270
1/1
✓ Branch 1 taken 14 times.
14 licenseString = ::openstudio::embedded_files::getFileAsString(licenseTemplate);
271 }
272
273 14 std::string readmeString;
274 // cppcheck-suppress knownConditionTrueFalse
275
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 6 times.
14 if (!readmeTemplate.empty()) {
276
1/1
✓ Branch 1 taken 8 times.
8 readmeString = ::openstudio::embedded_files::getFileAsString(readmeTemplate);
277 }
278
279 14 std::string docString;
280 // cppcheck-suppress knownConditionTrueFalse
281
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (!docTemplate.empty()) {
282
1/1
✓ Branch 1 taken 14 times.
14 docString = ::openstudio::embedded_files::getFileAsString(docTemplate);
283 }
284
285 14 std::string testString;
286
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (!testTemplate.empty()) {
287
2/2
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
14 testString = openstudio::replace(::openstudio::embedded_files::getFileAsString(testTemplate), templateClassName, className);
288 }
289
290 14 std::string testOSMString;
291
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 3 times.
14 if (!testOSM.empty()) {
292
1/1
✓ Branch 1 taken 11 times.
11 testOSMString = ::openstudio::embedded_files::getFileAsString(testOSM);
293 }
294
295 14 std::string testEPWString;
296
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 10 times.
14 if (!testEPW.empty()) {
297
1/1
✓ Branch 1 taken 4 times.
4 testEPWString = ::openstudio::embedded_files::getFileAsString(testEPW);
298 }
299
300 14 std::string resourceFileString;
301
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 12 times.
14 if (!resourceFile.empty()) {
302
1/1
✓ Branch 1 taken 2 times.
2 resourceFileString = ::openstudio::embedded_files::getFileAsString(resourceFile);
303 }
304
305 // write files
306 81 auto writeFile = [this](const openstudio::path& relativeFilePath, const std::string& usageType, const std::string& contentText,
307 bool addVersion = false) {
308
1/1
✓ Branch 1 taken 81 times.
162 openstudio::path absoluteFilePath = m_directory / relativeFilePath;
309
1/1
✓ Branch 1 taken 81 times.
81 openstudio::filesystem::ofstream file(absoluteFilePath, std::ios_base::binary);
310
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
81 if (!file.is_open()) {
311 ✗ LOG_AND_THROW("Cannot write " << usageType << " file to '" << toString(absoluteFilePath) << "'");
312 }
313
1/1
✓ Branch 1 taken 81 times.
81 file << contentText;
314
1/1
✓ Branch 1 taken 81 times.
81 file.close();
315
316
1/1
✓ Branch 1 taken 81 times.
81 BCLFileReference fileRef(m_directory, relativeFilePath, true);
317
1/1
✓ Branch 1 taken 81 times.
81 fileRef.setUsageType(usageType);
318
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 67 times.
81 if (addVersion) {
319
2/2
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
14 fileRef.setSoftwareProgramVersion(openStudioVersion());
320 }
321
1/1
✓ Branch 1 taken 81 times.
81 m_bclXML.addFile(fileRef);
322 81 };
323
324
3/3
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 6 times.
14 if (measureLanguage == MeasureLanguage::Ruby) {
325
3/3
✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
16 writeFile(toPath("measure.rb"), "script", measureString, true);
326
5/5
✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
✓ Branch 10 taken 8 times.
✓ Branch 13 taken 8 times.
24 writeFile(measureTestRelativeDir / toPath(lowerClassName + "_test.rb"), "test", testString);
327
3/3
✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
16 writeFile(toPath("README.md.erb"), "readmeerb", readmeString);
328
2/3
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 } else if (measureLanguage == MeasureLanguage::Python) {
329
3/3
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
12 writeFile(toPath("measure.py"), "script", measureString, true);
330
6/6
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
✓ Branch 16 taken 6 times.
18 writeFile(measureTestRelativeDir / toPath("test_" + lowerClassName + ".py"), "test", testString);
331 }
332
3/3
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
✓ Branch 7 taken 14 times.
28 writeFile(toPath("LICENSE.md"), "license", licenseString);
333
4/4
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
✓ Branch 7 taken 14 times.
✓ Branch 10 taken 14 times.
28 writeFile(measureDocRelativeDir / toPath(".gitkeep"), "doc", docString);
334
335
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 3 times.
14 if (!testOSMString.empty()) {
336
2/2
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
22 writeFile(testOSMRelativePath, "test", testOSMString);
337 }
338
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 10 times.
14 if (!testEPWString.empty()) {
339
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
8 writeFile(testEPWRelativePath, "test", testEPWString);
340 }
341
342
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 12 times.
14 if (!resourceFileString.empty()) {
343
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
4 writeFile(resourceFileRelativePath, "resource", resourceFileString);
344 }
345
346 // set rest of measure fields
347
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.setName(lowerClassName);
348
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.setDisplayName(name);
349
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.setClassName(className);
350
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.setDescription(description);
351
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.setModelerDescription(modelerDescription);
352
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.setArguments(arguments);
353
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.setOutputs(outputs);
354
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.addTag(taxonomyTag);
355
1/1
✓ Branch 1 taken 14 times.
14 this->setMeasureType(measureType);
356
1/1
✓ Branch 1 taken 14 times.
14 this->setMeasureLanguage(measureLanguage);
357
358 // reset the checksum to trigger update even if nothing has changed
359
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.resetXMLChecksum();
360
361
2/2
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
28 openstudio::path measureXMLPath = m_directory / toPath("measure.xml");
362
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.saveAs(measureXMLPath);
363 14 }
364
365
2/2
✓ Branch 2 taken 465 times.
✓ Branch 5 taken 465 times.
465 BCLMeasure::BCLMeasure(const openstudio::path& dir) : m_directory(openstudio::filesystem::system_complete(dir)), m_bclXML(BCLXMLType::MeasureXML) {
366 // Avoid potential problems, since toPath("/path/to/dir/") != toPath("/path/to/dir")
367
1/1
✓ Branch 1 taken 465 times.
465 m_directory.remove_trailing_separator();
368
369
2/2
✓ Branch 1 taken 465 times.
✓ Branch 4 taken 465 times.
930 openstudio::path xmlPath = m_directory / toPath("measure.xml");
370
1/1
✓ Branch 1 taken 465 times.
465 boost::optional<BCLXML> bclXML = BCLXML::load(xmlPath);
371
372
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 460 times.
465 if (!bclXML) {
373
19/19
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
✓ Branch 10 taken 5 times.
✓ Branch 13 taken 5 times.
✓ Branch 17 taken 5 times.
✓ Branch 20 taken 5 times.
✓ Branch 23 taken 5 times.
✓ Branch 29 taken 5 times.
✓ Branch 32 taken 5 times.
✓ Branch 35 taken 5 times.
✓ Branch 38 taken 5 times.
✓ Branch 41 taken 5 times.
✓ Branch 44 taken 5 times.
✓ Branch 47 taken 5 times.
✓ Branch 50 taken 5 times.
✓ Branch 53 taken 5 times.
✓ Branch 58 taken 5 times.
✓ Branch 61 taken 5 times.
5 LOG_AND_THROW("'" << toString(dir) << "' is not a measure");
374 }
375
376
2/2
✓ Branch 1 taken 460 times.
✓ Branch 4 taken 460 times.
460 m_bclXML = *bclXML;
377
378 // check for required attributes
379
2/2
✓ Branch 1 taken 460 times.
✓ Branch 4 taken 460 times.
920 std::vector<Attribute> measureTypes = m_bclXML.getAttributes("Measure Type");
380
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 459 times.
460 if (measureTypes.empty()) {
381
19/19
✓ 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.
✓ Branch 29 taken 1 time.
✓ Branch 32 taken 1 time.
✓ Branch 35 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
✓ Branch 44 taken 1 time.
✓ Branch 47 taken 1 time.
✓ Branch 50 taken 1 time.
✓ Branch 53 taken 1 time.
✓ Branch 58 taken 1 time.
✓ Branch 61 taken 1 time.
1 LOG_AND_THROW("'" << toString(dir) << "' is missing the required attribute \"Measure Type\"");
382
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 459 times.
459 } else if (measureTypes.size() > 1) {
383 ✗ LOG_AND_THROW("'" << toString(dir) << "' has multiple copies of required attribute \"Measure Type\"");
384
4/4
✓ Branch 1 taken 459 times.
✓ Branch 5 taken 459 times.
✓ Branch 8 taken 1 time.
✓ Branch 9 taken 458 times.
459 } else if (measureTypes[0].valueType() != AttributeValueType::String) {
385
19/19
✓ 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.
✓ Branch 29 taken 1 time.
✓ Branch 32 taken 1 time.
✓ Branch 35 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
✓ Branch 44 taken 1 time.
✓ Branch 47 taken 1 time.
✓ Branch 50 taken 1 time.
✓ Branch 53 taken 1 time.
✓ Branch 58 taken 1 time.
✓ Branch 61 taken 1 time.
1 LOG_AND_THROW("'" << toString(dir) << "' has wrong type for required attribute \"Measure Type\"");
386 }
387
388
2/2
✓ Branch 1 taken 458 times.
✓ Branch 4 taken 458 times.
916 std::vector<Attribute> measureLanguages = m_bclXML.getAttributes("Measure Language");
389 458 boost::optional<MeasureLanguage> measureLanguage_;
390
2/2
✓ Branch 1 taken 229 times.
✓ Branch 2 taken 229 times.
458 if (measureLanguages.empty()) {
391 // No-op
392
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 228 times.
229 } else if (measureLanguages.size() > 1) {
393
19/19
✓ 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.
✓ Branch 29 taken 1 time.
✓ Branch 32 taken 1 time.
✓ Branch 35 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
✓ Branch 44 taken 1 time.
✓ Branch 47 taken 1 time.
✓ Branch 50 taken 1 time.
✓ Branch 53 taken 1 time.
✓ Branch 58 taken 1 time.
✓ Branch 61 taken 1 time.
1 LOG_AND_THROW("'" << toString(dir) << "' has multiple copies of required attribute \"Measure Language\"");
394
3/4
✓ Branch 1 taken 228 times.
✓ Branch 5 taken 228 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 228 times.
228 } else if (measureLanguages[0].valueType() != AttributeValueType::String) {
395 ✗ LOG_AND_THROW("'" << toString(dir) << "' has wrong type for required attribute \"Measure Language\"");
396 } else {
397
3/3
✓ Branch 2 taken 228 times.
✓ Branch 5 taken 228 times.
✓ Branch 8 taken 228 times.
228 measureLanguage_ = MeasureLanguage(measureLanguages[0].valueAsString());
398 }
399
400 // Don't allow having both a ruby measure.rb and a python measure.py listed in the same XML
401
2/2
✓ Branch 1 taken 457 times.
✓ Branch 4 taken 457 times.
914 const bool hasRuby = m_bclXML.hasFile(m_directory / "measure.rb");
402
2/2
✓ Branch 1 taken 457 times.
✓ Branch 4 taken 457 times.
914 const bool hasPython = m_bclXML.hasFile(m_directory / "measure.py");
403
4/4
✓ Branch 0 taken 396 times.
✓ Branch 1 taken 61 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 395 times.
457 if (hasRuby && hasPython) {
404
19/19
✓ 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.
✓ Branch 29 taken 1 time.
✓ Branch 32 taken 1 time.
✓ Branch 35 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
✓ Branch 44 taken 1 time.
✓ Branch 47 taken 1 time.
✓ Branch 50 taken 1 time.
✓ Branch 53 taken 1 time.
✓ Branch 58 taken 1 time.
✓ Branch 61 taken 1 time.
1 LOG_AND_THROW("'" << toString(dir) << "' has both measure.rb and measure.py, and they cannot be used at the same time.");
405
2/2
✓ Branch 0 taken 395 times.
✓ Branch 1 taken 61 times.
456 } else if (hasRuby) {
406
8/8
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 227 times.
✓ Branch 3 taken 168 times.
✓ Branch 6 taken 168 times.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 167 times.
✓ Branch 11 taken 1 time.
✓ Branch 12 taken 394 times.
395 if (measureLanguage_ && measureLanguage_.get() != MeasureLanguage::Ruby) {
407
27/27
✓ 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 19 taken 1 time.
✓ Branch 22 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 30 taken 1 time.
✓ Branch 33 taken 1 time.
✓ Branch 36 taken 1 time.
✓ Branch 42 taken 1 time.
✓ Branch 45 taken 1 time.
✓ Branch 48 taken 1 time.
✓ Branch 51 taken 1 time.
✓ Branch 54 taken 1 time.
✓ Branch 57 taken 1 time.
✓ Branch 60 taken 1 time.
✓ Branch 63 taken 1 time.
✓ Branch 66 taken 1 time.
✓ Branch 69 taken 1 time.
✓ Branch 72 taken 1 time.
✓ Branch 75 taken 1 time.
✓ Branch 78 taken 1 time.
✓ Branch 84 taken 1 time.
✓ Branch 87 taken 1 time.
1 LOG_AND_THROW("'" << toString(dir) << "' has a measure.rb; but \"Measure Language\" is not 'Ruby', it's '" << measureLanguage_->valueName()
408 << "'.");
409 }
410
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1 time.
61 } else if (hasPython) {
411
8/8
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 59 times.
✓ Branch 6 taken 59 times.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 58 times.
✓ Branch 11 taken 1 time.
✓ Branch 12 taken 59 times.
60 if (measureLanguage_ && measureLanguage_.get() != MeasureLanguage::Python) {
412
27/27
✓ 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 19 taken 1 time.
✓ Branch 22 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 30 taken 1 time.
✓ Branch 33 taken 1 time.
✓ Branch 36 taken 1 time.
✓ Branch 42 taken 1 time.
✓ Branch 45 taken 1 time.
✓ Branch 48 taken 1 time.
✓ Branch 51 taken 1 time.
✓ Branch 54 taken 1 time.
✓ Branch 57 taken 1 time.
✓ Branch 60 taken 1 time.
✓ Branch 63 taken 1 time.
✓ Branch 66 taken 1 time.
✓ Branch 69 taken 1 time.
✓ Branch 72 taken 1 time.
✓ Branch 75 taken 1 time.
✓ Branch 78 taken 1 time.
✓ Branch 84 taken 1 time.
✓ Branch 87 taken 1 time.
1 LOG_AND_THROW("'" << toString(dir) << "' has a measure.py; but \"Measure Language\" is not 'Python', it's '" << measureLanguage_->valueName()
413 << "'.");
414 }
415 } else {
416
19/19
✓ 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.
✓ Branch 29 taken 1 time.
✓ Branch 32 taken 1 time.
✓ Branch 35 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
✓ Branch 44 taken 1 time.
✓ Branch 47 taken 1 time.
✓ Branch 50 taken 1 time.
✓ Branch 53 taken 1 time.
✓ Branch 58 taken 1 time.
✓ Branch 61 taken 1 time.
1 LOG_AND_THROW("'" << toString(dir) << "' has neither measure.rb nor measure.py");
417 }
418
419
3/3
✓ Branch 1 taken 453 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 449 times.
453 if (m_bclXML.xmlChecksum().empty()) {
420 // this will set the checksum but not increment the version id
421
1/1
✓ Branch 1 taken 4 times.
4 m_bclXML.checkForUpdatesXML();
422 }
423
424 // check that opening the file did not modify it
425
4/5
✓ Branch 1 taken 453 times.
✓ Branch 4 taken 453 times.
✓ Branch 7 taken 453 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 453 times.
453 if (m_bclXML.versionId() != bclXML->versionId()) {
426 // DLM: should this be an assert instead?
427 ✗ LOG_AND_THROW("Measure version_id is no longer valid");
428 }
429 518 }
430
431 4 std::string BCLMeasure::makeClassName(const std::string& name) {
432
1/1
✓ Branch 1 taken 4 times.
4 std::string str(name);
433
434 4 bool startsWithLetter = false;
435
2/2
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 4 times.
49 for (std::size_t i = 0; i < str.size(); ++i) {
436
6/6
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 25 times.
✓ Branch 4 taken 19 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 19 times.
✓ Branch 7 taken 26 times.
45 if (!((std::isalpha(str[i]) != 0) || (std::isdigit(str[i]) != 0))) {
437 19 str[i] = ' ';
438
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24 times.
26 } else if (i == 0) {
439 2 startsWithLetter = (std::isalpha(str[i]) != 0);
440 }
441 }
442
443 4 std::string result;
444 4 std::vector<std::string> parts;
445
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
4 boost::split(parts, str, boost::is_any_of(" "));
446
2/2
✓ Branch 5 taken 23 times.
✓ Branch 6 taken 4 times.
27 for (auto& part : parts) {
447
1/1
✓ Branch 1 taken 23 times.
23 boost::replace_all(part, " ", "");
448
1/1
✓ Branch 1 taken 23 times.
23 boost::replace_all(part, "\n", "");
449
1/1
✓ Branch 1 taken 23 times.
23 boost::replace_all(part, "\r", "");
450
1/1
✓ Branch 1 taken 23 times.
23 boost::replace_all(part, "\t", "");
451
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 18 times.
23 if (!part.empty()) {
452 5 part[0] = toupper(part[0]);
453
1/1
✓ Branch 1 taken 5 times.
5 result.append(part);
454 }
455 }
456
457
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 3 times.
4 if (result.empty()) {
458
4/4
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
3 result = std::string("A") + openstudio::toString(createUUID());
459
1/1
✓ Branch 1 taken 1 time.
1 boost::replace_all(result, "{", "");
460
1/1
✓ Branch 1 taken 1 time.
1 boost::replace_all(result, "}", "");
461
1/1
✓ Branch 1 taken 1 time.
1 boost::replace_all(result, "-", "");
462
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 } else if (!startsWithLetter) {
463
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
4 result = std::string("A") + result;
464 }
465
466 8 return result;
467 4 }
468
469 403 boost::optional<BCLMeasure> BCLMeasure::load(const openstudio::path& dir) {
470 403 boost::optional<BCLMeasure> result;
471 try {
472
2/2
✓ Branch 1 taken 398 times.
✓ Branch 4 taken 398 times.
403 result = BCLMeasure(dir);
473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 } catch (const std::exception&) {
474 5 }
475 403 return result;
476 ✗ }
477
478 ✗ std::vector<std::string> BCLMeasure::suggestedIntendedSoftwareTools() {
479 ✗ std::vector<std::string> result{"Apply Measure Now", "OpenStudio Application", "Parametric Analysis Tool", "Analysis Spreadsheet"};
480 ✗ return result;
481 ✗ }
482
483 ✗ std::vector<std::string> BCLMeasure::suggestedIntendedUseCases() {
484 std::vector<std::string> result{"Model Articulation", "Calibration", "Sensitivity Analysis",
485 ✗ "New Construction EE", "Retrofit EE", "Automatic Report Generation"};
486
487 ✗ return result;
488 ✗ }
489
490 262 std::vector<std::string> BCLMeasure::suggestedFirstLevelTaxonomyTerms() {
491 std::vector<std::string> result{
492 "Envelope", "Electric Lighting", "Equipment", "People", "HVAC", "Refrigeration", "Service Water Heating", "Onsite Power Generation",
493
3/5
✓ Branch 1 taken 262 times.
✓ Branch 3 taken 2882 times.
✓ Branch 4 taken 262 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
3406 "Whole Building", "Economics", "Reporting"};
494
495 262 return result;
496 786 }
497
498 2882 std::vector<std::string> BCLMeasure::suggestedSecondLevelTaxonomyTerms(const std::string& firstLevelTaxonomyTerm) {
499 2882 std::vector<std::string> result;
500
1/1
✓ Branch 1 taken 2882 times.
2882 result.reserve(8);
501
502
3/3
✓ Branch 1 taken 2882 times.
✓ Branch 3 taken 262 times.
✓ Branch 4 taken 2620 times.
2882 if (firstLevelTaxonomyTerm == "Envelope") {
503
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Form");
504
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Opaque");
505
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Fenestration");
506
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Construction Sets");
507
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Daylighting");
508
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Infiltration");
509
3/3
✓ Branch 1 taken 2620 times.
✓ Branch 3 taken 262 times.
✓ Branch 4 taken 2358 times.
2620 } else if (firstLevelTaxonomyTerm == "Electric Lighting") {
510
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Electric Lighting Controls");
511
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Lighting Equipment");
512
3/3
✓ Branch 1 taken 2358 times.
✓ Branch 3 taken 262 times.
✓ Branch 4 taken 2096 times.
2358 } else if (firstLevelTaxonomyTerm == "Equipment") {
513
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Equipment Controls");
514
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Electric Equipment");
515
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Gas Equipment");
516
3/3
✓ Branch 1 taken 2096 times.
✓ Branch 3 taken 262 times.
✓ Branch 4 taken 1834 times.
2096 } else if (firstLevelTaxonomyTerm == "People") {
517
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Characteristics");
518
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("People Schedules");
519
3/3
✓ Branch 1 taken 1834 times.
✓ Branch 3 taken 262 times.
✓ Branch 4 taken 1572 times.
1834 } else if (firstLevelTaxonomyTerm == "HVAC") {
520
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("HVAC Controls");
521
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Heating");
522
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Cooling");
523
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Heat Rejection");
524
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Energy Recovery");
525
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Distribution");
526
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Ventilation");
527
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Whole System");
528
3/3
✓ Branch 1 taken 1572 times.
✓ Branch 3 taken 262 times.
✓ Branch 4 taken 1310 times.
1572 } else if (firstLevelTaxonomyTerm == "Refrigeration") {
529
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Refrigeration Controls");
530
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Cases and Walkins");
531
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Compressors");
532
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Condensers");
533
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Heat Reclaim");
534
3/3
✓ Branch 1 taken 1310 times.
✓ Branch 3 taken 262 times.
✓ Branch 4 taken 1048 times.
1310 } else if (firstLevelTaxonomyTerm == "Service Water Heating") {
535
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Water Use");
536
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Water Heating");
537
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Distribution");
538
3/3
✓ Branch 1 taken 1048 times.
✓ Branch 3 taken 262 times.
✓ Branch 4 taken 786 times.
1048 } else if (firstLevelTaxonomyTerm == "Onsite Power Generation") {
539
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Photovoltaic");
540
3/3
✓ Branch 1 taken 786 times.
✓ Branch 3 taken 262 times.
✓ Branch 4 taken 524 times.
786 } else if (firstLevelTaxonomyTerm == "Whole Building") {
541
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Whole Building Schedules");
542
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Space Types");
543
3/3
✓ Branch 1 taken 524 times.
✓ Branch 3 taken 262 times.
✓ Branch 4 taken 262 times.
524 } else if (firstLevelTaxonomyTerm == "Economics") {
544
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Life Cycle Cost Analysis");
545
2/3
✓ Branch 1 taken 262 times.
✓ Branch 3 taken 262 times.
✗ Branch 4 not taken.
262 } else if (firstLevelTaxonomyTerm == "Reporting") {
546
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("QAQC");
547
1/1
✓ Branch 1 taken 262 times.
262 result.emplace_back("Troubleshooting");
548 }
549
550 2882 return result;
551 ✗ }
552
553 ✗ std::vector<BCLMeasure> BCLMeasure::getMeasuresInDir(const openstudio::path& dir) {
554 ✗ LOG(Debug, "Loading measures in path: " << openstudio::toString(dir));
555 ✗ std::vector<BCLMeasure> result;
556
557 ✗ if (!exists(dir) || !is_directory(dir)) {
558 ✗ LOG(Debug, "Error Loading measures in path: " << openstudio::toString(dir));
559 ✗ return result;
560 }
561
562 ✗ if (isNetworkPath(dir) && !isNetworkPathAvailable(dir)) {
563 ✗ LOG(Debug, "Error Loading measures in unavailable network location: " << openstudio::toString(dir));
564 ✗ return result;
565 }
566
567 try {
568 ✗ openstudio::filesystem::directory_iterator endit; // default construction yields past-the-end
569 ✗ openstudio::filesystem::directory_iterator it(dir);
570 ✗ for (; it != endit; ++it) {
571 ✗ if (is_directory(it->status())) {
572 ✗ LOG(Debug, "Attempting to load measure: " << openstudio::toString(it->path()));
573 ✗ boost::optional<BCLMeasure> measure = load(it->path());
574 ✗ if (measure) {
575 ✗ LOG(Debug, "Success loading measure: " << openstudio::toString(it->path()));
576 ✗ result.push_back(*measure);
577 }
578 ✗ }
579 }
580 ✗ } catch (const std::exception&) {
581 ✗ }
582
583 ✗ return result;
584 ✗ }
585
586 6 openstudio::path BCLMeasure::directory() const {
587 6 return m_directory;
588 }
589
590 ✗ boost::optional<std::string> BCLMeasure::error() const {
591 ✗ return m_bclXML.error();
592 }
593
594 60 std::string BCLMeasure::uid() const {
595 60 return m_bclXML.uid();
596 }
597
598 ✗ UUID BCLMeasure::uuid() const {
599 ✗ return toUUID("{" + uid() + "}");
600 }
601
602 60 std::string BCLMeasure::versionId() const {
603 60 return m_bclXML.versionId();
604 }
605
606 ✗ UUID BCLMeasure::versionUUID() const {
607 ✗ return toUUID("{" + versionId() + "}");
608 }
609
610 63 boost::optional<DateTime> BCLMeasure::versionModified() const {
611 63 return m_bclXML.versionModified();
612 }
613
614 60 std::string BCLMeasure::xmlChecksum() const {
615 60 return m_bclXML.xmlChecksum();
616 }
617
618 61 std::string BCLMeasure::name() const {
619 61 return m_bclXML.name();
620 }
621
622 61 std::string BCLMeasure::displayName() const {
623 61 return m_bclXML.displayName();
624 }
625
626 216 std::string BCLMeasure::className() const {
627 216 return m_bclXML.className();
628 }
629
630 ✗ std::string BCLMeasure::description() const {
631 ✗ return m_bclXML.description();
632 }
633
634 ✗ std::string BCLMeasure::modelerDescription() const {
635 ✗ return m_bclXML.modelerDescription();
636 }
637
638 ✗ std::vector<BCLMeasureArgument> BCLMeasure::arguments() const {
639 ✗ return m_bclXML.arguments();
640 }
641
642 ✗ std::vector<BCLMeasureOutput> BCLMeasure::outputs() const {
643 ✗ return m_bclXML.outputs();
644 }
645
646 ✗ std::vector<std::string> BCLMeasure::tags() const {
647 ✗ return m_bclXML.tags();
648 }
649
650 ✗ std::vector<Attribute> BCLMeasure::attributes() const {
651 ✗ return m_bclXML.attributes();
652 }
653
654 13 std::vector<BCLFileReference> BCLMeasure::files() const {
655 13 return m_bclXML.files();
656 }
657
658 ✗ void BCLMeasure::setError(const std::string& error) {
659 ✗ m_bclXML.setError(error);
660 ✗ }
661
662 ✗ void BCLMeasure::resetError() {
663 ✗ m_bclXML.resetError();
664 ✗ }
665
666 ✗ void BCLMeasure::setName(const std::string& name) {
667 ✗ m_bclXML.setName(name);
668 ✗ }
669
670 ✗ void BCLMeasure::setDisplayName(const std::string& displayName) {
671 ✗ m_bclXML.setDisplayName(displayName);
672 ✗ }
673
674 ✗ void BCLMeasure::setClassName(const std::string& className) {
675 ✗ m_bclXML.setClassName(className);
676 ✗ }
677
678 ✗ void BCLMeasure::setDescription(const std::string& description) {
679 ✗ m_bclXML.setDescription(description);
680 ✗ }
681
682 ✗ void BCLMeasure::setModelerDescription(const std::string& modelerDescription) {
683 ✗ m_bclXML.setModelerDescription(modelerDescription);
684 ✗ }
685
686 ✗ void BCLMeasure::setArguments(const std::vector<BCLMeasureArgument>& arguments) {
687 ✗ m_bclXML.setArguments(arguments);
688 ✗ }
689
690 ✗ void BCLMeasure::setOutputs(const std::vector<BCLMeasureOutput>& outputs) {
691 ✗ m_bclXML.setOutputs(outputs);
692 ✗ }
693
694 55 std::string BCLMeasure::taxonomyTag() const {
695 55 std::string result;
696
1/1
✓ Branch 1 taken 55 times.
55 std::vector<std::string> tags = m_bclXML.tags();
697
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 if (!tags.empty()) {
698
1/1
✓ Branch 2 taken 55 times.
55 result = tags[0];
699 }
700 110 return result;
701 55 }
702
703 ✗ void BCLMeasure::setTaxonomyTag(const std::string& taxonomyTag) {
704 ✗ m_bclXML.clearTags();
705 ✗ m_bclXML.addTag(taxonomyTag);
706 ✗ }
707
708 548 MeasureType BCLMeasure::measureType() const {
709
2/2
✓ Branch 1 taken 548 times.
✓ Branch 4 taken 548 times.
1096 std::vector<Attribute> measureTypes = m_bclXML.getAttributes("Measure Type");
710
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 548 times.
✗ Branch 4 not taken.
548 OS_ASSERT(measureTypes.size() == 1);
711
2/2
✓ Branch 2 taken 548 times.
✓ Branch 5 taken 548 times.
1096 return MeasureType(measureTypes[0].valueAsString());
712 548 }
713
714 14 void BCLMeasure::setMeasureType(const MeasureType& measureType) {
715
1/1
✓ Branch 1 taken 14 times.
14 const std::string attributeName("Measure Type");
716
2/2
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
14 Attribute attribute(attributeName, measureType.valueName());
717
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.removeAttributes(attributeName);
718
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.addAttribute(attribute);
719 14 }
720
721 111 MeasureLanguage BCLMeasure::measureLanguage() const {
722
2/2
✓ Branch 1 taken 111 times.
✓ Branch 4 taken 111 times.
222 std::vector<Attribute> measureLanguages = m_bclXML.getAttributes("Measure Language");
723
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 69 times.
111 if (measureLanguages.empty()) {
724 // This was added at 3.5.0, when Python measures came in, so fall back to Ruby if not there
725
1/1
✓ Branch 1 taken 42 times.
42 return MeasureLanguage::Ruby;
726 }
727
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 69 times.
✗ Branch 4 not taken.
69 OS_ASSERT(measureLanguages.size() == 1);
728
2/2
✓ Branch 2 taken 69 times.
✓ Branch 5 taken 69 times.
69 return MeasureLanguage(measureLanguages[0].valueAsString());
729 111 }
730
731 14 void BCLMeasure::setMeasureLanguage(const MeasureLanguage& measureLanguage) {
732
1/1
✓ Branch 1 taken 14 times.
14 const std::string attributeName("Measure Language");
733
2/2
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
14 Attribute attribute(attributeName, measureLanguage.valueName());
734
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.removeAttributes(attributeName);
735
1/1
✓ Branch 1 taken 14 times.
14 m_bclXML.addAttribute(attribute);
736 14 }
737
738 ✗ std::vector<std::string> BCLMeasure::intendedSoftwareTools() const {
739 ✗ std::vector<std::string> result;
740 ✗ std::vector<Attribute> attributes = m_bclXML.getAttributes("Intended Software Tool");
741 ✗ result.reserve(attributes.size());
742 ✗ std::transform(attributes.cbegin(), attributes.cend(), std::back_inserter(result),
743 ✗ [](const Attribute& attribute) { return attribute.valueAsString(); });
744 ✗ return result;
745 ✗ }
746
747 /// Returns values of any "Intended Use Case" attributes
748 ✗ std::vector<std::string> BCLMeasure::intendedUseCases() const {
749 ✗ std::vector<std::string> result;
750 ✗ std::vector<Attribute> attributes = m_bclXML.getAttributes("Intended Use Case");
751 ✗ result.reserve(attributes.size());
752 ✗ std::transform(attributes.cbegin(), attributes.cend(), std::back_inserter(result),
753 ✗ [](const Attribute& attribute) { return attribute.valueAsString(); });
754 ✗ return result;
755 ✗ }
756
757 63 boost::optional<openstudio::path> BCLMeasure::primaryRubyScriptPath() const {
758
2/2
✓ Branch 1 taken 63 times.
✓ Branch 4 taken 63 times.
126 openstudio::path result = m_directory / toPath("measure.rb");
759
5/8
✓ Branch 1 taken 63 times.
✓ Branch 3 taken 63 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 63 times.
✓ Branch 8 taken 63 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 63 times.
✗ Branch 11 not taken.
63 if (exists(result) && is_regular_file(result)) {
760
1/1
✓ Branch 1 taken 63 times.
63 return result;
761 }
762 ✗ return boost::none;
763 63 }
764
765 14 boost::optional<openstudio::path> BCLMeasure::primaryPythonScriptPath() const {
766
2/2
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
28 openstudio::path result = m_directory / toPath("measure.py");
767
5/8
✓ Branch 1 taken 14 times.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 14 times.
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 14 times.
✗ Branch 11 not taken.
14 if (exists(result) && is_regular_file(result)) {
768
1/1
✓ Branch 1 taken 14 times.
14 return result;
769 }
770 ✗ return boost::none;
771 14 }
772
773 51 boost::optional<openstudio::path> BCLMeasure::primaryScriptPath() const {
774
6/6
✓ Branch 1 taken 51 times.
✓ Branch 4 taken 51 times.
✓ Branch 7 taken 37 times.
✓ Branch 8 taken 14 times.
✓ Branch 10 taken 37 times.
✓ Branch 13 taken 14 times.
51 return (measureLanguage() == MeasureLanguage::Ruby) ? primaryRubyScriptPath() : primaryPythonScriptPath();
775 }
776
777 ✗ FileReferenceType BCLMeasure::inputFileType() const {
778 ✗ FileReferenceType result = FileReferenceType::Unknown;
779 ✗ MeasureType measureType = this->measureType();
780 ✗ if (measureType == MeasureType::ModelMeasure) {
781 ✗ result = FileReferenceType::OSM;
782 ✗ } else if (measureType == MeasureType::EnergyPlusMeasure) {
783 ✗ result = FileReferenceType::IDF;
784 ✗ } else if (measureType == MeasureType::UtilityMeasure) {
785 // no-op
786 ✗ } else if (measureType == MeasureType::ReportingMeasure) {
787 // DLM: is this right?
788 // no-op
789 }
790 ✗ return result;
791 }
792
793 ✗ FileReferenceType BCLMeasure::outputFileType() const {
794 ✗ FileReferenceType result = FileReferenceType::Unknown;
795 ✗ MeasureType measureType = this->measureType();
796 ✗ if (measureType == MeasureType::ModelMeasure) {
797 ✗ result = FileReferenceType::OSM;
798 ✗ } else if (measureType == MeasureType::EnergyPlusMeasure) {
799 ✗ result = FileReferenceType::IDF;
800 ✗ } else if (measureType == MeasureType::UtilityMeasure) {
801 // no-op
802 ✗ } else if (measureType == MeasureType::ReportingMeasure) {
803 // DLM: is this right?
804 // no-op
805 }
806 ✗ return result;
807 }
808
809 ✗ void BCLMeasure::changeUID() {
810 ✗ m_bclXML.changeUID();
811 ✗ }
812
813 ✗ void BCLMeasure::incrementVersionId() {
814 ✗ m_bclXML.incrementVersionId();
815 ✗ }
816
817 ✗ bool BCLMeasure::updateMeasureScript(const MeasureType& oldMeasureType, const MeasureType& newMeasureType, const MeasureLanguage& oldMeasureLanguage,
818 const MeasureLanguage& newMeasureLanguage, const std::string& oldClassName, const std::string& newClassName,
819 const std::string& name, const std::string& description, const std::string& modelerDescription) {
820 ✗ if (oldMeasureLanguage != newMeasureLanguage) {
821 ✗ LOG_AND_THROW("Cannot update a measure after a Language Change from " << oldMeasureLanguage.valueName() << " to "
822 << newMeasureLanguage.valueName());
823 }
824 // TODO: deal with Python
825 ✗ boost::optional<openstudio::path> rubyScriptPath_ = primaryRubyScriptPath();
826 ✗ if (rubyScriptPath_ && exists(*rubyScriptPath_)) {
827
828 ✗ openstudio::filesystem::ifstream file(*rubyScriptPath_, std::ios_base::binary);
829 ✗ if (file.is_open()) {
830
831 // Replacement paterns
832 ✗ std::string nameFunction = "$1def name\n$1 return \"" + name + "\"\n$1end";
833 ✗ std::string descriptionFunction = "$1def description\n$1 return \"" + description + "\"\n$1end";
834 ✗ std::string modelerDescriptionFunction = "$1def modeler_description\n$1 return \"" + modelerDescription + "\"\n$1end";
835
836 ✗ std::string fileString = openstudio::filesystem::read_as_string(file);
837
838 ✗ boost::regex re;
839
840 ✗ if (oldMeasureType != newMeasureType) {
841 ✗ boost::replace_all(fileString, oldMeasureType.valueName(), newMeasureType.valueName());
842 }
843
844 ✗ if (!oldClassName.empty() && !newClassName.empty() && oldClassName != newClassName) {
845 // DLM: might also want to check that oldClassName is greater than 3 characters long?
846 // should we be doing a more selective replace (like require leading space and trailing space, ., or :)?
847 ✗ boost::replace_all(fileString, oldClassName, newClassName);
848 }
849
850 // Note JM 2018-11-06: Use the iterator overload of regex_replace for inplace replacement? Actually don't.
851 // There's undefined behavior when you end up replacing with something that doesn't have the same length
852 ✗ re.set_expression("^([\t ]*?)def\\s+name(.*?)end[\\s#]?$");
853 ✗ fileString = boost::regex_replace(fileString, re, nameFunction);
854
855 ✗ re.set_expression("^([\t ]*?)def\\s+description(.*?)end[\\s#]?$");
856 ✗ fileString = boost::regex_replace(fileString, re, descriptionFunction);
857
858 ✗ re.set_expression("^([\t ]*?)def\\s+modeler_description(.*?)end[\\s#]?$");
859 ✗ fileString = boost::regex_replace(fileString, re, modelerDescriptionFunction);
860
861 ✗ file.close();
862
863 ✗ openstudio::filesystem::ofstream ofile(*rubyScriptPath_, std::ios_base::binary);
864 ✗ if (ofile.is_open()) {
865 ✗ ofile << fileString;
866 // openstudio::filesystem::write(ofile, fileString);
867 ✗ ofile.close();
868 ✗ return true;
869 }
870 ✗ }
871 ✗ }
872
873 ✗ return false;
874 ✗ }
875
876 ✗ bool BCLMeasure::updateMeasureTests(const std::string& oldClassName, const std::string& newClassName) {
877 ✗ bool result = true;
878 ✗ for (const BCLFileReference& fileRef : files()) {
879 ✗ std::string usageType = fileRef.usageType();
880 ✗ if (usageType != "test") {
881 ✗ continue;
882 }
883
884 ✗ if (exists(fileRef.path())) {
885
886 ✗ std::string oldLowerClassName = toUnderscoreCase(oldClassName);
887 ✗ std::string newLowerClassName = toUnderscoreCase(newClassName);
888
889 ✗ openstudio::path oldPath = fileRef.path();
890 ✗ openstudio::path newPath = oldPath;
891
892 ✗ const auto oldFileName = fileRef.path().filename();
893 ✗ auto newFileName = toString(oldFileName);
894 ✗ if (!oldLowerClassName.empty() && !newLowerClassName.empty() && oldLowerClassName != newLowerClassName) {
895 ✗ auto temp = oldLowerClassName;
896 ✗ const auto index = newFileName.rfind(temp);
897 ✗ if (index != std::string::npos) {
898 ✗ newFileName.replace(index, temp.size(), newLowerClassName);
899 ✗ newPath = oldPath.parent_path() / toPath(newFileName);
900
901 ✗ if (openstudio::filesystem::exists(newPath)) {
902 // somehow this file already exists (e.g. no name change), don't clobber it
903 ✗ newPath = oldPath;
904 } else {
905 ✗ openstudio::filesystem::copy_file(oldPath, newPath);
906 ✗ openstudio::filesystem::remove(oldPath);
907 }
908 }
909 ✗ }
910
911 ✗ openstudio::filesystem::ifstream file(newPath, std::ios_base::binary);
912 ✗ if (file.is_open()) {
913
914 ✗ auto fileString = openstudio::filesystem::read_as_string(file);
915 ✗ if (!oldClassName.empty() && !newClassName.empty() && oldClassName != newClassName) {
916 // DLM: might also want to check that oldClassName is greater than 3 characters long?
917 // should we be doing a more selective replace (like require leading space and trailing space, ., or :)?
918 ✗ boost::replace_all(fileString, oldClassName, newClassName);
919 }
920 ✗ file.close();
921
922 ✗ openstudio::filesystem::ofstream file2(newPath, std::ios_base::binary);
923 ✗ if (file2.is_open()) {
924 ✗ file2 << fileString;
925 ✗ file2.close();
926
927 } else {
928 ✗ result = false;
929 }
930 ✗ } else {
931 ✗ result = false;
932 }
933 ✗ } else {
934 ✗ result = false;
935 }
936 ✗ }
937 ✗ return result;
938 }
939
940 ✗ void BCLMeasure::clearFiles() {
941 ✗ m_bclXML.clearFiles();
942 ✗ }
943
944 ✗ void BCLMeasure::addAttribute(const Attribute& attribute) {
945 ✗ m_bclXML.addAttribute(attribute);
946 ✗ }
947
948 ✗ std::vector<Attribute> BCLMeasure::getAttributes(const std::string& name) const {
949 ✗ return m_bclXML.getAttributes(name);
950 }
951
952 ✗ bool BCLMeasure::removeAttributes(const std::string& name) {
953 ✗ return m_bclXML.removeAttributes(name);
954 }
955
956 ✗ void BCLMeasure::clearAttributes() {
957 ✗ m_bclXML.clearAttributes();
958 ✗ }
959
960 ✗ bool BCLMeasure::missingRequiredFields() const {
961 ✗ bool missing = (uid().empty() || versionId().empty() || xmlChecksum().empty() || name().empty() || displayName().empty() || className().empty()
962 ✗ || description().empty() || modelerDescription().empty());
963 ✗ return missing;
964 }
965
966 5 bool BCLMeasure::checkForUpdatesFiles() {
967 5 bool result = false;
968
969 5 std::vector<BCLFileReference> filesToRemove;
970 5 std::vector<BCLFileReference> filesToAdd;
971
972 // For all files we have on reference in the measure.xml
973
3/3
✓ Branch 1 taken 5 times.
✓ Branch 8 taken 35 times.
✓ Branch 9 taken 5 times.
40 for (BCLFileReference& file : m_bclXML.files()) {
974
975
1/1
✓ Branch 1 taken 35 times.
35 openstudio::path filePath = file.path();
976 // If the file has been deleted from disk, mark it for removal
977
3/3
✓ Branch 1 taken 35 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 33 times.
35 if (!openstudio::filesystem::exists(filePath)) {
978
6/6
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
2 LOG(Info, filePath << " has been deleted");
979 2 result = true;
980 // what if this is the measure.rb file?
981
1/1
✓ Branch 1 taken 2 times.
2 filesToRemove.push_back(file);
982
983 // Otherwise, if it's to be ignored
984
3/3
✓ Branch 1 taken 33 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 30 times.
33 } else if (!isApprovedFile(filePath, m_directory)) {
985 // The only way this file could have been added to the XML is if the user had manually added it,
986 // or we have changed the rules in the source code. In either case, I think a log message is helpful...
987 // We filter for ALL allowed cases here, so that we catch all mistakes, eg any file that the user manually added that isn't one of the
988 // approved root files or under an approved subdirectory
989
6/6
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
3 LOG(Warn, filePath << " was present in your measure.xml but it is not an approved file and will be removed from tracking.");
990 3 result = true;
991
1/1
✓ Branch 1 taken 3 times.
3 filesToRemove.push_back(file);
992
993 // otherwise, compute new checksum, and if not the same: mark it for addition
994
2/3
✓ Branch 1 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
30 } else if (file.checkForUpdate()) {
995 ✗ LOG(Info, filePath << " has been updated");
996 ✗ result = true;
997 ✗ filesToAdd.push_back(file);
998 }
999 40 }
1000
1001 47 auto addWithUsageTypeIfNotExisting = [this, &filesToAdd](const openstudio::path& relativeFilePath, const std::string& usageType) -> bool {
1002
4/4
✓ Branch 1 taken 47 times.
✓ Branch 4 taken 47 times.
✓ Branch 8 taken 17 times.
✓ Branch 9 taken 30 times.
94 if (!m_bclXML.hasFile(m_directory / relativeFilePath)) {
1003
1/1
✓ Branch 1 taken 17 times.
17 BCLFileReference fileref(m_directory, relativeFilePath, true);
1004
1/1
✓ Branch 1 taken 17 times.
17 fileref.setUsageType(usageType);
1005
1/1
✓ Branch 1 taken 17 times.
17 filesToAdd.push_back(fileref);
1006 17 return true;
1007 17 } else {
1008 30 return false;
1009 }
1010 5 };
1011
1012 // I could loop on all recursive_directory_files for the entire measureDir,
1013 // but this is likely faster by prefiltering on what has potential to be added
1014
2/2
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 5 times.
20 for (const auto& [subFolderName, usageType] : approvedSubFolderToUsageMap) {
1015
2/2
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
30 openstudio::path approvedSubFolderAbsolutePath = m_directory / toPath(subFolderName);
1016
3/3
✓ Branch 1 taken 15 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 3 times.
15 if (openstudio::filesystem::exists(approvedSubFolderAbsolutePath)) {
1017
1018
1/1
✓ Branch 1 taken 12 times.
12 for (auto it = openstudio::filesystem::recursive_directory_iterator(approvedSubFolderAbsolutePath);
1019
4/4
✓ Branch 1 taken 43 times.
✓ Branch 5 taken 55 times.
✓ Branch 8 taken 43 times.
✓ Branch 9 taken 12 times.
55 it != openstudio::filesystem::recursive_directory_iterator(); ++it) {
1020
1/1
✓ Branch 1 taken 43 times.
43 const auto& absoluteFilePath = it->path();
1021
1022
3/3
✓ Branch 1 taken 43 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 31 times.
43 if (openstudio::filesystem::is_directory(absoluteFilePath)) {
1023
1/1
✓ Branch 2 taken 12 times.
12 const std::string name = absoluteFilePath.filename().string();
1024
8/9
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 2 times.
✓ Branch 7 taken 10 times.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 9 times.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 9 times.
12 if (!name.empty() && (name[0] == '.' || name == "__pycache__")) {
1025 // Skip this directory and all its children
1026 3 it.disable_recursion_pending();
1027 }
1028
2/3
✓ Branch 2 taken 31 times.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
43 } else if (openstudio::filesystem::is_regular_file(absoluteFilePath)) {
1029
3/3
✓ Branch 1 taken 31 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 29 times.
31 if (!isApprovedFile(absoluteFilePath, m_directory)) {
1030 2 continue;
1031 }
1032
1/1
✓ Branch 1 taken 29 times.
29 const openstudio::path relativeFilePath = openstudio::filesystem::relative(absoluteFilePath, m_directory);
1033
2/2
✓ Branch 1 taken 29 times.
✓ Branch 4 taken 29 times.
29 result |= addWithUsageTypeIfNotExisting(relativeFilePath, std::string(usageType));
1034 29 }
1035 12 }
1036 }
1037 15 }
1038
1039
2/2
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 5 times.
30 for (const auto& [fileName, usageType] : rootToUsageTypeMap) {
1040
1/1
✓ Branch 1 taken 25 times.
25 openstudio::path relativeFilePath = toPath(fileName);
1041
1/1
✓ Branch 1 taken 25 times.
50 openstudio::path absoluteFilePath = m_directory / relativeFilePath;
1042
3/3
✓ Branch 1 taken 25 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 7 times.
25 if (openstudio::filesystem::exists(absoluteFilePath)) {
1043
2/2
✓ Branch 1 taken 18 times.
✓ Branch 4 taken 18 times.
18 bool thisResult = addWithUsageTypeIfNotExisting(relativeFilePath, std::string(usageType));
1044
5/8
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 3 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 18 times.
18 if (thisResult && ((fileName == "measure.rb") || (fileName == "measure.py"))) {
1045 // we don't know what the actual version this was created for, we also don't know minimum version
1046 ✗ filesToAdd.back().setSoftwareProgramVersion(openStudioVersion());
1047 }
1048 18 result |= thisResult;
1049 }
1050 25 }
1051
1052
2/2
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 5 times.
10 for (const BCLFileReference& file : filesToRemove) {
1053
2/2
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
5 m_bclXML.removeFile(file.path());
1054 }
1055
1056
2/2
✓ Branch 5 taken 17 times.
✓ Branch 6 taken 5 times.
22 for (const BCLFileReference& file : filesToAdd) {
1057
1/1
✓ Branch 1 taken 17 times.
17 m_bclXML.addFile(file);
1058 }
1059
1060 // Don't allow having both a ruby and a python measure in the same folder
1061
2/2
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
10 bool hasRuby = m_bclXML.hasFile(m_directory / "measure.rb");
1062
2/2
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
10 bool hasPython = m_bclXML.hasFile(m_directory / "measure.py");
1063
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (hasRuby && hasPython) {
1064 ✗ LOG(Warn, "Both measure.rb and measure.py cannot be in the same measure folder. Keeping only the ruby one");
1065 ✗ setMeasureLanguage(MeasureLanguage::Ruby);
1066 ✗ m_bclXML.removeFile(m_directory / "measure.py");
1067
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 } else if (hasRuby) {
1068
3/4
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5 times.
5 if (measureLanguage() != MeasureLanguage::Ruby) {
1069 ✗ setMeasureLanguage(MeasureLanguage::Ruby);
1070 }
1071 ✗ } else if (hasPython) {
1072 ✗ if (measureLanguage() != MeasureLanguage::Python) {
1073 ✗ setMeasureLanguage(MeasureLanguage::Python);
1074 }
1075 } else {
1076 ✗ LOG_AND_THROW("measure at " << m_directory << " has neither measure.rb nor measure.py");
1077 }
1078
1079 // increment version if anything changed
1080
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 if (result) {
1081
1/1
✓ Branch 1 taken 4 times.
4 m_bclXML.incrementVersionId();
1082 }
1083
1084 5 return result;
1085 5 }
1086
1087 5 bool BCLMeasure::checkForUpdatesXML() {
1088 5 return m_bclXML.checkForUpdatesXML();
1089 }
1090
1091 ✗ bool BCLMeasure::operator==(const BCLMeasure& other) const {
1092 ✗ return ((this->uid() == other.uid()) && (this->versionId() == other.versionId()));
1093 }
1094
1095 ✗ bool BCLMeasure::operator!=(const BCLMeasure& other) const {
1096 ✗ return !(operator==(other));
1097 }
1098
1099 ✗ bool BCLMeasure::operator<(const BCLMeasure& other) const {
1100 ✗ return (uid() + versionId()) < (other.uid() + other.versionId());
1101 }
1102
1103 4 bool BCLMeasure::save() const {
1104 4 return m_bclXML.save();
1105 }
1106
1107 2 boost::optional<BCLMeasure> BCLMeasure::clone(const openstudio::path& newDir) const {
1108
1109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (openstudio::filesystem::exists(newDir)) {
1110 ✗ if (!isEmptyDirectory(newDir)) {
1111 ✗ LOG(Warn, "Cannot clone measure to newDir=" << newDir << " since it already exists and is not empty.");
1112 ✗ return boost::none;
1113 }
1114 } else {
1115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!openstudio::filesystem::create_directories(newDir)) {
1116 ✗ LOG(Warn, "Cannot clone measure to newDir=" << newDir << " since we cannot create the directory. Check file permissions.");
1117 ✗ return boost::none;
1118 }
1119 }
1120
1121 // TODO: isn't it better to copy only what's inside measure.xml?
1122 2 constexpr bool weCareAboutOutdatedMeasureXML = true;
1123 // The only caveat is what if the measure.xml is outdated with regards to <files>?
1124 // Currently the BCLMeasure(const path&) ctor **does NOT** call checkUpdatesFiles()
1125 //
1126 // We could call it here by that would require a const_cast and it would increment the measure.xml versionId of the original measure,
1127 // and update the list of files, which isn't a big deal if the user doesn't then save it too? Even if the user did it, wouldn't we be helping out anyways?
1128
1129 // If this is really not wanted, we can go back to scanning the files on disk, with a similar logic as in checkForUpdatesFiles:
1130 // loop on all files on disk, and if isApproved is true, then copy them
1131
1132 // Copy the measure.xml
1133
5/5
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
6 openstudio::filesystem::copy_file_no_throw(m_directory / openstudio::path{"measure.xml"}, newDir / openstudio::path{"measure.xml"});
1134
1135 if constexpr (!weCareAboutOutdatedMeasureXML) {
1136 // Then copy whatever is referneced in the measure.xml
1137 for (const BCLFileReference& file : this->files()) {
1138
1139 if (std::find_if(usageTypesIgnoredOnClone.cbegin(), usageTypesIgnoredOnClone.cend(),
1140 [&file](const auto& sv) { return file.usageType() == std::string(sv); })
1141 == usageTypesIgnoredOnClone.cend()) {
1142
1143 // BCLFileReference::path() is absolute
1144 openstudio::path oriPath = file.path();
1145 openstudio::path relPath = openstudio::filesystem::relative(oriPath, m_directory);
1146 openstudio::path destination = newDir / relPath;
1147 // Create parent directories in destination if need be
1148 openstudio::filesystem::create_directories(destination.parent_path());
1149 openstudio::filesystem::copy_file_no_throw(oriPath, destination);
1150 }
1151 }
1152 } else {
1153
1154 2 std::vector<std::pair<openstudio::path, openstudio::path>> filesToCopy;
1155
1156 // I could loop on all recursive_directory_files for the entire measureDir,
1157 // but this is likely faster by prefiltering on what has potential to be added
1158
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (const auto& subFolderToUsagePair : approvedSubFolderToUsageMap) {
1159 // Structure bindings are never names of variables so they can't be captured in lambda, hence call to first and second on the pair
1160 6 std::string_view subFolderName = subFolderToUsagePair.first;
1161 6 std::string_view usageType = subFolderToUsagePair.second;
1162
1/1
✓ Branch 3 taken 6 times.
6 if (std::find_if(usageTypesIgnoredOnClone.cbegin(), usageTypesIgnoredOnClone.cend(),
1163
1/1
✓ Branch 1 taken 6 times.
18 [usageType](const auto& sv) { return usageType == std::string(sv); })
1164
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
6 != usageTypesIgnoredOnClone.cend()) {
1165 2 continue;
1166 }
1167
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
8 const openstudio::path approvedSubFolderAbsolutePath = m_directory / toPath(subFolderName);
1168
2/3
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
4 if (openstudio::filesystem::exists(approvedSubFolderAbsolutePath)) {
1169
1170
1/1
✓ Branch 1 taken 4 times.
4 for (auto it = openstudio::filesystem::recursive_directory_iterator(approvedSubFolderAbsolutePath);
1171
4/4
✓ Branch 1 taken 24 times.
✓ Branch 5 taken 28 times.
✓ Branch 8 taken 24 times.
✓ Branch 9 taken 4 times.
28 it != openstudio::filesystem::recursive_directory_iterator(); ++it) {
1172
1/1
✓ Branch 1 taken 24 times.
24 const auto& absoluteFilePath = it->path();
1173
1174
3/3
✓ Branch 1 taken 24 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 14 times.
24 if (openstudio::filesystem::is_directory(absoluteFilePath)) {
1175
1/1
✓ Branch 2 taken 10 times.
10 const std::string name = absoluteFilePath.filename().string();
1176
8/9
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 2 times.
✓ Branch 7 taken 8 times.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 6 times.
✓ Branch 11 taken 4 times.
✓ Branch 12 taken 6 times.
10 if (!name.empty() && (name[0] == '.' || name == "__pycache__")) {
1177 // Skip this directory and all its children
1178 4 it.disable_recursion_pending();
1179 }
1180
2/3
✓ Branch 2 taken 14 times.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
24 } else if (openstudio::filesystem::is_regular_file(absoluteFilePath)) {
1181
3/3
✓ Branch 1 taken 14 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 12 times.
14 if (!isApprovedFile(absoluteFilePath, m_directory)) {
1182 2 continue;
1183 }
1184
1/1
✓ Branch 1 taken 12 times.
12 const openstudio::path relativeFilePath = openstudio::filesystem::relative(absoluteFilePath, m_directory);
1185
2/2
✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
24 filesToCopy.emplace_back(absoluteFilePath, newDir / relativeFilePath);
1186 12 }
1187 4 }
1188 }
1189 4 }
1190
1191
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
12 for (std::string_view fileName : approvedRootFiles) {
1192
1/1
✓ Branch 1 taken 10 times.
10 openstudio::path relativeFilePath = toPath(fileName);
1193
1/1
✓ Branch 1 taken 10 times.
20 openstudio::path absoluteFilePath = m_directory / relativeFilePath;
1194
3/3
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 2 times.
10 if (openstudio::filesystem::exists(absoluteFilePath)) {
1195
2/2
✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
16 filesToCopy.emplace_back(absoluteFilePath, newDir / relativeFilePath);
1196 }
1197 10 }
1198
1199
2/2
✓ Branch 7 taken 20 times.
✓ Branch 8 taken 2 times.
22 for (const auto& [oriPath, destPath] : filesToCopy) {
1200
2/2
✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
20 openstudio::filesystem::create_directories(destPath.parent_path());
1201
1/1
✓ Branch 1 taken 20 times.
20 openstudio::filesystem::copy_file_no_throw(oriPath, destPath);
1202 }
1203 2 }
1204
1205 2 return BCLMeasure::load(newDir);
1206 }
1207
1208 ✗ std::string BCLMeasure::xmlString() const {
1209 ✗ return m_bclXML.toString();
1210 }
1211
1212 ✗ Json::Value BCLMeasure::toJSON() const {
1213 ✗ return m_bclXML.toJSON();
1214 }
1215
1216 ✗ std::string BCLMeasure::toJSONString() const {
1217 ✗ return m_bclXML.toJSONString();
1218 }
1219
1220 } // namespace openstudio
1221