use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class ExtendsTest method testDeclareInheritance.
@Test
public void testDeclareInheritance() throws Exception {
String s1 = "package org.drools;\n" + "import org.drools.compiler.integrationtests.ExtendsTest.*;\n" + "\n" + "declare A \n" + " @role( event )" + " @typesafe( false )\n" + "end\n" + "" + "declare C @role( event ) @typesafe( false ) end \n" + "" + "rule R \n" + "when " + " $x : C( this.x == 1 ) \n" + "then\n" + " System.out.println( $x ); \n" + "end\n" + "";
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
if (kBuilder.hasErrors()) {
System.err.println(kBuilder.getErrors());
}
assertFalse(kBuilder.hasErrors());
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
KieSession knowledgeSession = knowledgeBase.newKieSession();
FactHandle h = knowledgeSession.insert(new X());
assertTrue(((InternalFactHandle) h).isEvent());
}
use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class ExtendsTest method testMultipleInheritanceWithPosition1.
@Test
public void testMultipleInheritanceWithPosition1() 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("field0", sw.getFields().get(0).getName());
assertEquals("field1", sw.getFields().get(1).getName());
assertEquals("mfield1", sw.getFields().get(2).getName());
assertEquals("field2", sw.getFields().get(3).getName());
assertEquals("mfield0", sw.getFields().get(4).getName());
}
use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class ExtendsTest method testExtendSelf.
@Test
public void testExtendSelf() throws Exception {
String s1 = "package org.drools;\n" + "global java.util.List list;\n" + "\n" + "declare Bean extends Bean \n" + " foo : int @key\n" + "end\n";
KieBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
assertTrue(kBuilder.hasErrors());
}
use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class ExtendsTest method testExtendOverride.
@Test
public void testExtendOverride() {
String drl = "package test.beans;\n" + "\n" + "import java.util.List;\n" + "import java.util.ArrayList;\n" + "\n" + "global List ans;" + "\n" + "\n" + "declare ArrayList\n" + "end\n" + "" + "declare Bean extends ArrayList\n" + " fld : int = 4 \n" + " myField : String = \"xxx\" \n" + "end\n" + "\n" + "declare Bean2 extends Bean\n" + " moref : double\n" + " myField : String\n" + "end\n" + "rule \"Init\"\n" + "when\n" + "then\n" + " Bean b = new Bean2();\n" + " ans.add(b);" + " System.out.println(b);\t\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = createKnowledgeSession(kbase);
List out = new ArrayList();
ksession.setGlobal("ans", out);
ksession.fireAllRules();
Object x = out.get(0);
FactType type = kbase.getFactType("test.beans", "Bean2");
assertEquals(4, type.get(x, "fld"));
assertEquals("xxx", type.get(x, "myField"));
assertEquals(0.0, type.get(x, "moref"));
assertTrue(x instanceof ArrayList);
}
use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class FireUntilHaltAccumulateTest method setUp.
@Before
public void setUp() {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.err.println(kbuilder.getErrors().toString());
}
final KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(config);
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
this.statefulSession = kbase.newKieSession(sessionConfig, null);
this.stockFactory = new StockFactory(kbase);
}
Aggregations