@@ -75,6 +75,7 @@ protected
7575 import ComponentRef = NFComponentRef ;
7676 import Expression = NFExpression ;
7777 import ExpressionIterator = NFExpressionIterator ;
78+ import NFFunction.Function ;
7879 import Type = NFType ;
7980 import Operator = NFOperator ;
8081 import Variable = NFVariable ;
@@ -220,14 +221,15 @@ protected
220221 UnorderedMap < ComponentRef , Expression > replacements;
221222 EquationPointers newEquations;
222223 list< Pointer < Variable >> alias_vars, const_vars, non_trivial_alias;
223- list< Pointer < Equation >> non_trivial_eqs;
224+ list< Pointer < Equation >> non_trivial_eqs, auxEquations ;
224225
225226 case (BVariable . VAR_DATA_SIM (), BEquation . EQ_DATA_SIM ())
226227 algorithm
227228 // -----------------------------------
228229 // 1. 2. 3.
229230 // -----------------------------------
230231 (replacements, newEquations) := aliasCausalize(varData. unknowns, eqData. simulation, "Simulation" );
232+ (replacements, auxEquations) := checkReplacements(replacements, eqData);
231233
232234 // -----------------------------------
233235 // 4. apply replacements
@@ -273,14 +275,92 @@ protected
273275 non_trivial_eqs := list(Equation . generateBindingEquation(var , eqData. uniqueIndex, false ) for var in non_trivial_alias);
274276 eqData. removed := EquationPointers . addList(non_trivial_eqs, eqData. removed);
275277 // eqData.equations := EquationPointers.addList(non_trivial_eqs, eqData.equations);
276- then (varData, eqData);
278+ then (varData, EqData . addUntypedList( eqData, auxEquations, false ) );
277279
278280 else algorithm
279281 Error . addMessage(Error . INTERNAL_ERROR , {getInstanceName() + " failed." });
280282 then fail();
281283 end match;
282284 end aliasDefault;
283285
286+ function checkReplacements
287+ "Checks validity of all replacements, returns all valid replacements and auxiliary equations"
288+ input UnorderedMap < ComponentRef , Expression > replacements;
289+ input EqData eqData;
290+ output UnorderedMap < ComponentRef , Expression > newReplacements = UnorderedMap . new< Expression > (ComponentRef . hash, ComponentRef . isEqual);
291+ output list< Pointer < Equation >> auxEquations = {};
292+ protected
293+ UnorderedSet < ComponentRef > exceptionSet = UnorderedSet . new(ComponentRef . hash, ComponentRef . isEqual);
294+ ComponentRef cref;
295+ Expression exp;
296+ Pointer < Equation > eqPtr;
297+ EquationAttributes attr;
298+ algorithm
299+ EqData . mapExp(eqData, function filterPre(acc = exceptionSet));
300+ for keyValueTpl in UnorderedMap . toList(replacements) loop
301+ (cref, exp) := keyValueTpl;
302+ if isValidReplacement(cref, exp, exceptionSet) then
303+ // replacement is valid - add to newReplacements
304+ UnorderedMap . add(cref, exp, newReplacements);
305+ else
306+ // add auxiliary equation
307+ attr := BackendDAE . lowerEquationAttributes(ComponentRef . getSubscriptedType(cref), false );
308+ eqPtr := Equation . makeAssignment(Expression . fromCref(cref), exp, EqData . getUniqueIndex(eqData), "SIM" , Iterator . EMPTY (), attr);
309+ auxEquations := eqPtr :: auxEquations;
310+ end if ;
311+ end for ;
312+
313+ if Flags . isSet(Flags . DUMP_REPL ) then
314+ dumpReplacements(newReplacements, auxEquations);
315+ end if ;
316+ end checkReplacements;
317+
318+ function isValidReplacement
319+ "Checks if a replacement (cref, exp) is valid"
320+ input ComponentRef cref;
321+ input Expression exp;
322+ input UnorderedSet < ComponentRef > exceptionSet;
323+ output Boolean b = true ;
324+ algorithm
325+ // TODO: possibly match cref, exp here: add if needed
326+ if UnorderedSet . contains(cref, exceptionSet) then
327+ b := false ;
328+ end if ;
329+ end isValidReplacement;
330+
331+ function filterPre
332+ "Filter expression for pre call"
333+ input output Expression exp;
334+ input UnorderedSet < ComponentRef > acc;
335+ algorithm
336+ () := match exp
337+ local
338+ Call call;
339+ ComponentRef cref;
340+
341+ case Expression . CALL (call = call as Call . TYPED_CALL (arguments = {Expression . CREF (cref = cref)}))
342+ guard(AbsynUtil . pathString(Function . nameConsiderBuiltin(call. fn)) == "pre" ) algorithm
343+ UnorderedSet . add(cref, acc);
344+ then ();
345+
346+ else ();
347+ end match;
348+ end filterPre;
349+
350+ function dumpReplacements
351+ input UnorderedMap < ComponentRef , Expression > replacements;
352+ input list< Pointer < Equation >> auxEquations = {};
353+ algorithm
354+ print(Replacements . simpleToString(replacements) + " \n " );
355+ if not listEmpty(auxEquations) then
356+ print(StringUtil . headline_4("[dumprepl] Found But Illegal Alias Replacements (added as equations):" ));
357+ for eqPtr in auxEquations loop
358+ print(" \t " + Equation . toString(Pointer . access(eqPtr)) + " \n " );
359+ end for ;
360+ print(" \n " );
361+ end if ;
362+ end dumpReplacements;
363+
284364 function aliasClocks
285365 "STEPS:
286366 1. collect alias sets (variables, equations, optional constant binding)
@@ -296,13 +376,15 @@ protected
296376 UnorderedMap < ComponentRef , Expression > replacements;
297377 EquationPointers newEquations;
298378 list< Pointer < Variable >> alias_vars;
379+ list< Pointer < Equation >> auxEquations;
299380
300381 case (BVariable . VAR_DATA_SIM (), BEquation . EQ_DATA_SIM ())
301382 algorithm
302383 // -----------------------------------
303384 // 1. 2. 3.
304385 // -----------------------------------
305386 (replacements, newEquations) := aliasCausalize(varData. clocks, eqData. clocked, "Clocked" );
387+ (replacements, auxEquations) := checkReplacements(replacements, eqData);
306388
307389 // -----------------------------------
308390 // 4. apply replacements
@@ -317,7 +399,7 @@ protected
317399 // remove alias variables from clocks and add to alias
318400 varData. clocks := VariablePointers . removeList(alias_vars, varData. clocks);
319401 varData. aliasVars := VariablePointers . addList(alias_vars, varData. aliasVars);
320- then (varData, eqData);
402+ then (varData, EqData . addUntypedList( eqData, auxEquations, false ) );
321403
322404 else algorithm
323405 Error . addMessage(Error . INTERNAL_ERROR , {getInstanceName() + " failed." });
@@ -371,9 +453,6 @@ protected
371453 replacements := createReplacementRules(set, replacements);
372454 end for ;
373455
374- if Flags . isSet(Flags . DUMP_REPL ) then
375- print(Replacements . simpleToString(replacements) + " \n " );
376- end if ;
377456 end aliasCausalize;
378457
379458 function findSimpleEquation
0 commit comments