Skip to content

Commit 67c6a5d

Browse files
authored
Dump connections inside if-equations for getModelInstance (#11210)
Fixes #11202
1 parent 3825633 commit 67c6a5d

File tree

5 files changed

+192
-39
lines changed

5 files changed

+192
-39
lines changed

OMCompiler/Compiler/NFFrontEnd/NFInst.mo

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3073,6 +3073,13 @@ algorithm
30733073
guard name == "transition" or name == "initialState"
30743074
then eq :: outEql;
30753075

3076+
case SCode.Equation.EQ_IF()
3077+
algorithm
3078+
eq.thenBranch := list(filterInstanceAPIEquations(eql) for eql in eq.thenBranch);
3079+
eq.elseBranch := filterInstanceAPIEquations(eq.elseBranch);
3080+
then
3081+
if List.all(eq.thenBranch, listEmpty) and listEmpty(eq.elseBranch) then outEql else eq :: outEql;
3082+
30763083
else outEql;
30773084
end match;
30783085
end for;

OMCompiler/Compiler/NFFrontEnd/NFSections.mo

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ public
7777
end if;
7878
end new;
7979

80+
function equations
81+
input Sections sections;
82+
output list<Equation> equations;
83+
algorithm
84+
equations := match sections
85+
case SECTIONS() then sections.equations;
86+
else {};
87+
end match;
88+
end equations;
89+
8090
function prepend
8191
input list<Equation> equations;
8292
input list<Equation> initialEquations;

OMCompiler/Compiler/Script/NFApi.mo

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,7 @@ protected
19101910
JSON j;
19111911
InstContext.Type context;
19121912
algorithm
1913-
(connections, transitions, initial_states) := sortEquations(sections);
1913+
(connections, transitions, initial_states) := sortEquations(Sections.equations(sections));
19141914
context := InstContext.set(NFInstContext.CLASS, NFInstContext.RELAXED);
19151915
transitions := list(Typing.typeEquation(e, context) for e in transitions);
19161916
initial_states := list(Typing.typeEquation(e, context) for e in initial_states);
@@ -1926,47 +1926,49 @@ algorithm
19261926
end dumpJSONEquations;
19271927

19281928
function sortEquations
1929-
input Sections sections;
1930-
output list<Equation> connections = {};
1931-
output list<Equation> transitions = {};
1932-
output list<Equation> initialStates = {};
1933-
output list<Equation> others = {};
1929+
input list<Equation> equations;
1930+
input output list<Equation> connections = {};
1931+
input output list<Equation> transitions = {};
1932+
input output list<Equation> initialStates = {};
19341933
algorithm
1935-
() := match sections
1936-
case Sections.SECTIONS()
1937-
algorithm
1938-
for eq in listReverse(sections.equations) loop
1939-
() := match eq
1940-
case Equation.CONNECT()
1941-
algorithm
1942-
connections := eq :: connections;
1943-
then
1944-
();
1945-
1946-
case Equation.NORETCALL()
1947-
algorithm
1948-
if Expression.isCallNamed(eq.exp, "transition") then
1949-
transitions := eq :: transitions;
1950-
elseif Expression.isCallNamed(eq.exp, "initialState") then
1951-
initialStates := eq :: initialStates;
1952-
else
1953-
others := eq :: others;
1954-
end if;
1955-
then
1956-
();
1934+
for eq in listReverse(equations) loop
1935+
() := match eq
1936+
case Equation.CONNECT()
1937+
algorithm
1938+
connections := eq :: connections;
1939+
then
1940+
();
19571941

1958-
else
1959-
algorithm
1960-
others := eq :: others;
1961-
then
1962-
();
1963-
end match;
1964-
end for;
1965-
then
1966-
();
1942+
case Equation.IF()
1943+
algorithm
1944+
for b in eq.branches loop
1945+
() := match b
1946+
case Equation.Branch.BRANCH()
1947+
algorithm
1948+
(connections, transitions, initialStates) :=
1949+
sortEquations(b.body, connections, transitions, initialStates);
1950+
then
1951+
();
1952+
1953+
else ();
1954+
end match;
1955+
end for;
1956+
then
1957+
();
19671958

1968-
else ();
1969-
end match;
1959+
case Equation.NORETCALL()
1960+
algorithm
1961+
if Expression.isCallNamed(eq.exp, "transition") then
1962+
transitions := eq :: transitions;
1963+
elseif Expression.isCallNamed(eq.exp, "initialState") then
1964+
initialStates := eq :: initialStates;
1965+
end if;
1966+
then
1967+
();
1968+
1969+
else ();
1970+
end match;
1971+
end for;
19701972
end sortEquations;
19711973

19721974
function dumpJSONConnections
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// name: GetModelInstanceConnection4
2+
// keywords:
3+
// status: correct
4+
// cflags: -d=newInst
5+
//
6+
//
7+
8+
loadString("
9+
model M
10+
connector C
11+
Real e;
12+
flow Real f;
13+
end C;
14+
15+
parameter Boolean b=true;
16+
C c1, c2;
17+
equation
18+
if b then
19+
connect(c1, c2);
20+
end if;
21+
end M;
22+
");
23+
24+
getModelInstance(M, prettyPrint = true);
25+
26+
// Result:
27+
// true
28+
// "{
29+
// \"name\": \"M\",
30+
// \"restriction\": \"model\",
31+
// \"elements\": [
32+
// {
33+
// \"$kind\": \"component\",
34+
// \"name\": \"b\",
35+
// \"type\": \"Boolean\",
36+
// \"modifiers\": \"true\",
37+
// \"value\": {
38+
// \"binding\": true
39+
// },
40+
// \"prefixes\": {
41+
// \"variability\": \"parameter\"
42+
// }
43+
// },
44+
// {
45+
// \"$kind\": \"component\",
46+
// \"name\": \"c1\",
47+
// \"type\": {
48+
// \"name\": \"C\",
49+
// \"restriction\": \"connector\",
50+
// \"elements\": [
51+
// {
52+
// \"$kind\": \"component\",
53+
// \"name\": \"e\",
54+
// \"type\": \"Real\"
55+
// },
56+
// {
57+
// \"$kind\": \"component\",
58+
// \"name\": \"f\",
59+
// \"type\": \"Real\",
60+
// \"prefixes\": {
61+
// \"connector\": \"flow\"
62+
// }
63+
// }
64+
// ],
65+
// \"source\": {
66+
// \"filename\": \"<interactive>\",
67+
// \"lineStart\": 3,
68+
// \"columnStart\": 3,
69+
// \"lineEnd\": 6,
70+
// \"columnEnd\": 8
71+
// }
72+
// }
73+
// },
74+
// {
75+
// \"$kind\": \"component\",
76+
// \"name\": \"c2\",
77+
// \"type\": {
78+
// \"name\": \"C\",
79+
// \"restriction\": \"connector\",
80+
// \"elements\": [
81+
// {
82+
// \"$kind\": \"component\",
83+
// \"name\": \"e\",
84+
// \"type\": \"Real\"
85+
// },
86+
// {
87+
// \"$kind\": \"component\",
88+
// \"name\": \"f\",
89+
// \"type\": \"Real\",
90+
// \"prefixes\": {
91+
// \"connector\": \"flow\"
92+
// }
93+
// }
94+
// ],
95+
// \"source\": {
96+
// \"filename\": \"<interactive>\",
97+
// \"lineStart\": 3,
98+
// \"columnStart\": 3,
99+
// \"lineEnd\": 6,
100+
// \"columnEnd\": 8
101+
// }
102+
// }
103+
// }
104+
// ],
105+
// \"connections\": [
106+
// {
107+
// \"lhs\": {
108+
// \"$kind\": \"cref\",
109+
// \"parts\": [
110+
// {
111+
// \"name\": \"c1\"
112+
// }
113+
// ]
114+
// },
115+
// \"rhs\": {
116+
// \"$kind\": \"cref\",
117+
// \"parts\": [
118+
// {
119+
// \"name\": \"c2\"
120+
// }
121+
// ]
122+
// }
123+
// }
124+
// ],
125+
// \"source\": {
126+
// \"filename\": \"<interactive>\",
127+
// \"lineStart\": 2,
128+
// \"columnStart\": 1,
129+
// \"lineEnd\": 14,
130+
// \"columnEnd\": 6
131+
// }
132+
// }"
133+
// endResult

testsuite/openmodelica/instance-API/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ GetModelInstanceConditional2.mos \
2828
GetModelInstanceConnection1.mos \
2929
GetModelInstanceConnection2.mos \
3030
GetModelInstanceConnection3.mos \
31+
GetModelInstanceConnection4.mos \
3132
GetModelInstanceDerived1.mos \
3233
GetModelInstanceDerived2.mos \
3334
GetModelInstanceDerived3.mos \

0 commit comments

Comments
 (0)