Skip to content

Store Example

pydes.Store(sim, capacity=1)

Stores are useful to save and retrieve objects. Stores can be use to insert any type of object but it requires all the objects to be of the same type.

Parameters:

Name Type Description Default
sim Simulator

The simulator instance.

required
capacity int

The capacity of the store, default is infinity.

1

get()

Get an item from the store.

put(item)

Put an item into the store.

Parameters:

Name Type Description Default
item Any

The item to put into the store.

required

level()

Get the current level of the store.

capacity()

Get the capacity of the store.

Imports

from dataclasses import dataclass
from pydes import Component, Store, Simulator

Define Model

@dataclass
class Element:
    id: int


class Process1(Component):
    def __init__(self, sim: Simulator, store: Store):
        self.sim = sim
        self.store = store

    def main(self):
        # generating elements:
        i = 0
        while True:
            e = Element(i)
            self.sim.record(self.id, "start put")
            self.store.put(e)
            self.sim.record(self.id, "end put")
            self.sim.record(self.id, f"store level: {self.store.level()}")
            self.sim.sleep(5)
            i += 1
class Process2(Component):
    def __init__(self, sim: Simulator, store: Store):
        self.sim = sim
        self.store = store

    def main(self):
        while True:
            self.sim.record(self.id, "start get")
            e = self.store.get()
            self.sim.record(self.id, f"end get: {e}")
            self.sim.record(self.id, f"store level: {self.store.level()}")
            self.sim.sleep(20)

Run Simulation

sim = Simulator()
store = Store(sim, 3)
p1 = Process1(sim, store)
p2 = Process2(sim, store)
sim.schedule(p1.main)
sim.schedule(p2.main)
sim.run(until=20)
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
| time                           | component       | value                                    | description                    |
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
|                                |                 |                                          |                                |
| 0                              | Process1.0      | start put                                | None                           |
| 0                              | Process2.0      | start get                                | None                           |
| 0                              | Process1.0      | end put                                  | None                           |
| 0                              | Process1.0      | store level: 1                           | None                           |
| 0                              | Process2.0      | end get: Element(id=0)                   | None                           |
| 0                              | Process2.0      | store level: 0                           | None                           |
| 5                              | Process1.0      | start put                                | None                           |
| 5                              | Process1.0      | end put                                  | None                           |
| 5                              | Process1.0      | store level: 1                           | None                           |
| 10                             | Process1.0      | start put                                | None                           |
| 10                             | Process1.0      | end put                                  | None                           |
| 10                             | Process1.0      | store level: 2                           | None                           |
| 15                             | Process1.0      | start put                                | None                           |
| 15                             | Process1.0      | end put                                  | None                           |
| 15                             | Process1.0      | store level: 3                           | None                           |
| 20                             | Process2.0      | start get                                | None                           |
| 20                             | Process1.0      | start put                                | None                           |
| 20                             | Process2.0      | end get: Element(id=1)                   | None                           |
| 20                             | Process2.0      | store level: 2                           | None                           |
| 20                             | Process1.0      | end put                                  | None                           |
| 20                             | Process1.0      | store level: 3                           | None                           |