Replies: 4 comments 2 replies
-
Those functions, If you take a look at this snippet , you can see how type MyFact struct {
builtIn *ast.BuiltInFunctions
IntAttribute int64
}
func (mf *MyFact) DoItAndRetractSomeRule(sentence string) string {
// do it
// ,,,
mf.builtIn.Retract("SomeRule")
return fmt.Sprintf("SomeRule retracted")
}
func (mf *MyFact) DoThisAndComplete() {
// do this stuff
// ...
mf.builtIn.Complete()
}
func (mf *MyFact) DoThatsAndRedeploy() {
// do that stuff
// ...
mf.builtIn.Knowledge.Reset()
}
func TestTutorial(t *testing.T) {
// Prepare knowledgebase library and load it with our rule.
knowledgeLibrary := ast.NewKnowledgeLibrary()
ruleBuilder := builder.NewRuleBuilder(knowledgeLibrary)
drls := `
rule SomeRule salience 10 {
when
true
then
MF.DoItAndRetractSomeRule();
}
rule FinalRule salience 0 {
when
true
then
MF.DoThisAndComplete();
}
`
byteArr := pkg.NewBytesResource([]byte(drls))
err := ruleBuilder.BuildRuleFromResource("Tutorial", "0.0.1", byteArr)
assert.NoError(t, err)
knowledgeBase := knowledgeLibrary.NewKnowledgeBaseInstance("Tutorial", "0.0.1")
dataCtx := ast.NewDataContext()
myBuiltInFunction := &ast.BuiltInFunctions{
Knowledge: knowledgeBase,
WorkingMemory: knowledgeBase.WorkingMemory,
DataContext: dataCtx,
}
myFact := &MyFact{
builtIn: myBuiltInFunction ,
}
err = dataCtx.Add("MF", myFact)
assert.NoError(t, err)
engine := engine.NewGruleEngine()
err = engine.Execute(dataCtx, knowledgeBase)
} In my opinion, calling built-in functions from your custom function will reduce rule behaviour visibility. It could make harder for you to debug missbehaving rule execution since retraction, completion command is done by your function. So my advise is, do this as long as you know what you're doing. I hope this help. |
Beta Was this translation helpful? Give feedback.
-
Looks very promising. Is there any way here to know what rule actually is active (calling "me")? |
Beta Was this translation helpful? Give feedback.
-
Did you mean this ? https://github.com/hyperjumptech/grule-rule-engine/blob/master/docs/MatchingRules_en.md |
Beta Was this translation helpful? Give feedback.
-
Not really. My helper function can be called from many rules, and from it I cannot know what rule to retract. Forget will work, complete also but not retract |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem? Please describe.
I am trying to make the rules focus purly on "what", not "how" by hiding the "how" inside custom functions on the facts. This means that i may update one/many variable(s) in "my" fact as well as in other facts. The function may also need to make sure that the rule that called it is retracted based on some computation hidden in the function.
Currently my rules are full of Forget and Retract statements that adds no value to how to understand/define the rule itself.
Describe the solution you'd like
Ability to from within a fact's custom function inform about changed variables/facts (just as Forget in a rule) as well as to retract whatever rule that called me from the next round of evaluations. Maybe even declare Complete (less need but maybe for consistency?
Describe alternatives you've considered
A way to change the default so that all variables are forgotten every round and by default retract all rules if they fired this round. This would require a way to "unretract" a rule if it should be able to fire also next round and will unfortunatly loose ALOT of performance :(
Additional context
I am a Rookie (capital R) to rule-engines and if this question in any way crosses any boundary, please let me know.
Or if there is easier/better ways to do what i want. My goal is to use the rule engine as common language between business and technical implementation, enabling normal users to both understand the rules and make them able to suggest new rules to better describe their business with practices/processes and exceptions.
Beta Was this translation helpful? Give feedback.
All reactions