Skip to content

Commit

Permalink
- support for rewrite rules
Browse files Browse the repository at this point in the history
- the rewrite rules are only used in dumpXMLDAE if a rewrite rule file is given as last parameter
  (if interest exists we can make it part of the usual front-end chain)
- example of a rewriteRules.txt file
  rewrite(abs('$x'), if ('$x' >= 0) then '$x' else -'$x');
  rewrite('$x' ^ 2, '$x' * '$x'); 
  // rewrite(semiLinear(0.0, '$x', '$y'), 0.0);
  // rewrite(semiLinear('$x','$y','$z'), if ('$x' >= 0) then '$x'*'$y' else '$x'*'$z');
  rewrite(noEvent('$x'), '$x');
  rewrite(Modelica.Fluid.Utilities.regRoot('$x','$y'), if ('$x' >= 0) then sqrt('$x') else -(sqrt(-'$x')));
  rewrite(Modelica.Fluid.Utilities.regStep('$x',y1,y2,e), if ('$x' > e) then y1 else y2);
  rewrite(Modelica.Math.Matrices.isEqual('$x','$y','$e'),
         (size('$x',1)==size('$y',1) and size('$x',2) ==  size('$y',2) and sum('$x' .- '$y') == 0));


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18833 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Jan 29, 2014
1 parent 3a155cb commit 66a2bd9
Show file tree
Hide file tree
Showing 6 changed files with 603 additions and 35 deletions.
8 changes: 8 additions & 0 deletions Compiler/FrontEnd/Absyn.mo
Expand Up @@ -6267,4 +6267,12 @@ algorithm
end match;
end mapCrefParts;

public function opEqual
input Operator op1;
input Operator op2;
output Boolean isEqual;
algorithm
isEqual := valueEq(op1, op2);
end opEqual;

end Absyn;
1 change: 1 addition & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -1859,6 +1859,7 @@ function dumpXMLDAE "Outputs the DAE system corresponding to a specific model."
input Boolean addMathMLCode := false;
input Boolean dumpResiduals := false;
input String fileNamePrefix := "<default>" "this is the className in string form by default";
input String rewriteRulesFile := "" "the file from where the rewiteRules are read, default is empty which means no rewrite rules";
output Boolean success "if the function succeeded true/false";
output String xmlfileName "the Xml file";
external "builtin";
Expand Down
75 changes: 59 additions & 16 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -115,6 +115,7 @@ protected import PrefixUtil;
protected import VarTransform;
protected import SCodeDump;
protected import StaticScript;
protected import RewriteRules;

public function elabExpList "Expression elaboration of Absyn.Exp list, i.e. lists of expressions."
input Env.Cache inCache;
Expand Down Expand Up @@ -287,9 +288,51 @@ function: elabExp
output DAE.Properties outProperties;
output Option<GlobalScript.SymbolTable> st;
algorithm
(outCache,outExp,outProperties,st) := elabExp2(inCache,inEnv,inExp,inImplicit,inInteractiveInteractiveSymbolTableOption,performVectorization,inPrefix,info,Error.getNumErrorMessages());
(outCache,outExp,outProperties,st) := matchcontinue(inCache,inEnv,inExp,inImplicit,inInteractiveInteractiveSymbolTableOption,performVectorization,inPrefix,info)
local
Absyn.Exp expRewritten;
// we have some rewrite rules
case (_, _, _, _, _, _, _, _)
equation
false = RewriteRules.noRewriteRules();
expRewritten = RewriteRules.rewrite(inExp);
(outCache,outExp,outProperties,st) = elabExp_dispatch(inCache,inEnv,expRewritten,inImplicit,inInteractiveInteractiveSymbolTableOption,performVectorization,inPrefix,info);
then
(outCache,outExp,outProperties,st);

// we have no rewrite rules
case (_, _, _, _, _, _, _, _)
equation
true = RewriteRules.noRewriteRules();
(outCache,outExp,outProperties,st) = elabExp_dispatch(inCache,inEnv,inExp,inImplicit,inInteractiveInteractiveSymbolTableOption,performVectorization,inPrefix,info);
then
(outCache,outExp,outProperties,st);
end matchcontinue;
end elabExp;

