use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class LinkingTest method testExistsNodes1.
@Test
public void testExistsNodes1() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "import " + F.class.getCanonicalName() + "\n";
str += "import " + G.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " exists A() \n";
str += "then \n";
str += " list.add( 'x' ); \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession wm = kbase.newKieSession();
List list = new ArrayList();
wm.setGlobal("list", list);
wm.fireAllRules();
assertEquals(0, list.size());
wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
list = new ArrayList();
wm.setGlobal("list", list);
FactHandle fh = wm.insert(new A(1));
wm.fireAllRules();
assertEquals(1, list.size());
wm.retract(fh);
wm.fireAllRules();
assertEquals(1, list.size());
}
use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class LinkingTest method testAccumulateNodes2.
@Test
public void testAccumulateNodes2() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "import " + F.class.getCanonicalName() + "\n";
str += "import " + G.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " A() \n";
str += " accumulate( $a : B(); $l : collectList( $a ) ) \n";
str += " C() \n";
str += "then \n";
str += " list.add( $l.size() ); \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession wm = kbase.newKieSession();
List list = new ArrayList();
wm.setGlobal("list", list);
wm.fireAllRules();
assertEquals(0, list.size());
wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
list = new ArrayList();
wm.setGlobal("list", list);
FactHandle fh1 = wm.insert(new B(1));
FactHandle fh2 = wm.insert(new B(2));
FactHandle fh3 = wm.insert(new B(3));
FactHandle fh4 = wm.insert(new B(4));
FactHandle fha = wm.insert(new A(1));
FactHandle fhc = wm.insert(new C(1));
wm.fireAllRules();
assertEquals(4, list.get(0));
}
use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class LinkingTest method testSubNetworkSharing.
@Test
public void testSubNetworkSharing() throws Exception {
// Checks the network is correctly formed, with sharing
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " A() \n";
str += " B() \n";
str += " C() \n";
str += " D() \n";
str += "then \n";
str += "end \n";
str += "rule rule2 when \n";
str += " A() \n";
str += " exists( B() and C() ) \n";
str += " D() \n";
str += "then \n";
str += "end \n";
str += "rule rule3 when \n";
str += " A() \n";
str += " exists( B() and C() and D() ) \n";
str += " E() \n";
str += "then \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
assertEquals(3, liaNode.getSinkPropagator().size());
ExistsNode existsNode2 = (ExistsNode) liaNode.getSinkPropagator().getSinks()[1];
ExistsNode existsNode3 = (ExistsNode) liaNode.getSinkPropagator().getSinks()[2];
JoinNode joinNodeB = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
assertSame(joinNodeB.getRightInput(), getObjectTypeNode(kbase, B.class));
JoinNode joinNodeC = (JoinNode) joinNodeB.getSinkPropagator().getSinks()[0];
assertSame(joinNodeC.getRightInput(), getObjectTypeNode(kbase, C.class));
assertEquals(2, joinNodeC.getSinkPropagator().size());
JoinNode joinNodeD = (JoinNode) joinNodeC.getSinkPropagator().getSinks()[0];
assertSame(joinNodeD.getRightInput(), getObjectTypeNode(kbase, D.class));
assertEquals(2, joinNodeD.getSinkPropagator().size());
assertSame(existsNode2, ((RightInputAdapterNode) joinNodeC.getSinkPropagator().getSinks()[1]).getObjectSinkPropagator().getSinks()[0]);
assertSame(existsNode3, ((RightInputAdapterNode) joinNodeD.getSinkPropagator().getSinks()[1]).getObjectSinkPropagator().getSinks()[0]);
}
use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class MVELTest method testNestedEnum.
@Test
public void testNestedEnum() {
final String str = "" + "package org.drools.compiler.test \n" + "import " + Triangle.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $t: Triangle(t == Triangle.Type.ACUTE) \n" + "then \n" + " list.add($t.getT()); \n" + "end \n";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession ksession = createKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
final Triangle t = new Triangle(Triangle.Type.ACUTE);
ksession.insert(t);
ksession.fireAllRules();
assertEquals(Triangle.Type.ACUTE, list.get(0));
}
use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class MVELTest method testSizeCheckInObject.
@Test
public void testSizeCheckInObject() {
final String str = "" + "package org.drools.compiler.test \n" + "import " + Triangle.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $m : Triangle( deliveries.size == 0) \n" + "then \n" + " list.add('r1'); \n" + "end \n";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
}
Aggregations