Internals

Internals

Module

Simulate.SimulateModule.
Simulate

A Julia package for discrete event simulation based on state machines.

source

The module contains two main types: Clock and Logger. Both are implemented as state machines. The implementation functions and types are not exported. The exported functions documented above under Usage are commands to the internal state machines.

State machines

We have some definitions for them to work.

supertype for state machines in Sim.jl

source

States

Defined states for state machines.

Simulate.SStateType.

supertype for states

source

a state machine is undefined (after creation)

source
Simulate.IdleType.

a state machine is idle

source
Simulate.EmptyType.

a state machine is empty

source
Simulate.BusyType.

a state machine is busy

source
Simulate.HaltedType.

a state machine is halted

source

Events

Defined events.

Simulate.SEventType.

supertype for events

source
Simulate.InitType.

Init(info): Init event with some info.

source
Simulate.SetupType.

Setup(vars::Array{Symbol,1}, scope::Module): setup a logger with some info.

source
Simulate.SwitchType.

Switch(to): switch to some other mode

source
Simulate.LogType.

Log(): record command for logging

source
Simulate.StepType.

Step(): command

source
Simulate.RunType.

Run(): command

source
Simulate.StartType.

Start(): command

source
Simulate.StopType.

Stop(): command

source
Simulate.ResumeType.

Resume(): command

source
Simulate.ClearType.

Clear(): command

source

Transition functions

In state machines transitions occur depending on states and events. The different transitions are described through different methods of the step!-function.

Simulate.step!Function.
step!(A::SEngine, q::SState, σ::SEvent)

Default transition for clock and logger.

This is called if no otherwise defined transition occurs.

Arguments

  • A::SEngine: state machine for which a transition is called
  • q::SState: state of the state machine
  • σ::SEvent: event, triggering the transition
source
step!(sim::Clock, ::Undefined, ::Init)

initialize a clock.

source
step!(sim::Clock, ::Undefined, σ::Union{Step,Run})

if uninitialized, initialize and then Step or Run.

source
step!(sim::Clock, ::Union{Idle,Busy,Halted}, ::Step)

step forward to next tick or scheduled event.

At a tick evaluate 1) all sampling functions or expressions, 2) all conditional events, then 3) if an event is encountered, trigger the event.

The internal clock times sim.tev and sim.tsa is always at least sim.time.

source
step!(sim::Clock, ::Idle, σ::Run)

Run a simulation for a given duration.

The duration is given with Run(duration). Call scheduled events and evaluate sampling expressions at each tick in that timeframe.

source
step!(sim::Clock, ::Busy, ::Stop)

Stop the clock.

source
step!(sim::Clock, ::Halted, ::Resume)

Resume a halted clock.

source
step!(sim::Clock, q::SState, σ::SEvent)

catch all step!-function.

source
step!(A::Logger, ::Undefined, σ::Init)

Initialize a logger.

source
step!(A::Logger, ::Empty, σ::Setup)

Setup a logger with logging variables. They are given by Setup(vars, scope).

source
step!(A::Logger, ::Idle, ::Clear)

Clear the last record and the data table of a logger.

source
step!(A::Logger, ::Idle, σ::Log)

Logging event.

source
step!(A::Logger, ::Idle, σ::Switch)

Switch the operating mode of a logger by Switch(to).

to = 0: no output, to = 1: print, `to = 2: store in log table"

source

Other internal types and functions

SimEvent(ex::Array{SimExpr, 1}, scope::Module, t::Float64, Δt::Float64)

Create a simulation event: a SimExpr or an array of SimExpr to be executed at event time.

Arguments, fields

  • ex::Array{SimExpr, 1}: an array of SimExpr to be evaluated at event time,
  • scope::Module: evaluation scope,
  • t::Float64: event time,
  • Δt::Float64: repeat rate with which the event gets repeated.
source
SimCond(cond::Array{SimExpr, 1}, ex::Array{SimExpr, 1}, scope::Module)

create a condition to be evaluated repeatedly with expressions or functions to be executed if conditions are met.

Arguments, fields

  • cond::Array{SimExpr, 1}: Expr or SFs to be evaluated as conditions
  • ex::Array{SimExpr, 1}: Expr or SFs to be evaluated if conditions are all true
  • scope::Module: evaluation scope
source
Simulate.SampleType.
Sample(ex::SimExpr, scope::Module)

Create a sampling expression.

Arguments, fields

  • ex::SimExpr: expression or SimFunction to be called at sample time
  • scope::Module: evaluation scope
source
Simulate.sconvertFunction.
sconvert(ex::Union{SimExpr,Array,Tuple})::Array{SimExpr,1}

convert a SimExpr or an array or a tuple of it to an Array{SimExpr,1}

source
Simulate.simExecFunction.
simExec(ex::Union{SimExpr, Array{SimExpr,1}}, m::Module=Main)

evaluate the event expressions or SimFunctions.

Return

the evaluated value or a tuple of evaluated values

source
Simulate.nexteventFunction.
nextevent(sim::Clock)

Return the next scheduled event.

source
Simulate.nextevtimeFunction.
nextevtime(sim::Clock)

Return the internal time (unitless) of next scheduled event.

source
Simulate.checktimeFunction.
checktime(sim::Clock, t::Number)::Float64

check t given according to clock settings and return a Float64 value

source
Simulate.setTimesFunction.
setTimes(sim::Clock)

set clock times for next event or sampling action. The internal clock times sim.tev and sim.tsa must always be set to be at least sim.time.

source
Simulate.startup!Function.
startup!(p::SimProcess, cycles::Number)

Start a SimProcess as a task in a loop.

source
Simulate.loopFunction.
loop(p::SimProcess, start::Channel, cycles::Number)

Put a SimProcess in a loop, which can be broken by a SimException.

Arguments

  • p::SimProcess:
  • start::Channel: a channel to ensure that a process starts,
  • cycles=Inf: determine, how often the loop should be run.
source
Simulate.scaleFunction.
scale(n::Number)::Float64

calculate the scale from a given number

source