Documentation ¶
Index ¶
Constants ¶
const ( // Built-in event priorities PriorityUrgent = iota PriorityNormal )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Condition ¶
type Condition struct { *Event // contains filtered or unexported fields }
A Condition embeds an event that gets triggered once the "evaluate" condition function returns true on the list of events.
The value of the condition event is an instance of ConditionValue which allows convenient access to the input events and their values. The ConditionValue will only contain entries for those events that occurred before the condition is processed.
If one of the events fails, the condition also fails and... TODO: does what?
The evaluate function receives the list of target events and the number of processed events in this list: evaluate(events, processedCount). If it returns true, the condition is triggered.
Condition events can be nested.
func AllOf ¶
func AllOf(env *Environment, events []*Event) *Condition
func AnyOf ¶
func AnyOf(env *Environment, events []*Event) *Condition
func NewCondition ¶
func NewCondition(env *Environment, evaluateFn conditionEvaluateFn, events []*Event) *Condition
type ConditionValue ¶
type ConditionValue []interface{}
conditionEvaluateFn is the evaluate function signature used for conditions
type Environment ¶
type Environment struct { // Current time count Now uint64 // ActiveProcess is the currently active process ActiveProcess *Process // contains filtered or unexported fields }
An environment is the execution environment for an event-based simulation. The passing of time is simulated by stepping from event to event.
func NewEnvironment ¶
func NewEnvironment() *Environment
NewEnvironment returns an Environment with default values.
func (*Environment) Run ¶
func (env *Environment) Run(until interface{}) (interface{}, error)
Run executes Step() until a condition below is met for the provided "until" value:
If it is "None“, this method will return when there are no further events to be processed.
If it is an Event, the method will continue stepping until this event has been triggered and will return its value. Returns an error if there are no further events to be processed and the "until" event was not triggered.
If it is a number, the method will continue stepping until the environment's time reaches *until*.
type Event ¶
type Event struct { // Value holds the event's value Value *EventValue // contains filtered or unexported fields }
An Event is an event that may happen at some point in time.
An event - may happen (i.e., triggered is False), - is going to happen (i.e., triggered True) or - has happened (i.e., processed True).
Every event is bound to an environment (env) and is initially not triggered. Events are scheduled for processing by the environment after they are triggered by either `succeed`, `fail` or `trigger`. These methods also set the `ok` flag and the value of the event.
An event has a list of `callbacks`. Once an event gets processed, all callbacks will be called with the event as the single argument. Callbacks can check if the event was successful by examining `ok` and do further processing with the value it has produced.
TODO: Talk about how events are finalized/defused (?) after being processed.
func NewEvent ¶
func NewEvent(env *Environment) *Event
NewEvent returns a new Event object with default values.
type EventValue ¶
type EventValue struct {
// contains filtered or unexported fields
}
EventValue holds the value state for an Event. If the value is pending it means that the event
func (*EventValue) Add ¶
func (ev *EventValue) Add(eventValue *EventValue)
Add adds an underlying value to a map (and initializes the map and sets isPending accordingly).
func (*EventValue) Get ¶
func (ev *EventValue) Get() (interface{}, error)
Get returns the underlying event value along with an error if the value is still pending.
func (*EventValue) Set ¶
func (ev *EventValue) Set(value interface{})
Set sets the underlying value (and sets isPending accordingly).
type ProcComm ¶
type ProcComm struct {
// contains filtered or unexported fields
}
A ProcComm allows a process to communicate with a process function via channels, allowing the process function to behave as a coroutine.
func NewProcComm ¶
func NewProcComm() *ProcComm
NewProcComm returns a new ProcComm with initialized channels and suspended state.
func ProcWrapper ¶
func ProcWrapper(env *Environment, procFn func(*Environment, *ProcComm) interface{}) *ProcComm
ProcWrapper is function that turns a user process function into a coroutine that can suspend its execution by yielding an event (using ProcComm.Yield()).
See the examples directory for example usage.
func (*ProcComm) Finish ¶
func (pc *ProcComm) Finish(x interface{})
Finish finalizes the underlying channels such that the process can finish.
func (*ProcComm) Resume ¶
Resume communicates the provided value, x, such that it can be read from Yield(). Resume then receives a value from Yield(). The event received from Yield() is returned along with a bool indicating if the event was received from a valid, open channel. Resume() communicates over unbuffered channels and may block accordingly.
type Process ¶
type Process struct { *Event // contains filtered or unexported fields }
A Process processes an event yielding process function.
A user implements a process function which is a coroutine function that can suspend its execution by yielding an event (using ProcComm.Yield()). Process will take care of resuming the process function with the value of that event once it has happened.
func NewProcess ¶
func NewProcess(env *Environment, pc *ProcComm) *Process
NewProcess returns a new Process given an Environment and a ProcComm (which is used to communicate between the process function coroutine and the Process).
func (*Process) Init ¶
func (p *Process) Init()
Init initializes the process. The process's Event is automatically triggered and scheduled.
func (*Process) ReturnValue ¶
func (p *Process) ReturnValue() interface{}
ReturnValue returns the value returned by the process function.
type Timeout ¶
type Timeout struct { *Event // contains filtered or unexported fields }
Timeout embeds an event and adds a delay
func NewTimeout ¶
func NewTimeout(env *Environment, delay uint64, value interface{}) Timeout
NewTimeout returns a new Timeout object given an environment, delay and an Event value. The event is automatically triggered when this function is called.
func (*Timeout) Schedule ¶
func (to *Timeout) Schedule(env *Environment)
Schedule schedules the timeout event for the provided environment.