Search code examples
drools

jboss drools, multiple same facts, fire rule only one time


i want to execute my rules, and my java code like :

Fact fact1 = new Fact(); 
fact1.setName("name1"); 
fact1.setValue("chn"); 
.... 

Fact fact2 = new Fact(); 
fact2.setName("name2"); 
fact2.setValue("chn"); 
.... 

List<Fact> facts = new ArrayList<Fact>(); 
facts.add(fact1); 
facts.add(fact2); 

ksession.execute(facts); 

my rules like : 

rule "rule1" 
    when 
        $partFact:Fact(value=="chn") 
    then 
        Action action = new Action(); 
        .... 
end 

rule "rule2" 
    when 
        $partFact:Fact(name=="name1") 
    then 
        Action action = new Action(); 
        .... 
end 

what i want are :

  1. rule1 and rule2 only one rule executed, that is if 'rule1' executed, then 'rule2' won't executed even meet 'rule2' conditions.

  2. each rule only executed one time, for example, there are 2 Fact, and all these 2 Fact satisfy 'rule1', but 'rule1' only executed one time, not 2 times.

how can i achieve my goals? thanks in advance.


Solution

  • Try these:

    rule "rule1" 
        when 
            exists Fact(value=="chn") 
        then 
            Action action = new Action(); 
            .... 
    end 
    
    rule "rule2" 
        when
            not Fact(value=="chn")
            exists Fact(name=="name1") 
        then 
            Action action = new Action(); 
            .... 
    end