Search in sources :

Example 16 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class FactTemplateTest method testEqualsAndHashCode.

@Test
public void testEqualsAndHashCode() {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.store");
    // Create cheese1 with name and price fields
    final FieldTemplate cheeseName = new FieldTemplateImpl("name", 0, String.class);
    final FieldTemplate cheesePrice = new FieldTemplateImpl("price", 1, Integer.class);
    final FieldTemplate[] fields1 = new FieldTemplate[] { cheeseName, cheesePrice };
    final FactTemplate cheese1 = new FactTemplateImpl(pkg, "Cheese", fields1);
    // Create cheese2 with type and price fields
    final FieldTemplate cheeseType = new FieldTemplateImpl("type", 0, String.class);
    final FieldTemplate[] fields2 = new FieldTemplate[] { cheeseType, cheesePrice };
    final FactTemplate cheese2 = new FactTemplateImpl(pkg, "Cheese", fields2);
    assertNotSame(cheese1, cheese2);
    assertFalse(cheese1.equals(cheese2));
    assertFalse(cheese1.hashCode() == cheese2.hashCode());
    // create cheese3 with name and price fields, using new instances
    final FieldTemplate cheeseName2 = new FieldTemplateImpl("name", 0, String.class);
    final FieldTemplate cheesePrice2 = new FieldTemplateImpl("price", 1, Integer.class);
    final FieldTemplate[] fields3 = new FieldTemplate[] { cheeseName2, cheesePrice2 };
    final FactTemplate cheese3 = new FactTemplateImpl(pkg, "Cheese", fields3);
    assertNotSame(cheese1, cheese3);
    assertNotSame(cheese1.getAllFieldTemplates(), cheese3.getAllFieldTemplates());
    assertEquals(cheese1, cheese3);
    assertEquals(cheese1.hashCode(), cheese3.hashCode());
}
Also used : KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 17 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class CrossProductTest method setUp.

@Before
public void setUp() throws Exception {
    final ObjectType list1ObjectType = new ClassObjectType(String.class);
    final ObjectType list2ObjectType = new ClassObjectType(String.class);
    final RuleImpl rule = new RuleImpl("rule-1");
    final Pattern list1Pattern = new Pattern(0, list1ObjectType, "s1");
    final Pattern list2Pattern = new Pattern(1, list2ObjectType, "s2");
    rule.addPattern(list1Pattern);
    rule.addPattern(list2Pattern);
    final Declaration s1Declaration = rule.getDeclaration("s1");
    final Declaration s2Declaration = rule.getDeclaration("s2");
    this.values = new ArrayList();
    rule.setConsequence(new Consequence() {

        private static final long serialVersionUID = 510l;

        public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {
            final String s1 = (String) knowledgeHelper.get(s1Declaration);
            final String s2 = (String) knowledgeHelper.get(s2Declaration);
            CrossProductTest.this.values.add(new String[] { s1, s2 });
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    });
    this.pkg = new KnowledgePackageImpl("org.drools");
    this.pkg.addRule(rule);
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) ArrayList(java.util.ArrayList) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IOException(java.io.IOException) IOException(java.io.IOException) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) ObjectInput(java.io.ObjectInput) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Before(org.junit.Before)

Example 18 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class PackageCompilationDataTest method testCodeSourceUrl.

@Test
public void testCodeSourceUrl() throws Exception {
    final String className = TestEvalExpression.class.getName();
    KnowledgeBaseImpl kBase = new KnowledgeBaseImpl("xxx", null);
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
    pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    JavaDialectRuntimeData data = new JavaDialectRuntimeData();
    data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
    pkg.getDialectRuntimeRegistry().setDialectData("java", data);
    kBase.addPackage(pkg);
    final JavaDialectRuntimeData pcData = (JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java");
    final EvalCondition invoker = new EvalCondition(null);
    pcData.putInvoker(className, invoker);
    final InputStream is = getClass().getClassLoader().getResourceAsStream(className.replace('.', '/') + ".class");
    try {
        pcData.write(className.replace('.', '/') + ".class", read(is));
    } finally {
        is.close();
    }
    pcData.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
    pcData.onBeforeExecute();
    Class cls = kBase.getRootClassLoader().loadClass("org.drools.core.rule.PackageCompilationDataTest$TestEvalExpression");
    final CodeSource codeSource = invoker.getEvalExpression().getClass().getProtectionDomain().getCodeSource();
    assertNotNull(codeSource.getLocation());
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) InputStream(java.io.InputStream) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) CodeSource(java.security.CodeSource) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 19 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class KiePackagesBuilder method getClassObjectType.

private ObjectType getClassObjectType(Class<?> patternClass) {
    return objectTypeCache.computeIfAbsent(patternClass.getCanonicalName(), name -> {
        boolean isEvent = false;
        if (!name.startsWith("java.") && !patternClass.isPrimitive()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(patternClass.getPackage().getName(), this::createKiePackage);
            TypeDeclaration typeDeclaration = pkg.getTypeDeclaration(patternClass);
            if (typeDeclaration == null) {
                typeDeclaration = createTypeDeclaration(patternClass);
                pkg.addTypeDeclaration(typeDeclaration);
            }
            isEvent = typeDeclaration.getRole() == Role.Type.EVENT;
        }
        return new ClassObjectType(patternClass, isEvent);
    });
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 20 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl 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)

Aggregations

KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)41 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)26 Test (org.junit.Test)19 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)18 ClassObjectType (org.drools.core.base.ClassObjectType)13 Pattern (org.drools.core.rule.Pattern)11 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)9 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)9 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)8 WorkingMemory (org.drools.core.WorkingMemory)7 Consequence (org.drools.core.spi.Consequence)7 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)7 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)6 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)6 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)6 Declaration (org.drools.core.rule.Declaration)6 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)6 IOException (java.io.IOException)5 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)5 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)5