Skip to content

Commit e2bb85a

Browse files
authored
Improve SCode modifiers in getModelInstance (#9187)
- Use `$value` for binding expressions in SCode modifiers to allow mixing it with submodifiers, and change the structure to be more compact.
1 parent d2b32cd commit e2bb85a

File tree

8 files changed

+274
-60
lines changed

8 files changed

+274
-60
lines changed

OMCompiler/Compiler/Script/NFApi.mo

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ algorithm
937937

938938
json := JSON.addPair("restriction",
939939
JSON.makeString(Restriction.toString(InstNode.restriction(node))), json);
940-
json := dumpJSONMod(SCodeUtil.elementMod(def), json);
940+
json := dumpJSONSCodeMod(SCodeUtil.elementMod(def), json);
941941

942942
if not listEmpty(exts) then
943943
json := JSON.addPair("extends", dumpJSONExtends(exts), json);
@@ -986,7 +986,7 @@ algorithm
986986
ext_def := InstNode.extendsDefinition(node);
987987

988988
ext_json := JSON.emptyObject();
989-
ext_json := dumpJSONMod(SCodeUtil.elementMod(ext_def), ext_json);
989+
ext_json := dumpJSONSCodeMod(SCodeUtil.elementMod(ext_def), ext_json);
990990
ext_json := dumpJSONCommentOpt(SCodeUtil.getElementComment(ext_def), node, ext_json);
991991
ext_json := JSON.addPair("baseClass", dumpJSONInstanceTree(ext, root = false), ext_json);
992992

@@ -1042,10 +1042,7 @@ algorithm
10421042
dumpJSONDims(elem.attributes.arrayDims, Type.arrayDims(comp.ty)), json);
10431043
end if;
10441044

1045-
j := dumpJSONMod(elem.modifications, JSON.makeNull());
1046-
if not JSON.isNull(j) then
1047-
json := JSON.addPair("modifiers", j, json);
1048-
end if;
1045+
json := dumpJSONSCodeMod(elem.modifications, json);
10491046

10501047
//if not Type.isComplex(comp.ty) then
10511048
// json := dumpJSONBuiltinClassComponents(comp.classInst, elem.modifications, json);
@@ -1461,35 +1458,47 @@ algorithm
14611458
end for;
14621459
end dumpJSONReplaceableElements;
14631460

1464-
function dumpJSONMod
1461+
function dumpJSONSCodeMod
14651462
input SCode.Mod mod;
14661463
input output JSON json;
14671464
protected
14681465
JSON j;
1466+
algorithm
1467+
j := dumpJSONSCodeMod_impl(mod);
1468+
1469+
if not JSON.isNull(j) then
1470+
json := JSON.addPair("modifiers", j, json);
1471+
end if;
1472+
end dumpJSONSCodeMod;
1473+
1474+
function dumpJSONSCodeMod_impl
1475+
input SCode.Mod mod;
1476+
output JSON json = JSON.makeNull();
1477+
protected
1478+
JSON binding_json;
14691479
algorithm
14701480
() := match mod
14711481
case SCode.Mod.MOD()
14721482
algorithm
1473-
if isSome(mod.binding) then
1474-
json := JSON.addPair("value",
1475-
JSON.makeString(Dump.printExpStr(Util.getOption(mod.binding))), json);
1476-
end if;
1477-
1478-
if not listEmpty(mod.subModLst) then
1479-
j := JSON.emptyObject();
1483+
for m in mod.subModLst loop
1484+
json := JSON.addPair(m.ident, dumpJSONSCodeMod_impl(m.mod), json);
1485+
end for;
14801486

1481-
for sm in mod.subModLst loop
1482-
j := JSON.addPair(sm.ident, dumpJSONMod(sm.mod, JSON.makeNull()), j);
1483-
end for;
1487+
if isSome(mod.binding) then
1488+
binding_json := JSON.makeString(Dump.printExpStr(Util.getOption(mod.binding)));
14841489

1485-
json := JSON.addPair("modifiers", j, json);
1490+
if JSON.isNull(json) then
1491+
json := binding_json;
1492+
else
1493+
json := JSON.addPair("$value", binding_json, json);
1494+
end if;
14861495
end if;
14871496
then
14881497
();
14891498

14901499
else ();
14911500
end match;
1492-
end dumpJSONMod;
1501+
end dumpJSONSCodeMod_impl;
14931502

14941503
annotation(__OpenModelica_Interface="backend");
14951504
end NFApi;

