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));
}
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));
}
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());
}
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);
}
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);
}
Aggregations