Skip to content

Commit 422ecd3

Browse files
authored
Add constrainedby info to getModelInstance (#9877)
1 parent 38d0683 commit 422ecd3

File tree

6 files changed

+167
-13
lines changed

6 files changed

+167
-13
lines changed

OMCompiler/Compiler/Script/NFApi.mo

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ algorithm
979979
JSON.makeString(Restriction.toString(InstNode.restriction(node))), json);
980980
json := dumpJSONSCodeMod(SCodeUtil.elementMod(def), json);
981981

982-
json := JSON.addPairNotNull("prefixes", dumpJSONClassPrefixes(def), json);
982+
json := JSON.addPairNotNull("prefixes", dumpJSONClassPrefixes(def, node), json);
983983

984984
if not listEmpty(exts) then
985985
json := JSON.addPair("extends", dumpJSONExtendsList(exts), json);
@@ -1137,7 +1137,7 @@ algorithm
11371137
json := JSON.addPair("name", JSON.makeString(InstNode.name(node)), json);
11381138
json := dumpJSONSCodeMod(elem.modifications, json);
11391139
json := JSON.addPair("condition", JSON.makeBoolean(false), json);
1140-
json := JSON.addPairNotNull("prefixes", dumpJSONAttributes(elem.attributes, elem.prefixes), json);
1140+
json := JSON.addPairNotNull("prefixes", dumpJSONAttributes(elem.attributes, elem.prefixes, node), json);
11411141
json := dumpJSONCommentOpt(SOME(elem.comment), InstNode.parent(node), json);
11421142
then
11431143
();
@@ -1168,7 +1168,7 @@ algorithm
11681168
json := JSON.addPair("condition", dumpJSONBinding(comp.condition), json);
11691169
end if;
11701170

1171-
json := JSON.addPairNotNull("prefixes", dumpJSONAttributes(elem.attributes, elem.prefixes), json);
1171+
json := JSON.addPairNotNull("prefixes", dumpJSONAttributes(elem.attributes, elem.prefixes, node), json);
11721172
json := dumpJSONCommentOpt(comp.comment, InstNode.parent(node), json);
11731173
then
11741174
();
@@ -1346,11 +1346,12 @@ end dumpJSONDims;
13461346
function dumpJSONAttributes
13471347
input SCode.Attributes attrs;
13481348
input SCode.Prefixes prefs;
1349+
input InstNode scope;
13491350
output JSON json;
13501351
protected
13511352
String s;
13521353
algorithm
1353-
json := dumpJSONSCodePrefixes(prefs);
1354+
json := dumpJSONSCodePrefixes(prefs, scope);
13541355

13551356
s := SCodeDump.connectorTypeStr(attrs.connectorType);
13561357
if not stringEmpty(s) then
@@ -1371,6 +1372,7 @@ end dumpJSONAttributes;
13711372

13721373
function dumpJSONSCodePrefixes
13731374
input SCode.Prefixes prefixes;
1375+
input InstNode scope;
13741376
output JSON json = JSON.makeNull();
13751377
algorithm
13761378
if not SCodeUtil.visibilityBool(prefixes.visibility) then
@@ -1389,9 +1391,8 @@ algorithm
13891391
json := JSON.addPair("outer", JSON.makeBoolean(true), json);
13901392
end if;
13911393

1392-
if SCodeUtil.replaceableBool(prefixes.replaceablePrefix) then
1393-
json := JSON.addPair("replaceable", JSON.makeBoolean(true), json);
1394-
end if;
1394+
json := JSON.addPairNotNull("replaceable",
1395+
dumpJSONReplaceable(prefixes.replaceablePrefix, scope), json);
13951396

13961397
if SCodeUtil.redeclareBool(prefixes.redeclarePrefix) then
13971398
json := JSON.addPair("redeclare", JSON.makeBoolean(true), json);
@@ -1400,6 +1401,7 @@ end dumpJSONSCodePrefixes;
14001401

