rule

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

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

Go to latest
Published: Oct 23, 2017 License: MIT Imports: 5 Imported by: 0

README

Rule engine (experimental)

GoDoc CircleCI

Example code

package rule_test

import (
	"fmt"

	rule "github.com/koron/go-rule"
)

func ExampleEngine() {
	eng := rule.NewEngine()
	eng.AddFuncs(map[string]rule.Func{
		"PRINT": func(ctx *rule.Context, args ...interface{}) (interface{}, error) {
			fmt.Println(args...)
			return true, nil
		},
	})
	eng.AddRule("too cold", `temp < 15`, `PRINT("COLD!")`)
	eng.AddRule("too hot", `temp >= 25`, `PRINT("HOT!")`)
	eng.AddRule("OK", `temp >= 15 && temp < 25`, `PRINT("OK")`)

	// Output:
	// COLD!
	// HOT!
	// OK
	eng.Eval(rule.Fact{"temp": 10.0}, nil)
	eng.Eval(rule.Fact{"temp": 30.0}, nil)
	eng.Eval(rule.Fact{"temp": 20.0}, nil)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArgTypeError

type ArgTypeError struct {
	Pos      int
	Expected string
	Given    interface{} // Given value
}

ArgTypeError reprents mismatch type of an argument.

func (*ArgTypeError) Error

func (err *ArgTypeError) Error() string

type ArgsCountError

type ArgsCountError struct {
	Expected int
	Given    int
}

ArgsCountError represents mismatch of arguments.

func (*ArgsCountError) Error

func (err *ArgsCountError) Error() string

type Context

type Context struct {
	// Fact hols current Fact in evaluation. Note that any modifications on
	// this affects later evaluation.
	Fact Fact
	// contains filtered or unexported fields
}

Context is an evaluation context.

type Engine

type Engine struct {
	// Funcs can be added functions which can be used by actions.
	Funcs map[string]Func
	// contains filtered or unexported fields
}

Engine is a rule engine.

Example
package main

import (
	"fmt"

	rule "github.com/koron/go-rule"
)

func main() {
	// Setup engine and rules.
	eng := rule.NewEngine()
	eng.AddFuncs(map[string]rule.Func{
		"PRINT": func(ctx *rule.Context, args ...interface{}) (interface{}, error) {
			fmt.Println(args...)
			return true, nil
		},
	})
	eng.AddRule("too cold", `temp < 15`, `PRINT("COLD!")`)
	eng.AddRule("too hot", `temp >= 25`, `PRINT("HOT!")`)
	eng.AddRule("OK", `temp >= 15 && temp < 25`, `PRINT("OK")`)
	// Evaluate facts.
	eng.Eval(rule.Fact{"temp": 10.0}, nil)
	eng.Eval(rule.Fact{"temp": 30.0}, nil)
	eng.Eval(rule.Fact{"temp": 20.0}, nil)
}
Output:

COLD!
HOT!
OK

func NewEngine

func NewEngine() *Engine

NewEngine creates a new rule engine.

func (*Engine) AddFuncs

func (eng *Engine) AddFuncs(funcs map[string]Func)

AddFuncs adds functions (set of named Func).

func (*Engine) AddRule

func (eng *Engine) AddRule(name, condition, action string) (*Rule, error)

AddRule compiles a new rule and puts into root rules of Engine.

func (*Engine) Eval

func (eng *Engine) Eval(fact Fact, m Monitor)

Eval evaluates rules with a fact. Evaluation can be monitored by Monitor. nil for m is allowed it make monitor disabled.

type Fact

type Fact map[string]interface{}

Fact is evaluation target context.

func (Fact) Clone

func (f Fact) Clone() Fact

Clone clones a fact except array members.

type Func

type Func func(*Context, ...interface{}) (interface{}, error)

Func represents a function which can be used by actions.

type Monitor

type Monitor interface {
	ConditionError(*Context, *Rule, error)
	ConditionResult(*Context, *Rule, bool)
	ActionIgnore(*Context, *Rule)
	ActionCompileError(*Context, *Rule, error)
	ActionError(*Context, *Rule, error)
	ActionResult(*Context, *Rule, interface{})
}

Monitor provides methods to monitoring evaluation.

type MonitorHooks

type MonitorHooks struct {
	OnConditionError     func(*Context, *Rule, error)
	OnConditionResult    func(*Context, *Rule, bool)
	OnActionIgnore       func(*Context, *Rule)
	OnActionCompileError func(*Context, *Rule, error)
	OnActionError        func(*Context, *Rule, error)
	OnActionResult       func(*Context, *Rule, interface{})
}

MonitorHooks implements Monitor interface and it make easy to implement each monitoring methods.

func (*MonitorHooks) ActionCompileError

func (m *MonitorHooks) ActionCompileError(ctx *Context, r *Rule, err error)

ActionCompileError implements Monitor interface.

func (*MonitorHooks) ActionError

func (m *MonitorHooks) ActionError(ctx *Context, r *Rule, err error)

ActionError implements Monitor interface.

func (*MonitorHooks) ActionIgnore

func (m *MonitorHooks) ActionIgnore(ctx *Context, r *Rule)

ActionIgnore implements Monitor interface.

func (*MonitorHooks) ActionResult

func (m *MonitorHooks) ActionResult(ctx *Context, r *Rule, v interface{})

ActionResult implements Monitor interface.

func (*MonitorHooks) ConditionError

func (m *MonitorHooks) ConditionError(ctx *Context, r *Rule, err error)

ConditionError implements Monitor interface.

func (*MonitorHooks) ConditionResult

func (m *MonitorHooks) ConditionResult(ctx *Context, r *Rule, f bool)

ConditionResult implements Monitor interface.

type NativeFuncError

type NativeFuncError struct {
	Name string
	Err  error
}

NativeFuncError represents runtime error in native function.

func (*NativeFuncError) Error

func (err *NativeFuncError) Error() string

type Rule

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

Rule represents a rule.

func (*Rule) AddRule

func (r *Rule) AddRule(name, condition, action string) (*Rule, error)

AddRule compiles a rule and puts into child rules.

func (*Rule) Name

func (r *Rule) Name() string

Name returns name of rule.

func (*Rule) Revivable

func (r *Rule) Revivable() bool

Revivable returns revivable flag. Revivable flag means whether evaluate again when other rules are applied or not.

func (*Rule) WithName

func (r *Rule) WithName(s string) *Rule

WithName changes name of rule.

func (*Rule) WithRevivable

func (r *Rule) WithRevivable(v bool) *Rule

WithRevivable changes revivable flag.

Jump to

Keyboard shortcuts

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