Search in sources :

Example 81 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class KnowledgeBaseImpl method removeObjectsGeneratedFromResource.

public boolean removeObjectsGeneratedFromResource(Resource resource) {
    boolean modified = false;
    for (InternalKnowledgePackage pkg : pkgs.values()) {
        List<RuleImpl> rulesToBeRemoved = pkg.getRulesGeneratedFromResource(resource);
        if (!rulesToBeRemoved.isEmpty()) {
            this.reteooBuilder.removeRules(rulesToBeRemoved);
            // in order to allow the correct flushing of all outstanding staged tuples
            for (RuleImpl rule : rulesToBeRemoved) {
                pkg.removeRule(rule);
            }
        }
        List<Function> functionsToBeRemoved = pkg.removeFunctionsGeneratedFromResource(resource);
        for (Function function : functionsToBeRemoved) {
            internalRemoveFunction(pkg.getName(), function.getName());
        }
        List<Process> processesToBeRemoved = pkg.removeProcessesGeneratedFromResource(resource);
        for (Process process : processesToBeRemoved) {
            processes.remove(process.getId());
        }
        List<TypeDeclaration> removedTypes = pkg.removeTypesGeneratedFromResource(resource);
        boolean resourceTypePackageSomethingRemoved = pkg.removeFromResourceTypePackageGeneratedFromResource(resource);
        modified |= !rulesToBeRemoved.isEmpty() || !functionsToBeRemoved.isEmpty() || !processesToBeRemoved.isEmpty() || !removedTypes.isEmpty() || resourceTypePackageSomethingRemoved;
    }
    return modified;
}
Also used : Function(org.drools.core.rule.Function) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Process(org.kie.api.definition.process.Process) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 82 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class KnowledgePackageImpl method readExternal.

/**
 * Handles the read serialization of the Package. Patterns in Rules may
 * reference generated data which cannot be serialized by default methods.
 * The Package uses PackageCompilationData to hold a reference to the
 * generated bytecode; which must be restored before any Rules. A custom
 * ObjectInputStream, able to resolve classes against the bytecode in the
 * PackageCompilationData, is used to restore the Rules.
 *
 * @param stream, the stream to read data from in order to restore the object;
 *                should be an instance of DroolsObjectInputStream or
 *                InputStream
 */
public void readExternal(ObjectInput stream) throws IOException, ClassNotFoundException {
    boolean isDroolsStream = stream instanceof DroolsObjectInputStream;
    DroolsObjectInputStream in = isDroolsStream ? (DroolsObjectInputStream) stream : new DroolsObjectInputStream(new ByteArrayInputStream((byte[]) stream.readObject()));
    this.name = (String) in.readObject();
    this.classFieldAccessorStore = (ClassFieldAccessorStore) in.readObject();
    in.setStore(this.classFieldAccessorStore);
    this.dialectRuntimeRegistry = (DialectRuntimeRegistry) in.readObject();
    this.typeDeclarations = (Map) in.readObject();
    this.imports = (Map<String, ImportDeclaration>) in.readObject();
    this.staticImports = (Set) in.readObject();
    this.functions = (Map<String, Function>) in.readObject();
    this.accumulateFunctions = (Map<String, AccumulateFunction>) in.readObject();
    this.factTemplates = (Map) in.readObject();
    this.ruleFlows = (Map) in.readObject();
    this.globals = (Map<String, String>) in.readObject();
    this.valid = in.readBoolean();
    this.needStreamMode = in.readBoolean();
    this.rules = (Map<String, RuleImpl>) in.readObject();
    this.entryPointsIds = (Set<String>) in.readObject();
    this.windowDeclarations = (Map<String, WindowDeclaration>) in.readObject();
    this.traitRegistry = (TraitRegistry) in.readObject();
    this.resourceTypePackages = (Map<ResourceType, ResourceTypePackage>) in.readObject();
    in.setStore(null);
    if (!isDroolsStream) {
        in.close();
    }
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Function(org.drools.core.rule.Function) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) ByteArrayInputStream(java.io.ByteArrayInputStream) WindowDeclaration(org.drools.core.rule.WindowDeclaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ResourceType(org.kie.api.io.ResourceType) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage)

