Search in sources :

Example 61 with RuleImpl

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;
}
Also used : RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl)

Example 62 with 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);
}
Also used : CanonicalKiePackages(org.drools.modelcompiler.CanonicalKiePackages) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) ResourceChange(org.kie.internal.builder.ResourceChange) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 63 with RuleImpl

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);
    }
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) DuplicateRule(org.drools.compiler.compiler.DuplicateRule) ParserError(org.drools.compiler.compiler.ParserError) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) BaseResource(org.drools.core.io.impl.BaseResource) DescrResource(org.drools.core.io.impl.DescrResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Resource(org.kie.api.io.Resource) ReaderResource(org.drools.core.io.impl.ReaderResource) PMMLResource(org.drools.compiler.compiler.PMMLResource) InternalResource(org.drools.core.io.internal.InternalResource) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) HashSet(java.util.HashSet) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 64 with RuleImpl

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);
        }
    }
}
Also used : RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Process(org.kie.api.definition.process.Process) ImportDeclaration(org.drools.core.rule.ImportDeclaration) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 65 with RuleImpl

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);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)107 Test (org.junit.Test)51 Pattern (org.drools.core.rule.Pattern)30 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)28 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)21 WorkingMemory (org.drools.core.WorkingMemory)21 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)18 BuildContext (org.drools.core.reteoo.builder.BuildContext)16 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)16 Consequence (org.drools.core.spi.Consequence)15 GroupElement (org.drools.core.rule.GroupElement)14 ClassObjectType (org.drools.core.base.ClassObjectType)13 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)13 IOException (java.io.IOException)12 ObjectInput (java.io.ObjectInput)12 ObjectOutput (java.io.ObjectOutput)12 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)12 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)12 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)11 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)11