From 67c6a5d2f9c28ac0c628a075cb20b9a6f0788b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=96stlund?= Date: Mon, 18 Sep 2023 14:30:05 +0200 Subject: [PATCH] Dump connections inside if-equations for getModelInstance (#11210) Fixes #11202 --- OMCompiler/Compiler/NFFrontEnd/NFInst.mo | 7 + OMCompiler/Compiler/NFFrontEnd/NFSections.mo | 10 ++ OMCompiler/Compiler/Script/NFApi.mo | 80 ++++++----- .../GetModelInstanceConnection4.mos | 133 ++++++++++++++++++ testsuite/openmodelica/instance-API/Makefile | 1 + 5 files changed, 192 insertions(+), 39 deletions(-) create mode 100644 testsuite/openmodelica/instance-API/GetModelInstanceConnection4.mos diff --git a/OMCompiler/Compiler/NFFrontEnd/NFInst.mo b/OMCompiler/Compiler/NFFrontEnd/NFInst.mo index 7910ec7ee29..d1208a592b2 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFInst.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFInst.mo @@ -3073,6 +3073,13 @@ algorithm guard name == "transition" or name == "initialState" then eq :: outEql; + case SCode.Equation.EQ_IF() + algorithm + eq.thenBranch := list(filterInstanceAPIEquations(eql) for eql in eq.thenBranch); + eq.elseBranch := filterInstanceAPIEquations(eq.elseBranch); + then + if List.all(eq.thenBranch, listEmpty) and listEmpty(eq.elseBranch) then outEql else eq :: outEql; + else outEql; end match; end for; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFSections.mo b/OMCompiler/Compiler/NFFrontEnd/NFSections.mo index 0058a052235..e1da61dcbbe 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFSections.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFSections.mo @@ -77,6 +77,16 @@ public end if; end new; + function equations + input Sections sections; + output list equations; + algorithm + equations := match sections + case SECTIONS() then sections.equations; + else {}; + end match; + end equations; + function prepend input list equations; input list initialEquations; diff --git a/OMCompiler/Compiler/Script/NFApi.mo b/OMCompiler/Compiler/Script/NFApi.mo index f475f51c4b2..755c9a18ea2 100644 --- a/OMCompiler/Compiler/Script/NFApi.mo +++ b/OMCompiler/Compiler/Script/NFApi.mo @@ -1910,7 +1910,7 @@ protected JSON j; InstContext.Type context; algorithm - (connections, transitions, initial_states) := sortEquations(sections); + (connections, transitions, initial_states) := sortEquations(Sections.equations(sections)); context := InstContext.set(NFInstContext.CLASS, NFInstContext.RELAXED); transitions := list(Typing.typeEquation(e, context) for e in transitions); initial_states := list(Typing.typeEquation(e, context) for e in initial_states); @@ -1926,47 +1926,49 @@ algorithm end dumpJSONEquations; function sortEquations - input Sections sections; - output list connections = {}; - output list transitions = {}; - output list initialStates = {}; - output list others = {}; + input list equations; + input output list connections = {}; + input output list transitions = {}; + input output list initialStates = {}; algorithm - () := match sections - case Sections.SECTIONS() - algorithm - for eq in listReverse(sections.equations) loop - () := match eq - case Equation.CONNECT() - algorithm - connections := eq :: connections; - then - (); - - case Equation.NORETCALL() - algorithm - if Expression.isCallNamed(eq.exp, "transition") then - transitions := eq :: transitions; - elseif Expression.isCallNamed(eq.exp, "initialState") then - initialStates := eq :: initialStates; - else - others := eq :: others; - end if; - then - (); + for eq in listReverse(equations) loop + () := match eq + case Equation.CONNECT() + algorithm + connections := eq :: connections; + then + (); - else - algorithm - others := eq :: others; - then - (); - end match; - end for; - then - (); + case Equation.IF() + algorithm + for b in eq.branches loop + () := match b + case Equation.Branch.BRANCH() + algorithm + (connections, transitions, initialStates) := + sortEquations(b.body, connections, transitions, initialStates); + then + (); + + else (); + end match; + end for; + then + (); - else (); - end match; + case Equation.NORETCALL() + algorithm + if Expression.isCallNamed(eq.exp, "transition") then + transitions := eq :: transitions; + elseif Expression.isCallNamed(eq.exp, "initialState") then + initialStates := eq :: initialStates; + end if; + then + (); + + else (); + end match; + end for; end sortEquations; function dumpJSONConnections diff --git a/testsuite/openmodelica/instance-API/GetModelInstanceConnection4.mos b/testsuite/openmodelica/instance-API/GetModelInstanceConnection4.mos new file mode 100644 index 00000000000..278e95c0e65 --- /dev/null +++ b/testsuite/openmodelica/instance-API/GetModelInstanceConnection4.mos @@ -0,0 +1,133 @@ +// name: GetModelInstanceConnection4 +// keywords: +// status: correct +// cflags: -d=newInst +// +// + +loadString(" +model M + connector C + Real e; + flow Real f; + end C; + + parameter Boolean b=true; + C c1, c2; +equation + if b then + connect(c1, c2); + end if; +end M; +"); + +getModelInstance(M, prettyPrint = true); + +// Result: +// true +// "{ +// \"name\": \"M\", +// \"restriction\": \"model\", +// \"elements\": [ +// { +// \"$kind\": \"component\", +// \"name\": \"b\", +// \"type\": \"Boolean\", +// \"modifiers\": \"true\", +// \"value\": { +// \"binding\": true +// }, +// \"prefixes\": { +// \"variability\": \"parameter\" +// } +// }, +// { +// \"$kind\": \"component\", +// \"name\": \"c1\", +// \"type\": { +// \"name\": \"C\", +// \"restriction\": \"connector\", +// \"elements\": [ +// { +// \"$kind\": \"component\", +// \"name\": \"e\", +// \"type\": \"Real\" +// }, +// { +// \"$kind\": \"component\", +// \"name\": \"f\", +// \"type\": \"Real\", +// \"prefixes\": { +// \"connector\": \"flow\" +// } +// } +// ], +// \"source\": { +// \"filename\": \"\", +// \"lineStart\": 3, +// \"columnStart\": 3, +// \"lineEnd\": 6, +// \"columnEnd\": 8 +// } +// } +// }, +// { +// \"$kind\": \"component\", +// \"name\": \"c2\", +// \"type\": { +// \"name\": \"C\", +// \"restriction\": \"connector\", +// \"elements\": [ +// { +// \"$kind\": \"component\", +// \"name\": \"e\", +// \"type\": \"Real\" +// }, +// { +// \"$kind\": \"component\", +// \"name\": \"f\", +// \"type\": \"Real\", +// \"prefixes\": { +// \"connector\": \"flow\" +// } +// } +// ], +// \"source\": { +// \"filename\": \"\", +// \"lineStart\": 3, +// \"columnStart\": 3, +// \"lineEnd\": 6, +// \"columnEnd\": 8 +// } +// } +// } +// ], +// \"connections\": [ +// { +// \"lhs\": { +// \"$kind\": \"cref\", +// \"parts\": [ +// { +// \"name\": \"c1\" +// } +// ] +// }, +// \"rhs\": { +// \"$kind\": \"cref\", +// \"parts\": [ +// { +// \"name\": \"c2\" +// } +// ] +// } +// } +// ], +// \"source\": { +// \"filename\": \"\", +// \"lineStart\": 2, +// \"columnStart\": 1, +// \"lineEnd\": 14, +// \"columnEnd\": 6 +// } +// }" +// endResult diff --git a/testsuite/openmodelica/instance-API/Makefile b/testsuite/openmodelica/instance-API/Makefile index e7d41f2c95d..a0bf50dd307 100644 --- a/testsuite/openmodelica/instance-API/Makefile +++ b/testsuite/openmodelica/instance-API/Makefile @@ -28,6 +28,7 @@ GetModelInstanceConditional2.mos \ GetModelInstanceConnection1.mos \ GetModelInstanceConnection2.mos \ GetModelInstanceConnection3.mos \ +GetModelInstanceConnection4.mos \ GetModelInstanceDerived1.mos \ GetModelInstanceDerived2.mos \ GetModelInstanceDerived3.mos \