simulator

The simulator module defines different simulators that can be used to advance time in a simulation and change the state of the model over time.

class pydsol.core.simulator.Simulator(name: str, time_type: type, initial_time: TIME)[source]

Bases: EventProducer, SimulatorInterface, Generic[TIME]

The Simulator class

property name: str

return the name of the simulator

property time_type: type

return the time type of the simulator

property simulator_time: TIME

return the current absolute time of the simulator

property initial_time: TIME

return the first possible time of the used time type

property replication: ReplicationInterface

return the replication with which the simulator has been initialized, or None when initialize has not yet been called

property model: ModelInterface

return the model that is being simulated, or None when initialize for a model has not yet been called

initialize(model: ModelInterface, replication: ReplicationInterface)[source]

initialize the simulator with a replication for a model

add_initial_method(target, method: str, **kwargs)[source]

Add a method call that has to be performed at the end if initialize, and before the model starts. This can, for instance, be used to schedule the execution of simulation events before initialize has been called, and solved the problem that, for discrete event simulators, the scheduleEvent(…) methods cannot be called before initialize().

cleanup()[source]

clean up after a replication has finished, and prepare for the next replication to run

start()[source]

Starts the simulator, and fire a START_EVENT that the simulator was started. Note that when the simulator was already started an exception will be thrown, and no event will be fired. The start uses the RunUntil property with a value of the end time of the replication when starting the simulator.

step()[source]

Steps the simulator, and fire a STEP_EVENT to indicate the simulator made a step. Note that when the simulator is running an exception will be thrown, and no event will be fired.

stop()[source]

Stops the simulator, and fire a STOP_EVENT that the simulator was stopped. Note that when the simulator was already stopped an exception will be thrown, and no event will be fired.

run_up_to(stop_time: TIME)[source]

Runs the simulator up to a certain time; any events at that time, or the solving of the differential equation at that timestep, will not yet be executed.

run_up_to_including(stop_time: TIME)[source]

Runs the simulator up to a certain time; all events at that time, or the solving of the differential equation at that timestep, will be executed.

warmup()[source]
property run_state: RunState

return the run state of the simulator

is_initialized() bool[source]

Return whether the simulator has been initialized with a replication for a model.

is_starting_or_running() bool[source]

Return whether the simulator is starting or has started.

is_stopping_or_stopped() bool[source]

Return whether the simulator is stopping or has been stopped. This method also returns True when the simulator has not yet been initialized, or when the model has not yet started, or when the model run has ended.

property replication_state: ReplicationState

return the replication state

end_replication()[source]
set_error_strategy(error_strategy: ErrorStrategy, log_level: int = -1)[source]

Set a new error handling strategy for the simulator, and possibly override the default log level belonging to the error strategy (e.g. with logging.NONE to suppress logging altogether).

STARTING_EVENT: EventType = EventType[SimulatorInterface.STARTING_EVENT metadata={self._metadata}]
START_EVENT: EventType = EventType[SimulatorInterface.START_EVENT metadata={self._metadata}]
STOPPING_EVENT: EventType = EventType[SimulatorInterface.STOPPING_EVENT metadata={self._metadata}]
STOP_EVENT: EventType = EventType[SimulatorInterface.STOP_EVENT metadata={self._metadata}]
TIME_CHANGED_EVENT: EventType = EventType[SimulatorInterface.TIME_CHANGED_EVENT metadata={self._metadata}]
add_listener(event_type: EventType, listener: EventListener)

Add an EventListener to this EventProducer for a given EventType. If the listener already is registered for this EventType, this will be ignored.

Parameters:
  • event_type (EventType) – the EventType for which this listener subscribes

  • listener (EventListener) – the subscriber to register for the provided Eventtype

Raises:

EventError – if any of the arguments is of the wrong type

fire(event_type: EventType, content, check: bool = True)

construct an event based on the arguments and fire this event to the subscribed listeners for the event_type

Parameters:
  • event_type (EventType) – a reference to a (usually static) event type for identification

  • content – the payload of the event, which can be of any type; common types are list, dict, or simple types

  • check (bool, optional) – whether to check the fields in the content in case the event_type has metadata; the check whether the content is a dict is always checked when there is metadata

Raises:
  • EventError – if event_type is not an EventType

  • EventError – if the EventType specified metadata and the content is not a dict

  • EventError – if the dict content is not consistent with the EventType metadata

fire_event(event: Event)

fire this event to the subscribed listeners for the EventType of the event.

Parameters:

event (Event) – the event to fire to the subscribed listeners

Raises:

EventError – if the event is not of the right type

fire_timed(time: int | float, event_type: EventType, content, check: bool = True)

construct a timed event based on the arguments and fire this event to the subscribed listeners for the event_type

Parameters:
  • timestamp (int or float) – the timestamp of the event, typically the simulator time

  • event_type (EventType) – a reference to a (usually static) event type for identification

  • content – the payload of the event, which can be of any type; common types are list, dict, or simple types

  • check (bool, optional) – whether to check the fields in the content in case the event_type has metadata; the check whether the content is a dict is always checked when there is metadata

