Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drools rule flow

Tags:

java

rules

drools

I'm having a strange issue with drools :

I have the following rules :

rule "is my dog a baby?"
  ruleflow-group "dog"
  salience 10
  when 
     dog : Dog(age <1 )
  then 
     dog.setIsBaby(true);
end


rule "baby dog"
    ruleflow-group "dog"
    salience 9
    when
        myData : MyData( myDog.isBaby() == false)
    then
        System.out.println(myData.getMyDog().getIsBaby());
end

I insert in my session myData and myData.getMyDog(), where myData.getMyDog.isBaby==false

The first rule is fired and my dog is set to be a baby. Then the second one is fired, and even it prints true .(even if the condition was to be false)

And when I test after firing all rules , myDog in myData is set to be a baby .

What am I doing wrong here ? Why does the second rule is fired ? is the problem in the session (stateful in my case) ?

I think that I need to say that I modify myData:myDog somewhere ,but I am not sure where .

Hope my question is clear, if not tell me.

like image 1000
Ricky Bobby Avatar asked Oct 13 '11 12:10

Ricky Bobby


People also ask

What is Rule Flow Group in drools?

A rule flow group works like a separate group of rules. Those who are setting the focus when the rule step is called with the same node id as the ruleflow-group. When no more rules can be fired, the process can continue to the next node.

What is Rule flow?

A ruleflow specifies how tasks are chained together: how, when, and under what conditions they are executed. You use ruleset parameters to transfer information between ruleflow tasks, and determine which path to follow through the transitions.

How do you fire a specific rule in drools?

In any case, the Customer has to be identified by a fact. To associcate a Customer with rules you need: class Customer { private String name; private List<String> rules; //... } So you are saying to create agenda for each customer and fire the rules based on agenda...


1 Answers

When you modify working memory facts, you need to tell Drools that the data has changed so it can re-evaluate all relevant rules.

Drools evaluates facts before firing any matched rules. If you have a dog with age = 0 and baby = false, both your rules will be activated. When your is my dog a baby? rule is fired, it doesn't change the fact that when Drools evaluated the baby dog rule, the myDog.isBaby() == false condition was true.

To inform Drools that you have modified some fact, use the update() knowledge helper method. Keep in mind that Drools associates fact handles to a specific object. If a rule references MyData, and you want that rule to be re-evaluated when the MyData.myDog object has changed, you'll need to use update() on the MyData object; just doing update() on your Dog object, will not cause the baby dog rule to be re-evaluated.

Try changing your is my dog a baby? rule to the following:

rule "is my dog a baby?"
    ruleflow-group "dog"
    salience 10
    when 
        dog : Dog(age < 1, baby == false)
        myData : MyData(myDog == dog)
    then
        dog.setIsBaby(true);
        update(dog);
        update(myData);
    end
like image 171
Dan Cruz Avatar answered Sep 28 '22 18:09

Dan Cruz