14011402
function dumpJSONClassPrefixes
14021403
input SCode.Element element;
1404+
input InstNode scope;
14031405
output JSON json;
14041406
protected
14051407
SCode.Prefixes prefs;
@@ -1409,8 +1411,8 @@ algorithm
14091411
case SCode.CLASS(classDef = cdef, prefixes = prefs)
14101412
algorithm
14111413
json := match cdef
1412-
case SCode.ClassDef.DERIVED() then dumpJSONAttributes(cdef.attributes, element.prefixes);
1413-
else dumpJSONSCodePrefixes(element.prefixes);
1414+
case SCode.ClassDef.DERIVED() then dumpJSONAttributes(cdef.attributes, element.prefixes, scope);
1415+
else dumpJSONSCodePrefixes(element.prefixes, scope);
14141416
end match;
14151417

14161418
if SCodeUtil.partialBool(element.partialPrefix) then
@@ -1427,6 +1429,28 @@ algorithm
14271429
end match;
14281430
end dumpJSONClassPrefixes;
14291431

1432+
function dumpJSONReplaceable
1433+
input SCode.Replaceable repl;
1434+
input InstNode scope;
1435+
output JSON json;
1436+
protected
1437+
SCode.ConstrainClass cc;
1438+
algorithm
1439+
json := match repl
1440+
case SCode.Replaceable.REPLACEABLE(cc = SOME(cc))
1441+
algorithm
1442+
json := JSON.emptyObject();
1443+
json := JSON.addPair("constrainedby", dumpJSONPath(cc.constrainingClass), json);
1444+
json := dumpJSONSCodeMod(cc.modifier, json);
1445+
json := dumpJSONCommentOpt(SOME(cc.comment), scope, json);
1446+
then
1447+
json;
1448+
1449+
case SCode.Replaceable.REPLACEABLE() then JSON.makeBoolean(true);
1450+
else JSON.makeNull();
1451+
end match;
1452+
end dumpJSONReplaceable;
1453+
14301454
function dumpJSONCommentOpt
14311455
input Option<SCode.Comment> cmtOpt;
14321456
input InstNode scope;

OMEdit/OMEditLIB/Modeling/Model.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,14 @@ namespace ModelInstance
651651
}
652652

653653
if (prefixes.contains("replaceable")) {
654-
mReplaceable = prefixes.value("replaceable").toBool();
654+
auto replaceable = prefixes.value("replaceble");
655+
656+
if (replaceable.isObject()) {
657+
mReplaceable = true;
658+
// constrainedby stuff goes here
659+
} else {
660+
mReplaceable = replaceable.toBool();
661+
}
655662
}
656663

657664
if (prefixes.contains("redeclare")) {
@@ -1254,7 +1261,14 @@ namespace ModelInstance
12541261
}
12551262

12561263
if (prefixes.contains("replaceable")) {
1257-
mReplaceable = prefixes.value("replaceable").toBool();
1264+
auto replaceable = prefixes.value("replaceble");
1265+
1266+
if (replaceable.isObject()) {
1267+
mReplaceable = true;
1268+
// constrainedby stuff goes here
1269+
} else {
1270+
mReplaceable = replaceable.toBool();
1271+
}
12581272
}
12591273