Raises:
  • EventError – if timestamp is not of type int or float

  • EventError – if event_type is not an EventType

  • EventError – if the EventType specified metadata and the content is not a dict

  • EventError – if the dict content is not consistent with the EventType metadata

fire_timed_event(timed_event: TimedEvent)

fire this timed_event to the subscribed listeners for the EventType of the event.

Parameters:

event (TimedEvent) – the timed_event to fire to the subscribed listeners

Raises:

EventError – if the timed_event is not of the right type

has_listeners() bool

indicate whether this producer has any listeners or not

remove_all_listeners(event_type: EventType | None = None, listener: EventListener | None = None)

Remove an EventListener (if given) for a provided EventType (if given) for this EventProducer. It is no problem if there are no matches. There are four situations:

event_type == None and listener == None

all listeners for all event types are removed

event_type == None and listener is specified

the listener is removed for any event for which it was registered

event_type is specified and listener == None

all listeners are removed for the given event_type

event_type and listener are both specified

the listener for the given event type is removed, if it was registered; in essence this is the same as remove_listener

Parameters:
  • event_type (EventType, optional) – the EventType for which this listener unsubscribes

  • listener (EventListener, optional) – the subscriber to remove for the provided EventType

Raises:

EventError – if any of the arguments is of the wrong type

remove_listener(event_type: EventType, listener: EventListener)

Remove an EventListener of this EventProducer for a given EventType. If the listener is not registered for this EventType, this will be ignored.

Parameters:
  • event_type (EventType) – the EventType for which this listener unsubscribes

  • listener (EventListener) – the subscriber to remove for the provided Eventtype

Raises:

EventError – if any of the arguments is of the wrong type

class pydsol.core.simulator.DEVSSimulator(name: str, time_type: type, initial_time: TIME)[source]

Bases: Simulator[TIME], Generic[TIME]

STARTING_EVENT: EventType = EventType[SimulatorInterface.STARTING_EVENT metadata={self._metadata}]
START_EVENT: EventType = EventType[SimulatorInterface.START_EVENT metadata={self._metadata}]
STOPPING_EVENT: EventType = EventType[SimulatorInterface.STOPPING_EVENT metadata={self._metadata}]
STOP_EVENT: EventType = EventType[SimulatorInterface.STOP_EVENT metadata={self._metadata}]
TIME_CHANGED_EVENT: EventType = EventType[SimulatorInterface.TIME_CHANGED_EVENT metadata={self._metadata}]
add_initial_method(target, method: str, **kwargs)

Add a method call that has to be performed at the end if initialize, and before the model starts. This can, for instance, be used to schedule the execution of simulation events before initialize has been called, and solved the problem that, for discrete event simulators, the scheduleEvent(…) methods cannot be called before initialize().

add_listener(event_type: EventType, listener: EventListener)

Add an EventListener to this EventProducer for a given EventType. If the listener already is registered for this EventType, this will be ignored.

Parameters:
  • event_type (EventType) – the EventType for which this listener subscribes

  • listener (EventListener) – the subscriber to register for the provided Eventtype

Raises:

EventError – if any of the arguments is of the wrong type

cleanup()

clean up after a replication has finished, and prepare for the next replication to run

fire(event_type: EventType, content, check: bool = True)

construct an event based on the arguments and fire this event to the subscribed listeners for the event_type

Parameters:
  • event_type (EventType) – a reference to a (usually static) event type for identification

  • content – the payload of the event, which can be of any type; common types are list, dict, or simple types

  • check (bool, optional) – whether to check the fields in the content in case the event_type has metadata; the check whether the content is a dict is always checked when there is metadata

Raises:
  • EventError – if event_type is not an EventType

  • EventError – if the EventType specified metadata and the content is not a dict

  • EventError – if the dict content is not consistent with the EventType metadata

fire_event(event: Event)

fire this event to the subscribed listeners for the EventType of the event.

Parameters:

event (Event) – the event to fire to the subscribed listeners

Raises:

EventError – if the event is not of the right type

fire_timed(time: int | float, event_type: EventType, content, check: bool = True)

construct a timed event based on the arguments and fire this event to the subscribed listeners for the event_type

Parameters:
  • timestamp (int or float) – the timestamp of the event, typically the simulator time

  • event_type (EventType) – a reference to a (usually static) event type for identification

  • content – the payload of the event, which can be of any type; common types are list, dict, or simple types

  • check (bool, optional) – whether to check the fields in the content in case the event_type has metadata; the check whether the content is a dict is always checked when there is metadata

Raises:
  • EventError – if timestamp is not of type int or float

  • EventError – if event_type is not an EventType

  • EventError – if the EventType specified metadata and the content is not a dict

  • EventError – if the dict content is not consistent with the EventType metadata

fire_timed_event(timed_event: TimedEvent)

fire this timed_event to the subscribed listeners for the EventType of the event.

Parameters:

event (TimedEvent) – the timed_event to fire to the subscribed listeners

Raises:

EventError – if the timed_event is not of the right type

has_listeners() bool

indicate whether this producer has any listeners or not

property initial_time: TIME

