1

我在同一个文件夹中有以下两个文件。RuleFile.drl 是规则库,Sample.drl 是函数库。当我尝试执行规则时,我收到下面提到的错误。

我不确定我错过了什么以及如何解决此错误。任何帮助是极大的赞赏。

文件:RuleFile.drl

package com.sample
rule "A stand alone rule"
    when
        //conditions
    then
        myFunction(5);
end

文件:Sample.drl

package com.sample
function int myFunction(int val){
    return val;
}

我在 Eclipse 中以及运行代码时收到以下错误消息。

The method myFunction(int) is undefined for the type Rule_A_stand_alone_rule_3e142aa079ee4a37b0fb21f9e9d9c0da

Java 代码:

public class DroolsTest {
    private static final String RULES_CHANGE_SET = "RuleConfig.xml";
    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource(RULES_CHANGE_SET), ResourceType.CHANGE_SET);
        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;
    } 
}

规则配置.xml

 <change-set xmlns='http://drools.org/drools-5.0/change-set'
             xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'
             xs:schemaLocation='http://drools.org/drools-5.0/change-set http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd' >
  <add>
       <resource source='classpath:RuleFile.drl' type='DRL' />
       <resource source='classpath:Sample.drl' type='DRL' />
   </add>
 </change-set>
4

1 回答 1

1

Drools 需要对规则进行排序:所以在你的 xml 文件中更改顺序,当它开始解析 RuleFile.drl 时,你的函数 myFunction() 还没有真正被 drools 读取:我认为这就是你得到错误的原因。Sample.drl 必须在 RuleFile.drl 之前解析,并且 XML 文件中的顺序很重要。

于 2013-11-08T14:25:16.490 回答