We have an app where we call drools rule engine inside the reducer task of a Hadoop map reduce job. It throws a NullPointerException when we the rule package resource to the knowledge
builder. The same code works fine when run as part of a stand alone app.
Code:
private static Map<String, StatefulKnowledgeSession>
sessions =
new
HashMap<String, StatefulKnowledgeSession>();
private static final String RULE_PACK_DIR = "file:///home/roger/Projects/gridx/";
private static final String RULE_PACK_EXT =
".drl";
public int process(String rulePackage, String dateTime,
String type) throws TException {
int rate = 0;
StatefulKnowledgeSession session =
sessions.get(rulePackage);
if (null == session){
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
String
rulePackPath = RULE_PACK_DIR + rulePackage + RULE_PACK_EXT;
kbuilder.add( ResourceFactory.newFileResource(rulePackPath ),
ResourceType.DRL);
if (
kbuilder.hasErrors() ) {
System.err.println( kbuilder.getErrors().toString() );
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
session =
kbase.newStatefulKnowledgeSession();
sessions.put(rulePackage,
session);
}
ContractRule contractRule = new
ContractRule();
contractRule.prepare(dateTime,
type);;
FactHandle ruleHandle =
session.insert(contractRule);
session.fireAllRules();
System.out.println("" +
contractRule);
rate = contractRule.getRate();
session.retract(ruleHandle);
return rate;
}
This line throws the exception:
kbuilder.add( ResourceFactory.newFileResource(rulePackPath ), ResourceType.DRL);
It works fine as a stand alone app, outside hadoop
Roger Smith