-
Notifications
You must be signed in to change notification settings - Fork 49
/
Values.cpp
1788 lines (1605 loc) · 62.9 KB
/
Values.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 "Values.h"
#include "Logging.h"
#include "ssd/Tags.h"
#include "Util.h"
#include <iostream>
#include <map>
#include "minizip/miniunz.h"
#include <pugixml.hpp>
#include <string>
oms::Values::Values()
{
}
oms::Values::~Values()
{
}
oms_status_enu_t oms::Values::setReal(const ComRef& cref, double value)
{
realStartValues[cref] = value;
setUnitDefinitions(cref);
return oms_status_ok;
}
/*
function which sets the unitdefinitions <ssv:Units> or <ssd:Units> which will be exported in ssp
*/
void oms::Values::setUnitDefinitions(const ComRef& cref)
{
// search in user provided map set
std::string unitname = "";
auto unitValue = variableUnits.find(cref);
if (unitValue != variableUnits.end())
unitname = unitValue->second;
else
{
auto unitValue = modelDescriptionVariableUnits.find(cref);
if (unitValue != modelDescriptionVariableUnits.end())
unitname = unitValue->second;
}
if (!unitname.empty())
{
unitDefinitionsToExport unitToExport = {cref, unitname, modeldescriptionUnitDefinitions[unitname], true};
unitDefinitionsToExportInSSP.push_back(unitToExport);
}
}
void oms::Values::getFilteredUnitDefinitionsToSSD(std::map<std::string, std::map<std::string, std::string>>& unitDefinitions)
{
if (unitDefinitionsToExportInSSP.empty())
return;
for(const auto &it: unitDefinitionsToExportInSSP)
{
auto unitvalue = unitDefinitions.find(it.unitValue);
if (unitvalue == unitDefinitions.end())
unitDefinitions[it.unitValue] = it.baseUnit;
}
}
oms_status_enu_t oms::Values::setInteger(const ComRef& cref, int value)
{
integerStartValues[cref] = value;
return oms_status_ok;
}
oms_status_enu_t oms::Values::setBoolean(const ComRef& cref, bool value)
{
booleanStartValues[cref] = value;
return oms_status_ok;
}
oms_status_enu_t oms::Values::setString(const ComRef& cref, const std::string& value)
{
stringStartValues[cref] = value;
return oms_status_ok;
}
void oms::Values::updateModelDescriptionRealStartValue(const ComRef& cref, double value)
{
// update start values in ssv template
auto realValue = modelDescriptionRealStartValues.find(cref);
if (realValue != modelDescriptionRealStartValues.end())
modelDescriptionRealStartValues[cref] = value;
}
void oms::Values::updateModelDescriptionIntegerStartValue(const ComRef& cref, int value)
{
// update start values in ssv template
auto integerValue = modelDescriptionIntegerStartValues.find(cref);
if (integerValue != modelDescriptionIntegerStartValues.end())
modelDescriptionIntegerStartValues[cref] = value;
}
void oms::Values::updateModelDescriptionBooleanStartValue(const ComRef& cref, bool value)
{
// update start values in ssv template
auto boolValue = modelDescriptionBooleanStartValues.find(cref);
if (boolValue != modelDescriptionBooleanStartValues.end())
modelDescriptionBooleanStartValues[cref] = value;
}
void oms::Values::updateModelDescriptionStringStartValue(const ComRef& cref, std::string value)
{
// update start values in ssv template
auto stringValue = modelDescriptionStringStartValues.find(cref);
if (stringValue != modelDescriptionStringStartValues.end())
modelDescriptionStringStartValues[cref] = value;
}
void oms::Values::updateModelDescriptionVariableUnit(const ComRef& cref, const std::string& value)
{
modelDescriptionVariableUnits[cref] = value; // override if exists otherwise make a new entry
// update unit Definitions
for (const auto & it:modeldescriptionUnitDefinitions)
if (it.first == cref.c_str())
modeldescriptionUnitDefinitions[it.first] = {}; // make the baseUnit empty, as we do not calculate base units
}
void oms::Values::copyModelDescriptionUnitToResources(Values& value)
{
for (auto &it: parameterResources)
{
for (auto &res: it.allresources)
{
res.second.modelDescriptionVariableUnits = value.modelDescriptionVariableUnits;
res.second.modeldescriptionUnitDefinitions = value.modeldescriptionUnitDefinitions;
}
}
}
oms_status_enu_t oms::Values::setUnit(const ComRef& cref, const std::string& value)
{
variableUnits[cref] = value;
// update unit Definitions
for (auto & it : unitDefinitionsToExportInSSP)
{
if (it.unitName == cref.c_str())
{
//std::cout << "\n override the unit:" << it.unitName << "=>" << cref.c_str() << "==>" << value;
it.unitValue = value;
it.baseUnit = {};
break;
}
}
return oms_status_ok;
}
oms_status_enu_t oms::Values::getReal(const ComRef& cref, double& value)
{
auto realValue = realStartValues.find(cref);
if (realValue != realStartValues.end())
{
value = realValue->second;
return oms_status_ok;
}
return oms_status_error;
}
oms_status_enu_t oms::Values::getString(const ComRef& cref, std::string& value)
{
auto stringValue = stringStartValues.find(cref);
if (stringValue != stringStartValues.end())
{
value = stringValue->second;
return oms_status_ok;
}
return oms_status_error;
}
oms_status_enu_t oms::Values::getInteger(const ComRef& cref, int& value)
{
auto integerValue = integerStartValues.find(cref);
if (integerValue != integerStartValues.end())
{
value = integerValue->second;
return oms_status_ok;
}
return oms_status_error;
}
oms_status_enu_t oms::Values::getBoolean(const ComRef& cref, bool& value)
{
auto boolValue = booleanStartValues.find(cref);
if (boolValue != booleanStartValues.end())
{
value = boolValue->second;
return oms_status_ok;
}
return oms_status_error;
}
oms_status_enu_t oms::Values::setRealResources(const ComRef& cref, double value, const ComRef& fullCref, bool externalInput, oms_modelState_enu_t modelState)
{
bool resourceAvailable = false;
for (auto &it : parameterResources)
{
for (auto &res : it.allresources)
{
//update the value in all resources, so that same cref in multiple ssv can be updated, this can result in duplication
auto realValue = res.second.realStartValues.find(cref);
if (realValue != res.second.realStartValues.end())
{
if (oms_modelState_simulation == modelState && externalInput)
{
res.second.realValues[cref] = value;
}
else
{
res.second.setReal(cref, value);
}
resourceAvailable = true;
// return oms_status_ok; return here to avoid updating the same value in different ssv file
}
}
}
if (!resourceAvailable)
{
auto &it = parameterResources.front();
for (auto &res : it.allresources)
{
// insert the new signal at the first resource available
res.second.setReal(cref, value);
break;
}
}
return oms_status_ok;
}
oms_status_enu_t oms::Values::setStringResources(const ComRef& cref, const std::string& value, const ComRef& fullCref, bool externalInput, oms_modelState_enu_t modelState)
{
bool resourceAvailable = false;
for (auto &it : parameterResources)
{
for (auto &res : it.allresources)
{
//update the value in all resources, so that same cref in multiple ssv can be updated, this can result in duplication
auto stringValue = res.second.stringStartValues.find(cref);
if (stringValue != res.second.stringStartValues.end())
{
if (oms_modelState_simulation == modelState && externalInput)
res.second.stringValues[cref] = value;
else
res.second.setString(cref, value);
resourceAvailable = true;
// return oms_status_ok; return here to avoid updating the same value in different ssv file
}
}
}
if (!resourceAvailable)
{
auto &it = parameterResources.front();
for (auto &res : it.allresources)
{
// insert the new signal at the first resource available
res.second.setString(cref, value);
break;
}
}
return oms_status_ok;
}
oms_status_enu_t oms::Values::setUnitResources(const ComRef& cref, const std::string& value, const ComRef& fullCref)
{
bool resourceAvailable = false;
for (auto &it : parameterResources)
{
for (auto &res : it.allresources)
{
//update the value in all resources, so that same cref in multiple ssv can be updated, this can result in duplication
auto unitValue = res.second.variableUnits.find(cref);
if (unitValue != res.second.variableUnits.end())
{
res.second.setUnit(cref, value);
resourceAvailable = true;
// return oms_status_ok; return here to avoid updating the same value in different ssv file
}
}
}
if (!resourceAvailable)
{
auto &it = parameterResources.front();
for (auto &res : it.allresources)
{
// insert the new signal at the first resource available
res.second.setUnit(cref, value);
break;
}
}
return oms_status_ok;
}
oms_status_enu_t oms::Values::setIntegerResources(const ComRef& cref, int value, const ComRef& fullCref, bool externalInput, oms_modelState_enu_t modelState)
{
bool resourceAvailable = false;
for (auto &it : parameterResources)
{
for (auto &res : it.allresources)
{
//update the value in all resources, so that same cref in multiple ssv can be updated, this can result in duplication
auto integerValue = res.second.integerStartValues.find(cref);
if (integerValue != res.second.integerStartValues.end())
{
if (oms_modelState_simulation == modelState && externalInput)
{
res.second.integerValues[cref] = value;
}
else
{
res.second.setInteger(cref, value);
}
resourceAvailable = true;
}
}
}
if (!resourceAvailable)
{
auto &it = parameterResources.front();
for (auto &res : it.allresources)
{
// insert the new signal at the first resource available
res.second.setInteger(cref, value);
break;
}
}
return oms_status_ok;
}
oms_status_enu_t oms::Values::setBooleanResources(const ComRef& cref, bool value, const ComRef& fullCref, bool externalInput, oms_modelState_enu_t modelState)
{
bool resourceAvailable = false;
for (auto &it : parameterResources)
{
for (auto &res : it.allresources)
{
//update the value in all resources, so that same cref in multiple ssv can be updated, this can result in duplication
auto boolValue = res.second.booleanStartValues.find(cref);
if (boolValue != res.second.booleanStartValues.end())
{
if (oms_modelState_simulation == modelState && externalInput)
{
res.second.booleanValues[cref] = value;
}
else
{
res.second.setBoolean(cref, value);
}
resourceAvailable = true;
}
}
}
if (!resourceAvailable)
{
auto &it = parameterResources.front();
for (auto &res : it.allresources)
{
// insert the new signal at the first resource available
res.second.setBoolean(cref, value);
break;
}
}
return oms_status_ok;
}
oms_status_enu_t oms::Values::getRealResources(const ComRef& cref, double& value, bool externalInput, oms_modelState_enu_t modelState)
{
for (auto &it: parameterResources)
{
for (auto &res: it.allresources)
{
if (externalInput && oms_modelState_simulation == modelState && res.second.realValues[cref] != 0.0)
{
if (res.second.linkResources)
{
value = res.second.realValues[cref];
return oms_status_ok;
}
}
auto realValue = res.second.realStartValues.find(cref);
if (realValue != res.second.realStartValues.end())
{
if (res.second.linkResources)
{
value = realValue->second;
return oms_status_ok;
}
}
}
}
return oms_status_error;
}
oms_status_enu_t oms::Values::getIntegerResources(const ComRef& cref, int& value, bool externalInput, oms_modelState_enu_t modelState)
{
for (auto &it: parameterResources)
{
for (auto &res: it.allresources)
{
if (externalInput && oms_modelState_simulation == modelState && res.second.integerValues[cref] != 0)
{
if (res.second.linkResources)
{
value = res.second.integerValues[cref];
return oms_status_ok;
}
}
auto integerValue = res.second.integerStartValues.find(cref);
if (integerValue != res.second.integerStartValues.end())
{
if (res.second.linkResources)
{
value = integerValue->second;
return oms_status_ok;
}
}
}
}
return oms_status_error;
}
oms_status_enu_t oms::Values::getBooleanResources(const ComRef& cref, bool& value, bool externalInput, oms_modelState_enu_t modelState)
{
for (auto &it: parameterResources)
{
for (auto &res: it.allresources)
{
if (externalInput && oms_modelState_simulation == modelState && res.second.booleanValues[cref] != false)
{
if (res.second.linkResources)
{
value = res.second.booleanValues[cref];
return oms_status_ok;
}
}
auto booleanValue = res.second.booleanStartValues.find(cref);
if (booleanValue != res.second.booleanStartValues.end())
{
if (res.second.linkResources)
{
value = booleanValue->second;
return oms_status_ok;
}
}
}
}
return oms_status_error;
}
oms_status_enu_t oms::Values::getStringResources(const ComRef& cref, std::string& value, bool externalInput, oms_modelState_enu_t modelState)
{
for (auto &it: parameterResources)
{
for (auto &res: it.allresources)
{
if (externalInput && oms_modelState_simulation == modelState && res.second.stringValues[cref] != "")
{
if (res.second.linkResources)
{
value = res.second.stringValues[cref];
return oms_status_ok;
}
}
auto stringValue = res.second.stringStartValues.find(cref);
if (stringValue != res.second.stringStartValues.end())
{
if (res.second.linkResources)
{
value = stringValue->second;
return oms_status_ok;
}
}
}
}
return oms_status_error;
}
oms_status_enu_t oms::Values::getRealFromModeldescription(const ComRef& cref, double& value)
{
// search in modelDescription.xml
auto realValue = modelDescriptionRealStartValues.find(cref);
if (realValue != modelDescriptionRealStartValues.end())
{
value = realValue->second;
return oms_status_ok;
}
return oms_status_error;
}
oms_status_enu_t oms::Values::getStringFromModeldescription(const ComRef& cref, std::string& value)
{
// search in modelDescription.xml
auto stringValue = modelDescriptionStringStartValues.find(cref);
if (stringValue != modelDescriptionStringStartValues.end())
{
value = stringValue->second;
return oms_status_ok;
}
return oms_status_error;
}
std::string oms::Values::getUnit(ComRef& cref) const
{
// search in user provided map set
auto unitValue = variableUnits.find(cref);
if (unitValue != variableUnits.end())
return unitValue->second;
return "";
}
std::string oms::Values::getUnitFromModeldescription(ComRef& cref) const
{
// search in modelDescription.xml
auto unitValue = modelDescriptionVariableUnits.find(cref);
if (unitValue != modelDescriptionVariableUnits.end())
return unitValue->second;
return "";
}
oms_status_enu_t oms::Values::getIntegerFromModeldescription(const ComRef& cref, int& value)
{
// search in modelDescription.xml
auto integerValue = modelDescriptionIntegerStartValues.find(cref);
if (integerValue != modelDescriptionIntegerStartValues.end())
{
value = integerValue->second;
return oms_status_ok;
}
return oms_status_error;
}
oms_status_enu_t oms::Values::getBooleanFromModeldescription(const ComRef& cref, bool& value)
{
// search in modelDescription.xml
auto boolValue = modelDescriptionBooleanStartValues.find(cref);
if (boolValue != modelDescriptionBooleanStartValues.end())
{
value = boolValue->second;
return oms_status_ok;
}
return oms_status_error;
}
oms_status_enu_t oms::Values::deleteStartValue(const ComRef& cref)
{
oms::ComRef signal(cref);
signal.pop_suffix("start");
// reals
auto realValue = realStartValues.find(signal);
if (realValue != realStartValues.end())
{
realStartValues.erase(realValue);
return oms_status_ok;
}
// integers
auto integerValue = integerStartValues.find(signal);
if (integerValue != integerStartValues.end())
{
integerStartValues.erase(integerValue);
return oms_status_ok;
}
// booleans
auto boolValue = booleanStartValues.find(signal);
if (boolValue != booleanStartValues.end())
{
booleanStartValues.erase(boolValue);
return oms_status_ok;
}
return oms_status_error;
}
oms_status_enu_t oms::Values::updateOrDeleteStartValueInReplacedComponent(Values& value, const ComRef& owner, std::vector<std::string>& warningList)
{
for (auto &it : parameterResources)
{
for (auto &res : it.allresources)
{
// delete the unreferenced signals in ssm file
if (!res.second.ssmFile.empty())
{
for (auto mappedCref = res.second.mappedEntry.begin(); mappedCref != res.second.mappedEntry.end();)
{
ComRef front(mappedCref->second);
ComRef tail = front.pop_front();
//std::cout << "\n mappingEntry :" << mappedCref->first.c_str() << "==>" << mappedCref->second.c_str();
if (tail == owner || front.isEmpty())
{
double value_ = 0.0;
// check for front.isEmpty() means local resources and names does not have owner (e.g) A.u1 = u1
if (front.isEmpty())
front = mappedCref->second;
if (oms_status_ok != value.getRealFromModeldescription(front, value_))
{
std::string errorMsg = "deleting start value \"" + std::string(owner + front) + "\"" + " in \"" + std::string(res.second.ssmFile) + "\"" + " resources, because the identifier couldn't be resolved to any system signal in the replacing model";
logWarning(errorMsg);
warningList.push_back(errorMsg);
//res.second.realStartValues.erase(mappedCref->second); // NOTE: should we keep unreferenced signals in ssm and ssv when importing ?
mappedCref = res.second.mappedEntry.erase(mappedCref);
}
}
++ mappedCref;
}
}
// delete the unreferenced signals in ssv file
for (auto &name : res.second.realStartValues)
{
ComRef front(name.first);
ComRef tail = front.pop_front();
/* update new start values only, if signal does not have a mapping in .ssm,
because a signal with parameter mapping might be used on different signals and the users should update the
values with the replaced component
(e.g)
<ssv:Parameter name="paramA">
<ssv:Real value="-15" />
</ssv:Parameter>
to
<ssm:MappingEntry source="paramA" target="A.u" />
<ssm:MappingEntry source="paramA" target="A.t" />
*/
if (!res.second.isEntryReferencedInSSM(name.first) && !res.second.isEntryReferencedInSSM(front))
{
if (tail == owner || front.isEmpty())
{
double value_ = 0.0;
// check for front.isEmpty() means local resources and names does not have owner (e.g) A.u1 = u1
if (front.isEmpty())
front = name.first;
if (oms_status_ok == value.getRealFromModeldescription(front, value_))
{
// do not update the start values, as value set by user have higher priority
//res.second.realStartValues[name.first] = value_; // update the start value from the replaced component
}
else
{
std::string errorMsg = "deleting start value \"" + std::string(owner + front) + "\"" + " in \"" + std::string(res.first) + "\"" + " resources, because the identifier couldn't be resolved to any system signal in the replacing model";
logWarning(errorMsg);
warningList.push_back(errorMsg);
// if (res.second.ssmFile.empty()) should we keep the unreferenced signals in ssv which does not have any reference in ssm when importing?
res.second.realStartValues.erase(name.first); // delete the start value as signal does not exist in replaced component
}
}
}
}
}
}
return oms_status_ok;
}
oms_status_enu_t oms::Values::deleteStartValueInResources(const ComRef& cref)
{
oms::ComRef signal(cref);
signal.pop_suffix("start");
for (auto &it : parameterResources)
{
for (auto &res : it.allresources)
{
// reals
auto realValue = res.second.realStartValues.find(signal);
if (realValue != res.second.realStartValues.end())
{
res.second.realStartValues.erase(realValue);
return oms_status_ok;
}
// integers
auto integerValue = res.second.integerStartValues.find(signal);
if (integerValue != res.second.integerStartValues.end())
{
res.second.integerStartValues.erase(integerValue);
return oms_status_ok;
}
// booleans
auto boolValue = res.second.booleanStartValues.find(signal);
if (boolValue != res.second.booleanStartValues.end())
{
res.second.booleanStartValues.erase(boolValue);
return oms_status_ok;
}
}
}
return oms_status_error;
}
oms_status_enu_t oms::Values::deleteReferencesInSSD(const std::string &filename)
{
for (auto &it : parameterResources)
{
for (auto &res : it.allresources)
{
if (res.first.c_str() == "resources/"+ filename)
{
res.second.linkResources = false; //unlink the references
return oms_status_ok;
}
}
}
return oms_status_error;
}
oms_status_enu_t oms::Values::deleteResourcesInSSP(const std::string &filename)
{
for (auto &it : parameterResources)
{
for (auto &res : it.allresources)
{
if (res.first.c_str() == "resources/"+ filename)
{
res.second.linkResources = false; //unlink the references
it.allresources.erase("resources/"+ filename); // delete the resouces
return oms_status_ok;
}
}
}
return oms_status_error;
}
oms_status_enu_t oms::Values::exportToSSD(pugi::xml_node& node) const
{
// skip this if there is nothing to export
if (this->empty())
return oms_status_ok;
// Top level Parameter nodes
pugi::xml_node node_parameters_bindings = node.append_child(oms::ssp::Version1_0::ssd::parameter_bindings);
pugi::xml_node node_parameter_binding = node_parameters_bindings.append_child(oms::ssp::Version1_0::ssd::parameter_binding);
pugi::xml_node node_parameter_values = node_parameter_binding.append_child(oms::ssp::Version1_0::ssd::parameter_values);
pugi::xml_node node_parameterset = node_parameter_values.append_child(oms::ssp::Version1_0::ssv::parameter_set);
node_parameterset.append_attribute("version") = "1.0";
node_parameterset.append_attribute("name") = "parameters";
pugi::xml_node node_parameters = node_parameterset.append_child(oms::ssp::Version1_0::ssv::parameters);
exportStartValuesHelper(node_parameters);
exportParameterMappingInline(node_parameter_binding);
return oms_status_ok;
}
oms_status_enu_t oms::Values::importFromSnapshot(const Snapshot &snapshot, const std::string& ssvFilename, const std::string& ssmFilename)
{
// import new ssm references
if (!ssmFilename.empty())
{
pugi::xml_node ssm_parameterMapping = snapshot.getResourceNode(ssmFilename);
if (!ssm_parameterMapping)
return logError("loading <oms:file> \"" + ssmFilename + "\" from <oms:snapshot> failed");
importParameterMapping(ssm_parameterMapping);
}
// import new ssv references
pugi::xml_node parameterSet = snapshot.getResourceNode(ssvFilename);
if (!parameterSet)
return logError("loading <oms:file> \"" + ssvFilename + "\" from <oms:snapshot> failed");
pugi::xml_node ssv_node_units = parameterSet.child(oms::ssp::Version1_0::ssv::units);
importUnitDefinitions(ssv_node_units);
pugi::xml_node parameters = parameterSet.child(oms::ssp::Version1_0::ssv::parameters);
importStartValuesHelper(parameters);
return oms_status_ok;
}
oms_status_enu_t oms::Values::importFromSnapshot(const pugi::xml_node& node, const std::string& sspVersion, const Snapshot& snapshot, std::string variantName)
{
// get the ssdNode to parse UnitDefinitions in "SystemStructure.ssd"
pugi::xml_node ssdNode = snapshot.getResourceNode(variantName);
for (pugi::xml_node parameterBindingNode = node.child(oms::ssp::Version1_0::ssd::parameter_binding); parameterBindingNode; parameterBindingNode = parameterBindingNode.next_sibling(oms::ssp::Version1_0::ssd::parameter_binding))
{
Values resources; // create a new value object for each parameter binding node
std::string ssvFile = parameterBindingNode.attribute("source").as_string();
if (!ssvFile.empty()) // parameter binding provided with .ssv file
{
//resourceFiles.push_back(ssvFile);
pugi::xml_node parameterSet = snapshot.getResourceNode(ssvFile);
if (!parameterSet)
return logError("loading <oms:file> \"" + ssvFile + "\" from <oms:snapshot> failed");
pugi::xml_node parameters = parameterSet.child(oms::ssp::Version1_0::ssv::parameters);
// check for parameterMapping (e.g) <ssd:ParameterMapping>
pugi::xml_node ssd_parameterMapping = parameterBindingNode.child(oms::ssp::Version1_0::ssd::parameter_mapping);
if (ssd_parameterMapping)
{
// parameterMapping provided
std::string ssmFileSource = ssd_parameterMapping.attribute("source").as_string();
if (!ssmFileSource.empty())
{
pugi::xml_node ssm_parameterMapping = snapshot.getResourceNode(ssmFileSource);
if (!ssm_parameterMapping)
return logError("loading <oms:file> \"" + ssmFileSource + "\" from <oms:snapshot> failed");
resources.ssmFile = ssmFileSource;
resources.importParameterMapping(ssm_parameterMapping);
}
}
// import unit definitions from ssv resources
pugi::xml_node ssv_node_units = parameterSet.child(oms::ssp::Version1_0::ssv::units);
resources.importUnitDefinitions(ssv_node_units);
resources.importStartValuesHelper(parameters);
// add the mapped ssv parameter binding node
allresources[ssvFile] = resources;
}
else // inline ParameterBindings
{
if (parameterBindingNode.child(oms::ssp::Version1_0::ssv::parameter_set))
logWarning_deprecated;
pugi::xml_node parameterValues = parameterBindingNode.child(oms::ssp::Version1_0::ssd::parameter_values);
pugi::xml_node parameterSet = parameterValues.child(oms::ssp::Version1_0::ssv::parameter_set);
pugi::xml_node parameters = parameterSet.child(oms::ssp::Version1_0::ssv::parameters);
// check for parameterMapping (e.g) <ssd:ParameterMapping>
pugi::xml_node ssd_parameterMapping = parameterBindingNode.child(oms::ssp::Version1_0::ssd::parameter_mapping);
pugi::xml_node ssm_parameterMapping = ssd_parameterMapping.child(oms::ssp::Version1_0::ssm::parameter_mapping);
if (ssm_parameterMapping)
resources.importParameterMapping(ssm_parameterMapping);
// import unitdefinitions from ssd
pugi::xml_node ssd_node_units = ssdNode.child(oms::ssp::Draft20180219::ssd::units);
resources.importUnitDefinitions(ssd_node_units);
resources.importStartValuesHelper(parameters);
// add the mapped inline parameter binding node
allresources["inline"] = resources;
}
}
return oms_status_ok;
}
bool oms::Values::empty() const
{
return realStartValues.empty() && integerStartValues.empty() && booleanStartValues.empty();
}
oms_status_enu_t oms::Values::exportToSSV(pugi::xml_node& node) const
{
// skip this if there is nothing to export
if (this->empty())
return oms_status_ok;
exportStartValuesHelper(node);
return oms_status_ok;
}
oms_status_enu_t oms::Values::exportUnitDefinitions(Snapshot &snapshot, std::string filename, std::string variantName) const
{
if (unitDefinitionsToExportInSSP.empty())
return oms_status_ok;
pugi::xml_node node_units;
// export unitDefinitions to ssv resources
if (!filename.empty())
{
pugi::xml_node parameterSet = snapshot.getResourceNode(filename);
if (!parameterSet)
return logError("loading <oms:file> \"" + filename + "\" from <oms:snapshot> failed");
node_units = parameterSet.append_child(oms::ssp::Version1_0::ssv::units);
}
else
{
// export unitDefinitions inline
pugi::xml_node ssdNode = snapshot.getResourceNode(variantName);
if (!ssdNode)
return logError("loading <oms:file> \"" + variantName + "\"" + " from <oms:snapshot> failed");
node_units = ssdNode.append_child(oms::ssp::Draft20180219::ssd::units);
}
std::vector<std::string> unitList;
for (const auto &it : unitDefinitionsToExportInSSP)
{
if (it.exportUnit && std::find(unitList.begin(), unitList.end(), it.unitValue) == unitList.end())
{
pugi::xml_node ssc_unit = node_units.append_child(oms::ssp::Version1_0::ssc::unit);
ssc_unit.append_attribute("name") = it.unitValue.c_str();
pugi::xml_node ssc_base_unit = ssc_unit.append_child(oms::ssp::Version1_0::ssc::base_unit);
for (const auto &baseunit : it.baseUnit)
{
ssc_base_unit.append_attribute(baseunit.first.c_str()) = baseunit.second.c_str();
}
unitList.push_back(it.unitValue);
}
}
return oms_status_ok;
}
oms_status_enu_t oms::Values::exportUnitDefinitionsToSSVTemplate(Snapshot &snapshot, std::string filename)
{
if (modelDescriptionVariableUnits.empty())
return oms_status_ok;
pugi::xml_node parameterSet = snapshot.getResourceNode(filename);
if (!parameterSet)
return logError("loading <oms:file> \"" + filename + "\" from <oms:snapshot> failed");
pugi::xml_node node_units;
if (!parameterSet.child(oms::ssp::Version1_0::ssv::units))
node_units = parameterSet.append_child(oms::ssp::Version1_0::ssv::units);
else
node_units = parameterSet.last_child();
for (auto &it : modelDescriptionVariableUnits)
{
pugi::xml_node nodeExists = node_units.find_child_by_attribute(oms::ssp::Version1_0::ssc::unit, "name", it.second.c_str());
// check if node already exist to avoid duplicates of units
if (!nodeExists)
{
auto unitvalue = modeldescriptionUnitDefinitions.find(it.second);
if (unitvalue != modeldescriptionUnitDefinitions.end())
{
pugi::xml_node ssc_unit = node_units.append_child(oms::ssp::Version1_0::ssc::unit);
ssc_unit.append_attribute("name") = unitvalue->first.c_str();
pugi::xml_node ssc_base_unit = ssc_unit.append_child(oms::ssp::Version1_0::ssc::base_unit);
for (const auto &baseunit : unitvalue->second)
{
ssc_base_unit.append_attribute(baseunit.first.c_str()) = baseunit.second.c_str();
}
}
else
{
// set user defined units
pugi::xml_node ssc_unit = node_units.append_child(oms::ssp::Version1_0::ssc::unit);
ssc_unit.append_attribute("name") = it.second.c_str();
pugi::xml_node ssc_base_unit = ssc_unit.append_child(oms::ssp::Version1_0::ssc::base_unit);
}
}
}