Search in sources :

Example 6 with ResourceTypePackage

use of org.kie.api.internal.io.ResourceTypePackage 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 7 with ResourceTypePackage

use of org.kie.api.internal.io.ResourceTypePackage 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 8 with ResourceTypePackage

use of org.kie.api.internal.io.ResourceTypePackage in project drools by kiegroup.

the class BayesAssemblerService method addResource.

@Override
public void addResource(Object kbuilder, Resource resource, ResourceType type, ResourceConfiguration configuration) throws Exception {
    BayesNetwork network;
    JunctionTreeBuilder builder;
    KnowledgeBuilder kb = (KnowledgeBuilder) kbuilder;
    Bif bif = XmlBifParser.loadBif(resource, kb.getErrors());
    if (bif == null) {
        return;
    }
    try {
        network = XmlBifParser.buildBayesNetwork(bif);
    } catch (Exception e) {
        kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to parse opening Stream:\n" + e.toString()));
        return;
    }
    try {
        builder = new JunctionTreeBuilder(network);
    } catch (Exception e) {
        kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to build Junction Tree:\n" + e.toString()));
        return;
    }
    KnowledgeBuilderImpl kbuilderImpl = (KnowledgeBuilderImpl) kbuilder;
    PackageRegistry pkgReg = kbuilderImpl.getOrCreatePackageRegistry(new PackageDescr(network.getPackageName()));
    InternalKnowledgePackage kpkgs = pkgReg.getPackage();
    Map<ResourceType, ResourceTypePackage> rpkg = kpkgs.getResourceTypePackages();
    BayesPackage bpkg = (BayesPackage) rpkg.get(ResourceType.BAYES);
    if (bpkg == null) {
        bpkg = new BayesPackage();
        rpkg.put(ResourceType.BAYES, bpkg);
    }
    bpkg.addJunctionTree(network.getName(), builder.build());
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) ResourceType(org.kie.api.io.ResourceType) BayesNetwork(org.drools.beliefs.bayes.BayesNetwork) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Bif(org.drools.beliefs.bayes.model.Bif) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 9 with ResourceTypePackage

use of org.kie.api.internal.io.ResourceTypePackage in project drools by kiegroup.

the class WeaverTest method testBayesPackageWeaving.

@Test
public void testBayesPackageWeaving() throws Exception {
    KnowledgeBuilderImpl kbuilder = new KnowledgeBuilderImpl();
    kbuilder.add(ResourceFactory.newClassPathResource("Garden.xmlbif", AssemblerTest.class), ResourceType.BAYES);
    InternalKnowledgeBase kbase = getKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    InternalKnowledgePackage kpkg = (InternalKnowledgePackage) kbase.getKiePackage("org.drools.beliefs.bayes.integration");
    Map<ResourceType, ResourceTypePackage> map = kpkg.getResourceTypePackages();
    BayesPackage existing = (BayesPackage) map.get(ResourceType.BAYES);
    JunctionTree jtree = existing.getJunctionTree("Garden");
    assertNotNull(jtree);
}
Also used : BayesPackage(org.drools.beliefs.bayes.assembler.BayesPackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) JunctionTree(org.drools.beliefs.bayes.JunctionTree) ResourceType(org.kie.api.io.ResourceType) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 10 with ResourceTypePackage

use of org.kie.api.internal.io.ResourceTypePackage in project drools by kiegroup.

the class DMNAssemblerService method addResource.

@Override
public void addResource(Object kbuilder, Resource resource, ResourceType type, ResourceConfiguration configuration) throws Exception {
    logger.warn("invoked legacy addResource (no control on the order of the assembler compilation): " + resource.getSourcePath());
    KnowledgeBuilderImpl kbuilderImpl = (KnowledgeBuilderImpl) kbuilder;
    DMNCompiler dmnCompiler = kbuilderImpl.getCachedOrCreate(DMN_COMPILER_CACHE_KEY, () -> getCompiler(kbuilderImpl));
    Collection<DMNModel> dmnModels = new ArrayList<>();
    for (PackageRegistry pr : kbuilderImpl.getPackageRegistry().values()) {
        ResourceTypePackage resourceTypePackage = pr.getPackage().getResourceTypePackages().get(ResourceType.DMN);
        if (resourceTypePackage != null) {
            DMNPackageImpl dmnpkg = (DMNPackageImpl) resourceTypePackage;
            dmnModels.addAll(dmnpkg.getAllModels().values());
        }
    }
    compileResourceToModel(kbuilderImpl, dmnCompiler, resource, dmnModels);
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) DMNPackageImpl(org.kie.dmn.core.impl.DMNPackageImpl) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ArrayList(java.util.ArrayList) DMNCompiler(org.kie.dmn.api.core.DMNCompiler) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) DMNModel(org.kie.dmn.api.core.DMNModel)

Aggregations

ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)11 ResourceType (org.kie.api.io.ResourceType)10 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)8 Map (java.util.Map)3 BayesPackage (org.drools.beliefs.bayes.assembler.BayesPackage)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 WindowDeclaration (org.drools.core.rule.WindowDeclaration)3 DMNModel (org.kie.dmn.api.core.DMNModel)3 DMNPackageImpl (org.kie.dmn.core.impl.DMNPackageImpl)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 JunctionTree (org.drools.beliefs.bayes.JunctionTree)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2 Function (org.drools.core.rule.Function)2 ImportDeclaration (org.drools.core.rule.ImportDeclaration)2 Process (org.kie.api.definition.process.Process)2 Rule (org.kie.api.definition.rule.Rule)2