hyperjumptech/grule-rule-engine

Wrong evaluation of slices returned by functions

pablolch opened this issue · 2 comments

Describe the bug
Expressions involving slices returned by functions are not properly treated.
Looks like the expression is memorized in the working memory even when other involved variables have changed and the change is known by the engine.

To Reproduce
Steps to reproduce the behavior:

type TestData struct {
	Index         int
	Strings       []string
	Concatenation string
}

func (f *TestData) GetStrings() []string {
	return f.Strings
}

const rule = ` rule test {
when 
	Fact.Index < Fact.Strings.Len()
then
	Fact.Concatenation = Fact.Concatenation + Fact.GetStrings()[Fact.Index];
	Fact.Index = Fact.Index + 1;
}`

func TestSliceFunctionTest(t *testing.T) {
	fact := &TestData{
		Index:   0,
		Strings: []string{"1", "2", "3"},
	}

	dataContext := ast.NewDataContext()
	err := dataContext.Add("Fact", fact)
	assert.NoError(t, err)
	knowledgeLibrary := ast.NewKnowledgeLibrary()
	ruleBuilder := builder.NewRuleBuilder(knowledgeLibrary)
	err = ruleBuilder.BuildRuleFromResource("test", "0.0.1", pkg.NewBytesResource([]byte(rule)))
	assert.NoError(t, err)
	knowledgeBase := knowledgeLibrary.NewKnowledgeBaseInstance("test", "0.0.1")
	engine := engine.NewGruleEngine()

	err = engine.Execute(dataContext, knowledgeBase)
	assert.NoError(t, err)
	assert.Equal(t, "123", fact.Concatenation)
}

Expected behavior
No error executing TestSliceFunctionTest(t *testing.T)

Additional context
Test logs:

                Error:          Not equal:
                                expected: "123"
                                actual  : "111"

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1 +1 @@
                                -123
                                +111
                Test:           TestSliceFunctionTest
--- FAIL: TestSliceFunctionTest (0.01s)

When the same slice is not being referenced from a function it works properly:

rule test {
when 
	Fact.Index < Fact.Strings.Len()
then
	Fact.Concatenation = Fact.Concatenation + Fact.Strings[Fact.Index];
	Fact.Index = Fact.Index + 1;
}

Hi @pablolch

Your suspicion is correct. The function Fact.GetStrings()[Fact.Index] returns immediately memorized by the engine.
Changing the value of Fact.Index does not make the engine see that this Fact.GetStrings()[Fact.Index] signature need to be removed from the working memory.

Its clearly a bug, since adding Changed("Fact.Index"); does not seem to fix the issue.
But Changed("[Fact.Index]"); do and also Changed("Fact.GetStrings()[Fact.Index]"); .
I suspected that the slice index evaluation value are memorized and not simply forgotten when we add the value of Fact.Index

For temporary solution, You can use this to correct it.

rule test {
when 
	Fact.Index < Fact.Strings.Len()
then
	Fact.Concatenation = Fact.Concatenation + Fact.GetStrings()[Fact.Index];
	Fact.Index = Fact.Index + 1;
	Changed("[Fact.Index]");
}

Fixed with #306