Search in sources :

Example 31 with KnowledgePackageImpl

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

the class FactTemplateFieldExtractorTest method testDeclaration.

@Test
public void testDeclaration() {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.store");
    final FieldTemplate cheeseName = new FieldTemplateImpl("name", 0, String.class);
    final FieldTemplate cheesePrice = new FieldTemplateImpl("price", 1, Integer.class);
    final FieldTemplate[] fields = new FieldTemplate[] { cheeseName, cheesePrice };
    final FactTemplate cheese = new FactTemplateImpl(pkg, "Cheese", fields);
    final InternalReadAccessor extractName = new FactTemplateFieldExtractor(cheese, 0);
    final Pattern pattern = new Pattern(0, new FactTemplateObjectType(cheese));
    final Declaration declaration = new Declaration("typeOfCheese", extractName, pattern);
    final Fact brie = cheese.createFact(12);
    brie.setFieldValue("name", "brie");
    brie.setFieldValue("price", new Integer(55));
    // Check we can extract Declarations correctly
    assertEquals("brie", declaration.getValue(null, brie));
}
Also used : Pattern(org.drools.core.rule.Pattern) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 32 with KnowledgePackageImpl

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

the class FactTemplateFieldExtractorTest method testExtractor.

@Test
public void testExtractor() {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.store");
    final FieldTemplate cheeseName = new FieldTemplateImpl("name", 0, String.class);
    final FieldTemplate cheesePrice = new FieldTemplateImpl("price", 1, Integer.class);
    final FieldTemplate[] fields = new FieldTemplate[] { cheeseName, cheesePrice };
    final FactTemplate cheese = new FactTemplateImpl(pkg, "Cheese", fields);
    final InternalReadAccessor extractName = new FactTemplateFieldExtractor(cheese, 0);
    final InternalReadAccessor extractPrice = new FactTemplateFieldExtractor(cheese, 1);
    final Fact stilton = cheese.createFact(10);
    stilton.setFieldValue("name", "stilton");
    stilton.setFieldValue("price", new Integer(200));
    assertEquals("stilton", extractName.getValue(null, stilton));
    assertEquals(new Integer(200), extractPrice.getValue(null, stilton));
    assertFalse(extractName.isNullValue(null, stilton));
    stilton.setFieldValue("name", null);
    assertTrue(extractName.isNullValue(null, stilton));
    assertFalse(extractPrice.isNullValue(null, stilton));
    final Fact brie = cheese.createFact(12);
    brie.setFieldValue("name", "brie");
    brie.setFieldValue("price", new Integer(55));
    assertEquals("brie", extractName.getValue(null, brie));
    assertEquals(new Integer(55), extractPrice.getValue(null, brie));
    assertFalse(extractName.isNullValue(null, brie));
    brie.setFieldValue("name", null);
    assertTrue(extractName.isNullValue(null, brie));
    assertFalse(extractPrice.isNullValue(null, stilton));
}
Also used : InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 33 with KnowledgePackageImpl

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

the class KnowledgeBaseImplTest method testStaticImports.

@Test
public void testStaticImports() {
    KnowledgeBaseImpl base = new KnowledgeBaseImpl("default", null);
    // assume empty knowledge base
    assertTrue(base.getPackages().length == 0);
    // add package with function static import into knowledge base
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.test");
    pkg.addStaticImport("org.drools.function.myFunction");
    base.addPackage(pkg);
    // verify package has been added
    assertTrue(base.getPackages().length == 1);
    // retrieve copied and merged package from the base
    InternalKnowledgePackage copy = base.getPackage("org.drools.test");
    assertEquals(Collections.singleton("org.drools.function.myFunction"), copy.getStaticImports());
}
Also used : KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 34 with KnowledgePackageImpl

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

the class ReteooRuleBaseMultiThreadedTest method setUp.

@Before
public void setUp() {
    this.kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    pkg = new KnowledgePackageImpl("org.droos.test");
    pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    JavaDialectRuntimeData data = new JavaDialectRuntimeData();
    data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
    pkg.getDialectRuntimeRegistry().setDialectData("java", data);
    // we need to add one rule to the package because the previous deadlock was encountered
    // while removing rules from a package when said package is removed from the rulebase
    rule = new RuleImpl("Test");
    rule.setDialect("java");
    rule.setConsequence(new Consequence() {

        public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
        }

        public String getName() {
            return "default";
        }
    });
    pkg.addRule(rule);
    kBase.addPackage(pkg);
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) WorkingMemory(org.drools.core.WorkingMemory) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) Before(org.junit.Before)

Example 35 with KnowledgePackageImpl

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

the class DroolsObjectIOTest method testStreaming.

@Test
public void testStreaming() throws Exception {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("test");
    byte[] buf = marshal(pkg);
    assertEquals(unmarshal(buf), pkg);
    buf = serialize(pkg);
    assertEquals(deserialize(buf), pkg);
}
Also used : KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

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