GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 78.0% 124 / 0 / 159
Functions: 79.2% 19 / 0 / 24
Branches: 48.2% 133 / 0 / 276

src/model/ExternalFile.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 "ExternalFile.hpp"
7 #include "ExternalFile_Impl.hpp"
8
9 #include "Model.hpp"
10 #include "ScheduleFile.hpp"
11 #include "PythonPluginInstance.hpp"
12 #include "PythonPluginInstance_Impl.hpp"
13 #include "ChillerElectricASHRAE205.hpp"
14 #include "ChillerElectricASHRAE205_Impl.hpp"
15
16 #include <utilities/idd/IddFactory.hxx>
17 #include <utilities/idd/IddEnums.hxx>
18 #include <utilities/idd/OS_External_File_FieldEnums.hxx>
19
20 #include "../utilities/filetypes/WorkflowJSON.hpp"
21 #include "../utilities/core/PathHelpers.hpp"
22 #include "../utilities/core/Checksum.hpp"
23 #include "../utilities/core/Assert.hpp"
24
25 #include <unordered_map>
26 #include "ScheduleFile_Impl.hpp"
27
28 namespace openstudio {
29 namespace model {
30
31 namespace detail {
32
33 24 ExternalFile_Impl::ExternalFile_Impl(const IdfObject& idfObject, Model_Impl* model, bool keepHandle)
34 24 : ResourceObject_Impl(idfObject, model, keepHandle) {
35
4/6
✓ Branch 1 taken 24 times.
✓ Branch 4 taken 24 times.
✓ Branch 7 taken 24 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 24 times.
✗ Branch 13 not taken.
24 OS_ASSERT(idfObject.iddObject().type() == ExternalFile::iddObjectType());
36 24 }
37
38 ✗ ExternalFile_Impl::ExternalFile_Impl(const openstudio::detail::WorkspaceObject_Impl& other, Model_Impl* model, bool keepHandle)
39 ✗ : ResourceObject_Impl(other, model, keepHandle) {
40 ✗ OS_ASSERT(other.iddObject().type() == ExternalFile::iddObjectType());
41 ✗ }
42
43 16 ExternalFile_Impl::ExternalFile_Impl(const ExternalFile_Impl& other, Model_Impl* model, bool keepHandle)
44 16 : ResourceObject_Impl(other, model, keepHandle) {}
45
46 ✗ const std::vector<std::string>& ExternalFile_Impl::outputVariableNames() const {
47 ✗ static const std::vector<std::string> result;
48 ✗ if (result.empty()) {
49 }
50 ✗ return result;
51 }
52
53 ✗ IddObjectType ExternalFile_Impl::iddObjectType() const {
54 ✗ return ExternalFile::iddObjectType();
55 }
56
57 ✗ ModelObject ExternalFile_Impl::clone(Model model) const {
58 ✗ boost::optional<ExternalFile> externalFile = ExternalFile::getExternalFile(model, toString(this->filePath()));
59 ✗ if (!externalFile) {
60 ✗ LOG_AND_THROW("Could not clone " << this->briefDescription());
61 }
62 ✗ return *externalFile;
63 ✗ }
64
65 6 std::vector<IdfObject> ExternalFile_Impl::remove() {
66
3/3
✓ Branch 1 taken 6 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 2 times.
6 if (!fileName().empty()) {
67
1/1
✓ Branch 1 taken 4 times.
4 path p = filePath();
68
3/3
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1 time.
4 if (exists(p)) {
69 try {
70
1/1
✓ Branch 1 taken 3 times.
3 boost::filesystem::remove(p);
71 ✗ } catch (std::exception&) {
72 ✗ LOG(Warn, "Could not remove file \"" << p << "\"");
73 ✗ }
74 }
75 4 }
76
77 6 std::vector<openstudio::IdfObject> tmp;
78
79 // ScheduleFile
80
1/1
✓ Branch 1 taken 6 times.
6 std::vector<ScheduleFile> sfs = scheduleFiles();
81
2/2
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 6 times.
8 for (auto& scheduleFile : sfs) {
82
1/1
✓ Branch 1 taken 2 times.
2 std::vector<openstudio::IdfObject> tmp2 = scheduleFile.remove();
83
1/1
✓ Branch 5 taken 2 times.
2 tmp.insert(tmp.end(), tmp2.begin(), tmp2.end());
84 2 }
85
86 // PythonPluginInstance
87
1/1
✓ Branch 1 taken 6 times.
6 std::vector<PythonPluginInstance> ppis = pythonPluginInstances();
88
2/2
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 6 times.
7 for (auto& pythonPluginInstance : ppis) {
89
1/1
✓ Branch 1 taken 1 time.
1 std::vector<openstudio::IdfObject> tmp2 = pythonPluginInstance.remove();
90
1/1
✓ Branch 5 taken 1 time.
1 tmp.insert(tmp.end(), tmp2.begin(), tmp2.end());
91 1 }
92
93 // ChillerElectricASHRAE205
94
1/1
✓ Branch 1 taken 6 times.
6 std::vector<ChillerElectricASHRAE205> chs = chillerElectricASHRAE205s();
95
2/2
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 6 times.
7 for (auto& ch : chs) {
96
1/1
✓ Branch 1 taken 1 time.
1 std::vector<openstudio::IdfObject> tmp2 = ch.remove();
97
1/1
✓ Branch 5 taken 1 time.
1 tmp.insert(tmp.end(), tmp2.begin(), tmp2.end());
98 1 }
99
100
1/1
✓ Branch 1 taken 6 times.
6 std::vector<openstudio::IdfObject> idfObjects = ModelObject_Impl::remove();
101
1/1
✓ Branch 5 taken 6 times.
6 idfObjects.insert(idfObjects.end(), tmp.begin(), tmp.end());
102
103 12 return idfObjects;
104 6 }
105
106 132 std::string ExternalFile_Impl::fileName() const {
107
1/1
✓ Branch 1 taken 132 times.
132 boost::optional<std::string> value = getString(OS_External_FileFields::FileName, true);
108
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
✗ Branch 4 not taken.
132 OS_ASSERT(value);
109
2/2
✓ Branch 1 taken 132 times.
✓ Branch 4 taken 132 times.
264 return value.get();
110 132 }
111
112 88 path ExternalFile_Impl::filePath() const {
113 88 path result;
114
2/2
✓ Branch 1 taken 88 times.
✓ Branch 4 taken 88 times.
88 path fname = toPath(fileName());
115
3/3
✓ Branch 1 taken 88 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 79 times.
88 if (openstudio::filesystem::is_regular_file(fname)) {
116 9 return fname;
117 }
118
3/3
✓ Branch 1 taken 79 times.
✓ Branch 4 taken 79 times.
✓ Branch 7 taken 79 times.
79 std::vector<path> absoluteFilePaths = this->model().workflowJSON().absoluteFilePaths();
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
79 if (absoluteFilePaths.empty()) {
120 ✗ result = this->model().workflowJSON().absoluteRootDir() / fname;
121 } else {
122 // Loop to find the file in all possible directories in order of preference
123 // (eg goes to hardcoded paths, then potentially generated_files, then files, etc.)
124
2/2
✓ Branch 5 taken 83 times.
✓ Branch 6 taken 1 time.
84 for (const openstudio::path& dirpath : absoluteFilePaths) {
125
1/1
✓ Branch 1 taken 83 times.
166 path p = dirpath / fname;
126
3/3
✓ Branch 1 taken 83 times.
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 5 times.
83 if (openstudio::filesystem::is_regular_file(p)) {
127
1/1
✓ Branch 1 taken 78 times.
78 result = p;
128 78 break;
129 }
130 83 }
131 }
132 79 return result;
133 88 }
134
135 //boost::optional<std::string> ExternalFile_Impl::columnSeparator() const {
136 // return getString(OS_External_FileFields::ColumnSeparator,true);
137 //}
138
139 //char ExternalFile_Impl::columnSeparatorChar() const {
140 // static std::unordered_map<std::string, char> lookup({ { "Comma", ',' }, { "Tab", '\t' }, { "Fixed", ' ' }, { "Semicolon", ';' } });
141 // boost::optional<std::string> value = getString(OS_External_FileFields::ColumnSeparator, true);
142 // OS_ASSERT(value);
143 // auto it = lookup.find(value.get());
144 // if (it == std::end(lookup)) {
145 // // Invalid separator
146 // return '\0';
147 // }
148 // return it->second;
149 //}
150
151 //bool ExternalFile_Impl::isColumnSeparatorDefaulted() const {
152 // return isEmpty(OS_External_FileFields::ColumnSeparator);
153 //}
154
155 22 bool ExternalFile_Impl::setFileName(const std::string& fileName) {
156 22 bool result = setString(OS_External_FileFields::FileName, fileName);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 OS_ASSERT(result);
158 22 return result;
159 }
160
161 //bool ExternalFile_Impl::setColumnSeparator(const std::string& columnSeparator) {
162 // bool result = setString(OS_External_FileFields::ColumnSeparator, columnSeparator);
163 // return result;
164 //}
165
166 //void ExternalFile_Impl::resetColumnSeparator() {
167 // bool result = setString(OS_External_FileFields::ColumnSeparator, "");
168 // OS_ASSERT(result);
169 //}
170
171 18 std::vector<ScheduleFile> ExternalFile_Impl::scheduleFiles() const {
172
2/2
✓ Branch 1 taken 18 times.
✓ Branch 4 taken 18 times.
18 std::vector<ScheduleFile> result = getObject<ExternalFile>().getModelObjectSources<ScheduleFile>();
173 18 return result;
174 }
175
176 11 std::vector<PythonPluginInstance> ExternalFile_Impl::pythonPluginInstances() const {
177
2/2
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
11 std::vector<PythonPluginInstance> result = getObject<ExternalFile>().getModelObjectSources<PythonPluginInstance>();
178 11 return result;
179 }
180
181 12 std::vector<ChillerElectricASHRAE205> ExternalFile_Impl::chillerElectricASHRAE205s() const {
182
2/2
✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
12 std::vector<ChillerElectricASHRAE205> result = getObject<ExternalFile>().getModelObjectSources<ChillerElectricASHRAE205>();
183 12 return result;
184 }
185
186 } // namespace detail
187
188 27 boost::optional<ExternalFile> ExternalFile::getExternalFile(const Model& model, const std::string& filename, bool copyFile) {
189
1/1
✓ Branch 1 taken 27 times.
27 path p = toPath(filename);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!p.has_filename()) {
191 ✗ return boost::none;
192 }
193 27 std::string s;
194
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 4 times.
27 if (copyFile) {
195
1/1
✓ Branch 1 taken 23 times.
23 s = toString(p.filename());
196 } else {
197
1/1
✓ Branch 1 taken 4 times.
4 s = filename;
198 }
199
200 // this factory method will give us better control if we start doing things like copying files
201
3/3
✓ Branch 1 taken 27 times.
✓ Branch 8 taken 9 times.
✓ Branch 9 taken 24 times.
33 for (const auto& externalFile : model.getConcreteModelObjects<ExternalFile>()) {
202
203 // handle Windows filename case insensitivity?
204
3/3
✓ Branch 1 taken 9 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 6 times.
9 if (externalFile.fileName() == s) {
205
1/1
✓ Branch 1 taken 3 times.
3 return externalFile;
206 }
207 27 }
208
209 try {
210
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
24 return ExternalFile(model, filename, copyFile);
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } catch (std::exception&) {
212 2 }
213 2 return boost::none;
214 27 }
215
216 24 ExternalFile::ExternalFile(const Model& model, const std::string& filename, bool copyFile) : ResourceObject(ExternalFile::iddObjectType(), model) {
217
1/3
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✗ Branch 5 not taken.
24 OS_ASSERT(getImpl<detail::ExternalFile_Impl>());
218
219
1/1
✓ Branch 1 taken 24 times.
24 WorkflowJSON workflow = model.workflowJSON();
220
221 // we expect all strings to be UTF-8 encoded
222
1/1
✓ Branch 1 taken 24 times.
24 path p = toPath(filename);
223
3/3
✓ Branch 1 taken 24 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 22 times.
24 if (!exists(p)) {
224
1/1
✓ Branch 1 taken 2 times.
2 boost::optional<path> op = workflow.findFile(filename);
225
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (!op) {
226
1/1
✓ Branch 1 taken 2 times.
2 this->remove();
227
21/21
✓ 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.
✓ Branch 20 taken 2 times.
✓ Branch 23 taken 2 times.
✓ Branch 26 taken 2 times.
✓ Branch 32 taken 2 times.
✓ Branch 35 taken 2 times.
✓ Branch 38 taken 2 times.
✓ Branch 41 taken 2 times.
✓ Branch 44 taken 2 times.
✓ Branch 47 taken 2 times.
✓ Branch 50 taken 2 times.
✓ Branch 53 taken 2 times.
✓ Branch 56 taken 2 times.
✓ Branch 59 taken 2 times.
✓ Branch 64 taken 2 times.
✓ Branch 67 taken 2 times.
2 LOG_AND_THROW("Cannot find file \"" << filename << "\" for " << this->briefDescription());
228 }
229 ✗ p = op.get();
230 2 }
231
2/4
✓ Branch 1 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 6 not taken.
22 OS_ASSERT(exists(p));
232
233 bool ok;
234
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 3 times.
22 if (copyFile) {
235 19 path destDir;
236
1/1
✓ Branch 1 taken 19 times.
19 std::vector<path> absoluteFilePaths = workflow.absoluteFilePaths();
237
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (absoluteFilePaths.empty()) {
238 ✗ destDir = workflow.absoluteRootDir();
239 } else {
240
1/1
✓ Branch 2 taken 19 times.
19 destDir = absoluteFilePaths[0];
241 }
242
1/1
✓ Branch 1 taken 19 times.
38 path dest = destDir / p.filename();
243
244
3/3
✓ Branch 1 taken 19 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 16 times.
19 if (exists(dest)) {
245
3/4
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
3 if (checksum(p) != checksum(dest)) {
246 ✗ this->remove();
247 ✗ LOG_AND_THROW("File \"" << p.filename() << "\" already exists in \"" << destDir << "\"");
248 }
249 } else {
250
251 try {
252
1/1
✓ Branch 2 taken 16 times.
16 makeParentFolder(dest, path(), true);
253 ✗ } catch (std::exception&) {
254 ✗ this->remove();
255 ✗ LOG_AND_THROW("Failed to created parent folder at \"" << dest << "\"");
256 ✗ }
257 try {
258
1/1
✓ Branch 1 taken 16 times.
16 boost::filesystem::copy(p, dest);
259 ✗ } catch (std::exception&) {
260 ✗ this->remove();
261 ✗ LOG_AND_THROW("Failed to copy file from \"" << p << "\" to \"" << dest << "\"");
262 ✗ }
263 }
264
2/4
✓ Branch 1 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 6 not taken.
19 OS_ASSERT(exists(dest));
265
266
2/2
✓ Branch 1 taken 19 times.
✓ Branch 4 taken 19 times.
19 ok = setFileName(toString(dest.filename()));
267
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
✗ Branch 3 not taken.
19 OS_ASSERT(ok);
268 19 } else {
269
1/1
✓ Branch 1 taken 3 times.
3 ok = setFileName(filename);
270
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
3 OS_ASSERT(ok);
271 }
272 28 }
273
274 7705 IddObjectType ExternalFile::iddObjectType() {
275
1/1
✓ Branch 1 taken 7705 times.
7705 return {IddObjectType::OS_External_File};
276 }
277
278 ✗ std::vector<std::string> ExternalFile::columnSeparatorValues() {
279 ✗ return getIddKeyNames(IddFactory::instance().getObject(iddObjectType()).get(), OS_External_FileFields::ColumnSeparator);
280 }
281
282 38 std::string ExternalFile::fileName() const {
283
1/1
✓ Branch 3 taken 38 times.
38 return getImpl<detail::ExternalFile_Impl>()->fileName();
284 }
285
286 84 path ExternalFile::filePath() const {
287
1/1
✓ Branch 3 taken 84 times.
84 return getImpl<detail::ExternalFile_Impl>()->filePath();
288 }
289
290 //boost::optional<std::string> ExternalFile::columnSeparator() const {
291 // return getImpl<detail::ExternalFile_Impl>()->columnSeparator();
292 //}
293
294 //bool ExternalFile::isColumnSeparatorDefaulted() const {
295 // return getImpl<detail::ExternalFile_Impl>()->isColumnSeparatorDefaulted();
296 //}
297
298 22 bool ExternalFile::setFileName(const std::string& fileName) {
299
1/1
✓ Branch 3 taken 22 times.
22 return getImpl<detail::ExternalFile_Impl>()->setFileName(fileName);
300 }
301
302 //bool ExternalFile::setColumnSeparator(const std::string& columnSeparator) {
303 // return getImpl<detail::ExternalFile_Impl>()->setColumnSeparator(columnSeparator);
304 //}
305
306 //void ExternalFile::resetColumnSeparator() {
307 // getImpl<detail::ExternalFile_Impl>()->resetColumnSeparator();
308 //}
309
310 //bool ExternalFile::isValid() {
311 // return getImpl<detail::ExternalFile_Impl>()->isValid();
312 //}
313
314 12 std::vector<ScheduleFile> ExternalFile::scheduleFiles() const {
315
1/1
✓ Branch 3 taken 12 times.
12 return getImpl<detail::ExternalFile_Impl>()->scheduleFiles();
316 }
317
318 5 std::vector<PythonPluginInstance> ExternalFile::pythonPluginInstances() const {
319
1/1
✓ Branch 3 taken 5 times.
5 return getImpl<detail::ExternalFile_Impl>()->pythonPluginInstances();
320 }
321
322 6 std::vector<ChillerElectricASHRAE205> ExternalFile::chillerElectricASHRAE205s() const {
323
1/1
✓ Branch 3 taken 6 times.
6 return getImpl<detail::ExternalFile_Impl>()->chillerElectricASHRAE205s();
324 }
325
326 /// @cond
327
1/1
✓ Branch 3 taken 127 times.
127 ExternalFile::ExternalFile(std::shared_ptr<detail::ExternalFile_Impl> impl) : ResourceObject(std::move(impl)) {}
328 /// @endcond
329
330 } // namespace model
331 } // namespace openstudio
332