-
Notifications
You must be signed in to change notification settings - Fork 49
/
Snapshot.cpp
468 lines (390 loc) · 16.3 KB
/
Snapshot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
#include "Snapshot.h"
#include "Logging.h"
#include "OMSString.h"
#include <iostream>
oms::Snapshot::Snapshot(bool partial)
{
// set the document with the root node <oms:snapshot>
doc.append_child(oms::ssp::Version1_0::snap_shot);
pugi::xml_node oms_snapshot = doc.document_element();
oms_snapshot.append_attribute("xmlns:oms") = "https://raw.githubusercontent.com/OpenModelica/OMSimulator/master/schema/oms.xsd";
oms_snapshot.append_attribute("partial") = partial ? "true" : "false";
}
oms::Snapshot::~Snapshot()
{
}
pugi::xml_node oms::Snapshot::newResourceNode(const filesystem::path& filename)
{
pugi::xml_node oms_snapshot = doc.document_element();
pugi::xml_node node = oms_snapshot.find_child_by_attribute(oms::ssp::Version1_0::oms_file, "name", filename.generic_string().c_str());
if (node)
{
logError("Node \"" + filename.generic_string() + "\" does already exist");
return node.first_child();
}
pugi::xml_node new_node = oms_snapshot.append_child(oms::ssp::Version1_0::oms_file);
new_node.append_attribute("name") = filename.generic_string().c_str();
return new_node;
}
oms_status_enu_t oms::Snapshot::import(const char* snapshot)
{
doc.reset();
pugi::xml_parse_result result = doc.load_string(snapshot);
if (!result)
return logError("loading snapshot failed (" + std::string(result.description()) + ")");
return oms_status_ok;
}
bool oms::Snapshot::isPartialSnapshot() const
{
pugi::xml_node oms_snapshot = doc.document_element();
return oms_snapshot.attribute("partial").as_bool();
}
oms_status_enu_t oms::Snapshot::importResourceFile(const filesystem::path& filename, const filesystem::path& root)
{
filesystem::path p = root / filename;
pugi::xml_document tmp_doc;
pugi::xml_parse_result result = tmp_doc.load_file(p.c_str());
if (!result)
return logError("loading resource \"" + p.generic_string() + "\" failed (" + std::string(result.description()) + ")");
return importResourceNode(filename, tmp_doc.document_element());
}
oms_status_enu_t oms::Snapshot::importResourceMemory(const filesystem::path& filename, const char* contents)
{
pugi::xml_document tmp_doc;
pugi::xml_parse_result result = tmp_doc.load_string(contents);
if (!result)
return logError("loading resource \"" + filename.generic_string() + "\" failed (" + std::string(result.description()) + ")");
return importResourceNode(filename, tmp_doc.document_element());
}
oms_status_enu_t oms::Snapshot::importResourceNode(const filesystem::path& filename, const pugi::xml_node& node)
{
pugi::xml_node oms_snapshot = doc.document_element();
pugi::xml_node oms_file = oms_snapshot.append_child(oms::ssp::Version1_0::oms_file);
oms_file.append_attribute("name") = filename.generic_string().c_str();
oms_file.append_copy(node);
return oms_status_ok;
}
oms_status_enu_t oms::Snapshot::importPartialResourceNode(const filesystem::path& filename, const filesystem::path& nodename, const pugi::xml_node& node)
{
pugi::xml_node oms_snapshot = doc.document_element();
pugi::xml_node oms_file = oms_snapshot.append_child(oms::ssp::Version1_0::oms_file);
oms_file.append_attribute("name") = filename.generic_string().c_str();
oms_file.append_attribute("node") = nodename.generic_string().c_str();
oms_file.append_copy(node);
return oms_status_ok;
}
void oms::Snapshot::getResources(std::vector<std::string>& resources) const
{
pugi::xml_node oms_snapshot = doc.document_element();
for (const auto& it : oms_snapshot.children())
resources.push_back(it.attribute("name").as_string());
}
pugi::xml_node oms::Snapshot::getResourceNode(const filesystem::path& filename) const
{
pugi::xml_node oms_snapshot = doc.document_element();
pugi::xml_node node = oms_snapshot.find_child_by_attribute(oms::ssp::Version1_0::oms_file, "name", filename.generic_string().c_str());
if (node)
return node.first_child();
logError("Failed to find node \"" + filename.generic_string() + "\"");
return node;
}
oms_status_enu_t oms::Snapshot::deleteResourceNode(const filesystem::path &filename)
{
pugi::xml_node oms_snapshot = doc.document_element(); // oms:snapshot
pugi::xml_node node = oms_snapshot.find_child_by_attribute(oms::ssp::Version1_0::oms_file, "name", filename.generic_string().c_str());
if (!node)
logError("Failed to find node \"" + filename.generic_string() + "\"");
oms_snapshot.remove_child(node);
return oms_status_ok;
}
pugi::xml_node oms::Snapshot::operator[](const filesystem::path& filename)
{
pugi::xml_node oms_snapshot = doc.document_element();
pugi::xml_node node = oms_snapshot.find_child_by_attribute(oms::ssp::Version1_0::oms_file, "name", filename.generic_string().c_str());
if (node)
return node.first_child();
return newResourceNode(filename);
}
void oms::Snapshot::debugPrintNode(const filesystem::path& filename) const
{
pugi::xml_node node = getResourceNode(filename);
if (node)
node.print(std::cout, " ", pugi::format_indent|pugi::format_indent_attributes, pugi::encoding_utf8);
}
void oms::Snapshot::debugPrintAll() const
{
doc.save(std::cout, " ", pugi::format_indent|pugi::format_indent_attributes, pugi::encoding_utf8);
}
pugi::xml_node oms::Snapshot::getTemplateResourceNodeSSD(const filesystem::path& filename, const ComRef& cref)
{
pugi::xml_node new_node = newResourceNode(filename);
pugi::xml_node ssdNode = new_node.append_child(oms::ssp::Draft20180219::ssd::system_structure_description);
ssdNode.append_attribute("xmlns:ssc") = "http://ssp-standard.org/SSP1/SystemStructureCommon";
ssdNode.append_attribute("xmlns:ssd") = "http://ssp-standard.org/SSP1/SystemStructureDescription";
ssdNode.append_attribute("xmlns:ssv") = "http://ssp-standard.org/SSP1/SystemStructureParameterValues";
ssdNode.append_attribute("xmlns:ssm") = "http://ssp-standard.org/SSP1/SystemStructureParameterMapping";
ssdNode.append_attribute("xmlns:ssb") = "http://ssp-standard.org/SSP1/SystemStructureSignalDictionary";
ssdNode.append_attribute("xmlns:oms") = "https://raw.githubusercontent.com/OpenModelica/OMSimulator/master/schema/oms.xsd";
ssdNode.append_attribute("name") = cref.c_str();
ssdNode.append_attribute("version") = "1.0";
return ssdNode;
}
pugi::xml_node oms::Snapshot::getTemplateResourceNodeSSV(const filesystem::path& filename, const std::string& cref)
{
pugi::xml_node new_node = newResourceNode(filename);
pugi::xml_node node_parameterset = new_node.append_child(oms::ssp::Version1_0::ssv::parameter_set);
node_parameterset.append_attribute("xmlns:ssc") = "http://ssp-standard.org/SSP1/SystemStructureCommon";
node_parameterset.append_attribute("xmlns:ssv") = "http://ssp-standard.org/SSP1/SystemStructureParameterValues";
node_parameterset.append_attribute("version") = "1.0";
node_parameterset.append_attribute("name") = cref.c_str();
pugi::xml_node node_parameters = node_parameterset.append_child(oms::ssp::Version1_0::ssv::parameters);
return node_parameters;
}
pugi::xml_node oms::Snapshot::getTemplateResourceNodeSSM(const filesystem::path& filename)
{
pugi::xml_node new_node = newResourceNode(filename);
pugi::xml_node node_parameterMapping = new_node.append_child(oms::ssp::Version1_0::ssm::parameter_mapping);
node_parameterMapping.append_attribute("xmlns:ssc") = "http://ssp-standard.org/SSP1/SystemStructureCommon";
node_parameterMapping.append_attribute("xmlns:ssm") = "http://ssp-standard.org/SSP1/SystemStructureParameterMapping";
node_parameterMapping.append_attribute("version") = "1.0";
return node_parameterMapping;
}
pugi::xml_node oms::Snapshot::getTemplateResourceNodeSignalFilter(const filesystem::path& filename)
{
pugi::xml_node new_node = newResourceNode(filename);
pugi::xml_node oms_signalFilter = new_node.append_child(oms::ssp::Version1_0::oms_signalFilter);
oms_signalFilter.append_attribute("version") = "1.0";
return oms_signalFilter;
}
pugi::xml_node oms::Snapshot::getTemplateResourceNodeSSDVariants()
{
pugi::xml_node new_node = newResourceNode("ssdVariants.xml");
pugi::xml_node oms_variants = new_node.append_child("oms:Variants");
oms_variants.append_attribute("version") = "1.0";
return oms_variants;
}
oms::ComRef oms::Snapshot::getSSDFilename() const
{
pugi::xml_node oms_snapshot = doc.document_element(); // oms:snapshot
for (const auto& it : oms_snapshot.children())
if (".ssd" == filesystem::path(it.attribute("name").as_string()).extension()) // get filename of variants of .ssd
return oms::ComRef(it.attribute("name").as_string());
return oms::ComRef();
}
oms::ComRef oms::Snapshot::getSignalFilterFilename() const
{
pugi::xml_node oms_snapshot = doc.document_element(); // oms:snapshot
for (const auto& it : oms_snapshot.children())
if (".xml" == filesystem::path(it.attribute("name").as_string()).extension()) // get signalFilter filename of variants
return oms::ComRef(it.attribute("name").as_string());
return oms::ComRef();
}
oms::ComRef oms::Snapshot::getRootCref() const
{
pugi::xml_node oms_snapshot = doc.document_element(); // oms:snapshot
for (const auto& it : oms_snapshot.children())
if (".ssd" == filesystem::path(it.attribute("name").as_string()).extension()) // get root cref for all variants of .ssd
return oms::ComRef(it.first_child().attribute("name").as_string());
return oms::ComRef();
}
oms_status_enu_t oms::Snapshot::exportPartialSnapshot(const ComRef& cref, Snapshot& partialSnapshot)
{
ComRef subCref(cref);
std::string suffix = subCref.pop_suffix();
// copy only single file
if (!suffix.empty() && subCref.isEmpty())
{
pugi::xml_node node = getResourceNode(filesystem::path(suffix));
if (!node)
return logError("Failed to find node \"" + suffix + "\"");
partialSnapshot.importResourceNode(filesystem::path(suffix), node);
}
// check cref if to filter component: subCref
if (!subCref.isEmpty() && !suffix.empty())
{
ComRef tail(subCref);
ComRef front = tail.pop_front();
// get SystemStructure.ssd
pugi::xml_node ssdNode = getResourceNode("SystemStructure.ssd");
pugi::xml_node systemNode = ssdNode.first_child();
std::string nodeName = (ComRef(ssdNode.attribute("name").as_string()) + subCref);
if (tail.isEmpty())
{
// return system
if (systemNode.attribute("name").as_string() == std::string(front))
{
partialSnapshot.importPartialResourceNode("SystemStructure.ssd", nodeName, systemNode);
}
}
else
{
// iterate System
for (pugi::xml_node_iterator it = systemNode.begin(); it != systemNode.end(); ++it)
{
if (std::string(it->name()) == oms::ssp::Draft20180219::ssd::elements)
{
for (pugi::xml_node_iterator itElements = (*it).begin(); itElements != (*it).end(); ++itElements)
{
std::string name = itElements->name();
if (name == oms::ssp::Draft20180219::ssd::system || name == oms::ssp::Draft20180219::ssd::component)
{
if(itElements->attribute("name").as_string() == std::string(tail))
{
partialSnapshot.importPartialResourceNode("SystemStructure.ssd", nodeName, *itElements);
}
}
}
}
}
}
}
return oms_status_ok;
}
oms_status_enu_t oms::Snapshot::importPartialSnapshot(const char* fullsnapshot)
{
// copy the partial snapshot to new doc
pugi::xml_document copy;
copy.append_copy(doc.first_child());
// load the full snapshot
import(fullsnapshot);
pugi::xml_node partialsnapshot = copy.document_element(); // oms:snapshot
std::string partialSnapshotfilename = partialsnapshot.child(oms::ssp::Version1_0::oms_file).attribute("name").as_string();
std::string nodeName = partialsnapshot.child(oms::ssp::Version1_0::oms_file).attribute("node").as_string();
// copy only single file
if (!partialSnapshotfilename.empty() && nodeName.empty())
{
pugi::xml_node oms_snapshot = doc.document_element(); // oms:snapshot
for (pugi::xml_node node : oms_snapshot.children())
{
std::string filename = node.attribute("name").as_string();
if (filename == partialSnapshotfilename)
{
oms_snapshot.remove_child(node);
// replace with partialsnapshot
oms_snapshot.append_copy(partialsnapshot.first_child());
}
}
}
// check node name if to filter component: subCref
if (!partialSnapshotfilename.empty() && !nodeName.empty())
{
ComRef ident = ComRef(nodeName);
ident.pop_front(); // remove model name
ComRef tail(ident);
ComRef front = tail.pop_front();
// get SystemStructure.ssd
pugi::xml_node ssdNode = getResourceNode("SystemStructure.ssd");
pugi::xml_node systemNode = ssdNode.first_child();
if (tail.isEmpty())
{
// replace system
if (systemNode.attribute("name").as_string() == std::string(front))
{
ssdNode.remove_child(systemNode);
ssdNode.prepend_copy(partialsnapshot.first_child().first_child());
}
}
else
{
// replace subsystems or components
for (pugi::xml_node_iterator it = systemNode.begin(); it != systemNode.end(); ++it)
{
if (std::string(it->name()) == oms::ssp::Draft20180219::ssd::elements)
{
for (pugi::xml_node_iterator itElements = (*it).begin(); itElements != (*it).end(); ++itElements)
{
std::string name = itElements->name();
if (name == oms::ssp::Draft20180219::ssd::system || name == oms::ssp::Draft20180219::ssd::component)
{
if(itElements->attribute("name").as_string() == std::string(tail))
{
it->remove_child(*itElements);
it->append_copy(partialsnapshot.first_child().first_child());
}
}
}
}
}
}
}
//copy.save(std::cout);
return oms_status_ok;
}
oms_status_enu_t oms::Snapshot::writeDocument(char** contents)
{
class : public pugi::xml_writer
{
public:
virtual void write(const void* data, size_t size)
{
result += std::string(static_cast<const char*>(data), size);
}
oms_status_enu_t copy(char** contents)
{
*contents = mallocAndCopyString(result);
if (!*contents)
return oms_status_error;
return oms_status_ok;
}
private:
std::string result;
} writer;
doc.save(writer, " ", pugi::format_indent|pugi::format_indent_attributes, pugi::encoding_utf8);
return writer.copy(contents);
}
oms_status_enu_t oms::Snapshot::writeResourceNode(const filesystem::path& filename, const filesystem::path& path) const
{
class : public pugi::xml_writer
{
public:
virtual void write(const void* data, size_t size)
{
result += std::string(static_cast<const char*>(data), size);
}
oms_status_enu_t copy(char** contents)
{
*contents = mallocAndCopyString(result);
if (!*contents)
return oms_status_error;
return oms_status_ok;
}
private:
std::string result;
} writer;
pugi::xml_document doc;
doc.append_copy(getResourceNode(filename));
filesystem::path filepath = path / filename;
if (!doc.save_file(filepath.string().c_str(), " ", pugi::format_indent|pugi::format_indent_attributes, pugi::encoding_utf8))
return oms_status_error;
return oms_status_ok;
}