-
Notifications
You must be signed in to change notification settings - Fork 1
/
shared_types.go
148 lines (125 loc) · 4.33 KB
/
shared_types.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
package rulesengine
import (
"encoding/json"
"fmt"
"github.com/asaskevich/EventBus"
"sync"
)
type Event struct {
Type string
Params map[string]interface{}
}
type FactOptions struct {
Cache bool
Priority int
}
type DynamicFactCallback func(almanac *Almanac, params ...interface{}) *ValueNode
type EventCallback func(result *RuleResult) interface{}
type EvaluationResult struct {
Result bool `json:"Result"`
LeftHandSideValue Fact `json:"LeftHandSideValue"`
RightHandSideValue interface{} `json:"RightHandSideValue"`
Operator string `json:"Operator"`
}
const (
READY = "READY"
RUNNING = "RUNNING"
FINISHED = "FINISHED"
)
// RuleProperties represents the properties of a rule.
type RuleProperties struct {
Conditions TopLevelCondition `json:"conditions"`
Event Event `json:"event"`
Name *string `json:"name,omitempty"`
Priority *int `json:"priority,omitempty"`
OnSuccess *EventHandler `json:"onSuccess,omitempty"`
OnFailure *EventHandler `json:"onFailure,omitempty"`
}
// TopLevelCondition represents the top-level condition, which can be AllConditions, AnyConditions, NotConditions, or ConditionReference.
type TopLevelCondition struct {
All *[]ConditionProperties `json:"all,omitempty"`
Any *[]ConditionProperties `json:"any,omitempty"`
Not *ConditionProperties `json:"not,omitempty"`
Condition *string `json:"condition,omitempty"`
Name *string `json:"name,omitempty"`
Priority *int `json:"priority,omitempty"`
}
// EventHandler represents an event handler function.
type EventHandler func(event Event, almanac Almanac, ruleResult RuleResult)
// ConditionProperties represents a condition inEvaluator the rule.
type ConditionProperties struct {
Fact string `json:"fact"`
Operator string `json:"operator"`
Value interface{} `json:"value"`
Path *string `json:"path,omitempty"`
Priority *int `json:"priority,omitempty"`
Params map[string]interface{} `json:"params,omitempty"`
Name *string `json:"name,omitempty"`
}
func (c *ConditionProperties) SetPriority(priority int) {
c.Priority = &priority
}
func (c *ConditionProperties) SetName(name string) {
c.Name = &name
}
type ConditionMap struct {
sync.Map
}
func (m *ConditionMap) Load(key string) (Condition, bool) {
val, ok := m.Map.Load(key)
if !ok {
return Condition{}, false
}
return val.(Condition), ok
}
func (m *ConditionMap) Store(key string, value Condition) {
m.Map.Store(key, value)
}
// Engine represents the core of the rules engine, responsible for managing and executing rules.
// It holds the rules, operators, and configuration options needed to evaluate facts against conditions.
// The engine also manages the event bus used for dispatching events during rule execution.
type Engine struct {
Rules []*Rule
AllowUndefinedFacts bool
AllowUndefinedConditions bool
ReplaceFactsInEventParams bool
Operators map[string]Operator
Facts FactMap
Conditions ConditionMap
Status string
prioritizedRules [][]*Rule
bus EventBus.Bus
mu sync.Mutex
}
type RuleEngineOptions struct {
AllowUndefinedFacts bool
AllowUndefinedConditions bool
ReplaceFactsInEventParams bool
}
type RuleConfig struct {
Name string `json:"name"`
Priority *int `json:"priority"`
Conditions Condition `json:"conditions"`
Event EventConfig `json:"event"`
OnSuccess func(result *RuleResult) interface{}
OnFailure func(result *RuleResult) interface{}
}
// UnmarshalJSON is a custom JSON unmarshaller for RuleConfig to ensure proper unmarshaling of Condition
func (r *RuleConfig) UnmarshalJSON(data []byte) error {
// Define an alias to avoid recursion
type Alias RuleConfig
aux := &struct {
*Alias
}{
Alias: (*Alias)(r),
}
// Unmarshal the data into the auxiliary struct
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
// Now manually unmarshal and validate the Conditions field
if err := json.Unmarshal(data, &r.Conditions); err != nil {
return fmt.Errorf("failed to unmarshal conditions: %v", err)
}
return nil
}