Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit e3fdafb

Browse files
adeas31OpenModelica-Hudson
authored andcommitted
ticket:4381
Updated the implementation of fetching connect equations. Build a list of connections and then use the list for getting a specific connection. Also moved `getConnectionCount` API to ModelicaBuiltin.mo
1 parent a8b251e commit e3fdafb

File tree

3 files changed

+62
-190
lines changed

3 files changed

+62
-190
lines changed

Compiler/FrontEnd/ModelicaBuiltin.mo

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,6 +3071,13 @@ annotation(
30713071
preferredView="text");
30723072
end removeExtendsModifiers;
30733073

3074+
function getConnectionCount "Counts the number of connect equation in a class."
3075+
input TypeName className;
3076+
output Integer count;
3077+
external "builtin";
3078+
annotation(preferredView="text");
3079+
end getConnectionCount;
3080+
30743081
function getNthConnection "Returns the Nth connection.
30753082
Example command:
30763083
getNthConnection(A) => {\"from\", \"to\", \"comment\"}"

Compiler/Script/CevalScriptBackend.mo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,15 @@ algorithm
23342334
then
23352335
(cache,Values.BOOL(b),st);
23362336

2337+
case (cache,_,"getConnectionCount",{Values.CODE(Absyn.C_TYPENAME(path))},(st as GlobalScript.SYMBOLTABLE(ast = p)),_)
2338+
equation
2339+
absynClass = Interactive.getPathedClassInProgram(path, p);
2340+
n = listLength(Interactive.getConnections(absynClass));
2341+
then
2342+
(cache,Values.INTEGER(n),st);
2343+
2344+
case (cache,_,"getConnectionCount",_,st,_) then (cache,Values.INTEGER(0),st);
2345+
23372346
case (cache,_,"getNthConnection",{Values.CODE(Absyn.C_TYPENAME(path)),Values.INTEGER(n)},(st as GlobalScript.SYMBOLTABLE(ast = p)),_)
23382347
equation
23392348
vals = Interactive.getNthConnection(Absyn.pathToCref(path), p, n);

Compiler/Script/Interactive.mo

Lines changed: 46 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,12 +1174,6 @@ algorithm
11741174
then
11751175
outResult;
11761176

1177-
case "getConnectionCount"
1178-
algorithm
1179-
{Absyn.CREF(componentRef = cr)} := args;
1180-
then
1181-
getConnectionCount(cr, p);
1182-
11831177
case "setConnectionComment"
11841178
algorithm
11851179
{Absyn.CREF(componentRef = cr),
@@ -10227,36 +10221,6 @@ algorithm
1022710221
end match;
1022810222
end getComponentitemsCondition;
1022910223

10230-
protected function getConnectionCount "
10231-
This function takes a `ComponentRef\' and a `Program\' and returns a
10232-
string containing the number of connections in the model identified by
10233-
the `ComponentRef\'.
10234-
"
10235-
input Absyn.ComponentRef inComponentRef;
10236-
input Absyn.Program inProgram;
10237-
output String outString;
10238-
algorithm
10239-
outString:=
10240-
matchcontinue (inComponentRef,inProgram)
10241-
local
10242-
Absyn.Path modelpath;
10243-
Absyn.Class cdef;
10244-
Integer numconn;
10245-
String res;
10246-
Absyn.ComponentRef model_;
10247-
Absyn.Program p;
10248-
case (model_,p)
10249-
equation
10250-
modelpath = Absyn.crefToPath(model_);
10251-
cdef = getPathedClassInProgram(modelpath, p);
10252-
numconn = countConnections(cdef);
10253-
res = intString(numconn);
10254-
then
10255-
res;
10256-
else "Error";
10257-
end matchcontinue;
10258-
end getConnectionCount;
10259-
1026010224
public function getNthConnection "
1026110225
This function takes a `ComponentRef\' and a `Program\' and an int and
1026210226
returns a comma separated string for the nth connection, e.g. \"R1.n,C.p\".
@@ -10282,7 +10246,7 @@ algorithm
1028210246
equation
1028310247
modelpath = Absyn.crefToPath(model_);
1028410248
cdef = getPathedClassInProgram(modelpath, p);
10285-
Absyn.EQUATIONITEM(equation_ = eq, comment = cmt) = getNthConnectionitemInClass(cdef, n);
10249+
Absyn.EQUATIONITEM(equation_ = eq, comment = cmt) = listGet(getConnections(cdef), n);
1028610250
str = getStringComment(cmt);
1028710251
(s1, s2) = getConnectionStr(eq);
1028810252
vals = {Values.STRING(s1), Values.STRING(s2), Values.STRING(str)};
@@ -11387,7 +11351,7 @@ algorithm
1138711351
case (modelpath,p,n)
1138811352
equation
1138911353
cdef = getPathedClassInProgram(modelpath, p);
11390-
citem = getNthConnectionitemInClass(cdef, n);
11354+
citem = listGet(getConnections(cdef), n);
1139111355
s1 = getConnectionAnnotationStr(citem, cdef, p, modelpath);
1139211356
str = stringAppendList({"{", s1, "}"});
1139311357
then
@@ -13102,116 +13066,6 @@ algorithm
1310213066
end matchcontinue;
1310313067
end createFuncargsFromElementargs;
1310413068

13105-
protected function getNthConnectionitemInClass
13106-
" This function takes a Class and an int ane returns the nth
13107-
`EquationItem\' containing a connect statement in that class."
13108-
input Absyn.Class inClass;
13109-
input Integer inInteger;
13110-
output Absyn.EquationItem outEquationItem;
13111-
algorithm
13112-
outEquationItem := match (inClass,inInteger)
13113-
local
13114-
Absyn.EquationItem eq;
13115-
list<Absyn.ClassPart> parts;
13116-
Integer n;
13117-
13118-
case (Absyn.CLASS(body = Absyn.PARTS(classParts = parts)),n)
13119-
equation
13120-
eq = getNthConnectionitemInClassparts(parts, n);
13121-
then
13122-
eq;
13123-
13124-
case (Absyn.CLASS(body = Absyn.CLASS_EXTENDS(parts = parts)),n)
13125-
equation
13126-
eq = getNthConnectionitemInClassparts(parts, n);
13127-
then
13128-
eq;
13129-
end match;
13130-
end getNthConnectionitemInClass;
13131-
13132-
protected function getNthConnectionitemInClassparts
13133-
"This function takes a `ClassPart\' list and an int and returns
13134-
the nth connections as an `EquationItem\'."
13135-
input list<Absyn.ClassPart> inAbsynClassPartLst;
13136-
input Integer inInteger;
13137-
output Absyn.EquationItem outEquationItem;
13138-
algorithm
13139-
outEquationItem := matchcontinue (inAbsynClassPartLst,inInteger)
13140-
local
13141-
Absyn.EquationItem eq;
13142-
list<Absyn.EquationItem> e;
13143-
list<Absyn.ClassPart> xs;
13144-
Integer n,c1,newn;
13145-
13146-
case ((Absyn.EQUATIONS(contents = e) :: _),n)
13147-
equation
13148-
eq = getNthConnectionitemInEquations(e, n);
13149-
then
13150-
eq;
13151-
13152-
case ((Absyn.EQUATIONS(contents = e) :: xs),n) /* The rule above failed, subtract the number of connections in the first equation section and try with the rest of the classparts */
13153-
equation
13154-
c1 = countConnectionsInEquations(e, 0);
13155-
newn = n - c1;
13156-
eq = getNthConnectionitemInClassparts(xs, newn);
13157-
then
13158-
eq;
13159-
13160-
case ((_ :: xs),n)
13161-
equation
13162-
eq = getNthConnectionitemInClassparts(xs, n);
13163-
then
13164-
eq;
13165-
end matchcontinue;
13166-
end getNthConnectionitemInClassparts;
13167-
13168-
protected function getNthConnectionitemInEquations
13169-
" This function takes an Equation list and an int
13170-
and returns the nth connection as an Equation.
13171-
If the number is larger than the number of connections
13172-
in the list, the function fails."
13173-
input list<Absyn.EquationItem> inAbsynEquationItemLst;
13174-
input Integer inInteger;
13175-
output Absyn.EquationItem outEquationItem;
13176-
algorithm
13177-
outEquationItem := matchcontinue (inAbsynEquationItemLst,inInteger)
13178-
local
13179-
Absyn.EquationItem eq;
13180-
list<Absyn.EquationItem> xs;
13181-
list<Absyn.EquationItem> forEqList;
13182-
Integer newn,n;
13183-
13184-
case (((eq as Absyn.EQUATIONITEM(equation_ = Absyn.EQ_CONNECT())) :: _),1) then eq;
13185-
13186-
case ((Absyn.EQUATIONITEM(equation_ = Absyn.EQ_CONNECT()) :: xs),n)
13187-
equation
13188-
newn = n - 1;
13189-
eq = getNthConnectionitemInEquations(xs, newn);
13190-
then
13191-
eq;
13192-
13193-
case ((Absyn.EQUATIONITEM(equation_ = Absyn.EQ_FOR(forEquations = forEqList)) :: xs), n)
13194-
algorithm
13195-
try
13196-
eq := getNthConnectionitemInEquations(forEqList, n);
13197-
else
13198-
newn := n - listLength(forEqList);
13199-
eq := getNthConnectionitemInEquations(xs, newn);
13200-
end try;
13201-
then
13202-
eq;
13203-
13204-
case ((_ :: xs),n)
13205-
equation
13206-
eq = getNthConnectionitemInEquations(xs, n);
13207-
then
13208-
eq;
13209-
13210-
case ({},_) then fail();
13211-
13212-
end matchcontinue;
13213-
end getNthConnectionitemInEquations;
13214-
1321513069
protected function getConnectionStr
1321613070
" This function takes an Equation assumed to contain a connection and
1321713071
returns a comma separated string of componentreferences, e.g \"R1.n,C.p\"
@@ -13234,98 +13088,100 @@ algorithm
1323413088
end match;
1323513089
end getConnectionStr;
1323613090

13237-
protected function countConnections
13238-
"This function takes a Class and returns an int
13239-
with the number of connections in the Class."
13091+
public function getConnections
13092+
"This function takes a Class and returns a list of connections in the Class."
1324013093
input Absyn.Class inClass;
13241-
output Integer outInteger;
13094+
output list<Absyn.EquationItem> outList;
1324213095
algorithm
13243-
outInteger := match (inClass)
13096+
outList := match (inClass)
1324413097
local
13245-
Integer count;
13098+
list<Absyn.EquationItem> connectionsList;
1324613099
list<Absyn.ClassPart> parts;
1324713100

1324813101
case Absyn.CLASS(body = Absyn.PARTS(classParts = parts))
1324913102
equation
13250-
count = countConnectionsInClassparts(parts);
13103+
connectionsList = getConnectionsInClassparts(parts);
1325113104
then
13252-
count;
13105+
connectionsList;
1325313106

1325413107
// adrpo: handle also the case model extends X end X;
1325513108
case Absyn.CLASS(body = Absyn.CLASS_EXTENDS(parts = parts))
1325613109
equation
13257-
count = countConnectionsInClassparts(parts);
13110+
connectionsList = getConnectionsInClassparts(parts);
1325813111
then
13259-
count;
13112+
connectionsList;
1326013113

13261-
case Absyn.CLASS(body = Absyn.DERIVED()) then 0;
13114+
case Absyn.CLASS(body = Absyn.DERIVED()) then {};
1326213115

1326313116
end match;
13264-
end countConnections;
13117+
end getConnections;
1326513118

13266-
protected function countConnectionsInClassparts
13119+
protected function getConnectionsInClassparts
1326713120
" This function takes a ClassPart list and returns
13268-
an int with the number of connections in that list."
13121+
a list of connections in that list."
1326913122
input list<Absyn.ClassPart> inAbsynClassPartLst;
13270-
output Integer outInteger;
13123+
output list<Absyn.EquationItem> outList;
1327113124
algorithm
13272-
outInteger := matchcontinue (inAbsynClassPartLst)
13125+
outList := matchcontinue (inAbsynClassPartLst)
1327313126
local
13274-
Integer r1,r2,res;
13275-
list<Absyn.EquationItem> eqlist;
13127+
list<Absyn.EquationItem> eqlist1, eqlist2;
1327613128
list<Absyn.ClassPart> xs;
1327713129

13278-
case ((Absyn.EQUATIONS(contents = eqlist) :: xs))
13130+
case ((Absyn.EQUATIONS(contents = eqlist1) :: xs))
1327913131
equation
13280-
r1 = countConnectionsInEquations(eqlist, 0);
13281-
r2 = countConnectionsInClassparts(xs);
13132+
eqlist1 = getConnectionsInEquations(eqlist1);
13133+
eqlist2 = getConnectionsInClassparts(xs);
1328213134
then
13283-
r1 + r2;
13135+
listAppend(eqlist1, eqlist2);
1328413136

1328513137
case ((_ :: xs))
1328613138
equation
13287-
res = countConnectionsInClassparts(xs);
13139+
eqlist1 = getConnectionsInClassparts(xs);
1328813140
then
13289-
res;
13141+
eqlist1;
1329013142

13291-
case ({}) then 0;
13143+
case ({}) then {};
1329213144

1329313145
end matchcontinue;
13294-
end countConnectionsInClassparts;
13146+
end getConnectionsInClassparts;
1329513147

13296-
protected function countConnectionsInEquations
13297-
" This function takes an Equation list and returns an int
13298-
with the number of connect statements in that list."
13148+
protected function getConnectionsInEquations
13149+
" This function takes an Equation list and returns a list
13150+
of connect statements in that list."
1329913151
input list<Absyn.EquationItem> inAbsynEquationItemLst;
13300-
input Integer inInteger;
13301-
output Integer outInteger;
13152+
output list<Absyn.EquationItem> outList;
1330213153
algorithm
13303-
outInteger := match (inAbsynEquationItemLst)
13154+
outList := match (inAbsynEquationItemLst)
1330413155
local
13305-
Integer r1,res;
13156+
Absyn.EquationItem eq;
13157+
list<Absyn.EquationItem> eqlist1, eqlist2;
1330613158
list<Absyn.EquationItem> xs;
1330713159
list<Absyn.EquationItem> forEqList;
1330813160

13309-
case ((Absyn.EQUATIONITEM(equation_ = Absyn.EQ_CONNECT()) :: xs))
13161+
case (((eq as Absyn.EQUATIONITEM(equation_ = Absyn.EQ_CONNECT())) :: xs))
13162+
equation
13163+
eqlist1 = getConnectionsInEquations(xs);
1331013164
then
13311-
countConnectionsInEquations(xs, inInteger + 1);
13165+
eq::eqlist1;
13166+
1331213167

1331313168
case ((Absyn.EQUATIONITEM(equation_ = Absyn.EQ_FOR(forEquations = forEqList)) :: xs))
1331413169
equation
13315-
outInteger = countConnectionsInEquations(forEqList, inInteger);
13170+
eqlist1 = getConnectionsInEquations(forEqList);
13171+
eqlist2 = getConnectionsInEquations(xs);
1331613172
then
13317-
countConnectionsInEquations(xs, outInteger);
13173+
listAppend(eqlist1, eqlist2);
1331813174

1331913175
case ((_ :: xs))
13176+
equation
13177+
eqlist1 = getConnectionsInEquations(xs);
1332013178
then
13321-
countConnectionsInEquations(xs, inInteger);
13179+
eqlist1;
1332213180

13323-
case ({})
13324-
then
13325-
inInteger;
13181+
case ({}) then {};
1332613182

1332713183
end match;
13328-
end countConnectionsInEquations;
13184+
end getConnectionsInEquations;
1332913185

1333013186
protected function getComponentAnnotationsFromElts
1333113187
"Helper function to getComponentAnnotations."

0 commit comments

Comments
 (0)