Example 83 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class DroolsAbstractPMMLTest method getStartingRuleUnit.

protected Class<? extends RuleUnit> getStartingRuleUnit(String startingRule, InternalKnowledgeBase ikb, List<String> possiblePackages) {
    RuleUnitRegistry unitRegistry = ikb.getRuleUnitRegistry();
    Map<String, InternalKnowledgePackage> pkgs = ikb.getPackagesMap();
    RuleImpl ruleImpl = null;
    for (String pkgName : possiblePackages) {
        if (pkgs.containsKey(pkgName)) {
            InternalKnowledgePackage pkg = pkgs.get(pkgName);
            ruleImpl = pkg.getRule(startingRule);
            if (ruleImpl != null) {
                RuleUnitDescr descr = unitRegistry.getRuleUnitFor(ruleImpl).orElse(null);
                if (descr != null) {
                    return descr.getRuleUnitClass();
                }
            }
        }
    }
    return null;
}
Also used : RuleUnitRegistry(org.drools.core.ruleunit.RuleUnitRegistry) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 84 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class PMMLExecutor method startingRuleUnit.

protected Class<? extends RuleUnit> startingRuleUnit(String startingRule, String modelId, String... knownPackageNames) {
    List<String> possiblePackages = calculatePossiblePackageNames(modelId, knownPackageNames);
    InternalKnowledgeBase internalKnowledgeBase = (InternalKnowledgeBase) kieBase;
    RuleUnitRegistry unitRegistry = internalKnowledgeBase.getRuleUnitRegistry();
    Map<String, InternalKnowledgePackage> pkgs = internalKnowledgeBase.getPackagesMap();
    RuleImpl ruleImpl = null;
    for (String pkgName : possiblePackages) {
        if (pkgs.containsKey(pkgName)) {
            InternalKnowledgePackage pkg = pkgs.get(pkgName);
            ruleImpl = pkg.getRule(startingRule);
            if (ruleImpl != null) {
                RuleUnitDescr descr = unitRegistry.getRuleUnitFor(ruleImpl).orElse(null);
                if (descr != null) {
                    return descr.getRuleUnitClass();
                }
            }
        }
    }
    return null;
}
Also used : RuleUnitRegistry(org.drools.core.ruleunit.RuleUnitRegistry) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 85 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class KiePackagesBuilder method build.

public CanonicalKiePackages build() {
    for (Model model : models) {
        for (TypeMetaData metaType : model.getTypeMetaDatas()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(metaType.getPackage(), this::createKiePackage);
            pkg.addTypeDeclaration(createTypeDeclaration(pkg, metaType));
        }
        for (Global global : model.getGlobals()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(global.getPackage(), this::createKiePackage);
            pkg.addGlobal(global.getName(), global.getType());
        }
        for (Query query : model.getQueries()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(query.getPackage(), this::createKiePackage);
            pkg.addRule(compileQuery(pkg, query));
        }
        int ruleCounter = 0;
        for (Rule rule : model.getRules()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(rule.getPackage(), this::createKiePackage);
            RuleImpl ruleImpl = compileRule(pkg, rule);
            ruleImpl.setLoadOrder(ruleCounter++);
            pkg.addRule(ruleImpl);
        }
    }
    return new CanonicalKiePackages(packages);
}
Also used : TypeMetaData(org.drools.model.TypeMetaData) Query(org.drools.model.Query) DroolsQuery(org.drools.core.base.DroolsQuery) Model(org.drools.model.Model) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Rule(org.drools.model.Rule) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Global(org.drools.model.Global) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) FlowDSL.entryPoint(org.drools.model.FlowDSL.entryPoint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) EntryPoint(org.drools.model.EntryPoint) Constraint(org.drools.model.Constraint)

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