return the first possible time of the used time type

is_initialized() bool

Return whether the simulator has been initialized with a replication for a model.

is_starting_or_running() bool

Return whether the simulator is starting or has started.

is_stopping_or_stopped() bool

Return whether the simulator is stopping or has been stopped. This method also returns True when the simulator has not yet been initialized, or when the model has not yet started, or when the model run has ended.

property model: ModelInterface

return the model that is being simulated, or None when initialize for a model has not yet been called

property name: str

return the name of the simulator

remove_all_listeners(event_type: EventType | None = None, listener: EventListener | None = None)

Remove an EventListener (if given) for a provided EventType (if given) for this EventProducer. It is no problem if there are no matches. There are four situations:

event_type == None and listener == None

all listeners for all event types are removed

event_type == None and listener is specified

the listener is removed for any event for which it was registered

event_type is specified and listener == None

all listeners are removed for the given event_type

event_type and listener are both specified

the listener for the given event type is removed, if it was registered; in essence this is the same as remove_listener

Parameters:
  • event_type (EventType, optional) – the EventType for which this listener unsubscribes

  • listener (EventListener, optional) – the subscriber to remove for the provided EventType

Raises:

EventError – if any of the arguments is of the wrong type

remove_listener(event_type: EventType, listener: EventListener)

Remove an EventListener of this EventProducer for a given EventType. If the listener is not registered for this EventType, this will be ignored.

Parameters:
  • event_type (EventType) – the EventType for which this listener unsubscribes

  • listener (EventListener) – the subscriber to remove for the provided Eventtype

Raises:

EventError – if any of the arguments is of the wrong type

property replication: ReplicationInterface

return the replication with which the simulator has been initialized, or None when initialize has not yet been called

property replication_state: ReplicationState

return the replication state

property run_state: RunState

return the run state of the simulator

run_up_to(stop_time: TIME)

Runs the simulator up to a certain time; any events at that time, or the solving of the differential equation at that timestep, will not yet be executed.

run_up_to_including(stop_time: TIME)

Runs the simulator up to a certain time; all events at that time, or the solving of the differential equation at that timestep, will be executed.

set_error_strategy(error_strategy: ErrorStrategy, log_level: int = -1)

Set a new error handling strategy for the simulator, and possibly override the default log level belonging to the error strategy (e.g. with logging.NONE to suppress logging altogether).

property simulator_time: TIME

return the current absolute time of the simulator

start()

Starts the simulator, and fire a START_EVENT that the simulator was started. Note that when the simulator was already started an exception will be thrown, and no event will be fired. The start uses the RunUntil property with a value of the end time of the replication when starting the simulator.

step()

Steps the simulator, and fire a STEP_EVENT to indicate the simulator made a step. Note that when the simulator is running an exception will be thrown, and no event will be fired.

stop()

Stops the simulator, and fire a STOP_EVENT that the simulator was stopped. Note that when the simulator was already stopped an exception will be thrown, and no event will be fired.

property time_type: type

return the time type of the simulator

warmup()
initialize(model: ModelInterface, replication: ReplicationInterface)[source]

initialize the simulator with a replication for a model

eventlist() EventListInterface[source]

Return the event list used by this simulator.

schedule_event(event: SimEventInterface) SimEventInterface[source]

schedule the provided event on the event list

schedule_event_now(target, method: str, priority: int = 5, **kwargs) SimEventInterface[source]

schedule a method call at the current simulator time

schedule_event_rel(delay, target, method: str, priority: int = 5, **kwargs) SimEventInterface[source]

schedule a methodCall at a relative duration. The execution time is thus simulator.simulator_time + delay.

schedule_event_abs(time, target, method: str, priority: int = 5, **kwargs) SimEventInterface[source]

schedule a methodCall at a relative duration. The execution time is thus simulator.simulator_time + delay.

cancel_event(event: SimEventInterface)[source]

remove the provided event from the event list

end_replication()[source]
class pydsol.core.simulator.DEVSSimulatorFloat(name: str)[source]

Bases: DEVSSimulator[float]

STARTING_EVENT: EventType = EventType[SimulatorInterface.STARTING_EVENT metadata={self._metadata}]
START_EVENT: EventType = EventType[SimulatorInterface.START_EVENT metadata={self._metadata}]
STOPPING_EVENT: EventType = EventType[SimulatorInterface.STOPPING_EVENT metadata={self._metadata}]
STOP_EVENT: EventType = EventType[SimulatorInterface.STOP_EVENT metadata={self._metadata}]
TIME_CHANGED_EVENT: EventType = EventType[SimulatorInterface.TIME_CHANGED_EVENT metadata={self._metadata}]
add_initial_method(target, method: str, **kwargs)

Add a method call that has to be performed at the end if initialize, and before the model starts. This can, for instance, be used to schedule the execution of simulation events before initialize has been called, and solved the problem that, for discrete event simulators, the scheduleEvent(…) methods cannot be called before initialize().

add_listener(event_type: EventType, listener: EventListener)

Add an EventListener to this EventProducer for a given EventType. If the listener already is registered for this EventType, this will be ignored.

