@@ -79,7 +79,7 @@ import Face = NFConnector.Face;
7979import System ;
8080import ComplexType = NFComplexType ;
8181import NFInstNode.CachedData ;
82- import NFPrefixes . {Direction , Variability , Visibility , Purity , Parallelism };
82+ import NFPrefixes . {ConnectorType , Direction , Variability , Visibility , Purity , Parallelism };
8383import Variable = NFVariable ;
8484import ElementSource ;
8585import Ceval = NFCeval ;
@@ -2183,13 +2183,15 @@ function resolveConnections
21832183 input FlattenSettings settings;
21842184protected
21852185 Connections conns;
2186- list< Equation > conn_eql, ec_eql;
2186+ list< Equation > conn_eql, ec_eql, tlio_eql;
2187+ list< Variable > tlio_vars;
21872188 ConnectionSets . Sets csets;
21882189 array< list< Connector >> csets_array;
21892190 CardinalityTable . Table ctable;
21902191 Connections . BrokenEdges broken = {};
21912192 UnorderedMap < ComponentRef , Variable > vars;
21922193 UnorderedSet < ComponentRef > connectedLocalIOs;
2194+ Integer exposeLocalIOs;
21932195algorithm
21942196 vars := UnorderedMap . new< Variable > (ComponentRef . hash, ComponentRef . isEqual,
21952197 listLength(flatModel. variables));
@@ -2240,9 +2242,12 @@ algorithm
22402242 // add the equations to the flat model
22412243 flatModel. equations := listAppend(conn_eql, flatModel. equations);
22422244
2243- // remove input and output prefixes from local IOs that are determined through connect equations
2244- if Flags . getConfigInt(Flags . EXPOSE_LOCAL_IOS ) > 0 then
2245- flatModel. variables := list(stripInputOutputForConnected(v, connectedLocalIOs) for v in flatModel. variables);
2245+ // add top-level IOs for unconnected local IOs
2246+ exposeLocalIOs := Flags . getConfigInt(Flags . EXPOSE_LOCAL_IOS );
2247+ if exposeLocalIOs > 0 then
2248+ (tlio_vars, tlio_eql) := generateTopLevelIOs(vars, connectedLocalIOs, exposeLocalIOs);
2249+ flatModel. variables := List . append_reverse(flatModel. variables, tlio_vars);
2250+ flatModel. equations := List . append_reverse(flatModel. equations, tlio_eql);
22462251 end if ;
22472252
22482253 // Evaluate any connection operators if they're used.
@@ -2253,18 +2258,56 @@ algorithm
22532258 execStat(getInstanceName());
22542259end resolveConnections;
22552260
2256- function stripInputOutputForConnected
2257- "remove input and output prefixes if variable appears in connectedIOs"
2258- input output Variable v;
2259- input UnorderedSet < ComponentRef > connectedIOs;
2261+ function generateTopLevelIOs
2262+ "generate top-level inputs and outputs for public unconnected local input and output connectors"
2263+ input UnorderedMap < ComponentRef , Variable > variables;
2264+ input UnorderedSet < ComponentRef > connectedLocalIOs;
2265+ input Integer exposeLocalIOs;
2266+ output list< Variable > tlio_vars;
2267+ output list< Equation > tlio_eql;
22602268protected
2261- Attributes attributes = v. attributes;
2262- algorithm
2263- if UnorderedSet . contains(v. name, connectedIOs) then
2264- attributes. direction := Direction . NONE ;
2265- v. attributes := attributes;
2266- end if ;
2267- end stripInputOutputForConnected;
2269+ Attributes attributes;
2270+ Variable tlio_var;
2271+ ComponentRef cref;
2272+ String name;
2273+ InstNode tlio_node;
2274+ Integer level;
2275+ algorithm
2276+ tlio_vars := {};
2277+ tlio_eql := {};
2278+ for variable in UnorderedMap . valueList(variables) loop
2279+ level := ComponentRef . depth(variable. name) - 1 ;
2280+ attributes := variable. attributes;
2281+ if 0 < level and level <= exposeLocalIOs and
2282+ variable. visibility == Visibility . PUBLIC and
2283+ attributes. connectorType <> ConnectorType . NON_CONNECTOR and
2284+ (attributes. direction == Direction . INPUT or attributes. direction == Direction . OUTPUT ) and
2285+ not UnorderedSet . contains(variable. name, connectedLocalIOs)
2286+ then
2287+ // add a new variable and equation if removeNonTopLevelDirection removes the direction
2288+ tlio_var := Variable . removeNonTopLevelDirection(variable);
2289+ attributes := tlio_var. attributes;
2290+ if attributes. direction == Direction . NONE then
2291+ tlio_var := variable; // same attributes like start, unit
2292+ tlio_var. name := ComponentRef . combineSubscripts(tlio_var. name);
2293+ tlio_var. binding := UNBOUND (); // value is defined with tlio_eql
2294+ // find new name in global scope, starting with quoted identifier
2295+ cref := tlio_var. name;
2296+ name := stringDelimitList(ComponentRef . toString_impl(cref, {}), "." );
2297+ while UnorderedMap . contains(tlio_var. name, variables) loop
2298+ tlio_node := InstNode . NAME_NODE (Util . makeQuotedIdentifier(name));
2299+ tlio_var. name := match cref case ComponentRef . CREF () then
2300+ ComponentRef . CREF (tlio_node, cref. subscripts, cref. ty, cref. origin, ComponentRef . EMPTY ());
2301+ end match;
2302+ name := name + "_" "append underscore until name is unique" ;
2303+ end while ;
2304+ tlio_vars := tlio_var :: tlio_vars;
2305+ tlio_eql := Equation . makeCrefEquality(variable. name, tlio_var. name,
2306+ InstNode . EMPTY_NODE (), ElementSource . createElementSource(variable. info)) :: tlio_eql;
2307+ end if ;
2308+ end if ;
2309+ end for ;
2310+ end generateTopLevelIOs;
22682311
22692312function evaluateConnectionOperators
22702313 input output FlatModel flatModel;
0 commit comments