lewis.core.simulation
A Simulation
combines a Device
and its interface (derived from
an Adapter
).
Members
The Simulation class controls certain aspects of a device simulation, the most important one being time. |
|
This class is used to create |
- class lewis.core.simulation.Simulation(device, adapters=(), device_builder=None, control_server=None)[source]
Bases:
object
The Simulation class controls certain aspects of a device simulation, the most important one being time.
Once
start()
is called, the process-method of the device is called in regular intervals. The time between these calls is influenced by the cycle_delay property. Because of the way some network protocols work, the actual processing time can be longer or shorter, so cycle_delay should be seen as a guideline rather than a guaranteed parameter.In the simplest case, the actual time-delta between two cycles is passed to the simulated device so that it can update its internal state according to the elapsed time. It is however possible to set a simulation speed, which serves as a multiplier for this time. If the speed is set to 2 and 0.1 seconds pass between two cycles, the simulation is asked to simulate 0.2 seconds, and so on. Speed 0 effectively stops all time dependent calculations in the simulated device.
Another possibility to pause the simulation is the pause-method. After calling it, all processing in the device is suspended, while the communication adapters continue to work. This can be used to simulate that a device is “hanging”. The simulation can be continued using the resume-method.
A number of status properties provide information about the simulation. The total uptime (in actually elapsed time) can be obtained through the uptime-property, whereas the runtime-property contains the simulated time. The cycles-property indicates the total number of simulation cycles, which does not increase when the simulation is paused.
Finally, the simulation can be stopped entirely with the stop-method.
All functionality except for the start-method can be made available to remote computers via a
ControlServer
-instance. The way to expose device and simulation is to pass a ‘host:port’-string as the control_server argument, which will construct the control server. Simulation will try to start the control server using the start_server method.- Parameters:
device – The simulated device.
adapters – Adapters which expose the simulated device.
device_builder –
DeviceBuilder
instance to enable setup- switching at runtime.control_server – ‘host:port’-string to construct control server or None.
- property control_server
ControlServer-instance that exposes the object to remote machines. Can only be set before start has been called or on a running simulation if no control server was previously present. If the server is not running, it will be started after it has been set.
- property cycle_delay
Desired time between simulation cycles, this can not be negative. Use 0 for highest possible processing rate.
- property cycles
Simulation cycles processed since start has been called.
- property is_paused
True if the simulation is paused (implies that the simulation has been started).
- property is_started
This property is true if the simulation has been started.
- resume() None [source]
Resume a paused simulation. Can only be called after start and pause have been called.
- property runtime
The accumulated simulation time. Whenever speed is different from 1, this progresses at a different rate than uptime.
- set_device_parameters(parameters) None [source]
Set multiple parameters of the simulated device “simultaneously”. The passed parameter is assumed to be device parameter/value dict. The method only allows to set existing attributes. If there are invalid attribute names, the attributes are not updated, instead a RuntimeError is raised. The same happens if any of the parameters are methods, which can not be updated with this mechanisms.
- Parameters:
parameters – Dict of device attribute/values to update the device.
- property setups
A list of setups that are available. Use
switch_setup()
to change the setup.
- property speed
Simulation speed. Actual elapsed time is multiplied with this property to determine simulated time. Values greater than 1 increase the simulation speed, values between 1 and 0 decrease it. A speed of 0 effectively pauses the simulation.
- switch_setup(new_setup) None [source]
This method switches the setup, which means that it replaces the currently simulated device with a new device, as defined by the setup.
If any error occurs during setup switching it is logged and re-raised.
- Parameters:
new_setup – Name of the new setup to load.
- property uptime
Elapsed time in seconds since the simulation has been started.
- class lewis.core.simulation.SimulationFactory(devices_package)[source]
Bases:
object
This class is used to create
Simulation
-objects according to a certain set of parameters, such as device, setup and protocol. To create a simulation, it needs to know where devices are stored:factory = SimulationFactory("lewis.devices")
The actual creation happens via the
create()
-method:simulation = factory.create("device_name", protocol="protocol")
The simulation can then be started and stopped as desired.
Warning
This class is meant for internal use at the moment and may change frequently.
- create(device, setup=None, protocols=None, control_server=None)[source]
Creates a
Simulation
according to the supplied parameters.- Parameters:
device – Name of device.
setup – Name of the setup for device creation.
protocols – Dictionary where each key is assigned a dictionary with options for the corresponding
Adapter
. For available protocols, seeget_protocols()
.control_server – String to construct a control server (host:port).
- Returns:
Simulation object according to input parameters.
- property devices
Names of available devices.