问题描述
- Drools6 规则流的问题
-
在Drools5老版本drl和**规则流xml**文件是通过KnowledgeBuilder 手动加载的,然后执行的过程和执行drl文件方式差不多,只是多了一句对Process的执行,也就是**ksession.startProcess("ruleflow.salary"); **完整代码引用示例:public class EmployTest2 { public static void main(String[] args) { try { // load up the knowledge base KnowledgeBase kbase = readKnowledgeBase(); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test"); // go ! Employ emp = new Employ(); emp.setEduInfo("master"); emp.setResume("tech"); emp.setAnnualExam("good"); emp.setAwardPunish("award"); ksession.insert(emp); ksession.startProcess("ruleflow.salary"); ksession.fireAllRules(); System.out.println("Basic Salary: " + emp.getBasicSalary()); System.out.println("Duty Salary: " + emp.getDutySalary()); System.out.println("Bonus : " + emp.getBonus()); System.out.println("rate : " + emp.getPercent()); System.out.printf("Total Salary: %.0f" , emp.getTotalSalary()); logger.close(); } catch (Throwable t) { t.printStackTrace(); } } private static KnowledgeBase readKnowledgeBase() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newFileResource("./rules/EduInfoRule.drl"), ResourceType.DRL); kbuilder.add(ResourceFactory.newFileResource("./rules/ResumeRule.drl"), ResourceType.DRL); kbuilder.add(ResourceFactory.newFileResource("./rules/BonusRule.drl"), ResourceType.DRL); kbuilder.add(ResourceFactory.newFileResource("./rules/AwardPunish.drl"), ResourceType.DRL); kbuilder.add(ResourceFactory.newFileResource("./rules/TotalRule.drl"), ResourceType.DRL); kbuilder.add(ResourceFactory.newFileResource("./rules/saflow.xml"), ResourceType.BPMN2); 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; } }
在Drools6的环境下执行这段规则流示例没有任何问题,而由于Drools6更新了全新的Kie API,所以现在碰到的问题就是使用KieAPI时并不能执行ksession.startProcess("ruleflow.salary");的逻辑,报错如下:
java.lang.IllegalArgumentException: Unknown process ID: ruleflow.salaryxml流文件配置如下:
...
type="RuleFlow" name="salary" id="ruleflow.salary" package-name="ruleflow" >个人想到的原因可能是因为Drools6自动加载kmodule.xml文件指定下的包路径的drl文件, 而并没有加载流配置文件,就是老版本示例中的 ** kbuilder.add(ResourceFactory.newFileResource("./rules/saflow.xml"), ResourceType.BPMN2);** 这句代码, 所以导致报错,,,
不知道大家有过规则流这方面的接触没有, 本人菜鸟到处找资料,然并卵,,, 百度也无力了...
解决方案
解决方案二:
是在module中配置kase,ksession吗?
时间: 2024-11-02 11:14:32