当我们在字符串缓冲区中编写drools规则时出现错误

时间:2019-04-25 09:19:06

标签: java drools

我正在使用字符串缓冲区设置drools规则,其中规则将从db数据构建。下面是我的代码片段,用于在字符串缓冲区中准备规则。

    List<DbRules> dbRuleList = getAllRules();   
    KieSession kieSession = null;
    StringBuffer ruleBuffer = new StringBuffer();
    ruleBuffer.append("import com.model.ContractHeader; import static com.service.SupplierService.isRuleAppliesToSupplier;  ");
    for(DbRules rule : dbRuleList) {
        ruleBuffer.append(" rule \""+rule.getRuleName()+"\" when  eval( isRuleAppliesToSupplier( cHeader : ContractHeader (supplierName) , "+rule.getRuleId()+" )) && ("+rule.getLhs()+") then "+rule.getRhs()+" 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=1, column=0
text=Rule Compilation error cHeader cannot be resolved to a variable
Syntax error on token ":", invalid AssignmentOperator
supplierName cannot be resolved to a variable]

警告消息:

信息消息:

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.StringDrools.executeRules(StringDrools.java:88)
at com.sample.StringDrools.main(StringDrools.java:41)

1 个答案:

答案 0 :(得分:0)

通过进行一些代码修改可以解决编译错误。谢谢!

  for(DbRules rule : dbRuleList) {
        ruleBuffer.append(" rule \""+rule.getRuleName()+"\" when cHeader:ContractHeader()  eval( isRuleAppliesToSupplier(  (cHeader.getSupplierName()) , "+rule.getRuleId()+" )) && ("+rule.getLhs()+") then "+rule.getRhs()+" end; ");
    }