Reputation: 2381
I have below two files in the same folder.RuleFile.drl is the rules library and Sample.drl is the functions library. I am getting error mentioned below when I try to execute the rules.
I am not sure what am I missing and how to resolve this error. Any help is greatly appreciated.
File: RuleFile.drl
package com.sample
rule "A stand alone rule"
when
//conditions
then
myFunction(5);
end
File: Sample.drl
package com.sample
function int myFunction(int val){
return val;
}
I am getting the below error message in the eclipse and also when I run the code.
The method myFunction(int) is undefined for the type Rule_A_stand_alone_rule_3e142aa079ee4a37b0fb21f9e9d9c0da
Java Code:
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;
}
}
RuleConfig.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>
Upvotes: 1
Views: 2857
Reputation: 7223
Drools need that rules are ordered: so in your xml file change the order, your function myFunction() is not actually read yet by drools when it starts parsing RuleFile.drl: I think this is why you get the error. Sample.drl must be parsed before RuleFile.drl and the order in your XML file is important.
Upvotes: 1