doc/instanceAPI/getModelInstance.schema.json

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
"description": "The kind of class",
1313
"type": "string"
1414
},
15-
"modifier": {
15+
"modifiers": {
1616
"description": "Modifier from the SCode",
17-
"type": "#/definitions/modifier"
17+
"type": "#/definitions/scodeModifier"
1818
},
1919
"comment": {
2020
"$ref": "#/definitions/comment"
@@ -125,7 +125,7 @@
125125
"properties": {
126126
"modifiers": {
127127
"description": "SCode modifier on the extends clause",
128-
"type": "#/definitions/modifier"
128+
"type": "#/definitions/scodeModifier"
129129
},
130130
"annotation": {
131131
"description": "Annotation on the extends clause",
@@ -187,7 +187,7 @@
187187
},
188188
"modifiers": {
189189
"description": "Modifier from the SCode",
190-
"type": "#/definitions/modifier"
190+
"type": "#/definitions/scodeModifier"
191191
},
192192
"value": {
193193
"description": "The component's binding equation",
@@ -261,21 +261,29 @@
261261
},
262262
"required": ["binding"]
263263
},
264-
"modifier": {
265-
"description": "A modifier",
266-
"type": "object",
267-
"properties": {
268-
"value": {
269-
"type": "string"
264+
"scodeModifier": {
265+
"description": "An SCode modifier",
266+
"oneOf": [
267+
{
268+
"type": "string",
269+
"description": "The string representation of the binding equation"
270270
},
271-
"modifiers": {
272-
"type": "array",
273-
"items": {
274-
"type": "#/definitions/modifier"
275-
}
271+
{
272+
"type": "object",
273+
"description": "A modifier with submodifiers",
274+
"properties": {
275+
"$value": {
276+
"type": "string",
277+
"description": "The string representation of the binding equation"
278+
}
279+
},
280+
"additionalProperties": {
281+
"$ref": "#/definitions/scodeModifier"
282+
},
283+
"required": [],
284+
"minProperties": 1
276285
}
277-
},
278-
"required": []
286+
]
279287
}
280288
}
281289
}

testsuite/openmodelica/instance-API/GetModelInstanceAttributes1.mos

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ getModelInstance(M, prettyPrint = true);
2424
// {
2525
// \"name\": \"pi\",
2626
// \"type\": \"Real\",
27-
// \"modifiers\": {
28-
// \"value\": \"3\"
29-
// },
27+
// \"modifiers\": \"3\",
3028
// \"value\": {
3129
// \"binding\": 3
3230
// },
@@ -38,12 +36,8 @@ getModelInstance(M, prettyPrint = true);
3836
// \"name\": \"deg\",
3937
// \"type\": \"Real\",
4038
// \"modifiers\": {
41-
// \"value\": \"2\",
42-
// \"modifiers\": {
43-
// \"start\": {
44-
// \"value\": \"pi\"
45-
// }
46-
// }
39+
// \"start\": \"pi\",
40+
// \"$value\": \"2\"
4741
// },
4842
// \"value\": {
4943
// \"binding\": 2

testsuite/openmodelica/instance-API/GetModelInstanceExp1.mos

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ getModelInstance(M, prettyPrint = true);
2828
// {
2929
// \"name\": \"x\",
3030
// \"type\": \"Real\",
31-
// \"modifiers\": {
32-
// \"value\": \"2.0\"
33-
// },
31+
// \"modifiers\": \"2.0\",
3432
// \"value\": {
3533
// \"binding\": 2
3634
// },
@@ -55,9 +53,7 @@ getModelInstance(M, prettyPrint = true);
5553
// \"3\"
5654
// ]
5755
// },
58-
// \"modifiers\": {
59-
// \"value\": \"{1, 2, 3}\"
60-
// },
56+
// \"modifiers\": \"{1, 2, 3}\",
6157
// \"value\": {
6258
// \"binding\": [
6359
// 1,
@@ -78,9 +74,7 @@ getModelInstance(M, prettyPrint = true);
7874
// {
7975
// \"name\": \"y\",
8076
// \"type\": \"Real\",
81-
// \"modifiers\": {
82-
// \"value\": \"a.x[1]\"
83-
// },
77+
// \"modifiers\": \"a.x[1]\",
8478
// \"value\": {
8579
// \"binding\": {
8680
// \"$kind\": \"cref\",

testsuite/openmodelica/instance-API/GetModelInstanceExtends1.mos

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ getModelInstance(M, prettyPrint=true);
2929
// \"extends\": [
3030
// {
3131
// \"modifiers\": {
32-
// \"x\": {
33-
// \"value\": \"1\"
34-
// }
32+
// \"x\": \"1\"
3533
// },
3634
// \"annotation\": {
3735
// \"IconMap\": {

0 commit comments

Comments
 (0)