/
NFConnector.mo
272 lines (243 loc) · 7.81 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
/*
* 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;
protected
import Origin = NFComponentRef.Origin;
import Connector = NFConnector;
import NFInstNode.InstNode;
import ElementSource;
import NFComponent.Component;
import NFClassTree.ClassTree;
import NFClass.Class;
import Restriction = NFRestriction;
import ComplexType = NFComplexType;
public
type Face = enumeration(INSIDE, OUTSIDE);
record CONNECTOR
ComponentRef name;
Type ty;
Face face;
ConnectorType 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);
algorithm
conn := CONNECTOR(ComponentRef.simplifySubscripts(cref), ty, face,
Component.connectorType(InstNode.component(node)), 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 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 isDeleted
input Connector conn;
output Boolean isDeleted = ComponentRef.isDeleted(conn.name);
end isDeleted;
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 hash
input Connector conn;
input Integer mod;
output Integer hash = ComponentRef.hash(conn.name, mod);
end hash;
function split
"Splits a connector into its primitive components."
input Connector conn;
output list<Connector> connl;
algorithm
connl := splitImpl(conn.name, conn.ty, conn.face, conn.source, conn.cty);
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 cty;
input output list<Connector> conns = {};
algorithm
conns := match ty
local
Type t;
ComplexType ct;
ClassTree tree;
case Type.COMPLEX(complexTy = ct as ComplexType.CONNECTOR())
algorithm
conns := splitImpl2(name, face, source, ct.potentials, conns);
conns := splitImpl2(name, face, source, ct.flows, conns);
conns := splitImpl2(name, face, source, ct.streams, conns);
then
conns;
case Type.COMPLEX()
algorithm
tree := Class.classTree(InstNode.getClass(ty.cls));
conns := splitImpl2(name, face, source,
arrayList(ClassTree.getComponents(tree)), conns);
then
conns;
case Type.ARRAY()
algorithm
if Flags.isSet(Flags.NF_SCALARIZE) then
t := Type.arrayElementType(ty);
for c in ComponentRef.scalarize(name) loop
conns := splitImpl(c, t, face, source, cty, conns);
end for;
else
if not Type.isEmptyArray(ty) then
conns := CONNECTOR(name, ty, face, cty, source) :: conns;
end if;
end if;
then
conns;
else CONNECTOR(name, ty, 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 output list<Connector> conns;
protected
Component c;
ComponentRef cref;
Type ty;
ConnectorType cty;
algorithm
for comp in comps loop
c := InstNode.component(comp);
ty := Component.getType(c);
cty := Component.connectorType(c);
cref := ComponentRef.append(ComponentRef.fromNode(comp, ty), name);
conns := splitImpl(cref, ty, face, source, cty, conns);
end for;
end splitImpl2;
annotation(__OpenModelica_Interface="frontend");
end NFConnector;