ruleeng

package
v4.2.7 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2023 License: MIT Imports: 8 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action interface {
	GetName() string
	GetParameters() map[string]interface{}
	GetMetaData() map[string]interface{}
}

Action ... See DefaultAction for an example implementation.

type ActionDef

type ActionDef struct {
	Name       Expression            `json:"name"`
	Parameters map[string]Expression `json:"parameters"`
}

ActionDef action definition

func (ActionDef) Resolve

func (a ActionDef) Resolve(k KnowledgeBase) (DefaultAction, error)

Resolve resolves the ActionDef into a DefaultAction

func (*ActionDef) UnmarshalJSON

func (a *ActionDef) UnmarshalJSON(data []byte) error

UnmarshalJSON unmashals a quoted json string to Expression

type Case

type Case struct {
	Name      string      `json:"name"`
	Condition Expression  `json:"condition"`
	Actions   []ActionDef `json:"actions"`
}

Case : pair condition tasks use to compose a Rule

func (*Case) UnmarshalJSON

func (c *Case) UnmarshalJSON(data []byte) error

UnmarshalJSON unmashals a quoted json string to Expression

type DefaultAction

type DefaultAction struct {
	Name       string                 `json:"name"`
	Parameters map[string]interface{} `json:"parameters"`
	MetaData   map[string]interface{} `json:"metaData"`
}

DefaultAction default action implementation

func (DefaultAction) GetMetaData

func (a DefaultAction) GetMetaData() map[string]interface{}

GetMetaData returns the action metadata

func (DefaultAction) GetName

func (a DefaultAction) GetName() string

GetName returns the action name

func (DefaultAction) GetParameters

func (a DefaultAction) GetParameters() map[string]interface{}

GetParameters returns the action parameters

type DefaultKnowledgeBase

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

DefaultKnowledgeBase default knowledge base implementation

func (*DefaultKnowledgeBase) GetFact

func (kBase *DefaultKnowledgeBase) GetFact(key string) interface{}

GetFact returns the value of a key in the facts map

func (*DefaultKnowledgeBase) GetFacts

func (kBase *DefaultKnowledgeBase) GetFacts() map[string]interface{}

GetFacts returns the facts maps of the KBase

func (*DefaultKnowledgeBase) InsertFact

func (kBase *DefaultKnowledgeBase) InsertFact(key string, value interface{})

InsertFact inserts a key value (fact) in the facts map

func (*DefaultKnowledgeBase) Reset

func (kBase *DefaultKnowledgeBase) Reset()

Reset removes all de facts and indexs in the KBase

func (*DefaultKnowledgeBase) SetDefaultValues

func (kBase *DefaultKnowledgeBase) SetDefaultValues(parameters map[string]interface{})

SetDefaultValues add defaults values at the facts in the KBase

func (*DefaultKnowledgeBase) SetFacts

func (kBase *DefaultKnowledgeBase) SetFacts(facts map[string]interface{})

SetFacts overwrides the facts in the KBase

func (*DefaultKnowledgeBase) String

func (kBase *DefaultKnowledgeBase) String() string

type DefaultRule

type DefaultRule struct {
	ID         int64                  `json:"id,omitempty"`
	Cases      []Case                 `json:"cases"`
	Version    int64                  `json:"version"`
	Parameters map[string]interface{} `json:"parameters"`
}

DefaultRule default rule implementation

func (DefaultRule) Execute

func (r DefaultRule) Execute(k KnowledgeBase) []Action

Execute executes the rule and return the resulting actions

func (DefaultRule) GetDefaultValues

func (r DefaultRule) GetDefaultValues() map[string]interface{}

GetDefaultValues returns rule default values

func (DefaultRule) GetID

func (r DefaultRule) GetID() int64

GetID returns the rule id

func (*DefaultRule) UnmarshalJSON

func (r *DefaultRule) UnmarshalJSON(data []byte) error

UnmarshalJSON unmashals a quoted json string to Expression

type DefaultRuleBase

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

DefaultRuleBase default rule base implementation

func (*DefaultRuleBase) ExecuteAll

func (rBase *DefaultRuleBase) ExecuteAll(k KnowledgeBase) []Action

ExecuteAll executes all the rules of the ruleBase using the knowledgeBase provided as parameter

func (*DefaultRuleBase) ExecuteByID

func (rBase *DefaultRuleBase) ExecuteByID(ruleID int64, k KnowledgeBase) ([]Action, error)

ExecuteByID executes the rule with the name provide in the parameter 'ruleName' using the knowledgeBase provided as parameter

func (*DefaultRuleBase) ExecuteRules

func (rBase *DefaultRuleBase) ExecuteRules(ruleIDs []int64, k KnowledgeBase) []Action

ExecuteRules executes a list of the rules of the ruleBase using the knowledgeBase provided as parameter

func (*DefaultRuleBase) GetRules

func (rBase *DefaultRuleBase) GetRules() map[int64]Rule

GetRules returns the rules

func (*DefaultRuleBase) InsertRule

func (rBase *DefaultRuleBase) InsertRule(rule Rule)

InsertRule allows to inser a Rule in the rulesBase