public function elabExp_dispatch "
function: elabExp
Static analysis of expressions means finding out the properties of
the expression. These properties are described by the
DAE.Properties type, and include the type and the variability of the
expression. This function performs analysis, and returns an
DAE.Exp and the properties."
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.Exp inExp;
input Boolean inImplicit;
input Option<GlobalScript.SymbolTable> inInteractiveInteractiveSymbolTableOption;
input Boolean performVectorization;
input Prefix.Prefix inPrefix;
input Absyn.Info info;
output Env.Cache outCache;
output DAE.Exp outExp;
output DAE.Properties outProperties;
output Option<GlobalScript.SymbolTable> st;
algorithm
(outCache,outExp,outProperties,st) := elabExp2(inCache,inEnv,inExp,inImplicit,inInteractiveInteractiveSymbolTableOption,performVectorization,inPrefix,info,Error.getNumErrorMessages());
end elabExp_dispatch;

public function elabExpInExpression "Like elabExp but casts PROP_TUPLE to a PROP"
input Env.Cache inCache;
input Env.Env inEnv;
Expand Down Expand Up @@ -808,22 +851,22 @@ algorithm

// The Absyn.LIST() node is used for list expressions that are
// transformed from Absyn.ARRAY()
case (cache,env,Absyn.LIST({}),impl,st,doVect,_,_,_)
equation
t = DAE.T_METALIST_DEFAULT;
prop = DAE.PROP(t,DAE.C_CONST());
then (cache,DAE.LIST({}),prop,st);
case (cache,env,Absyn.LIST({}),impl,st,doVect,_,_,_)
equation
t = DAE.T_METALIST_DEFAULT;
prop = DAE.PROP(t,DAE.C_CONST());
then (cache,DAE.LIST({}),prop,st);

case (cache,env,Absyn.LIST(es),impl,st,doVect,pre,_,_)
equation
(cache,es_1,propList,st_2) = elabExpList(cache,env, es, impl, st,doVect,pre,info);
typeList = List.map(propList, Types.getPropType);
constList = Types.getConstList(propList);
c = List.fold(constList, Types.constAnd, DAE.C_CONST());
t = Types.boxIfUnboxedType(List.reduce(typeList, Types.superType));
(es_1,_) = Types.matchTypes(es_1, typeList, t, true);
prop = DAE.PROP(DAE.T_METALIST(t,DAE.emptyTypeSource),c);
then (cache,DAE.LIST(es_1),prop,st_2);
case (cache,env,Absyn.LIST(es),impl,st,doVect,pre,_,_)
equation
(cache,es_1,propList,st_2) = elabExpList(cache,env, es, impl, st,doVect,pre,info);
typeList = List.map(propList, Types.getPropType);
constList = Types.getConstList(propList);
c = List.fold(constList, Types.constAnd, DAE.C_CONST());
t = Types.boxIfUnboxedType(List.reduce(typeList, Types.superType));
(es_1,_) = Types.matchTypes(es_1, typeList, t, true);
prop = DAE.PROP(DAE.T_METALIST(t,DAE.emptyTypeSource),c);
then (cache,DAE.LIST(es_1),prop,st_2);
// ----------------------------------

// Pattern matching has its own module that handles match expressions
Expand Down
2 changes: 0 additions & 2 deletions Compiler/Main/Main.mo
Expand Up @@ -67,7 +67,6 @@ protected import Interactive;
protected import List;
protected import Parser;
protected import Print;
protected import RewriteRules;
protected import Settings;
protected import SimCode;
protected import SimCodeMain;
Expand Down Expand Up @@ -1010,7 +1009,6 @@ algorithm
System.realtimeTick(GlobalScript.RT_CLOCK_SIMULATE_TOTAL);
args_1 = Flags.new(args);
System.gettextInit(Util.if_(Config.getRunningTestsuite(),"C",Flags.getConfigString(Flags.LOCALE_FLAG)));
RewriteRules.loadRules();
main2(args_1);
then ();
else
Expand Down
68 changes: 52 additions & 16 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -135,6 +135,7 @@ protected import FGraphEnv;
protected import FGraph;
protected import UnitAbsynBuilder;
protected import UnitParserExt;
protected import RewriteRules;

