Skip to content

Commit

Permalink
Dump connections inside if-equations for getModelInstance (#11210)
Browse files Browse the repository at this point in the history
Fixes #11202
  • Loading branch information
perost committed Sep 18, 2023
1 parent 3825633 commit 67c6a5d
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 39 deletions.
7 changes: 7 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFSections.mo
Expand Up @@ -77,6 +77,16 @@ public
end if;
end new;

function equations
input Sections sections;
output list<Equation> equations;
algorithm
equations := match sections
case SECTIONS() then sections.equations;
else {};
end match;
end equations;

function prepend
input list<Equation> equations;
input list<Equation> initialEquations;
Expand Down
80 changes: 41 additions & 39 deletions OMCompiler/Compiler/Script/NFApi.mo
Expand Up @@ -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);
Expand All @@ -1926,47 +1926,49 @@ algorithm
end dumpJSONEquations;

function sortEquations
input Sections sections;
output list<Equation> connections = {};
output list<Equation> transitions = {};
output list<Equation> initialStates = {};
output list<Equation> others = {};
input list<Equation> equations;
input output list<Equation> connections = {};
input output list<Equation> transitions = {};
input output list<Equation> 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
Expand Down
133 changes: 133 additions & 0 deletions 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\": \"<interactive>\",
// \"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\": \"<interactive>\",
// \"lineStart\": 3,
// \"columnStart\": 3,
// \"lineEnd\": 6,
// \"columnEnd\": 8
// }
// }
// }
// ],
// \"connections\": [
// {
// \"lhs\": {
// \"$kind\": \"cref\",
// \"parts\": [
// {
// \"name\": \"c1\"
// }
// ]
// },
// \"rhs\": {
// \"$kind\": \"cref\",
// \"parts\": [
// {
// \"name\": \"c2\"
// }
// ]
// }
// }
// ],
// \"source\": {
// \"filename\": \"<interactive>\",
// \"lineStart\": 2,
// \"columnStart\": 1,
// \"lineEnd\": 14,
// \"columnEnd\": 6
// }
// }"
// endResult
1 change: 1 addition & 0 deletions testsuite/openmodelica/instance-API/Makefile
Expand Up @@ -28,6 +28,7 @@ GetModelInstanceConditional2.mos \
GetModelInstanceConnection1.mos \
GetModelInstanceConnection2.mos \
GetModelInstanceConnection3.mos \
GetModelInstanceConnection4.mos \
GetModelInstanceDerived1.mos \
GetModelInstanceDerived2.mos \
GetModelInstanceDerived3.mos \
Expand Down

0 comments on commit 67c6a5d

Please sign in to comment.