State Example
In this example, we introduce another Py-DES component called State. States are an extension of
events that can take a finite number of values and can be waited for one of them in particular.
pydes.State(sim, value)
Represents a state in the simulation. It is highly recommended to use Enums to control the possible values that a State object can take.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim
|
Simulator
|
The simulator instance. |
required |
value
|
Any
|
The initial value of the state. |
required |
Methods:
| Name | Description |
|---|---|
set |
changes the state to a new provided value |
wait |
waits until a certain value is set in the State. |
set(value)
Set the state to a new value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The new value of the state. |
required |
wait(value)
Wait for the state to become a specific value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to wait for. |
required |
Imports
First of all we import the necessary objects from pydes
Define Model
For this example we will define a State that can take two possible values.
Of course this same behavior could be achieved using simply an Event, but
the idea is to showcase the usage of the State component.
There is one process that will stop its execution until the state is set to a specific value.
The other process is going to be responsible of changing the State into
its new value. This will be done after a delay and thus delaying the advance
of the other process until the change in the state.
Run Simulation
With all this defined. We build the Simulator and the components involved. We schedule
both processes and then start the simulation.
sim = Simulator()
state = State(sim, States.FALSE)
p1 = Process1(sim, state)
p2 = Process2(sim, state)
sim.schedule(p1.main)
sim.schedule(p2.main)
sim.run()
The simulation output would resemble the following:
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
| time | component | value | description |
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
| | | | |
| 0 | Process1.0 | wait for state | None |
| 0 | Process2.0 | sleeps before changing state | None |
| 10 | Process2.0 | sets state | None |
| 10 | Process2.0 | state changed | None |
| 10 | Process1.0 | state wait finished | None |