- From: jonathan.cooper at comlab.ox.ac.uk (Jonathan Cooper)
- Subject: [cellml-dev] Connections and v1.3 suggestions
- Date: Tue, 10 Aug 2010 09:00:55 +0100
Hi Lucian et al,
I'd written some (Python) code a few weeks ago that essentially applies
Lucian's algorithm, and thought it might be helpful for anyone that
prefers reading code to text :)
Start with:
src_vname = target_vname = the name of the variables you're connecting
src_comp = component containing source variable
target_comp = component containing target variable
# Determine encapsulation paths from target & source to the root
(None)
src_path = self._parent_path(src_comp)
target_path = self._parent_path(target_comp)
# At some point these will share a common path, even if it's
just the root itself
meeting_index = self._find_common_tail(src_path, target_path)
# Construct path from source to target, leaving out the root (None)
path = src_path[:meeting_index]
if src_path[meeting_index]:
path.append(src_path[meeting_index])
path.extend(reversed(target_path[:meeting_index]))
# Traverse this path, adding connections at each step
for i, src_comp in enumerate(path[:-1]):
target_comp = path[i+1]
self._make_connection(src_comp, target_comp, src_vname)
_parent_path gives a list of components tracing up the encapsulation
hierarchy. For simplicity, we consider an imaginary component 'None' as
the parent of all top-level components. Connections then need to be
created up the path from src_comp to the root, until we meet the path
from target_comp to the root, at which point we follow that path down to
target_comp. _make_connection handles adding a connection between
adjacent components, and deals with the up/down/sideways issue and
already-connected variables.
Best wishes,
Jonathan
Lucian Smith wrote:
>
If you have widely-separated components that use the same variable in an
>
equation or two, you must trace out a path between those components,
>
putting the variable in the interstitial components as needed, and
>
connecting them with 'up', 'down', and 'sideways' connections. This can
>
seem daunting, but it turns out there is a relatively straightforward
>
algorithm than can get you from one to the other:
>
>
First, find the series of encapsulation parents of the canonical variable.
>
If the variable you wish to connect to it is in one of these parents,
>
connect 'down', either to the canonical variable or to the child in this
>
chain. If doing this doesn't connect directly to the canonical variable,
>
recursively call this algorithm again on the target.
>
>
If we are not connecting down, check instead if the parent of the
>
component is in the list of encapsulation parents. If it is, connect
>
'sideways', either to the canonical variable or to the sibling in the
>
parent chain. If the latter, recursively call the algorithm again on the
>
target.
>
>
In all other cases, connect 'up'. Call recursively on the target if you
>
haven't connected to the canonical variable yet.
>
>
>
There is a slight complication in that if you have some variables that are
>
already connected, you must connect to those variables before creating a
>
new one.
>
>
And that's it!
Archive powered by MHonArc 2.6.18.