Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch connection list API #10042

Merged
merged 5 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions OMCompiler/Compiler/FrontEnd/ModelicaBuiltin.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3281,6 +3281,18 @@ external "builtin";
annotation(preferredView="text");
end getNthConnection;

function getConnectionList "returns an array of all connections including those within loops"
input TypeName className;
output String[:,:] result;
external "builtin";
annotation(
Documentation(info="<html>
Returns a list of all connect equations including those in loops. For example:
<pre>{{\"connection1.lhs\",\"connection1.rhs\"}, {\"connection2.lhs\",\"connection2.rhs\"}}</pre>
</html>"),
preferredView="text");
end getConnectionList;

function getAlgorithmCount "Counts the number of Algorithm sections in a class."
input TypeName class_;
output Integer count;
Expand Down
7 changes: 7 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFConnections.mo
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,12 @@ public
str := stringDelimitList(strl, "\n");
end toString;

function toStringList
input Connections conns;
output list<list<String>> strl = {};
algorithm
strl := list({Connector.toString(c.lhs), Connector.toString(c.rhs)} for c in conns.connections);
end toStringList;

annotation(__OpenModelica_Interface="frontend");
end NFConnections;
34 changes: 27 additions & 7 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ constant Prefix EMPTY_INDEXED_PREFIX = Prefix.INDEXED_PREFIX(ComponentRef.EMPTY(
function flatten
input InstNode classInst;
input String name;
input Boolean getConnectionResolved = true;
output FlatModel flatModel;
protected
Sections sections;
Expand Down Expand Up @@ -327,18 +328,37 @@ algorithm
// get inputs and outputs for algorithms now that types are computed
flatModel.algorithms := list(Algorithm.setInputsOutputs(al) for al in flatModel.algorithms);
flatModel.initialAlgorithms := list(Algorithm.setInputsOutputs(al) for al in flatModel.initialAlgorithms);

execStat(getInstanceName());
InstUtil.dumpFlatModelDebug("flatten", flatModel);

if settings.arrayConnect then
flatModel := resolveArrayConnections(flatModel);
else
flatModel := resolveConnections(flatModel, deleted_vars, settings);

if getConnectionResolved then
if settings.arrayConnect then
flatModel := resolveArrayConnections(flatModel);
else
flatModel := resolveConnections(flatModel, deleted_vars, settings);
end if;
InstUtil.dumpFlatModelDebug("connections", flatModel);
end if;
InstUtil.dumpFlatModelDebug("connections", flatModel);
end flatten;

function flattenConnection
rahulp13 marked this conversation as resolved.
Show resolved Hide resolved
input InstNode classInst;
input String name;
output Connections conns;
protected
FlatModel flatModel;
UnorderedSet<ComponentRef> deleted_vars;
algorithm
flatModel := flatten(classInst, name, false);
deleted_vars := UnorderedSet.new(ComponentRef.hash, ComponentRef.isEqual);

// get the connections from the model
(flatModel, conns) := Connections.collect(flatModel, function isDeletedConnector(deletedVars = deleted_vars));
// Elaborate expandable connectors.
(_, conns) := ExpandableConnectors.elaborate(flatModel, conns);
end flattenConnection;

function collectFunctions
input FlatModel flatModel;
output FunctionTree funcs;
Expand Down
41 changes: 41 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import Array;
import Error;
import FlagsUtil;
import Flatten = NFFlatten;
import Connections = NFConnections;
import InstUtil = NFInstUtil;
import List;
import Lookup = NFLookup;
Expand Down Expand Up @@ -237,6 +238,46 @@ algorithm
//print(name + " has " + String(var_count) + " variable(s) and " + String(eq_count) + " equation(s).\n");
end instClassInProgram;

function instClassForConnection
"Instantiates a class given by its fully qualified path, with the result being
a list of all connections."
input Absyn.Path classPath;
input SCode.Program program;
input SCode.Program annotationProgram;
output list<list<String>> connList = {};
protected
Connections conns;
InstNode top, cls, inst_cls;
String name;
InstContext.Type context;
algorithm
resetGlobalFlags();
context := if Flags.getConfigBool(Flags.CHECK_MODEL) or Flags.isSet(Flags.NF_API) then
NFInstContext.RELAXED else NFInstContext.NO_CONTEXT;

// Create a top scope from the given top-level classes.
top := makeTopNode(program, annotationProgram);
name := AbsynUtil.pathString(classPath);

// Look up the class to instantiate.
cls := lookupRootClass(classPath, top, context);

// Instantiate the class.
inst_cls := instantiateRootClass(cls, context);

// Instantiate expressions (i.e. anything that can contains crefs, like
// bindings, dimensions, etc). This is done as a separate step after
// instantiation to make sure that lookup is able to find the correct nodes.
instExpressions(inst_cls, context = context);

// Type the class.
Typing.typeClass(inst_cls, context);

// Flatten the model and get connections
conns := Flatten.flattenConnection(inst_cls, name);
connList := Connections.toStringList(conns);
end instClassForConnection;

function resetGlobalFlags
"Resets the global flags that the frontend uses."
algorithm
Expand Down
12 changes: 12 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFModelicaBuiltin.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3534,6 +3534,18 @@ external "builtin";
annotation(preferredView="text");
end getNthConnection;

function getConnectionList "returns an array of all connections including those within loops"
input TypeName className;
output String[:,:] result;
external "builtin";
annotation(
Documentation(info="<html>
Returns a list of all connect equations including those in loops. For example:
<pre>{{\"connection1.lhs\",\"connection1.rhs\"}, {\"connection2.lhs\",\"connection2.rhs\"}}</pre>
</html>"),
preferredView="text");
end getConnectionList;

function getAlgorithmCount "Counts the number of Algorithm sections in a class."
input TypeName class_;
output Integer count;
Expand Down
19 changes: 19 additions & 0 deletions OMCompiler/Compiler/Script/CevalScriptBackend.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2785,6 +2785,9 @@ algorithm

case ("getNthConnection",_) then ValuesUtil.makeArray({});

case ("getConnectionList", {Values.CODE(Absyn.C_TYPENAME(path))})
then getConnectionList(path);

case ("getAlgorithmCount",{Values.CODE(Absyn.C_TYPENAME(path))})
equation
absynClass = InteractiveUtil.getPathedClassInProgram(path, SymbolTable.getAbsyn());
Expand Down Expand Up @@ -8611,6 +8614,22 @@ algorithm
result := Values.STRING(str);
end instantiateModel;

protected function getConnectionList
"@author: rahulp
Returns a list of all connect equations including those in loops"
input Absyn.Path className;
output Values.Value valList;
protected
SCode.Program sp, annotation_sp;
list<list<String>> connList;
algorithm
annotation_sp := AbsynToSCode.translateAbsyn2SCode(InteractiveUtil.modelicaAnnotationProgram(Config.getAnnotationVersion()));
(_, sp) := FBuiltin.getInitialFunctions();
sp := listAppend(SymbolTable.getSCode(), sp);
connList := NFInst.instClassForConnection(className, sp, annotation_sp);
valList := ValuesUtil.makeArray(list(ValuesUtil.makeArray(List.map(conn, ValuesUtil.makeString)) for conn in connList));
end getConnectionList;

protected function runConversionScript
input Absyn.Path clsPath;
input String scriptFile;
Expand Down
69 changes: 69 additions & 0 deletions testsuite/openmodelica/interactive-API/ConnectionList.mos
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// name: ConnectionList
// status: correct
// cflags: -d=newInst
//
// Tests the getConnectionList API function.
//

loadString("
model M
connector A
end A;
connector B
end B;

parameter Integer n = 3;
A a[n], c;
B b[n], d;
equation
connect(a,b);
connect(c,d);
end M;
"); getErrorString();

getConnectionList(M); getErrorString();

deleteClass(M); getErrorString();



// Testing complex loop structure

loadString("
model M
connector A
end A;
connector B
end B;

parameter Integer n = 5;
A a[n];
B b[n];
equation
for i in n-2:(n-2)*2-3 loop
for j in 1:n loop
if j <> i then
connect(a[j],b[j]);
end if;
end for;

connect(a[i],b[i]);
end for;
end M;
"); getErrorString();

getConnectionList(M); getErrorString();


// Result:
// true
// ""
// {{"c","d"},{"a[3]","b[3]"},{"a[2]","b[2]"},{"a[1]","b[1]"}}
// ""
// true
// ""
// true
// ""
// {{"a[3]","b[3]"},{"a[5]","b[5]"},{"a[4]","b[4]"},{"a[2]","b[2]"},{"a[1]","b[1]"}}
// ""
// endResult
1 change: 1 addition & 0 deletions testsuite/openmodelica/interactive-API/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ checkAllModelsRecursive1.mos \
choicesAllMatching.mos \
ConvertUnits.mos \
ConversionVersions.mos \
ConnectionList.mos \
CopyClass.mos \
DefaultComponentName.mos \
DeleteConnection.mos \
Expand Down