Search in sources :

Example 1 with Function

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

the class KnowledgeBuilderImpl method addPackage.

public synchronized void addPackage(InternalKnowledgePackage newPkg) {
    PackageRegistry pkgRegistry = this.pkgRegistryMap.get(newPkg.getName());
    InternalKnowledgePackage pkg = null;
    if (pkgRegistry != null) {
        pkg = pkgRegistry.getPackage();
    }
    if (pkg == null) {
        PackageDescr packageDescr = new PackageDescr(newPkg.getName());
        pkgRegistry = getOrCreatePackageRegistry(packageDescr);
        mergePackage(this.pkgRegistryMap.get(packageDescr.getNamespace()), packageDescr);
        pkg = pkgRegistry.getPackage();
    }
    // first merge anything related to classloader re-wiring
    pkg.getDialectRuntimeRegistry().merge(newPkg.getDialectRuntimeRegistry(), this.rootClassLoader);
    if (newPkg.getFunctions() != null) {
        for (Map.Entry<String, Function> entry : newPkg.getFunctions().entrySet()) {
            if (pkg.getFunctions().containsKey(entry.getKey())) {
                addBuilderResult(new DuplicateFunction(entry.getValue(), this.configuration));
            }
            pkg.addFunction(entry.getValue());
        }
    }
    pkg.getClassFieldAccessorStore().merge(newPkg.getClassFieldAccessorStore());
    pkg.getDialectRuntimeRegistry().onBeforeExecute();
    // we have to do this before the merging, as it does some classloader resolving
    TypeDeclaration lastType = null;
    try {
        // Resolve the class for the type declaation
        if (newPkg.getTypeDeclarations() != null) {
            // add type declarations
            for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
                lastType = type;
                type.setTypeClass(this.rootClassLoader.loadClass(type.getTypeClassName()));
            }
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("unable to resolve Type Declaration class '" + lastType.getTypeName() + "'");
    }
    // now merge the new package into the existing one
    mergePackage(pkg, newPkg);
}
Also used : AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) DuplicateFunction(org.drools.compiler.compiler.DuplicateFunction) Function(org.drools.core.rule.Function) DuplicateFunction(org.drools.compiler.compiler.DuplicateFunction) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 2 with Function

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

the class KnowledgeBuilderImpl method processFunctions.

private void processFunctions(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
    for (FunctionDescr function : packageDescr.getFunctions()) {
        Function existingFunc = pkgRegistry.getPackage().getFunctions().get(function.getName());
        if (existingFunc != null && function.getNamespace().equals(existingFunc.getNamespace())) {
            addBuilderResult(new DuplicateFunction(function, this.configuration));
        }
    }
    for (final FunctionImportDescr functionImport : packageDescr.getFunctionImports()) {
        String importEntry = functionImport.getTarget();
        pkgRegistry.addStaticImport(functionImport);
        pkgRegistry.getPackage().addStaticImport(importEntry);
    }
}
Also used : AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) DuplicateFunction(org.drools.compiler.compiler.DuplicateFunction) Function(org.drools.core.rule.Function) DuplicateFunction(org.drools.compiler.compiler.DuplicateFunction) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr)

Example 3 with Function

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

the class KnowledgeBaseImpl method internalAddPackages.

