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 ¶
const ( ErrCodeNone = iota ErrCodeInvalidValueType ErrCodeInvalidOperator ErrCodeInvalidOperandType ErrCodeInvalidOperandsLength ErrCodeInvalidConditionType ErrCodeInvalidSubConditionCount ErrCodeConditionTypeNotFound ErrCodeInvalidOperandAs ErrCodeFieldNotFound ErrCodeFailedParsingInput ErrCodeRuleNotFound )
const ( GreaterOperator = ">" GreaterEqualOperator = ">=" LessOpeartor = "<" LessEqualOpeartor = "<=" EqualOpearator = "=" NotEqualOperator = "!=" ContainOperator = "contain" NegationOperator = "not" AndOperator = "and" OrOperator = "or" )
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 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 Fields ¶
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 ¶
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 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 ¶
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