From 4b69a3ec456b2f1a3a5d8aca7fdd70e2f9e41350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=96stlund?= Date: Thu, 5 Oct 2023 14:38:12 +0200 Subject: [PATCH] Add replaceable for modifiers in getModelInstance (#11337) --- OMCompiler/Compiler/Script/NFApi.mo | 4 ++ doc/instanceAPI/getModelInstance.schema.json | 4 ++ .../GetModelInstanceReplaceable5.mos | 72 +++++++++++++++++++ testsuite/openmodelica/instance-API/Makefile | 1 + 4 files changed, 81 insertions(+) create mode 100644 testsuite/openmodelica/instance-API/GetModelInstanceReplaceable5.mos diff --git a/OMCompiler/Compiler/Script/NFApi.mo b/OMCompiler/Compiler/Script/NFApi.mo index 687084b45bd..d19d8c8a011 100644 --- a/OMCompiler/Compiler/Script/NFApi.mo +++ b/OMCompiler/Compiler/Script/NFApi.mo @@ -2120,6 +2120,10 @@ algorithm json := JSON.addPair("redeclare", JSON.makeBoolean(true), json); + if SCodeUtil.isElementReplaceable(mod.element) then + json := JSON.addPair("replaceable", JSON.makeBoolean(true), json); + end if; + binding_json := JSON.makeString(SCodeDump.unparseElementStr(mod.element)); json := JSON.addPair("$value", binding_json, json); diff --git a/doc/instanceAPI/getModelInstance.schema.json b/doc/instanceAPI/getModelInstance.schema.json index 73218a5f4f2..cd3c08a4bdb 100644 --- a/doc/instanceAPI/getModelInstance.schema.json +++ b/doc/instanceAPI/getModelInstance.schema.json @@ -454,6 +454,10 @@ "redeclare": { "type": "boolean", "description": "Whether the modifier is a redeclare or not" + }, + "replaceable": { + "type": "boolean", + "description": "Whether the modifier is a replaceable or not" } }, "additionalProperties": { diff --git a/testsuite/openmodelica/instance-API/GetModelInstanceReplaceable5.mos b/testsuite/openmodelica/instance-API/GetModelInstanceReplaceable5.mos new file mode 100644 index 00000000000..64b0b18f1ea --- /dev/null +++ b/testsuite/openmodelica/instance-API/GetModelInstanceReplaceable5.mos @@ -0,0 +1,72 @@ +// name: GetModelInstanceReplaceable5 +// keywords: +// status: correct +// cflags: -d=newInst +// +// + +loadString(" + package P + model A + replaceable Real x; + end A; + + model M + A a(redeclare replaceable Real x = 1.0); + end M; + end P; +"); + +getModelInstance(P.M, prettyPrint = true); + +// Result: +// true +// "{ +// \"name\": \"P.M\", +// \"restriction\": \"model\", +// \"elements\": [ +// { +// \"$kind\": \"component\", +// \"name\": \"a\", +// \"type\": { +// \"name\": \"P.A\", +// \"restriction\": \"model\", +// \"elements\": [ +// { +// \"$kind\": \"component\", +// \"name\": \"x\", +// \"type\": \"Real\", +// \"value\": { +// \"binding\": 1 +// }, +// \"prefixes\": { +// \"replaceable\": true +// } +// } +// ], +// \"source\": { +// \"filename\": \"\", +// \"lineStart\": 3, +// \"columnStart\": 5, +// \"lineEnd\": 5, +// \"columnEnd\": 10 +// } +// }, +// \"modifiers\": { +// \"x\": { +// \"redeclare\": true, +// \"replaceable\": true, +// \"$value\": \"redeclare replaceable Real x = 1.0\" +// } +// } +// } +// ], +// \"source\": { +// \"filename\": \"\", +// \"lineStart\": 7, +// \"columnStart\": 5, +// \"lineEnd\": 9, +// \"columnEnd\": 10 +// } +// }" +// endResult diff --git a/testsuite/openmodelica/instance-API/Makefile b/testsuite/openmodelica/instance-API/Makefile index 078b70a1f6a..584e60ad74e 100644 --- a/testsuite/openmodelica/instance-API/Makefile +++ b/testsuite/openmodelica/instance-API/Makefile @@ -63,6 +63,7 @@ GetModelInstanceReplaceable1.mos \ GetModelInstanceReplaceable2.mos \ GetModelInstanceReplaceable3.mos \ GetModelInstanceReplaceable4.mos \ +GetModelInstanceReplaceable5.mos \ GetModelInstanceReplaceableComment.mos \ GetModelInstanceStateMachine1.mos \ ModifierToJSON1.mos \