public void setXPathEventHandler(XPathEventHandler xPathEventHandler) {
this.xPathEventHandler = xPathEventHandler;
}
public XPathEvaluator create() throws Exception {
XPathBuilder builder = new XPathBuilder();
for (Map.Entry<String, String> entry : prefixes.entrySet()) {
builder.addPrefix(entry.getKey(), entry.getValue());
}
if (workingMemory == null && ruleBase == null && packageStream == null) {
throw new RuntimeException("You must set either the WorkingMemory, RuleBase, or PackageStream properties.");
}
if (packageStream != null) {
// process package stream
ruleBase = RuleBaseFactory.newRuleBase();
final PackageBuilder packageBuilder = new PackageBuilder();
packageBuilder.addPackageFromDrl(new InputStreamReader(packageStream));
ruleBase.addPackage(packageBuilder.getPackage());
workingMemory = ruleBase.newStatefulSession(true);
} else {
if (ruleBase != null) {
// process rule base
workingMemory = ruleBase.newStatefulSession(true);
} else {
// process working memory
ruleBase = workingMemory.getRuleBase();
}
}
assert (ruleBase != null);
assert (workingMemory != null);
final org.drools.rule.Package[] packages = ruleBase.getPackages();
final Set<String> xPathStrings = new HashSet<String>();
for (final org.drools.rule.Package aPackage : packages) {
final Rule[] rules = aPackage.getRules();
for (final Rule rule : rules) {
final GroupElement lhs = rule.getLhs();
final List list = lhs.getChildren();
processList(list, xPathStrings);
}
}
if (xPathEventHandler == null) {
// allow them to pass in their own
xPathEventHandler = new DroolsXPathEventHandler(workingMemory, fireAllInOnMatch);
}
for (String xPathString : xPathStrings) {
builder.listen(xPathString, xPathEventHandler);
}
return builder.compile();
}