Search in sources :

Example 76 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class TimerAndCalendarTest method testDurationMemoryLeakonRepeatedUpdate.

@Test(timeout = 10000)
public void testDurationMemoryLeakonRepeatedUpdate() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test\n";
    str += "import org.drools.compiler.Alarm\n";
    str += "global java.util.List list;";
    str += "rule \"COMPTEUR\"\n";
    str += "  timer (int: 50s)\n";
    str += "  when\n";
    str += "    $alarm : Alarm( number < 5 )\n";
    str += "  then\n";
    str += "    $alarm.incrementNumber();\n";
    str += "    list.add( $alarm );\n";
    str += "    update($alarm);\n";
    str += "end\n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = createKnowledgeSession(kbase, conf);
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new Alarm());
    ksession.fireAllRules();
    for (int i = 0; i < 6; i++) {
        timeService.advanceTime(55, TimeUnit.SECONDS);
        ksession.fireAllRules();
    }
    assertEquals(5, list.size());
}
Also used : KieBase(org.kie.api.KieBase) Alarm(org.drools.compiler.Alarm) 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) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 77 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class TimerAndCalendarTest method testNoProtocolIntervalTimer.

@Test(timeout = 10000)
public void testNoProtocolIntervalTimer() throws Exception {
    String str = "";
    str += "package org.simple \n";
    str += "global java.util.List list \n";
    str += "rule xxx \n";
    str += "  duration (30s 10s) ";
    str += "when \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);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(20, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(15, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(3, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(2, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    timeService.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(3, list.size());
}
Also used : 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 78 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class TimerAndCalendarTest method testCalendarsWithIntervalsAndStartAndEnd.

