use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class LinkingTest method testNotNodes1.
@Test
public void testNotNodes1() 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 += " not 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());
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
wm.fireAllRules();
assertEquals(1, list.size());
wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
list = new ArrayList();
wm.setGlobal("list", list);
FactHandle fh = wm.insert(new A(1));
wm.fireAllRules();
assertEquals(0, list.size());
wm.retract(fh);
wm.fireAllRules();
assertEquals(1, list.size());
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class LinkingTest method testExistsNodes2.
@Test
public void testExistsNodes2() 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 : A() \n";
str += " exists B() \n";
str += " $c : C() \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);
for (int i = 0; i < 3; i++) {
wm.insert(new A(i));
}
for (int i = 0; i < 3; i++) {
wm.insert(new C(i));
}
wm.fireAllRules();
assertEquals(0, list.size());
wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
list = new ArrayList();
wm.setGlobal("list", list);
for (int i = 0; i < 3; i++) {
wm.insert(new A(i));
}
FactHandle fh = wm.insert(new B(1));
for (int i = 0; i < 3; i++) {
wm.insert(new C(i));
}
wm.fireAllRules();
assertEquals(9, list.size());
wm.retract(fh);
wm.fireAllRules();
assertEquals(9, list.size());
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl 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.drools.core.impl.StatefulKnowledgeSessionImpl 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.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class CepEspTest method testIdleTime.
// @Test(timeout=10000) @Ignore
// public void testTransactionCorrelation() throws Exception {
// // read in the source
// final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_TransactionCorrelation.drl" ) );
// final RuleBase ruleBase = loadRuleBase( reader );
//
// final WorkingMemory wm = ruleBase.newStatefulSession();
// final List results = new ArrayList();
//
// wm.setGlobal( "results",
// results );
//
//
// }
@Test(timeout = 10000)
public void testIdleTime() throws Exception {
// read in the source
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_CEP_SimpleEventAssertion.drl")), ResourceType.DRL);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) createKnowledgeSession(kbase, conf);
SessionPseudoClock clock = (SessionPseudoClock) session.getSessionClock();
final List results = new ArrayList();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, 10100);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, 11000);
assertEquals(0, session.getIdleTime());
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
clock.advanceTime(10, TimeUnit.SECONDS);
assertEquals(10000, session.getIdleTime());
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
assertEquals(0, session.getIdleTime());
clock.advanceTime(15, TimeUnit.SECONDS);
assertEquals(15000, session.getIdleTime());
clock.advanceTime(15, TimeUnit.SECONDS);
assertEquals(30000, session.getIdleTime());
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
assertEquals(0, session.getIdleTime());
clock.advanceTime(20, TimeUnit.SECONDS);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
clock.advanceTime(10, TimeUnit.SECONDS);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
assertEquals(10000, session.getIdleTime());
session.fireAllRules();
assertEquals(0, session.getIdleTime());
assertEquals(2, ((List) session.getGlobal("results")).size());
}
Aggregations