Parameters:
  • event_type (EventType) – the EventType for which this listener subscribes

  • listener (EventListener) – the subscriber to register for the provided Eventtype

Raises:

EventError – if any of the arguments is of the wrong type

cancel_event(event: SimEventInterface)

remove the provided event from the event list

cleanup()

clean up after a replication has finished, and prepare for the next replication to run

end_replication()
eventlist() EventListInterface

Return the event list used by this simulator.

fire(event_type: EventType, content, check: bool = True)

construct an event based on the arguments and fire this event to the subscribed listeners for the event_type

Parameters:
  • event_type (EventType) – a reference to a (usually static) event type for identification

  • content – the payload of the event, which can be of any type; common types are list, dict, or simple types

  • check (bool, optional) – whether to check the fields in the content in case the event_type has metadata; the check whether the content is a dict is always checked when there is metadata

Raises:
  • EventError – if event_type is not an EventType

  • EventError – if the EventType specified metadata and the content is not a dict

  • EventError – if the dict content is not consistent with the EventType metadata

fire_event(event: Event)

fire this event to the subscribed listeners for the EventType of the event.

Parameters:

event (Event) – the event to fire to the subscribed listeners

Raises:

EventError – if the event is not of the right type

fire_timed(time: int | float, event_type: EventType, content, check: bool = True)

construct a timed event based on the arguments and fire this event to the subscribed listeners for the event_type

Parameters:
  • timestamp (int or float) – the timestamp of the event, typically the simulator time

  • event_type (EventType) – a reference to a (usually static) event type for identification

  • content – the payload of the event, which can be of any type; common types are list, dict, or simple types

  • check (bool, optional) – whether to check the fields in the content in case the event_type has metadata; the check whether the content is a dict is always checked when there is metadata

Raises:
  • EventError – if timestamp is not of type int or float

  • EventError – if event_type is not an EventType

  • EventError – if the EventType specified metadata and the content is not a dict

  • EventError – if the dict content is not consistent with the EventType metadata

fire_timed_event(timed_event: TimedEvent)

fire this timed_event to the subscribed listeners for the EventType of the event.

Parameters:

event (TimedEvent) – the timed_event to fire to the subscribed listeners

Raises:

EventError – if the timed_event is not of the right type

has_listeners() bool

indicate whether this producer has any listeners or not

property initial_time: TIME

return the first possible time of the used time type

initialize(model: ModelInterface, replication: ReplicationInterface)

initialize the simulator with a replication for a model

is_initialized() bool

Return whether the simulator has been initialized with a replication for a model.

is_starting_or_running() bool

Return whether the simulator is starting or has started.

is_stopping_or_stopped() bool

Return whether the simulator is stopping or has been stopped. This method also returns True when the simulator has not yet been initialized, or when the model has not yet started, or when the model run has ended.

property model: ModelInterface

return the model that is being simulated, or None when initialize for a model has not yet been called

property name: str

return the name of the simulator

remove_all_listeners(event_type: EventType | None = None, listener: EventListener | None = None)

Remove an EventListener (if given) for a provided EventType (if given) for this EventProducer. It is no problem if there are no matches. There are four situations:

event_type == None and listener == None

all listeners for all event types are removed

event_type == None and listener is specified

the listener is removed for any event for which it was registered

event_type is specified and listener == None

all listeners are removed for the given event_type

event_type and listener are both specified

the listener for the given event type is removed, if it was registered; in essence this is the same as remove_listener

Parameters:
  • event_type (EventType, optional) – the EventType for which this listener unsubscribes

  • listener (EventListener, optional) – the subscriber to remove for the provided EventType

Raises:

EventError – if any of the arguments is of the wrong type

remove_listener(event_type: EventType, listener: EventListener)

Remove an EventListener of this EventProducer for a given EventType. If the listener is not registered for this EventType, this will be ignored.

Parameters:
  • event_type (EventType) – the EventType for which this listener unsubscribes

  • listener (EventListener) – the subscriber to remove for the provided Eventtype

Raises:

EventError – if any of the arguments is of the wrong type

property replication: ReplicationInterface

return the replication with which the simulator has been initialized, or None when initialize has not yet been called

property replication_state: ReplicationState

return the replication state

property run_state: RunState

return the run state of the simulator

run_up_to(stop_time: TIME)

Runs the simulator up to a certain time; any events at that time, or the solving of the differential equation at that timestep, will not yet be executed.

run_up_to_including(stop_time: TIME)

Runs the simulator up to a certain time; all events at that time, or the solving of the differential equation at that timestep, will be executed.

schedule_event(event: SimEventInterface) SimEventInterface

schedule the provided event on the event list

schedule_event_abs(time, target, method: str, priority: int = 5, **kwargs) SimEventInterface

schedule a methodCall at a relative duration. The execution time is thus simulator.simulator_time + delay.

schedule_event_now(target, method: str, priority: int = 5, **kwargs) SimEventInterface

schedule a method call at the current simulator time

schedule_event_rel(delay, target, method: str, priority: int = 5, **kwargs) SimEventInterface

