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 secondsReturn type: Event
-
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
-
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 anEvent
).
-
init
()[source]¶ Creates a new
Environment
.
-
-
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
) – TheLogger
instance to be wrapped by the SourcePrependerLoggerAdapter
-
class
SimTimePrepender
(logger)[source]¶ Bases:
gymwipe.simtools.SourcePrepender
A
LoggerAdapter
that prepends the current simulation time (fetched by requestingSimMan.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 inSourcePrepender
.Examples
The following command sets up a
Logger
that prepends simulation time:logger = SimTimePrepender(logging.getLogger(__name__))
Parameters: logger ( Logger
) – TheLogger
instance to be wrapped by the SimTimePrependerLoggerAdapter
-
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: 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 theNotifier
is triggered, it will run its callback methods and trigger the execution of the subscribed SimPy generators. Aditionally, SimPy generators can wait for aNotifier
to be triggered by yielding itsevent
.Parameters: -
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 theNotifier
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
- callback (
-
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 totrigger()
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, iftrigger()
is called while the SimPy process started by an earliertrigger()
call has not terminated, no action is taken. - queued – Only relevant if blocking is
True
. If queued is set to falseFalse
, atrigger()
call while an instance of the generator is still active will not result in an additional generator execution. If queued is set toTrue
instead, the values oftrigger()
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.
- blocking – If set to
-