use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class KieContainerTest method insertMessageFromTypeDeclaration.
private void insertMessageFromTypeDeclaration(KieSession ksession) throws InstantiationException, IllegalAccessException {
FactType messageType = ksession.getKieBase().getFactType("org.drools.test", "Message");
Object message = messageType.newInstance();
messageType.set(message, "message", "Hello World");
ksession.insert(message);
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class ExtendsTest method testExpressionFieldInit.
@Test
public void testExpressionFieldInit() throws Exception {
KieSession ksession = genSession("test_ExtFieldInit.drl");
FactType test = ksession.getKieBase().getFactType("org.drools.compiler", "MyBoxExpressionBean");
Object x = test.newInstance();
assertNotNull(x);
assertEquals("foobar", test.get(x, "f0"));
assertEquals(-32, test.get(x, "fieldint"));
assertEquals(4.0, test.get(x, "fielddbl"));
assertEquals(0.5f, test.get(x, "fieldflt"));
short sht = 6;
assertEquals(sht, test.get(x, "fieldsht"));
byte byt = 2;
assertEquals(byt, test.get(x, "fieldbyt"));
assertEquals(true, test.get(x, "fieldbln"));
assertEquals('x', test.get(x, "fieldchr"));
assertEquals(9999L, test.get(x, "fieldlng"));
System.out.println(x);
FactType test2 = ksession.getKieBase().getFactType("org.drools.compiler", "MySimpleExpressionBean");
x = test2.newInstance();
assertNotNull(x);
assertEquals("foobar", test2.get(x, "f0"));
assertEquals(-32, test2.get(x, "fieldint"));
assertEquals(4.0, test2.get(x, "fielddbl"));
assertEquals(0.5f, test2.get(x, "fieldflt"));
sht = 6;
assertEquals(sht, test2.get(x, "fieldsht"));
byt = 2;
assertEquals(byt, test2.get(x, "fieldbyt"));
assertEquals(true, test2.get(x, "fieldbln"));
assertEquals('x', test2.get(x, "fieldchr"));
assertEquals(9999L, test2.get(x, "fieldlng"));
System.out.println(x);
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class ExtendsTest method testMultipleInheritanceWithPosition6.
@Test
public void testMultipleInheritanceWithPosition6() throws Exception {
// DROOLS-249
String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait SecPosTrait\n" + "@propertyReactive\n" + " field3 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait, SecPosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 //@position(0)\n" + " mfield1 : int = 201 //@position(2)\n" + "end";
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kBuilder.hasErrors()) {
System.err.println(kBuilder.getErrors());
}
assertFalse(kBuilder.hasErrors());
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
assertEquals(6, sw.getFields().size());
assertEquals("field0", sw.getFields().get(0).getName());
assertEquals("field1", sw.getFields().get(1).getName());
assertEquals("field2", sw.getFields().get(2).getName());
assertEquals("field3", sw.getFields().get(3).getName());
assertEquals("mfield0", sw.getFields().get(4).getName());
assertEquals("mfield1", sw.getFields().get(5).getName());
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class ExtendsTest method testExtendsDump.
@Test
public void testExtendsDump() {
PackageDescrBuilder pkgd = DescrFactory.newPackage();
pkgd.name("org.test").newDeclare().type().name("Foo").newField("id").type("int").end().end().newDeclare().type().name("Bar").superType("Foo").newField("val").type("int").initialValue("42").end().end();
String drl = new DrlDumper().dump(pkgd.getDescr());
KieBase kb = loadKnowledgeBaseFromString(drl);
FactType bar = kb.getFactType("org.test", "Bar");
try {
Object x = bar.newInstance();
assertEquals(42, bar.get(x, "val"));
bar.set(x, "id", 1);
assertEquals(1, bar.get(x, "id"));
} catch (Exception e) {
fail(e.getMessage());
}
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class ExtendsTest method testMultipleInheritanceWithPosition2.
@Test
public void testMultipleInheritanceWithPosition2() throws Exception {
// DROOLS-249
String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end";
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kBuilder.hasErrors()) {
System.err.println(kBuilder.getErrors());
}
assertFalse(kBuilder.hasErrors());
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
assertEquals(5, sw.getFields().size());
assertEquals("mfield0", sw.getFields().get(0).getName());
assertEquals("field0", sw.getFields().get(1).getName());
assertEquals("mfield1", sw.getFields().get(2).getName());
assertEquals("field1", sw.getFields().get(3).getName());
assertEquals("field2", sw.getFields().get(4).getName());
}
Aggregations