Skip to content

Commit

Permalink
Added attempt to show rule name when Rule entry fails (#408)
Browse files Browse the repository at this point in the history
Co-authored-by: Ferdinand Neman <[email protected]>
  • Loading branch information
newm4n and Ferdinand Neman authored Sep 19, 2023
1 parent eb4f61d commit 7272ef7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 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
13 changes: 13 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 @@ -80,6 +90,9 @@ type IDataContext interface {
IsComplete() bool
Retracted() []string
Reset()

SetRuleEntry(re *RuleEntry)
GetRuleEntry() *RuleEntry
}

// ResetVariableChangeCount will reset the variable change count
Expand Down
11 changes: 6 additions & 5 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)

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
2 changes: 2 additions & 0 deletions engine/GruleEngine.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ 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

1 comment on commit 7272ef7

@vivlis
Copy link

@vivlis vivlis commented on 7272ef7 Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello
dataCtx.SetRuleEntry(runner) is probably needed in Evaluation part as well
dataContext.GetRuleEntry().RuleName - panic (line 186 RuleEntry) -> will go to defer

Please sign in to comment.