/
ConnectUtil.mo
4164 lines (3687 loc) · 130 KB
/
ConnectUtil.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
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-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 package ConnectUtil
" file: ConnectUtil.mo
package: ConnectUtil
description: Connection set management
RCS: $Id: ConnectUtil.mo 25082 2015-03-13 09:40:07Z lochel $
Connections generate connection sets (datatype SET is described in Connect)
which are constructed during instantiation. When a connection
set is generated, it is used to create a number of equations.
The kind of equations created depends on the type of the set.
ConnectUtil.mo is called from Inst.mo and is responsible for
creation of all connect-equations later passed to the DAE module
in DAEUtil.mo."
// public imports
public import Absyn;
public import SCode;
public import ClassInf;
public import Config;
public import Connect;
public import DAE;
public import FCore;
public import InnerOuter;
public import Prefix;
public import ConnectionGraph;
// protected imports
protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import Flags;
protected import List;
protected import Lookup;
protected import PrefixUtil;
protected import System;
protected import Types;
protected import Util;
protected import InstSection;
// Import some types from Connect.
public type Face = Connect.Face;
public type ConnectorType = Connect.ConnectorType;
public type ConnectorElement = Connect.ConnectorElement;
public type SetTrieNode = Connect.SetTrieNode;
public type SetTrie = Connect.SetTrie;
public type SetConnection = Connect.SetConnection;
public type OuterConnect = Connect.OuterConnect;
public type Sets = Connect.Sets;
public type Set = Connect.Set;
// Set graph represented as an adjacency list.
protected type SetGraph = array<list<Integer>>;
public function newSet
"This function creates a 'new' set for the given prefix. This means that it
makes a set with a new empty trie, but copies the set count and connection
crefs from the old set. This is done because we don't need to propagate
connections down in the instance hierarchy, but the list of connection crefs
needs to be propagated to be able to evaluate the cardinality operator. See
comments in addSet below for how the sets are merged later."
input Prefix.Prefix inPrefix;
input Sets inSets;
output Sets outSets;
algorithm
outSets := matchcontinue(inPrefix, inSets)
local
String pstr;
Integer sc;
DAE.ComponentRef cr;
case (_, Connect.SETS(setCount = sc))
equation
cr = PrefixUtil.prefixFirstCref(inPrefix);
pstr = ComponentReference.printComponentRefStr(cr);
then
Connect.SETS(Connect.SET_TRIE_NODE(pstr, cr, {}, 0), sc, {}, {});
case (_, Connect.SETS(setCount = sc))
then
Connect.SETS(Connect.SET_TRIE_NODE("", DAE.WILD(), {}, 0), sc, {}, {});
end matchcontinue;
end newSet;
public function addSet
"This function adds a child set to a parent set."
input Connect.Sets inParentSets;
input Connect.Sets inChildSets;
output Connect.Sets outSets;
algorithm
outSets := matchcontinue(inParentSets, inChildSets)
local
String name;
list<SetTrieNode> nodes;
list<SetConnection> c1, c2;
list<OuterConnect> o1, o2;
Integer sc, count;
SetTrieNode node;
DAE.ComponentRef cr;
// If the child set is empty we don't need to add it.
case (_, _)
equation
true = isEmptySet(inChildSets);
then
inParentSets;
// If both sets are nameless, i.e. a top scope set, just return the child
// set as it is. This is to avoid getting nestled top scope sets in some
// cases, and the child should be a superset of the parent.
case (Connect.SETS(sets = Connect.SET_TRIE_NODE(cref = DAE.WILD())),
Connect.SETS(sets = Connect.SET_TRIE_NODE(cref = DAE.WILD())))
then inChildSets;
// Check if the node already exists. In that case it's probably due to
// multiple inheritance and we should ignore it.
case (Connect.SETS(sets = Connect.SET_TRIE_NODE(nodes = nodes)),
Connect.SETS(sets = node))
equation
name = setTrieNodeName(node);
_ = setTrieGetNode(name, nodes);
then
inParentSets;
// In the normal case we add the trie on the child sets to the parent, and
// also merge their lists of outer connects.
case (Connect.SETS(Connect.SET_TRIE_NODE(name = name, cref = cr,
nodes = nodes, connectCount = count), _, c1, o1), Connect.SETS(node, sc, c2, o2))
equation
c1 = listAppend(c2, c1);
o1 = listAppend(o2, o1);
nodes = node :: nodes;
then
Connect.SETS(Connect.SET_TRIE_NODE(name, cr, nodes, count), sc, c1, o1);
end matchcontinue;
end addSet;
protected function isEmptySet
"Check if a given set is empty."
input Connect.Sets inSets;
output Boolean outIsEmpty;
algorithm
outIsEmpty := match(inSets)
case Connect.SETS(sets = Connect.SET_TRIE_NODE(nodes = {}),
connections = {}, outerConnects = {}) then true;
else false;
end match;
end isEmptySet;
protected function setSets
input SetTrie inTrie;
input Sets inSets;
output Sets outSets;
protected
Integer sc;
list<SetConnection> c;
list<OuterConnect> o;
algorithm
Connect.SETS(_, sc, c, o) := inSets;
outSets := Connect.SETS(inTrie, sc, c, o);
end setSets;
public function addConnection
"Adds a new connection by looking up both the given connector elements in the
set trie and merging the sets together."
input Sets inSets;
input DAE.ComponentRef inCref1;
input Face inFace1;
input DAE.ComponentRef inCref2;
input Face inFace2;
input SCode.ConnectorType inConnectorType;
input DAE.ElementSource inSource;
output Sets outSets;
algorithm
outSets := match(inSets, inCref1, inFace1, inCref2, inFace2, inConnectorType, inSource)
local
ConnectorElement e1, e2;
ConnectorType ty;
Sets sets;
case (_, _, _, _, _, _, _)
equation
ty = makeConnectorType(inConnectorType);
e1 = findElement(inCref1, inFace1, ty, inSource, inSets);
e2 = findElement(inCref2, inFace2, ty, inSource, inSets);
sets = mergeSets(e1, e2, inSets);
then
sets;
end match;
end addConnection;
protected function getConnectCount
input DAE.ComponentRef inCref;
input SetTrie inTrie;
output Integer outCount;
algorithm
outCount := matchcontinue(inCref, inTrie)
local
SetTrieNode node;
Integer count;
case (_, _)
equation
node = setTrieGet(inCref, inTrie, false);
then
getConnectCount2(node);
else 0;
end matchcontinue;
end getConnectCount;
protected function getConnectCount2
input SetTrieNode inNode;
output Integer outCount;
algorithm
outCount := match(inNode)
local
Integer count;
case Connect.SET_TRIE_NODE(connectCount = count) then count;
case Connect.SET_TRIE_LEAF(connectCount = count) then count;
end match;
end getConnectCount2;
public function addArrayConnection
"Connects two arrays of connectors."
input Connect.Sets inSets;
input DAE.ComponentRef inCref1;
input Connect.Face inFace1;
input DAE.ComponentRef inCref2;
input Connect.Face inFace2;
input DAE.ElementSource inSource;
input SCode.ConnectorType inConnectorType;
output Connect.Sets outSets;
algorithm
outSets :=
match(inSets, inCref1, inFace1, inCref2, inFace2, inSource, inConnectorType)
local
list<DAE.ComponentRef> crefs1, crefs2;
case (_, _, _, _, _, _, _)
equation
crefs1 = ComponentReference.expandCref(inCref1,false);
crefs2 = ComponentReference.expandCref(inCref2,false);
then
addArrayConnection2(inSets, crefs1, inFace1, crefs2, inFace2, inSource,
inConnectorType);
end match;
end addArrayConnection;
protected function addArrayConnection2
input Connect.Sets inSets;
input list<DAE.ComponentRef> inCrefs1;
input Connect.Face inFace1;
input list<DAE.ComponentRef> inCrefs2;
input Connect.Face inFace2;
input DAE.ElementSource inSource;
input SCode.ConnectorType inConnectorType;
output Connect.Sets outSets;
algorithm
outSets := match(inSets, inCrefs1, inFace1, inCrefs2, inFace2, inSource,
inConnectorType)
local
DAE.ComponentRef cref1, cref2;
list<DAE.ComponentRef> rest_crefs1, rest_crefs2;
Connect.Sets cs;
case (cs, cref1 :: rest_crefs1, _, cref2 :: rest_crefs2, _, _, _)
equation
cs = addConnection(cs, cref1, inFace1, cref2, inFace2, inConnectorType, inSource);
then
addArrayConnection2(cs, rest_crefs1, inFace1, rest_crefs2, inFace2,
inSource, inConnectorType);
else inSets;
end match;
end addArrayConnection2;
protected function makeConnectorType
"Creates a connector type from the flow or stream prefix given."
input SCode.ConnectorType inConnectorType;
output ConnectorType outType;
algorithm
outType := match(inConnectorType)
case SCode.POTENTIAL() then Connect.EQU();
case SCode.FLOW() then Connect.FLOW();
case SCode.STREAM() then Connect.STREAM(NONE());
else
equation
Error.addMessage(Error.INTERNAL_ERROR,
{"ConnectUtil.makeConnectorType: invalid connector type."});
then
fail();
end match;
end makeConnectorType;
public function addConnectorVariablesFromDAE
"If the class state indicates a connector, this function adds all flow
variables in the dae as inside connectors to the connection sets."
input Boolean inIgnore;
input ClassInf.State inClassState;
input Prefix.Prefix inPrefix;
input list<DAE.Var> inVars;
input Sets inConnectionSet;
input SourceInfo info;
input DAE.ElementSource inElementSource;
output Sets outConnectionSet;
algorithm
outConnectionSet :=
match(inIgnore, inClassState, inPrefix, inVars, inConnectionSet, info, inElementSource)
local
Absyn.Path class_path;
list<DAE.Var> vars, streams, flows;
Sets cs;
// check balance of non expandable connectors!
case (false, ClassInf.CONNECTOR(path = class_path, isExpandable = false), _, _, cs, _, _)
equation
checkConnectorBalance(inVars, class_path, info);
vars = if Flags.isSet(Flags.DISABLE_SINGLE_FLOW_EQ) then {} else inVars;
(flows, streams) = getStreamAndFlowVariables(vars, {}, {});
cs = List.fold2(flows, addFlowVariableFromDAE, inElementSource, inPrefix, cs);
cs = addStreamFlowAssociations(cs, inPrefix, streams, flows);
then
cs;
else inConnectionSet;
end match;
end addConnectorVariablesFromDAE;
protected function addFlowVariableFromDAE
"Adds a flow variable from the DAE to the sets as an inside flow variable."
input DAE.Var inVariable;
input DAE.ElementSource inElementSource;
input Prefix.Prefix inPrefix;
input Sets inConnectionSet;
output Sets outConnectionSet;
protected
list<DAE.ComponentRef> crefs;
algorithm
crefs := daeVarToCrefs(inVariable);
outConnectionSet := List.fold2r(crefs, addInsideFlowVariable,
inElementSource, inPrefix, inConnectionSet);
end addFlowVariableFromDAE;
public function isExpandable
input DAE.ComponentRef inName;
output Boolean isExpandableConnector;
algorithm
isExpandableConnector := match(inName)
local
DAE.Type ty;
Boolean b;
DAE.ComponentRef cr;
case (DAE.CREF_IDENT(identType = ty))
equation
b = InstSection.isExpandableConnectorType(ty);
then
b;
case (DAE.CREF_QUAL(identType = ty, componentRef = cr))
equation
b = InstSection.isExpandableConnectorType(ty);
b = diveIsExpandable(b, cr);
then
b;
else false;
end match;
end isExpandable;
protected function diveIsExpandable
"@author: adrpo
if isExpandable is true don't dive in"
input Boolean inIsExpandable;
input DAE.ComponentRef inName;
output Boolean b;
algorithm
b := match(inIsExpandable, inName)
case (true, _) then true;
case (false, _)
equation
b = isExpandable(inName);
then
b;
end match;
end diveIsExpandable;
protected function daeHasExpandableConnectors
"Goes through a list of variables and returns their crefs"
input DAE.DAElist inDAE;
output Boolean hasExpandable;
algorithm
hasExpandable := matchcontinue(inDAE)
local
list<DAE.Element> vars;
DAE.ComponentRef name;
Boolean b;
// if we didn't detect any there aren't any
case (_)
equation
false = System.getHasExpandableConnectors();
then false;
case (DAE.DAE(vars)) then List.exist(vars, isVarExpandable);
end matchcontinue;
end daeHasExpandableConnectors;
protected function isVarExpandable
input DAE.Element var;
output Boolean b;
algorithm
b := match var
local
DAE.ComponentRef name;
case DAE.VAR(componentRef = name) then isExpandable(name);
else false;
end match;
end isVarExpandable;
protected function getExpandableVariablesWithNoBinding
"@author: adrpo
Goes through a list of expandable variables
THAT HAVE NO BINDING and returns their crefs"
input list<DAE.Element> inVariables;
input list<DAE.ComponentRef> inAccPotential;
output list<DAE.ComponentRef> outPotential;
algorithm
outPotential := match (inVariables, inAccPotential)
local
list<DAE.Element> rest_vars;
DAE.ComponentRef name;
list<DAE.ComponentRef> potential;
Option<DAE.Exp> bnd;
case ({}, _) then (inAccPotential);
// do not return the ones that have a binding as they are used
// TODO: actually only if their binding is not another expandable??!!
case (DAE.VAR(componentRef = name, binding = bnd) :: rest_vars, _)
equation
potential =
if isSome(bnd)
then inAccPotential
else List.consOnTrue(isExpandable(name), name, inAccPotential);
potential = getExpandableVariablesWithNoBinding(rest_vars, potential);
then
potential;
case (_::rest_vars, _)
equation
potential = getExpandableVariablesWithNoBinding(rest_vars, inAccPotential);
then
potential;
end match;
end getExpandableVariablesWithNoBinding;
protected function getStreamAndFlowVariables
"Goes through a list of variables and filters out all flow and stream
variables into separate lists."
input list<DAE.Var> inVariable;
input list<DAE.Var> inAccFlows;
input list<DAE.Var> inAccStreams;
output list<DAE.Var> outFlows;
output list<DAE.Var> outStreams;
algorithm
(outFlows, outStreams) := match(inVariable, inAccFlows, inAccStreams)
local
DAE.Var var;
list<DAE.Var> rest_vars, flows, streams;
case ({}, _, _) then (inAccFlows, inAccStreams);
case ((var as DAE.TYPES_VAR(attributes = DAE.ATTR(
connectorType = SCode.FLOW()))) :: rest_vars, _, _)
equation
(flows, streams) =
getStreamAndFlowVariables(rest_vars, var :: inAccFlows, inAccStreams);
then
(flows, streams);
case ((var as DAE.TYPES_VAR(attributes = DAE.ATTR(
connectorType = SCode.STREAM()))) :: rest_vars, _, _)
equation
(flows, streams) =
getStreamAndFlowVariables(rest_vars, inAccFlows, var :: inAccStreams);
then
(flows, streams);
case (_ :: rest_vars, _ ,_)
equation
(flows, streams) =
getStreamAndFlowVariables(rest_vars, inAccFlows, inAccStreams);
then
(flows, streams);
end match;
end getStreamAndFlowVariables;
protected function addStreamFlowAssociations
"Adds information to the connection sets about which flow variables each
stream variable is associated to."
input Sets inSets;
input Prefix.Prefix inPrefix;
input list<DAE.Var> inStreamVars;
input list<DAE.Var> inFlowVars;
output Sets outSets;
algorithm
outSets := match(inSets, inPrefix, inStreamVars, inFlowVars)
local
DAE.Var flow_var;
DAE.ComponentRef flow_cr;
list<DAE.ComponentRef> stream_crs;
Sets sets;
// No stream variables => not a stream connector.
case (_, _, {}, _) then inSets;
// Stream variables and exactly one flow => add associations.
case (_, _, _, {flow_var})
equation
{flow_cr} = daeVarToCrefs(flow_var);
flow_cr = PrefixUtil.prefixCrefNoContext(inPrefix, flow_cr);
stream_crs = List.mapFlat(inStreamVars, daeVarToCrefs);
sets = List.fold1(stream_crs, addStreamFlowAssociation,
flow_cr, inSets);
then sets;
end match;
end addStreamFlowAssociations;
protected function daeVarToCrefs
"Converts a DAE.Var to a list of crefs."
input DAE.Var inVar;
output list<DAE.ComponentRef> outCrefs;
protected
String name;
DAE.Type ty;
list<DAE.ComponentRef> crefs;
DAE.Dimensions dims;
DAE.ComponentRef cr;
algorithm
DAE.TYPES_VAR(name = name, ty = ty) := inVar;
ty := Types.derivedBasicType(ty);
outCrefs := match ty
// Scalar
case DAE.T_REAL() then {DAE.CREF_IDENT(name, ty, {})};
// Complex type
case DAE.T_COMPLEX()
algorithm
crefs := listAppend(daeVarToCrefs(v) for v in listReverse(ty.varLst));
cr := DAE.CREF_IDENT(name, DAE.T_REAL_DEFAULT, {});
then
list(ComponentReference.joinCrefs(cr, c) for c in crefs);
// Array
case DAE.T_ARRAY()
algorithm
dims := Types.getDimensions(ty);
cr := DAE.CREF_IDENT(name, ty, {});
then
expandArrayCref(cr, dims, {});
else
algorithm
Error.addInternalError("Unknown var " + name +
" in ConnectUtil.daeVarToCrefs", sourceInfo());
then
fail();
end match;
end daeVarToCrefs;
protected function expandArrayCref
"This function takes an array cref and a list of dimensions, and generates all
scalar crefs by expanding the dimensions into subscripts."
input DAE.ComponentRef inCref;
input DAE.Dimensions inDims;
input list<DAE.ComponentRef> inAccumCrefs;
output list<DAE.ComponentRef> outCrefs;
algorithm
outCrefs := matchcontinue(inCref, inDims, inAccumCrefs)
local
DAE.Dimension dim;
DAE.Dimensions dims;
DAE.Exp idx;
DAE.ComponentRef cr;
list<DAE.ComponentRef> crefs;
case (_, {}, _) then inCref :: inAccumCrefs;
case (_, dim :: dims, _)
equation
(idx, dim) = getNextIndex(dim);
cr = ComponentReference.subscriptCref(inCref, {DAE.INDEX(idx)});
crefs = expandArrayCref(cr, dims, inAccumCrefs);
crefs = expandArrayCref(inCref, dim :: dims, crefs);
then
crefs;
else inAccumCrefs;
end matchcontinue;
end expandArrayCref;
protected function reverseEnumType
"Reverses the order of the literals in an enumeration dimension, or just
returns the given dimension if it's not an enumeration. This is used by
getNextIndex that starts from the end, so that it can take the first literal
in the list instead of the last (more efficient)."
input DAE.Dimension inDim;
output DAE.Dimension outDim;
algorithm
outDim := match(inDim)
local
Absyn.Path p;
list<String> lits;
Integer dim_size;
case DAE.DIM_ENUM(p, lits, dim_size)
equation
lits = listReverse(lits);
then DAE.DIM_ENUM(p, lits, dim_size);
else inDim;
end match;
end reverseEnumType;
protected function getNextIndex
"Returns the next index given a dimension, and updates the dimension. Fails
when there are no indices left."
input DAE.Dimension inDim;
output DAE.Exp outNextIndex;
output DAE.Dimension outDim;
algorithm
(outNextIndex, outDim) := match(inDim)
local
Integer new_idx, dim_size;
Absyn.Path p, ep;
String l;
list<String> l_rest;
case DAE.DIM_INTEGER(integer = 0) then fail();
case DAE.DIM_ENUM(size = 0) then fail();
case DAE.DIM_INTEGER(integer = new_idx)
equation
dim_size = new_idx - 1;
then
(DAE.ICONST(new_idx), DAE.DIM_INTEGER(dim_size));
// Assumes that the enum has been reversed with reverseEnumType.
case DAE.DIM_ENUM(p, l :: l_rest, new_idx)
equation
ep = Absyn.joinPaths(p, Absyn.IDENT(l));
dim_size = new_idx - 1;
then
(DAE.ENUM_LITERAL(ep, new_idx), DAE.DIM_ENUM(p, l_rest, dim_size));
end match;
end getNextIndex;
protected function addInsideFlowVariable
"Adds a single inside flow variable to the connection sets."
input Connect.Sets inSets;
input DAE.ComponentRef inCref;
input DAE.ElementSource inSource;
input Prefix.Prefix inPrefix;
output Connect.Sets outSets;
algorithm
outSets := matchcontinue(inSets, inCref, inSource, inPrefix)
local
ConnectorElement e;
SetTrie sets;
Integer sc;
list<SetConnection> c;
list<OuterConnect> o;
SourceInfo info;
DAE.ElementSource src;
// Check if it exists in the sets already.
case (Connect.SETS(sets = sets), _, _, _)
equation
_ = setTrieGetElement(inCref, Connect.INSIDE(), sets);
then
inSets;
// Otherwise, add a new set for it.
case (Connect.SETS(sets, sc, c, o), _, DAE.SOURCE(), _)
equation
sc = sc + 1;
//src = DAEUtil.addAdditionalComment(inSource, " add inside flow(" +
// PrefixUtil.printPrefixStr(inPrefix) + "/" +
// ComponentReference.printComponentRefStr(inCref) +
// ")");
e = newElement(inCref, Connect.INSIDE(), Connect.FLOW(), inSource, sc);
sets = setTrieAdd(e, sets);
then
Connect.SETS(sets, sc, c, o);
end matchcontinue;
end addInsideFlowVariable;
protected function addStreamFlowAssociation
"Adds an association between a stream variable and a flow."
input DAE.ComponentRef inStreamCref;
input DAE.ComponentRef inFlowCref;
input Connect.Sets inSets;
output Connect.Sets outSets;
algorithm
outSets := updateSetLeaf(inSets, inStreamCref, inFlowCref,
addStreamFlowAssociation2);
end addStreamFlowAssociation;
protected function addStreamFlowAssociation2
"Helper function to addSTreamFlowAssocication, sets the flow association in a
leaf node."
input DAE.ComponentRef inFlowCref;
input SetTrieNode inNode;
output SetTrieNode outNode;
algorithm
outNode := match(inFlowCref, inNode)
local
String name;
Option<ConnectorElement> ie, oe;
Integer c;
case (_, Connect.SET_TRIE_LEAF(name, ie, oe, _, c))
then Connect.SET_TRIE_LEAF(name, ie, oe, SOME(inFlowCref), c);
end match;
end addStreamFlowAssociation2;
protected function getStreamFlowAssociation
"Returns the associated flow variable for a stream variable."
input DAE.ComponentRef inStreamCref;
input Connect.Sets inSets;
output DAE.ComponentRef outFlowCref;
protected
SetTrie sets;
algorithm
Connect.SETS(sets = sets) := inSets;
Connect.SET_TRIE_LEAF(flowAssociation = SOME(outFlowCref)) :=
setTrieGet(inStreamCref, sets, false);
end getStreamFlowAssociation;
public function addOuterConnect
"Adds an outer connect to a Connect.Sets."
input Sets inSets;
input OuterConnect inOuterConnect;
output Sets outSets;
protected
SetTrie sets;
Integer sc;
list<SetConnection> c;
list<OuterConnect> o;
algorithm
Connect.SETS(sets, sc, c, o) := inSets;
outSets := Connect.SETS(sets, sc, c, inOuterConnect :: o);
end addOuterConnect;
public function setOuterConnects
"Sets the outer connect part of a Connect.Sets."
input Sets inSets;
input list<OuterConnect> inOuterConnects;
output Sets outSets;
protected
SetTrie sets;
Integer sc;
list<SetConnection> c;
algorithm
Connect.SETS(sets, sc, c, _) := inSets;
outSets := Connect.SETS(sets, sc, c, inOuterConnects);
end setOuterConnects;
public function addOuterConnection
"Adds a connection with a reference to an outer connector These are added to a
special list, such that they can be moved up in the instance hierarchy to a
place where both instances are defined."
input Prefix.Prefix scope;
input Connect.Sets sets;
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
input Absyn.InnerOuter io1;
input Absyn.InnerOuter io2;
input Connect.Face f1;
input Connect.Face f2;
input DAE.ElementSource source;
output Connect.Sets outSets;
algorithm
outSets := matchcontinue(scope,sets,cr1,cr2,io1,io2,f1,f2,source)
local
list<Connect.OuterConnect> oc;
Connect.OuterConnect new_oc;
// First check if already added
case(_, Connect.SETS(outerConnects = oc),_,_,_,_,_,_,_)
equation
_::_ = List.select2(oc,outerConnectionMatches,cr1,cr2);
then sets;
// add the outerconnect
else
equation
new_oc = Connect.OUTERCONNECT(scope, cr1, io1, f1, cr2, io2, f2, source);
then addOuterConnect(sets, new_oc);
end matchcontinue;
end addOuterConnection;
protected function outerConnectionMatches
"Returns true if Connect.OuterConnect matches the two component references
passed as argument."
input Connect.OuterConnect oc;
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Boolean matches;
algorithm
matches := match(oc,cr1,cr2)
local DAE.ComponentRef cr11,cr22;
case(Connect.OUTERCONNECT(cr1=cr11,cr2=cr22),_,_)
equation
matches =
ComponentReference.crefEqual(cr11,cr1) and ComponentReference.crefEqual(cr22,cr2) or
ComponentReference.crefEqual(cr11,cr2) and ComponentReference.crefEqual(cr22,cr1);
then matches;
end match;
end outerConnectionMatches;
public function addDeletedComponent
"Marks a component as deleted in the sets."
input String inComponentName;
input Sets inSets;
output Sets outSets;
protected
SetTrie sets;
Integer sc, count;
list<SetConnection> c;
list<OuterConnect> o;
String name;
DAE.ComponentRef cref;
list<SetTrieNode> nodes;
algorithm
Connect.SETS(sets, sc, c, o) := inSets;
Connect.SET_TRIE_NODE(name, cref, nodes, count) := sets;
nodes := Connect.SET_TRIE_DELETED(inComponentName) :: nodes;
sets := Connect.SET_TRIE_NODE(name, cref, nodes, count);
outSets := Connect.SETS(sets, sc, c, o);
end addDeletedComponent;
protected function isDeletedComponent
"Checks if the given component is deleted or not."
input DAE.ComponentRef inComponent;
input SetTrie inSets;
protected
DAE.ComponentRef cr;
algorithm
// Send true as last argument to setTrieGet, so that it also matches any
// prefix of the cref in case the cref is a subcomponent of a deleted component.
//cr := ComponentReference.crefStripSubs(inComponent);
Connect.SET_TRIE_DELETED() := setTrieGet(inComponent, inSets, true);
end isDeletedComponent;
public function connectionContainsDeletedComponents
"Checks if a connection contains a deleted component, i.e. if either of the
given crefs belong to a deleted component."
input DAE.ComponentRef inComponent1;
input DAE.ComponentRef inComponent2;
input Sets inSets;
output Boolean containsDeletedComponent;
algorithm
containsDeletedComponent := matchcontinue(inComponent1, inComponent2, inSets)
local
SetTrie sets;
// No sets, so nothing can be deleted.
case (_, _, Connect.SETS(sets = Connect.SET_TRIE_NODE(nodes = {})))
then
false;
// Check if the first component is deleted.
case (_, _, Connect.SETS(sets = sets))
equation
isDeletedComponent(inComponent1, sets);
then
true;
// Check if the second component is deleted.
case (_, _, Connect.SETS(sets = sets))
equation
isDeletedComponent(inComponent2, sets);
then
true;
else false;
end matchcontinue;
end connectionContainsDeletedComponents;
public function addOuterConnectToSets
"Adds an outer connection to all sets where a corresponding inner definition
is present. For instance, if a connection set contains {world.v, topPin.v} and
we have an outer connection connect(world, a2.aPin), the connection is added
to the sets, resulting in {world.v, topPin.v, a2.aPin.v}. Returns the updated
sets and a boolean that indicates if anything was added or not."
input DAE.ComponentRef inCref1;
input DAE.ComponentRef inCref2;
input Absyn.InnerOuter inIO1;
input Absyn.InnerOuter inIO2;
input Connect.Face inFace1;
input Connect.Face inFace2;
input Connect.Sets inSets;
input SourceInfo inInfo;
output Connect.Sets outSets;
output Boolean outAdded;
protected
Boolean is_outer1, is_outer2;
algorithm
is_outer1 := Absyn.isOuter(inIO1);
is_outer2 := Absyn.isOuter(inIO2);
(outSets, outAdded) := addOuterConnectToSets2(inCref1, inCref2, is_outer1,
is_outer2, inFace1, inFace2, inSets, inInfo);
end addOuterConnectToSets;
protected function addOuterConnectToSets2
"Helper function to addOuterConnectToSets. Dispatches based on the inner/outer
prefix of both connector elements."
input DAE.ComponentRef inCref1;
input DAE.ComponentRef inCref2;
input Boolean inIsOuter1;
input Boolean inIsOuter2;
input Face inFace1;
input Face inFace2;
input Sets inSets;
input SourceInfo inInfo;
output Sets outSets;
output Boolean outAdded;
algorithm
(outSets, outAdded) := match(inCref1, inCref2, inIsOuter1, inIsOuter2,
inFace1, inFace2, inSets, inInfo)
local
Sets sets;
Boolean added;
// Both are outer => error.
case (_, _, true, true, _, _, _, _)
equation
Error.addSourceMessage(Error.UNSUPPORTED_LANGUAGE_FEATURE,
{"Connections where both connectors are outer references", "No suggestion"}, inInfo);
then
(inSets, false);
// Both are inner => do nothing.
case (_, _, false, false, _, _, _, _) then (inSets, false);
// The first is outer and the second inner, call addOuterConnectToSets3.