Documentation
¶
Index ¶
- type ArgTypeError
- type ArgsCountError
- type Context
- type Engine
- type Fact
- type Func
- type Monitor
- type MonitorHooks
- func (m *MonitorHooks) ActionCompileError(ctx *Context, r *Rule, err error)
- func (m *MonitorHooks) ActionError(ctx *Context, r *Rule, err error)
- func (m *MonitorHooks) ActionIgnore(ctx *Context, r *Rule)
- func (m *MonitorHooks) ActionResult(ctx *Context, r *Rule, v interface{})
- func (m *MonitorHooks) ConditionError(ctx *Context, r *Rule, err error)
- func (m *MonitorHooks) ConditionResult(ctx *Context, r *Rule, f bool)
- type NativeFuncError
- type Rule
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArgTypeError ¶
ArgTypeError reprents mismatch type of an argument.
func (*ArgTypeError) Error ¶
func (err *ArgTypeError) Error() string
type ArgsCountError ¶
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
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 ¶
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) Revivable ¶
Revivable returns revivable flag. Revivable flag means whether evaluate again when other rules are applied or not.
func (*Rule) WithRevivable ¶
WithRevivable changes revivable flag.