Getting un expected error message saying msg = "Max cycle reached" #201
nagarajtrivedi
started this conversation in
General
Replies: 2 comments 4 replies
-
can you paste the content of the new test file, so we can take a look at it |
Beta Was this translation helpful? Give feedback.
3 replies
-
@nagarajtrivedi, couple of issues identified in your test file
Coming to maxCycle reached error, its happening because it will try to find the right rule and grule engine not able to find even after 5000 cycles. I have updated your test file and please find the correct one below: package examples
import (
"fmt"
"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"
"time"
)
type YourFact struct {
StringAttribute string
BooleanAttribute bool
FloatAttribute float64
TimeAttribute time.Time
IntAttribute int64
WhatToSay string
}
func (mf *YourFact) ReadWhatToSay(sentence string) string {
return fmt.Sprintf("Let say \"%s\"", sentence)
}
func TestHello(t *testing.T) {
yourFact := &YourFact{
StringAttribute: "Nagaraj",
BooleanAttribute: false,
FloatAttribute: 1.234,
TimeAttribute: time.Now(),
IntAttribute: 254,
}
dataCtx := ast.NewDataContext()
err := dataCtx.Add("YF", yourFact)
assert.NoError(t, err)
// Prepare knowledgebase library and load it with our rule.
knowledgeLibrary := ast.NewKnowledgeLibrary()
ruleBuilder := builder.NewRuleBuilder(knowledgeLibrary)
drls := `
rule ChkValues "Check Nagaraj" {
when
YF.IntAttribute == 254 && YF.BooleanAttribute == false && YF.StringAttribute == "Nagaraj" && YF.FloatAttribute == 1.234
then
YF.WhatToSay = YF.ReadWhatToSay("Hello Nagaraj");
Retract("ChkValues");
}
`
byteArr := pkg.NewBytesResource([]byte(drls))
err = ruleBuilder.BuildRuleFromResource("HelloNagaraj", "0.0.1", byteArr)
assert.NoError(t, err)
knowledgeBase := knowledgeLibrary.NewKnowledgeBaseInstance("HelloNagaraj", "0.0.1")
engine := engine.NewGruleEngine()
/*engine.MaxCycle = 10000 */
err = engine.Execute(dataCtx, knowledgeBase)
assert.NoError(t, err)
assert.Equal(t, "Let say \"Hello Nagaraj\"", yourFact.WhatToSay)
println(yourFact.WhatToSay)
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi all, while executing the rule I am getting an error message as "Max cycle reached"
I will give a brief information on this.
=== RUN TestHello
time="2021-06-14T16:13:57+05:30" level=error msg="Max cycle reached" lib=grule-rule-engine package=engine
Let say "Hello Nagaraj"
--- FAIL: TestHello (0.03s)
Hello_test.go:75:
Error Trace: Hello_test.go:75
Error: Received unexpected error:
the GruleEngine successfully selected rule candidate for execution after 5000 cycles, this could possibly caused by rule entry(s) that keep added into execution pool but when executed it does not change any data in context. Please evaluate your rule entries "When" and "Then" scope. You can adjust the maximum cycle using GruleEngine.MaxCycle variable
Test: TestHello
Beta Was this translation helpful? Give feedback.
All reactions