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

Wrong evaluation of slices returned by functions #303

Closed
pablolch opened this issue May 15, 2022 · 2 comments
Closed

Wrong evaluation of slices returned by functions #303

pablolch opened this issue May 15, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@pablolch
Copy link

pablolch commented May 15, 2022

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;
}
@pablolch pablolch changed the title Problem evaluating slices returned by functions Wrong evaluation of slices returned by functions May 15, 2022
@newm4n
Copy link
Member

newm4n commented May 30, 2022

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]");
}

@newm4n newm4n added the bug Something isn't working label May 30, 2022
@newm4n
Copy link
Member

newm4n commented May 31, 2022

Fixed with #306

@newm4n newm4n closed this as completed May 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants