Skip to content

Commit

Permalink
saveTotalModel change
Browse files Browse the repository at this point in the history
- The exported model is added at the end of the file, extending from the base class and with the original annotations added (for experiment annotations)
- showAnnotations=false (the default) will now only remove useless annotations like graphical annotations. experiment, Evaluate and other things not on the blacklist will pass through.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@21347 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jun 30, 2014
1 parent 281175e commit 9070d0e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 36 deletions.
8 changes: 1 addition & 7 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -1857,13 +1857,7 @@ external "builtin";
annotation(preferredView="text");
end save;

function saveTotalSCode
input String fileName;
input TypeName className;
output Boolean success;
external "builtin";
annotation(preferredView="text");
end saveTotalSCode;
function saveTotalSCode = saveTotalModel;

function translateGraphics
input TypeName className;
Expand Down
12 changes: 11 additions & 1 deletion Compiler/FrontEnd/SCodeDump.mo
Expand Up @@ -46,7 +46,7 @@ protected import List;
protected import SCodeDumpTpl;
protected import Tpl;

public constant SCodeDumpOptions defaultOptions = OPTIONS(false,false,false,false,true);
public constant SCodeDumpOptions defaultOptions = OPTIONS(false,false,false,false,true,true);

public uniontype SCodeDumpOptions
record OPTIONS
Expand All @@ -55,6 +55,7 @@ public uniontype SCodeDumpOptions
Boolean stripProtectedClasses;
Boolean stripProtectedComponents;
Boolean stripMetaRecords "The automatically generated records that change scope from uniontype to the package";
Boolean stripGraphicalAnnotations;
end OPTIONS;
end SCodeDumpOptions;

Expand Down Expand Up @@ -99,6 +100,15 @@ algorithm
outString := Tpl.tplString2(SCodeDumpTpl.dumpModifier, inMod, options);
end printModStr;

public function printCommentStr
"Prints SCode.Mod to a string."
input SCode.Comment inComment;
input SCodeDumpOptions options;
output String outString;
algorithm
outString := Tpl.tplString2(SCodeDumpTpl.dumpComment, inComment, options);
end printCommentStr;

public function restrString
"Prints SCode.Restriction to a string."
input SCode.Restriction inRestriction;
Expand Down
53 changes: 31 additions & 22 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -103,6 +103,7 @@ protected import List;
protected import Lookup;
protected import MetaUtil;
protected import Mod;
protected import NFSCodeLookup;
protected import Prefix;
protected import Parser;
protected import Print;
Expand Down Expand Up @@ -1126,7 +1127,7 @@ algorithm
name = Absyn.pathLastIdent(path);
str = Debug.bcallret2(name ==& "Absyn", Dump.unparseStr, p, false, "");
str = Debug.bcallret2(name ==& "SCode", SCodeDump.programStr, scodeP, SCodeDump.defaultOptions, str);
str = Debug.bcallret2(name ==& "MetaModelicaInterface", SCodeDump.programStr, scodeP, SCodeDump.OPTIONS(true,false,true,true,true), str);
str = Debug.bcallret2(name ==& "MetaModelicaInterface", SCodeDump.programStr, scodeP, SCodeDump.OPTIONS(true,false,true,true,true,true), str);
str = Debug.bcallret1(name ==& "Internal", System.anyStringCode, p, str);
then
(cache,Values.STRING(str),st);
Expand All @@ -1144,7 +1145,7 @@ algorithm
str = Debug.bcallret1(name ==& "Internal", System.anyStringCode, p, str);
cl = SCodeUtil.getElementWithPathCheckBuiltin(scodeP, className);
str = Debug.bcallret2(name ==& "SCode", SCodeDump.unparseElementStr, cl, SCodeDump.defaultOptions, str);
str = Debug.bcallret2(name ==& "MetaModelicaInterface", SCodeDump.unparseElementStr, cl, SCodeDump.OPTIONS(true,false,true,true,true), str);
str = Debug.bcallret2(name ==& "MetaModelicaInterface", SCodeDump.unparseElementStr, cl, SCodeDump.OPTIONS(true,false,true,true,true,true), str);
then
(cache,Values.STRING(str),st);

Expand Down Expand Up @@ -2216,16 +2217,6 @@ algorithm
then
(cache,Values.BOOL(true),st);

case (cache,_,"saveTotalModel",{Values.STRING(filename),Values.CODE(Absyn.C_TYPENAME(classpath))},(st as GlobalScript.SYMBOLTABLE(ast = _)),_)
equation
(scodeP, st) = Interactive.symbolTableToSCode(st);
(scodeP, _) = NFSCodeFlatten.flattenClassInProgram(classpath, scodeP);
scodeP = SCode.removeBuiltinsFromTopScope(scodeP);
str = SCodeDump.programStr(scodeP,SCodeDump.defaultOptions);
System.writeFile(filename, str);
then
(cache,Values.BOOL(true),st);

case (cache,_,"save",{Values.CODE(Absyn.C_TYPENAME(className))},(st as GlobalScript.SYMBOLTABLE(ast = p)),_)
equation
(newp,filename) = Interactive.getContainedClassAndFile(className, p);
Expand Down Expand Up @@ -2272,19 +2263,14 @@ algorithm
then
(cache,Values.BOOL(false),st);

