Search in sources :

Example 61 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class KnowledgeBaseImpl method addPackage.

@Override
public Future<KiePackage> addPackage(final KiePackage newPkg) {
    InternalKnowledgePackage clonedPkg = ((InternalKnowledgePackage) newPkg).deepCloneIfAlreadyInUse(rootClassLoader);
    CompletableFuture<KiePackage> result = new CompletableFuture<>();
    enqueueModification(() -> {
        internalAddPackages(Collections.singletonList(clonedPkg));
        result.complete(getPackage(newPkg.getName()));
    });
    return result;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 62 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class KnowledgeBaseImpl method readExternal.

// ------------------------------------------------------------
// Instance methods
// ------------------------------------------------------------
/**
 * 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.
 */
public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
    // PackageCompilationData must be restored before Rules as it has the ClassLoader needed to resolve the generated code references in Rules
    DroolsObjectInput droolsStream;
    boolean isDrools = in instanceof DroolsObjectInputStream;
    boolean wasDrools = in.readBoolean();
    if (wasDrools && !isDrools) {
        throw new IllegalArgumentException("The knowledge base was serialized using a DroolsObjectOutputStream. A DroolsObjectInputStream is required for deserialization.");
    }
    if (isDrools) {
        droolsStream = (DroolsObjectInput) in;
    } else {
        ByteArrayInputStream bytes = new ByteArrayInputStream((byte[]) in.readObject());
        droolsStream = new DroolsObjectInputStream(bytes);
    }
    boolean classLoaderCacheEnabled = droolsStream.readBoolean();
    Map<String, byte[]> store = (Map<String, byte[]>) droolsStream.readObject();
    this.rootClassLoader = createProjectClassLoader(droolsStream.getParentClassLoader(), store);
    droolsStream.setClassLoader(this.rootClassLoader);
    droolsStream.setKnowledgeBase(this);
    this.classFieldAccessorCache = new ClassFieldAccessorCache(this.rootClassLoader);
    this.config = (RuleBaseConfiguration) droolsStream.readObject();
    this.config.setClassLoader(droolsStream.getParentClassLoader());
    this.sessionConfiguration = new SessionConfigurationImpl(null, config.getClassLoader(), config.getChainedProperties());
    kieComponentFactory = getConfiguration().getComponentFactory();
    this.pkgs = (Map<String, InternalKnowledgePackage>) droolsStream.readObject();
    for (InternalKnowledgePackage pkg : this.pkgs.values()) {
        pkg.getDialectRuntimeRegistry().onAdd(this.rootClassLoader);
    }
    // PackageCompilationData must be restored before Rules as it has the ClassLoader needed to resolve the generated code references in Rules
    this.id = (String) droolsStream.readObject();
    this.workingMemoryCounter.set(droolsStream.readInt());
    this.processes = (Map<String, Process>) droolsStream.readObject();
    Class cls = null;
    try {
        cls = droolsStream.getParentClassLoader().loadClass(droolsStream.readUTF());
        this.factHandleFactory = (FactHandleFactory) cls.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        DroolsObjectInputStream.newInvalidClassException(cls, e);
    }
    for (InternalKnowledgePackage pkg : this.pkgs.values()) {
        pkg.getDialectRuntimeRegistry().onBeforeExecute();
        pkg.getClassFieldAccessorStore().setClassFieldAccessorCache(this.classFieldAccessorCache);
        pkg.getClassFieldAccessorStore().wire();
    }
    this.populateTypeDeclarationMaps();
    // read globals
    Map<String, String> globs = (Map<String, String>) droolsStream.readObject();
    populateGlobalsMap(globs);
    this.eventSupport = (KieBaseEventSupport) droolsStream.readObject();
    this.eventSupport.setKnowledgeBase(this);
    this.reteooBuilder = (ReteooBuilder) droolsStream.readObject();
    this.reteooBuilder.setRuleBase(this);
    this.rete = (Rete) droolsStream.readObject();
    this.resolvedReleaseId = (ReleaseId) droolsStream.readObject();
    ((DroolsObjectInputStream) droolsStream).bindAllExtractors(this);
    if (!isDrools) {
        droolsStream.close();
    }
    this.getConfiguration().getComponentFactory().getTraitFactory().setRuleBase(this);
    rewireReteAfterDeserialization();
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Process(org.kie.api.definition.process.Process) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) DroolsObjectInput(org.drools.core.common.DroolsObjectInput) ByteArrayInputStream(java.io.ByteArrayInputStream) SessionConfigurationImpl(org.drools.core.SessionConfigurationImpl) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 63 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage 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 64 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class KnowledgeBaseImpl method removeKiePackage.

public void removeKiePackage(String packageName) {
    enqueueModification(() -> {
        final InternalKnowledgePackage pkg = this.pkgs.get(packageName);
        if (pkg == null) {
            throw new IllegalArgumentException("Package name '" + packageName + "' does not exist for this Rule Base.");
        }
        this.eventSupport.fireBeforePackageRemoved(pkg);
        internalRemoveRules(((Collection<RuleImpl>) (Object) pkg.getRules()));
        // getting the list of referenced globals
        final Set<String> referencedGlobals = new HashSet<String>();
        for (InternalKnowledgePackage pkgref : this.pkgs.values()) {
            if (pkgref != pkg) {
                referencedGlobals.addAll(pkgref.getGlobals().keySet());
            }
        }
        // removing globals declared inside the package that are not shared
        for (String globalName : pkg.getGlobals().keySet()) {
            if (!referencedGlobals.contains(globalName)) {
                this.globals.remove(globalName);
            }
        }
        // and now the rule flows
        for (String processName : new ArrayList<String>(pkg.getRuleFlows().keySet())) {
            internalRemoveProcess(processName);
        }
        // removing the package itself from the list
        this.pkgs.remove(pkg.getName());
        pkg.getDialectRuntimeRegistry().onRemove();
        // clear all members of the pkg
        pkg.clear();
        this.eventSupport.fireAfterPackageRemoved(pkg);
    });
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) HashSet(java.util.HashSet)

Example 65 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage 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

InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)97 Test (org.junit.Test)32 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)28 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)26 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)24 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)21 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)20 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)16 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)14 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 TypeDeclaration (org.drools.core.rule.TypeDeclaration)13 HashMap (java.util.HashMap)12 Pattern (org.drools.core.rule.Pattern)12 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)11 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)11 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)11 ClassObjectType (org.drools.core.base.ClassObjectType)10 ArrayList (java.util.ArrayList)9 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)9 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)8