schedule a methodCall at a relative duration. The execution time is thus simulator.simulator_time + delay.

set_error_strategy(error_strategy: ErrorStrategy, log_level: int = -1)

Set a new error handling strategy for the simulator, and possibly override the default log level belonging to the error strategy (e.g. with logging.NONE to suppress logging altogether).

property simulator_time: TIME

return the current absolute time of the simulator

start()

Starts the simulator, and fire a START_EVENT that the simulator was started. Note that when the simulator was already started an exception will be thrown, and no event will be fired. The start uses the RunUntil property with a value of the end time of the replication when starting the simulator.

step()

Steps the simulator, and fire a STEP_EVENT to indicate the simulator made a step. Note that when the simulator is running an exception will be thrown, and no event will be fired.

stop()

Stops the simulator, and fire a STOP_EVENT that the simulator was stopped. Note that when the simulator was already stopped an exception will be thrown, and no event will be fired.

property time_type: type

return the time type of the simulator

warmup()
class pydsol.core.simulator.DEVSSimulatorInt(name: str)[source]

Bases: DEVSSimulator[int]

STARTING_EVENT: EventType = EventType[SimulatorInterface.STARTING_EVENT metadata={self._metadata}]
START_EVENT: EventType = EventType[SimulatorInterface.START_EVENT metadata={self._metadata}]
STOPPING_EVENT: EventType = EventType[SimulatorInterface.STOPPING_EVENT metadata={self._metadata}]
STOP_EVENT: EventType = EventType[SimulatorInterface.STOP_EVENT metadata={self._metadata}]
TIME_CHANGED_EVENT: EventType = EventType[SimulatorInterface.TIME_CHANGED_EVENT metadata={self._metadata}]
add_initial_method(target, method: str, **kwargs)

Add a method call that has to be performed at the end if initialize, and before the model starts. This can, for instance, be used to schedule the execution of simulation events before initialize has been called, and solved the problem that, for discrete event simulators, the scheduleEvent(…) methods cannot be called before initialize().

add_listener(event_type: EventType, listener: EventListener)

Add an EventListener to this EventProducer for a given EventType. If the listener already is registered for this EventType, this will be ignored.

Parameters:
  • event_type (EventType) – the EventType for which this listener subscribes

  • listener (EventListener) – the subscriber to register for the provided Eventtype

Raises:

EventError – if any of the arguments is of the wrong type

cancel_event(event: SimEventInterface)

remove the provided event from the event list

cleanup()

clean up after a replication has finished, and prepare for the next replication to run

end_replication()
eventlist() EventListInterface

Return the event list used by this simulator.

fire(event_type: EventType, content, check: bool = True)

construct an event based on the arguments and fire this event to the subscribed listeners for the event_type

Parameters:
  • event_type (EventType) – a reference to a (usually static) event type for identification

  • content – the payload of the event, which can be of any type; common types are list, dict, or simple types

  • check (bool, optional) – whether to check the fields in the content in case the event_type has metadata; the check whether the content is a dict is always checked when there is metadata

Raises:
  • EventError – if event_type is not an EventType

  • EventError – if the EventType specified metadata and the content is not a dict

  • EventError – if the dict content is not consistent with the EventType metadata

fire_event(event: Event)

fire this event to the subscribed listeners for the EventType of the event.

Parameters:

event (Event) – the event to fire to the subscribed listeners

Raises:

EventError – if the event is not of the right type

fire_timed(time: int | float, event_type: EventType, content, check: bool = True)

construct a timed event based on the arguments and fire this event to the subscribed listeners for the event_type

Parameters:
  • timestamp (int or float) – the timestamp of the event, typically the simulator time

  • event_type (EventType) – a reference to a (usually static) event type for identification

  • content – the payload of the event, which can be of any type; common types are list, dict, or simple types

  • check (bool, optional) – whether to check the fields in the content in case the event_type has metadata; the check whether the content is a dict is always checked when there is metadata

Raises:
  • EventError – if timestamp is not of type int or float

  • EventError – if event_type is not an EventType

  • EventError – if the EventType specified metadata and the content is not a dict

  • EventError – if the dict content is not consistent with the EventType metadata

fire_timed_event(timed_event: TimedEvent)

fire this timed_event to the subscribed listeners for the EventType of the event.

Parameters:

event (TimedEvent) – the timed_event to fire to the subscribed listeners

Raises:

EventError – if the timed_event is not of the right type

has_listeners() bool

indicate whether this producer has any listeners or not

property initial_time: TIME

return the first possible time of the used time type

initialize(model: ModelInterface, replication: ReplicationInterface)

initialize the simulator with a replication for a model

is_initialized() bool

Return whether the simulator has been initialized with a replication for a model.

is_starting_or_running() bool

Return whether the simulator is starting or has started.

is_stopping_or_stopped() bool

Return whether the simulator is stopping or has been stopped. This method also returns True when the simulator has not yet been initialized, or when the model has not yet started, or when the model run has ended.

property model: ModelInterface

return the model that is being simulated, or None when initialize for a model has not yet been called

property name: str

return the name of the simulator

