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

Commit eef9c24

Browse files
adeas31OpenModelica-Hudson
authored andcommitted
Update the connection instead of deleting and adding it
Moved the updateConnection API to ModelicaBuiltin.mo Added a new API updateConnectionNames which updates the connection connectors. Belonging to [master]: - OpenModelica/OpenModelica#151 - #3061
1 parent c252b04 commit eef9c24

File tree

3 files changed

+282
-13
lines changed

3 files changed

+282
-13
lines changed

Compiler/FrontEnd/ModelicaBuiltin.mo

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,6 +3120,31 @@ annotation(
31203120
preferredView="text");
31213121
end removeExtendsModifiers;
31223122

3123+
function updateConnection
3124+
input TypeName className;
3125+
input String from;
3126+
input String to;
3127+
input ExpressionOrModification annotate;
3128+
output Boolean result;
3129+
external "builtin";
3130+
annotation(preferredView="text",Documentation(info="<html>
3131+
<p>Updates the connection annotation in the class. See also updateConnectionNames().</p>
3132+
</html>"));
3133+
end updateConnection;
3134+
3135+
function updateConnectionNames
3136+
input TypeName className;
3137+
input String from;
3138+
input String to;
3139+
input String fromNew;
3140+
input String toNew;
3141+
output Boolean result;
3142+
external "builtin";
3143+
annotation(preferredView="text",Documentation(info="<html>
3144+
<p>Updates the connection connector names in the class. See also updateConnection().</p>
3145+
</html>"));
3146+
end updateConnectionNames;
3147+
31233148
function getConnectionCount "Counts the number of connect equation in a class."
31243149
input TypeName className;
31253150
output Integer count;

Compiler/Script/CevalScriptBackend.mo

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,6 +2613,26 @@ algorithm
26132613
then
26142614
(cache,v);
26152615

2616+
case (cache,_,"updateConnection",{Values.CODE(Absyn.C_TYPENAME(classpath)),Values.STRING(str1), Values.STRING(str2),
2617+
Values.CODE(Absyn.C_MODIFICATION(Absyn.CLASSMOD(elementArgLst=eltargs,eqMod=Absyn.NOMOD())))},_)
2618+
equation
2619+
(b, p) = Interactive.updateConnectionAnnotation(classpath, str1, str2, Absyn.ANNOTATION(eltargs), SymbolTable.getAbsyn());
2620+
SymbolTable.setAbsyn(p);
2621+
then
2622+
(cache,Values.BOOL(b));
2623+
2624+
case (cache,_,"updateConnection",_,_) then (cache,Values.BOOL(false));
2625+
2626+
case (cache,_,"updateConnectionNames",{Values.CODE(Absyn.C_TYPENAME(classpath)),Values.STRING(str1), Values.STRING(str2),
2627+
Values.STRING(str3), Values.STRING(str4)},_)
2628+
equation
2629+
(b, p) = Interactive.updateConnectionNames(classpath, str1, str2, str3, str4, SymbolTable.getAbsyn());
2630+
SymbolTable.setAbsyn(p);
2631+
then
2632+
(cache,Values.BOOL(b));
2633+
2634+
case (cache,_,"updateConnectionNames",_,_) then (cache,Values.BOOL(false));
2635+
26162636
case (cache,_,"getConnectionCount",{Values.CODE(Absyn.C_TYPENAME(path))},_)
26172637
equation
26182638
absynClass = Interactive.getPathedClassInProgram(path, SymbolTable.getAbsyn());

Compiler/Script/Interactive.mo

Lines changed: 237 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,17 +1126,6 @@ algorithm
11261126
then
11271127
outResult;
11281128

1129-
case "updateConnection"
1130-
algorithm
1131-
{Absyn.CREF(componentRef = cr1),
1132-
Absyn.CREF(componentRef = cr2),
1133-
Absyn.CREF(componentRef = cr)} := args;
1134-
nargs := getApiFunctionNamedArgs(inStatement);
1135-
(_, p) := deleteConnection(cr, cr1, cr2, p);
1136-
(outResult, p) := addConnection(cr, cr1, cr2, nargs, p);
1137-
then
1138-
outResult;
1139-
11401129
case "getNthConnectionAnnotation"
11411130
algorithm
11421131
{Absyn.CREF(componentRef = cr), Absyn.INTEGER(value = n)} := args;
@@ -10048,6 +10037,241 @@ algorithm
1004810037
end matchcontinue;
1004910038
end addConnection;
1005010039

10040+
public function updateConnectionAnnotation
10041+
"Updates a connection annotation in a model."
10042+
input Absyn.Path inPath;
10043+
input String inFrom;
10044+
input String inTo;
10045+
input Absyn.Annotation inAnnotation;
10046+
input Absyn.Program inProgram;
10047+
output Boolean outResult;
10048+
output Absyn.Program outProgram;
10049+
algorithm
10050+
(outResult, outProgram) := matchcontinue (inPath, inFrom , inTo, inAnnotation, inProgram)
10051+
local
10052+
Absyn.Path path, modelwithin;
10053+
String from, to;
10054+
Absyn.Annotation ann;
10055+
Absyn.Class cdef, newcdef;
10056+
Absyn.Program newp, p;
10057+
10058+
case (path, from, to, ann, (p as Absyn.PROGRAM()))
10059+
equation
10060+
modelwithin = Absyn.stripLast(path);
10061+
cdef = getPathedClassInProgram(path, p);
10062+
newcdef = updateConnectionAnnotationInClass(cdef, from, to, ann);
10063+
newp = updateProgram(Absyn.PROGRAM({newcdef},Absyn.WITHIN(modelwithin)), p);
10064+
then
10065+
(true, newp);
10066+
10067+
case (path, from, to, ann, (p as Absyn.PROGRAM()))
10068+
equation
10069+
cdef = getPathedClassInProgram(path, p);
10070+
newcdef = updateConnectionAnnotationInClass(cdef, from, to, ann);
10071+
newp = updateProgram(Absyn.PROGRAM({newcdef},Absyn.TOP()), p);
10072+
then
10073+
(true, newp);
10074+
10075+
case (_, _, _, _, (p as Absyn.PROGRAM())) then (false, p);
10076+
end matchcontinue;
10077+
end updateConnectionAnnotation;
10078+
10079+
protected function updateConnectionAnnotationInClass
10080+
"Helper function to updateConnectionAnnotation."
10081+
input Absyn.Class inClass1;
10082+
input String inFrom;
10083+
input String inTo;
10084+
input Absyn.Annotation inAnnotation;
10085+
output Absyn.Class outClass;
10086+
algorithm
10087+
outClass:=
10088+
match (inClass1, inFrom, inTo, inAnnotation)
10089+
local
10090+
list<Absyn.EquationItem> eqlst,eqlst_1;
10091+
list<Absyn.ClassPart> parts2,parts;
10092+
String i, bcname;
10093+
Boolean p,f,e;
10094+
Absyn.Restriction r;
10095+
Option<String> cmt;
10096+
SourceInfo file_info;
10097+
list<Absyn.ElementArg> modif;
10098+
list<String> typeVars;
10099+
list<Absyn.NamedArg> classAttrs;
10100+
list<Absyn.Annotation> ann;
10101+
String from, to;
10102+
Absyn.Annotation annotation_;
10103+
/* a class with parts */
10104+
case (Absyn.CLASS(name = i,partialPrefix = p,finalPrefix = f,encapsulatedPrefix = e,restriction = r,
10105+
body = Absyn.PARTS(typeVars = typeVars,classAttrs = classAttrs,classParts = parts,ann=ann,comment = cmt),info = file_info),from,to,annotation_)
10106+
equation
10107+
eqlst = getEquationList(parts);
10108+
eqlst_1 = updateConnectionAnnotationInEqList(eqlst, from, to, annotation_);
10109+
parts2 = replaceEquationList(parts, eqlst_1);
10110+
then
10111+
Absyn.CLASS(i,p,f,e,r,Absyn.PARTS(typeVars,classAttrs,parts2,ann,cmt),file_info);
10112+
/* an extended class with parts: model extends M end M; */
10113+
case (Absyn.CLASS(name = i,partialPrefix = p,finalPrefix = f,encapsulatedPrefix = e,restriction = r,
10114+
body = Absyn.CLASS_EXTENDS(baseClassName = bcname,modifications=modif,parts = parts,ann = ann,comment = cmt),info = file_info),from,to,annotation_)
10115+
equation
10116+
eqlst = getEquationList(parts);
10117+
eqlst_1 = updateConnectionAnnotationInEqList(eqlst, from, to, annotation_);
10118+
parts2 = replaceEquationList(parts, eqlst_1);
10119+
then
10120+
Absyn.CLASS(i,p,f,e,r,Absyn.CLASS_EXTENDS(bcname,modif,cmt,parts2,ann),file_info);
10121+
end match;
10122+
end updateConnectionAnnotationInClass;
10123+
10124+
protected function updateConnectionAnnotationInEqList
10125+
"Helper function to updateConnectionAnnotation."
10126+
input list<Absyn.EquationItem> equations;
10127+
input String from;
10128+
input String to;
10129+
input Absyn.Annotation ann;
10130+
output list<Absyn.EquationItem> outEquations = {};
10131+
protected
10132+
Absyn.ComponentRef c1, c2;
10133+
String c1_str, c2_str;
10134+
algorithm
10135+
for eq in equations loop
10136+
eq := match eq
10137+
case Absyn.EQUATIONITEM(equation_ = Absyn.EQ_CONNECT(connector1 = c1, connector2 = c2))
10138+
algorithm
10139+
c1_str := Absyn.crefString(c1);
10140+
c2_str := Absyn.crefString(c2);
10141+
10142+
if (c1_str == from and c2_str == to) or (c1_str == to and c2_str == from) then
10143+
eq.comment := SOME(Absyn.COMMENT(SOME(ann), NONE()));
10144+
end if;
10145+
then
10146+
eq;
10147+
10148+
else eq;
10149+
end match;
10150+
10151+
outEquations := eq :: outEquations;
10152+
end for;
10153+
10154+
outEquations := Dangerous.listReverseInPlace(outEquations);
10155+
end updateConnectionAnnotationInEqList;
10156+
10157+
public function updateConnectionNames
10158+
"Updates a connection connector names in a model."
10159+
input Absyn.Path inPath;
10160+
input String inFrom;
10161+
input String inTo;
10162+
input String inFromNew;
10163+
input String inToNew;
10164+
input Absyn.Program inProgram;
10165+
output Boolean outResult;
10166+
output Absyn.Program outProgram;
10167+
algorithm
10168+
(outResult, outProgram) := matchcontinue (inPath, inFrom, inTo, inFromNew, inToNew, inProgram)
10169+
local
10170+
Absyn.Path path, modelwithin;
10171+
String from, to, fromNew, toNew;
10172+
Absyn.Class cdef, newcdef;
10173+
Absyn.Program newp, p;
10174+
10175+
case (path, from, to, fromNew, toNew, (p as Absyn.PROGRAM()))
10176+
equation
10177+
modelwithin = Absyn.stripLast(path);
10178+
cdef = getPathedClassInProgram(path, p);
10179+
newcdef = updateConnectionNamesInClass(cdef, from, to, fromNew, toNew);
10180+
newp = updateProgram(Absyn.PROGRAM({newcdef},Absyn.WITHIN(modelwithin)), p);
10181+
then
10182+
(true, newp);
10183+
10184+
case (path, from, to, fromNew, toNew, (p as Absyn.PROGRAM()))
10185+
equation
10186+
cdef = getPathedClassInProgram(path, p);
10187+
newcdef = updateConnectionNamesInClass(cdef, from, to, fromNew, toNew);
10188+
newp = updateProgram(Absyn.PROGRAM({newcdef},Absyn.TOP()), p);
10189+
then
10190+
(true, newp);
10191+
10192+
case (_, _, _, _, _, (p as Absyn.PROGRAM())) then (false, p);
10193+
end matchcontinue;
10194+
end updateConnectionNames;
10195+
10196+
protected function updateConnectionNamesInClass
10197+
"Helper function to updateConnectionNames."
10198+
input Absyn.Class inClass1;
10199+
input String inFrom;
10200+
input String inTo;
10201+
input String inFromNew;
10202+
input String inToNew;
10203+
output Absyn.Class outClass;
10204+
algorithm
10205+
outClass:=
10206+
match (inClass1, inFrom, inTo, inFromNew, inToNew)
10207+
local
10208+
list<Absyn.EquationItem> eqlst,eqlst_1;
10209+
list<Absyn.ClassPart> parts2,parts;
10210+
String i, bcname;
10211+
Boolean p,f,e;
10212+
Absyn.Restriction r;
10213+
Option<String> cmt;
10214+
SourceInfo file_info;
10215+
list<Absyn.ElementArg> modif;
10216+
list<String> typeVars;
10217+
list<Absyn.NamedArg> classAttrs;
10218+
list<Absyn.Annotation> ann;
10219+
String from, to, fromNew, toNew;
10220+
/* a class with parts */
10221+
case (Absyn.CLASS(name = i,partialPrefix = p,finalPrefix = f,encapsulatedPrefix = e,restriction = r,
10222+
body = Absyn.PARTS(typeVars = typeVars,classAttrs = classAttrs,classParts = parts,ann=ann,comment = cmt),info = file_info),from,to,fromNew,toNew)
10223+
equation
10224+
eqlst = getEquationList(parts);
10225+
eqlst_1 = updateConnectionNamesInEqList(eqlst, from, to, fromNew, toNew);
10226+
parts2 = replaceEquationList(parts, eqlst_1);
10227+
then
10228+
Absyn.CLASS(i,p,f,e,r,Absyn.PARTS(typeVars,classAttrs,parts2,ann,cmt),file_info);
10229+
/* an extended class with parts: model extends M end M; */
10230+
case (Absyn.CLASS(name = i,partialPrefix = p,finalPrefix = f,encapsulatedPrefix = e,restriction = r,
10231+
body = Absyn.CLASS_EXTENDS(baseClassName = bcname,modifications=modif,parts = parts,ann = ann,comment = cmt),info = file_info),from,to,fromNew,toNew)
10232+
equation
10233+
eqlst = getEquationList(parts);
10234+
eqlst_1 = updateConnectionNamesInEqList(eqlst, from, to, fromNew, toNew);
10235+
parts2 = replaceEquationList(parts, eqlst_1);
10236+
then
10237+
Absyn.CLASS(i,p,f,e,r,Absyn.CLASS_EXTENDS(bcname,modif,cmt,parts2,ann),file_info);
10238+
end match;
10239+
end updateConnectionNamesInClass;
10240+
10241+
protected function updateConnectionNamesInEqList
10242+
"Helper function to updateConnectionNames."
10243+
input list<Absyn.EquationItem> equations;
10244+
input String from;
10245+
input String to;
10246+
input String fromNew;
10247+
input String toNew;
10248+
output list<Absyn.EquationItem> outEquations = {};
10249+
protected
10250+
Absyn.ComponentRef c1, c2;
10251+
String c1_str, c2_str;
10252+
algorithm
10253+
for eq in equations loop
10254+
eq := match eq
10255+
case Absyn.EQUATIONITEM(equation_ = Absyn.EQ_CONNECT(connector1 = c1, connector2 = c2))
10256+
algorithm
10257+
c1_str := Absyn.crefString(c1);
10258+
c2_str := Absyn.crefString(c2);
10259+
10260+
if (c1_str == from and c2_str == to) or (c1_str == to and c2_str == from) then
10261+
eq.equation_ := Absyn.EQ_CONNECT(Parser.stringCref(fromNew), Parser.stringCref(toNew));
10262+
end if;
10263+
then
10264+
eq;
10265+
10266+
else eq;
10267+
end match;
10268+
10269+
outEquations := eq :: outEquations;
10270+
end for;
10271+
10272+
outEquations := Dangerous.listReverseInPlace(outEquations);
10273+
end updateConnectionNamesInEqList;
10274+
1005110275
protected function deleteConnection "
1005210276
Delete the connection connect(c1,c2) from a model.
1005310277

@@ -14510,7 +14734,7 @@ algorithm
1451014734
info = file_info),eitem)
1451114735
equation
1451214736
eqlst = getEquationList(parts);
14513-
eqlst2 = (eitem :: eqlst);
14737+
eqlst2 = listAppend(eqlst, {eitem});
1451414738
parts2 = replaceEquationList(parts, eqlst2);
1451514739
then
1451614740
Absyn.CLASS(i,p,f,e,r,Absyn.PARTS(typeVars,classAttrs,parts2,ann,cmt),file_info);
@@ -14533,7 +14757,7 @@ algorithm
1453314757
info = file_info),eitem)
1453414758
equation
1453514759
eqlst = getEquationList(parts);
14536-
eqlst2 = (eitem :: eqlst);
14760+
eqlst2 = listAppend(eqlst, {eitem});
1453714761
parts2 = replaceEquationList(parts, eqlst2);
1453814762
then
1453914763
Absyn.CLASS(i,p,f,e,r,Absyn.CLASS_EXTENDS(baseClassName,modifications,cmt,parts2,ann),file_info);

0 commit comments

Comments
 (0)