Skip to content

Commit

Permalink
Improve getModelInstanceIcon (#10078)
Browse files Browse the repository at this point in the history
- Instantiate the scope of an annotation if the annotation contains
  component references that we need to be able to look up.
  • Loading branch information
perost committed Jan 20, 2023
1 parent 7a97398 commit 84c3c16
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 6 deletions.
18 changes: 13 additions & 5 deletions OMCompiler/Compiler/FrontEnd/AbsynUtil.mo
Expand Up @@ -3148,14 +3148,22 @@ algorithm
case Absyn.NOMOD() then true;

// search inside, some(exp)
case Absyn.EQMOD()
algorithm
(_, lst::{}) := traverseExpBidir(eqMod.exp, onlyLiteralsInExpEnter, onlyLiteralsInExpExit, {}::{});
then
listEmpty(lst);
case Absyn.EQMOD() then onlyLiteralsInExp(eqMod.exp);

end match;
end onlyLiteralsInEqMod;

public function onlyLiteralsInExp
"Checks if an expression only contains literal expressions."
input Absyn.Exp exp;
output Boolean onlyLiterals;
protected
list<Absyn.Exp> lst;
algorithm
(_, lst::{}) := traverseExpBidir(exp, onlyLiteralsInExpEnter, onlyLiteralsInExpExit, {}::{});
onlyLiterals := listEmpty(lst);
end onlyLiteralsInExp;

protected function onlyLiteralsInExpEnter
"@author: adrpo
Visitor function for checking if Absyn.Exp contains only literals, NO CREFS!
Expand Down
32 changes: 32 additions & 0 deletions OMCompiler/Compiler/FrontEnd/SCodeUtil.mo
Expand Up @@ -6174,5 +6174,37 @@ function isNonEmptyAlgorithm
output Boolean res = not listEmpty(alg.statements);
end isNonEmptyAlgorithm;

function onlyLiteralsInMod
"Checks if the bindings in a modifier only contains literal expressions."
input SCode.Mod mod;
output Boolean onlyLiterals;
protected
list<Absyn.Exp> lst;
algorithm
onlyLiterals := match mod
case SCode.Mod.MOD()
algorithm
if isSome(mod.binding) then
onlyLiterals := AbsynUtil.onlyLiteralsInExp(Util.getOption(mod.binding));
else
onlyLiterals := true;
end if;

if onlyLiterals then
for m in mod.subModLst loop
onlyLiterals := onlyLiteralsInMod(m.mod);

if not onlyLiterals then
break;
end if;
end for;
end if;
then
onlyLiterals;

else true;
end match;
end onlyLiteralsInMod;

annotation(__OpenModelica_Interface="frontend");
end SCodeUtil;
21 changes: 20 additions & 1 deletion OMCompiler/Compiler/Script/NFApi.mo
Expand Up @@ -1010,6 +1010,9 @@ protected
SCode.Annotation ann;
array<InstNode> exts;
JSON j;
InstNode scope = node;
InstContext.Type context;
Boolean annotation_is_literal = true;
algorithm
Inst.expand(node);
json := JSON.addPair("name", dumpJSONNodePath(node), json);
Expand All @@ -1033,13 +1036,29 @@ algorithm
algorithm
ann.modification := SCodeUtil.filterSubMods(ann.modification,
function SCodeUtil.filterGivenSubModNames(namesToKeep = {"Icon", "IconMap"}));
annotation_is_literal := SCodeUtil.onlyLiteralsInMod(ann.modification);
then
if SCodeUtil.isEmptyMod(ann.modification) then NONE() else SOME(SCode.Comment.COMMENT(SOME(ann), NONE()));

else NONE();
end match;

json := dumpJSONCommentOpt(cmt, node, json, failOnError = true);
// Instantiate the scope if the annotation contains component references that
// we need to be able to look up.
if not annotation_is_literal then
ErrorExt.setCheckpoint(getInstanceName());
try
context := InstContext.set(NFInstContext.CLASS, NFInstContext.RELAXED);
scope := InstNode.setNodeType(InstNodeType.ROOT_CLASS(InstNode.EMPTY_NODE()), scope);
scope := Inst.instantiate(scope, InstNode.parent(scope), context, true);
Inst.insertGeneratedInners(scope, InstNode.topScope(scope), context);
Inst.instExpressions(scope, context = context);
else
end try;
ErrorExt.rollBack(getInstanceName());
end if;

json := dumpJSONCommentOpt(cmt, scope, json, failOnError = true);
end dumpJSONInstanceIcon;

function dumpJSONInstanceIconExtends
Expand Down
137 changes: 137 additions & 0 deletions testsuite/openmodelica/instance-API/GetModelInstanceIcon3.mos
@@ -0,0 +1,137 @@
// name: GetModelInstanceIcon3
// keywords:
// status: correct
// cflags: -d=newInst
//
//

loadString("
package Icons
partial model Example
parameter Boolean visible = true;
annotation (Icon(coordinateSystem(preserveAspectRatio=false, extent={{-100,-100},{100,100}}), graphics={
Ellipse(visible = visible,
lineColor = {75,138,73},
fillColor={255,255,255},
fillPattern = FillPattern.Solid,
extent = {{-100,-100},{100,100}})}),
IconMap(extent = {{0, 0}, {0, 0}}));
end Example;
end Icons;

model M
extends Icons.Example;
annotation (Icon(coordinateSystem(preserveAspectRatio=true)));
end M;
");

getModelInstanceIcon(M, prettyPrint=true);

// Result:
// true
// "{
// \"name\": \"M\",
// \"extends\": [
// {
// \"baseClass\": {
// \"name\": \"Icons.Example\",
// \"annotation\": {
// \"Icon\": {
// \"coordinateSystem\": {
// \"preserveAspectRatio\": false,
// \"extent\": [
// [
// -100,
// -100
// ],
// [
// 100,
// 100
// ]
// ]
// },
// \"graphics\": [
// {
// \"$kind\": \"record\",
// \"name\": \"Ellipse\",
// \"elements\": [
// {
// \"$kind\": \"cref\",
// \"parts\": [
// {
// \"name\": \"visible\"
// }
// ]
// },
// [
// 0,
// 0
// ],
// 0,
// [
// 75,
// 138,
// 73
// ],
// [
// 255,
// 255,
// 255
// ],
// {
// \"$kind\": \"enum\",
// \"name\": \"LinePattern.Solid\",
// \"index\": 2
// },
// {
// \"$kind\": \"enum\",
// \"name\": \"FillPattern.Solid\",
// \"index\": 2
// },
// 0.25,
// [
// [
// -100,
// -100
// ],
// [
// 100,
// 100
// ]
// ],
// 0,
// 360,
// {
// \"$kind\": \"enum\",
// \"name\": \"EllipseClosure.Chord\",
// \"index\": 2
// }
// ]
// }
// ]
// },
// \"IconMap\": {
// \"extent\": [
// [
// 0,
// 0
// ],
// [
// 0,
// 0
// ]
// ]
// }
// }
// }
// }
// ],
// \"annotation\": {
// \"Icon\": {
// \"coordinateSystem\": {
// \"preserveAspectRatio\": true
// }
// }
// }
// }"
// endResult
1 change: 1 addition & 0 deletions testsuite/openmodelica/instance-API/Makefile
Expand Up @@ -25,6 +25,7 @@ GetModelInstanceExtends2.mos \
GetModelInstanceExtends3.mos \
GetModelInstanceIcon1.mos \
GetModelInstanceIcon2.mos \
GetModelInstanceIcon3.mos \
GetModelInstanceInnerOuter1.mos \
GetModelInstanceInnerOuter2.mos \
GetModelInstanceInnerOuter3.mos \
Expand Down

0 comments on commit 84c3c16

Please sign in to comment.