Skip to content

Commit

Permalink
- Added scriptable API functions: isPartial isModel regularFileExists
Browse files Browse the repository at this point in the history
  - Added script BuildModelRecursive.mos to systematically try to build every example in MSL


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11057 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Feb 9, 2012
1 parent 297cb69 commit 7614b72
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 14 deletions.
33 changes: 32 additions & 1 deletion Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -1130,6 +1130,15 @@ external "builtin";
annotation(preferredView="text");
end getVersion;

function regularFileExists
"The contents of the given file are returned.
Note that if the function fails, the error message is returned as a string instead of multiple output or similar."
input String fileName;
output Boolean exists;
external "builtin" annotation(__OpenModelica_Impure=true);
annotation(preferredView="text");
end regularFileExists;

function readFile
"The contents of the given file are returned.
Note that if the function fails, the error message is returned as a string instead of multiple output or similar."
Expand Down Expand Up @@ -2161,11 +2170,33 @@ function isPackage
external "builtin";
annotation(
Documentation(info="<html>
Returns true if the given classname is a package.
Returns true if the given class is a package.
</html>"),
preferredView="text");
end isPackage;

function isPartial
input TypeName cl;
output Boolean b;
external "builtin";
annotation(
Documentation(info="<html>
Returns true if the given class is partial.
</html>"),
preferredView="text");
end isPartial;

function isModel
input TypeName cl;
output Boolean b;
external "builtin";
annotation(
Documentation(info="<html>
Returns true if the given class has restriction model.
</html>"),
preferredView="text");
end isModel;

annotation(preferredView="text");
end Scripting;

Expand Down
18 changes: 18 additions & 0 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -1465,6 +1465,12 @@ algorithm
then
(cache,Values.INTEGER(resI),st);

case (cache,env,"regularFileExists",{Values.STRING(str)},st,msg)
equation
b = System.regularFileExists(str);
then
(cache,Values.BOOL(b),st);

case (cache,env,"readFile",{Values.STRING(str)},st,msg)
equation
str_1 = System.readFile(str);
Expand Down Expand Up @@ -1691,6 +1697,18 @@ algorithm
then
(cache,Values.BOOL(b),st);

case (cache,env,"isPartial",{Values.CODE(Absyn.C_TYPENAME(classpath))},st as Interactive.SYMBOLTABLE(ast=p),msg)
equation
b = Interactive.isPartial(classpath, p);
then
(cache,Values.BOOL(b),st);

case (cache,env,"isModel",{Values.CODE(Absyn.C_TYPENAME(classpath))},st as Interactive.SYMBOLTABLE(ast=p),msg)
equation
b = Interactive.isModel(classpath, p);
then
(cache,Values.BOOL(b),st);

case (cache,env,"getAstAsCorbaString",{Values.STRING("<interactive>")},st as Interactive.SYMBOLTABLE(ast=p),msg)
equation
Print.clearBuf();
Expand Down
42 changes: 29 additions & 13 deletions Compiler/Script/Interactive.mo
Expand Up @@ -1954,7 +1954,8 @@ algorithm
equation
matchApiFunction(istmts, "isModel");
{Absyn.CREF(componentRef = cr)} = getApiFunctionArgs(istmts);
b1 = isModel(cr, p);
path = Absyn.crefToPath(cr);
b1 = isModel(path, p);
resstr = boolString(b1);
then
(resstr,st);
Expand Down Expand Up @@ -2068,8 +2069,9 @@ algorithm
equation
matchApiFunction(istmts, "existModel");
{Absyn.CREF(componentRef = cr)} = getApiFunctionArgs(istmts);
path = Absyn.crefToPath(cr);
b1 = existClass(cr, p);
b2 = isModel(cr, p);
b2 = isModel(path, p);
b = boolAnd(b1, b2);
resstr = boolString(b);
then
Expand Down Expand Up @@ -8501,28 +8503,23 @@ algorithm
end matchcontinue;
end isConnector;

protected function isModel
public function isModel
"function: isModel
This function takes a component reference and a program.
It returns true if the refrenced class has the restriction
\"model\", otherwise it returns false."
input Absyn.ComponentRef inComponentRef;
input Absyn.Path path;
input Absyn.Program inProgram;
output Boolean outBoolean;
algorithm
outBoolean:=
matchcontinue (inComponentRef,inProgram)
outBoolean := matchcontinue (path,inProgram)
local
Absyn.Path path;
Absyn.ComponentRef cr;
Absyn.Program p;
case (cr,p)
case (path,p)
equation
path = Absyn.crefToPath(cr);
Absyn.CLASS(_,_,_,_,Absyn.R_MODEL(),_,_) = getPathedClassInProgram(path, p);
then
true;
case (cr,p) then false;
then true;
else false;
end matchcontinue;
end isModel;

Expand Down Expand Up @@ -8649,6 +8646,25 @@ algorithm
end matchcontinue;
end isClass;

public function isPartial
"function: isClass
This function takes a component reference and a program.
It returns true if the refrenced class has the restriction
\"class\", otherwise it returns false."
input Absyn.Path p;
input Absyn.Program prog;
output Boolean outBoolean;
algorithm
outBoolean:=
matchcontinue (p,prog)
case (p,prog)
equation
Absyn.CLASS(partialPrefix=true) = getPathedClassInProgram(p, prog);
then true;
else false;
end matchcontinue;
end isPartial;

protected function isParameter
"function: isParameter
This function takes a class and a component reference and a program
Expand Down
25 changes: 25 additions & 0 deletions Examples/BuildModelRecursive.mos
@@ -0,0 +1,25 @@
// Note: Run with +g=MetaModelica

log:="BuildModelRecursive.log";
loadModel(Modelica,{"3.1"});

omc:=getInstallationDirectoryPath()+"/bin/omc";
a:={typeNameString(x) for x guard max("Examples" == s for s in typeNameStrings(x)) and isModel(x) and (not isPartial(x)) in getClassNames(recursive=true)};
isModel(Modelica.Blocks.Examples.PID_Controller);
getErrorString();
print("Numer of classes to build: " + String(size(a,1)));
system("rm -f Modelica.*");
min(writeFile(s + ".mos","loadModel(Modelica,{\"3.1\"});\nbuildModel("+s+");\ngetErrorString();") for s in a);
getErrorString();
min(0==system(omc + " " + s + ".mos") for s in a);
getErrorString();

echo(false);
system("rm -f " + log);
line:="================================================================================\n";
str:="";
str:=line+"Results (" + String(sum(if OpenModelica.Scripting.regularFileExists(s) then 1 else 0 for s in a)) + "/" + String(size(a,1)) + " succeeded)\n" + line;
estr:=getErrorString();
writeFile(log,if estr<>"" then estr else str);getErrorString();
writeFile(log,sum(s + ": " + boolString(OpenModelica.Scripting.regularFileExists(s)) + "\n" for s in a),append=true);getErrorString();
print(readFile(log));

0 comments on commit 7614b72

Please sign in to comment.