-
Notifications
You must be signed in to change notification settings - Fork 49
/
ComponentTable.cpp
442 lines (373 loc) · 13.4 KB
/
ComponentTable.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
/*
* 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 "ComponentTable.h"
#include "Logging.h"
#include "Model.h"
#include "OMSFileSystem.h"
#include "ssd/Tags.h"
#include "System.h"
#include <RegEx.h>
oms::ComponentTable::ComponentTable(const ComRef& cref, System* parentSystem, const std::string& path)
: oms::Component(cref, oms_component_table, parentSystem, path), resultReader(NULL)
{
}
oms::ComponentTable::~ComponentTable()
{
for (auto it=series.begin(); it != series.end(); it++)
ResultReader::deleteSeries(&it->second);
series.clear();
if (resultReader)
delete resultReader;
}
oms::Component* oms::ComponentTable::NewComponent(const oms::ComRef& cref, oms::System* parentSystem, const std::string& path)
{
if (!cref.isValidIdent())
{
logError_InvalidIdent(cref);
return NULL;
}
if (!parentSystem)
{
logError_InternalError;
return NULL;
}
std::string extension = "";
if (path.length() > 4)
extension = path.substr(path.length() - 4);
filesystem::path temp_root(parentSystem->getModel().getTempDirectory());
filesystem::path relPath = parentSystem->copyResources() ? (filesystem::path("resources") / (parentSystem->getUniqueID() + "_" + std::string(cref) + extension)) : filesystem::path(path);
filesystem::path absPath = temp_root / relPath;
ComponentTable* component = new ComponentTable(cref, parentSystem, relPath.generic_string());
// Copy the resource to the temp directory of the model? We don't want have
// to copy resources if importing an SSP file or snapshot.
if (parentSystem->copyResources())
oms_copy_file(filesystem::path(path), absPath);
component->resultReader = ResultReader::newReader(absPath.string().c_str());
if (!component->resultReader)
{
logError("Could not load lookup table: " + path);
delete component;
return NULL;
}
int i = 1;
int size = 1 + component->resultReader->getAllSignals().size();
for (auto const &signal : component->resultReader->getAllSignals())
{
component->exportSeries[ComRef(signal)] = true;
component->connectors.back() = new Connector(oms_causality_output, oms_signal_type_real, ComRef(signal), component->getFullCref(), i++/(double)size);
component->connectors.push_back(NULL);
}
component->element.setConnectors(&component->connectors[0]);
return component;
}
oms::Component* oms::ComponentTable::NewComponent(const pugi::xml_node& node, oms::System* parentSystem, const std::string& sspVersion, const Snapshot& snapshot)
{
ComRef cref = ComRef(node.attribute("name").as_string());
std::string type = node.attribute("type").as_string();
std::string source = node.attribute("source").as_string();
if (type != "application/table")
{
logError("Unexpected component type: " + type);
return NULL;
}
oms::ComponentTable* component = dynamic_cast<oms::ComponentTable*>(oms::ComponentTable::NewComponent(cref, parentSystem, source));
if (!component)
return NULL;
// delete connectors
for (const auto& connector : component->connectors)
if (connector)
delete connector;
component->connectors.clear();
for (auto const &signal : component->resultReader->getAllSignals())
component->exportSeries[component->getFullCref() + ComRef(signal)] = false;
for(pugi::xml_node_iterator it = node.begin(); it != node.end(); ++it)
{
std::string name = it->name();
if(name == oms::ssp::Draft20180219::ssd::connectors)
{
// import connectors
for(pugi::xml_node_iterator itConnectors = (*it).begin(); itConnectors != (*it).end(); ++itConnectors)
{
component->connectors.push_back(oms::Connector::NewConnector(*itConnectors, sspVersion, component->getFullCref()));
component->exportSeries[component->connectors.back()->getName()] = true;
}
}
else if(name == oms::ssp::Draft20180219::ssd::element_geometry)
{
oms::ssd::ElementGeometry geometry;
geometry.importFromSSD(*it);
component->setGeometry(geometry);
}
else
{
logError_WrongSchema(name);
delete component;
return NULL;
}
}
component->connectors.push_back(NULL);
component->element.setConnectors(&component->connectors[0]);
return component;
}
oms_status_enu_t oms::ComponentTable::exportToSSD(pugi::xml_node& node, Snapshot& snapshot, std::string variantName) const
{
node.append_attribute("name") = this->getCref().c_str();
node.append_attribute("type") = "application/table";
node.append_attribute("source") = getPath().c_str();
pugi::xml_node node_connectors = node.append_child(oms::ssp::Draft20180219::ssd::connectors);
if (element.getGeometry())
element.getGeometry()->exportToSSD(node);
for (const auto& connector : connectors)
if (connector)
if (oms_status_ok != connector->exportToSSD(node_connectors))
return oms_status_error;
return oms_status_ok;
}
oms_status_enu_t oms::ComponentTable::instantiate()
{
time = getModel().getStartTime();
return oms_status_ok;
}
oms_status_enu_t oms::ComponentTable::reset()
{
time = getModel().getStartTime();
return oms_status_ok;
}
oms_status_enu_t oms::ComponentTable::getReal(const oms::ComRef& cref, double& value)
{
if (!resultReader)
logError("the table isn't initialized properly");
ResultReader::Series* pSeries;
if (series.find(cref) == series.end())
{
pSeries = resultReader->getSeries(cref.c_str());
series[cref] = pSeries;
if (pSeries->length < 1)
return logError("empty table");
else if (pSeries->time[0] > time)
return logError("out of range (cref=" + std::string(cref) + ", time=" + std::to_string(time) + " cannot be less than first time point in table " + std::to_string(pSeries->time[0]) + ")");
}
else
pSeries = series[cref];
// check that lastIndex isn't in the future (e.g. due to rollback)
if (pSeries->time[lastIndex] > time)
lastIndex = 0;
for (; lastIndex<pSeries->length; ++lastIndex)
{
if (pSeries->time[lastIndex] == time)
{
value = pSeries->value[lastIndex];
return oms_status_ok;
}
else if (pSeries->time[lastIndex] <= time && pSeries->time[lastIndex+1] >= time)
{
double m = (pSeries->value[lastIndex+1] - pSeries->value[lastIndex]) / (pSeries->time[lastIndex+1] - pSeries->time[lastIndex]);
value = pSeries->value[lastIndex] + (time - pSeries->time[lastIndex]) * m;
return oms_status_ok;
}
}
value = 0.0;
return logError("out of range (cref=" + std::string(cref) + ", time=" + std::to_string(time) + ")");
}
oms_status_enu_t oms::ComponentTable::getInteger(const oms::ComRef& cref, int& value)
{
if (!resultReader)
logError("the table isn't initialized properly");
ResultReader::Series* pSeries;
if (series.find(cref) == series.end())
{
pSeries = resultReader->getSeries(cref.c_str());
series[cref] = pSeries;
if (pSeries->length < 1)
return logError("empty table");
else if (pSeries->time[0] > time)
return logError("out of range (cref=" + std::string(cref) + ", time=" + std::to_string(time) + " cannot be less than first time point in table " + std::to_string(pSeries->time[0]) + ")");
}
else
pSeries = series[cref];
// check that lastIndex isn't in the future (e.g. due to rollback)
if (pSeries->time[lastIndex-1] > time)
lastIndex = 0;
for (; lastIndex<pSeries->length; ++lastIndex)
{
if (time == pSeries->time[lastIndex])
{
value = pSeries->value[lastIndex];
return oms_status_ok;
}
else if (lastIndex > 0 && time < pSeries->time[lastIndex])
{
value = pSeries->value[lastIndex-1];
return oms_status_ok;
}
}
value = 0;
return logError("out of range (cref=" + std::string(cref) + ", time=" + std::to_string(time) + ")");
}
oms_status_enu_t oms::ComponentTable::getBoolean(const oms::ComRef& cref, bool& value)
{
if (!resultReader)
logError("the table isn't initialized properly");
ResultReader::Series* pSeries;
if (series.find(cref) == series.end())
{
pSeries = resultReader->getSeries(cref.c_str());
series[cref] = pSeries;
if (pSeries->length < 1)
return logError("empty table");
else if (pSeries->time[0] > time)
return logError("out of range (cref=" + std::string(cref) + ", time=" + std::to_string(time) + " cannot be less than first time point in table " + std::to_string(pSeries->time[0]) + ")");
}
else
pSeries = series[cref];
// check that lastIndex isn't in the future (e.g. due to rollback)
if (pSeries->time[lastIndex-1] > time)
lastIndex = 0;
for (; lastIndex<pSeries->length; ++lastIndex)
{
if (time == pSeries->time[lastIndex])
{
value = pSeries->value[lastIndex];
return oms_status_ok;
}
else if (lastIndex > 0 && time < pSeries->time[lastIndex])
{
value = pSeries->value[lastIndex-1];
return oms_status_ok;
}
}
value = 0;
return logError("out of range (cref=" + std::string(cref) + ", time=" + std::to_string(time) + ")");
}
oms_status_enu_t oms::ComponentTable::getRealOutputDerivative(const ComRef& cref, SignalDerivative& value)
{
if (!resultReader)
logError("the table isn't initialized properly");
ResultReader::Series* pSeries;
if (series.find(cref) == series.end())
{
pSeries = resultReader->getSeries(cref.c_str());
series[cref] = pSeries;
if (pSeries->length < 1)
return logError("empty table");
else if (pSeries->time[0] > time)
return logError("out of range (cref=" + std::string(cref) + ", time=" + std::to_string(time) + " cannot be less than first time point in table " + std::to_string(pSeries->time[0]) + ")");
}
else
pSeries = series[cref];
// check that lastIndex isn't in the future (e.g. due to rollback)
if (pSeries->time[lastIndex-1] > time)
lastIndex = 0;
for (; lastIndex<pSeries->length; ++lastIndex)
{
if ((pSeries->time[lastIndex] <= time && pSeries->time[lastIndex+1] > time) ||
(pSeries->time[lastIndex+1] == time && lastIndex == pSeries->length))
{
double m = (pSeries->value[lastIndex+1] - pSeries->value[lastIndex]) / (pSeries->time[lastIndex+1] - pSeries->time[lastIndex]);
value = SignalDerivative(m);
return oms_status_ok;
}
}
value = SignalDerivative();
return logError("out of range (cref=" + std::string(cref) + ", time=" + std::to_string(time) + ")");
}
oms_status_enu_t oms::ComponentTable::registerSignalsForResultFile(ResultWriter& resultFile)
{
resultFileMapping.clear();
for (unsigned int i=0; i<connectors.size(); ++i)
{
if (!connectors[i] || !exportSeries[connectors[i]->getName()])
continue;
std::string name = std::string(getFullCref() + connectors[i]->getName());
unsigned int ID = resultFile.addSignal(name, "lookup table", SignalType_REAL);
resultFileMapping[ID] = i;
}
return oms_status_ok;
}
oms_status_enu_t oms::ComponentTable::updateSignals(ResultWriter& resultWriter)
{
for (auto const &it : resultFileMapping)
{
unsigned int ID = it.first;
const ComRef& var = connectors[it.second]->getName();
SignalValue_t value;
if (oms_status_ok != getReal(var, value.realValue))
return logError("failed to fetch variable " + std::string(getFullCref()) + "." + std::string(var));
resultWriter.updateSignal(ID, value);
}
return oms_status_ok;
}
oms_status_enu_t oms::ComponentTable::addSignalsToResults(const char* regex)
{
oms_regex exp(regex);
for (auto& x: exportSeries)
{
if (x.second)
continue;
if (regex_match(std::string(x.first), exp))
x.second = true;
}
return oms_status_ok;
}
oms_status_enu_t oms::ComponentTable::removeSignalsFromResults(const char* regex)
{
oms_regex exp(regex);
for (auto& x: exportSeries)
{
if (!x.second)
continue;
if (regex_match(std::string(x.first), exp))
x.second = false;
}
return oms_status_ok;
}
oms_status_enu_t oms::ComponentTable::saveState()
{
storedTime = time;
return oms_status_ok;
}
oms_status_enu_t oms::ComponentTable::freeState()
{
return oms_status_ok;
}
oms_status_enu_t oms::ComponentTable::restoreState()
{
time = storedTime;
return oms_status_ok;
}
void oms::ComponentTable::getFilteredSignals(std::vector<Connector>& filteredSignals) const
{
for (auto& x: exportSeries)
{
if (x.second)
filteredSignals.push_back(Connector(oms_causality_output, oms_signal_type_real, x.first, this->getFullCref()));
}
}