use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class KiePackagesBuilder method compileRule.
private RuleImpl compileRule(KnowledgePackageImpl pkg, Rule rule) {
RuleImpl ruleImpl = new RuleImpl(rule.getName());
ruleImpl.setPackage(pkg.getName());
setRuleAttributes(rule, ruleImpl);
setRuleMetaAttributes(rule, ruleImpl);
ruleImpl.setPackage(rule.getPackage());
if (rule.getUnit() != null) {
ruleImpl.setRuleUnitClassName(rule.getUnit());
pkg.getRuleUnitRegistry().getRuleUnitFor(ruleImpl);
}
RuleContext ctx = new RuleContext(this, pkg, ruleImpl);
populateLHS(ctx, pkg, rule.getView());
processConsequences(ctx, rule);
return ruleImpl;
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class CanonicalKieBaseUpdater method run.
@Override
public void run() {
CanonicalKieModule oldKM = (CanonicalKieModule) ctx.currentKM;
CanonicalKieModule newKM = (CanonicalKieModule) ctx.newKM;
List<RuleImpl> rulesToBeRemoved;
List<RuleImpl> rulesToBeAdded;
if (ctx.modifyingUsedClass) {
// remove all ObjectTypeNodes for the modified classes
for (Class<?> cls : ctx.modifiedClasses) {
clearInstancesOfModifiedClass(cls);
}
rulesToBeRemoved = getAllRulesInKieBase(oldKM, ctx.currentKieBaseModel);
rulesToBeAdded = getAllRulesInKieBase(newKM, ctx.newKieBaseModel);
} else {
newKM.setModuleClassLoader(((CanonicalKieModule) ctx.currentKM).getModuleClassLoader());
CanonicalKiePackages newPkgs = newKM.getKiePackages(ctx.newKieBaseModel);
rulesToBeRemoved = new ArrayList<>();
rulesToBeAdded = new ArrayList<>();
for (ResourceChangeSet changeSet : ctx.cs.getChanges().values()) {
if (!isPackageInKieBase(ctx.newKieBaseModel, changeSet.getResourceName())) {
continue;
}
InternalKnowledgePackage oldKpkg = ctx.kBase.getPackage(changeSet.getResourceName());
InternalKnowledgePackage kpkg = (InternalKnowledgePackage) newPkgs.getKiePackage(changeSet.getResourceName());
for (ResourceChange change : changeSet.getChanges()) {
String changedItemName = change.getName();
if (change.getChangeType() == ChangeType.UPDATED || change.getChangeType() == ChangeType.REMOVED) {
if (change.getType() == ResourceChange.Type.GLOBAL) {
ctx.kBase.removeGlobal(changedItemName);
} else {
rulesToBeRemoved.add(oldKpkg.getRule(changedItemName));
}
}
if (change.getChangeType() == ChangeType.UPDATED || change.getChangeType() == ChangeType.ADDED) {
if (change.getType() == ResourceChange.Type.GLOBAL) {
try {
ctx.kBase.addGlobal(changedItemName, kpkg.getTypeResolver().resolveType(kpkg.getGlobals().get(changedItemName)));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else {
rulesToBeAdded.add(kpkg.getRule(changedItemName));
}
}
}
}
}
ctx.kBase.removeRules(rulesToBeRemoved);
ctx.kBase.addRules(rulesToBeAdded);
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class KnowledgeBuilderImpl method validateUniqueRuleNames.
protected void validateUniqueRuleNames(final PackageDescr packageDescr) {
final Set<String> names = new HashSet<String>();
PackageRegistry packageRegistry = this.pkgRegistryMap.get(packageDescr.getNamespace());
InternalKnowledgePackage pkg = null;
if (packageRegistry != null) {
pkg = packageRegistry.getPackage();
}
for (final RuleDescr rule : packageDescr.getRules()) {
validateRule(packageDescr, rule);
final String name = rule.getName();
if (names.contains(name)) {
addBuilderResult(new ParserError(rule.getResource(), "Duplicate rule name: " + name, rule.getLine(), rule.getColumn(), packageDescr.getNamespace()));
}
if (pkg != null) {
RuleImpl duplicatedRule = pkg.getRule(name);
if (duplicatedRule != null) {
Resource resource = rule.getResource();
Resource duplicatedResource = duplicatedRule.getResource();
if (resource == null || duplicatedResource == null || duplicatedResource.getSourcePath() == null || duplicatedResource.getSourcePath().equals(resource.getSourcePath())) {
addBuilderResult(new DuplicateRule(rule, packageDescr, this.configuration));
} else {
addBuilderResult(new ParserError(rule.getResource(), "Duplicate rule name: " + name, rule.getLine(), rule.getColumn(), packageDescr.getNamespace()));
}
}
}
names.add(name);
}
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class KnowledgeBuilderImpl method mergePackage.
/**
* Merge a new package with an existing package. Most of the work is done by
* the concrete implementations, but this class does some work (including
* combining imports, compilation data, globals, and the actual Rule objects
* into the package).
*/
private void mergePackage(InternalKnowledgePackage pkg, InternalKnowledgePackage newPkg) {
// Merge imports
final Map<String, ImportDeclaration> imports = pkg.getImports();
imports.putAll(newPkg.getImports());
String lastType = null;
try {
// merge globals
if (newPkg.getGlobals() != null && newPkg.getGlobals() != Collections.EMPTY_MAP) {
Map<String, String> globals = pkg.getGlobals();
// Add globals
for (final Map.Entry<String, String> entry : newPkg.getGlobals().entrySet()) {
final String identifier = entry.getKey();
final String type = entry.getValue();
lastType = type;
if (globals.containsKey(identifier) && !globals.get(identifier).equals(type)) {
throw new RuntimeException(pkg.getName() + " cannot be integrated");
} else {
pkg.addGlobal(identifier, this.rootClassLoader.loadClass(type));
// this isn't a package merge, it's adding to the rulebase, but I've put it here for convenience
this.globals.put(identifier, this.rootClassLoader.loadClass(type));
}
}
}
} catch (ClassNotFoundException e) {
throw new RuntimeException("Unable to resolve class '" + lastType + "'");
}
// merge the type declarations
if (newPkg.getTypeDeclarations() != null) {
// add type declarations
for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
// @TODO should we allow overrides? only if the class is not in use.
if (!pkg.getTypeDeclarations().containsKey(type.getTypeName())) {
// add to package list of type declarations
pkg.addTypeDeclaration(type);
}
}
}
for (final org.kie.api.definition.rule.Rule newRule : newPkg.getRules()) {
pkg.addRule(((RuleImpl) newRule));
}
// Merge The Rule Flows
if (newPkg.getRuleFlows() != null) {
final Map flows = newPkg.getRuleFlows();
for (Object o : flows.values()) {
final Process flow = (Process) o;
pkg.addProcess(flow);
}
}
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class KnowledgeBuilderImpl method compileRete.
protected void compileRete(PackageDescr packageDescr) {
if (!hasErrors() && this.kBase != null) {
Collection<RuleImpl> rulesToBeAdded = new ArrayList<>();
for (RuleDescr ruleDescr : packageDescr.getRules()) {
if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
InternalKnowledgePackage pkg = pkgRegistryMap.get(ruleDescr.getNamespace()).getPackage();
rulesToBeAdded.add(pkg.getRule(ruleDescr.getName()));
}
}
if (!rulesToBeAdded.isEmpty()) {
this.kBase.addRules(rulesToBeAdded);
}
}
}
Aggregations