hyperjumptech/grule-rule-engine

Grule engine panics

pablolch opened this issue · 1 comments

Describe the bug
Grule engine panics and then recovers with an error:

panic: expression  1 is not on the clone table [recovered]
	panic: expression  1 is not on the clone table

To Reproduce
Steps to reproduce the behavior:

type TestData struct {
	Strings []string
}

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

const rule = ` rule test {
when 
	Fact.GetStrings()[0] == Fact.GetStrings()[1]
then
	Complete();
}`

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

	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)
}

Expected behavior
No error executing the Grule engine.

Additional context
Test logs:


--- FAIL: TestSliceFunctionTest (0.00s)
panic: expression  1 is not on the clone table [recovered]
	panic: expression  1 is not on the clone table

goroutine 6 [running]:
testing.tRunner.func1.2({0x156e480, 0xc0001ed8d0})
	/usr/local/go/src/testing/testing.go:1209 +0x24e
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1212 +0x218
panic({0x156e480, 0xc0001ed8d0})
	/usr/local/go/src/runtime/panic.go:1038 +0x215
github.com/hyperjumptech/grule-rule-engine/ast.(*WorkingMemory).Clone(0xc00007f2c0, 0xc000233c58)
	/Users/pchamorro/go/pkg/mod/github.com/hyperjumptech/grule-rule-engine@v1.10.5/ast/WorkingMemory.go:147 +0x11ff
github.com/hyperjumptech/grule-rule-engine/ast.(*KnowledgeBase).Clone(0xc0001ca0a0, 0xc000233c58)
	/Users/pchamorro/go/pkg/mod/github.com/hyperjumptech/grule-rule-engine@v1.10.5/ast/KnowledgeBase.go:217 +0x125
github.com/hyperjumptech/grule-rule-engine/ast.(*KnowledgeLibrary).NewKnowledgeBaseInstance(0xc000233e30, {0x1638db6, 0x0}, {0x1638f28, 0x5})
	/Users/pchamorro/go/pkg/mod/github.com/hyperjumptech/grule-rule-engine@v1.10.5/ast/KnowledgeBase.go:125 +0x194

Fixed with #306