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

Fix/403how rule name #407

Closed
wants to merge 2 commits into from
Closed
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
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
6 changes: 5 additions & 1 deletion ast/Serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1535,11 +1535,15 @@ func (meta *ConstantMeta) ReadMetaFrom(reader io.Reader) error {
return err
}
byteArr := make([]byte, length)
_, err = reader.Read(byteArr)
readCount, err := reader.Read(byteArr)
if err != nil {

return err
}
if uint64(readCount) != length {

return io.ErrShortBuffer
}
meta.ValueBytes = byteArr

b, err := ReadBoolFromReader(reader)
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
Loading