-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruleengine.go
196 lines (163 loc) · 4.94 KB
/
ruleengine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// rulengine-core is a strictly typed rule engine library, providing a simple interface to create ruleengine and evaluate rule for given input.
package ruleenginecore
import (
"context"
"fmt"
"sort"
)
type RuleEngine interface {
// 'Evaluate' evaluates the input based on options
Evaluate(ctx context.Context, input Input, op *evaluateOption) ([]*Output, *RuleEngineError)
// 'EvaluateSingleRule' evaluates the input for one rule having given 'rulename'
EvaluateSingleRule(ctx context.Context, input Input, rulename string) (*Output, *RuleEngineError)
}
type rule struct {
name string
priority int
rootEvaluator evaluator
result map[string]any
}
func newRule(ruleName string, r *RuleConfig, customConditionType map[string]*ConditionType, fs Fields) (*rule, *RuleEngineError) {
rootEvaluator, err := ruleEvaluatorBuild(r.RootCondition, customConditionType, fs)
if err != nil {
return nil, err
}
ru := &rule{
name: ruleName,
priority: r.Priority,
result: r.Result,
rootEvaluator: rootEvaluator,
}
return ru, nil
}
func (r *rule) evaluate(ctx context.Context, input parsedInput) (bool, *RuleEngineError) {
out := make(chan bool)
ctxCancelled := false
go func(ctx context.Context, input parsedInput, resultChan chan<- bool) {
select {
case <-ctx.Done():
ctxCancelled = true
resultChan <- false
default:
resultChan <- r.rootEvaluator.evaluate(input)
}
}(ctx, input, out)
result := <-out
if ctxCancelled {
return false, newError(ErrCodeContextCancelled,
fmt.Sprintf("Context cancelled while evaluating RuleName: %v", r.name))
}
return result, nil
}
type ruleEngine struct {
fields Fields
// map of rulename and rule
ruleMap map[string]*rule
// ascending ordered rules
rules []*rule
}
func (re *ruleEngine) validateAndParseInput(input Input) (parsedInput, *RuleEngineError) {
ret := parsedInput{}
for fieldname, fieldtype := range re.fields {
strVal, found := input[fieldname]
if !found {
return nil, newError(ErrCodeFieldNotFound,
fmt.Sprintf("Expecting input with name: %v and valueType: %v", fieldname, fieldtype))
}
if val, err := parseValue(strVal, fieldtype); err != nil {
err.addMsg(fmt.Sprintf("Input parsing failed for field: %v having type %v", fieldname, fieldtype))
return nil, err
} else {
ret[fieldname] = val
}
}
return ret, nil
}
func (re *ruleEngine) Evaluate(ctx context.Context, input Input, op *evaluateOption) ([]*Output, *RuleEngineError) {
parsedInput, err := re.validateAndParseInput(input)
if err != nil {
return nil, err
}
if op.evalType == complete {
return re.ascendingEvaluation(ctx, parsedInput, len(re.rules))
} else if op.evalType == ascendingPriorityBased {
return re.ascendingEvaluation(ctx, parsedInput, op.limit)
} else {
return re.descendingEvaluation(ctx, parsedInput, op.limit)
}
}
func (re *ruleEngine) ascendingEvaluation(ctx context.Context, input parsedInput, limit int) ([]*Output, *RuleEngineError) {
result := []*Output{}
for i := 0; i < len(re.rules); i++ {
rule := re.rules[i]
matched, err := rule.evaluate(ctx, input)
if err != nil {
return nil, err
}
if matched {
result = append(result, newOutput(rule.name, rule.priority, rule.result))
if len(result) == limit {
break
}
}
}
return result, nil
}
func (re *ruleEngine) descendingEvaluation(ctx context.Context, input parsedInput, limit int) ([]*Output, *RuleEngineError) {
result := []*Output{}
for i := len(re.rules) - 1; i >= 0; i-- {
rule := re.rules[i]
matched, err := rule.evaluate(ctx, input)
if err != nil {
return nil, err
}
if matched {
result = append(result, newOutput(rule.name, rule.priority, rule.result))
if len(result) == limit {
break
}
}
}
return result, nil
}
func (re *ruleEngine) EvaluateSingleRule(ctx context.Context, input Input, rulename string) (*Output, *RuleEngineError) {
parsedInput, err := re.validateAndParseInput(input)
if err != nil {
return nil, err
}
rule, ok := re.ruleMap[rulename]
if !ok {
return nil, newError(ErrCodeRuleNotFound, fmt.Sprintf("RuleName: %v", rulename))
}
matched, err := rule.evaluate(ctx, parsedInput)
if err != nil {
return nil, err
}
if matched {
return newOutput(rulename, rule.priority, rule.result), nil
}
return nil, nil
}
// creates new rule engine using provided configuration
func New(engineConfig *RuleEngineConfig) (RuleEngine, *RuleEngineError) {
if err := engineConfigValidator.validate(engineConfig); err != nil {
return nil, err
}
engine := ruleEngine{
fields: engineConfig.Fields,
ruleMap: map[string]*rule{},
rules: []*rule{},
}
for ruleName, r := range engineConfig.Rules {
ru, err := newRule(ruleName, r, engineConfig.ConditionTypes, engine.fields)
if err != nil {
return nil, err
}
engine.ruleMap[ruleName] = ru
engine.rules = append(engine.rules, ru)
}
sort.Slice(engine.rules, func(i, j int) bool {
return engine.rules[i].priority < engine.rules[j].priority
})
return &engine, nil
}