Skip to content

Commit

Permalink
MAke sure all error surounding RuleEntry Evaluate and Execute method …
Browse files Browse the repository at this point in the history
…to return the name of the Rule
  • Loading branch information
Ferdinand Neman committed Sep 19, 2023
1 parent 28f392e commit f76f1ef
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions ast/ArgumentList.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (e *ArgumentList) Evaluate(dataContext IDataContext, memory *WorkingMemory)
for i, exp := range e.Arguments {
val, err := exp.Evaluate(dataContext, memory)
if err != nil {

return values, err
}
values[i] = val
Expand Down
1 change: 1 addition & 0 deletions ast/ArrayMapSelector.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (e *ArrayMapSelector) Evaluate(dataContext IDataContext, memory *WorkingMem
if e.Expression != nil {
val, err := e.Expression.Evaluate(dataContext, memory)
if err != nil {

return val, err
}
e.Value = val
Expand Down
12 changes: 12 additions & 0 deletions ast/DataContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type DataContext struct {
retracted []string
variableChangeCount uint64
complete bool
ruleEntry *RuleEntry
}

func (ctx *DataContext) GetKeys() []string {
Expand All @@ -63,6 +64,15 @@ func (ctx *DataContext) IsComplete() bool {
return ctx.complete
}

func (ctx *DataContext) GetRuleEntry() *RuleEntry {

return ctx.ruleEntry
}

func (ctx *DataContext) SetRuleEntry(re *RuleEntry) {
ctx.ruleEntry = re
}

// IDataContext is the interface for the DataContext struct.
type IDataContext interface {
ResetVariableChangeCount()
Expand All @@ -74,6 +84,8 @@ type IDataContext interface {
Get(key string) model.ValueNode
GetKeys() []string

SetRuleEntry(re *RuleEntry)
GetRuleEntry() *RuleEntry
Retract(key string)
IsRetracted(key string) bool
Complete()
Expand Down
13 changes: 7 additions & 6 deletions ast/RuleEntry.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (e *RuleEntry) SetGrlText(grlText string) {
func (e *RuleEntry) Evaluate(ctx context.Context, dataContext IDataContext, memory *WorkingMemory) (can bool, err error) {
if ctx.Err() != nil {

return false, ctx.Err()
return false, fmt.Errorf("context error on evaluating rule %s. got %w", e.RuleName, ctx.Err())
}
defer func() {
if r := recover(); r != nil {
Expand All @@ -176,17 +176,18 @@ func (e *RuleEntry) Evaluate(ctx context.Context, dataContext IDataContext, memo
}
}()
if e.Retracted {

return false, nil
}
val, err := e.WhenScope.Evaluate(dataContext, memory)
if err != nil {
AstLog.Errorf("Error while evaluating rule %s, got %v", e.RuleName, err)
AstLog.Errorf("Error while evaluating rule '%s', got %v", e.RuleName, err)

return false, err
return false, fmt.Errorf("evaluating expression in rule '%s' the when raised an error. got %v", dataContext.GetRuleEntry().RuleName, err)
}
if val.Kind() != reflect.Bool {

return false, fmt.Errorf("expression in when is not a boolean expression : %s", e.WhenScope.Expression.GetGrlText())
return false, fmt.Errorf("evaluating expression in rule '%s', the when is not a boolean expression : %s", dataContext.GetRuleEntry().RuleName, e.WhenScope.Expression.GetGrlText())
}

return val.Bool(), nil
Expand All @@ -196,15 +197,15 @@ func (e *RuleEntry) Evaluate(ctx context.Context, dataContext IDataContext, memo
func (e *RuleEntry) Execute(ctx context.Context, dataContext IDataContext, memory *WorkingMemory) (err error) {
if ctx.Err() != nil {

return ctx.Err()
return fmt.Errorf("context error on executing rule %s. got %w", e.RuleName, ctx.Err())
}
if e.ThenScope == nil {

return fmt.Errorf("RuleEntry %s have no then scope", e.RuleName)
}
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("rule engine execute panic ! recovered : %v", r)
err = fmt.Errorf("rule engine execute panic on rule %s ! recovered : %v", e.RuleName, r)
}
}()

Expand Down
3 changes: 3 additions & 0 deletions engine/GruleEngine.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ 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)
// execute the top most prioritized rule
Expand Down

0 comments on commit f76f1ef

Please sign in to comment.