Crate desim

source ·
Expand description

This crate implements a discrete time event simulation framework inspired by the SimPy library for Python. It uses the coroutine feature that is nightly. Once the feature is stabilized, also this crate will use stable. Coroutines will be the only nightly feature used in this crate.

The examples directory in this repository contains full usage examples of the desim crate as a simulation framework.

Simulation

A simulation is performed scheduling one or more processes that models the environment you are going to simulate. Your model may consider some kind of finite resource that must be shared among the processes, e.g. a bunch of servers in a simulation on queues.

After setting up the simulation, it can be run step-by-step, using the step() method, or all at once, with run(), until and ending condition is met.

The simulation will generate a log of all the events with a state that returns true to should_log.

Process

A process is implemented using the rust coroutines syntax. This let us avoid the overhead of spawning a new thread for each process, while still keeping the use of this framework quite simple.

When a new process is created in the simulation, an identifier, of type ProcessId is assigned to it. That id can be used to schedule an event that resumes the process.

A process can be stopped and resumed later on. To stop the process, the coroutine yields an Effect that specify what the simulator should do. For example, a coroutine can set a timeout after which it is executed again. The process may also return. In that case it can not be resumed anymore.

Resource

A resource is a finite amount of entities, eachone of which can be used by one process a time. When the process does not need the resource anymore, it must release it.

A resource can be created in the simulation using the create_resource method, which requires the resource to add to the simulation and returns an identifier for that resource that can be used to require and release it.

A resource can be required and reelased by a process yielding the corresponding Effect. There is no check on the fact that a process yielding Release was holding a resource with that ID.

For more information about the Resource trait and the SimpleResource implementation, see the resources module.

Modules

Structs

  • An event that can be scheduled by a process, yelding the Event Effect or by the owner of a Simulation through the schedule method
  • The Simulation Context is the argument used to resume the coroutine. It can be used to retrieve the simulation time and the effect that caused the process’ wake up.
  • This struct provides the methods to create and run the simulation in a single thread.

Enums

  • The effect is yelded by a process coroutine to interact with the simulation environment.
  • Specify which condition must be met for the simulation to stop.

Traits

  • Data structures implementing this trait can be yielded from the coroutine associated with a Process. This allows attaching application-specific data to Effects. This data is then carried arround by the Simulation, passed into user callbacks for context or simply logged for later.

Type Aliases

  • The type of each Process coroutine
  • Identifies a process. Can be used to resume it from another one and to schedule it.
  • Identifies a resource. Can be used to request and release it.
  • Identifies a store. Can be used to push into and pull out of it.