Internals
Module
Simulate.Simulate
— Module.Simulate
A Julia package for discrete event simulation based on state machines.
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.
Simulate.SEngine
— Type.supertype for state machines in Sim.jl
States
Defined states for state machines.
Simulate.SState
— Type.supertype for states
Simulate.Undefined
— Type.a state machine is undefined (after creation)
Simulate.Idle
— Type.a state machine is idle
Simulate.Empty
— Type.a state machine is empty
Simulate.Busy
— Type.a state machine is busy
Simulate.Halted
— Type.a state machine is halted
Events
Defined events.
Simulate.SEvent
— Type.supertype for events
Simulate.Init
— Type.Init(info)
: Init event with some info.
Simulate.Setup
— Type.Setup(vars::Array{Symbol,1}, scope::Module)
: setup a logger with some info.
Simulate.Switch
— Type.Switch(to)
: switch to some other mode
Simulate.Log
— Type.Log()
: record command for logging
Simulate.Step
— Type.Step()
: command
Simulate.Run
— Type.Run()
: command
Simulate.Start
— Type.Start()
: command
Simulate.Stop
— Type.Stop()
: command
Simulate.Resume
— Type.Resume()
: command
Simulate.Clear
— Type.Clear()
: command
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 calledq::SState
: state of the state machineσ::SEvent
: event, triggering the transition
step!(sim::Clock, ::Undefined, ::Init)
initialize a clock.
step!(sim::Clock, ::Undefined, σ::Union{Step,Run})
if uninitialized, initialize and then Step or Run.
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
.
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.
step!(sim::Clock, ::Busy, ::Stop)
Stop the clock.
step!(sim::Clock, ::Halted, ::Resume)
Resume a halted clock.
step!(sim::Clock, q::SState, σ::SEvent)
catch all step!-function.
step!(A::Logger, ::Undefined, σ::Init)
Initialize a logger.
step!(A::Logger, ::Empty, σ::Setup)
Setup a logger with logging variables. They are given by Setup(vars, scope)
.
step!(A::Logger, ::Idle, ::Clear)
Clear the last record and the data table of a logger.
step!(A::Logger, ::Idle, σ::Log)
Logging event.
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"
Other internal types and functions
Simulate.SimEvent
— Type.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.
Simulate.SimCond
— Type.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 conditionsex::Array{SimExpr, 1}
: Expr or SFs to be evaluated if conditions are all truescope::Module
: evaluation scope
Simulate.Sample
— Type.Sample(ex::SimExpr, scope::Module)
Create a sampling expression.
Arguments, fields
ex::SimExpr
: expression or SimFunction to be called at sample timescope::Module
: evaluation scope
Simulate.sconvert
— Function.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}
Simulate.simExec
— Function.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
Simulate.nextevent
— Function.nextevent(sim::Clock)
Return the next scheduled event.
Simulate.nextevtime
— Function.nextevtime(sim::Clock)
Return the internal time (unitless) of next scheduled event.
Simulate.checktime
— Function.checktime(sim::Clock, t::Number)::Float64
check t
given according to clock settings and return a Float64 value
Simulate.setTimes
— Function.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
.
Simulate.startup!
— Function.startup!(p::SimProcess, cycles::Number)
Start a SimProcess
as a task in a loop.
Simulate.loop
— Function.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.
Simulate.scale
— Function.scale(n::Number)::Float64
calculate the scale from a given number