remove_all_listeners(event_type: EventType | None = None, listener: EventListener | None = None)

Remove an EventListener (if given) for a provided EventType (if given) for this EventProducer. It is no problem if there are no matches. There are four situations:

event_type == None and listener == None

all listeners for all event types are removed

event_type == None and listener is specified

the listener is removed for any event for which it was registered

event_type is specified and listener == None

all listeners are removed for the given event_type

event_type and listener are both specified

the listener for the given event type is removed, if it was registered; in essence this is the same as remove_listener

Parameters:
  • event_type (EventType, optional) – the EventType for which this listener unsubscribes

  • listener (EventListener, optional) – the subscriber to remove for the provided EventType

Raises:

EventError – if any of the arguments is of the wrong type

remove_listener(event_type: EventType, listener: EventListener)

Remove an EventListener of this EventProducer for a given EventType. If the listener is not registered for this EventType, this will be ignored.

Parameters:
  • event_type (EventType) – the EventType for which this listener unsubscribes

  • listener (EventListener) – the subscriber to remove for the provided Eventtype

Raises:

EventError – if any of the arguments is of the wrong type

property replication: ReplicationInterface

return the replication with which the simulator has been initialized, or None when initialize has not yet been called

property replication_state: ReplicationState

return the replication state

property run_state: RunState

return the run state of the simulator

run_up_to(stop_time: TIME)

Runs the simulator up to a certain time; any events at that time, or the solving of the differential equation at that timestep, will not yet be executed.

run_up_to_including(stop_time: TIME)

Runs the simulator up to a certain time; all events at that time, or the solving of the differential equation at that timestep, will be executed.

schedule_event(event: SimEventInterface) SimEventInterface

schedule the provided event on the event list

schedule_event_abs(time, target, method: str, priority: int = 5, **kwargs) SimEventInterface

schedule a methodCall at a relative duration. The execution time is thus simulator.simulator_time + delay.

schedule_event_now(target, method: str, priority: int = 5, **kwargs) SimEventInterface

schedule a method call at the current simulator time

schedule_event_rel(delay, target, method: str, priority: int = 5, **kwargs) SimEventInterface

schedule a methodCall at a relative duration. The execution time is thus simulator.simulator_time + delay.

set_error_strategy(error_strategy: ErrorStrategy, log_level: int = -1)

Set a new error handling strategy for the simulator, and possibly override the default log level belonging to the error strategy (e.g. with logging.NONE to suppress logging altogether).

property simulator_time: TIME

return the current absolute time of the simulator

start()

Starts the simulator, and fire a START_EVENT that the simulator was started. Note that when the simulator was already started an exception will be thrown, and no event will be fired. The start uses the RunUntil property with a value of the end time of the replication when starting the simulator.

step()

Steps the simulator, and fire a STEP_EVENT to indicate the simulator made a step. Note that when the simulator is running an exception will be thrown, and no event will be fired.

stop()

Stops the simulator, and fire a STOP_EVENT that the simulator was stopped. Note that when the simulator was already stopped an exception will be thrown, and no event will be fired.

property time_type: type

return the time type of the simulator

warmup()
class pydsol.core.simulator.DEVSSimulatorDuration(name: str, display_unit: str = 's')[source]

Bases: DEVSSimulator[Duration]

STARTING_EVENT: EventType = EventType[SimulatorInterface.STARTING_EVENT metadata={self._metadata}]
START_EVENT: EventType = EventType[SimulatorInterface.START_EVENT metadata={self._metadata}]
STOPPING_EVENT: EventType = EventType[SimulatorInterface.STOPPING_EVENT metadata={self._metadata}]
STOP_EVENT: EventType = EventType[SimulatorInterface.STOP_EVENT metadata={self._metadata}]
TIME_CHANGED_EVENT: EventType = EventType[SimulatorInterface.TIME_CHANGED_EVENT metadata={self._metadata}]
add_initial_method(target, method: str, **kwargs)

Add a method call that has to be performed at the end if initialize, and before the model starts. This can, for instance, be used to schedule the execution of simulation events before initialize has been called, and solved the problem that, for discrete event simulators, the scheduleEvent(…) methods cannot be called before initialize().

add_listener(event_type: EventType, listener: EventListener)

Add an EventListener to this EventProducer for a given EventType. If the listener already is registered for this EventType, this will be ignored.

Parameters:
  • event_type (EventType) – the EventType for which this listener subscribes

  • listener (EventListener) – the subscriber to register for the provided Eventtype

Raises:

EventError – if any of the arguments is of the wrong type

cancel_event(event: SimEventInterface)

remove the provided event from the event list

cleanup()

clean up after a replication has finished, and prepare for the next replication to run

end_replication()
eventlist() EventListInterface

Return the event list used by this simulator.

fire(event_type: EventType, content, check: bool = True)

construct an event based on the arguments and fire this event to the subscribed listeners for the event_type

Parameters:
  • event_type (EventType) – a reference to a (usually static) event type for identification

  • content – the payload of the event, which can be of any type; common types are list, dict, or simple types

  • check (bool, optional) – whether to check the fields in the content in case the event_type has metadata; the check whether the content is a dict is always checked when there is metadata

