Skip to content

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

from enum import Enum
from pydes import Component, Simulator, State

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.

1
2
3
class States(Enum):
    FALSE = 0
    TRUE = 1

There is one process that will stop its execution until the state is set to a specific value.

1
2
3
4
5
6
7
8
9
class Process1(Component):
    def __init__(self, sim: Simulator, state: State):
        self.sim = sim
        self.state = state

    def main(self):
        self.sim.record(self.id, "wait for state")
        self.state.wait(States.TRUE)
        self.sim.record(self.id, "state wait finished")

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.

class Process2(Component):
    def __init__(self, sim: Simulator, state: State):
        self.sim = sim
        self.state = state

    def main(self):
        self.sim.record(self.id, "sleeps before changing state")
        self.sim.sleep(10)
        self.sim.record(self.id, "sets state")
        self.state.set(States.TRUE)
        self.sim.record(self.id, "state changed")

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                           |