在Drools规则中累积功能

时间:2019-02-27 14:21:45

标签: drools

我正在尝试Drools示例,但遇到了一个我不知道在做什么的示例。 问题在于规则中的累加函数,因为我不了解结果。

主Java

package com.javainuse.main;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import com.javainuse.model.Counter;

public class DroolsTest {

    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KieServices ks = KieServices.Factory.get();
            KieContainer kContainer = ks.getKieClasspathContainer();
            KieSession kSession = kContainer.newKieSession("ksession-rule");

            Counter count1 = new Counter(2, "count1");
            Counter count2 = new Counter(7, "count2");
            Counter count3 = new Counter(11, "count3");

            System.out.println();
            System.out.println("fire rules after inserting counter1");
            System.out.println();
            kSession.insert(count1);

            //fire rules with counter1
            kSession.fireAllRules();

            System.out.println();
            System.out.println("fire rules after inserting counter2");
            System.out.println();
            kSession.insert(count2);

            //fire rules with already existing counter1 and newly inserted counter2
            kSession.fireAllRules();

            //Dispose the session at the end.
            kSession.dispose();

            } catch (Throwable t) {
                t.printStackTrace();
            }
    }

}

计数器类

package com.javainuse.model;

public class Counter {
    public String name;
    public int count;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Counter(int count, String name) {
        System.out.println("creating new using overloaded constructor");
        this.count = count;
        this.name = name;
    }
}

Drools Ruel

package rules

import com.javainuse.model.Counter

rule "Counter shower 1" 
    when $Counter  : Counter() 
then 
    System.out.println("Counter there (1) : " + $Counter.count + " and the 
    name is : " + $Counter.name);
end

rule "Counter shower 2" 
when 
    $Counter  : Counter()  
    accumulate (Counter() ; $count:count())
then 
    System.out.println("Counter there (2) : " + $Counter.count + " and the 
    name is : " + $Counter.name
    +" accumulated value is " +$count);     
end 

rule "Counter shower 3" 
when 
    Counter()  
then 
System.out.println("Counters there (3) : ");
end

执行的输出:

creating new using overloaded constructor
creating new using overloaded constructor
creating new using overloaded constructor

fire rules after inserting counter1

Counter there (1) : 2 and the name is : count1
Counter there (2) : 2 and the name is : count1 accumulated value is 1
Counters there (3) : 

fire rules after inserting counter2

Counter there (1) : 7 and the name is : count2
Counter there (2) : 2 and the name is : count1 accumulated value is 2
Counter there (2) : 7 and the name is : count2 accumulated value is 2
Counters there (3) : 

问题是我不理解为什么当我在counter2处触发规则时,count 1出现在累积的输出丝毫上,为什么会出现此行:

`Counter there (2) : 2 and the name is : count1 accumulated value is 2`

但不要再这样:

Counter there (1) : 2 and the name is : count1

0 个答案:

没有答案