Search in sources :

Example 76 with InternalKnowledgePackage

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

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

the class FactTemplateTest method testFacts.

@Test
public void testFacts() {
    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[] fields1 = new FieldTemplate[] { cheeseName, cheesePrice };
    final FactTemplate cheese1 = new FactTemplateImpl(pkg, "Cheese", fields1);
    final Fact stilton1 = cheese1.createFact(10);
    stilton1.setFieldValue("name", "stilton");
    stilton1.setFieldValue("price", new Integer(200));
    final Fact stilton2 = cheese1.createFact(11);
    stilton2.setFieldValue(0, "stilton");
    stilton2.setFieldValue(1, new Integer(200));
    assertEquals(stilton1, stilton2);
    assertEquals(stilton1.hashCode(), stilton2.hashCode());
    final Fact brie = cheese1.createFact(12);
    brie.setFieldValue("name", "brie");
    brie.setFieldValue("price", new Integer(55));
    assertFalse(stilton1.equals(brie));
    assertFalse(stilton1.hashCode() == brie.hashCode());
}
Also used : KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 78 with InternalKnowledgePackage

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

the class FactTemplateTest method testFieldsAndGetters.

@Test
public void testFieldsAndGetters() {
    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);
    assertEquals("org.store", cheese.getPackage().getName());
    assertEquals("Cheese", cheese.getName());
    assertEquals(2, cheese.getNumberOfFields());
    assertSame(fields, cheese.getAllFieldTemplates());
    assertSame(cheeseName, cheese.getFieldTemplate(0));
    assertSame(cheesePrice, cheese.getFieldTemplate(1));
    assertSame(cheeseName, cheese.getFieldTemplate("name"));
    assertSame(cheesePrice, cheese.getFieldTemplate("price"));
    assertEquals(0, cheese.getFieldTemplateIndex("name"));
    assertEquals(1, cheese.getFieldTemplateIndex("price"));
}
Also used : KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 79 with InternalKnowledgePackage

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

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

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