private void internalAddPackages(List<InternalKnowledgePackage> clonedPkgs) {
    for (InternalWorkingMemory wm : getWorkingMemories()) {
        wm.flushPropagations();
    }
    // we need to merge all byte[] first, so that the root classloader can resolve classes
    for (InternalKnowledgePackage newPkg : clonedPkgs) {
        newPkg.checkValidity();
        this.eventSupport.fireBeforePackageAdded(newPkg);
        if (newPkg.hasTraitRegistry()) {
            getTraitRegistry().merge(newPkg.getTraitRegistry());
        }
        InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
        if (pkg == null) {
            pkg = new KnowledgePackageImpl(newPkg.getName());
            // @TODO we really should have a single root cache
            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.getClassFieldAccessorStore().merge(newPkg.getClassFieldAccessorStore());
    }
    for (InternalKnowledgePackage newPkg : clonedPkgs) {
        InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
        // now merge the new package into the existing one
        mergePackage(pkg, newPkg);
        // add the window declarations to the kbase
        for (WindowDeclaration window : newPkg.getWindowDeclarations().values()) {
            this.reteooBuilder.addNamedWindow(window);
        }
        // add entry points to the kbase
        for (String id : newPkg.getEntryPointIds()) {
            this.reteooBuilder.addEntryPoint(id);
        }
        // add the rules to the RuleBase
        for (Rule r : newPkg.getRules()) {
            RuleImpl rule = (RuleImpl) r;
            checkMultithreadedEvaluation(rule);
            internalAddRule(rule);
        }
        // add the flows to the RuleBase
        if (newPkg.getRuleFlows() != null) {
            final Map<String, Process> flows = newPkg.getRuleFlows();
            for (Process process : flows.values()) {
                internalAddProcess(process);
            }
        }
        if (!newPkg.getResourceTypePackages().isEmpty()) {
            KieWeavers weavers = ServiceRegistry.getInstance().get(KieWeavers.class);
            for (ResourceTypePackage rtkKpg : newPkg.getResourceTypePackages().values()) {
                ResourceType rt = rtkKpg.getResourceType();
                KieWeaverService factory = weavers.getWeavers().get(rt);
                factory.weave(this, newPkg, rtkKpg);
            }
        }
        ruleUnitRegistry.add(newPkg.getRuleUnitRegistry());
        this.eventSupport.fireAfterPackageAdded(newPkg);
    }
    if (config.isMultithreadEvaluation() && !hasMultiplePartitions()) {
        disableMultithreadEvaluation("The rete network cannot be partitioned: disabling multithread evaluation");
    }
}
Also used : KieWeaverService(org.kie.api.internal.weaver.KieWeaverService) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Process(org.kie.api.definition.process.Process) ResourceType(org.kie.api.io.ResourceType) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) KieWeavers(org.kie.api.internal.weaver.KieWeavers) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Function(org.drools.core.rule.Function) WindowDeclaration(org.drools.core.rule.WindowDeclaration) Rule(org.kie.api.definition.rule.Rule) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) 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 4 with Function

use of org.drools.core.rule.Function 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 5 with Function

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

the class KnowledgeBaseImpl method internalRemoveFunction.

private void internalRemoveFunction(String packageName, String functionName) {
    final InternalKnowledgePackage pkg = this.pkgs.get(packageName);
    if (pkg == null) {
        throw new IllegalArgumentException("Package name '" + packageName + "' does not exist for this Rule Base.");
    }
    Function function = pkg.getFunctions().get(functionName);
    if (function == null) {
        throw new IllegalArgumentException("function name '" + packageName + "' does not exist in the Package '" + packageName + "'.");
    }
    this.eventSupport.fireBeforeFunctionRemoved(pkg, functionName);
    pkg.removeFunction(functionName);
    this.eventSupport.fireAfterFunctionRemoved(pkg, functionName);
    if (rootClassLoader instanceof ProjectClassLoader) {
        ((ProjectClassLoader) rootClassLoader).undefineClass(function.getClassName());
    }
    addReloadDialectDatas(pkg.getDialectRuntimeRegistry());
}
Also used : ProjectClassLoader.createProjectClassLoader(org.drools.core.common.ProjectClassLoader.createProjectClassLoader) ProjectClassLoader(org.drools.core.common.ProjectClassLoader) Function(org.drools.core.rule.Function) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

Function (org.drools.core.rule.Function)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 AccumulateFunction (org.kie.api.runtime.rule.AccumulateFunction)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 DuplicateFunction (org.drools.compiler.compiler.DuplicateFunction)2 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2 WindowDeclaration (org.drools.core.rule.WindowDeclaration)2 Process (org.kie.api.definition.process.Process)2 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)2 ResourceType (org.kie.api.io.ResourceType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 LinkedHashMap (java.util.LinkedHashMap)1 FunctionErrorHandler (org.drools.compiler.builder.impl.errors.FunctionErrorHandler)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)1 FunctionDescr (org.drools.compiler.lang.descr.FunctionDescr)1 FunctionImportDescr (org.drools.compiler.lang.descr.FunctionImportDescr)1