This repository was archived by the owner on May 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathBackendVarTransform.mo
More file actions
2948 lines (2786 loc) · 112 KB
/
BackendVarTransform.mo
File metadata and controls
2948 lines (2786 loc) · 112 KB
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 BackendVarTransform
" file: BackendVarTransform.mo
package: BackendVarTransform
description: BackendVarTransform contains a Binary Tree representation of variable replacements.
This module contain a Binary tree representation of variable replacements
along with some functions for performing replacements of variables in equations"
public import BackendDAE;
public import DAE;
public import HashTable2;
public import HashTable3;
protected import Absyn;
protected import BackendDAETransform;
protected import BaseHashTable;
protected import BaseHashSet;
protected import BackendEquation;
protected import BackendVariable;
protected import ClassInf;
protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
protected import ElementSource;
protected import EvaluateFunctions;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import Flags;
protected import HashSet;
protected import List;
protected import Util;
protected import MetaModelica.Dangerous.listReverseInPlace;
public
uniontype VariableReplacements
"VariableReplacements consists of a mapping between variables and expressions, the first binary tree of this type.
To eliminate a variable from an equation system a replacement rule varname->expression is added to this
datatype.
To be able to update these replacement rules incrementally a backward lookup mechanism is also required.
For instance, having a rule a->b and adding a rule b->c requires to find the first rule a->b and update it to
a->c. This is what the second binary tree is used for."
record REPLACEMENTS
HashTable2.HashTable hashTable "src -> dst, used for replacing. src is variable, dst is expression.";
HashTable3.HashTable invHashTable "dst -> list of sources. dst is a variable, sources are variables.";
HashTable2.HashTable extendhashTable "src -> noting, used for extend arrays and records.";
list<DAE.Ident> iterationVars "this are the implicit declerate iteration variables for for and range expressions";
Option<HashTable2.HashTable> derConst "this is used if states are constant to replace der(state) with 0.0";
end REPLACEMENTS;
end VariableReplacements;
public function emptyReplacements "
Returns an empty set of replacement rules
"
output VariableReplacements outVariableReplacements;
algorithm
outVariableReplacements:=
match ()
local HashTable2.HashTable ht,eht;
HashTable3.HashTable invHt;
case ()
equation
ht = HashTable2.emptyHashTable();
eht = HashTable2.emptyHashTable();
invHt = HashTable3.emptyHashTable();
then
REPLACEMENTS(ht,invHt,eht,{},NONE());
end match;
end emptyReplacements;
public function emptyReplacementsSized "Returns an empty set of replacement rules, giving a size of hashtables to allocate"
input Integer size;
output VariableReplacements outVariableReplacements;
algorithm
outVariableReplacements := match (size)
local HashTable2.HashTable ht,eht;
HashTable3.HashTable invHt;
case _
equation
ht = HashTable2.emptyHashTableSized(size);
invHt = HashTable3.emptyHashTableSized(size);
eht = HashTable2.emptyHashTableSized(size);
then
REPLACEMENTS(ht,invHt,eht,{},NONE());
end match;
end emptyReplacementsSized;
public function removeReplacement " removes the replacement for a given key using BaseHashTable.delete
the extendhashtable is not updated"
input VariableReplacements repl;
input DAE.ComponentRef inSrc;
input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
partial function FuncTypeExp_ExpToBoolean
input DAE.Exp inExp;
output Boolean outBoolean;
end FuncTypeExp_ExpToBoolean;
protected
DAE.Exp dst;
HashTable2.HashTable ht,ht_1;
HashTable3.HashTable invHt,invHt_1;
String s;
algorithm
REPLACEMENTS(ht,invHt,_,_,_) := repl;
if not BaseHashTable.hasKey(inSrc,ht) then
return;
end if;
try
dst := BaseHashTable.get(inSrc,ht);
BaseHashTable.delete(inSrc,ht);
removeReplacementInv(invHt, dst);
else
Error.addInternalError("-BackendVarTransform.removeReplacement failed for " + ComponentReference.printComponentRefStr(inSrc) +"\n", sourceInfo());
end try;
end removeReplacement;
public function removeReplacements
input VariableReplacements iRepl;
input list<DAE.ComponentRef> inSrcs;
input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
partial function FuncTypeExp_ExpToBoolean
input DAE.Exp inExp;
output Boolean outBoolean;
end FuncTypeExp_ExpToBoolean;
algorithm
for cr in inSrcs loop
removeReplacement(iRepl,cr,inFuncTypeExpExpToBooleanOption);
end for;
end removeReplacements;
public function addReplacements
input VariableReplacements iRepl;
input list<DAE.ComponentRef> inSrcs;
input list<DAE.Exp> inDsts;
input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
output VariableReplacements outRepl;
partial function FuncTypeExp_ExpToBoolean
input DAE.Exp inExp;
output Boolean outBoolean;
end FuncTypeExp_ExpToBoolean;
algorithm
outRepl := match(iRepl,inSrcs,inDsts,inFuncTypeExpExpToBooleanOption)
local
DAE.ComponentRef cr;
list<DAE.ComponentRef> crlst;
DAE.Exp exp;
VariableReplacements repl;
list<DAE.Exp> explst;
case (_,{},{},_) then iRepl;
case (_,cr::crlst,exp::explst,_)
equation
repl = addReplacement(iRepl,cr,exp,inFuncTypeExpExpToBooleanOption);
then
addReplacements(repl,crlst,explst,inFuncTypeExpExpToBooleanOption);
end match;
end addReplacements;
public function addReplacement "
Adds a replacement rule to the set of replacement rules given as argument.
If a replacement rule a->b already exists and we add a new rule b->c then
the rule a->b is updated to a->c. This is done using the make_transitive
function.
"
input VariableReplacements repl;
input DAE.ComponentRef inSrc;
input DAE.Exp inDst;
input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
output VariableReplacements outRepl;
partial function FuncTypeExp_ExpToBoolean
input DAE.Exp inExp;
output Boolean outBoolean;
end FuncTypeExp_ExpToBoolean;
algorithm
outRepl:=
matchcontinue (repl,inSrc,inDst,inFuncTypeExpExpToBooleanOption)
local
DAE.ComponentRef src,src_1;
DAE.Exp dst,dst_1;
HashTable2.HashTable ht,ht_1,eht,eht_1;
HashTable3.HashTable invHt,invHt_1;
list<DAE.Ident> iv;
String s;
Option<HashTable2.HashTable> derConst;
// PA: Commented out this, since it will only slow things down without adding any functionality.
// Once match is available as a complement to matchcontinue, this case could be useful again.
//case ((repl as REPLACEMENTS(ht,invHt)),src,dst) /* source dest */
// equation
// olddst = BaseHashTable.get(src, ht) "if rule a->b exists, fail";
// then
// fail();
case (_,src,dst,_)
equation
(REPLACEMENTS(ht,invHt,eht,iv,derConst),src_1,dst_1) = makeTransitive(repl, src, dst, inFuncTypeExpExpToBooleanOption);
/*s1 = ComponentReference.printComponentRefStr(src);
s2 = ExpressionDump.printExpStr(dst);
s3 = ComponentReference.printComponentRefStr(src_1);
s4 = ExpressionDump.printExpStr(dst_1);
s = stringAppendList(
{"add_replacement(",s1,", ",s2,") -> add_replacement(",s3,
", ",s4,")\n"});
print(s);
fprint(Flags.ADD_REPL, s);*/
ht_1 = BaseHashTable.add((src_1, dst_1),ht);
invHt_1 = addReplacementInv(invHt, src_1, dst_1);
eht_1 = addExtendReplacement(eht,src_1,NONE());
then
REPLACEMENTS(ht_1,invHt_1,eht_1,iv,derConst);
case (_,_,_,_)
equation
s = ComponentReference.printComponentRefStr(inSrc);
print("-BackendVarTransform.addReplacement failed for " + s);
then
fail();
end matchcontinue;
end addReplacement;
public function performReplacementsEqSystem
input BackendDAE.EqSystem inEqs;
input VariableReplacements inRepl;
output BackendDAE.EqSystem outEqs = inEqs;
protected
BackendDAE.EquationArray eqArr;
algorithm
eqArr := inEqs.orderedEqs;
(_, _) := BackendVariable.traverseBackendDAEVarsWithUpdate(inEqs.orderedVars, replaceVarTraverser, inRepl);
((eqArr, _)) := replaceEquationsArr(eqArr, inRepl, NONE());
outEqs.orderedEqs := eqArr;
end performReplacementsEqSystem;
protected function addReplacementNoTransitive "Similar to addReplacement but
does not make transitive replacement rules.
"
input VariableReplacements repl;
input DAE.ComponentRef inSrc;
input DAE.Exp inDst;
output VariableReplacements outRepl;
algorithm
outRepl:=
matchcontinue (repl,inSrc,inDst)
local
DAE.ComponentRef src;
DAE.Exp dst,olddst;
HashTable2.HashTable ht,ht_1,eht,eht_1;
HashTable3.HashTable invHt,invHt_1;
list<DAE.Ident> iv;
Option<HashTable2.HashTable> derConst;
case ((REPLACEMENTS(hashTable=ht)),src,_) /* source dest */
guard
BaseHashTable.hasKey(src,ht) "if rule a->b exists, fail"
then
fail();
case ((REPLACEMENTS(ht,invHt,eht,iv,derConst)),src,dst)
equation
ht_1 = BaseHashTable.add((src, dst),ht);
invHt_1 = addReplacementInv(invHt, src, dst);
eht_1 = addExtendReplacement(eht,src,NONE());
then
REPLACEMENTS(ht_1,invHt_1,eht_1,iv,derConst);
case (_,_,_)
equation
print("-add_replacement failed for " + ComponentReference.printComponentRefStr(inSrc) + " = " + ExpressionDump.printExpStr(inDst) + "\n");
then
fail();
end matchcontinue;
end addReplacementNoTransitive;
protected function removeReplacementInv "
Helper function to removeReplacement
removes the inverse rule of a replacement in the second binary tree
of VariableReplacements.
"
input HashTable3.HashTable invHt;
input DAE.Exp dst;
algorithm
for d in Expression.extractCrefsFromExp(dst) loop
BaseHashTable.delete(d, invHt);
end for;
end removeReplacementInv;
protected function addReplacementInv "
Helper function to addReplacement
Adds the inverse rule of a replacement to the second binary tree
of VariableReplacements.
"
input HashTable3.HashTable invHt;
input DAE.ComponentRef src;
input DAE.Exp dst;
output HashTable3.HashTable outInvHt;
algorithm
outInvHt:=
match (invHt,src,dst)
local
HashTable3.HashTable invHt_1;
HashSet.HashSet set;
list<DAE.ComponentRef> dests;
case (_,_,_) equation
// (_,set) = Expression.traverseExpTopDown(dst, traversingCrefFinder, HashSet.emptyHashSet() /* Very expensive operation */);
// dests = BaseHashSet.hashSetList(set);
dests = Expression.extractCrefsFromExp(dst);
invHt_1 = List.fold1r(dests,addReplacementInv2,src,invHt);
then
invHt_1;
end match;
end addReplacementInv;
protected function traversingCrefFinder "
Author: Frenkel 2012-12"
input DAE.Exp e;
input HashSet.HashSet ihs;
output DAE.Exp outExp;
output Boolean cont;
output HashSet.HashSet set;
algorithm
(outExp,cont,set) := matchcontinue (e,ihs)
local
DAE.ComponentRef cr;
case (DAE.CREF(DAE.CREF_IDENT(ident = "time",subscriptLst = {}),_), set)
then (e,false,set);
case (DAE.CREF(componentRef = cr), set)
equation
set = BaseHashSet.add(cr,set);
then (e,false,set);
else (e,true,ihs);
end matchcontinue;
end traversingCrefFinder;
protected function addReplacementInv2 "
Helper function to addReplacementInv
Adds the inverse rule for one of the variables of a replacement to the second binary tree
of VariableReplacements.
Since a replacement is on the form var -> expression of vars(v1,v2,...,vn) the inverse binary tree
contains rules for v1 -> var, v2 -> var, ...., vn -> var so that any of the variables of the expression
will update the rule.
"
input HashTable3.HashTable invHt;
input DAE.ComponentRef dst;
input DAE.ComponentRef src;
output HashTable3.HashTable outInvHt;
protected
list<DAE.ComponentRef> srcs;
algorithm
if BaseHashTable.hasKey(dst,invHt) then
// previous elt for dst -> src, append..
srcs := BaseHashTable.get(dst,invHt);
srcs := src::srcs;
outInvHt := BaseHashTable.add((dst, srcs),invHt);
else
// No previous elt for dst -> src
outInvHt:= BaseHashTable.add((dst, {src}),invHt);
end if;
end addReplacementInv2;
protected function makeTransitive "
This function takes a set of replacement rules and a new replacement rule
in the form of two ComponentRef:s and makes sure the new replacement rule
is replaced with the transitive value.
For example, if we have the rule a->b and a new rule c->a it is changed to c->b.
Also, if we have a rule a->b and a new rule b->c then the -old- rule a->b is changed
to a->c.
For arbitrary expressions: if we have a rule ax-> expr(b1,..,bn) and a new rule c->expr(a1,ax,..,an)
it is changed to c-> expr(a1,expr(b1,...,bn),..,an).
And similary for a rule ax -> expr(b1,bx,..,bn) and a new rule bx->expr(c1,..,cn) then old rule is changed to
ax -> expr(b1,expr(c1,..,cn),..,bn).
"
input VariableReplacements repl;
input DAE.ComponentRef src;
input DAE.Exp dst;
input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
output VariableReplacements outRepl;
output DAE.ComponentRef outSrc;
output DAE.Exp outDst;
partial function FuncTypeExp_ExpToBoolean
input DAE.Exp inExp;
output Boolean outBoolean;
end FuncTypeExp_ExpToBoolean;
algorithm
(outRepl,outSrc,outDst):=
match (repl,src,dst,inFuncTypeExpExpToBooleanOption)
local
VariableReplacements repl_1,repl_2;
DAE.ComponentRef src_1,src_2;
DAE.Exp dst_1,dst_2,dst_3;
case (_,_,_,_)
equation
(repl_1,src_1,dst_1) = makeTransitive1(repl, src, dst,inFuncTypeExpExpToBooleanOption);
(repl_2,src_2,dst_2) = makeTransitive2(repl_1, src_1, dst_1,inFuncTypeExpExpToBooleanOption);
(dst_3,_) = ExpressionSimplify.simplify1(dst_2) "to remove e.g. --a";
then
(repl_2,src_2,dst_3);
end match;
end makeTransitive;
protected function makeTransitive1 "
helper function to makeTransitive
"
input VariableReplacements repl;
input DAE.ComponentRef src;
input DAE.Exp dst;
input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
output VariableReplacements outRepl;
output DAE.ComponentRef outSrc;
output DAE.Exp outDst;
partial function FuncTypeExp_ExpToBoolean
input DAE.Exp inExp;
output Boolean outBoolean;
end FuncTypeExp_ExpToBoolean;
algorithm
(outRepl,outSrc,outDst):=
matchcontinue (repl,src,dst,inFuncTypeExpExpToBooleanOption)
local
list<DAE.ComponentRef> lst;
VariableReplacements repl_1,singleRepl;
HashTable3.HashTable invHt;
// old rule a->expr(b1,..,bn) must be updated to a->expr(c_exp,...,bn) when new rule b1->c_exp
// is introduced
case ((REPLACEMENTS(invHashTable=invHt)),_,_,_)
equation
lst = BaseHashTable.get(src, invHt);
singleRepl = addReplacementNoTransitive(emptyReplacementsSized(53),src,dst);
repl_1 = makeTransitive12(lst,repl,singleRepl,inFuncTypeExpExpToBooleanOption,HashSet.emptyHashSet());
then
(repl_1,src,dst);
else (repl,src,dst);
end matchcontinue;
end makeTransitive1;
protected function makeTransitive12 "Helper function to makeTransitive1
For each old rule a->expr(b1,..,bn) update dest by applying the new rule passed as argument
in singleRepl."
input list<DAE.ComponentRef> lst;
input VariableReplacements repl;
input VariableReplacements singleRepl "contain one replacement rule: the rule to be added";
input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
input HashSet.HashSet inSet "to avoid touble work";
output VariableReplacements outRepl;
partial function FuncTypeExp_ExpToBoolean
input DAE.Exp inExp;
output Boolean outBoolean;
end FuncTypeExp_ExpToBoolean;
algorithm
outRepl := matchcontinue(lst,repl,singleRepl,inFuncTypeExpExpToBooleanOption,inSet)
local
DAE.Exp crDst;
DAE.ComponentRef cr;
list<DAE.ComponentRef> crs;
VariableReplacements repl1;
HashTable2.HashTable ht;
HashSet.HashSet set;
case({},_,_,_,_) then repl;
case(cr::crs,REPLACEMENTS(hashTable=ht),_,_,_)
equation
false = BaseHashSet.has(cr,inSet);
set = BaseHashSet.add(cr,inSet);
crDst = BaseHashTable.get(cr,ht);
(crDst,_) = replaceExp(crDst,singleRepl,inFuncTypeExpExpToBooleanOption);
repl1 = addReplacementNoTransitive(repl,cr,crDst) "add updated old rule";
then
makeTransitive12(crs,repl1,singleRepl,inFuncTypeExpExpToBooleanOption,set);
case(_::crs,_,_,_,_)
then
makeTransitive12(crs,repl,singleRepl,inFuncTypeExpExpToBooleanOption,inSet);
end matchcontinue;
end makeTransitive12;
protected function makeTransitive2 "
Helper function to makeTransitive
"
input VariableReplacements repl;
input DAE.ComponentRef src;
input DAE.Exp dst;
input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
output VariableReplacements outRepl;
output DAE.ComponentRef outSrc;
output DAE.Exp outDst;
partial function FuncTypeExp_ExpToBoolean
input DAE.Exp inExp;
output Boolean outBoolean;
end FuncTypeExp_ExpToBoolean;
algorithm
(outRepl,outSrc,outDst):=
matchcontinue (repl,src,dst,inFuncTypeExpExpToBooleanOption)
local
DAE.Exp dst_1;
// for rule a->b1+..+bn, replace all b1 to bn's in the expression;
case (_,_,_,_)
equation
(dst_1,_) = replaceExp(dst,repl,inFuncTypeExpExpToBooleanOption);
then
(repl,src,dst_1);
// replace Exp failed, keep old rule.
case (_,_,_,_) then (repl,src,dst); /* dst has no own replacement, return */
end matchcontinue;
end makeTransitive2;
protected function addExtendReplacement
"author: Frenkel TUD 2011-04
checks if the parents of cref from type array or record
and add a rule to extend them."
input HashTable2.HashTable extendrepl;
input DAE.ComponentRef cr;
input Option<DAE.ComponentRef> preCr;
output HashTable2.HashTable outExtendrepl;
algorithm
outExtendrepl:=
matchcontinue (extendrepl,cr,preCr)
local
HashTable2.HashTable erepl,erepl1;
DAE.ComponentRef subcr,precr,precr1,pcr,precrn,precrn1;
DAE.Ident ident;
DAE.Type ty;
list<DAE.Subscript> subscriptLst;
list<DAE.Var> varLst;
list<DAE.ComponentRef> crefs;
String s;
case (_,DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_ARRAY()),NONE())
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
false = BaseHashTable.hasKey(precr,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr, DAE.RCONST(0.0)),extendrepl);
then erepl;
case (_,DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_ARRAY()),SOME(pcr))
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
precr1 = ComponentReference.joinCrefs(pcr,precr);
false = BaseHashTable.hasKey(precr1,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr1, DAE.RCONST(0.0)),extendrepl);
then erepl;
case (_,DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(_),varLst=varLst)),NONE())
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
false = BaseHashTable.hasKey(precr,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr, DAE.RCONST(0.0)),extendrepl);
// Create a list of crefs from names
crefs = List.map(varLst,ComponentReference.creffromVar);
erepl = List.fold1r(crefs,addExtendReplacement,SOME(precr),erepl);
then erepl;
case (_,DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(_),varLst=varLst)),SOME(pcr))
equation
_ = ComponentReference.makeCrefIdent(ident,ty,{});
precr1 = ComponentReference.joinCrefs(pcr,cr);
false = BaseHashTable.hasKey(precr1,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr1, DAE.RCONST(0.0)),extendrepl);
// Create a list of crefs from names
crefs = List.map(varLst,ComponentReference.creffromVar);
erepl = List.fold1r(crefs,addExtendReplacement,SOME(precr1),erepl);
then erepl;
case (_,DAE.CREF_IDENT(ident=ident,identType=ty,subscriptLst=_::_),NONE())
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
false = BaseHashTable.hasKey(precr,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr, DAE.RCONST(0.0)),extendrepl);
then erepl;
case (_,DAE.CREF_IDENT(ident=ident,identType=ty,subscriptLst=_::_),SOME(pcr))
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
precr1 = ComponentReference.joinCrefs(pcr,precr);
false = BaseHashTable.hasKey(precr1,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr1, DAE.RCONST(0.0)),extendrepl);
then erepl;
case (_,DAE.CREF_IDENT(),_)
then
extendrepl;
case (_,DAE.CREF_QUAL(ident=ident,identType=ty,subscriptLst=subscriptLst,componentRef=subcr),NONE())
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
false = BaseHashTable.hasKey(precr,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr, DAE.RCONST(0.0)),extendrepl);
precrn = ComponentReference.makeCrefIdent(ident,ty,subscriptLst);
erepl1 = addExtendReplacement(erepl,subcr,SOME(precrn));
then erepl1;
case (_,DAE.CREF_QUAL(ident=ident,identType=ty,subscriptLst=subscriptLst,componentRef=subcr),SOME(pcr))
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
precr1 = ComponentReference.joinCrefs(pcr,precr);
false = BaseHashTable.hasKey(precr1,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr1, DAE.RCONST(0.0)),extendrepl);
precrn = ComponentReference.makeCrefIdent(ident,ty,subscriptLst);
precrn1 = ComponentReference.joinCrefs(pcr,precrn);
erepl1 = addExtendReplacement(erepl,subcr,SOME(precrn1));
then erepl1;
// all other
case (_,DAE.CREF_QUAL(ident=ident,identType=ty,subscriptLst=subscriptLst,componentRef=subcr),NONE())
equation
precrn = ComponentReference.makeCrefIdent(ident,ty,subscriptLst);
erepl = addExtendReplacement(extendrepl,subcr,SOME(precrn));
then erepl;
case (_,DAE.CREF_QUAL(ident=ident,identType=ty,subscriptLst=subscriptLst,componentRef=subcr),SOME(pcr))
equation
precrn = ComponentReference.makeCrefIdent(ident,ty,subscriptLst);
precrn1 = ComponentReference.joinCrefs(pcr,precrn);
erepl = addExtendReplacement(extendrepl,subcr,SOME(precrn1));
then erepl;
case (_,_,_)
equation
true = Flags.isSet(Flags.FAILTRACE);
s = ComponentReference.printComponentRefStr(cr);
Debug.trace("- BackendVarTransform.addExtendReplacement failed for " + s);
then extendrepl;
end matchcontinue;
end addExtendReplacement;
protected function addIterationVar
"add a var to the iterationVars"
input VariableReplacements repl;
input DAE.Ident inVar;
output VariableReplacements outRepl;
algorithm
outRepl:=
match (repl,inVar)
local
HashTable2.HashTable ht,eht;
HashTable3.HashTable invHt;
list<DAE.Ident> iv;
Option<HashTable2.HashTable> derConst;
case (REPLACEMENTS(ht,invHt,eht,iv,derConst),_)
then
REPLACEMENTS(ht,invHt,eht,inVar::iv,derConst);
end match;
end addIterationVar;
protected function removeIterationVar
"remove the first equal var from the iterationVars"
input VariableReplacements repl;
input DAE.Ident inVar;
output VariableReplacements outRepl;
algorithm
outRepl:=
match (repl,inVar)
local
HashTable2.HashTable ht,eht;
HashTable3.HashTable invHt;
list<DAE.Ident> iv;
Option<HashTable2.HashTable> derConst;
case (REPLACEMENTS(ht,invHt,eht,iv,derConst),_)
equation
iv = removeFirstOnTrue(iv,stringEq,inVar,{});
then
REPLACEMENTS(ht,invHt,eht,iv,derConst);
end match;
end removeIterationVar;
protected function isIterationVar
"remove true if it is an iteration var"
input VariableReplacements repl;
input DAE.Ident inVar;
output Boolean is;
algorithm
is:=
match (repl,inVar)
local
list<DAE.Ident> iv;
case (REPLACEMENTS(iterationVars=iv),_)
then
listMember(inVar, iv);
end match;
end isIterationVar;
protected function removeFirstOnTrue
input list<ArgType1> iLst;
input CompFunc func;
input ArgType2 value;
input list<ArgType1> iAcc;
output list<ArgType1> oAcc;
partial function CompFunc
input ArgType1 inElement;
input ArgType2 value;
output Boolean outIsEqual;
end CompFunc;
replaceable type ArgType1 subtypeof Any;
replaceable type ArgType2 subtypeof Any;
algorithm
oAcc := match(iLst,func,value,iAcc)
local
ArgType1 arg;
list<ArgType1> arglst;
case ({},_,_,_) then listReverse(iAcc);
case (arg::arglst,_,_,_) guard func(arg,value)
then
List.append_reverse(iAcc,arglst);
case (arg::arglst,_,_,_)
then
removeFirstOnTrue(arglst,func,value,arg::iAcc);
end match;
end removeFirstOnTrue;
public function addDerConstRepl
"add a var to the derConst replacements, replace der(const) with 0.0"
input DAE.ComponentRef inComponentRef;
input DAE.Exp inExp;
input VariableReplacements repl;
output VariableReplacements outRepl;
algorithm
outRepl:= match (inComponentRef,inExp,repl)
local
HashTable2.HashTable ht,eht;
HashTable3.HashTable invHt;
list<DAE.Ident> iv;
HashTable2.HashTable derConst;
case (_,_,REPLACEMENTS(ht,invHt,eht,iv,NONE()))
equation
derConst = HashTable2.emptyHashTable();
derConst = BaseHashTable.add((inComponentRef,inExp),derConst);
then
REPLACEMENTS(ht,invHt,eht,iv,SOME(derConst));
case (_,_,REPLACEMENTS(ht,invHt,eht,iv,SOME(derConst)))
equation
derConst = BaseHashTable.add((inComponentRef,inExp),derConst);
then
REPLACEMENTS(ht,invHt,eht,iv,SOME(derConst));
end match;
end addDerConstRepl;
public function getReplacement "
Retrives a replacement variable given a set of replacement rules and a
source variable.
"
input VariableReplacements inVariableReplacements;
input DAE.ComponentRef inComponentRef;
output DAE.Exp outComponentRef;
algorithm
outComponentRef:=
match (inVariableReplacements,inComponentRef)
local
DAE.ComponentRef src;
DAE.Exp dst;
HashTable2.HashTable ht;
case (REPLACEMENTS(hashTable=ht),src)
equation
dst = BaseHashTable.get(src,ht);
then
dst;
end match;
end getReplacement;
public function hasReplacement "
Outputs true if the replacements contain a rule for the cref
"
input VariableReplacements inVariableReplacements;
input DAE.ComponentRef inComponentRef;
output Boolean bOut;
algorithm
bOut:=
match (inVariableReplacements,inComponentRef)
local
DAE.ComponentRef src;
DAE.Exp dst;
HashTable2.HashTable ht;
case (REPLACEMENTS(hashTable=ht),src)
guard BaseHashTable.hasKey(src,ht)
then
true;
else false;
end match;
end hasReplacement;
public function hasReplacementCrefFirst "
Outputs true if the replacements contain a rule for the cref
"
input DAE.ComponentRef inComponentRef;
input VariableReplacements inVariableReplacements;
output Boolean bOut;
algorithm
bOut:=
match (inComponentRef,inVariableReplacements)
local
DAE.ComponentRef src;
DAE.Exp dst;
HashTable2.HashTable ht;
case (src,REPLACEMENTS(hashTable=ht))
guard BaseHashTable.hasKey(src,ht)
then
true;
else false;
end match;
end hasReplacementCrefFirst;
public function hasNoReplacementCrefFirst "
Outputs true if the replacements contain a rule for the cref
"
input DAE.ComponentRef inComponentRef;
input VariableReplacements inVariableReplacements;
output Boolean bOut;
algorithm
bOut:=
match (inComponentRef,inVariableReplacements)
local
DAE.ComponentRef src;
DAE.Exp dst;
HashTable2.HashTable ht;
case (src,REPLACEMENTS(hashTable=ht))
guard BaseHashTable.hasKey(src,ht)
then
false;
else true;
end match;
end hasNoReplacementCrefFirst;
public function varHasNoReplacement "
Outputs true if the replacements contains no rule for the var
"
input BackendDAE.Var var;
input VariableReplacements inVariableReplacements;
output Boolean bOut;
algorithm
bOut:=
match (var,inVariableReplacements)
local
DAE.ComponentRef src;
DAE.Exp dst;
HashTable2.HashTable ht;
case (BackendDAE.VAR(varName=src),REPLACEMENTS(hashTable=ht))
guard BaseHashTable.hasKey(src,ht)
then
false;
else true;
end match;
end varHasNoReplacement;
public function getReplacementVarArraySize
input VariableReplacements inVariableReplacements;
output Integer size;
protected
HashTable2.HashTable hashTable;
algorithm
REPLACEMENTS(hashTable=hashTable) := inVariableReplacements;
size := BaseHashTable.hashTableCurrentSize(hashTable);
end getReplacementVarArraySize;
public function getReplacementCRefFirst "
Retrives a replacement variable given a set of replacement rules and a
source variable.
"
input DAE.ComponentRef inComponentRef;
input VariableReplacements inVariableReplacements;
output DAE.Exp outComponentRef;
algorithm
outComponentRef:=
match (inComponentRef,inVariableReplacements)
local
DAE.ComponentRef src;
DAE.Exp dst;
HashTable2.HashTable ht;
case (src,REPLACEMENTS(hashTable=ht))
equation
dst = BaseHashTable.get(src,ht);
then
dst;
end match;
end getReplacementCRefFirst;
public function getAllReplacements "
Author BZ 2009-04
Extract all crefs -> exp to two separate lists.
"
input VariableReplacements inVariableReplacements;
output list<DAE.ComponentRef> crefs;
output list<DAE.Exp> dsts;
algorithm (crefs,dsts) := match (inVariableReplacements)
local
HashTable2.HashTable ht;
list<tuple<DAE.ComponentRef,DAE.Exp>> tplLst;
case (REPLACEMENTS(hashTable = ht))
equation
tplLst = BaseHashTable.hashTableList(ht);
crefs = List.map(tplLst,Util.tuple21);
dsts = List.map(tplLst,Util.tuple22);
then
(crefs,dsts);
end match;
end getAllReplacements;
public function getExtendReplacement "
Retrives a replacement variable given a set of replacement rules and a
source variable.
"
input VariableReplacements inVariableReplacements;
input DAE.ComponentRef inComponentRef;
output DAE.Exp outComponentRef;
algorithm
outComponentRef:=
match (inVariableReplacements,inComponentRef)
local
DAE.ComponentRef src, src_1;
DAE.Exp dst;
HashTable2.HashTable ht;
case (REPLACEMENTS(extendhashTable=ht),src)
equation
src_1 = ComponentReference.crefStripLastSubs(src);
dst = BaseHashTable.get(src_1,ht);
then
dst;
end match;
end getExtendReplacement;
protected function avoidDoubleHashLookup "
Author BZ 200X-XX modified 2008-06
When adding replacement rules, we might not have the correct type availible at the moment.
Then DAE.T_UNKNOWN_DEFAULT is used, so when replacing exp and finding DAE.T_UNKNOWN_DEFAULT, we use the
type of the expression to be replaced instead.
TODO: find out why array residual functions containing arrays as xloc[] does not work,
doing that will allow us to use this function for all crefs."
input DAE.Exp inExp;
input DAE.Type inType;
output DAE.Exp outExp;
algorithm outExp := matchcontinue(inExp,inType)
local DAE.ComponentRef cr;
case(DAE.CREF(cr,DAE.T_UNKNOWN()),_) then Expression.makeCrefExp(cr,inType);
else inExp;
end matchcontinue;
end avoidDoubleHashLookup;
public function isReplacementEmpty
input VariableReplacements repl;
output Boolean empty;
algorithm
empty := match(repl)
local
HashTable2.HashTable ht;
case REPLACEMENTS(hashTable=ht, derConst=NONE())
then intLt(BaseHashTable.hashTableCurrentSize(ht), 1);
case REPLACEMENTS(derConst=SOME(_)) then false;
end match;
end isReplacementEmpty;
public function replacementCurrentSize
input VariableReplacements repl;
output Integer size;
protected
HashTable2.HashTable ht;
algorithm
REPLACEMENTS(hashTable = ht) := repl;
size := BaseHashTable.hashTableCurrentSize(ht);
end replacementCurrentSize;
/*********************************************************/
/* replace Expression with condition function */
/*********************************************************/
public function replaceExp "Takes a set of replacement rules and an expression and a function
giving a boolean value for an expression.
The function replaces all variables in the expression using
the replacement rules, if the boolean value is true children of the
expression is visited (including the expression itself). If it is false,
no replacemet is performed."
input DAE.Exp inExp;
input VariableReplacements inVariableReplacements;
input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
output DAE.Exp outExp;
output Boolean replacementPerformed;
partial function FuncTypeExp_ExpToBoolean
input DAE.Exp inExp;
output Boolean outBoolean;