Skip to content

Commit

Permalink
Fix/306and307 Will fix #306 and #307 (#306)
Browse files Browse the repository at this point in the history
* Better make file

* Added samples

* Taking the oportunity in this branch to fix some documentation issue

* Fixed issue 306 and 307, due to wrong logic when adding ArrayMapSelector ast into ExpressionAtom

* Removed unnecessary dummy file
  • Loading branch information
newm4n committed May 31, 2022
1 parent 84f5022 commit 9f272d7
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 45,002 deletions.
3 changes: 2 additions & 1 deletion ast/ExpressionAtom.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ func (e *ExpressionAtom) GetSnapshot() string {
buff.WriteString(e.ExpressionAtom.GetSnapshot())
buff.WriteString("->MV:")
buff.WriteString(e.VariableName)
} else if e.ArrayMapSelector != nil && e.ExpressionAtom != nil {
}
if e.ArrayMapSelector != nil && e.ExpressionAtom != nil {
buff.WriteString(e.ExpressionAtom.GetSnapshot())
buff.WriteString("-[]>")
buff.WriteString(e.ArrayMapSelector.GetSnapshot())
Expand Down
2 changes: 1 addition & 1 deletion ast/WorkingMemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (e *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemory {
if cloneTable.IsCloned(expr.AstID) {
clone.expressionSnapshotMap[k] = cloneTable.Records[expr.AstID].CloneInstance.(*Expression)
} else {
panic(fmt.Sprintf("expression %s is not on the clone table", expr.GrlText))
panic(fmt.Sprintf("expression %s is not on the clone table - %s", expr.GrlText, expr.GetSnapshot()))
}
}
}
Expand Down
44 changes: 44 additions & 0 deletions examples/CloneTableIssue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package examples

import (
"github.com/hyperjumptech/grule-rule-engine/ast"
"github.com/hyperjumptech/grule-rule-engine/builder"
"github.com/hyperjumptech/grule-rule-engine/engine"
"github.com/hyperjumptech/grule-rule-engine/pkg"
"github.com/stretchr/testify/assert"
"testing"
)

type StructStringsData struct {
Strings []string
}

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

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

func TestSliceFunctionPanicTest(t *testing.T) {
fact := &StructStringsData{
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(panickingRule)))
assert.NoError(t, err)
knowledgeBase := knowledgeLibrary.NewKnowledgeBaseInstance("test", "0.0.1")
engine := engine.NewGruleEngine()

err = engine.Execute(dataContext, knowledgeBase)
assert.NoError(t, err)
}
49 changes: 49 additions & 0 deletions examples/MemoizeSliceFunction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package examples

import (
"github.com/hyperjumptech/grule-rule-engine/ast"
"github.com/hyperjumptech/grule-rule-engine/builder"
"github.com/hyperjumptech/grule-rule-engine/engine"
"github.com/hyperjumptech/grule-rule-engine/pkg"
"github.com/stretchr/testify/assert"
"testing"
)

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)
}
Binary file removed examples/benchmark/5000_dummy_rules.grb
Binary file not shown.
Loading

0 comments on commit 9f272d7

Please sign in to comment.