@Test(timeout = 10000)
public void testCalendarsWithIntervalsAndStartAndEnd() throws Exception {
    String str = "";
    str += "package org.simple \n";
    str += "global java.util.List list \n";
    str += "rule xxx \n";
    str += "  calendars \"cal1\"\n";
    // int: protocol is assumed
    str += "  timer (0d 1d; start=3-JAN-2010, end=5-JAN-2010) ";
    str += "when \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();
    DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK);
    Date date = df.parse("1-JAN-2010");
    Calendar cal1 = new Calendar() {

        public boolean isTimeIncluded(long timestamp) {
            return true;
        }
    };
    long oneDay = 60 * 60 * 24;
    ksession.getCalendars().set("cal1", cal1);
    ksession.setGlobal("list", list);
    timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(oneDay, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    // day 3
    timeService.advanceTime(oneDay, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(oneDay, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    // day 5
    timeService.advanceTime(oneDay, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    timeService.advanceTime(oneDay, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(3, list.size());
}
Also used : KieBase(org.kie.api.KieBase) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Calendar(org.kie.api.time.Calendar) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) Test(org.junit.Test)

Example 79 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class RuleTest method testTimeMachine.

@Test
public void testTimeMachine() {
    SessionConfiguration conf = SessionConfiguration.newInstance();
    conf.setClockType(ClockType.PSEUDO_CLOCK);
    WorkingMemory wm = (WorkingMemory) new KnowledgeBaseImpl("x", null).newKieSession(conf, null);
    final Calendar future = Calendar.getInstance();
    ((PseudoClockScheduler) wm.getSessionClock()).setStartupTime(future.getTimeInMillis());
    final RuleImpl rule = new RuleImpl("myrule");
    rule.setEnabled(EnabledBoolean.ENABLED_TRUE);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
    future.setTimeInMillis(future.getTimeInMillis() + 100000000);
    rule.setDateEffective(future);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
    ((PseudoClockScheduler) wm.getSessionClock()).advanceTime(1000000000000L, TimeUnit.MILLISECONDS);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) Calendar(java.util.Calendar) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) SessionConfiguration(org.drools.core.SessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 80 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class ProtobufOutputMarshaller method serializeSession.

private static ProtobufMessages.KnowledgeSession serializeSession(MarshallerWriteContext context) throws IOException {
    StatefulKnowledgeSessionImpl wm = (StatefulKnowledgeSessionImpl) context.wm;
    try {
        wm.getLock().lock();
        for (WorkingMemoryEntryPoint ep : wm.getWorkingMemoryEntryPoints().values()) {
            if (ep instanceof NamedEntryPoint) {
                ((NamedEntryPoint) ep).lock();
            }
        }
        evaluateRuleActivations(wm);
        ProtobufMessages.RuleData.Builder _ruleData = ProtobufMessages.RuleData.newBuilder();
        long time = 0;
        if (context.wm.getTimerService() instanceof PseudoClockScheduler) {
            time = context.clockTime;
        }
        _ruleData.setLastId(wm.getFactHandleFactory().getId());
        _ruleData.setLastRecency(wm.getFactHandleFactory().getRecency());
        InternalFactHandle handle = context.wm.getInitialFactHandle();
        if (handle != null) {
            // can be null for RETE, if fireAllRules has not yet been called
            ProtobufMessages.FactHandle _ifh = ProtobufMessages.FactHandle.newBuilder().setType(ProtobufMessages.FactHandle.HandleType.INITIAL_FACT).setId(handle.getId()).setRecency(handle.getRecency()).build();
            _ruleData.setInitialFact(_ifh);
        }
        writeAgenda(context, _ruleData);
        writeNodeMemories(context, _ruleData);
        for (EntryPoint wmep : wm.getWorkingMemoryEntryPoints().values()) {
            org.drools.core.marshalling.impl.ProtobufMessages.EntryPoint.Builder _epb = ProtobufMessages.EntryPoint.newBuilder();
            _epb.setEntryPointId(wmep.getEntryPointId());
            writeObjectTypeConfiguration(context, ((WorkingMemoryEntryPoint) wmep).getObjectTypeConfigurationRegistry(), _epb);
            writeFactHandles(context, _epb, ((NamedEntryPoint) wmep).getObjectStore());
            writeTruthMaintenanceSystem(context, wmep, _epb);
            _ruleData.addEntryPoint(_epb.build());
        }
        writeActionQueue(context, _ruleData);
        ProtobufMessages.KnowledgeSession.Builder _session = ProtobufMessages.KnowledgeSession.newBuilder().setMultithread(false).setTime(time).setRuleData(_ruleData.build());
        if (processMarshaller != null) {
            Builder _pdata = ProtobufMessages.ProcessData.newBuilder();
            if (context.marshalProcessInstances) {
                context.parameterObject = _pdata;
                processMarshaller.writeProcessInstances(context);
            }
            if (context.marshalWorkItems) {
                context.parameterObject = _pdata;
                processMarshaller.writeWorkItems(context);
            }
            // this now just assigns the writer, it will not write out any timer information
            context.parameterObject = _pdata;
            processMarshaller.writeProcessTimers(context);
            _session.setProcessData(_pdata.build());
        }
        Timers _timers = writeTimers(context.wm.getTimerService().getTimerJobInstances(context.wm.getIdentifier()), context);
        if (_timers != null) {
            _session.setTimers(_timers);
        }
        return _session.build();
    } finally {
        for (WorkingMemoryEntryPoint ep : wm.getWorkingMemoryEntryPoints().values()) {
            if (ep instanceof NamedEntryPoint) {
                ((NamedEntryPoint) ep).unlock();
            }
        }
        wm.getLock().unlock();
    }
}
Also used : NamedEntryPoint(org.drools.core.common.NamedEntryPoint) Builder(org.drools.core.marshalling.impl.ProtobufMessages.ProcessData.Builder) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) Timers(org.drools.core.marshalling.impl.ProtobufMessages.Timers)

Aggregations

PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)81 KieSession (org.kie.api.runtime.KieSession)76 Test (org.junit.Test)75 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)72 KieBase (org.kie.api.KieBase)60 ArrayList (java.util.ArrayList)39 KieHelper (org.kie.internal.utils.KieHelper)39 List (java.util.List)29 Date (java.util.Date)19 Arrays.asList (java.util.Arrays.asList)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)14 StockTick (org.drools.compiler.StockTick)13 DateFormat (java.text.DateFormat)9 SimpleDateFormat (java.text.SimpleDateFormat)9 Calendar (org.kie.api.time.Calendar)8 StockTickInterface (org.drools.compiler.StockTickInterface)7 EntryPoint (org.kie.api.runtime.rule.EntryPoint)7 IOException (java.io.IOException)5 FactA (org.drools.compiler.FactA)5