Search in sources :

Example 31 with FactHandle

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());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) Fire(org.drools.compiler.integrationtests.DynamicRulesChangesTest.Fire) ArrayList(java.util.ArrayList) Room(org.drools.compiler.integrationtests.DynamicRulesChangesTest.Room) Sprinkler(org.drools.compiler.integrationtests.DynamicRulesChangesTest.Sprinkler)

Example 32 with FactHandle

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);
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 33 with FactHandle

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());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) Test(org.junit.Test)

Example 34 with FactHandle

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());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 35 with FactHandle

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"));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Aggregations

FactHandle (org.kie.api.runtime.rule.FactHandle)650 Test (org.junit.Test)547 KieSession (org.kie.api.runtime.KieSession)506 KieBase (org.kie.api.KieBase)304 ArrayList (java.util.ArrayList)298 InternalFactHandle (org.drools.core.common.InternalFactHandle)192 List (java.util.List)165 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)119 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)66 KieHelper (org.kie.internal.utils.KieHelper)50 Person (org.drools.modelcompiler.domain.Person)46 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)43 HashMap (java.util.HashMap)41 Person (org.drools.compiler.Person)41 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)39 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)39 Person (org.drools.mvel.compiler.Person)38 Ignore (org.junit.Ignore)36 Match (org.kie.api.runtime.rule.Match)36 Collection (java.util.Collection)34