|
|
@@ -24,27 +24,28 @@ import pypdevs.accurate_time as time
|
|
|
class BaseDEVS(object):
|
|
|
"""
|
|
|
Abstract base class for AtomicDEVS and CoupledDEVS classes.
|
|
|
-
|
|
|
+
|
|
|
This class provides basic DEVS attributes and query/set methods.
|
|
|
"""
|
|
|
+
|
|
|
def __init__(self, name):
|
|
|
"""
|
|
|
Constructor
|
|
|
|
|
|
:param name: the name of the DEVS model
|
|
|
"""
|
|
|
-
|
|
|
+
|
|
|
# Prevent any attempt to instantiate this abstract class
|
|
|
if self.__class__ == BaseDEVS:
|
|
|
- raise DEVSException ("Cannot instantiate abstract class '%s' ... "
|
|
|
- % (self.__class__.__name__))
|
|
|
+ raise DEVSException("Cannot instantiate abstract class '%s' ... "
|
|
|
+ % (self.__class__.__name__))
|
|
|
|
|
|
# The parent of the current model
|
|
|
self.parent = None
|
|
|
# The local name of the model
|
|
|
self.name = name
|
|
|
- self.IPorts = []
|
|
|
- self.OPorts = []
|
|
|
+ self.IPorts = []
|
|
|
+ self.OPorts = []
|
|
|
self.ports = []
|
|
|
|
|
|
# Initialise the times
|
|
|
@@ -52,14 +53,14 @@ class BaseDEVS(object):
|
|
|
self.time_next = (0.0, 1)
|
|
|
|
|
|
self.location = None
|
|
|
-
|
|
|
+
|
|
|
# Variables used for optimisations
|
|
|
- self.my_input = {}
|
|
|
+ self.my_input = {}
|
|
|
self.my_output = {}
|
|
|
|
|
|
# The state queue, used for time warp
|
|
|
self.old_states = []
|
|
|
- # List of all memoized states, only useful in distributed simulation
|
|
|
+ # List of all memoized states, only useful in distributed simulation
|
|
|
# with memoization enabled
|
|
|
self.memo = []
|
|
|
|
|
|
@@ -120,7 +121,7 @@ class BaseDEVS(object):
|
|
|
:param is_input: whether or not this is an input port
|
|
|
"""
|
|
|
name = name if name is not None else "port%s" % len(self.ports)
|
|
|
- port = Port(is_input=is_input, name=name)
|
|
|
+ port = Port(is_input=is_input, name=name)
|
|
|
if is_input:
|
|
|
self.IPorts.append(port)
|
|
|
else:
|
|
|
@@ -132,18 +133,18 @@ class BaseDEVS(object):
|
|
|
# If simulation is already running, the port should be direct connected too!
|
|
|
|
|
|
return port
|
|
|
-
|
|
|
+
|
|
|
def addInPort(self, name=None):
|
|
|
"""
|
|
|
Add an input port to the DEVS model.
|
|
|
-
|
|
|
- addInPort is the only proper way to add input ports to a DEVS model.
|
|
|
+
|
|
|
+ addInPort is the only proper way to add input ports to a DEVS model.
|
|
|
As for the CoupledDEVS.addSubModel method, calls
|
|
|
to addInPort and addOutPort can appear in any DEVS'
|
|
|
descriptive class constructor, or the methods can be used with an
|
|
|
instantiated object.
|
|
|
-
|
|
|
- The methods add a reference to the new port in the DEVS' IPorts
|
|
|
+
|
|
|
+ The methods add a reference to the new port in the DEVS' IPorts
|
|
|
attributes and set the port's hostDEVS attribute. The modeler
|
|
|
should typically save the returned reference somewhere.
|
|
|
|
|
|
@@ -151,7 +152,7 @@ class BaseDEVS(object):
|
|
|
:returns: port -- the generated port
|
|
|
"""
|
|
|
return self.addPort(name, True)
|
|
|
-
|
|
|
+
|
|
|
def addOutPort(self, name=None):
|
|
|
"""Add an output port to the DEVS model.
|
|
|
|
|
|
@@ -160,7 +161,7 @@ class BaseDEVS(object):
|
|
|
to addInPort and addOutPort can appear in any DEVS'
|
|
|
descriptive class constructor, or the methods can be used with an
|
|
|
instantiated object.
|
|
|
-
|
|
|
+
|
|
|
The methods add a reference to the new port in the DEVS'
|
|
|
OPorts attributes and set the port's hostDEVS attribute. The modeler
|
|
|
should typically save the returned reference somewhere.
|
|
|
@@ -169,7 +170,7 @@ class BaseDEVS(object):
|
|
|
:returns: port -- the generated port
|
|
|
"""
|
|
|
return self.addPort(name, False)
|
|
|
-
|
|
|
+
|
|
|
def getModelName(self):
|
|
|
"""
|
|
|
Get the local model name
|
|
|
@@ -203,11 +204,12 @@ class BaseDEVS(object):
|
|
|
return self.getModelName()
|
|
|
return self.parent.getModelFullNameRec() + "." + self.getModelName()
|
|
|
|
|
|
+
|
|
|
class AtomicDEVS(BaseDEVS):
|
|
|
"""
|
|
|
Abstract base class for all atomic-DEVS descriptive classes.
|
|
|
"""
|
|
|
-
|
|
|
+
|
|
|
def __init__(self, name=None):
|
|
|
"""
|
|
|
Constructor for an AtomicDEVS model
|
|
|
@@ -216,14 +218,14 @@ class AtomicDEVS(BaseDEVS):
|
|
|
"""
|
|
|
# Prevent any attempt to instantiate this abstract class
|
|
|
if self.__class__ == AtomicDEVS:
|
|
|
- raise DEVSException("Cannot instantiate abstract class '%s' ... "
|
|
|
+ raise DEVSException("Cannot instantiate abstract class '%s' ... "
|
|
|
% (self.__class__.__name__))
|
|
|
|
|
|
# The minimal constructor shall first call the superclass
|
|
|
# (i.e., BaseDEVS) constructor.
|
|
|
BaseDEVS.__init__(self, name)
|
|
|
-
|
|
|
- self.elapsed = 0.0
|
|
|
+
|
|
|
+ self.elapsed = 0.0
|
|
|
self.state = None
|
|
|
self.relocatable = True
|
|
|
self.last_read_time = (0, 0)
|
|
|
@@ -250,7 +252,7 @@ class AtomicDEVS(BaseDEVS):
|
|
|
if state.time_last[0] < time:
|
|
|
accumulator += state.activity
|
|
|
activities[self.model_id] = accumulator
|
|
|
-
|
|
|
+
|
|
|
def setGVT(self, gvt, activities, last_state_only):
|
|
|
"""
|
|
|
Set the GVT of the model, cleaning up the states vector as required
|
|
|
@@ -265,10 +267,10 @@ class AtomicDEVS(BaseDEVS):
|
|
|
for i in range(len(self.old_states)):
|
|
|
state = self.old_states[i]
|
|
|
if state.time_last[0] >= gvt:
|
|
|
- # Possible that all elements should be kept,
|
|
|
+ # Possible that all elements should be kept,
|
|
|
# in which case it will return -1 and only keep the last element
|
|
|
# So the copy element should be AT LEAST 0
|
|
|
- copy = max(0, i-1)
|
|
|
+ copy = max(0, i - 1)
|
|
|
break
|
|
|
elif not last_state_only:
|
|
|
activity += state.activity
|
|
|
@@ -326,7 +328,7 @@ class AtomicDEVS(BaseDEVS):
|
|
|
:returns: state -- the state at that time
|
|
|
"""
|
|
|
if self.location != MPIRedirect.local.name:
|
|
|
- return getProxy(self.location).getStateAtTime(self.model_id,
|
|
|
+ return getProxy(self.location).getStateAtTime(self.model_id,
|
|
|
request_time)
|
|
|
elif first_call:
|
|
|
# Shortcut if the call is local
|
|
|
@@ -342,7 +344,7 @@ class AtomicDEVS(BaseDEVS):
|
|
|
def extTransition(self, inputs):
|
|
|
"""
|
|
|
DEFAULT External Transition Function.
|
|
|
-
|
|
|
+
|
|
|
Accesses state and elapsed attributes, as well as inputs
|
|
|
through the passed dictionary. Returns the new state.
|
|
|
The elapsed time is accessible as an attribute (i.e., self.elapsed), see :ref:`elapsed_time` for a detailed explanation.
|
|
|
@@ -353,11 +355,11 @@ class AtomicDEVS(BaseDEVS):
|
|
|
:returns: state -- the new state of the model
|
|
|
"""
|
|
|
return self.state
|
|
|
-
|
|
|
+
|
|
|
def intTransition(self):
|
|
|
"""
|
|
|
DEFAULT Internal Transition Function.
|
|
|
-
|
|
|
+
|
|
|
.. note:: Should only write to the *state* attribute.
|
|
|
|
|
|
:returns: state -- the new state of the model
|
|
|
@@ -370,7 +372,7 @@ class AtomicDEVS(BaseDEVS):
|
|
|
def confTransition(self, inputs):
|
|
|
"""
|
|
|
DEFAULT Confluent Transition Function.
|
|
|
-
|
|
|
+
|
|
|
Accesses state and elapsed attributes, as well as inputs
|
|
|
through the passed dictionary. Returns the new state.
|
|
|
|
|
|
@@ -382,11 +384,11 @@ class AtomicDEVS(BaseDEVS):
|
|
|
self.state = self.intTransition()
|
|
|
self.state = self.extTransition(inputs)
|
|
|
return self.state
|
|
|
-
|
|
|
+
|
|
|
def outputFnc(self):
|
|
|
"""
|
|
|
DEFAULT Output Function.
|
|
|
-
|
|
|
+
|
|
|
Accesses only state attribute. Returns the output on the different ports as a dictionary.
|
|
|
|
|
|
.. note:: Should **not** write to any attribute.
|
|
|
@@ -397,11 +399,11 @@ class AtomicDEVS(BaseDEVS):
|
|
|
|
|
|
"""
|
|
|
return {}
|
|
|
-
|
|
|
+
|
|
|
def timeAdvance(self):
|
|
|
"""
|
|
|
DEFAULT Time Advance Function.
|
|
|
-
|
|
|
+
|
|
|
.. note:: Should ideally be deterministic, though this is not mandatory for simulation.
|
|
|
|
|
|
:returns: the time advance of the model
|
|
|
@@ -409,7 +411,7 @@ class AtomicDEVS(BaseDEVS):
|
|
|
.. versionchanged:: 2.1 The *elapsed* attribute is no longer guaranteed to be correct as this isn't required by the DEVS formalism.
|
|
|
|
|
|
"""
|
|
|
- # By default, return infinity
|
|
|
+ # By default, return infinity
|
|
|
return float('inf')
|
|
|
|
|
|
def preActivityCalculation(self):
|
|
|
@@ -493,12 +495,13 @@ class AtomicDEVS(BaseDEVS):
|
|
|
lst[self.location] += 1
|
|
|
self.num_children = 1
|
|
|
return self.num_children
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
class CoupledDEVS(BaseDEVS):
|
|
|
"""
|
|
|
Abstract base class for all coupled-DEVS descriptive classes.
|
|
|
"""
|
|
|
-
|
|
|
+
|
|
|
def __init__(self, name=None):
|
|
|
"""
|
|
|
Constructor.
|
|
|
@@ -507,12 +510,12 @@ class CoupledDEVS(BaseDEVS):
|
|
|
"""
|
|
|
# Prevent any attempt to instantiate this abstract class
|
|
|
if self.__class__ == CoupledDEVS:
|
|
|
- raise DEVSException("Cannot instantiate abstract class '%s' ... "
|
|
|
+ raise DEVSException("Cannot instantiate abstract class '%s' ... "
|
|
|
% (self.__class__.__name__))
|
|
|
# The minimal constructor shall first call the superclass
|
|
|
# (i.e., BaseDEVS) constructor.
|
|
|
BaseDEVS.__init__(self, name)
|
|
|
-
|
|
|
+
|
|
|
# All components of this coupled model (the submodels)
|
|
|
self.component_set = []
|
|
|
|
|
|
@@ -521,7 +524,7 @@ class CoupledDEVS(BaseDEVS):
|
|
|
Force a sequential simulation
|
|
|
"""
|
|
|
self.setLocation(0, force=True)
|
|
|
-
|
|
|
+
|
|
|
def select(self, imm_children):
|
|
|
"""
|
|
|
DEFAULT select function, only used when using Classic DEVS simulation
|
|
|
@@ -543,7 +546,7 @@ class CoupledDEVS(BaseDEVS):
|
|
|
children += i.getModelLoad(lst)
|
|
|
self.num_children = children
|
|
|
return self.num_children
|
|
|
-
|
|
|
+
|
|
|
def finalize(self, name, model_counter, model_ids, locations, select_hierarchy):
|
|
|
"""
|
|
|
Finalize the model hierarchy by doing all pre-simulation configuration
|
|
|
@@ -561,8 +564,8 @@ class CoupledDEVS(BaseDEVS):
|
|
|
# Set name, even though it will never be requested
|
|
|
self.full_name = name + str(self.getModelName())
|
|
|
for i in self.component_set:
|
|
|
- model_counter = i.finalize(self.full_name + ".", model_counter,
|
|
|
- model_ids, locations, select_hierarchy + [self])
|
|
|
+ model_counter = i.finalize(self.full_name + ".", model_counter,
|
|
|
+ model_ids, locations, select_hierarchy + [self])
|
|
|
return model_counter
|
|
|
|
|
|
def flattenConnections(self):
|
|
|
@@ -579,7 +582,7 @@ class CoupledDEVS(BaseDEVS):
|
|
|
for i in self.component_set:
|
|
|
i.unflattenConnections()
|
|
|
|
|
|
- def addSubModel(self, model, location = None):
|
|
|
+ def addSubModel(self, model, location=None):
|
|
|
"""
|
|
|
Adds a specified model to the current coupled model as its child. This
|
|
|
is the function that must be used to make distributed simulation
|
|
|
@@ -619,7 +622,7 @@ class CoupledDEVS(BaseDEVS):
|
|
|
def disconnectPorts(self, p1, p2):
|
|
|
"""
|
|
|
Disconnect two ports
|
|
|
-
|
|
|
+
|
|
|
.. note:: If these ports are connected multiple times, **only one** of them will be removed.
|
|
|
|
|
|
:param p1: the port at the start of the connection
|
|
|
@@ -645,7 +648,7 @@ class CoupledDEVS(BaseDEVS):
|
|
|
p2.inline = new_connection
|
|
|
self.server.getSelfProxy().dsDisconnectPorts(p1, p2)
|
|
|
|
|
|
- def connectPorts(self, p1, p2, z = None):
|
|
|
+ def connectPorts(self, p1, p2, z=None):
|
|
|
"""
|
|
|
Connects two ports together. The coupling is to begin at p1 and
|
|
|
to end at p2.
|
|
|
@@ -668,23 +671,23 @@ class CoupledDEVS(BaseDEVS):
|
|
|
(p1.type() == 'OUTPORT' and p2.type() == 'INPORT')):
|
|
|
if p1.host_DEVS is p2.host_DEVS:
|
|
|
raise DEVSException(("In coupled model '%s', connecting ports" +
|
|
|
- " '%s' and '%s' belong to the same model" +
|
|
|
- " '%s'. " +
|
|
|
- " Direct feedback coupling not allowed") % (
|
|
|
- self.getModelName(),
|
|
|
- p1.getPortName(),
|
|
|
- p2.getPortName(),
|
|
|
- p1.host_DEVS.getModelName()))
|
|
|
+ " '%s' and '%s' belong to the same model" +
|
|
|
+ " '%s'. " +
|
|
|
+ " Direct feedback coupling not allowed") % (
|
|
|
+ self.getModelName(),
|
|
|
+ p1.getPortName(),
|
|
|
+ p2.getPortName(),
|
|
|
+ p1.host_DEVS.getModelName()))
|
|
|
else:
|
|
|
p1.outline.append(p2)
|
|
|
p2.inline.append(p1)
|
|
|
-
|
|
|
+
|
|
|
# External input couplings:
|
|
|
elif ((p1.host_DEVS == self and p2.host_DEVS.parent == self) and
|
|
|
(p1.type() == p2.type() == 'INPORT')):
|
|
|
p1.outline.append(p2)
|
|
|
p2.inline.append(p1)
|
|
|
-
|
|
|
+
|
|
|
# Eternal output couplings:
|
|
|
elif ((p1.host_DEVS.parent == self and p2.host_DEVS == self) and
|
|
|
(p1.type() == p2.type() == 'OUTPORT')):
|
|
|
@@ -694,9 +697,9 @@ class CoupledDEVS(BaseDEVS):
|
|
|
# Other cases (illegal coupling):
|
|
|
else:
|
|
|
raise DEVSException(("Illegal coupling in coupled model '%s' " +
|
|
|
- "between ports '%s' and '%s'") % (
|
|
|
- self.getModelName(), p1.getPortName(),
|
|
|
- p2.getPortName()))
|
|
|
+ "between ports '%s' and '%s'") % (
|
|
|
+ self.getModelName(), p1.getPortName(),
|
|
|
+ p2.getPortName()))
|
|
|
|
|
|
p1.z_functions[p2] = z
|
|
|
if hasattr(self, "full_name"):
|
|
|
@@ -715,11 +718,13 @@ class CoupledDEVS(BaseDEVS):
|
|
|
for child in self.component_set:
|
|
|
child.setLocation(location, force)
|
|
|
|
|
|
+
|
|
|
class RootDEVS(BaseDEVS):
|
|
|
"""
|
|
|
The artificial RootDEVS model is the only 'coupled' model in the simulation after direct connection is performed.
|
|
|
"""
|
|
|
- def __init__(self, components, models, scheduler_type):
|
|
|
+
|
|
|
+ def __init__(self, components, models, scheduler_type, routing_mode=""):
|
|
|
"""
|
|
|
Basic constructor.
|
|
|
|
|
|
@@ -736,7 +741,9 @@ class RootDEVS(BaseDEVS):
|
|
|
self.models = models
|
|
|
self.scheduler_type = scheduler_type
|
|
|
self.listeners = {}
|
|
|
-
|
|
|
+
|
|
|
+ self.routing_mode = routing_mode
|
|
|
+
|
|
|
def redoDirectConnection(self, ports):
|
|
|
"""
|
|
|
Redo direct connection for a specified port, and all ports connected to it.
|
|
|
@@ -747,15 +754,35 @@ class RootDEVS(BaseDEVS):
|
|
|
worklist = list(ports)
|
|
|
for outport in worklist:
|
|
|
worklist.extend(outport.inline)
|
|
|
-
|
|
|
- for p in set(worklist):
|
|
|
- directConnectPort(p, self.listeners)
|
|
|
+
|
|
|
+ unique_worklist = set(worklist)
|
|
|
+ # print(f"\nChanged: {clean(str(unique_worklist))}")
|
|
|
+
|
|
|
+ # --- NEW: Smart Invalidation ---
|
|
|
+ # Clear the cache strictly for the affected upstream lineage
|
|
|
+ for p in unique_worklist:
|
|
|
+ p.cached_routes = None
|
|
|
+
|
|
|
+ # Rebuild the routes for the affected ports
|
|
|
+ for p in unique_worklist:
|
|
|
+ if self.routing_mode == "memoised":
|
|
|
+ for p in unique_worklist:
|
|
|
+ p.cached_routes = None
|
|
|
+ for p in unique_worklist:
|
|
|
+ directConnectPortMem(p, self.listeners)
|
|
|
+
|
|
|
+ elif self.routing_mode == "old":
|
|
|
+ for p in unique_worklist:
|
|
|
+ directConnectPortOLD(p, self.listeners)
|
|
|
+ else:
|
|
|
+ for p in unique_worklist:
|
|
|
+ directConnectPort(p, self.listeners)
|
|
|
|
|
|
def directConnect(self):
|
|
|
"""
|
|
|
Perform direct connection on the models again
|
|
|
"""
|
|
|
- directConnect(self.models, self.listeners)
|
|
|
+ directConnect(self.models, self.listeners, self.routing_mode)
|
|
|
|
|
|
def setScheduler(self, scheduler_type):
|
|
|
"""
|
|
|
@@ -810,7 +837,7 @@ class RootDEVS(BaseDEVS):
|
|
|
controller_revert |= child.revert(time, memorize)
|
|
|
# Was reverted, so reschedule
|
|
|
reschedules.add(child)
|
|
|
- # Always clear the inputs, as it is possible that there are only
|
|
|
+ # Always clear the inputs, as it is possible that there are only
|
|
|
# partial results, which doesn't get found in the time_last >= time
|
|
|
child.my_input = {}
|
|
|
self.scheduler.massReschedule(reschedules)
|
|
|
@@ -827,10 +854,12 @@ class RootDEVS(BaseDEVS):
|
|
|
# No element found in the scheduler, so put it to INFINITY
|
|
|
self.time_next = (float('inf'), 1)
|
|
|
|
|
|
+
|
|
|
class Port(object):
|
|
|
"""
|
|
|
Class for DEVS model ports (both input and output). This class provides basic port attributes and query methods.
|
|
|
"""
|
|
|
+
|
|
|
def __init__(self, is_input, name=None):
|
|
|
"""
|
|
|
Constructor. Creates an input port if isInput evaluates to True, and
|
|
|
@@ -839,16 +868,19 @@ class Port(object):
|
|
|
:param is_input: whether or not this is an input port
|
|
|
:param name: the name of the port. If None is provided, a unique ID is generated
|
|
|
"""
|
|
|
- self.inline = []
|
|
|
+ self.inline = []
|
|
|
self.outline = []
|
|
|
- self.host_DEVS = None
|
|
|
+ self.host_DEVS = None
|
|
|
self.msg_count = 0
|
|
|
-
|
|
|
+
|
|
|
# The name of the port
|
|
|
self.name = name
|
|
|
self.is_input = is_input
|
|
|
self.z_functions = {}
|
|
|
|
|
|
+ # --- NEW: Smart Caching Variables ---
|
|
|
+ self.cached_routes = None
|
|
|
+
|
|
|
def __repr__(self):
|
|
|
return "%s (%s)" % (self.type(), self.getPortFullName())
|
|
|
|
|
|
@@ -879,6 +911,7 @@ class Port(object):
|
|
|
else:
|
|
|
return 'OUTPORT'
|
|
|
|
|
|
+
|
|
|
def appendZ(first_z, new_z):
|
|
|
if first_z is None:
|
|
|
return new_z
|
|
|
@@ -887,6 +920,7 @@ def appendZ(first_z, new_z):
|
|
|
else:
|
|
|
return lambda x: new_z(first_z(x))
|
|
|
|
|
|
+
|
|
|
class ExternalWrapper(AtomicDEVS):
|
|
|
def __init__(self, function):
|
|
|
AtomicDEVS.__init__(self, "Fake")
|
|
|
@@ -898,7 +932,8 @@ class ExternalWrapper(AtomicDEVS):
|
|
|
# Fake object is created with a single fake port, so unpack that
|
|
|
self.f(list(self.my_input.values())[0])
|
|
|
|
|
|
-def directConnect(component_set, listeners):
|
|
|
+
|
|
|
+def directConnect(component_set, listeners, routing_mode="updated"):
|
|
|
"""
|
|
|
Perform direct connection on this CoupledDEVS model
|
|
|
|
|
|
@@ -915,92 +950,129 @@ def directConnect(component_set, listeners):
|
|
|
component_set = new_list
|
|
|
|
|
|
# All and only all atomic models are now direct children of this model
|
|
|
- listener_keys = set(listeners.keys())
|
|
|
for i in component_set:
|
|
|
# Remap the output ports
|
|
|
for outport in i.OPorts:
|
|
|
- # The new contents of the line
|
|
|
- outport.routing_outline = []
|
|
|
- worklist = [(p, outport.z_functions.get(p, None))
|
|
|
- for p in outport.outline]
|
|
|
- for outline, z in worklist:
|
|
|
- if outline in listener_keys:
|
|
|
- # This port is being listened on, so just add it as a fake model
|
|
|
- fake_port = Port(is_input=False,name="Fake")
|
|
|
- fake_port.host_DEVS = ExternalWrapper(listeners[outline])
|
|
|
- outport.routing_outline.append((fake_port, z))
|
|
|
- # If it is a coupled model, we must expand this model
|
|
|
- if isinstance(outline.host_DEVS, CoupledDEVS):
|
|
|
- for inline in outline.outline:
|
|
|
- # Add it to the current iterating list, so we can just continue
|
|
|
- entry = (inline, appendZ(z, outline.z_functions[inline]))
|
|
|
- worklist.append(entry)
|
|
|
- # If it is a Coupled model, we should just continue
|
|
|
- # expanding it and not add it to the finished line
|
|
|
- if not isinstance(inline.host_DEVS, CoupledDEVS):
|
|
|
- entry = (inline, appendZ(z, outline.z_functions[inline]))
|
|
|
- outport.routing_outline.append(entry)
|
|
|
- else:
|
|
|
- for ol, z in outport.routing_outline:
|
|
|
- if ol == outline:
|
|
|
- break
|
|
|
- else:
|
|
|
- # Add to the new line if it isn't already there
|
|
|
- # Note that it isn't really mandatory to check for this,
|
|
|
- # it is a lot cleaner to do so.
|
|
|
- # This will greatly increase the complexity of the connector though
|
|
|
- outport.routing_outline.append((outline, z))
|
|
|
+ if routing_mode == "memoised":
|
|
|
+ directConnectPortMem(outport, listeners)
|
|
|
+ elif routing_mode == "old":
|
|
|
+ directConnectPortOLD(outport, listeners)
|
|
|
+ else:
|
|
|
+ directConnectPort(outport, listeners)
|
|
|
return component_set
|
|
|
|
|
|
-# def directConnectPort(outport, listeners):
|
|
|
-# """
|
|
|
-# Perform direct connection on a single port.
|
|
|
-#
|
|
|
-# :param outport: the port to reconnect
|
|
|
-# :param listeners: the listeners that exist, potentially on this port
|
|
|
-# :returns: None
|
|
|
-# """
|
|
|
-#
|
|
|
-# # The new contents of the line
|
|
|
-# outport.routing_outline = []
|
|
|
-# # Tracks ONLY the ports already added to routing_outline.
|
|
|
-# # This makes duplicate detection O(1) while maintaining identical behavior.
|
|
|
-# routing_outline = set()
|
|
|
-#
|
|
|
-# worklist = [(p, outport.z_functions.get(p, None))
|
|
|
-# for p in outport.outline]
|
|
|
-#
|
|
|
-# for outline, z in worklist:
|
|
|
-# if outline in listeners:
|
|
|
-# # This port is being listened on, so just add it as a fake model
|
|
|
-# fake_port = Port(is_input=False, name="Fake")
|
|
|
-# fake_port.host_DEVS = ExternalWrapper(listeners[outline])
|
|
|
-# outport.routing_outline.append((fake_port, z))
|
|
|
-#
|
|
|
-# # Add it to the current iterating list, so we can just continue
|
|
|
-# if isinstance(outline.host_DEVS, CoupledDEVS):
|
|
|
-# for inline in outline.outline:
|
|
|
-# new_z = appendZ(z, outline.z_functions.get(inline, None))
|
|
|
-# worklist.append((inline, new_z))
|
|
|
-#
|
|
|
-# # If it is a Coupled model, we should just continue
|
|
|
-# # expanding it and not add it to the finished line
|
|
|
-# if not isinstance(inline.host_DEVS, CoupledDEVS):
|
|
|
-# if inline not in routing_outline:
|
|
|
-# outport.routing_outline.append((inline, new_z))
|
|
|
-# routing_outline.add(inline)
|
|
|
-# else:
|
|
|
-# # Atomic model reached, add if not already in routing_outline
|
|
|
-# if outline not in routing_outline:
|
|
|
-# outport.routing_outline.append((outline, z))
|
|
|
-# routing_outline.add(outline)
|
|
|
-
|
|
|
-# ORIGINAL
|
|
|
+
|
|
|
+def clean(outline):
|
|
|
+ text = str(outline)
|
|
|
+ text = text.replace("Coupled", "C")
|
|
|
+ text = text.replace("Atomic", "A")
|
|
|
+ text = text.replace("DEVStoneEnvironment", "E")
|
|
|
+ text = text.replace("_root", "")
|
|
|
+ text = text.replace(", None", "")
|
|
|
+ return text
|
|
|
+
|
|
|
|
|
|
def directConnectPort(outport, listeners):
|
|
|
"""
|
|
|
Perform direct connection on a single port.
|
|
|
|
|
|
+ :param outport: the port to reconnect
|
|
|
+ :param listeners: the listeners that exist, potentially on this port
|
|
|
+ :returns: None
|
|
|
+ """
|
|
|
+ # print("="*246)
|
|
|
+ # print(clean(outport))
|
|
|
+
|
|
|
+ # The new contents of the line
|
|
|
+ outport.routing_outline = []
|
|
|
+ # Tracks ONLY the ports already added to routing_outline.
|
|
|
+ # This makes duplicate detection O(1) while maintaining identical behavior.
|
|
|
+ routing_outline = set()
|
|
|
+
|
|
|
+ worklist = [(p, outport.z_functions.get(p, None))
|
|
|
+ for p in outport.outline]
|
|
|
+
|
|
|
+ for outline, z in worklist:
|
|
|
+ if outline in listeners:
|
|
|
+ # This port is being listened on, so just add it as a fake model
|
|
|
+ fake_port = Port(is_input=False, name="Fake")
|
|
|
+ fake_port.host_DEVS = ExternalWrapper(listeners[outline])
|
|
|
+ outport.routing_outline.append((fake_port, z))
|
|
|
+
|
|
|
+ # Add it to the current iterating list, so we can just continue
|
|
|
+ if isinstance(outline.host_DEVS, CoupledDEVS):
|
|
|
+ for inline in outline.outline:
|
|
|
+ new_z = appendZ(z, outline.z_functions.get(inline, None))
|
|
|
+ worklist.append((inline, new_z))
|
|
|
+ # print(f"Worklist: {clean(str(worklist))}")
|
|
|
+
|
|
|
+ # If it is a Coupled model, we should just continue
|
|
|
+ # expanding it and not add it to the finished line
|
|
|
+ if not isinstance(inline.host_DEVS, CoupledDEVS):
|
|
|
+ if inline not in routing_outline:
|
|
|
+ outport.routing_outline.append((inline, new_z))
|
|
|
+ routing_outline.add(inline)
|
|
|
+ else:
|
|
|
+ # Atomic model reached, add if not already in routing_outline
|
|
|
+ if outline not in routing_outline:
|
|
|
+ outport.routing_outline.append((outline, z))
|
|
|
+ routing_outline.add(outline)
|
|
|
+ # print(clean(outport.routing_outline))
|
|
|
+
|
|
|
+
|
|
|
+def directConnectPortMem(outport, listeners):
|
|
|
+ """
|
|
|
+ Perform direct connection on a single portusing Granular Object Caching.
|
|
|
+
|
|
|
+ :param outport: the port to reconnect
|
|
|
+ :param listeners: the listeners that exist, potentially on this port
|
|
|
+ :returns: None
|
|
|
+ """
|
|
|
+ outport.routing_outline = []
|
|
|
+ routing_outline_set = set()
|
|
|
+
|
|
|
+ def resolve_downstream(current_port):
|
|
|
+ # 1. CACHE HIT
|
|
|
+ if current_port.cached_routes is not None:
|
|
|
+ return current_port.cached_routes
|
|
|
+
|
|
|
+ targets = []
|
|
|
+
|
|
|
+ if current_port in listeners:
|
|
|
+ fake_port = Port(is_input=False, name="Fake")
|
|
|
+ fake_port.host_DEVS = ExternalWrapper(listeners[current_port])
|
|
|
+ targets.append((fake_port, None))
|
|
|
+
|
|
|
+ if isinstance(current_port.host_DEVS, CoupledDEVS):
|
|
|
+ for inline in current_port.outline:
|
|
|
+ local_z = current_port.z_functions.get(inline, None)
|
|
|
+
|
|
|
+ # Recursive Dive
|
|
|
+ for ds_port, ds_z in resolve_downstream(inline):
|
|
|
+ targets.append((ds_port, appendZ(local_z, ds_z)))
|
|
|
+ else:
|
|
|
+ targets.append((current_port, None))
|
|
|
+
|
|
|
+ # 2. CACHE MISS: Save the persistent routes
|
|
|
+ current_port.cached_routes = targets
|
|
|
+ return targets
|
|
|
+
|
|
|
+ # Execute Kickoff
|
|
|
+ for outline in outport.outline:
|
|
|
+ initial_z = outport.z_functions.get(outline, None)
|
|
|
+
|
|
|
+ for target_port, target_z in resolve_downstream(outline):
|
|
|
+ combined_z = appendZ(initial_z, target_z)
|
|
|
+
|
|
|
+ if target_port not in routing_outline_set:
|
|
|
+ outport.routing_outline.append((target_port, combined_z))
|
|
|
+ routing_outline_set.add(target_port)
|
|
|
+
|
|
|
+
|
|
|
+def directConnectPortOLD(outport, listeners):
|
|
|
+ """
|
|
|
+ Perform direct connection on a single port.
|
|
|
+
|
|
|
:param outpurt: the port to reconnect
|
|
|
:param listeners: the listeners that exist, potentially on this port
|
|
|
:returns: None
|
|
|
@@ -1014,7 +1086,7 @@ def directConnectPort(outport, listeners):
|
|
|
for outline, z in worklist:
|
|
|
if outline in listener_keys:
|
|
|
# This port is being listened on, so just add it as a fake model
|
|
|
- fake_port = Port(is_input=False,name="Fake")
|
|
|
+ fake_port = Port(is_input=False, name="Fake")
|
|
|
fake_port.host_DEVS = ExternalWrapper(listeners[outline])
|
|
|
outport.routing_outline.append((fake_port, z))
|
|
|
|