simgo

package module
v0.0.0-...-2c8ccfb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2019 License: MIT Imports: 5 Imported by: 0

README

simgo

simgo is a discrete-event simulation framework based on simpy

TODO

Documentation

Index

Constants

View Source
const (
	// Built-in event priorities
	PriorityUrgent = iota
	PriorityNormal
)

Variables

This section is empty.

Functions

func NewEventQueueItem

func NewEventQueueItem(v *Event, time uint64, priority int, eid EventID) *eventQueueItem

NewEventQueueItem returns a new eventQueueItem given an event, time, priority code, and EID.

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*.

func (*Environment) Schedule

func (env *Environment) Schedule(v *Event, priority int, delay uint64)

Schedule adds the provided Event to the event priority queue. A priority and delay for the event is also provided.

func (*Environment) Step

func (env *Environment) Step()

Step processes the next event.

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.

func (*Event) Fail

func (e *Event) Fail(eventValue *EventValue) (*Event, error)

Fail sets the provided EventValue as the events value, marks the event as failed, and schedules it for processing by the environment. The event instance is returned along with any errors.

func (*Event) Succeed

func (e *Event) Succeed(val interface{}) (*Event, error)

Succeeds sets the event's value, marks it as successful and schedules it for processing by the environment. Returns the event instance along with any errors.

type EventID

type EventID uint64

An EventID is a unique numerical ID for each event.

func (*EventID) Next

func (eid *EventID) Next() EventID

Next returns the next sequential event ID.

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 NewEventValue

func NewEventValue() *EventValue

NewEventValue returns a pending EventValue.

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

func (pc *ProcComm) Resume(x interface{}) (*Event, bool)

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.

func (*ProcComm) Yield

func (pc *ProcComm) Yield(event *Event) interface{}

Yield waits for a value from a Resume() call and sets the state to stateRunning. Subsequent calls to Yield() communicate the provided event such that it can be read from Resume(). Yield() 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL