ruleengine

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2022 License: MIT Imports: 5 Imported by: 0

README

Rule Engine Core

Overview

rulengine-core is a strickly typed rule engine library,provding a simple interface to create ruleengine and evaluate rule for given input. It abstracts a business logic for rule setup and rule evaluation. You can store rules(RuleEngineConfig) as a JSON in a store outside of core rule engine logic, which makes it easy for rule versioning.

Todo
  • documentation
  • prepare user-guide
Todo feature
  • context aware evaluate operation
  • conditions are getting re-evaluated, introduce caching for condition evaluation result and avoid re-evaluation

Documentation

Overview

rulengine-core is a strickly typed rule engine library,provding a simple interface to create ruleengine and evaluate rule for given input. It abstracts a business logic for rule setup and rule evaluation.

Index

Constants

View Source
const (
	ErrCodeNone = iota
	ErrCodeInvalidValueType
	ErrCodeInvalidOperator
	ErrCodeInvalidOperandType
	ErrCodeInvalidOperandsLength
	ErrCodeInvalidConditionType
	ErrCodeInvalidSubConditionCount
	ErrCodeConditionTypeNotFound
	ErrCodeInvalidOperandAs
	ErrCodeFieldNotFound
	ErrCodeFailedParsingInput
	ErrCodeRuleNotFound
)
View Source
const (
	GreaterOperator      = ">"
	GreaterEqualOperator = ">="
	LessOpeartor         = "<"
	LessEqualOpeartor    = "<="
	EqualOpearator       = "="
	NotEqualOperator     = "!="
	ContainOperator      = "contain"

	NegationOperator = "not"
	AndOperator      = "and"
	OrOperator       = "or"
)
View Source
const (
	IntType    = "int"
	FloatType  = "float"
	BoolType   = "bool"
	StringType = "string"

	OperandAsField    = "field"
	OperandAsConstant = "constant"
)

Variables

This section is empty.

Functions

func EvaluateOptions

func EvaluateOptions() *evaluateOptionSelector

options for rule engine evaluate operation

Types

type AnyValueMap

type AnyValueMap map[string]any

type Condition

type Condition struct {
	ConditionType string       `json:"conditionType"`
	SubConditions []*Condition `json:"subConditions"`
}

type ConditionType

type ConditionType struct {
	Operator    string     `json:"operator"`
	OperandType string     `json:"operandType"`
	Operands    []*Operand `json:"opearands"`
}

defines a custom condition type which can be used as part of rule definition Valid Operators are '>','>=','<','<=','==', '!=', 'contain' '>','>=','<','<=' operators supports 'int', 'float' operandType '>','>=','<','<=','==', '!=' operator support 'int','float','bool','string' operandType 'contain' operator supports 'string' operandType Operator, OperandType and Operands can be defined as following

type Evaluator

type Evaluator interface {
	// contains filtered or unexported methods
}

type Fields

type Fields map[string]string

defines a mandatory fields (fieldname and fieldtype) for rule engine input. as part engine evaluation field value is picked from the input via given name.

valid field types are 'int', 'float', 'bool', 'string' 'int' is 64 bit signed integer 'float' is 64 bit signed float 'bool' is boolean true or false 'string' is UTF-8 based string

func (Fields) IsValid

func (fs Fields) IsValid(name, fType string) *RuleEngineError

check for 1. is field exist with <name> 2. does field having <name> have type <fType>

type Input

type Input map[string]string

func (Input) Validate

func (input Input) Validate(fs Fields) (AnyValueMap, *RuleEngineError)

validates for mandatory fields and type conversion from string to respective field type

type Operand

type Operand struct {
	// define operand as field or constant. valid values are 'field' and 'constant'
	OperandAs string `json:"operandAs"`
	Val       string `json:"val"`
	// contains filtered or unexported fields
}

define an Operand for any operator as part of evaluation process operand value is determined as following: if operand.OperandAs is 'field'

operand.Val is fieldname, Operand value is determined from the input having fieldname as <operand.Val>

if operand.OperandAs is 'constant'

operand.Val is considered as operand value in a string form.

type Output

type Output struct {
	Rulename string `json:"rulename"`
	Priority int    `json:"prority"`
	// the same result as an entity defined as part rule engine config for every rule.
	Result map[string]any `json:"result"`
}

defines an output of a successful rule evaluation

type Rule

type Rule struct {
	Priority      int            `json:"prority"`
	RootCondition *Condition     `json:"condition"`
	Result        map[string]any `json:"result"`
}

type RuleEngineConfig

type RuleEngineConfig struct {
	Fields         Fields                    `json:"fields"`
	ConditionTypes map[string]*ConditionType `json:"conditionTypes"`
	Rules          map[string]*Rule          `json:"rules"`
}

func (*RuleEngineConfig) Validate

func (c *RuleEngineConfig) Validate() *RuleEngineError

validates for valid types, operators, condition definition and rule definition.

type RuleEngineError

type RuleEngineError struct {
	ComponentName string
	ErrMsg        string
	ErrCode       uint
	OtherMsg      string
}

func New

func New(engineConfig *RuleEngineConfig) (*ruleEngine, *RuleEngineError)

creates new rule engine with given engine configuration

func NewRuleEngineError

func NewRuleEngineError(errCode uint, componentName string, otherMsg string) *RuleEngineError

func (RuleEngineError) Error

func (ce RuleEngineError) Error() string

Jump to

Keyboard shortcuts

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