12601274
if (prefixes.contains("redeclare")) {

doc/instanceAPI/getModelInstance.schema.json

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,35 @@
3333
"type": "boolean"
3434
},
3535
"replaceable": {
36-
"type": "boolean"
36+
"oneOf": [
37+
{
38+
"description": "A replaceable prefix without a constrainedby clause",
39+
"type": "boolean"
40+
},
41+
{
42+
"description": "A replaceable prefix with a constrainedby clause",
43+
"type": "object",
44+
"properties": {
45+
"constrainedby": {
46+
"description": "The name of the constraining class",
47+
"type": "string"
48+
},
49+
"modifiers": {
50+
"description": "The modifiers on the constrainedby clause",
51+
"type": "#/definitions/scodeModifier"
52+
},
53+
"comment": {
54+
"description": "The comment on the constrainedby clause",
55+
"$ref": "#/definitions/comment"
56+
},
57+
"annotation": {
58+
"description": "The annotation on the constrainedby clause",
59+
"$ref": "#/definitions/annotation"
60+
}
61+
},
62+
"required": ["constrainedby"]
63+
}
64+
]
3765
},
3866
"redeclare": {
3967
"type": "boolean"

testsuite/openmodelica/instance-API/GetModelInstanceReplaceable1.mos

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// name: GetModelInstanceAttributes1
1+
// name: GetModelInstanceReplaceable1
22
// keywords:
33
// status: correct
44
// cflags: -d=newInst
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// name: GetModelInstanceReplaceable2
2+
// keywords:
3+
// status: correct
4+
// cflags: -d=newInst
5+
//
6+
//
7+
8+
loadString("
9+
model M
10+
replaceable Real x \"definition comment\" constrainedby Real(start = 1);
11+
replaceable Real y constrainedby Real(start = 2) \"constrainedby comment\";
12+
replaceable Real z \"definition comment\" constrainedby Real \"constrainedby comment\" annotation(choicesAllMatching = true);
13+
end M;
14+
");
15+
16+
getModelInstance(M, prettyPrint = true);
17+
18+
// Result:
19+
// true
20+
// "{
21+
// \"name\": \"M\",
22+
// \"restriction\": \"model\",
23+
// \"components\": [
24+
// {
25+
// \"name\": \"x\",
26+
// \"type\": \"Real\",
27+
// \"prefixes\": {
28+
// \"replaceable\": {
29+
// \"constrainedby\": \"Real\",
30+
// \"modifiers\": {
31+
// \"start\": \"1\"
32+
// }
33+
// }
34+
// },
35+
// \"comment\": \"definition comment\"
36+
// },
37+
// {
38+
// \"name\": \"y\",
39+
// \"type\": \"Real\",
40+
// \"prefixes\": {
41+
// \"replaceable\": {
42+
// \"constrainedby\": \"Real\",
43+
// \"modifiers\": {
44+
// \"start\": \"2\"
45+
// },
46+
// \"comment\": \"constrainedby comment\"
47+
// }
48+
// }
49+
// },
50+
// {
51+
// \"name\": \"z\",
52+
// \"type\": \"Real\",
53+
// \"prefixes\": {
54+
// \"replaceable\": {
55+
// \"constrainedby\": \"Real\",
56+
// \"comment\": \"constrainedby comment\",
57+
// \"annotation\": {
58+
// \"choicesAllMatching\": true
59+
// }
60+
// }
61+
// },
62+
// \"comment\": \"definition comment\"
63+
// }
64+
// ],
65+
// \"replaceable\": [
66+
// {
67+
// \"name\": \"x\",
68+
// \"type\": \"Real\"
69+
// },
70+
// {
71+
// \"name\": \"y\",
72+
// \"type\": \"Real\"
73+
// },
74+
// {
75+
// \"name\": \"z\",
76+
// \"type\": \"Real\"
77+
// }
78+
// ],
79+
// \"source\": {
80+
// \"filename\": \"<interactive>\",
81+
// \"lineStart\": 2,
82+
// \"columnStart\": 3,
83+
// \"lineEnd\": 6,
84+
// \"columnEnd\": 8
85+
// }
86+
// }"
87+
// endResult

testsuite/openmodelica/instance-API/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ GetModelInstanceInnerOuter3.mos \
3030
GetModelInstanceMod1.mos \
3131
GetModelInstanceMod2.mos \
3232
GetModelInstanceReplaceable1.mos \
33+
GetModelInstanceReplaceable2.mos \
3334
GetModelInstanceStateMachine1.mos \
3435

3536

0 commit comments

Comments
 (0)