Raises:
  • EventError – if event_type is not an EventType

  • EventError – if the EventType specified metadata and the content is not a dict

  • EventError – if the dict content is not consistent with the EventType metadata

fire_event(event: Event)

fire this event to the subscribed listeners for the EventType of the event.

Parameters:

event (Event) – the event to fire to the subscribed listeners

Raises:

EventError – if the event is not of the right type

fire_timed(time: int | float, event_type: EventType, content, check: bool = True)

construct a timed event based on the arguments and fire this event to the subscribed listeners for the event_type

Parameters:
  • timestamp (int or float) – the timestamp of the event, typically the simulator time

  • event_type (EventType) – a reference to a (usually static) event type for identification

  • content – the payload of the event, which can be of any type; common types are list, dict, or simple types

  • check (bool, optional) – whether to check the fields in the content in case the event_type has metadata; the check whether the content is a dict is always checked when there is metadata

Raises:
  • EventError – if timestamp is not of type int or float

  • EventError – if event_type is not an EventType

  • EventError – if the EventType specified metadata and the content is not a dict

  • EventError – if the dict content is not consistent with the EventType metadata

fire_timed_event(timed_event: TimedEvent)

fire this timed_event to the subscribed listeners for the EventType of the event.

Parameters:

event (TimedEvent) – the timed_event to fire to the subscribed listeners

Raises:

EventError – if the timed_event is not of the right type

has_listeners() bool

indicate whether this producer has any listeners or not

property initial_time: TIME

return the first possible time of the used time type

initialize(model: ModelInterface, replication: ReplicationInterface)

initialize the simulator with a replication for a model

is_initialized() bool

Return whether the simulator has been initialized with a replication for a model.

is_starting_or_running() bool

Return whether the simulator is starting or has started.

is_stopping_or_stopped() bool

Return whether the simulator is stopping or has been stopped. This method also returns True when the simulator has not yet been initialized, or when the model has not yet started, or when the model run has ended.

property model: ModelInterface

return the model that is being simulated, or None when initialize for a model has not yet been called

property name: str

return the name of the simulator

remove_all_listeners(event_type: EventType | None = None, listener: EventListener | None = None)

Remove an EventListener (if given) for a provided EventType (if given) for this EventProducer. It is no problem if there are no matches. There are four situations:

event_type == None and listener == None

all listeners for all event types are removed

event_type == None and listener is specified

the listener is removed for any event for which it was registered

event_type is specified and listener == None

all listeners are removed for the given event_type

event_type and listener are both specified

the listener for the given event type is removed, if it was registered; in essence this is the same as remove_listener

Parameters:
  • event_type (EventType, optional) – the EventType for which this listener unsubscribes

  • listener (EventListener, optional) – the subscriber to remove for the provided EventType

Raises:

EventError – if any of the arguments is of the wrong type

remove_listener(event_type: EventType, listener: EventListener)

Remove an EventListener of this EventProducer for a given EventType. If the listener is not registered for this EventType, this will be ignored.

Parameters:
  • event_type (EventType) – the EventType for which this listener unsubscribes

  • listener (EventListener) – the subscriber to remove for the provided Eventtype

Raises:

EventError – if any of the arguments is of the wrong type

property replication: ReplicationInterface

return the replication with which the simulator has been initialized, or None when initialize has not yet been called

property replication_state: ReplicationState

return the replication state

property run_state: RunState

return the run state of the simulator

run_up_to(stop_time: TIME)

Runs the simulator up to a certain time; any events at that time, or the solving of the differential equation at that timestep, will not yet be executed.

run_up_to_including(stop_time: TIME)

Runs the simulator up to a certain time; all events at that time, or the solving of the differential equation at that timestep, will be executed.

schedule_event(event: SimEventInterface) SimEventInterface

schedule the provided event on the event list

schedule_event_abs(time, target, method: str, priority: int = 5, **kwargs) SimEventInterface

schedule a methodCall at a relative duration. The execution time is thus simulator.simulator_time + delay.

schedule_event_now(target, method: str, priority: int = 5, **kwargs) SimEventInterface

schedule a method call at the current simulator time

schedule_event_rel(delay, target, method: str, priority: int = 5, **kwargs) SimEventInterface

schedule a methodCall at a relative duration. The execution time is thus simulator.simulator_time + delay.

set_error_strategy(error_strategy: ErrorStrategy, log_level: int = -1)

Set a new error handling strategy for the simulator, and possibly override the default log level belonging to the error strategy (e.g. with logging.NONE to suppress logging altogether).

property simulator_time: TIME

return the current absolute time of the simulator

start()

Starts the simulator, and fire a START_EVENT that the simulator was started. Note that when the simulator was already started an exception will be thrown, and no event will be fired. The start uses the RunUntil property with a value of the end time of the replication when starting the simulator.

step()

Steps the simulator, and fire a STEP_EVENT to indicate the simulator made a step. Note that when the simulator is running an exception will be thrown, and no event will be fired.

stop()

