我在同一个文件夹中有以下两个文件。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>