Search in sources :

Example 6 with WindowDeclaration

use of org.drools.core.rule.WindowDeclaration 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.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.globals = (Map<String, Class<?>>) 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.resourceTypePackages = (ResourceTypePackageRegistry) in.readObject();
    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) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction)

Example 7 with WindowDeclaration

use of org.drools.core.rule.WindowDeclaration in project drools by kiegroup.

the class KnowledgeBaseImpl method kBaseInternal_addPackages.

public void kBaseInternal_addPackages(Collection<InternalKnowledgePackage> clonedPkgs, Collection<InternalWorkingMemory> workingMemories) {
    // we need to merge all byte[] first, so that the root classloader can resolve classes
    for (InternalKnowledgePackage newPkg : clonedPkgs) {
        newPkg.checkValidity();
        newPkg.mergeTraitRegistry(this);
        InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
        if (pkg == null) {
            pkg = CoreComponentFactory.get().createKnowledgePackage(newPkg.getName());
            pkg.setClassFieldAccessorCache(this.classFieldAccessorCache);
            pkgs.put(pkg.getName(), pkg);
        }
        // first merge anything related to classloader re-wiring
        pkg.getDialectRuntimeRegistry().merge(newPkg.getDialectRuntimeRegistry(), this.rootClassLoader, true);
    }
    processAllTypesDeclaration(clonedPkgs);
    for (InternalKnowledgePackage newPkg : clonedPkgs) {
        // Add functions
        JavaDialectRuntimeData runtime = ((JavaDialectRuntimeData) newPkg.getDialectRuntimeRegistry().getDialectData("java"));
        for (Function function : newPkg.getFunctions().values()) {
            String functionClassName = function.getClassName();
            try {
                registerFunctionClassAndInnerClasses(functionClassName, runtime, this::registerAndLoadTypeDefinition);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException("Unable to compile function '" + function.getName() + "'", e);
            }
        }
    }
    // now iterate again, this time onBeforeExecute will handle any wiring or cloader re-creating that needs to be done as part of the merge
    for (InternalKnowledgePackage newPkg : clonedPkgs) {
        InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
        // this needs to go here, as functions will set a java dialect to dirty
        if (newPkg.getFunctions() != null) {
            for (Map.Entry<String, Function> entry : newPkg.getFunctions().entrySet()) {
                pkg.addFunction(entry.getValue());
            }
        }
        pkg.getDialectRuntimeRegistry().onBeforeExecute();
        // with the classloader recreated for all byte[] classes, we should now merge and wire any new accessors
        pkg.mergeStore(newPkg);
    }
    for (InternalKnowledgePackage newPkg : clonedPkgs) {
        InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
        // now merge the new package into the existing one
        mergePackage(pkg, newPkg, workingMemories);
        // add the window declarations to the kbase
        for (WindowDeclaration window : newPkg.getWindowDeclarations().values()) {
            this.reteooBuilder.addNamedWindow(window, workingMemories);
        }
        // add entry points to the kbase
        for (String entryPointId : newPkg.getEntryPointIds()) {
            this.reteooBuilder.addEntryPoint(entryPointId, workingMemories);
        }
        // add the rules to the RuleBase
        kBaseInternal_addRules(newPkg.getRules(), workingMemories);
        // add the flows to the RuleBase
        if (newPkg.getRuleFlows() != null) {
            final Map<String, Process> flows = newPkg.getRuleFlows();
            for (Process process : flows.values()) {
                kBaseInternal_addProcess(process);
            }
        }
        if (!newPkg.getResourceTypePackages().isEmpty()) {
            KieWeavers weavers = KieService.load(KieWeavers.class);
            for (ResourceTypePackage rtkKpg : newPkg.getResourceTypePackages().values()) {
                weavers.weave(newPkg, rtkKpg);
            }
        }
        ruleUnitDescriptionRegistry.add(newPkg.getRuleUnitDescriptionLoader());
    }
    if (config.isMultithreadEvaluation() && !hasMultiplePartitions()) {
        disableMultithreadEvaluation("The rete network cannot be partitioned: disabling multithread evaluation");
    }
}
Also used : Process(org.kie.api.definition.process.Process) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) KieWeavers(org.kie.api.internal.weaver.KieWeavers) Function(org.drools.core.rule.Function) WindowDeclaration(org.drools.core.rule.WindowDeclaration) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 8 with WindowDeclaration

use of org.drools.core.rule.WindowDeclaration in project drools by kiegroup.

the class KiePackagesBuilder method createWindowReference.

private <T> void createWindowReference(RuleContext ctx, WindowReference<T> window) {
    WindowDeclaration windowDeclaration = new WindowDeclaration(window.getName(), ctx.getPkg().getName());
    Variable<T> variable = declarationOf(window.getPatternType());
    Pattern windowPattern = new Pattern(ctx.getNextPatternIndex(), getClassObjectType(window.getPatternType()), variable.getName());
    windowDeclaration.setPattern(windowPattern);
    if (window.getEntryPoint() != null) {
        windowPattern.setSource(new EntryPointId(window.getEntryPoint().getName()));
    }
    for (Predicate1<T> predicate : window.getPredicates()) {
        SingleConstraint singleConstraint = new SingleConstraint1<>(generateName("expr"), variable, predicate);
        ConstraintEvaluator constraintEvaluator = new ConstraintEvaluator(windowPattern, singleConstraint);
        windowPattern.addConstraint(new LambdaConstraint(constraintEvaluator, singleConstraint.predicateInformation()));
    }
    windowPattern.addBehavior(createWindow(window));
    ctx.getPkg().addWindowDeclaration(windowDeclaration);
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) GroupByPattern(org.drools.model.GroupByPattern) SingleConstraint1(org.drools.model.constraints.SingleConstraint1) EntryPointId(org.drools.core.rule.EntryPointId) SingleConstraint(org.drools.model.SingleConstraint) AbstractSingleConstraint(org.drools.model.constraints.AbstractSingleConstraint) WindowDeclaration(org.drools.core.rule.WindowDeclaration) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) TemporalConstraintEvaluator(org.drools.modelcompiler.constraints.TemporalConstraintEvaluator) ConstraintEvaluator(org.drools.modelcompiler.constraints.ConstraintEvaluator)

Example 9 with WindowDeclaration

use of org.drools.core.rule.WindowDeclaration in project drools by kiegroup.

the class TraitKnowledgePackageImpl method readExternal.

@Override
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.globals = (Map<String, Class<?>>) 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 = (TraitRegistryImpl) in.readObject();
    this.resourceTypePackages = (ResourceTypePackageRegistry) 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) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction)

Aggregations

WindowDeclaration (org.drools.core.rule.WindowDeclaration)9 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)6 Function (org.drools.core.rule.Function)5 ImportDeclaration (org.drools.core.rule.ImportDeclaration)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Process (org.kie.api.definition.process.Process)4 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)4 KieWeavers (org.kie.api.internal.weaver.KieWeavers)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 DroolsObjectInputStream (org.drools.core.common.DroolsObjectInputStream)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 Rule (org.kie.api.definition.rule.Rule)3 AccumulateFunction (org.kie.api.runtime.rule.AccumulateFunction)3 ArrayList (java.util.ArrayList)2 JavaDialectRuntimeData (org.drools.core.rule.JavaDialectRuntimeData)2 Pattern (org.drools.core.rule.Pattern)2 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2 KieWeaverService (org.kie.api.internal.weaver.KieWeaverService)2