protected constant DAE.Type simulationResultType_rtest = DAE.T_COMPLEX(ClassInf.RECORD(Absyn.IDENT("SimulationResult")),{
DAE.TYPES_VAR("resultFile",DAE.dummyAttrVar,DAE.T_STRING_DEFAULT,DAE.UNBOUND(),NONE()),
Expand Down Expand Up @@ -4436,18 +4437,41 @@ protected function dumpXMLDAEFrontEnd
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.Path inClassName;
input String rewriteRulesFile;
input GlobalScript.SymbolTable inInteractiveSymbolTable;
output Env.Cache outCache;
output Env.Env outEnv;
output DAE.DAElist outDae;
protected
Absyn.Program p;
SCode.Program scode;
algorithm
GlobalScript.SYMBOLTABLE(ast = p) := inInteractiveSymbolTable;
scode := SCodeUtil.translateAbsyn2SCode(p);
(outCache, outEnv, _, outDae) := Inst.instantiateClass(inCache, InnerOuter.emptyInstHierarchy, scode, inClassName);
outDae := DAEUtil.transformationsBeforeBackend(outCache,outEnv,outDae);
(outCache, outEnv, outDae) := matchcontinue(inCache, inEnv, inClassName, rewriteRulesFile, inInteractiveSymbolTable)
local
Absyn.Program p;
SCode.Program scode;

case (_, _, _, _, _)
equation
// set the rewrite rules flag
Flags.setConfigString(Flags.REWRITE_RULES_FILE, rewriteRulesFile);
// load the rewrite rules
RewriteRules.loadRules();
GlobalScript.SYMBOLTABLE(ast = p) = inInteractiveSymbolTable;
scode = SCodeUtil.translateAbsyn2SCode(p);
(outCache, outEnv, _, outDae) = Inst.instantiateClass(inCache, InnerOuter.emptyInstHierarchy, scode, inClassName);
outDae = DAEUtil.transformationsBeforeBackend(outCache,outEnv,outDae);
// clear the rewrite rules after running the front-end
Flags.setConfigString(Flags.REWRITE_RULES_FILE, "");
RewriteRules.clearRules();
then
(outCache, outEnv, outDae);

case (_, _, _, _, _)
equation
// clear the rewrite rules if we fail!
Flags.setConfigString(Flags.REWRITE_RULES_FILE, "");
RewriteRules.clearRules();
then
fail();
end matchcontinue;
end dumpXMLDAEFrontEnd;

protected function dumpXMLDAE " author: fildo
Expand All @@ -4464,7 +4488,7 @@ algorithm
(outCache,outInteractiveSymbolTable3,xml_filename) :=
match (inCache,inEnv,vals,inInteractiveSymbolTable,inMsg)
local
String cname_str,filenameprefix,compileDir;
String cname_str,filenameprefix,compileDir,rewriteRulesFile;
list<Env.Frame> env;
Absyn.Path classname;
Absyn.Program p;
Expand All @@ -4476,11 +4500,14 @@ algorithm
DAE.DAElist dae_1,dae;
list<SCode.Element> p_1;

case (cache,env,{Values.CODE(Absyn.C_TYPENAME(classname)),Values.STRING(string="flat"),Values.BOOL(addOriginalIncidenceMatrix),Values.BOOL(addSolvingInfo),Values.BOOL(addMathMLCode),Values.BOOL(dumpResiduals),Values.STRING(filenameprefix)},st,msg)
case (cache,env,{Values.CODE(Absyn.C_TYPENAME(classname)),Values.STRING(string="flat"),
Values.BOOL(addOriginalIncidenceMatrix),Values.BOOL(addSolvingInfo),
Values.BOOL(addMathMLCode),Values.BOOL(dumpResiduals),
Values.STRING(filenameprefix),Values.STRING(rewriteRulesFile)},st,msg)
equation
Error.clearMessages() "Clear messages";

(cache, env, dae) = dumpXMLDAEFrontEnd(cache, env, classname, st);
(cache, env, dae) = dumpXMLDAEFrontEnd(cache, env, classname, rewriteRulesFile, st);

compileDir = System.pwd() +& System.pathDelimiter();
cname_str = Absyn.pathString(classname);
Expand All @@ -4498,12 +4525,15 @@ algorithm
then
(cache,st,stringAppendList({compileDir,xml_filename}));

case (cache,env,{Values.CODE(Absyn.C_TYPENAME(classname)),Values.STRING(string="optimiser"),Values.BOOL(addOriginalIncidenceMatrix),Values.BOOL(addSolvingInfo),Values.BOOL(addMathMLCode),Values.BOOL(dumpResiduals),Values.STRING(filenameprefix)},(st as GlobalScript.SYMBOLTABLE(ast = p)),msg)
case (cache,env,{Values.CODE(Absyn.C_TYPENAME(classname)),Values.STRING(string="optimiser"),
Values.BOOL(addOriginalIncidenceMatrix),Values.BOOL(addSolvingInfo),
Values.BOOL(addMathMLCode),Values.BOOL(dumpResiduals),
Values.STRING(filenameprefix),Values.STRING(rewriteRulesFile)},(st as GlobalScript.SYMBOLTABLE(ast = p)),msg)
equation
//asInSimulationCode==false => it's NOT necessary to do all the translation's steps before dumping with xml
Error.clearMessages() "Clear messages";

(cache, env, dae) = dumpXMLDAEFrontEnd(cache, env, classname, st);
(cache, env, dae) = dumpXMLDAEFrontEnd(cache, env, classname, rewriteRulesFile, st);

compileDir = System.pwd() +& System.pathDelimiter();
cname_str = Absyn.pathString(classname);
Expand All @@ -4522,12 +4552,15 @@ algorithm
then
(cache,st,stringAppendList({compileDir,xml_filename}));

case (cache,env,{Values.CODE(Absyn.C_TYPENAME(classname)),Values.STRING(string="backEnd"),Values.BOOL(addOriginalIncidenceMatrix),Values.BOOL(addSolvingInfo),Values.BOOL(addMathMLCode),Values.BOOL(dumpResiduals),Values.STRING(filenameprefix)},(st as GlobalScript.SYMBOLTABLE(ast = p)),msg)
case (cache,env,{Values.CODE(Absyn.C_TYPENAME(classname)),Values.STRING(string="backEnd"),
Values.BOOL(addOriginalIncidenceMatrix),Values.BOOL(addSolvingInfo),
Values.BOOL(addMathMLCode),Values.BOOL(dumpResiduals),
Values.STRING(filenameprefix),Values.STRING(rewriteRulesFile)},(st as GlobalScript.SYMBOLTABLE(ast = p)),msg)
equation
//asInSimulationCode==true => it's necessary to do all the translation's steps before dumping with xml
Error.clearMessages() "Clear messages";

(cache, env, dae) = dumpXMLDAEFrontEnd(cache, env, classname, st);
(cache, env, dae) = dumpXMLDAEFrontEnd(cache, env, classname, rewriteRulesFile, st);

compileDir = System.pwd() +& System.pathDelimiter();
cname_str = Absyn.pathString(classname);
Expand All @@ -4544,12 +4577,15 @@ algorithm
then
(cache,st,stringAppendList({compileDir,xml_filename}));

case (cache,env,{Values.CODE(Absyn.C_TYPENAME(classname)),Values.STRING(string="stateSpace"),Values.BOOL(addOriginalIncidenceMatrix),Values.BOOL(addSolvingInfo),Values.BOOL(addMathMLCode),Values.BOOL(dumpResiduals),Values.STRING(filenameprefix)},(st as GlobalScript.SYMBOLTABLE(ast = p)),msg)
case (cache,env,{Values.CODE(Absyn.C_TYPENAME(classname)),Values.STRING(string="stateSpace"),
Values.BOOL(addOriginalIncidenceMatrix),Values.BOOL(addSolvingInfo),
Values.BOOL(addMathMLCode),Values.BOOL(dumpResiduals),
Values.STRING(filenameprefix),Values.STRING(rewriteRulesFile)},(st as GlobalScript.SYMBOLTABLE(ast = p)),msg)
equation
//asInSimulationCode==true => it's necessary to do all the translation's steps before dumping with xml
Error.clearMessages() "Clear messages";

(cache, env, dae) = dumpXMLDAEFrontEnd(cache, env, classname, st);
(cache, env, dae) = dumpXMLDAEFrontEnd(cache, env, classname, rewriteRulesFile, st);

compileDir = System.pwd() +& System.pathDelimiter();
cname_str = Absyn.pathString(classname);
Expand Down

0 comments on commit 66a2bd9

Please sign in to comment.