gopetri

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2019 License: MIT Imports: 3 Imported by: 0

README

gopetri

This is first implementation of petri net for Go.

!!! This support only one chip in progress. 

Examples.

Base example.

U can run example and watch changes of states in log.

go run example/base/main.go
Base example with visualisation.

We use graphviz for visualisation. If u run next cmd, you will receive graph.dot file with dot lang and graph img.

go run example/base/main.go > graph.dot && dot -Tpng graph.dot > graph.png

Documentation

Index

Constants

View Source
const DefaultPoolTimeout time.Duration = 30 * time.Second

DefaultPoolTimeout is default timeout for poll get.

Variables

This section is empty.

Functions

func Is

func Is(err error, code ErrCode) bool

Is compare err and error code. If err is not nil, is *Error type and have the same code or false.

Types

type Cfg

type Cfg struct {
	Start       string                   `json:"start"`
	Finish      []string                 `json:"finish"`
	Places      []string                 `json:"places"`
	Transitions map[string]CfgTransition `json:"transitions"`
}

Cfg is config for petri network.

type CfgTransition

type CfgTransition struct {
	From []string `json:"from"`
	To   []string `json:"to"`
}

CfgTransition is struct of transition.

type Consumer

type Consumer interface {
	BeforePlace(placeID string) error
	AfterPlace(placeID string)
	CanTransit(transitionID string) bool
	BeforeTransit(transitionID string) error
	AfterTransit(transitionID string)
}

Consumer is interface for external consumer.

type ErrCode

type ErrCode int
const (
	ErrCodePlaceAlreadyRegistered ErrCode = iota + 1
	ErrCodeTransitionAlreadyRegistered
	ErrCodePlaceIsNotRegistered
	ErrCodeUnexpectedAvailableTransitionsNumber
	ErrCodeHasNotChipForNewPlace
	ErrCodeNetInErrState
	ErrCodeBeforePlaceReturnedErr
	ErrCodeBeforeTransitReturnedErr
	ErrCodeCantSetErrState
	ErrCodeFinished
	ErrCodeFromTransitionAlreadyRegistered
	ErrCodeToTransitionAlreadyRegistered
	ErrCodeConsumerNotSet
	ErrCodePoolAlreadyInit
	ErrCodeWaitingForNetFromPoolTooLong
)

type Error

type Error struct {
	Code    ErrCode `json:"code"`
	Message string  `json:"message"`
}

Error is err model of component.

func NewError

func NewError(errorCode ErrCode, msg string) *Error

NewError init error.

func NewErrorf

func NewErrorf(errorCode ErrCode, format string, args ...interface{}) *Error

NewErrorf init error by format.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(errorCode ErrCode) bool

Is check error.

type Factory

type Factory struct {
	// contains filtered or unexported fields
}

Factory fro net with prepared buffer.

func NewFactory

func NewFactory(cfg Cfg, consumer Consumer, numberOfPrepared int) *Factory

NewFactory init factory.

func (*Factory) Get

func (f *Factory) Get() *Net

Get return prepared petri net.

func (*Factory) Run

func (f *Factory) Run() error

Run async preparing of net.

type Net

type Net struct {
	// contains filtered or unexported fields
}

Net of petri net.

func BuildFromCfg

func BuildFromCfg(cfg Cfg) (*Net, error)

BuildFromCfg return net by Cfg.

func New

func New() *Net

New net instance with consumer.

func (*Net) AddPlace

func (c *Net) AddPlace(place *Place) error

AddPlace to petri net.

func (*Net) AddTransition

func (c *Net) AddTransition(transition *Transition) error

AddTransition tp petri net.

func (*Net) AsGraphvizDotLang

func (c *Net) AsGraphvizDotLang(name string, withState bool) (string, error)

AsGraphvizDotLang return string with graphviz dot lang view of graph. If u set 'withState', you will have chips on graph. 'turquoise' is color for start place, 'sienna' is coor for finish places. 'red' is for chips on map.

func (*Net) FullReset

func (c *Net) FullReset()

FullReset state of net for reusing.

func (*Net) GetErrFromState

func (c *Net) GetErrFromState() *Error

GetErrFromState return err state reason.

func (*Net) GetPlace

func (c *Net) GetPlace(placeID string) (*Place, error)

GetPlace return place by id.

func (*Net) GetState

func (c *Net) GetState() State

GetState return current state of net.

func (*Net) IsErrState

func (c *Net) IsErrState() bool

IsErrState check state on error.

func (*Net) IsFinished

func (c *Net) IsFinished() bool

IsFinished return info about state, finished or not.

func (*Net) SetConsumer

func (c *Net) SetConsumer(l Consumer)

SetConsumer for net.

func (*Net) SetErrorState

func (c *Net) SetErrorState(err *Error) error

SetStartPlace by name of state.

func (*Net) SetPlace

func (c *Net) SetPlace(placeID string) error

SetPlace for chip. If chip in finish place, will return true as first result.

func (*Net) SetStartPlace

func (c *Net) SetStartPlace(place string) error

SetStartPlace by name of state.

func (*Net) StartPlace

func (c *Net) StartPlace() error

StartPlace start net.

!!! Not supported V of nodes and more than one chip.

func (*Net) UpFromState

func (c *Net) UpFromState(state State)

UpFromState set current state for work.

type Place

type Place struct {
	ID             string
	ToTransitions  []*Transition
	FromTransition *Transition
	IsFinished     bool
}

Place of chip in net.

func NewPlace

func NewPlace(id string, isFinished bool) *Place

NewPlace create place.

func (*Place) AddToTransitions

func (s *Place) AddToTransitions(transition *Transition) error

AddToTransitions to place in net.

func (*Place) SetFromTransition

func (s *Place) SetFromTransition(transition *Transition) error

SetFromTransition to place in net.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool is pool of net.

func NewPool

func NewPool(size int, getTimeout time.Duration) *Pool

NewPool init pool.

func (*Pool) Get

func (p *Pool) Get() (*PoolNet, error)

Get return net.

func (*Pool) Init

func (p *Pool) Init(cfg Cfg) error

Init pool.

type PoolNet

type PoolNet struct {
	*Net
	// contains filtered or unexported fields
}

PoolNet is wrapper of net for pool.

func (*PoolNet) Close

func (n *PoolNet) Close()

Close current net and return it to pool.

type State

type State struct {
	Finished        bool           `json:"finished"`
	Err             *Error         `json:"err"`
	PlaceChips      map[string]int `json:"place_chips"`
	TransitionChips map[string]int `json:"transition_chips"`
}

State is current state of net, contains places for chips in places and transitions.

func (State) IsError

func (s State) IsError() bool

IsError state.

func (State) IsFinished

func (s State) IsFinished() bool

IsFinished state.

type Transition

type Transition struct {
	ID         string
	ToPlaces   []*Place
	FromPlaces []*Place
}

Transition model of net.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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