如何维护Drools全球反事实并根据其价值应用规则

时间:2016-12-02 22:16:22

标签: drools drools-guvnor drools-planner drools-fusion drools-flow

我是Drools的新手,并且正在处理我将获得一批事实的要求(BankAccount信息)。当连续三个小帐户(年龄<16)插入会话工作内存时,我必须发出警报消息。我已经尝试维护全局变量并在插入次要帐户时递增它。但我无法根据全局计数器值定义规则,我必须使用该规则发送警报消息。请在下面找到DRL文件并为此提供解决方案。

以下规则未触发

 rule checkCounterAndProduceAlert when
       b: ResultFact( value > 2 )   
    then
        System.out.println("Alert****");  
    end

Here is the DRL full content of DRL file




        package com.ys.drools.rules

import com.ys.drools.facts.*;

rule checkCounterAndProduceAlert 

no-loop true
when
   b: ResultFact( value > 2 )   
then
    System.out.println("Alert****");  
end


rule checkSeriesOfMinorAccount when
    a : AccountHolder( age < 16 )
    b : ResultFact()
then
b.inc();
update(b);

System.out.println("Incrementing count  since minor account appered");
System.out.println("***********************************************");
System.out.println(b.getValue());
end


rule checMajorAccount when
    a : AccountHolder( age >16 )
    b :ResultFact()
then
b.reset();
update(b);
System.out.println("Making count zero since major account appered");
System.out.println(b.getValue());
end

这是Java类,它将加载3个次要帐户并测试应用程序

package com.ys.drools.data.loader;

import java.util.ArrayList;
import java.util.List;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ys.drools.facts.AccountHolder;
import com.ys.drools.util.BankAccountUtilService;
import com.ys.drools.util.DroolsGlobalUtil;

public class Test {
    private BankAccountUtilService service;
    private  KieContainer kieContainer;
    private KieSession kieSession;
    private static Logger log = LoggerFactory
            .getLogger(Test.class);

    private List<AccountHolder> accountHolders=new ArrayList<AccountHolder>();  

    public Test(){
        kieContainer= KieServices.Factory.get().getKieClasspathContainer();     
          kieSession = kieContainer.newKieSession("BankAccountSession");
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Test test=new Test();
        List<AccountHolder> aList=test.fillAccountHolderList();
         ResultFact fact=new ResultFact();
       test.kieSession.insert(fact);
       for(int i=0;i<aList.size();i++){
        test.kieSession.insert(aList.get(i));
        test.kieSession.fireAllRules();
        }

      }


    public List<AccountHolder> fillAccountHolderList(){
         List<AccountHolder> accountHolders=new ArrayList<AccountHolder>(); 
         AccountHolder a1=new AccountHolder("Karun", 12);
         AccountHolder a2=new AccountHolder("kumar", 12);
         AccountHolder a3=new AccountHolder("rakesh", 10);
         accountHolders.add(a1);
         accountHolders.add(a2);
         accountHolders.add(a3);
         return accountHolders; 
    }
}

请为此提供解决方案。运行测试类时的输出

Incrementing count  since minor account appered
***********************************************
1
Incrementing count  since minor account appered
***********************************************
2
Incrementing count  since minor account appered
***********************************************
3
Making count zero since major account appered
0
即使值> 2后,

(“警报****”)也不显示。规则“checkCounterAndProduceAlert”未触发

1 个答案:

答案 0 :(得分:0)

解决方案很简单。

class Counter {
    private int value;
    public void inc(){ value++; }
    public void reset(){ value = 0; }
    public int getValue(){ return value; }
}

将反事实插入工作记忆中,并在将inc模式添加到规则后适当时使用方法reset$c: Counter()

rule checkCounterAndProduceAlert when
    Counter( value == 3 )   
then
    System.out.println("Alert****");  
end

您可能需要一个规范,是否应该在连续未成年人的第六,第九,......帐户之后发出另一个警报。

修改

您也可以在DRL文件中声明并插入它:

declare Counter
  value : int
end

rule "insert counter"
  salience 999999
when
  not Counter()
then
  insert( new Counter() );
end