Ez Pz to use event simulation in Python
-
Create a simulation
-
Set the time limit
-
Create the event
-
Create functions do handle events
-
Run the simulation
Import MSim
import Msim
Create the simulation object
simulation = Msim.Sim()
Set the time limit for the simulation
simulation.set_run_till(1000)
Add event
evt = simulation.create_event(type_desc="create", start=simulation.now()+1, extra=None, f=func_creating)
simulation.add_event(evt)
And here are the parameters that you can pass to the create_event()
| Parameter | Description |
|---|---|
| type_desc | Description of the event |
| start | Start time (When the event will be started) |
| extra | Any other extra information that you may need. The simulation class do not access this parameter, so you can pass anything you want (like IDs, counters..) |
| f | Function that will be called once the event is started. The parameters the function receive are the simulation and the event itself |
Create the functions that the events will call
def func_creating(s, evt):
evt = s.create_event(type_desc="terminate", start=s.now()+5, extra=None, f=func_terminating)
s.add_event(evt)
def func_terminating(s, evt):
return
Run the simulation!
simulation.run()
Examples folder contain 2 examples you can try.
In "example1.py" the events keep recreating more of then.
In "example2.py" there is an special event that is recalled every 60 seconds.
This is a basic template for a function that handle the events. The first parameter is aways the simulation and the second parameter is the event.
Every time a event is started the function asigned for this event is called.
def func_creating(s, evt):
pass
For a event to be recalled you need to re-add this event to the event list in the simulation, so just call create event again
def func_repeating(s, evt):
evt = None
evt = s.create_event(type_desc="repeating", start=s.now()+interval, extra=evt.extra, f=func_repeating)
s.add_event(evt)
Parameters
| Parameter | Description |
|---|---|
| events_list | Entire list of events waiting to be started |
| time_now | The actual time of the simulation |
| run_till | Time limit to stop the simulation (may stop before if the event_list is empty) |
| total_events_created | Information about total event created |
| total_events_add_list | Information about total events really added to the list |
| total_events_executed | Information about total events executed |
Methods
| Methods | Description |
|---|---|
| now() | Return the actual time of the simulation |
| set_run_till(t:int) | Set the time limit to run the simulation |
| create_event(type_desc:str, start:int, extra=None, f=None) | Create an event object |
| add_event(evt:Evento) | Add the event object to the event_list |
| run() | Run the simulation |
| is_empty() | Return if the event_list is empty |
| print_eventos() | Print the events in the event_list |
| get_proximo_evento() | Return the next event and set the simulation to its time |
| validate_fila() | Validate if the event_list is organized (not need unless you are messing with the queue) |