pypdevsbbl.generic.routing module¶
Set of PythonPDEVS blocks for routing messages.
-
class
pypdevsbbl.generic.routing.Finish(name)[source]¶ Bases:
pypdevs.DEVS.AtomicDEVSDestroys a message from the simulation.
Same as a collector (
pypdevsbbl.generic.collectors), only no data was gathered. The routing is just terminated.Note
This block is only added for a better user experience.
Pythonhas no necessity for garbage collection within the PythonPDEVS simulator. It’s a better practice to use this block, but it is not enforced nor required.Parameters: name (str) – The name of the block. Input Ports: input (any) – The message to be destroyed.
-
class
pypdevsbbl.generic.routing.Halt(name)[source]¶ Bases:
pypdevs.DEVS.AtomicDEVSProvides an end-point for the simulation.
Similar to a
Finishblock, but the value will be stored instead of destroyed. It can be used to indicate the end of a simulation, or as a simple single-value collector.Example
sim = Simulator(model) # -- [1] sim.setTerminationCondition(Halt.anyHalted) # -- [2] sim.simulate()
modelis aCoupledDEVSwhich may contain aHaltblock.- Let’s set a termination condition on any
Haltblock. Note that this is decreases the efficiency, as mentioned in the PythonPDEVS documentation.
Note
It is preferred only to use a
Haltblock and its corresponding termination condition when absolutely necessary, but know that this will decrease the overall efficieny of the simulation. More often than not, usage of this block can and should be omitted.Parameters: name (str) – The name of the block. State: The item that arrived. Input Ports: input (any) – The item to store.
-
class
pypdevsbbl.generic.routing.ChooseInput(name: str, n: int = 2, selected: int = 0)[source]¶ Bases:
pypdevs.DEVS.AtomicDEVSChoose a specific input to be opened.
All other inputs will be closed and messages arrived there are discarded. This acts like a “path” that is enabled. The input ports can be indexed with an integer in [0, n).
Parameters: - name (str) – The name of the block.
- n (int) – The amount of input ports there are available in this block. Defaults to 2.
- selected (int) – The initial input port that is enabled.
State: - selected (int) – The currently selected input port to be used.
- item (any) – The item that has arrived on this port.
Input Ports: - select (int) – The index of the new input port to be selected. When not in [0, n), a round robin method is applied.
- inputs (list) – A list of the input ports to choose from. These ports are named input-N, where N represents their identifier.
Output Ports: output (any) – The item that arrived on the currently enabled input port.
-
class
pypdevsbbl.generic.routing.ChooseOutput(name: str, n: int = 2, selected: int = 0)[source]¶ Bases:
pypdevs.DEVS.AtomicDEVSChoose a specific output to be opened.
All other outputs will be closed and messages arrived there are discarded. This acts like a “path” that is enabled. The output ports can be indexed with an integer in [0, n).
See also
Parameters: - name (str) – The name of the block.
- n (int) – The amount of output ports there are available in this block. Defaults to 2.
- selected (int) – The initial output port that is enabled.
State: - selected (int) – The currently selected output port to be used.
- item (any) – The item that needs to be sent through a specific output.
Input Ports: - select (int) – The index of the new input port to be selected. When not in [0, n), a round robin method is applied.
- input (any) – The item that needs to be routed to the currently enabled output port.
Output Ports: outputs (list) – A list of the output ports to choose from. These ports are named output-N, where N represents their identifier.
-
class
pypdevsbbl.generic.routing.Pick(name, n, contents=None, select=<function Pick.<lambda>>, fc=<function Pick.<lambda>>, lif=False)[source]¶ Bases:
pypdevs.DEVS.AtomicDEVSMarks certain paths as available.
This block helps the
ChooseOutputblock to balance the load if each output of that block can be busy. Whenever such an event occurs, any free output is chosen with thePickblock. It is up to the user to prevent access to theChooseOutputblock if no path is free.Note
Whenever no free path is found, no new value is outputted and therefore, the
ChooseOutputwill select the last chosen path.See also
Parameters: - name (str) – The name of the block.
- n (numeric) – The amount of paths to choose from.
- contents (iter) – A sorted iterable of size
n, consisting of a set of booleans. Each value corresponds to the freedom of the path. E.g.[True, False, True]marks path 1 to be busy and paths 0 and 2 to be free. WhenNone, all paths are marked as free. Defaults toNone. - select (def) – Function that selects a path from a list of available
paths. As an argument, a list of pairs is given in the
form of
(port, idle time). By default, the first available path is chosen. - fc (def) – Function that checks if the claim input may mark the
path as busy. Defaults to always
True. - lif (bool) – Indicative that the last port the fallback port is or not. This port cannot be marked busy.
State: - claim (bool) – When
True, the last selected path was marked as busy. - input-n (numeric) – The freedom of the n th input port and its idle time.
Input Ports: - claim (any) – Marks the currently selected path to be busy.
- inputs (list) – A list of the input ports to choose from. These ports are named input-N, where N represents their identifier. When a message arrives, the corresponding path is marked as “free”.
Output Ports: - output (int) – The index of the path to select. Should be used as-is as
an input to the corresponding
ChooseOutputblock. - free (bool) – Outputs
Truewhen there is at least one free path.
-
class
pypdevsbbl.generic.routing.Guard(name: str, n: int = 0, w=<function Guard.<lambda>>, u=<function Guard.<lambda>>)[source]¶ Bases:
pypdevs.DEVS.AtomicDEVSShields sections to a limited amount of resources.
Items that request access should arrive on the input port, whereas items that release their access should arrive on the leave port. When no access is granted, the message will be outputted on the blocked port and otherwise, it is passed on to the guarded port. Items that leave are also outputted on the unguarded port.
Parameters: - name (str) – The name of the block.
- n (int) – Amount of available resources at the start. Defaults to 0.
- w (def) – Weight function for items that enter the block. Takes the item itself as an argument and should return an integer. It may not alter the item.
- u (def) – Weight function for items that leave the block. Takes the item itself as an argument and should return an integer. It may not alter the item.
State: - n (int) – Amount of available resources.
- enter (any) – The item that requested access to the resources.
- leave (any) – The item that releases access to resources.
Input Ports: - input (any) – The items that request access to resources.
- leave (any) – The items that release access to resources.
Output Ports: - blocked (any) – The item that requested access will be outputted here if there are not enough resources for it.
- guarded (any) – Items that were granted access to resources.
- unguarded (any) – Items that released access to the resources.
-
class
pypdevsbbl.generic.routing.Gate(name: str, blocked: bool = True)[source]¶ Bases:
pypdevs.DEVS.AtomicDEVSToggleable access to a section of a model.
Works similar to the
Guard, but does not make use of resources. Instead a boolean value indicates if the message receives access or not.When this value is
True, all items will be outputted on the blocked output, otherwise on the out output.Parameters: - name (str) – The name of the block.
- blocked (bool) – The initial state of the gate.
State: - blocked (bool) – Indicates where the items should be outputted.
- item (any) – The item to be outputted.
Input Ports: - input (any) – The items that want to gain access to the critical section.
- block (bool) – Indicates the state of the gate.
True== closed;False== open
Output Ports: - output (any) – All items that arrived on input if the gate is open.
- blocked (any) – All items that arrived on input if the gate is closed.
-
class
pypdevsbbl.generic.routing._DT(name: str)[source]¶ Bases:
pypdevs.DEVS.AtomicDEVSSuperclass for both the Delayer and the Timer.
Warning
This class should not be used as a block by itself.
Parameters: name (str) – The name of the block.
-
class
pypdevsbbl.generic.routing.Delayer(name: str, ft=<function Delayer.<lambda>>)[source]¶ Bases:
pypdevsbbl.generic.routing._DTHolds a message for a certain delay, as is defined by the item.
Parameters: - name (str) – The name of the block.
- ft (def) – Time delay function that takes the item as argument and returns a numeric value. When the returnvalue is less than 0, INFINITY is assumed. The function musn’t change the item. Defaults to a function that always returns 1.
State: - el (numeric) – The elapsed time since an item is being held.
- dt (numeric) – The time delay to hold an item for. Must be
positive or 0. This is the result of
ft(). - item (any) – The item to hold.
- other (any) – When an item is already being held, store the new item that arrives.
Input Ports: start (any) – Start the timer for the item.
Output Ports: - blocked (any) – All items that arrive when an item is being held.
- finished (any) – The item thet was held for time delay dt.
-
class
pypdevsbbl.generic.routing.Timer(name: str, dt=0.0)[source]¶ Bases:
pypdevsbbl.generic.routing._DTHolds a message for a predefined delay.
Basically corresponds to a blocking queue with a capacity of 1. When there is a requirement for more items. Use a
pypdevsbbl.generic.queues.Queueor apypdevsbbl.generic.queues.SimpleQueuewith an infinite dd and a non-infinte dr.Note
DEVS are evaluated at each event, not at each time instance, meaning that there can be multiple events at the same time. If some of them are external, while others are internal, they will never be displayed in the same timeframe. A timeframe is therefore a collection of events. Each point in time can have multiple timeframes.
Use this block (with a dt of 0) to sync up events that happen out-of-order w.r.t. these timeframes.
Parameters: - name (str) – The name of the block.
- dt (numeric) – The initial delay to hold an item for.
State: - el (numeric) – The elapsed time since an item is being held.
- dt (numeric) – The time delay to hold an item for. Must be positive or 0. Defaults to 0.
- item (any) – The item to hold.
- other (any) – When an item is already being held, store the new item that arrives.
Input Ports: - start (any) – Start the timer for the item.
- dt (numeric) – New time delay to hold an item for. Must be positive or 0. Any other value is discarded.
Output Ports: - blocked (any) – All items that arrive when an item is being held.
- finished (any) – The item thet was held for time delay dt.
-
class
pypdevsbbl.generic.routing.Sync(name, n)[source]¶ Bases:
pypdevs.DEVS.AtomicDEVSSynchronizes a set of connections.
Note
This block does not enqueue the arriving messages and should thus not be used to delay messages that arrive too fast.
Parameters: - name (str) – The name of the block.
- n (int) – The amount of ports to sync. Must be greater than 0.
State: A list of all currently arrived messages.
Output Ports: - input (list) – A list of the input ports to choose from. These ports are named input-N, where N represents their identifier.
- output (list) – A list of the output ports to choose from. These ports are named output-N, where N represents their identifier.