0

我是 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

这是将加载 3 个 Minor 帐户并测试应用程序的 Java 类

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

("Alert****") 即使在 value 为 >2 之后也不显示。规则“checkCounterAndProduceAlert”未触发

4

1 回答 1

0

解决方案很简单。

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

在将模式添加到规则后,将Counter 事实插入工作内存并使用方法increset适当的地方。$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
于 2016-12-03T04:59:34.530 回答