1.drl definition:
package com.sample
import com.sample.DroolsTest.Holding;
import com.sample.DroolsTest.MatchingRule;
global java.util.List batchMatchingList;
declare CustomerAddress
number: int
streetName: String
city: String
end
function void printRuleInfo(){
int i ;
i = 108;
String str = "define local variable in my rule engine";
java.util.List lst = new java.util.ArrayList();
lst.add("test1");
lst.add("test2");
System.out.println( "local variable: " + str + " : " + i + " : " + lst.get(1) );
}
rule "Rule 1"
salience 100
no-loop true
when
hd : Holding()
then
System.out.println ( drools.getRule().getName() +":"+ hd.getExternalId() );
if (hd.doMatching(MatchingRule.MatchingByStock)){
//retract(hd);
}
//drools.setFocus("G2");
end
rule "Rule 2"
salience 99
agenda-group "G2"
no-loop true
when
hd: Holding()
then
System.out.println ( drools.getRule().getName() +":"+ hd.getExternalId() );
hd.doMatching(MatchingRule.MatchingByFund);
end
rule "Rule 3"
salience 98
agenda-group "G3"
no-loop true
when
hd: Holding()
then
System.out.println ( drools.getRule().getName() +":"+ hd.getExternalId() );
hd.doMatching(MatchingRule.MatchingByBond);
end
2.java class:
package com.sample;
import java.util.ArrayList;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.base.RuleNameEndsWithAgendaFilter;
import org.drools.base.RuleNameStartsWithAgendaFilter;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.common.DefaultFactHandle;
import org.drools.event.rule.DefaultWorkingMemoryEventListener;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.rule.Activation;
import org.drools.runtime.rule.AgendaFilter;
import org.drools.runtime.rule.FactHandle;
/**
* This is a sample class to launch a rule.
*/
public class DroolsTest {
private static StatefulKnowledgeSession ksession = null;
private static java.util.Map
public static final void main(String[] args) {
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
java.util.List batchMatchingList = new ArrayList();
ksession.setGlobal("batchMatchingList", batchMatchingList);
ksession.addEventListener(new DefaultWorkingMemoryEventListener());
// go !
Holding hd = new Holding();
hd.externalId = "s01";
Holding hd2 = new Holding();
hd2.externalId = "d02";
Holding hd3 = new Holding();
hd3.externalId = "f03";
Holding hd4 = new Holding();
hd4.externalId = "s09";
FactHandle f1 = ksession.insert(hd);
FactHandle f2 = ksession.insert(hd2);
FactHandle f3 = ksession.insert(hd3);
FactHandle f4 = ksession.insert(hd4);
holdingMapping.put(hd.externalId, f1);
holdingMapping.put(hd2.externalId, f2);
holdingMapping.put(hd3.externalId, f3);
holdingMapping.put(hd4.externalId, f4);
// ksession.fireAllRules(new RuleNameStartsWithAgendaFilter("F"));
ksession.fireAllRules();
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("MatchById.drl"), ResourceType.DRL);
//kbuilder.add(ResourceFactory.newClassPathResource("MatchByDetail.drl"), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
public static class Holding {
private String externalId;
private String externalName;
public String getExternalId() {
return externalId;
}
public String getExternalName() {
return externalName;
}
public void doBatchMatching(MatchingRule matchingRule) {
java.util.List batchMatchingList = (java.util.List) ksession.getGlobal("batchMatchingList");
for (int i = 0; i < batchMatchingList.size(); i++) {
String externalId = (String) batchMatchingList.get(i);
Boolean isMatched = false;
switch (matchingRule) {
case MatchingByStock:
if (externalId.startsWith("s0")) {
isMatched = true;
}
break;
case MatchingByBond:
if (externalId == "d02") {
isMatched = true;
}
break;
case MatchingByFund:
if (externalId == "f03") {
isMatched = true;
}
break;
default:
break;
}
if (isMatched) {
System.out.println(" ==> backend matched: " + matchingRule + " --->>> " + externalId);
FactHandle handle = holdingMapping.get(externalId);
DroolsTest.ksession.retract(handle);
}
}
ksession.getAgenda().getAgendaGroup("AfterDoMatching").setFocus();
//ksession.fireAllRules(new RuleNameStartsWithAgendaFilter("AfterDoMatching"));
}
public Boolean doMatching(MatchingRule matchingRule) {
Boolean isMatched = false;
switch (matchingRule) {
case MatchingByStock:
if (externalId.startsWith("s0")) {
isMatched = true;
}
break;
case MatchingByBond:
if (externalId == "d02") {
isMatched = true;
}
break;
case MatchingByFund:
if (externalId == "f03") {
isMatched = true;
}
break;
default:
break;
}
if (isMatched) {
System.out.println(" ==> backend matched: " + matchingRule + " --->>> " + externalId);
FactHandle handle = DroolsTest.ksession.getFactHandle(this);
DroolsTest.ksession.retract(handle);
}
return isMatched;
}
}
public static enum MatchingRule {
MatchingByStock, MatchingByBond, MatchingByFund,
}
}
多个Fact时,如:m个c1,n个c2,最多要处理m*n个rule,最好在能处理fact时,retract掉当前fact