forked from xlvector/rulengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rulengine.go
217 lines (198 loc) · 4.85 KB
/
rulengine.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package rulengine
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/going/rulengine/expression"
"github.com/going/rulengine/facts"
"github.com/going/rulengine/logic"
)
type RuleEngine struct {
expressions [][]string
expressionNames []string
varCountInExpressions []int
expressionIndex map[string][]int
rules [][]string
actions []string
ruleIndex map[string][]int
}
func NewRuleEngine() *RuleEngine {
ret := RuleEngine{}
ret.expressions = [][]string{}
ret.expressionNames = []string{}
ret.varCountInExpressions = []int{}
ret.expressionIndex = make(map[string][]int)
ret.ruleIndex = make(map[string][]int)
ret.rules = [][]string{}
ret.actions = []string{}
return &ret
}
func (self *RuleEngine) AddExpression(expr string, name string) {
tks := expression.Tokenize(expr)
reversePolishExpr := expression.ToReversePolishNotation(tks)
size := len(self.expressions)
nv := 0
for _, tk := range reversePolishExpr {
if tk[0] == '$' {
_, ok := self.expressionIndex[tk]
if !ok {
self.expressionIndex[tk] = []int{}
}
self.expressionIndex[tk] = append(self.expressionIndex[tk], size)
nv += 1
}
}
self.varCountInExpressions = append(self.varCountInExpressions, nv)
self.expressionNames = append(self.expressionNames, name)
self.expressions = append(self.expressions, reversePolishExpr)
}
func (self *RuleEngine) GetFiredExpressions(data *facts.FactCollection) []string {
counter := make(map[int]int)
for _, k := range data.Keys() {
ids, ok := self.expressionIndex[k]
if ok {
for _, id := range ids {
v, exist := counter[id]
if !exist {
counter[id] = 1
} else {
counter[id] = v + 1
}
}
}
}
firedExpressions := []string{}
for k, v := range counter {
fmt.Println(self.expressions[k])
if v == self.varCountInExpressions[k] {
ret := expression.CalcReversePolishNotation(self.expressions[k], data)
if ret == "true" {
fmt.Println("fire expression", self.expressionNames[k], self.expressions[k])
firedExpressions = append(firedExpressions, self.expressionNames[k])
}
}
}
return firedExpressions
}
func (self *RuleEngine) AddRule(rule *logic.Rule) {
andOrRule := logic.AndOrFormat(rule.Expression)
for _, e := range andOrRule.Sets {
keys := strings.Split(e.ToString(), "\t")
size := len(self.rules)
self.rules = append(self.rules, keys)
self.actions = append(self.actions, rule.Action)
for _, key := range keys {
_, ok := self.ruleIndex[key]
if ok {
self.ruleIndex[key] = append(self.ruleIndex[key], size)
} else {
self.ruleIndex[key] = []int{size}
}
}
}
}
type Action struct {
Name string
Reason string
}
func (self *RuleEngine) GetAction(data *facts.FactCollection) []*Action {
exprs := self.GetFiredExpressions(data)
ret := []*Action{}
keys := make(map[string]bool)
for _, key := range exprs {
keys[key] = true
}
checked := make(map[string]bool)
checkedRules := make(map[int]bool)
counter := make(map[int]int)
for {
if len(keys) == 0 {
break
}
for key, _ := range keys {
checked[key] = true
ids, ok := self.ruleIndex[key]
if ok {
for _, id := range ids {
_, ok2 := counter[id]
if ok2 {
counter[id] += 1
} else {
counter[id] = 1
}
}
}
}
for id, c := range counter {
if len(self.rules[id]) == c {
_, ok := checkedRules[id]
if ok {
continue
}
checkedRules[id] = true
ret = append(ret, &(Action{Name: self.actions[id], Reason: strings.Join(self.rules[id], "&")}))
_, ok = checked[self.actions[id]]
if ok {
continue
}
keys[self.actions[id]] = true
}
}
for key, _ := range checked {
delete(keys, key)
}
}
return ret
}
type ActionRecord struct {
Action string `json:"action"`
Reasons []string `json:"reasons"`
}
func ConverActionListToActionRecords(actions []*Action) []*ActionRecord {
dict := make(map[string][]int)
for k, a := range actions {
_, ok := dict[a.Name]
if !ok {
dict[a.Name] = []int{}
}
dict[a.Name] = append(dict[a.Name], k)
}
ret := []*ActionRecord{}
for a, ids := range dict {
ar := ActionRecord{
Action: a,
Reasons: []string{},
}
for _, i := range ids {
ar.Reasons = append(ar.Reasons, actions[i].Reason)
}
ret = append(ret, &ar)
}
return ret
}
func (self *RuleEngine) Load(fname string) {
f, err := os.Open(fname)
if err != nil {
panic(err)
}
defer f.Close()
reader := bufio.NewReader(f)
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
line = strings.Trim(line, "\n")
if strings.Contains(line, ":=") {
tks := strings.Split(line, ":=")
self.AddExpression(strings.Trim(tks[1], " "), strings.Trim(tks[0], " "))
continue
}
if strings.Contains(line, "->") {
tks := strings.Split(line, "->")
rule := logic.Rule{Expression: tks[0], Action: strings.Trim(tks[1], " ")}
self.AddRule(&rule)
}
}
}