Drools规则语法中的异常

时间:2019-04-29 10:13:28

标签: java drools

我正在尝试使用以下两个不同的POJO类编写流口水的规则。

  for (int i = 0; i < 5; i++) {

        dbRules = new DbRules(i, "Rule Name - " + i, "  $mdmContractHeader : MdmContractHeader();  $contractHeader1 : ContractHeader (supplierName==$mdmContractHeader.supplierName); "
                + " &&  contractHeader:ContractHeader(supplierId >= 100)",
                "System.out.print(\"Test, Drools rule - " + i
                        + ".! \"); System.out.println(contractHeader.getSupplierName());");
        list.add(dbRules);
    }

执行后,我面临以下异常。看来我在pojo类声明中遇到了错误。

  Exception in thread "main" java.lang.RuntimeException: Unable to get KieModule, Errors Existed: Error Messages:
      Message [id=1, kieBase=defaultKieBase, level=ERROR, path=rules/Sample.drl, line=1, column=0
    text=[ERR 102] Line 1:306 mismatched input '$contractHeader1' in rule "Rule Name - 0"]
        Message [id=2, kieBase=defaultKieBase, level=ERROR, path=rules/Sample.drl, line=1, column=0
      text=[ERR 102] Line 1:723 mismatched input '$contractHeader1' in rule "Rule Name - 1"]
      Message [id=3, kieBase=defaultKieBase, level=ERROR, path=rules/Sample.drl, line=1, column=0
     text=[ERR 102] Line 1:1140 mismatched input '$contractHeader1' in rule "Rule Name - 2"]
     Message [id=4, kieBase=defaultKieBase, level=ERROR, path=rules/Sample.drl, line=1, column=0
    text=[ERR 102] Line 1:1557 mismatched input '$contractHeader1' in rule "Rule Name - 3"]
    Message [id=5, kieBase=defaultKieBase, level=ERROR, path=rules/Sample.drl, line=1, column=0
    text=[ERR 102] Line 1:1974 mismatched input '$contractHeader1' in rule "Rule Name - 4"]
     Message [id=6, kieBase=defaultKieBase, level=ERROR, path=rules/Sample.drl, line=0, column=0
      text=Parser returned a null Package]
  ---
  Warning Messages:
 ---
 Info Messages:

at org.drools.compiler.kie.builder.impl.KieBuilderImpl.getKieModule(KieBuilderImpl.java:467)
at org.drools.compiler.kie.builder.impl.KieBuilderImpl.getKieModule(KieBuilderImpl.java:453)
at com.sample.StringDroolsMdm.executeRules(StringDroolsMdm.java:118)
at com.sample.StringDroolsMdm.main(StringDroolsMdm.java:70)

我已经如下更新了StringBuffer中的内容,但是仍然遇到相同的异常。

 import com.model.ContractHeader; 
 import com.model.MdmContractHeader; 
 import static com.service.SupplierService.isRuleAppliesToSupplier;  
 rule "Rule Name - 0" 
  when 
 cHeader:ContractHeader() 
 eval(isRuleAppliesToSupplier((cHeader.getSupplierName()) , 0 )) && 
 ( 
 $mdmContractHeader:MdmContractHeader($s : supplierName) 
 $myContractHeader:ContractHeader(supplierName == $s) 
  contractHeader:ContractHeader(supplierId >= 100)  
 ) 
  then 
 System.out.print("Test, Drools rule - 0.! "); System.out.println(contractHeader.getSupplierName()); 
 end; 

例外是:

Exception in thread "main" java.lang.RuntimeException: Unable to get KieModule, Errors Existed: Error Messages:
Message [id=1, kieBase=defaultKieBase, level=ERROR, path=rules/Sample.drl, line=10, column=0
   text=[ERR 102] Line 10:1 mismatched input '$myContractHeader' in rule "Rule Name - 0"]
Message [id=2, kieBase=defaultKieBase, level=ERROR, path=rules/Sample.drl, line=0, column=0
   text=Parser returned a null Package]
---

1 个答案:

答案 0 :(得分:0)

可以做一些事情来使规则看起来更好:

  • 在第一个模式之后和;关键字之后删除end
  • 在规则名称之后以及\nwhen关键字之后添加then
  • 在每个模式之后添加一个\n
  • 请勿在模式之间使用不必要的&&

这些更改之后,您的规则应如下所示:

rule "Rule Name - 0" 
when 
  cHeader:ContractHeader() 
  eval(isRuleAppliesToSupplier((cHeader.getSupplierName()) , 0 )) && 
  (  
    $mdmContractHeader : MdmContractHeader( $s : supplierName )  
    $contractHeader1 : ContractHeader ( supplierName == $s ) 
    contractHeader: ContractHeader(supplierId >= 100)
  ) 
then 
  System.out.print("Test, Drools rule - 0.! "); System.out.println(contractHeader.getSupplierName()); 
end; 

我的猜测是,该错误是由于在\n模式之后缺少MdmContractHeader()而引起的。

希望有帮助,