-
Notifications
You must be signed in to change notification settings - Fork 298
/
NFConnector.mo
336 lines (298 loc) · 10.2 KB
/
NFConnector.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
/*
* 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 NFConnector
import ComponentRef = NFComponentRef;
import Expression = NFExpression;
import Type = NFType;
import NFPrefixes.ConnectorType;
import NFPrefixes.Variability;
import DAE;
import Flags;
protected
import Origin = NFComponentRef.Origin;
import Connector = NFConnector;
import NFInstNode.InstNode;
import ElementSource;
import Component = NFComponent;
import NFClassTree.ClassTree;
import Class = NFClass;
import Restriction = NFRestriction;
import ComplexType = NFComplexType;
import Dimension = NFDimension;
public
type Face = enumeration(INSIDE, OUTSIDE);
record CONNECTOR
ComponentRef name;
Type ty;
Face face;
ConnectorType.Type cty;
DAE.ElementSource source;
end CONNECTOR;
function fromCref
input ComponentRef cref;
input Type ty;
input DAE.ElementSource source;
output Connector conn = fromFacedCref(cref, ty, crefFace(cref), source);
end fromCref;
function fromFacedCref
input ComponentRef cref;
input Type ty;
input Face face;
input DAE.ElementSource source;
output Connector conn;
protected
InstNode node = ComponentRef.node(cref);
Component comp;
ConnectorType.Type cty;
Restriction res;
algorithm
if InstNode.isComponent(node) then
comp := InstNode.component(node);
res := Class.restriction(InstNode.getClass(Component.classInstance(comp)));
cty := Component.connectorType(comp);
else
cty := intBitOr(ConnectorType.VIRTUAL, ConnectorType.POTENTIAL);
end if;
conn := CONNECTOR(ComponentRef.simplifySubscripts(cref), ty, face, cty, source);
end fromFacedCref;
function fromExp
"Constructs a list of Connectors from a cref or an array of crefs."
input Expression exp;
input DAE.ElementSource source;
input output list<Connector> conns = {};
algorithm
conns := match exp
case Expression.CREF() then fromCref(exp.cref, exp.ty, source) :: conns;
case Expression.ARRAY()
algorithm
for e in listReverse(exp.elements) loop
conns := fromExp(e, source, conns);
end for;
then
conns;
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown expression " +
Expression.toString(exp), sourceInfo());
then
fail();
end match;
end fromExp;
function getType
input Connector conn;
output Type ty = conn.ty;
end getType;
function getInfo
input Connector conn;
output SourceInfo info = conn.source.info;
end getInfo;
function variability
input Connector conn;
output Variability var =
Component.variability(InstNode.component(ComponentRef.node(conn.name)));
end variability;
function isEqual
input Connector conn1;
input Connector conn2;
output Boolean isEqual = ComponentRef.isEqual(conn1.name, conn2.name) and
conn1.face == conn2.face;
end isEqual;
function isPrefix
input Connector conn1;
input Connector conn2;
output Boolean isPrefix = ComponentRef.isPrefix(conn1.name, conn2.name);
end isPrefix;
function isNodeNameEqual
input Connector conn1;
input Connector conn2;
output Boolean isEqual = InstNode.name(ComponentRef.node(conn1.name)) ==
InstNode.name(ComponentRef.node(conn2.name));
end isNodeNameEqual;
function isOutside
input Connector conn;
output Boolean isOutside;
protected
Face f = conn.face; // Needed due to #4502
algorithm
isOutside := f == Face.OUTSIDE;
end isOutside;
function isInside
input Connector conn;
output Boolean isInside;
protected
Face f = conn.face; // Needed due to #4502
algorithm
isInside := f == Face.INSIDE;
end isInside;
function setOutside
input output Connector conn;
algorithm
if conn.face <> Face.OUTSIDE then
conn.face := Face.OUTSIDE;
end if;
end setOutside;
function isDeleted
input Connector conn;
output Boolean isDeleted = ComponentRef.isDeleted(conn.name);
end isDeleted;
function isExpandable
input Connector conn;
output Boolean isExpandable = ConnectorType.isExpandable(conn.cty);
end isExpandable;
function name
input Connector conn;
output ComponentRef name = conn.name;
end name;
function toString
input Connector conn;
output String str = ComponentRef.toString(conn.name);
end toString;
function faceString
input Connector conn;
output String str = if conn.face == Face.INSIDE then "inside" else "outside";
end faceString;
function hash
input Connector conn;
input Integer mod;
output Integer hash = ComponentRef.hash(conn.name, mod);
end hash;
type ScalarizeSetting = enumeration(
NONE "a[2].b[2] => {a[2].b[2]}",
PREFIX "a[2].b[2] => {a[1].b[2], a[2].b[2]}",
ALL "a[2].b[2] => {a[1].b[1], a[1].b[2], a[2].b[1], a[2].b[2]}"
);
function split
"Splits a connector into its primitive components."
input Connector conn;
input ScalarizeSetting scalarize = if Flags.isSet(Flags.NF_SCALARIZE) then
ScalarizeSetting.ALL else ScalarizeSetting.NONE;
output list<Connector> connl;
algorithm
connl := splitImpl(conn.name, conn.ty, conn.face, conn.source, conn.cty, scalarize);
end split;
protected
function crefFace
"Determines whether a cref refers to an inside or outside connector, where
an outside connector is a connector where the first part of the cref is a
connector, and an inside connector all other crefs."
input ComponentRef cref;
output Face face;
algorithm
face := match cref
// Simple identifiers must be connectors and thus outside.
case ComponentRef.CREF(restCref = ComponentRef.EMPTY()) then Face.OUTSIDE;
// Otherwise, check first part of the cref.
else if InstNode.isConnector(ComponentRef.node(ComponentRef.firstNonScope(cref)))
then Face.OUTSIDE else Face.INSIDE;
end match;
end crefFace;
function splitImpl
input ComponentRef name;
input Type ty;
input Face face;
input DAE.ElementSource source;
input ConnectorType.Type cty;
input ScalarizeSetting scalarize;
input output list<Connector> conns = {};
input list<Dimension> dims = {} "accumulated dimensions if splitArrays = false";
algorithm
conns := match ty
local
Type ety;
ComplexType ct;
ClassTree tree;
case Type.COMPLEX(complexTy = ct as ComplexType.CONNECTOR())
algorithm
conns := splitImpl2(name, face, source, ct.potentials, scalarize, conns, dims);
conns := splitImpl2(name, face, source, ct.flows, scalarize, conns, dims);
conns := splitImpl2(name, face, source, ct.streams, scalarize, conns, dims);
then
conns;
case Type.COMPLEX(complexTy = ComplexType.EXTERNAL_OBJECT())
then CONNECTOR(name, Type.liftArrayLeftList(ty, dims), face, cty, source) :: conns;
case Type.COMPLEX()
algorithm
tree := Class.classTree(InstNode.getClass(ty.cls));
conns := splitImpl2(name, face, source,
arrayList(ClassTree.getComponents(tree)), scalarize, conns, dims);
then
conns;
case Type.ARRAY(elementType = ety as Type.COMPLEX())
guard scalarize >= ScalarizeSetting.PREFIX
algorithm
for c in ComponentRef.scalarize(name) loop
conns := splitImpl(c, ety, face, source, cty, scalarize, conns, dims);
end for;
then
conns;
case Type.ARRAY(elementType = ety)
algorithm
if scalarize == ScalarizeSetting.ALL then
for c in ComponentRef.scalarize(name) loop
conns := splitImpl(c, ety, face, source, cty, scalarize, conns, dims);
end for;
else
if not Type.isEmptyArray(ty) then
conns := splitImpl(name, ety, face, source, cty, scalarize, conns,
listAppend(dims, ty.dimensions));
end if;
end if;
then
conns;
else CONNECTOR(name, Type.liftArrayLeftList(ty, dims), face, cty, source) :: conns;
end match;
end splitImpl;
function splitImpl2
input ComponentRef name;
input Face face;
input DAE.ElementSource source;
input list<InstNode> comps;
input ScalarizeSetting scalarize;
input output list<Connector> conns;
input list<Dimension> dims;
protected
Component c;
ComponentRef cref;
Type ty;
ConnectorType.Type cty;
algorithm
for comp in comps loop
c := InstNode.component(comp);
ty := Component.getType(c);
cty := Component.connectorType(c);
if not ConnectorType.isPotentiallyPresent(cty) then
cref := ComponentRef.append(ComponentRef.fromNode(comp, ty), name);
conns := splitImpl(cref, ty, face, source, cty, scalarize, conns, dims);
end if;
end for;
end splitImpl2;
annotation(__OpenModelica_Interface="frontend");
end NFConnector;