}
private Map<String, RuleBuildContext> preProcessRules(PackageDescr packageDescr, PackageRegistry pkgRegistry) {
Map<String, RuleBuildContext> ruleCxts = buildRuleBuilderContext(packageDescr.getRules());
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
if (this.kBase != null) {
boolean needsRemoval = false;
// first, check if any rules no longer exist
for( org.kie.api.definition.rule.Rule rule : pkg.getRules() ) {
if (filterAcceptsRemoval( rule.getPackageName(), rule.getName() ) ) {
needsRemoval = true;
break;
}
}
if( !needsRemoval ) {
for (RuleDescr ruleDescr : packageDescr.getRules()) {
if (filterAccepts(ruleDescr.getNamespace(), ruleDescr.getName()) ) {
if (pkg.getRule(ruleDescr.getName()) != null) {
needsRemoval = true;
break;
}
}
}
}
if (needsRemoval) {
try {
this.kBase.lock();
for( org.kie.api.definition.rule.Rule rule : pkg.getRules() ) {
if (filterAcceptsRemoval( rule.getPackageName(), rule.getName() ) ) {
this.kBase.removeRule(pkg, pkg.getRule(rule.getName()));
pkg.removeRule(((RuleImpl)rule));
}
}
for (RuleDescr ruleDescr : packageDescr.getRules()) {
if (filterAccepts(ruleDescr.getNamespace(), ruleDescr.getName()) ) {
if (pkg.getRule(ruleDescr.getName()) != null) {
// XXX: this one notifies listeners
this.kBase.removeRule(pkg, pkg.getRule(ruleDescr.getName()));
}
}
}
} finally {
this.kBase.unlock();
}
}
}
// Pre Process each rule, needed for Query signuture registration
for (RuleDescr ruleDescr : packageDescr.getRules()) {
if (filterAccepts(ruleDescr.getNamespace(), ruleDescr.getName()) ) {
RuleBuildContext ruleBuildContext = ruleCxts.get(ruleDescr.getName());
ruleBuilder.preProcess(ruleBuildContext);
pkg.addRule(ruleBuildContext.getRule());
}
}
return ruleCxts;
}