这是作为规则引擎的java文件 RuleRunner.java
public class RuleRunner {
KieSession kSession = null;
public RuleRunner() {
}
/**
* Method that executes all the rules specified in rule file
*
* @param rules
* @param facts
*/
public void runRules(String[] rules, String[] values) {
KieServices kieServices = KieServices.Factory.get();
KieResources kieResources = kieServices.getResources();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
KieRepository kieRepository = kieServices.getRepository();
for (String ruleFile : rules) {
Resource resource = kieResources.newClassPathResource(ruleFile);
kieFileSystem.write(
"src/main/resources/com/skills421/examples/drools6_0/"
+ ruleFile, resource);
}
KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
kb.buildAll();
if (kb.getResults().hasMessages(Level.ERROR)) {
throw new RuntimeException("Build Errors:\n"
+ kb.getResults().toString());
}
KieContainer kContainer = kieServices.newKieContainer(kieRepository
.getDefaultReleaseId());
kSession = kContainer.newKieSession();
for (String value : values){
kSession.insert(value);
}
kSession.fireAllRules();
}
}
这是Main.java,它集成了drl文件和规则引擎
public class Main
{
public static void main(String[] args)
{
long startTime = System.currentTimeMillis();
//Creating the rule and specifying the rule file
RuleRunner runner = new RuleRunner();
String[] rules = { "test1.drl"};
String[] values={"creditcard","personal_status","male single"};
//Object[] facts = { new CreditCard("male single",25,">=7")};
runner.runRules(rules,values);
long endTime = System.currentTimeMillis();
System.out.println("Took "+(endTime - startTime) + " ms");
}
}
我是DROOLS的新手。这是我的.drl文件
dialect "mvel"
/*
*This rule picks people with good credit record and an employment history
of more than 7 years
*/
rule "Credit Card employment and class check"
when
passList : ArrayList()
$creditCard : CreditCard() from passList
eval($creditCard.get_class() == "good" && $creditCard.getEmployment() ==
">=7")
then
passList = new ArrayList();
passList.add($creditCard);
end
dialect "java"
rule "passList check"
when
eval( true )
then
System.out.println("passList size2....");
end
我想将passList的计算值从rule1传递给rule2.Any帮助将不胜感激。