Skip to content

Commit 8747b04

Browse files
xiaoquanideanewm4n
andauthored
GruleEngineListener add ctx parameters (#465)
Co-authored-by: Ferdinand Neman <ferdinand.neman@gmail.com>
1 parent 319a121 commit 8747b04

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

engine/GruleEngine.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,28 @@ func (g *GruleEngine) Execute(dataCtx ast.IDataContext, knowledge *ast.Knowledge
9090
}
9191

9292
// notifyEvaluateRuleEntry will notify all registered listener that a rule is being evaluated.
93-
func (g *GruleEngine) notifyEvaluateRuleEntry(cycle uint64, entry *ast.RuleEntry, candidate bool) {
93+
func (g *GruleEngine) notifyEvaluateRuleEntry(ctx context.Context, cycle uint64, entry *ast.RuleEntry, candidate bool) {
9494
if g.Listeners != nil && len(g.Listeners) > 0 {
9595
for _, gl := range g.Listeners {
96-
gl.EvaluateRuleEntry(cycle, entry, candidate)
96+
gl.EvaluateRuleEntry(ctx, cycle, entry, candidate)
9797
}
9898
}
9999
}
100100

101101
// notifyEvaluateRuleEntry will notify all registered listener that a rule is being executed.
102-
func (g *GruleEngine) notifyExecuteRuleEntry(cycle uint64, entry *ast.RuleEntry) {
102+
func (g *GruleEngine) notifyExecuteRuleEntry(ctx context.Context, cycle uint64, entry *ast.RuleEntry) {
103103
if g.Listeners != nil && len(g.Listeners) > 0 {
104104
for _, gl := range g.Listeners {
105-
gl.ExecuteRuleEntry(cycle, entry)
105+
gl.ExecuteRuleEntry(ctx, cycle, entry)
106106
}
107107
}
108108
}
109109

110110
// notifyEvaluateRuleEntry will notify all registered listener that a rule is being executed.
111-
func (g *GruleEngine) notifyBeginCycle(cycle uint64) {
111+
func (g *GruleEngine) notifyBeginCycle(ctx context.Context, cycle uint64) {
112112
if g.Listeners != nil && len(g.Listeners) > 0 {
113113
for _, gl := range g.Listeners {
114-
gl.BeginCycle(cycle)
114+
gl.BeginCycle(ctx, cycle)
115115
}
116116
}
117117
}
@@ -161,7 +161,7 @@ func (g *GruleEngine) ExecuteWithContext(ctx context.Context, dataCtx ast.IDataC
161161
return ctx.Err()
162162
}
163163

164-
g.notifyBeginCycle(cycle + 1)
164+
g.notifyBeginCycle(ctx, cycle+1)
165165

166166
// Select all rule entry that can be executed.
167167
log.Tracef("Select all rule entry that can be executed.")
@@ -187,7 +187,7 @@ func (g *GruleEngine) ExecuteWithContext(ctx context.Context, dataCtx ast.IDataC
187187
runnable = append(runnable, ruleEntry)
188188
}
189189
// notify all listeners that a rule's when scope is been evaluated.
190-
g.notifyEvaluateRuleEntry(cycle+1, ruleEntry, can)
190+
g.notifyEvaluateRuleEntry(ctx, cycle+1, ruleEntry, can)
191191
}
192192
}
193193

@@ -221,7 +221,7 @@ func (g *GruleEngine) ExecuteWithContext(ctx context.Context, dataCtx ast.IDataC
221221
// set the current rule entry to run. This is for trace ability purpose
222222
dataCtx.SetRuleEntry(runner)
223223
// notify listeners that we are about to execute a rule entry then scope
224-
g.notifyExecuteRuleEntry(cycle, runner)
224+
g.notifyExecuteRuleEntry(ctx, cycle, runner)
225225
// execute the top most prioritized rule
226226
err := runner.Execute(ctx, dataCtx, knowledge.WorkingMemory)
227227
if err != nil {

engine/GruleEngineListener.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package engine
22

3-
import "github.com/hyperjumptech/grule-rule-engine/ast"
3+
import (
4+
"context"
5+
"github.com/hyperjumptech/grule-rule-engine/ast"
6+
)
47

58
// GruleEngineListener is an interface to be implemented by those who want to listen the Engine execution.
69
type GruleEngineListener interface {
710
// EvaluateRuleEntry will be called by the engine if it evaluate a rule entry
8-
EvaluateRuleEntry(cycle uint64, entry *ast.RuleEntry, candidate bool)
11+
EvaluateRuleEntry(ctx context.Context, cycle uint64, entry *ast.RuleEntry, candidate bool)
912
// ExecuteRuleEntry will be called by the engine if it execute a rule entry in a cycle
10-
ExecuteRuleEntry(cycle uint64, entry *ast.RuleEntry)
13+
ExecuteRuleEntry(ctx context.Context, cycle uint64, entry *ast.RuleEntry)
1114
// BeginCycle will be called by the engine every time it start a new evaluation cycle
12-
BeginCycle(cycle uint64)
15+
BeginCycle(ctx context.Context, cycle uint64)
1316
}

0 commit comments

Comments
 (0)