0
我是Drools的新手,並且正在研究一個我會得到一批事實(BankAccount信息)的要求。當連續三次輕微賬戶(年齡< 16)插入會話工作記憶中時,我必須發出警報消息。我已經嘗試維護全局變量並在插入小帳戶時增加它。但是我無法定義一個基於全局計數器值的規則,我必須使用它來發送警報消息。請在DRL文件下面找到並提供解決方案。如何維護Drools全球事實計數器並根據其價值應用規則
下面的規則不點火
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」沒有觸發
感謝您的答覆。你能解釋一下我需要在DRL文件中聲明這個Counter事實嗎?因爲我在不同的事實上應用的實際規則,並且在那部分我應該增加或重置計數器。我需要再次宣佈這是一個全球..? – Kiran
請注意,我寫了「插入一個Counter事實」。查看我的更新。 - 沒有全局。 – laune
感謝您的更新。我編輯了DRL內容,請仔細閱讀並幫助我解決問題。我通過Java程序將ResultFact插入到工作內存中。其他方法公司和重置工作正常,我能夠打印計數器值使用getValue方法但「規則checkCounterAndProduceAlert」不是射擊 – Kiran