use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class SegmentMemoryPrototypeTest method checkKieSession.
private void checkKieSession(KieSession ksession) {
final List<String> events = new ArrayList<String>();
ksession.setGlobal("events", events);
// phase 1
Room room1 = new Room("Room 1");
ksession.insert(room1);
FactHandle fireFact1 = ksession.insert(new Fire(room1));
ksession.fireAllRules();
assertEquals(1, events.size());
// phase 2
Sprinkler sprinkler1 = new Sprinkler(room1);
ksession.insert(sprinkler1);
ksession.fireAllRules();
assertEquals(2, events.size());
// phase 3
ksession.delete(fireFact1);
ksession.fireAllRules();
assertEquals(5, events.size());
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class TimerAndCalendarTest method testHaltWithTimer.
@Test(timeout = 10000)
public void testHaltWithTimer() throws Exception {
KieBase kbase = loadKnowledgeBase("test_Halt_With_Timer.drl");
final KieSession ksession = createKnowledgeSession(kbase);
new Thread(new Runnable() {
public void run() {
ksession.fireUntilHalt();
}
}).start();
Thread.sleep(1000);
FactHandle handle = (FactHandle) ksession.insert("halt");
Thread.sleep(2000);
// now check that rule "halt" fired once, creating one Integer
assertEquals(2, ksession.getFactCount());
ksession.retract(handle);
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class TimerAndCalendarTest method testExprIntervalTimerRaceCondition.
@Test(timeout = 10000)
public void testExprIntervalTimerRaceCondition() throws Exception {
String str = "";
str += "package org.simple \n";
str += "global java.util.List list \n";
str += "rule xxx \n";
str += " timer (expr: $i, $i) \n";
str += "when \n";
str += " $i : Long() \n";
str += "then \n";
str += " list.add(\"fired\"); \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
List list = new ArrayList();
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
ksession.setGlobal("list", list);
FactHandle fh = (FactHandle) ksession.insert(10000l);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(17, TimeUnit.SECONDS);
ksession.update(fh, 5000l);
ksession.fireAllRules();
assertEquals(2, list.size());
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class TimerAndCalendarTest method testFireRuleAfterDuration.
@Test(timeout = 10000)
public void testFireRuleAfterDuration() throws Exception {
KieBase kbase = loadKnowledgeBase("test_FireRuleAfterDuration.drl");
KieSession ksession = createKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
final Cheese brie = new Cheese("brie", 12);
final FactHandle brieHandle = (FactHandle) ksession.insert(brie);
ksession.fireAllRules();
// now check for update
assertEquals(0, list.size());
// sleep for 300ms
Thread.sleep(300);
ksession.fireAllRules();
// now check for update
assertEquals(2, list.size());
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class TruthMaintenanceTest method testStatedShadowLogicalThenLogicalOnly.
@Test(timeout = 10000)
public void testStatedShadowLogicalThenLogicalOnly() {
String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + " String( this == 'go1' ) " + "then \n" + " insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + " String( this == 'go2' ) " + " s : String( this == 'f1' ) " + "then \n" + " list.add( s ); \n" + "end \n" + "";
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
FactHandle g1 = session.insert("go1");
session.fireAllRules();
// This removes the stated position, but it should still be logical now and exist
session.delete(fh1, FactHandle.State.STATED);
session.insert("go2");
session.fireAllRules();
// fh1 is invalid and no longer in the WM. However it's now reverted to it's Justified version and will still be there
assertFalse(fh1.isValid());
// Make sure f1 is still there, but logical only now
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
InternalFactHandle jfh1 = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem().get("f1").getLogicalFactHandle();
assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
assertSame(jfh1, session.getFactHandle("f1"));
}
Aggregations