fished

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2020 License: MIT Imports: 7 Imported by: 0

README

fished

Fish answers from rules and facts

Install

$ go get -u github.com/jonathansudibya/fished

or using dep

$ dep ensure -add github.com/jonathansudibya/fished

Structs

Rule :

type Rule struct {
	Output 			string   `json:"output"`
	Input  			[]string `json:"input"`
	Expression   	string   `json:"expression"`
	...
}

Engine:

type Engine struct {
    Facts         map[string]interface{}
	Rules         []Rule
	RuleFunctions map[string]govaluate.ExpressionFunction
	Jobs          chan int
    ...
}

Example

Please see in test file especially TestRun() function.

package main

import "github.com/jonathansudibya/fished"

func main() {
	e := fished.New(nil)
	e.SetRules([]Rule{
		Rule{
			Input: []string{"hello"},
			Output: "result_end",
			Expression: "hello == 'world'",
		}
	})
	e.SetFacts = map[string]interface{}{
		"hello": "world"
	}
	res, errs := e.Run()

	fmt.Println(res) // will result true
}

Notes

Remember it is more expensive to set new rules than to set facts.

Credits

This project is powered by

LICENSE

See LICENSE.md

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//DefaultTarget is the default target facts
	DefaultTarget = "result_end"

	// DefaultWorker is the default worker for Engine
	DefaultWorker = 0

	// DefaultRuleLength ...
	DefaultRuleLength = 100
)

Functions

This section is empty.

Types

type Engine

type Engine struct {
	InitialFacts  map[string]interface{}
	Rules         []Rule
	RuleFunctions map[string]govaluate.ExpressionFunction
	RuleCache     *cache.Cache
	RunLock       sync.RWMutex
	RuntimePool   *pool.ReferenceCountedPool
}

Engine core of the machine

func New

func New() *Engine

New will create new engine

func NewWithCustomWorkerSize added in v0.6.0

func NewWithCustomWorkerSize(worker int) *Engine

NewWithCustomWorkerSize ...

func (*Engine) NewRuntime added in v0.6.0

func (e *Engine) NewRuntime(facts map[string]interface{}) *Runtime

NewRuntime ...

func (*Engine) Run

func (e *Engine) Run(target string, worker int) (interface{}, []error)

Run will execute rule and facts to get the result DEPRICATION NOTICE : worker param is depricated since it has been moved to engine struct

func (*Engine) RunDefault added in v0.5.0

func (e *Engine) RunDefault() (interface{}, []error)

RunDefault will execute run with default parameneter

func (*Engine) RunWithCustomTarget added in v0.6.0

func (e *Engine) RunWithCustomTarget(target string) (interface{}, []error)

RunWithCustomTarget will execute run using customizable end target

func (*Engine) Set added in v0.5.0

func (e *Engine) Set(facts map[string]interface{}, rules []Rule, ruleFunction map[string]RuleFunction) error

Set all of engine attibutes in one single function

func (*Engine) SetFacts added in v0.4.0

func (e *Engine) SetFacts(facts map[string]interface{}) error

SetFacts will set current engine with initial facts (replace the old one)

func (*Engine) SetRuleFunctions added in v0.5.0

func (e *Engine) SetRuleFunctions(ruleFunctions map[string]RuleFunction) error

SetRuleFunctions will set current engine with Expression Functions

func (*Engine) SetRules added in v0.4.0

func (e *Engine) SetRules(rules []Rule) error

SetRules will set current engine with rules

type EvalResult added in v0.5.0

type EvalResult struct {
	Key   string
	Value interface{}
	Error error
}

EvalResult is evaluation Result

type Job added in v0.5.0

type Job struct {
	Output           string
	ParsedExpression *govaluate.EvaluableExpression
}

Job struct

type Rule

type Rule struct {
	Input      []string `json:"input"`
	Output     string   `json:"output"`
	Expression string   `json:"expression"`
}

Rule is struct for rule in fished

type RuleFunction added in v0.2.0

type RuleFunction func(...interface{}) (interface{}, error)

RuleFunction if type defined for rule function

type Runtime added in v0.5.0

type Runtime struct {
	pool.ReferenceCounter
	Facts      map[string]interface{}
	JobCh      chan *Job
	ResultCh   chan *EvalResult
	UsedRule   map[int]struct{}
	FactsMutex sync.RWMutex
}

Runtime is an struct for each time Engine.Run() is called

func (*Runtime) Evaluate added in v0.5.0

func (r *Runtime) Evaluate(job *Job, result chan<- *EvalResult)

Evaluate will evaluate each job in runtime

func (*Runtime) Reset added in v0.6.0

func (r *Runtime) Reset() error

Reset Current Runtime

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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