Documentation
¶
Overview ¶
Package hoff is a library to define a Node workflow and compute data against it.
Create a node system and activate it:
ns := hoff.NewNodeSystem() ns.AddNode(some_action_node) // read the input_data in context and create a output_data in context ns.AddNode(decision_node) // check if the input_data is valid with some functionals rules ns.AddNode(another_action_node) // enhance output_data with some functionals tasks ns.ConfigureJoinModeOnNode(another_action_node, hoff.JoinAnd) ns.AddLink(some_action_node, another_action_node) ns.AddLinkOnBranch(decision_node, another_action_node, true) errs := ns.Activate() if errs != nil { // error handling }
Create a single computation and launch it:
cxt := hoff.NewContextWithoutData() cxt.Store("input_info", input_info) cp := hoff.NewComputation(ns, context) err := cp.Compute() if err != nil { // error handling } fmt.Printf("computation report: %+v", cp.Report) fmt.Printf("output_data: %+v", cp.Context.Read("output_data"))
Create an engine and run multiple computations:
eng := hoff.NewEngine(hoff.SequentialComputation) eng.ConfigureNodeSystem(ns) cr1 := eng.Compute(input_info) fmt.Printf("computation error: %+v", cr1.Error) fmt.Printf("computation report: %+v", cr1.Report) fmt.Printf("computed data: %+v", cr1.Data) cr2 := eng.Compute(another_aninput_info) fmt.Printf("computation error: %+v", cr2.Error) fmt.Printf("computation report: %+v", cr2.Report) fmt.Printf("computed data: %+v", cr2.Data)
Index ¶
- Constants
- Variables
- type ActionNode
- type Computation
- type ComputationMode
- type ComputationResult
- type ComputeState
- type Context
- type DecisionNode
- type Engine
- type JoinMode
- type Node
- type NodeSystem
- func (s *NodeSystem) Activate() error
- func (s *NodeSystem) AddLink(from, to Node) (bool, error)
- func (s *NodeSystem) AddLinkOnBranch(from, to Node, branch bool) (bool, error)
- func (s *NodeSystem) AddNode(n Node) (bool, error)
- func (s *NodeSystem) Ancestors(n Node, branch *bool) ([]Node, error)
- func (s *NodeSystem) ConfigureJoinModeOnNode(n Node, m JoinMode) (bool, error)
- func (s *NodeSystem) Equal(o *NodeSystem) bool
- func (s *NodeSystem) Follow(n Node, branch *bool) ([]Node, error)
- func (s *NodeSystem) InitialNodes() []Node
- func (s *NodeSystem) IsActivated() bool
- func (s *NodeSystem) IsValid() (bool, []error)
- func (s *NodeSystem) JoinModeOfNode(n Node) JoinMode
- type StateType
Constants ¶
const ( // JoinAnd will force the system to have a ComputeState at Continue // for all defined Nodes in order to compute the linked Node. JoinAnd JoinMode = "and" // JoinOr will force the system to have at least on ComputeState at Continue // for all defined Nodes in order to compute the linked Node. JoinOr = "or" // JoinNone is the default JoinMode to define a mono link between two Nodes. JoinNone = "none" )
const ( // ContinueState tell that the Node computation authorize // to compute the following nodes ContinueState StateType = "Continue" // SkipState tell that the Node computation is skipped SkipState = "Skip" // AbortState tell the Node computation encounter an error // and abort the computation AbortState = "Abort" )
Variables ¶
var ( // NodeComparator is a google/go-cmp comparator of Node NodeComparator = cmp.Comparer(func(x, y Node) bool { return x == y }) )
Functions ¶
This section is empty.
Types ¶
type ActionNode ¶
type ActionNode struct {
// contains filtered or unexported fields
}
ActionNode is a type of Node who compute a function to realize some actions based on Context.
func NewActionNode ¶
func NewActionNode(name string, actionFunc func(*Context) error) (*ActionNode, error)
NewActionNode create a ActionNode based on a name and a function to realize the needed action.
func (*ActionNode) Compute ¶
func (n *ActionNode) Compute(c *Context) ComputeState
Compute run the action function and decide which compute state to return.
func (*ActionNode) DecideCapability ¶
func (n *ActionNode) DecideCapability() bool
DecideCapability is desactived due to the fact that an action don't take a decision.
func (ActionNode) String ¶
func (n ActionNode) String() string
type Computation ¶
type Computation struct { System *NodeSystem Context *Context Status bool Report map[Node]ComputeState }
Computation take a NodeSystem and compute a Context against it.
func NewComputation ¶
func NewComputation(system *NodeSystem, context *Context) (*Computation, error)
NewComputation create a computation based on a valid, and activated NodeSystem and a Context.
func (*Computation) Compute ¶
func (cp *Computation) Compute() error
Compute run all nodes in the defined order to enhance the Context. At the end of the computation (Status at true), you can read the compute state of each node in the Report.
func (Computation) Equal ¶
func (cp Computation) Equal(o Computation) bool
Equal validate the two Computation are equals.
type ComputationMode ¶
type ComputationMode string
ComputationMode define the mode of computation for the engine.
const ( // SequentialComputation will run sequential computations in the engine. SequentialComputation ComputationMode = "seq" )
type ComputationResult ¶
type ComputationResult struct { Error error Data map[string]interface{} Report map[Node]ComputeState }
ComputationResult store the result of a computation.
type ComputeState ¶
ComputeState hold the result of a Node computation
func NewAbortComputeState ¶
func NewAbortComputeState(err error) ComputeState
NewAbortComputeState generate a computation state to throw an unexpected error
func NewContinueComputeState ¶
func NewContinueComputeState() ComputeState
NewContinueComputeState generate a computation state to continue to following nodes
func NewContinueOnBranchComputeState ¶
func NewContinueOnBranchComputeState(branch bool) ComputeState
NewContinueOnBranchComputeState generate a computation state to continue to following nodes on a branch taken by an Decision Node (DecideCapability at true)
func NewSkipComputeState ¶
func NewSkipComputeState() ComputeState
NewSkipComputeState generate a computation state to specify that the Node computation have been skipped
func (ComputeState) String ¶
func (cs ComputeState) String() string
String print human-readable version of a compute state
type Context ¶
type Context struct {
Data map[string]interface{}
}
Context hold data during an Computation
func NewContext ¶
NewContext generate a new Context with data
func NewContextWithoutData ¶
func NewContextWithoutData() *Context
NewContextWithoutData generate a new empty Context
type DecisionNode ¶
type DecisionNode struct {
// contains filtered or unexported fields
}
DecisionNode is a type of Node who compute a function to take a decision based on Context.
func NewDecisionNode ¶
NewDecisionNode create a DecisionNode based on a name and a function to take the needed decision.
func (*DecisionNode) Compute ¶
func (n *DecisionNode) Compute(c *Context) ComputeState
Compute run the decision function and decide which compute state to return.
func (*DecisionNode) DecideCapability ¶
func (n *DecisionNode) DecideCapability() bool
DecideCapability is actived due to the fact that an decision is taked during compute.
func (DecisionNode) String ¶
func (n DecisionNode) String() string
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine expose an engine to manage multiple computations based on a node system.
func NewEngine ¶
func NewEngine(mode ComputationMode) *Engine
NewEngine create an engine with computation mode. Need to be configured with a node system
func (*Engine) Compute ¶
func (e *Engine) Compute(data map[string]interface{}) ComputationResult
Compute run computation against node system with input data.
func (*Engine) ConfigureNodeSystem ¶
func (e *Engine) ConfigureNodeSystem(system *NodeSystem) error
ConfigureNodeSystem add a node system to the engine (only once).
type JoinMode ¶
type JoinMode string
JoinMode define the mode to join multiple Nodes (source) to the same linked Node (target)
type Node ¶
type Node interface { // Compute a node based on a context Compute(c *Context) ComputeState // DecideCapability tell if the Node can decide. // This impact the compute state by adding a branch to the state. DecideCapability() bool }
Node define
type NodeSystem ¶
type NodeSystem struct {
// contains filtered or unexported fields
}
NodeSystem is a system to configure workflow between action nodes, or decision nodes. The nodes are linked between them by link and join mode options. An activated Node system will be walked throw Follow and Ancestors functions
func NewNodeSystem ¶
func NewNodeSystem() *NodeSystem
NewNodeSystem create an empty Node system who need to be valid and activated in order to be used.
func (*NodeSystem) Activate ¶
func (s *NodeSystem) Activate() error
Activate prepare the node system to be used. In order to activate it, the node system must be valid. Once activated, the initial nodes, following nodes, and ancestors nodes will be accessibles.
func (*NodeSystem) AddLink ¶
func (s *NodeSystem) AddLink(from, to Node) (bool, error)
AddLink add a link from a node to another node into the system before activation.
func (*NodeSystem) AddLinkOnBranch ¶
func (s *NodeSystem) AddLinkOnBranch(from, to Node, branch bool) (bool, error)
AddLinkOnBranch add a link from a node (on a specific branch) to another node into the system before activation.
func (*NodeSystem) AddNode ¶
func (s *NodeSystem) AddNode(n Node) (bool, error)
AddNode add a node to the system before activation.
func (*NodeSystem) Ancestors ¶
func (s *NodeSystem) Ancestors(n Node, branch *bool) ([]Node, error)
Ancestors get the set of nodes who access using one of their branch to a specific node after activation.
func (*NodeSystem) ConfigureJoinModeOnNode ¶
func (s *NodeSystem) ConfigureJoinModeOnNode(n Node, m JoinMode) (bool, error)
ConfigureJoinModeOnNode configure the join mode of a node into the system before activation.
func (*NodeSystem) Equal ¶
func (s *NodeSystem) Equal(o *NodeSystem) bool
Equal validate the two NodeSystem are equals.
func (*NodeSystem) Follow ¶
func (s *NodeSystem) Follow(n Node, branch *bool) ([]Node, error)
Follow get the set of nodes accessible from a specific node and one of its branch after activation.
func (*NodeSystem) InitialNodes ¶
func (s *NodeSystem) InitialNodes() []Node
InitialNodes get the initial nodes
func (*NodeSystem) IsActivated ¶
func (s *NodeSystem) IsActivated() bool
IsActivated give the activation state of the node system. Only true if the node system is valid and have run the activate function without errors.
func (*NodeSystem) IsValid ¶
func (s *NodeSystem) IsValid() (bool, []error)
IsValid check if the configuration of the node system is valid based on checks. Check for decision node with any node links as from, check for cyclic redundancy in node links, check for undeclared node used in node links, check for multiple declaration of same node instance.
func (*NodeSystem) JoinModeOfNode ¶
func (s *NodeSystem) JoinModeOfNode(n Node) JoinMode
JoinModeOfNode get the configured join mode of a node