Stops the simulator, and fire a STOP_EVENT that the simulator was stopped. Note that when the simulator was already stopped an exception will be thrown, and no event will be fired.

property time_type: type

return the time type of the simulator

warmup()
class pydsol.core.simulator.RunState(value)[source]

Bases: Enum

RunState indicates the precise state of the Simulator.

NOT_INITIALIZED = 1

“The simulator has been instantiated, but not yet initialized with a Replication

INITIALIZED = 2

The replication has started, and the simulator has been initialized, but it has not been started yet

STARTING = 3

The Simulator has been started, but the run() thread did not start yet

STARTED = 4

The Simulator run() thread has started; the simulation is running

STOPPING = 5

The stopping of the simulator has been initiated, but the run() thread is still running

STOPPED = 6

The Simulator run() thread has been stopped; the simulator is not running

ENDED = 7

The replication has ended, and the simulator cannot be restarted

class pydsol.core.simulator.ReplicationState(value)[source]

Bases: Enum

ReplicationState indicates the precise state of the replication that is being executed by the simulator.

NOT_INITIALIZED = 1

“The simulator has been instantiated, but not yet initialized with a Replication

INITIALIZED = 2

The simulator has been initialized with the replication, but it has not been started yet, and the the START_REPLICATION_EVENT has not yet been fired.

STARTED = 3

The execution of the replication has started, and the START_REPLICATION_EVENT has been fired

ENDING = 4

The replication has ended, but the run() thread is still running; the END_REPLICATION_EVENT has not yet been fired

ENDED = 5

The replication has ended, and the simulator cannot be restarted; the END_REPLICATION_EVENT has been fired.

class pydsol.core.simulator.SimulatorWorkerThread(name: str, job: Simulator)[source]

Bases: Thread

The Simulator uses a worker thread to execute the simulation. The reason to use a worker thread is to not block execution of user interface activities while the simulation is running. In an interactive setting, a user can hereby stop() the simulator and inspect the state of the simulation model, and continue the simulation by calling start().

property daemon

A boolean value indicating whether this thread is a daemon thread.

This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.

The entire Python program exits when only daemon threads are left.

cleanup()[source]
is_running() bool[source]
wakeup()[source]
is_waiting()[source]
is_finalized()[source]
run()[source]

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

getName()

Return a string used for identification purposes only.

This method is deprecated, use the name attribute instead.

property ident

Thread identifier of this thread or None if it has not been started.

This is a nonzero integer. See the get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited.

isDaemon()

Return whether this thread is a daemon.

This method is deprecated, use the daemon attribute instead.

is_alive()

Return whether the thread is alive.

This method returns True just before the run() method starts until just after the run() method terminates. See also the module function enumerate().

join(timeout=None)

Wait until the thread terminates.

This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception or until the optional timeout occurs.

When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As join() always returns None, you must call is_alive() after join() to decide whether a timeout happened – if the thread is still alive, the join() call timed out.

When the timeout argument is not present or None, the operation will block until the thread terminates.

A thread can be join()ed many times.

join() raises a RuntimeError if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.

property name

A string used for identification purposes only.

It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.

property native_id

Native integral thread ID of this thread, or None if it has not been started.

This is a non-negative integer. See the get_native_id() function. This represents the Thread ID as reported by the kernel.

setDaemon(daemonic)

Set whether this thread is a daemon.

This method is deprecated, use the .daemon property instead.

setName(name)

Set the name string for this thread.

This method is deprecated, use the name attribute instead.

start()

Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.

class pydsol.core.simulator.ErrorStrategy[source]

Bases: object

ErrorStrategy indicates what to do when there is an error in the execution of the simulation. In order to set the error handling, the Simulator’s error_strategy can be set and changed, even during the execution of the simulation run. The log level can be overridden (and even be set to NONE).

LOG_AND_CONTINUE = 1

Send the error to the logger as WARNING. Both RunState and ReplicationState remain in the RUNNING state. The Simulator.run() continues as if the error did not occur.

WARN_AND_CONTINUE = 2

Send the error to logger as ERROR and print the exception on stderr. Both RunState and ReplicationState remain in the RUNNING state. The Simulator.run() continues as if the error did not occur.

WARN_AND_PAUSE = 3

Send the error to logger as ERROR and print the exception on stderr The RunState goes to STOPPING, leading to the stop of the loop in the Simulator.run() method and a subsequent STOPPED state in the SimulatorWorkerThread.run() method. The SimulatorWorkerThread will go into a Thread.wait(), to wait for start (or cleanup).

WARN_AND_END = 4

Send the error to logger as CRITICAL and print the exception on stderr The Simulator.cleanup() method is called to ensure the SimulatorWorkerThread.run() method completely ends and can be garbage collected. If there is a UI thread, it will keep running.

WARN_AND_EXIT = 5

Send the error to logger as FATAL and print the exception on stderr The Simulator.cleanup() method is called to ensure the stop of the run() in SimulatorWorkerThread; the System.exit() method is called to end the complete program.

LOG_LEVELS = {1: 30, 2: 40, 3: 40, 4: 50, 5: 50}

Dictionary for default log level per error handling strategy.