-
Notifications
You must be signed in to change notification settings - Fork 298
/
NFSections.mo
434 lines (387 loc) · 13.5 KB
/
NFSections.mo
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
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, 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.
*
*/
encapsulated uniontype NFSections
import Equation = NFEquation;
import Algorithm = NFAlgorithm;
import Statement = NFStatement;
import ComponentRef = NFComponentRef;
import Expression = NFExpression;
import SCode.Annotation;
protected
import Sections = NFSections;
import SCodeUtil;
import IOStream;
public
record SECTIONS
list<Equation> equations;
list<Equation> initialEquations;
list<Algorithm> algorithms;
list<Algorithm> initialAlgorithms;
end SECTIONS;
record EXTERNAL
String name;
list<Expression> args;
ComponentRef outputRef;
String language;
Option<Annotation> ann;
Boolean explicit;
end EXTERNAL;
record EMPTY end EMPTY;
function new
input list<Equation> equations;
input list<Equation> initialEquations;
input list<Algorithm> algorithms;
input list<Algorithm> initialAlgorithms;
output Sections sections;
algorithm
if listEmpty(equations) and listEmpty(initialEquations) and
listEmpty(algorithms) and listEmpty(initialAlgorithms) then
sections := EMPTY();
else
sections := SECTIONS(equations, initialEquations, algorithms, initialAlgorithms);
end if;
end new;
function prepend
input list<Equation> equations;
input list<Equation> initialEquations;
input list<Algorithm> algorithms;
input list<Algorithm> initialAlgorithms;
input output Sections sections;
algorithm
sections := match sections
case SECTIONS()
then SECTIONS(
listAppend(equations, sections.equations),
listAppend(initialEquations, sections.initialEquations),
listAppend(algorithms, sections.algorithms),
listAppend(initialAlgorithms, sections.initialAlgorithms));
else SECTIONS(equations, initialEquations, algorithms, initialAlgorithms);
end match;
end prepend;
function prependEquation
input Equation eq;
input output Sections sections;
input Boolean isInitial = false;
algorithm
sections := match sections
case SECTIONS()
algorithm
if isInitial then
sections.initialEquations := eq :: sections.initialEquations;
else
sections.equations := eq :: sections.equations;
end if;
then
sections;
case EMPTY()
then if isInitial then SECTIONS({}, {eq}, {}, {}) else SECTIONS({eq}, {}, {}, {});
else
algorithm
Error.assertion(false, getInstanceName() +
" got invalid Sections to prepend equation to", sourceInfo());
then
fail();
end match;
end prependEquation;
function prependAlgorithm
input Algorithm alg;
input output Sections sections;
input Boolean isInitial = false;
algorithm
sections := match sections
case SECTIONS()
algorithm
if isInitial then
sections.initialAlgorithms := alg :: sections.initialAlgorithms;
else
sections.algorithms := alg :: sections.algorithms;
end if;
then
sections;
case EMPTY()
then if isInitial then SECTIONS({}, {}, {}, {alg}) else SECTIONS({}, {}, {alg}, {});
else
algorithm
Error.assertion(false, getInstanceName() +
" got invalid Sections to prepend algorithm to", sourceInfo());
then
fail();
end match;
end prependAlgorithm;
function append
input list<Equation> equations;
input list<Equation> initialEquations;
input list<Algorithm> algorithms;
input list<Algorithm> initialAlgorithms;
input output Sections sections;
algorithm
sections := match sections
case SECTIONS()
then SECTIONS(
listAppend(sections.equations, equations),
listAppend(sections.initialEquations, initialEquations),
listAppend(sections.algorithms, algorithms),
listAppend(sections.initialAlgorithms, initialAlgorithms));
else SECTIONS(equations, initialEquations, algorithms, initialAlgorithms);
end match;
end append;
function join
input Sections sections1;
input Sections sections2;
output Sections sections;
algorithm
sections := match (sections1, sections2)
case (EMPTY(), _) then sections2;
case (_, EMPTY()) then sections1;
case (SECTIONS(), SECTIONS())
then SECTIONS(
listAppend(sections1.equations, sections2.equations),
listAppend(sections1.initialEquations, sections2.initialEquations),
listAppend(sections1.algorithms, sections2.algorithms),
listAppend(sections1.initialAlgorithms, sections2.initialAlgorithms));
end match;
end join;
function map
input output Sections sections;
input EquationFn eqFn;
input AlgorithmFn algFn;
input EquationFn ieqFn = eqFn;
input AlgorithmFn ialgFn = algFn;
partial function EquationFn
input output Equation eq;
end EquationFn;
partial function AlgorithmFn
input output Algorithm alg;
end AlgorithmFn;
protected
list<Equation> eq, ieq;
list<Algorithm> alg, ialg;
algorithm
() := match sections
case SECTIONS()
algorithm
eq := list(eqFn(e) for e in sections.equations);
ieq := list(ieqFn(e) for e in sections.initialEquations);
alg := list(algFn(a) for a in sections.algorithms);
ialg := list(ialgFn(a) for a in sections.initialAlgorithms);
sections := SECTIONS(eq, ieq, alg, ialg);
then
();
else ();
end match;
end map;
function map1<ArgT>
input output Sections sections;
input ArgT arg;
input EquationFn eqFn;
input AlgorithmFn algFn;
input EquationFn ieqFn = eqFn;
input AlgorithmFn ialgFn = algFn;
partial function EquationFn
input output Equation eq;
input ArgT arg;
end EquationFn;
partial function AlgorithmFn
input output Algorithm alg;
input ArgT arg;
end AlgorithmFn;
protected
list<Equation> eq, ieq;
list<Algorithm> alg, ialg;
algorithm
() := match sections
case SECTIONS()
algorithm
eq := list(eqFn(e, arg) for e in sections.equations);
ieq := list(ieqFn(e, arg) for e in sections.initialEquations);
alg := list(algFn(a, arg) for a in sections.algorithms);
ialg := list(ialgFn(a, arg) for a in sections.initialAlgorithms);
sections := SECTIONS(eq, ieq, alg, ialg);
then
();
else ();
end match;
end map1;
function mapExp
input output Sections sections;
input MapFn mapFn;
partial function MapFn
input output Expression exp;
end MapFn;
protected
list<Equation> eq, ieq;
list<Algorithm> alg, ialg;
algorithm
sections := match sections
case SECTIONS()
algorithm
eq := Equation.mapExpList(sections.equations, mapFn);
ieq := Equation.mapExpList(sections.initialEquations, mapFn);
alg := Algorithm.mapExpList(sections.algorithms, mapFn);
ialg := Algorithm.mapExpList(sections.initialAlgorithms, mapFn);
then
SECTIONS(eq, ieq, alg, ialg);
case EXTERNAL()
algorithm
sections.args := list(mapFn(e) for e in sections.args);
then
sections;
else sections;
end match;
end mapExp;
function foldExp<ArgT>
input Sections sections;
input FoldFn foldFn;
input output ArgT arg;
partial function FoldFn
input Expression exp;
input output ArgT arg;
end FoldFn;
algorithm
arg := match sections
case SECTIONS()
algorithm
arg := Equation.foldExpList(sections.equations, foldFn, arg);
arg := Equation.foldExpList(sections.initialEquations, foldFn, arg);
arg := Algorithm.foldExpList(sections.algorithms, foldFn, arg);
arg := Algorithm.foldExpList(sections.initialAlgorithms, foldFn, arg);
then
arg;
case EXTERNAL()
then List.fold(sections.args, foldFn, arg);
else arg;
end match;
end foldExp;
function apply
input Sections sections;
input EquationFn eqFn;
input AlgorithmFn algFn;
input EquationFn ieqFn = eqFn;
input AlgorithmFn ialgFn = algFn;
partial function EquationFn
input Equation eq;
end EquationFn;
partial function AlgorithmFn
input Algorithm alg;
end AlgorithmFn;
algorithm
() := match sections
case SECTIONS()
algorithm
for eq in sections.equations loop
eqFn(eq);
end for;
for ieq in sections.initialEquations loop
ieqFn(ieq);
end for;
for alg in sections.algorithms loop
algFn(alg);
end for;
for ialg in sections.initialAlgorithms loop
ialgFn(ialg);
end for;
then
();
else ();
end match;
end apply;
function isEmpty
input Sections sections;
output Boolean isEmpty;
algorithm
isEmpty := match sections
case EMPTY() then true;
else false;
end match;
end isEmpty;
function toFlatStream
input Sections sections;
input Absyn.Path scopeName;
input output IOStream.IOStream s;
protected
Annotation ann;
SCode.Mod mod, modLib, modInc, modLibDir, modIncDir;
algorithm
() := match sections
case SECTIONS()
algorithm
for alg in sections.algorithms loop
s := IOStream.append(s, "algorithm\n");
s := Statement.toFlatStreamList(alg.statements, " ", s);
end for;
then ();
case EXTERNAL()
algorithm
s := IOStream.append(s, "external \"");
s := IOStream.append(s, sections.language);
s := IOStream.append(s, "\"");
if sections.explicit then
if not ComponentRef.isEmpty(sections.outputRef) then
s := IOStream.append(s, " ");
s := IOStream.append(s, ComponentRef.toFlatString(sections.outputRef));
s := IOStream.append(s, " =");
end if;
s := IOStream.append(s, " ");
s := IOStream.append(s, sections.name);
s := IOStream.append(s, "(");
s := IOStream.append(s, stringDelimitList(list(Expression.toFlatString(e) for e in sections.args), ", "));
s := IOStream.append(s, ")");
end if;
if isSome(sections.ann) then
SOME(ann) := sections.ann;
mod := ann.modification;
modLib := SCodeUtil.filterSubMods(mod, function SCodeUtil.filterGivenSubModNames(namesToKeep={"Library"}));
modInc := SCodeUtil.filterSubMods(mod, function SCodeUtil.filterGivenSubModNames(namesToKeep={"Include"}));
if SCodeUtil.isEmptyMod(modLib) then
modLibDir := SCode.NOMOD();
else
modLibDir := SCodeUtil.filterSubMods(mod, function SCodeUtil.filterGivenSubModNames(namesToKeep={"LibraryDirectory"}));
if SCodeUtil.isEmptyMod(modLibDir) then
modLibDir := SCode.MOD(SCode.NOT_FINAL(), SCode.NOT_EACH(), {SCode.NAMEMOD("LibraryDirectory", SCode.MOD(SCode.NOT_FINAL(), SCode.NOT_EACH(), {}, SOME(Absyn.STRING("modelica://" + AbsynUtil.pathFirstIdent(scopeName) + "/Resources/Library")), Error.dummyInfo))}, NONE(), Error.dummyInfo);
end if;
end if;
if SCodeUtil.isEmptyMod(modInc) then
modIncDir := SCode.NOMOD();
else
modIncDir := SCodeUtil.filterSubMods(mod, function SCodeUtil.filterGivenSubModNames(namesToKeep={"IncludeDirectory"}));
if SCodeUtil.isEmptyMod(modLibDir) then
modLibDir := SCode.MOD(SCode.NOT_FINAL(), SCode.NOT_EACH(), {SCode.NAMEMOD("IncludeDirectory", SCode.MOD(SCode.NOT_FINAL(), SCode.NOT_EACH(), {}, SOME(Absyn.STRING("modelica://" + AbsynUtil.pathFirstIdent(scopeName) + "/Resources/Include")), Error.dummyInfo))}, NONE(), Error.dummyInfo);
end if;
end if;
ann.modification := SCodeUtil.mergeSCodeMods(SCodeUtil.mergeSCodeMods(modLib, modLibDir), SCodeUtil.mergeSCodeMods(modInc, modIncDir));
s := IOStream.append(s, SCodeDump.printAnnotationStr(SCode.COMMENT(SOME(ann), NONE())));
end if;
s := IOStream.append(s, ";\n");
then ();
end match;
end toFlatStream;
annotation(__OpenModelica_Interface="frontend");
end NFSections;