Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GruleEngineListener add ctx parameters #465

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions engine/GruleEngine.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,28 @@ func (g *GruleEngine) Execute(dataCtx ast.IDataContext, knowledge *ast.Knowledge
}

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

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

// notifyEvaluateRuleEntry will notify all registered listener that a rule is being executed.
func (g *GruleEngine) notifyBeginCycle(cycle uint64) {
func (g *GruleEngine) notifyBeginCycle(ctx context.Context, cycle uint64) {
if g.Listeners != nil && len(g.Listeners) > 0 {
for _, gl := range g.Listeners {
gl.BeginCycle(cycle)
gl.BeginCycle(ctx, cycle)
}
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ func (g *GruleEngine) ExecuteWithContext(ctx context.Context, dataCtx ast.IDataC
return ctx.Err()
}

g.notifyBeginCycle(cycle + 1)
g.notifyBeginCycle(ctx, cycle+1)

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

Expand Down Expand Up @@ -221,7 +221,7 @@ func (g *GruleEngine) ExecuteWithContext(ctx context.Context, dataCtx ast.IDataC
// set the current rule entry to run. This is for trace ability purpose
dataCtx.SetRuleEntry(runner)
// notify listeners that we are about to execute a rule entry then scope
g.notifyExecuteRuleEntry(cycle, runner)
g.notifyExecuteRuleEntry(ctx, cycle, runner)
// execute the top most prioritized rule
err := runner.Execute(ctx, dataCtx, knowledge.WorkingMemory)
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions engine/GruleEngineListener.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package engine

import "github.com/hyperjumptech/grule-rule-engine/ast"
import (
"context"
"github.com/hyperjumptech/grule-rule-engine/ast"
)

// GruleEngineListener is an interface to be implemented by those who want to listen the Engine execution.
type GruleEngineListener interface {
// EvaluateRuleEntry will be called by the engine if it evaluate a rule entry
EvaluateRuleEntry(cycle uint64, entry *ast.RuleEntry, candidate bool)
EvaluateRuleEntry(ctx context.Context, cycle uint64, entry *ast.RuleEntry, candidate bool)
// ExecuteRuleEntry will be called by the engine if it execute a rule entry in a cycle
ExecuteRuleEntry(cycle uint64, entry *ast.RuleEntry)
ExecuteRuleEntry(ctx context.Context, cycle uint64, entry *ast.RuleEntry)
// BeginCycle will be called by the engine every time it start a new evaluation cycle
BeginCycle(cycle uint64)
BeginCycle(ctx context.Context, cycle uint64)
}