gymwipe.simtools module

Module for simulation tools

class SimulationManager[source]

Bases: object

The SimulationManager offers methods and properties for managing and accessing a SimPy simulation.

Note

Do not create instances on your own. Reference the existing instance by SimMan instead.

env[source]

The SimPy Environment object belonging to the current simulation

nextTimeSlot(timeSlotLength)[source]

Returns a SimPy timeout event that is scheduled for the beginning of the next time slot. A time slot starts whenever now % timeSlotLength is 0.

Parameters:timeSlotLength (float) – The time slot length in seconds
Return type:Event
now[source]

The current simulation time step

Type:int
process(generator)[source]

Registers a SimPy process generator (a generator yielding SimPy events) at the SimPy environment and returns it.

Parameters:process – The generator to be registered as a process
Return type:Process
event()[source]

Creates and returns a new Event object belonging to the current environment.

runSimulation(until)[source]

Runs the simulation (or continues running it) until the amount of simulated time specified by until has passed (with until being a float) or until is triggered (with until being an Event).

init()[source]

Creates a new Environment.

timeout(duration, value=None)[source]

Shorthand for env.timeout(duration, value)

Return type:Event
timeoutUntil(triggerTime, value=None)[source]

Returns a SimPy EventEvent that succeeds at the simulated time specified by triggerTime.

Parameters:
Return type:

Event

triggerAfterTimeout(event, timeout, value=None)[source]

Calls succeed() on the event after the simulated time specified in timeout has passed. If the event has already been triggered by then, no action is taken.

SimMan = <gymwipe.simtools.SimulationManager object>[source]

A globally accessible SimulationManager instance to be used whenever the SimPy simulation is involved

class SourcePrepender(logger)[source]

Bases: logging.LoggerAdapter

A LoggerAdapter that prepends the string representation of a provided object to log messages.

Examples

The following command sets up a Logger that prepends message sources:

logger = SourcePrepender(logging.getLogger(__name__))

Assuming self is the object that logs a message, it could prepend str(self) to a message like this:

logger.info("Something happened", sender=self)

If str(self) is myObject, this example would result in the log message “myObject: Something happened”.

Parameters:logger (Logger) – The Logger instance to be wrapped by the SourcePrepender LoggerAdapter
process(msg, kwargs)[source]

If a sender keyword argument is provided, prepends “obj: ” to msg, with obj being the string representation of the sender keyword argument.

class SimTimePrepender(logger)[source]

Bases: gymwipe.simtools.SourcePrepender

A LoggerAdapter that prepends the current simulation time (fetched by requesting SimMan.now) to any log message sent. Additionally, the sender keyword argument can be used for logging calls, which also prepends a sender to messages like explained in SourcePrepender.

Examples

The following command sets up a Logger that prepends simulation time:

logger = SimTimePrepender(logging.getLogger(__name__))
Parameters:logger (Logger) – The Logger instance to be wrapped by the SimTimePrepender LoggerAdapter
process(msg, kwargs)[source]

Prepends “[Time: x]” to msg, with x being the current simulation time. Additionally, if a sender argument is provided, str(sender) is also prepended to the simulation time.

ensureType(input, validTypes, caller)[source]

Checks whether input is an instance of the type / one of the types provided as validTypes. If not, raises a TypeError with a message containing the string representation of caller.

Parameters:
  • input (Any) – The object for which to check the type
  • validTypes (Union[type, Tuple[type]]) – The type / tuple of types to be allowed
  • caller (Any) – The object that (on type mismatch) will be mentioned in the error message.
Raises:

TypeError – If the type of input mismatches the type(s) specified in validClasses

class Notifier(name='', owner=None)[source]

Bases: object

A class that helps implementing the observer pattern. A Notifier can be triggered providing a value. Both callback functions and SimPy generators can be subscribed. Every time the Notifier is triggered, it will run its callback methods and trigger the execution of the subscribed SimPy generators. Aditionally, SimPy generators can wait for a Notifier to be triggered by yielding its event.

Parameters:
  • name (str) – A string to identify the Notifier instance (e.g. among all other Notifier instances of the owner object)
  • owner (Optional[Any]) – The object that provides the Notifier instance
subscribeCallback(callback, priority=0, additionalArgs=None)[source]

Adds the passed callable to the set of callback functions. Thus, when the Notifier gets triggered, the callable will be invoked passing the value that the Notifier was triggered with.

Note

A callable can only be added once, regardless of its priority.

Parameters:
  • callback (Callable[[Any], None]) – The callable to be subscribed
  • priority (int) – If set, the callable is guaranteed to be invoked only after every callback with a higher priority value has been executed. Callbacks added without a priority value are assumed to have priority 0.
  • additionalArgs (Optional[List[Any]]) – A list of arguments that are passed as further arguments when the callback function is invoked
unsubscribeCallback(callback)[source]

Removes the passed callable from the set of callback functions. Afterwards, it is not triggered anymore by this Notifier.

Parameters:callback (Callable[[Any], None]) – The callable to be removed
subscribeProcess(process, blocking=True, queued=False)[source]

Makes the SimPy environment process the passed generator function when trigger() is called. The value passed to trigger() will also be passed to the generator function.

Parameters:
  • blocking – If set to False, only one instance of the generator will be processed at a time. Thus, if trigger() is called while the SimPy process started by an earlier trigger() call has not terminated, no action is taken.
  • queued – Only relevant if blocking is True. If queued is set to false False, a trigger() call while an instance of the generator is still active will not result in an additional generator execution. If queued is set to True instead, the values of trigger() calls that happen while the subscribed generator is being processed will be queued and as long as the queue is not empty, a new generator instance with a queued value will be processed every time a previous instance has terminated.
trigger(value=None)[source]

Triggers the Notifier. This runs the callbacks, makes the event succeed, and triggers the processing of subscribed SimPy generators.

event[source]

A SimPy event that succeeds when the Notifier is triggered

Type:Event
name[source]

The Notifier’s name as it has been passed to the constructor

Type:str