hoff

package module
v0.0.0-...-f8b1265 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2020 License: MIT Imports: 3 Imported by: 0

README

= HOFF

> Don't hassle with HOFF

image:https://travis-ci.org/rlespinasse/hoff.svg?branch=v0["Build Status", link="https://travis-ci.org/rlespinasse/hoff"]
image:https://coveralls.io/repos/github/rlespinasse/hoff/badge.svg?branch=v0["Coverage Status", link="https://coveralls.io/github/rlespinasse/hoff?branch=v0"]
image:https://goreportcard.com/badge/github.com/rlespinasse/hoff["Go Report Card", link="https://goreportcard.com/report/github.com/rlespinasse/hoff"]

**HOFF** is a library to configure and run a computation engine in order to process data throught a high order functions workflow.

== Usage

Import the `HOFF` library in your **golang** project

[source,go]
----
import "github.com/rlespinasse/hoff"
----
Than read the documentation image:https://pkg.go.dev/badge/github.com/rlespinasse/hoff["GoDoc", link="https://pkg.go.dev/github.com/rlespinasse/hoff"] to have some usage examples.

== Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

== License

https://choosealicense.com/licenses/mit/[MIT]

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

View Source
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"
)
View Source
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

View Source
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

type ComputeState struct {
	Value  StateType
	Branch *bool
	Error  error
}

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

func NewContext(data map[string]interface{}) *Context

NewContext generate a new Context with data

func NewContextWithoutData

func NewContextWithoutData() *Context

NewContextWithoutData generate a new empty Context

func (*Context) Delete

func (c *Context) Delete(key string)

Delete remove a value in the context by its key

func (Context) Equal

func (c Context) Equal(o Context) bool

Equal validate the two Context are equals

func (*Context) HaveKey

func (c *Context) HaveKey(key string) bool

HaveKey validate that a key is in the context

func (*Context) Read

func (c *Context) Read(key string) (interface{}, bool)

Read get a value in the context by its key

func (*Context) Store

func (c *Context) Store(key string, value interface{})

Store add a key and its value to the 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

func NewDecisionNode(name string, decisionFunc func(*Context) (bool, error)) (*DecisionNode, error)

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 (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

type StateType

type StateType string

StateType is the type of the computation state of a Node during a Computation

Jump to

Keyboard shortcuts

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