Skip to content

Commit

Permalink
Dump annotations for replaceable classes in getModelInstance (#10306)
Browse files Browse the repository at this point in the history
  • Loading branch information
perost committed Mar 2, 2023
1 parent 9f5492b commit ad95fd6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
39 changes: 31 additions & 8 deletions OMCompiler/Compiler/Script/NFApi.mo
Expand Up @@ -1194,14 +1194,15 @@ protected
InstNode node, derivedNode;
Absyn.Path path;
Option<list<Absyn.Subscript>> odims;
SCode.Comment cmt;
algorithm
elem := InstNode.definition(cls);

json := JSON.addPair("$kind", JSON.makeString("class"), json);
json := JSON.addPair("name", JSON.makeString(InstNode.name(cls)), json);
json := JSON.addPairNotNull("prefixes", dumpJSONClassPrefixes(elem, scope), json);

SCode.Element.CLASS(classDef = cdef) := elem;
SCode.Element.CLASS(classDef = cdef, cmt = cmt) := elem;

() := match cdef
case SCode.ClassDef.DERIVED(typeSpec = Absyn.TypeSpec.TPATH(path = path, arrayDim = odims))
Expand Down Expand Up @@ -1229,6 +1230,8 @@ algorithm
else ();
end match;

json := dumpJSONCommentAnnotation(SOME(cmt), scope, json,
{"Dialog", "choices", "choicesAllMatching"});
json := JSON.addPair("source", dumpJSONSourceInfo(InstNode.info(cls)), json);
end dumpJSONReplaceableClass;

Expand Down Expand Up @@ -1564,34 +1567,51 @@ algorithm
end if;

if dumpAnnotation then
json := dumpJSONAnnotationOpt(cmt.annotation_, scope, failOnError, json);
json := dumpJSONAnnotationOpt(cmt.annotation_, scope, {}, failOnError, json);
end if;
end if;
end dumpJSONCommentOpt;

function dumpJSONCommentAnnotation
input Option<SCode.Comment> cmtOpt;
input InstNode scope;
input output JSON json;
input list<String> filter = {};
input Boolean failOnError = false;
protected
SCode.Comment cmt;
algorithm
if isSome(cmtOpt) then
SOME(cmt) := cmtOpt;
json := dumpJSONAnnotationOpt(cmt.annotation_, scope, filter, failOnError, json);
end if;
end dumpJSONCommentAnnotation;

function dumpJSONAnnotationOpt
input Option<SCode.Annotation> annOpt;
input InstNode scope;
input list<String> filter;
input Boolean failOnError;
input output JSON json;
protected
SCode.Annotation ann;
algorithm
if isSome(annOpt) then
SOME(ann) := annOpt;
json := JSON.addPair("annotation", dumpJSONAnnotationMod(ann.modification, scope, failOnError), json);
json := JSON.addPair("annotation", dumpJSONAnnotationMod(ann.modification, scope, filter, failOnError), json);
end if;
end dumpJSONAnnotationOpt;

function dumpJSONAnnotationMod
input SCode.Mod mod;
input InstNode scope;
input list<String> filter;
input Boolean failOnError;
output JSON json;
algorithm
json := match mod
case SCode.Mod.MOD()
then dumpJSONAnnotationSubMods(mod.subModLst, scope, failOnError);
then dumpJSONAnnotationSubMods(mod.subModLst, scope, filter, failOnError);

else JSON.makeNull();
end match;
Expand All @@ -1600,11 +1620,14 @@ end dumpJSONAnnotationMod;
function dumpJSONAnnotationSubMods
input list<SCode.SubMod> subMods;
input InstNode scope;
input list<String> filter;
input Boolean failOnError;
output JSON json = JSON.makeNull();
algorithm
for m in subMods loop
json := dumpJSONAnnotationSubMod(m, scope, failOnError, json);
if listEmpty(filter) or List.contains(filter, m.ident, stringEq) then
json := dumpJSONAnnotationSubMod(m, scope, failOnError, json);
end if;
end for;
end dumpJSONAnnotationSubMods;

Expand Down Expand Up @@ -1656,7 +1679,7 @@ algorithm

case (_, SCode.Mod.MOD())
algorithm
json := JSON.addPair(name, dumpJSONAnnotationSubMods(mod.subModLst, scope, failOnError), json);
json := JSON.addPair(name, dumpJSONAnnotationSubMods(mod.subModLst, scope, {}, failOnError), json);
then
();

Expand Down Expand Up @@ -1851,7 +1874,7 @@ algorithm
Equation.CONNECT(lhs = lhs, rhs = rhs, source = src) := connEq;
json := JSON.addPair("lhs", Expression.toJSON(lhs), json);
json := JSON.addPair("rhs", Expression.toJSON(rhs), json);
json := dumpJSONCommentOpt(ElementSource.getOptComment(src), scope, json, dumpComment = false);
json := dumpJSONCommentAnnotation(ElementSource.getOptComment(src), scope, json);
end dumpJSONConnection;

function dumpJSONStateCalls
Expand Down Expand Up @@ -1882,7 +1905,7 @@ algorithm
j := JSON.addElement(Expression.toJSON(arg), j);
end for;
json := JSON.addPair("arguments", j, json);
json := dumpJSONCommentOpt(ElementSource.getOptComment(src), scope, json, dumpComment = false);
json := dumpJSONCommentAnnotation(ElementSource.getOptComment(src), scope, json);
then
();

Expand Down
Expand Up @@ -7,7 +7,7 @@

loadString("
model M
replaceable Real x \"definition comment\" constrainedby Real(start = 1);
replaceable Real x \"definition comment\" annotation(choicesAllMatching = false) constrainedby Real(start = 1);
replaceable Real y constrainedby Real(start = 2) \"constrainedby comment\";
replaceable Real z \"definition comment\" constrainedby Real \"constrainedby comment\" annotation(choicesAllMatching = true);
end M;
Expand All @@ -33,7 +33,10 @@ getModelInstance(M, prettyPrint = true);
// }
// }
// },
// \"comment\": \"definition comment\"
// \"comment\": \"definition comment\",
// \"annotation\": {
// \"choicesAllMatching\": false
// }
// },
// {
// \"$kind\": \"component\",
Expand Down

0 comments on commit ad95fd6

Please sign in to comment.