Search in sources :

Example 16 with ClassFieldAccessorCache

use of org.drools.core.base.ClassFieldAccessorCache in project drools by kiegroup.

the class KnowledgeBuilderImpl method createPackageRegistry.

private PackageRegistry createPackageRegistry(PackageDescr packageDescr) {
    initPackage(packageDescr);
    InternalKnowledgePackage pkg;
    if (this.kBase == null || (pkg = this.kBase.getPackage(packageDescr.getName())) == null) {
        // there is no rulebase or it does not define this package so define it
        pkg = new KnowledgePackageImpl(packageDescr.getName());
        pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(this.rootClassLoader));
        // if there is a rulebase then add the package.
        if (this.kBase != null) {
            try {
                pkg = (InternalKnowledgePackage) this.kBase.addPackage(pkg).get();
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }
        } else {
            // the RuleBase will also initialise the
            pkg.getDialectRuntimeRegistry().onAdd(this.rootClassLoader);
        }
    }
    PackageRegistry pkgRegistry = new PackageRegistry(rootClassLoader, configuration, pkg);
    // add default import for this namespace
    pkgRegistry.addImport(new ImportDescr(packageDescr.getNamespace() + ".*"));
    for (ImportDescr importDescr : packageDescr.getImports()) {
        pkgRegistry.registerImport(importDescr.getTarget());
    }
    return pkgRegistry;
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.compiler.lang.descr.AccumulateImportDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) ExecutionException(java.util.concurrent.ExecutionException) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 17 with ClassFieldAccessorCache

use of org.drools.core.base.ClassFieldAccessorCache 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 18 with ClassFieldAccessorCache

use of org.drools.core.base.ClassFieldAccessorCache in project drools by kiegroup.

the class KiePackagesBuilder method createKiePackage.

private KnowledgePackageImpl createKiePackage(String name) {
    KnowledgePackageImpl kpkg = new KnowledgePackageImpl(name);
    kpkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(configuration.getClassLoader()));
    TypeResolver typeResolver = new ClassTypeResolver(new HashSet<>(kpkg.getImports().keySet()), configuration.getClassLoader(), name);
    typeResolver.addImport(name + ".*");
    kpkg.setTypeResolver(typeResolver);
    return kpkg;
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)

Example 19 with ClassFieldAccessorCache

use of org.drools.core.base.ClassFieldAccessorCache in project drools by kiegroup.

the class RightTupleIndexHashTableIteratorTest method getConstraint.

protected BetaNodeFieldConstraint getConstraint(String identifier, Operator operator, String fieldName, Class clazz) {
    ClassFieldAccessorStore store = new ClassFieldAccessorStore();
    store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    store.setEagerWire(true);
    InternalReadAccessor extractor = store.getReader(clazz, fieldName);
    Declaration declaration = new Declaration(identifier, extractor, new Pattern(0, new ClassObjectType(clazz)));
    String expression = fieldName + " " + operator.getOperatorString() + " " + declaration.getIdentifier();
    return new MvelConstraintTestUtil(expression, declaration, extractor);
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ClassFieldAccessorStore(org.drools.core.base.ClassFieldAccessorStore) Declaration(org.drools.core.rule.Declaration) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil)

Example 20 with ClassFieldAccessorCache

use of org.drools.core.base.ClassFieldAccessorCache in project drools by kiegroup.

the class LeftLeftTupleIndexHashTableIteratorTest method getConstraint.

protected BetaNodeFieldConstraint getConstraint(String identifier, Operator operator, String fieldName, Class clazz) {
    ClassFieldAccessorStore store = new ClassFieldAccessorStore();
    store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    store.setEagerWire(true);
    InternalReadAccessor extractor = store.getReader(clazz, fieldName);
    Declaration declaration = new Declaration(identifier, extractor, new Pattern(0, new ClassObjectType(clazz)));
    String expression = fieldName + " " + operator.getOperatorString() + " " + declaration.getIdentifier();
    return new MvelConstraintTestUtil(expression, declaration, extractor);
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ClassFieldAccessorStore(org.drools.core.base.ClassFieldAccessorStore) Declaration(org.drools.core.rule.Declaration) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil)

Aggregations

ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)34 Before (org.junit.Before)22 ClassFieldAccessorStore (org.drools.core.base.ClassFieldAccessorStore)17 TestBean (org.drools.core.base.TestBean)9 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)9 ClassObjectType (org.drools.core.base.ClassObjectType)7 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)6 MvelConstraintTestUtil (org.drools.core.rule.MvelConstraintTestUtil)6 Pattern (org.drools.core.rule.Pattern)6 WorkingMemory (org.drools.core.WorkingMemory)5 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)5 Consequence (org.drools.core.spi.Consequence)5 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)5 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)4 IOException (java.io.IOException)3 ObjectInput (java.io.ObjectInput)3 ObjectOutput (java.io.ObjectOutput)3 ClassFieldReader (org.drools.core.base.ClassFieldReader)3 Declaration (org.drools.core.rule.Declaration)3 JavaDialectRuntimeData (org.drools.core.rule.JavaDialectRuntimeData)3