case (cache, _, "saveTotalSCode",
{Values.STRING(filename), Values.CODE(Absyn.C_TYPENAME(classpath))}, st, _)
case (cache,_,"saveTotalModel",{Values.STRING(filename),Values.CODE(Absyn.C_TYPENAME(classpath))},st,_)
equation
(scodeP, st) = Interactive.symbolTableToSCode(st);
(scodeP, _) = NFSCodeFlatten.flattenClassInProgram(classpath, scodeP);
scodeP = SCode.removeBuiltinsFromTopScope(scodeP);
str = SCodeDump.programStr(scodeP,SCodeDump.defaultOptions);
System.writeFile(filename, str);
st = saveTotalModel(filename,classpath,st);
then
(cache, Values.BOOL(true), st);
(cache,Values.BOOL(true),st);

case (cache, _, "saveTotalSCode", _, st, _)
then (cache, Values.BOOL(false), st);
case (cache,_,"saveTotalModel",{Values.STRING(filename),Values.CODE(Absyn.C_TYPENAME(classpath))},(st as GlobalScript.SYMBOLTABLE(ast = _)),_)
then (cache,Values.BOOL(false),st);

case (cache,_,"getDocumentationAnnotation",{Values.CODE(Absyn.C_TYPENAME(classpath))},st as GlobalScript.SYMBOLTABLE(ast=p),_)
equation
Expand Down Expand Up @@ -7692,4 +7678,27 @@ algorithm
end match;
end checkFMUVersion;

protected function saveTotalModel
input String filename;
input Absyn.Path classpath;
input GlobalScript.SymbolTable st;
output GlobalScript.SymbolTable outSt;
protected
SCode.Program scodeP;
String str,str1,str2;
NFSCodeEnv.Env env;
SCode.Comment cmt;
algorithm
(scodeP, outSt) := Interactive.symbolTableToSCode(st);
(scodeP, env) := NFSCodeFlatten.flattenClassInProgram(classpath, scodeP);
(NFSCodeEnv.CLASS(cls=SCode.CLASS(cmt=cmt)),_,_) := NFSCodeLookup.lookupClassName(classpath, env, Absyn.dummyInfo);
scodeP := SCode.removeBuiltinsFromTopScope(scodeP);
str := SCodeDump.programStr(scodeP,SCodeDump.defaultOptions);
str1 := Absyn.pathLastIdent(classpath);
str2 := SCodeDump.printCommentStr(cmt,SCodeDump.defaultOptions);
str2 := Util.if_(stringEq(str2,""), "", " " +& str2 +& ";\n");
str1 := "\nmodel " +& str1 +& "\n extends " +& Absyn.pathString(classpath) +& ";\n"+str2+"end " +& str1 +& ";\n";
System.writeFile(filename, str +& str1);
end saveTotalModel;

end CevalScript;
42 changes: 36 additions & 6 deletions Compiler/Template/SCodeDumpTpl.tpl
Expand Up @@ -647,6 +647,16 @@ match modifier
'<%submod_str%><%binding_str%>'
end dumpModifier;

template dumpAnnotationModifier(SCode.Mod modifier, SCodeDumpOptions options)
::=
match modifier
case MOD(__) then
let binding_str = dumpModifierBinding(binding)
let text = subModLst |> submod => dumpAnnotationSubModifier(submod, options) ;separator=", "
let submod_str = if text then '(<%text%>)'
'<%submod_str%><%binding_str%>'
end dumpAnnotationModifier;

template dumpModifierPrefix(SCode.Mod modifier)
::=
match modifier
Expand Down Expand Up @@ -681,6 +691,27 @@ match submod
'<%dumpRedeclModifier(A,options)%>'
end dumpSubModifier;

template dumpAnnotationSubModifier(SCode.SubMod submod, SCodeDumpOptions options)
::=
match submod
case NAMEMOD(A = nameMod as MOD(__)) then
(if Config.showAnnotations() then
'<%dumpModifierPrefix(A)%><%ident%><%dumpAnnotationModifier(nameMod,options)%>'
else
match ident
case "choices"
case "Documentation"
case "Dialog"
case "Diagram"
case "Icon"
case "Line"
case "Placement"
then ""
else '<%dumpModifierPrefix(nameMod)%><%ident%><%dumpAnnotationModifier(nameMod,options)%>')
case NAMEMOD(A = REDECL(__)) then
'<%dumpRedeclModifier(A,options)%>'
end dumpAnnotationSubModifier;

template dumpAttributes(SCode.Attributes attributes)
::=
match attributes
Expand Down Expand Up @@ -731,12 +762,10 @@ end dumpAnnotationOpt;

template dumpAnnotation(SCode.Annotation annotation, SCodeDumpOptions options)
::=
if Config.showAnnotations() then
match annotation
case ANNOTATION(__) then
let modifStr = '<%dumpModifier(modification,options)%>'
let annStr = if modifStr then modifStr else '()'
' annotation<%annStr%>'
let modifStr = dumpAnnotationModifier(modification,options)
if modifStr then ' annotation<%modifStr%>'
end dumpAnnotation;

template dumpAnnotationElement(SCode.Annotation annotation, SCodeDumpOptions options)
Expand Down Expand Up @@ -768,7 +797,6 @@ end dumpCommentOpt;

template dumpComment(SCode.Comment comment, SCodeDumpOptions options)
::=
if Config.showAnnotations() then
match comment
case COMMENT(__) then
let ann_str = dumpAnnotationOpt(annotation_, options)
Expand All @@ -777,7 +805,9 @@ if Config.showAnnotations() then
end dumpComment;

template dumpCommentStr(Option<String> comment)
::= match comment
::=
if Config.showAnnotations() then
match comment
case SOME(cmt) then ' "<%cmt%>"'
else ''
end dumpCommentStr;
Expand Down

0 comments on commit 9070d0e

Please sign in to comment.