-
Notifications
You must be signed in to change notification settings - Fork 296
/
CodegenXML.tpl
3986 lines (3713 loc) · 132 KB
/
CodegenXML.tpl
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 defines templates for transforming flattened Modelica code to Xml code.
// + Optimica code to XML code
// @author Alachew Shitahun <alash325@student.liu.se> - Some of the template taken from CodegenC.tpl
package CodegenXML
import interface SimCodeTV;
import ExpressionDumpTpl;
/*********************************************************************
* SECTION: SIMULATION TARGET, ROOT TEMPLATE
*********************************************************************/
template translateModel(SimCode simCode)
"Generates root template for compiling a simulation of a Modelica model."
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
let() = textFile(generateXml(simCode), '<%dotPathXml(modelInfo.name)%>.xml')
"" //always returns an empty result since generated texts are written to files directly
end translateModel;
/*********************************************************************
* SECTION: SIMULATION TARGET, XML FILE SPECIFIC TEMPLATES
*********************************************************************/
template generateXml(SimCode simCode)
"Generates XML code for simulation file."
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
let guid = getUUIDStr()
<<
<?xml version="1.0" encoding="UTF-8"?>
<OpenModelicaModelDescription
xmlns:exp="https://svn.jmodelica.org/trunk/XML/daeExpressions.xsd"
xmlns:equ="https://svn.jmodelica.org/trunk/XML/daeEquations.xsd"
xmlns:fun="https://svn.jmodelica.org/trunk/XML/daeFunctions.xsd"
xmlns:opt="https://svn.jmodelica.org/trunk/XML/daeOptimization.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<%modelDescriptionXml(simCode,guid)%>
>
<%vendorAnnotationsXml(simCode)%>
<%defaultExperiment(simulationSettingsOpt)%>
<%modelVariablesXml(modelInfo)%>
<%bindingEquationsXml(modelInfo)%>
<%equationsXml(allEquations)%>
<%initialEquationsXml(modelInfo, initialEquations)%>
<%algorithmicEquationsXml(allEquations)%>
<%recordsXml(recordDecls)%>
<%functionsXml(modelInfo.functions)%>
<%objectiveFunctionXml(classAttributes, simCode)%>
</OpenModelicaModelDescription>
>>
end generateXml;
/***********************************************************************************
* SECTION: GENERATE XML for MODEL DESCRIPTION AND SCALAR VARIABLES IN SIMULATION FILE
*************************************************************************************/
template vendorAnnotationsXml(SimCode simCode)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(varInfo = VARINFO(__))) then
let generationTool= 'OpenModelica Compiler <%getVersionNr()%>'
<<
<VendorAnnotations>
<Tool name="<%generationTool%>"> </Tool>
</VendorAnnotations>
>>
end vendorAnnotationsXml;
template modelDescriptionXml(SimCode simCode, String guid) ::=
match simCode
case SIMCODE(modelInfo = MODELINFO(varInfo = VARINFO(__))) then
let fmiVersion = '1.0'
let modelName = dotPathXml(modelInfo.name)
let modelIdentifier = System.stringReplace(fileNamePrefix,".", "_")
let description = ''
let author = ''
let version= ''
let generationDateAndTime = xsdateTimeXml(getCurrentDateTime())
let variableNamingConvention= 'structured'
let numberOfContinuousStates =modelInfo.varInfo.numStateVars
let numberOfEventIndicators =modelInfo.varInfo.numZeroCrossings
<<
fmiVersion="<%fmiVersion%>"
modelName="<%modelName%>"
modelIdentifier="<%modelIdentifier%>"
guid="{<%guid%>}"
generationDateAndTime="<%generationDateAndTime%>"
variableNamingConvention="<%variableNamingConvention%>"
numberOfContinuousStates="<%numberOfContinuousStates%>"
numberOfEventIndicators="<%numberOfEventIndicators%>"
>>
end modelDescriptionXml;
template xsdateTimeXml(DateTime dt)
"YYYY-MM-DDThh:mm:ss"
::=
match dt
case DATETIME(__) then
<<
<%year%>-<%twodigit(mon)%>-<%twodigit(mday)%>T<%twodigit(hour)%>:<%twodigit(min)%>:<%twodigit(sec)%>
>>
end xsdateTimeXml;
template defaultExperiment(Option<SimulationSettings> simulationSettingsOpt)
"Generates code for defaultExperiment file for FMU target."
::=
match simulationSettingsOpt
case SOME(de as SIMULATION_SETTINGS(__)) then
<<
<DefaultExperiment startTime="<%de.startTime%>" stopTime="<%de.stopTime%>" tolerance="<%de.tolerance%>" />
>>
end defaultExperiment;
template modelVariablesXml(ModelInfo modelInfo)
"Generates XML code for ModelVariables file ."
::=
match modelInfo
case MODELINFO(vars=SIMVARS(__)) then
<<
<ModelVariables>
<%vars.stateVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%vars.derivativeVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%vars.algVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%vars.discreteAlgVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%vars.intAlgVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%vars.boolAlgVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%/*vars.inputVars |> var => ScalarVariableXml(var) ;separator="\n"*/%>
<%vars.outputVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%vars.aliasVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%vars.intAliasVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%vars.boolAliasVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%vars.paramVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%vars.intParamVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%vars.boolParamVars |> var => ScalarVariableXml(var) ;separator="\n"%>
<%vars.stringAlgVars |> var => ScalarVariableXml(var) ; separator="\n"%>
<%vars.stringParamVars |> var => ScalarVariableXml(var) ; separator="\n"%>
<%vars.stringAliasVars |> var => ScalarVariableXml(var) ; separator="\n"%>
<%vars.extObjVars |> var => ScalarVariableXml(var) ; separator="\n"%>
<%vars.constVars |> var => ScalarVariableXml(var) ; separator="\n"%>
<%vars.intConstVars |> var => ScalarVariableXml(var) ; separator="\n"%>
<%vars.boolConstVars |> var => ScalarVariableXml(var) ; separator="\n"%>
<%vars.stringConstVars |> var => ScalarVariableXml(var) ; separator="\n"%>
</ModelVariables><%\n%>
>>
end modelVariablesXml;
template ScalarVariableXml(SimVar simVar)
"Generates XML code for ScalarVariable file ."
::=
match simVar
case SIMVAR(__) then
<<
<%ScalarVariableAttributesXml(simVar)%>
>>
end ScalarVariableXml;
template ScalarVariableAttributesXml(SimVar simVar)
"Generates XML code for ScalarVariable Attribute file ."
::=
match simVar
case SIMVAR(__) then
let valueReference = '<%System.tmpTick()%>'
let variability = getVariablityXml(varKind)
let description = if comment then 'description="<%Util.escapeModelicaStringToXmlString(comment)%>"'
let alias = getAliasVarXml(aliasvar)
let caus = getCausalityXml(causality)
let variableCategory = variableCategoryXml(varKind)
<<
<ScalarVariable name="<%crefStrXml(name)%>" <%description%> valueReference="<%valueReference%>" variability="<%variability%>" causality="<%caus%>" alias="<%alias%>">
<%ScalarVariableTypeXml(type_,unit,displayUnit, minValue, maxValue, initialValue,isFixed)%>
<QualifiedName>
<%qualifiedNamePartXml(name)%>
</QualifiedName>
<isLinearTimedVariables>
<TimePoint index="0" isLinear="true"/>
</isLinearTimedVariables>
<VariableCategory><%variableCategory%></VariableCategory>
</ScalarVariable> <%\n%>
>>
end ScalarVariableAttributesXml;
template getCausalityXml(Causality c)
"Returns the Causality Attribute of ScalarVariable."
::=
match c
case NONECAUS(__) then "none"
case INTERNAL(__) then "internal"
case OUTPUT(__) then "output"
case INPUT(__) then "input"
end getCausalityXml;
template getVariablityXml(VarKind varKind)
"Returns the variablity Attribute of ScalarVariable."
::=
match varKind
case DISCRETE(__) then "discrete"
case PARAM(__) then "parameter"
case CONST(__) then "constant"
else "continuous"
end getVariablityXml;
template getAliasVarXml(AliasVariable aliasvar)
"Returns the alias Attribute of ScalarVariable."
::=
match aliasvar
case NOALIAS(__) then "noAlias"
case ALIAS(__) then '<%crefStrXml(varName)%>'
case NEGATEDALIAS(__) then '-<%crefStrXml(varName)%>'
else ""
end getAliasVarXml;
template variableCategoryXml(VarKind varKind)
"Returns the variable category of ScalarVariable."
::=
match varKind
case VARIABLE(__) then "algebraic"
case STATE(__) then "state"
case STATE_DER(__) then "derivative"
case DUMMY_DER(__) then "algebraic"
case DUMMY_STATE(__) then "algebraic"
case DISCRETE(__) then "algebraic"
case PARAM(__) then "independentParameter"
case CONST(__) then "independentConstant"
case EXTOBJ(__) then 'externalObject_<%dotPathXml(fullClassName)%>'
case JAC_VAR(__) then "jacobianVar"
case JAC_DIFF_VAR(__) then "jacobianDiffVar"
else error(sourceInfo(), "Unexpected simVarTypeName varKind")
end variableCategoryXml;
template ScalarVariableTypeXml(DAE.Type type_, String unit, String displayUnit, Option<DAE.Exp> minValue, Option<DAE.Exp> maxValue, Option<DAE.Exp> initialValue, Boolean isFixed)
"Generates XML code for ScalarVariable Type file."
::=
match type_
case T_INTEGER(__) then '<Integer <%ScalarVariableTypeCommonAttributeXml(initialValue,isFixed)%> <%ScalarVariableTypeMinAttribute(minValue)%> <%ScalarVariableTypeMaxAttribute(maxValue)%>/>'
case T_REAL(__) then '<Real <%ScalarVariableTypeCommonAttributeXml(initialValue,isFixed)%> <%ScalarVariableTypeMinAttribute(minValue)%> <%ScalarVariableTypeMaxAttribute(maxValue)%> <%ScalarVariableTypeRealAttributeXml(unit,displayUnit)%>/>'
case T_BOOL(__) then '<Boolean <%ScalarVariableTypeCommonAttributeXml(initialValue,isFixed)%>/>'
case T_STRING(__) then '<String <%ScalarVariableTypeCommonAttributeXml(initialValue,isFixed)%>/>'
case T_ENUMERATION(__) then '<Real <%ScalarVariableTypeCommonAttributeXml(initialValue,isFixed)%>/>'
else 'UNKOWN_TYPE'
end ScalarVariableTypeXml;
template ScalarVariableTypeCommonAttributeXml(Option<DAE.Exp> initialValue, Boolean isFixed)
"Generates XML code for ScalarVariable Type file ."
::=
match initialValue
case SOME(exp) then 'start="<%initValXml(exp)%>" fixed="<%isFixed%>"'
end ScalarVariableTypeCommonAttributeXml;
template ScalarVariableTypeMinAttribute(Option<DAE.Exp> minValue)
"generates code for min attribute"
::=
match minValue
case SOME(exp) then 'min="<%initValXml(exp)%>"'
end ScalarVariableTypeMinAttribute;
template ScalarVariableTypeMaxAttribute(Option<DAE.Exp> maxValue)
"generates code for max attribute"
::=
match maxValue
case SOME(exp) then 'max="<%initValXml(exp)%>"'
end ScalarVariableTypeMaxAttribute;
template initValXml(Exp initialValue)
"Returns initial value of ScalarVariable."
::=
match initialValue
case ICONST(__) then integer
case RCONST(__) then real
case SCONST(__) then '"<%Util.escapeModelicaStringToXmlString(string)%>"'
case BCONST(__) then (if bool then "1" else "0")
case ENUM_LITERAL(__) then '<%index%>'
case CREF(__) then crefStrXml(componentRef)
else "*ERROR* initial value of unknown type"
end initValXml;
template ScalarVariableTypeRealAttributeXml(String unit, String displayUnit)
"Generates XML code for ScalarVariable Type Real file ."
::=
let unit_ = if unit then 'unit="<%unit%>"'
let displayUnit_ = if displayUnit then 'displayUnit="<%displayUnit%>"'
<<
<%unit_%> <%displayUnit_%>
>>
end ScalarVariableTypeRealAttributeXml;
template contextCrefXml(ComponentRef cr, Context context)
"Generates XML code for a component reference depending on which context we're in."
::=
match context
case FUNCTION_CONTEXT(__) then System.unquoteIdentifier(crefStrXml(cr))
else crefXml(cr)
end contextCrefXml;
template contextIteratorNameXml(Ident name, Context context)
"Generates XML code for an iterator variable."
::=
match context
case FUNCTION_CONTEXT(__) then name
else name
end contextIteratorNameXml;
template crefXml(ComponentRef cr)
"Generates Xml equivalent name for component reference."
::=
match cr
case CREF_IDENT(ident = "xloc") then crefStrXml(cr)
case CREF_IDENT(ident = "time") then '<exp:Time>time</exp:Time>'
case WILD(__) then ''
else crefToXmlStr(cr)
end crefXml;
template qualifiedNamePartXml(ComponentRef cr)
"Generates XML code of the Qualified name of a variable . "
::=
match cr
case CREF_IDENT(__) then
let arrayTest = arraysubscriptsStrXml(subscriptLst)
if arrayTest then
<<
<exp:QualifiedNamePart name="<%ident%>">
<%arraysubscriptsStrXml(subscriptLst)%>
</exp:QualifiedNamePart>
>>
else
<<
<exp:QualifiedNamePart name="<%ident%>"/>
>>
case CREF_QUAL(ident = "$DER") then '<%qualifiedNamePartXml(componentRef)%>'
case CREF_QUAL(__) then
let arrayTest = arraysubscriptsStrXml(subscriptLst)
if arrayTest then
<<
<exp:QualifiedNamePart name="<%ident%>">
<%arraysubscriptsStrXml(subscriptLst)%>
<%qualifiedNamePartXml(componentRef)%>
</exp:QualifiedNamePart>
>>
else
<<
<exp:QualifiedNamePart name="<%ident%>"/>
<%qualifiedNamePartXml(componentRef)%>
>>
else "CREF_NOT_IDENT_OR_QUAL"
end qualifiedNamePartXml;
template arraysubscriptsStrXml(list<Subscript> subscripts)
"Generares XML code for subscript part of the name."
::=
if subscripts then
<<
<exp:ArraySubscripts>
<%subscripts |> s => arraysubscriptStrXml(s) ;separator="\n"%>
</exp:ArraySubscripts>
>>
else
<<>>
end arraysubscriptsStrXml;
template arraysubscriptStrXml(Subscript subscript)
"Generates a single subscript XML code.
Only works for constant integer indicies."
::=
match subscript
case INDEX(exp=ICONST(integer=i)) then
<<
<exp:IndexExpression>
<exp:IntegerLiteral><%i%></exp:IntegerLiteral>
</exp:IndexExpression>
>>
case SLICE(exp=ICONST(integer=i)) then
<<
<exp:IndexExpression>
<exp:IntegerLiteral><%i%></exp:IntegerLiteral>
</exp:IndexExpression>
>>
case WHOLEDIM(__) then "WHOLEDIM"
else "UNKNOWN_SUBSCRIPT"
end arraysubscriptStrXml;
template crefToXmlStr(ComponentRef cr)
"Helper function to crefXml"
::=
match cr
case CREF_IDENT(__) then
<<
<exp:Identifier>
<%qualifiedNamePartXml(cr)%>
</exp:Identifier>
>>
case CREF_QUAL(ident = "$DER") then
<<
<exp:Der>
<%crefToXmlStr(componentRef)%>
</exp:Der>
>>
case CREF_QUAL(__) then
<<
<exp:Identifier>
<%qualifiedNamePartXml(cr)%>
</exp:Identifier>
>>
case OPTIMICA_ATTR_INST_CREF(__) then
<<
<exp:TimedVariable timePointIndex = "0">
<%crefToXmlStr(componentRef)%>
<exp:Instant><%instant%></exp:Instant>
</exp:TimedVariable>
>>
case WILD(__) then ''
else "CREF_NOT_IDENT_OR_QUAL"
end crefToXmlStr;
template crefStrXml(ComponentRef cr)
"Generates the name of a variable for variable name array."
::=
match cr
case CREF_IDENT(__) then '<%ident%><%subscriptsStrXml(subscriptLst)%>'
case CREF_QUAL(ident = "$DER") then 'der(<%crefStrXml(componentRef)%>)'
case CREF_QUAL(ident = "$PRE") then 'pre(<%crefStrXml(componentRef)%>)'
case CREF_QUAL(__) then '<%ident%><%subscriptsStrXml(subscriptLst)%>.<%crefStrXml(componentRef)%>'
else "CREF_NOT_IDENT_OR_QUAL"
end crefStrXml;
template contextArrayCrefXml(ComponentRef cr, Context context)
"Generates XML code for an array component reference depending on the context."
::=
match context
case FUNCTION_CONTEXT(__) then arrayCrefStrXml(cr)
else arrayCrefXmlStr(cr)
end contextArrayCrefXml;
template arrayCrefXmlStr(ComponentRef cr)
::= '<%arrayCrefXmlStr2(cr)%>'
end arrayCrefXmlStr;
template arrayCrefXmlStr2(ComponentRef cr)
::=
match cr
case CREF_IDENT(__) then
<<
<exp:QualifiedName>
<exp:QualifiedNamePart name="<%unquoteIdentifier(ident)%>">
>>
case CREF_QUAL(__) then '<%unquoteIdentifier(ident)%><%subscriptsStrXml(subscriptLst)%>$P<%arrayCrefXmlStr2(componentRef)%>testing array'
else "CREF_NOT_IDENT_OR_QUAL"
end arrayCrefXmlStr2;
template arrayCrefStrXml(ComponentRef cr)
::=
match cr
case CREF_IDENT(__) then
<<
<exp:Identifier>
<exp:QualifiedNamepart name ="<%ident%>"/>
</exp:Identifier>
>>
case CREF_QUAL(__) then '<%ident%>.<%arrayCrefStrXml(componentRef)%>'
else "CREF_NOT_IDENT_OR_QUAL"
end arrayCrefStrXml;
template subscriptsStrXml(list<Subscript> subscripts)
"Generares subscript part of the name."
::=
if subscripts then
'[<%subscripts |> s => subscriptStrXml(s) ;separator=","%>]'
end subscriptsStrXml;
template subscriptStrXml(Subscript subscript)
"Generates a single subscript.
Only works for constant integer indicies."
::=
match subscript
case INDEX(exp=ICONST(integer=i)) then i
case SLICE(exp=ICONST(integer=i)) then i
case WHOLEDIM(__) then "WHOLEDIM"
else "UNKNOWN_SUBSCRIPT"
end subscriptStrXml;
template expCrefXml(DAE.Exp ecr)
::=
match ecr
case CREF(__) then crefXml(componentRef)
case CALL(path = IDENT(name = "der"), expLst = {arg as CREF(__)}) then
<<
<exp:Der>
<%crefXml(arg.componentRef)%>
</exp:Der>
>>
else "ERROR_NOT_A_CREF"
end expCrefXml;
template crefFunctionNameXml(ComponentRef cr)
::=
match cr
case CREF_IDENT(__) then
System.stringReplace(unquoteIdentifier(ident), "_", "__")
case CREF_QUAL(__) then
'<%System.stringReplace(unquoteIdentifier(ident), "_", "__")%>_<%crefFunctionNameXml(componentRef)%>'
end crefFunctionNameXml;
template dotPathXml(Path path)
"Generates paths with components separated by dots."
::=
match path
case QUALIFIED(__) then '<%name%>.<%dotPathXml(path)%>'
case IDENT(__) then name
case FULLYQUALIFIED(__) then dotPathXml(path)
end dotPathXml;
template replaceDotAndUnderscoreXml(String str)
"Replace _ with __ and dot in identifiers with _"
::=
match str
case name then
let str_dots = System.stringReplace(name,".", "_")
let str_underscores = System.stringReplace(str_dots, "_", "__")
System.unquoteIdentifier(str_underscores)
end replaceDotAndUnderscoreXml;
template underscorePathXml(Path path)
"Generate XML code for paths"
::=
match path
case QUALIFIED(__) then
<<
<exp:QualifiedNamePart name="<%name%>"/>
<%underscorePathXml(path)%>
>>
case IDENT(__) then
<<
<exp:QualifiedNamePart name="<%name%>"/>
>>
case FULLYQUALIFIED(__) then
<<
<%underscorePathXml(path)%>
>>
end underscorePathXml;
/*****************************************************************************
* SECTION: GENERATE All Function IN SIMULATION FILE
*****************************************************************************/
template bindingEquationsXml(ModelInfo modelInfo)
"Function for Binding Equations"
::=
match modelInfo
case MODELINFO(varInfo=VARINFO(numStateVars=numStateVars),vars=SIMVARS(__)) then
<<
<equ:BindingEquations>
<%vars.paramVars |> var => bindingEquationXml(var) ;separator="\n"%>
<%vars.intParamVars |> var => bindingEquationXml(var) ;separator="\n"%>
<%vars.boolParamVars |> var => bindingEquationXml(var) ;separator="\n"%>
<%vars.stringParamVars |> var => bindingEquationXml(var) ;separator="\n"%>
</equ:BindingEquations>
>>
end bindingEquationsXml;
template bindingEquationXml(SimVar var)
"Generate XML code for binding Equations"
::=
match var
case SIMVAR(__) then
let varName = '<%qualifiedNamePartXml(name)%>'
match initialValue
case SOME(exp) then
let &varDecls = buffer "" /*BUFD*/
let &preExp = buffer "" /*BUFD*/
<<
<equ:BindingEquation>
<equ:Parameter>
<%varName%>
</equ:Parameter>
<equ:BindingExp>
<%daeExpXml(exp, contextOther, &preExp, &varDecls)%>
</equ:BindingExp>
</equ:BindingEquation><%\n%>
>>
end bindingEquationXml;
template equationsXml(list<SimEqSystem> allEquationsPlusWhen)
"Function for all equations"
::=
let &varDecls = buffer "" /*BUFD*/
let jens = System.tmpTickReset(0)
let &tmp = buffer ""
let eqs = (allEquationsPlusWhen |> eq =>
equation_Xml(eq, contextSimulationDiscrete, &varDecls /*BUFD*/, &tmp)
;separator="\n")
<<
<equ:DynamicEquations>
<%&tmp%>
<%eqs%>
</equ:DynamicEquations>
>>
end equationsXml;
template algorithmicEquationsXml( list<SimEqSystem> allEquations)
"Generates XML for an equation that is an algorithm."
::=
let &varDecls = buffer "" /*BUFD*/
let algs = (allEquations |> eq =>
equationAlgorithmXml(eq, contextSimulationDiscrete, &varDecls /*BUFD*/)
;separator="\n")
<<
<fun:Algorithm>
<%algs%>
</fun:Algorithm>
>>
end algorithmicEquationsXml;
template equationAlgorithmXml(SimEqSystem eq, Context context, Text &varDecls /*BUFP*/)
"Generates XML for an equation that is an algorithm."
::=
match eq
case SES_ALGORITHM(__) then
let alg =(statements |> stmt =>
algStatementXml(stmt, contextFunction, &varDecls /*BUFD*/)
;separator="\n")
<<
<%alg%>
>>
end equationAlgorithmXml;
template initialEquationsXml(ModelInfo modelInfo, list<SimEqSystem> initialEqs)
"Function for Inititial Equations."
::=
match modelInfo
case MODELINFO(varInfo=VARINFO(numStateVars=numStateVars),vars=SIMVARS(__)) then
let &varDecls = buffer "" /*BUFD*/
let jens = System.tmpTickReset(0)
let &tmp = buffer ""
let eqs = (initialEqs |> eq =>
equation_Xml(eq, contextSimulationDiscrete, &varDecls /*BUFD*/, &tmp)
;separator="\n")
<<
<equ:InitialEquations>
<%vars.stateVars |> var => initialEquationXml(var) ;separator="\n"%>
<%vars.derivativeVars |> var => initialEquationXml(var) ;separator="\n"%>
<%vars.algVars |> var => initialEquationXml(var) ;separator="\n"%>
<%vars.discreteAlgVars |> var => initialEquationXml(var) ;separator="\n"%>
<%vars.intAlgVars |> var => initialEquationXml(var) ;separator="\n"%>
<%vars.boolAlgVars |> var => initialEquationXml(var) ;separator="\n"%>
<%vars.stringAlgVars |> var => initialEquationXml(var) ;separator="\n"%>
<%&tmp%>
<%eqs%>
</equ:InitialEquations>
>>
end initialEquationsXml;
template initialEquationXml(SimVar var)
"Generates XML code for Inititial Equations."
::=
match var
case SIMVAR(__) then
let identName = '<%crefXml(name)%>'
match initialValue
case SOME(exp) then
let &varDecls = buffer "" /*BUFD*/
let &preExp = buffer "" /*BUFD*/
<<
<equ:Equation>
<exp:Sub>
<%identName%>
<%daeExpXml(exp, contextOther, &preExp, &varDecls)%>
</exp:Sub>
</equ:Equation><%\n%>
>>
end initialEquationXml;
/*****************************************************************************
* SECTION: GENERATE All EQUATIONS IN SIMULATION FILE
*****************************************************************************/
template equation_Xml(SimEqSystem eq, Context context, Text &varDecls /*BUFP*/, Text &eqs)
"Generates an equation.
This template should not be used for a SES_RESIDUAL.
Residual equations are handled differently."
::=
match eq
case e as SES_MIXED(__) then " MIXED EQUATION NOT IMPLEMENTED "
case e as SES_ALGORITHM(statements={}) then " "
case e as SES_ALGORITHM(__) then " "
case e as SES_WHEN(__)
then equationWhenXml(e, context, &varDecls /*BUFD*/)
else
(
let ix = System.tmpTickIndex(10)
let &tmp = buffer ""
let &varD = buffer ""
let x = match eq
case e as SES_SIMPLE_ASSIGN(__)
case e as SES_SIMPLE_ASSIGN_CONSTRAINTS(__)
then equationSimpleAssignXml(e, context, &varD /*BUFD*/)
case e as SES_ARRAY_CALL_ASSIGN(__)
then equationArrayCallAssignXml(e, context, &varD /*BUFD*/)
case e as SES_IFEQUATION(__)
then 'IfEquation Assign Not implemente yet'
case e as SES_LINEAR(__)
then equationLinearXml(e, context, &varD /*BUFD*/)
case e as SES_NONLINEAR(__)
then equationNonlinearXml(e, context, &varD /*BUFD*/)
case e as SES_WHEN(__)
then " "
else
"NOT IMPLEMENTED EQUATION"
let &eqs +=
<<
<equ:Equation>
<exp:Sub>
<%x%>
</exp:Sub>
</equ:Equation> <%\n%>
>>
<<
>>
)
end equation_Xml;
template old_equation_Xml(SimEqSystem eq, Context context, Text &varDecls)
"Generates an equation.
This template should not be used for a SES_RESIDUAL.
Residual equations are handled differently."
::=
match eq
case e as SES_MIXED(__)
case e as SES_SIMPLE_ASSIGN(__)
case e as SES_SIMPLE_ASSIGN_CONSTRAINTS(__)
then equationSimpleAssignXml(e, context, &varDecls)
case e as SES_ARRAY_CALL_ASSIGN(__)
then equationArrayCallAssignXml(e, context, &varDecls)
case e as SES_ALGORITHM(__) then " "
case e as SES_LINEAR(__) then " equations are not implemented yet"
case e as SES_NONLINEAR(__) then "equations are not implemented yet "
case e as SES_WHEN(__)
then equationWhenXml(e, context, &varDecls)
else
"NOT IMPLEMENTED EQUATION"
end old_equation_Xml;
template equationSimpleAssignXml(SimEqSystem eq, Context context,
Text &varDecls /*BUFP*/)
"Generates an equation that is just a simple assignment."
::=
match eq
case SES_SIMPLE_ASSIGN(__)
case SES_SIMPLE_ASSIGN_CONSTRAINTS(__) then
let &preExp = buffer "" /*BUFD*/
let expPart = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let result = if preExp then preExp else expPart
<<
<%crefXml(cref)%>
<%result%>
>>
end equationSimpleAssignXml;
template equationArrayCallAssignXml(SimEqSystem eq, Context context,
Text &varDecls /*BUFP*/)
"Generates equation on form 'cref_array = call(...)'."
::=
<<
<%match eq
case eqn as SES_ARRAY_CALL_ASSIGN(lhs=lhs as CREF(__)) then
let &preExp = buffer "" /*BUFD*/
let expPart = daeExpXml(exp, context, &preExp /*BUF let &preExp = buffer "" /*BUFD*/
//let &helpInits = buffer "" /*BUFD*/
//let helpIf = (conditions |> e =>
// let helpInit = daeExpXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
//let &helpInits += ' <%helpInit%>'
// '';separator=" || ")C*/, &varDecls /*BUFD*/)
match expTypeFromExpShortXml(eqn.exp)
case "boolean" then
<<
<%expPart%>
<%crefXml(lhs.componentRef)%>
>>
case "integer" then
<<
<%expPart%>
<%crefXml(lhs.componentRef)%>
>>
case "real" then
<<
<%crefXml(lhs.componentRef)%>
<%expPart%>
>>
else error(sourceInfo(), 'No runtime support for this sort of array call: <%ExpressionDumpTpl.dumpExp(eqn.exp,"\"")%>')
%>
>>
end equationArrayCallAssignXml;
template equationLinearXml(SimEqSystem eq, Context context, Text &varDecls /*BUFP*/)
"Generates a when equation XML."
::=
match eq
case SES_LINEAR(lSystem=ls as LINEARSYSTEM(__)) then
<<
<%ls.simJac |> (row, col, eq as SES_RESIDUAL(__)) =>
let &preExp = buffer "" /*BUFD*/
let expPart = daeExpXml(eq.exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
'<%preExp%>
<%expPart%>' ;separator="\n"%>
<%ls.beqs |> exp hasindex i0 =>
let &preExp = buffer "" /*BUFD*/
let expPart = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
'<%preExp%>
<%expPart%>' ;separator="\n"%>
>>
end equationLinearXml;
template equationNonlinearXml(SimEqSystem eq, Context context, Text &varDecls /*BUFP*/)
"Generates a when equation XML."
::=
match eq
case SES_NONLINEAR(nlSystem=nls as NONLINEARSYSTEM(__)) then
let &varDecls = buffer "" /*BUFD*/
let &tmp = buffer ""
let prebody = (nls.eqs |> eq2 =>
functionExtraResidualsPreBody(eq2, &varDecls /*BUFD*/, &tmp)
;separator="\n")
let body = (nls.eqs |> eq2 as SES_RESIDUAL(__) hasindex i0 =>
let &preExp = buffer "" /*BUFD*/
let expPart = daeExpXml(eq2.exp, contextSimulationDiscrete,
&preExp /*BUFC*/, &varDecls /*BUFD*/)
'<%preExp%>
<%expPart%>;';separator="\n")
<<
<%&tmp%>
<%prebody%>
<%body%>
>>
end equationNonlinearXml;
template functionExtraResidualsPreBody(SimEqSystem eq, Text &varDecls /*BUFP*/, Text &eqs)
"Generates an equation."
::=
match eq
case e as SES_RESIDUAL(__) then ""
else equation_Xml(eq, contextSimulationDiscrete, &varDecls /*BUFD*/, &eqs)
end match
end functionExtraResidualsPreBody;
template equationWhenXml(SimEqSystem eq, Context context, Text &varDecls /*BUFP*/)
"Generates a when equation XML."
::=
match eq
case SES_WHEN(whenStmtLst = whenStmtLst, conditions=conditions,elseWhen = NONE()) then
let &preExp = buffer "" /*BUFD*/
let &helpInits = buffer "" /*BUFD*/
let helpIf = (conditions |> e =>
let helpInit = crefToXmlStr(e)
let &helpInits += '<%helpInit%><%\n%>'
'';separator="\n")
let body = whenOps(whenStmtLst, context, &varDecls /*BUFD*/)
let cond = if preExp then preExp else helpInits
<<
<equ:When>
<equ:Condition>
<%cond%>
</equ:Condition>
<equ:Equation>
<%body%>
</equ:Equation>
</equ:When>
>>
case SES_WHEN(whenStmtLst = whenStmtLst, conditions=conditions,elseWhen = SOME(elseWhenEq)) then
let &preExp = buffer "" /*BUFD*/
let &helpInits = buffer "" /*BUFD*/
let helpIf = (conditions |> e =>
let helpInit = crefToXmlStr(e)
let &helpInits += '<%helpInit%><%\n%>'
'';separator=" || ")
let body = whenOps(whenStmtLst, context, &varDecls /*BUFD*/)
let elseWhen = equationElseWhenXml(elseWhenEq,context,preExp,helpInits, varDecls)
let cond = if preExp then preExp else helpInits
<<
<equ:When>
<equ:Condition>
<%cond%>
</equ:Condition>
<equ:Equation>
<%body%>
</equ:Equation>
</equ:When>
<%elseWhen%>
>>
end equationWhenXml;
template equationElseWhenXml(SimEqSystem eq, Context context, Text &preExp /*BUFD*/, Text &helpInits /*BUFD*/, Text &varDecls /*BUFP*/)
"Generates a else when equation."
::=
match eq
case SES_WHEN(whenStmtLst = whenStmtLst, conditions=conditions,elseWhen = NONE()) then
let helpIf = (conditions |> e =>
let helpInit = crefToXmlStr(e)
let &helpInits += '<%helpInit%><%\n%>'
'';separator=" || ")
let body = whenOps(whenStmtLst, context, &varDecls /*BUFD*/)
let cond = if preExp then preExp else helpInits
<<
<equ:ElseWhen>
<equ:Condition>
<%cond%>
</equ:Condition>
<equ:Equation>
<%body%>
</equ:Equation>
</equ:ElseWhen>
>>
case SES_WHEN(whenStmtLst = whenStmtLst, conditions=conditions,elseWhen = SOME(elseWhenEq)) then
let helpIf = (conditions |> e =>
let helpInit = crefToXmlStr(e)
let &helpInits += '<%helpInit%><%\n%>'
'';separator=" || ")
let body = whenOps(whenStmtLst, context, &varDecls /*BUFD*/)
let elseWhen = equationElseWhenXml(elseWhenEq,context,preExp,helpInits, varDecls)
let cond = if preExp then preExp else helpInits
<<
<equ:ElseWhen>
<equ:Condition>
<%cond%>
</equ:Condition>
<equ:Equation>
<%body%>
</equ:Equation>
</equ:ElseWhen>
<%elseWhen%>
>>
end equationElseWhenXml;
template whenOps(list<WhenOperator> whenOps, Context context, Text &varDecls /*BUFP*/)
"Generates re-init statement for when equation."
::=
let body = (whenOps |> whenOp =>
match whenOp
case ASSIGN(left = lhls as DAE.CREF(componentRef = cr)) then
let &preExp = buffer "" /*BUFD*/
let exp = daeExpXml(right, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
<<
<exp:Sub>
<%crefXml(cr)%>
<%exp%>
</exp:Sub>
>>
case REINIT(__) then
let &preExp = buffer "" /*BUFD*/
let val = daeExpXml(value, contextSimulationDiscrete,
&preExp /*BUFC*/, &varDecls /*BUFD*/)
<<
<exp:Reinit>
<%crefXml(stateVar)%>
<%val%>
</exp:Reinit>
>>
case TERMINATE(__) then
let &preExp = buffer "" /*BUFD*/
let msgVar = daeExpXml(message, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)
<<
<%preExp%>
<%msgVar%>
>>
case ASSERT(source=SOURCE(info=info)) then
assertCommonXml(condition, message, contextSimulationDiscrete, &varDecls, info)
;separator="\n")
<<
<%body%>
>>
end whenOps;
/*****************************************************************************
* SECTION: GENERATE ALL RECORDS ( RECORD LIST) IN SIMULATION FILE
*****************************************************************************/
template recordsXml(list<RecordDeclaration> recordDecls)
"Generates XML code for all records."
::=