func (*DefaultRuleBase) InsertRules

func (rBase *DefaultRuleBase) InsertRules(rules []Rule)

InsertRules allows to inser a liste of Rules in the rulesBase

func (*DefaultRuleBase) RemoveRule

func (rBase *DefaultRuleBase) RemoveRule(id int64)

RemoveRule allows to remove a Rule in the rulesBase

func (*DefaultRuleBase) Reset

func (rBase *DefaultRuleBase) Reset()

Reset removes the rules, tasks and errors of the rulesBase

type Expression

type Expression string

Expression struct to represent an expression

func (Expression) Evaluate

func (exp Expression) Evaluate(k KnowledgeBase) (interface{}, error)

Evaluate evaluates the expression and return the result as interface{}

func (Expression) EvaluateAsBool

func (exp Expression) EvaluateAsBool(k KnowledgeBase) (bool, error)

EvaluateAsBool evaluates the expression and verifies that the result is the type boolean use to evaluate the conditions in the rule cases (conditions should return a boolean as result)

func (Expression) EvaluateAsString

func (exp Expression) EvaluateAsString(k KnowledgeBase) (string, error)

EvaluateAsString evaluates the expression and verifies that the result is the type string

func (Expression) MarshalJSON

func (exp Expression) MarshalJSON() ([]byte, error)

MarshalJSON mashals a Expression to a a quoted json string

func (*Expression) UnmarshalJSON

func (exp *Expression) UnmarshalJSON(data []byte) error

UnmarshalJSON unmashals a quoted json string to Expression

type KnowledgeBase

type KnowledgeBase interface {
	GetFacts() map[string]interface{}
	InsertFact(key string, value interface{})
	SetFacts(facts map[string]interface{})
	SetDefaultValues(parameters map[string]interface{})
	GetFact(key string) interface{}
	Reset()
}

KnowledgeBase ...

func NewKBase

func NewKBase() KnowledgeBase

NewKBase builds a KBase

type Rule

type Rule interface {
	GetID() int64
	GetDefaultValues() map[string]interface{}
	Execute(k KnowledgeBase) []Action
}

Rule ... See DefaultRule for an example implementation.

type RuleBase

type RuleBase interface {
	GetRules() map[int64]Rule
	InsertRule(r Rule)
	InsertRules(rules []Rule)
	RemoveRule(id int64)
	Reset()
	ExecuteAll(k KnowledgeBase) []Action
	ExecuteRules(ids []int64, k KnowledgeBase) []Action
	ExecuteByID(id int64, k KnowledgeBase) ([]Action, error)
}

RuleBase ...

func NewRBase

func NewRBase() RuleBase

NewRBase creates a new rulesBase

type RuleEngine

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

RuleEngine represents an instance of a rule engine

func NewRuleEngine

func NewRuleEngine() *RuleEngine

NewRuleEngine builds a RuleEngine

func (*RuleEngine) ExecuteAllRules

func (engine *RuleEngine) ExecuteAllRules()

ExecuteAllRules executes all the rules in the baseRules using the knowledgeBase

func (*RuleEngine) ExecuteRule

func (engine *RuleEngine) ExecuteRule(id int64) error

ExecuteRule executes a single rule by id using the knowledgeBase

func (*RuleEngine) ExecuteRules

func (engine *RuleEngine) ExecuteRules(ids []int64)

ExecuteRules executes all a list of rules in the baseRules using the knowledgeBase

func (*RuleEngine) GetKnowledgeBase

func (engine *RuleEngine) GetKnowledgeBase() KnowledgeBase

GetKnowledgeBase returns the knowledgeBase in the RuleEngine

func (*RuleEngine) GetResults

func (engine *RuleEngine) GetResults() []Action

GetResults returns the Results of the rules executed

func (*RuleEngine) GetRulesBase

func (engine *RuleEngine) GetRulesBase() RuleBase

GetRulesBase returns the RulesBase in the RuleEngine

func (*RuleEngine) InsertKnowledge

func (engine *RuleEngine) InsertKnowledge(key string, value interface{})

InsertKnowledge inserts a key value (fact) in the knowledgeBase of the RuleEngine

func (*RuleEngine) InsertRule

func (engine *RuleEngine) InsertRule(r Rule)

InsertRule inserts a rule in the ruleBase of the RuleEngine

func (*RuleEngine) InsertRules

func (engine *RuleEngine) InsertRules(rules []Rule)

InsertRules inserts a slice of rules in the ruleBase of the RuleEngine

func (*RuleEngine) RemoveRule

func (engine *RuleEngine) RemoveRule(id int64)

RemoveRule remocves a rule in the ruleBase of the RuleEngine

func (*RuleEngine) Reset

func (engine *RuleEngine) Reset()

Reset remove all the results added in previous rules execution

func (*RuleEngine) SetKnowledge

func (engine *RuleEngine) SetKnowledge(k KnowledgeBase)

SetKnowledge overwrites the knowledgeBase in the RuleEngine

func (*RuleEngine) SetRules

func (engine *RuleEngine) SetRules(r RuleBase)

SetRules overwrites the ruleBase in the RuleEngine

Jump to

Keyboard shortcuts

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