Search in sources :

Example 1 with PseudoClockScheduler

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

the class ScenarioRunnerTest method testTestingEventListener.

@Test
public void testTestingEventListener() throws Exception {
    Scenario sc = new Scenario();
    sc.getRules().add("foo");
    sc.getRules().add("bar");
    ExecutionTrace ext = new ExecutionTrace();
    sc.getFixtures().add(ext);
    PseudoClockScheduler clock = new PseudoClockScheduler();
    long time = new Date().getTime();
    clock.setStartupTime(time);
    when(ksession.getSessionClock()).thenReturn(clock);
    ScenarioRunner run = new ScenarioRunner(ksession);
    run.run(sc);
    verify(ksession).addEventListener(any(TestingEventListener.class));
    assertEquals(2, sc.getRules().size());
    assertTrue(sc.getRules().contains("foo"));
    assertTrue(sc.getRules().contains("bar"));
}
Also used : ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) Test(org.junit.Test)

Example 2 with PseudoClockScheduler

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

the class ScenarioRunnerTest method testSimulatedDate.

@SuppressWarnings("deprecation")
@Test
public void testSimulatedDate() throws Exception {
    Scenario sc = new Scenario();
    PseudoClockScheduler clock = new PseudoClockScheduler();
    long time = new Date().getTime();
    clock.setStartupTime(time);
    when(ksession.getSessionClock()).thenReturn(clock);
    ScenarioRunner run = new ScenarioRunner(ksession);
    run.run(sc);
    assertEquals(time, ksession.getSessionClock().getCurrentTime());
    ExecutionTrace ext = new ExecutionTrace();
    ext.setScenarioSimulatedDate(new Date("10-Jul-1974"));
    sc.getFixtures().add(ext);
    run = new ScenarioRunner(ksession);
    run.run(sc);
    long expected = ext.getScenarioSimulatedDate().getTime();
    assertEquals(expected, ksession.getSessionClock().getCurrentTime());
}
Also used : ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) Test(org.junit.Test)

Example 3 with PseudoClockScheduler

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

the class ProtobufInputMarshaller method readSession.

public static StatefulKnowledgeSessionImpl readSession(ProtobufMessages.KnowledgeSession _session, StatefulKnowledgeSessionImpl session, InternalAgenda agenda, MarshallerReaderContext context) throws IOException, ClassNotFoundException {
    GlobalResolver globalResolver = (GlobalResolver) context.env.get(EnvironmentName.GLOBALS);
    if (globalResolver != null) {
        session.setGlobalResolver(globalResolver);
    }
    if (session.getTimerService() instanceof PseudoClockScheduler) {
        PseudoClockScheduler clock = (PseudoClockScheduler) session.getTimerService();
        clock.advanceTime(_session.getTime(), TimeUnit.MILLISECONDS);
    }
    // RuleFlowGroups need to reference the session
    // for ( InternalAgendaGroup group : agenda.getAgendaGroupsMap().values() ) {
    // ((RuleFlowGroupImpl) group).setWorkingMemory( session );
    // }
    context.wm = session;
    // need to read node memories before reading the fact handles
    // because this data is required during fact propagation
    readNodeMemories(context, _session.getRuleData());
    List<PropagationContext> pctxs = new ArrayList<PropagationContext>();
    if (_session.getRuleData().hasInitialFact()) {
        session.setInitialFactHandle(session.initInitialFact(context.kBase, context));
        context.handles.put(session.getInitialFactHandle().getId(), session.getInitialFactHandle());
    }
    for (ProtobufMessages.EntryPoint _ep : _session.getRuleData().getEntryPointList()) {
        EntryPoint wmep = ((StatefulKnowledgeSessionImpl) context.wm).getEntryPointMap().get(_ep.getEntryPointId());
        readFactHandles(context, _ep, ((WorkingMemoryEntryPoint) wmep).getObjectStore(), pctxs);
        context.filter.fireRNEAs(context.wm);
        readTruthMaintenanceSystem(context, wmep, _ep, pctxs);
    }
    cleanReaderContexts(pctxs);
    readActionQueue(context, _session.getRuleData());
    if (processMarshaller != null) {
        if (_session.hasProcessData()) {
            context.parameterObject = _session.getProcessData();
            processMarshaller.readProcessInstances(context);
            context.parameterObject = _session.getProcessData();
            processMarshaller.readWorkItems(context);
            // This actually does ALL timers, due to backwards compatability issues
            // It will read in old JBPM binaries, but always write to the new binary format.
            context.parameterObject = _session.getProcessData();
            processMarshaller.readProcessTimers(context);
        }
    } else {
        if (_session.hasProcessData()) {
            throw new IllegalStateException("No process marshaller, unable to unmarshall process data.");
        }
    }
    if (_session.hasTimers()) {
        for (ProtobufMessages.Timers.Timer _timer : _session.getTimers().getTimerList()) {
            readTimer(context, _timer);
        }
    }
    // need to process any eventual left over timer node timers
    if (!context.timerNodeSchedulers.isEmpty()) {
        for (Map<TupleKey, Scheduler> schedulers : context.timerNodeSchedulers.values()) {
            for (Scheduler scheduler : schedulers.values()) {
                scheduler.schedule(scheduler.getTrigger());
            }
        }
        context.timerNodeSchedulers.clear();
    }
    // remove the activations filter
    agenda.setActivationsFilter(null);
    return session;
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) Scheduler(org.drools.core.phreak.PhreakTimerNode.Scheduler) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) ArrayList(java.util.ArrayList) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) GlobalResolver(org.drools.core.spi.GlobalResolver) Timer(org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer)

Example 4 with PseudoClockScheduler

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

the class IncrementalCompilationTest method testIncrementalCompilationWithTimerNode.

@Test
public void testIncrementalCompilationWithTimerNode() throws Exception {
    // DROOLS-1195
    String drl1 = "package fr.edf.distribution.brms.common.test\n" + "import " + DummyEvent.class.getCanonicalName() + "\n" + "declare DummyEvent\n" + "    @role( event )\n" + "    @timestamp( eventTimestamp )\n" + "end\n" + "rule \"RG_TEST_TIMER\"\n" + "timer (int: 0 1; start=$expirationTimestamp , repeat-limit=0 )\n" + "    when\n" + "       $dummy: DummyEvent (id == 'timer', $expirationTimestamp : systemTimestamp )\n" + "    then\n " + "System.out.println(\"1\");\n" + "end\n";
    String drl2 = "package fr.edf.distribution.brms.common.test\n" + "import " + DummyEvent.class.getCanonicalName() + "\n" + "declare DummyEvent\n" + "    @role( event )\n" + "    @timestamp( eventTimestamp )\n" + "end\n" + "rule \"RG_TEST_TIMER_NEW\"\n" + "timer (int: 0 1; start=$expirationTimestamp , repeat-limit=0 )\n" + "    when\n" + "       $dummy: DummyEvent (id == 'timer', $expirationTimestamp : systemTimestamp )\n" + "		DummyEvent (id == 'timer_match')\n" + "    then\n " + "System.out.println(\"1\");\n" + "end\n" + "rule \"RG_OTHER_RULE\"\n" + "    when\n" + "       $dummy: DummyEvent ( id == 'timer' )\n" + "    then\n " + "System.out.println(\"2\");\n" + "end\n";
    long now = System.currentTimeMillis();
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1));
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession kieSession = kc.newKieSession();
    DummyEvent dummyEvent = new DummyEvent();
    dummyEvent.setId("timer");
    dummyEvent.setEventTimestamp(now);
    dummyEvent.setSystemTimestamp(now + TimeUnit.HOURS.toMillis(1));
    DummyEvent other = new DummyEvent();
    other.setId("timer_match");
    other.setEventTimestamp(now);
    kieSession.insert(dummyEvent);
    kieSession.insert(other);
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "2.0.0");
    deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
    kc.updateToVersion(releaseId2);
    PseudoClockScheduler scheduler = kieSession.getSessionClock();
    scheduler.setStartupTime(now);
    scheduler.advanceTime(1, TimeUnit.DAYS);
    assertEquals(2, kieSession.fireAllRules());
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 5 with PseudoClockScheduler

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

the class IncrementalCompilationTest method testDrlRenamingWithEvents.

@Test
public void testDrlRenamingWithEvents() throws Exception {
    // DROOLS-965
    String drl1 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "\n" + "global java.util.concurrent.atomic.AtomicInteger counter1;\n" + "global java.util.concurrent.atomic.AtomicInteger counter2;\n" + "\n" + "declare SimpleEvent\n" + "    @role( event )\n" + "    @timestamp( timestamp )\n" + "    @expires( 2d )\n" + "end\n" + "\n" + "rule R1 when\n" + "    $s:SimpleEvent(code==\"MY_CODE\")\n" + "then\n" + "    counter1.incrementAndGet();\n" + "end\n" + "\n" + "rule R2 when\n" + "    $s:SimpleEvent(code==\"MY_CODE\")\n" + "    not SimpleEvent(this != $s, this after [0,10s] $s)\n" + "then\n" + "    counter2.incrementAndGet();\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
    KieModule km = deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1));
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    PseudoClockScheduler clock = ksession.getSessionClock();
    AtomicInteger counter1 = new AtomicInteger(0);
    AtomicInteger counter2 = new AtomicInteger(0);
    ksession.setGlobal("counter1", counter1);
    ksession.setGlobal("counter2", counter2);
    ksession.insert(new SimpleEvent("1", "MY_CODE", 0));
    ksession.fireAllRules();
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new SimpleEvent("2", "MY_CODE", 5));
    ksession.fireAllRules();
    assertEquals(2, counter1.get());
    assertEquals(0, counter2.get());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
    // the null drl placeholder is used to have the same drl with a different file name
    // this causes the removal and readdition of both rules
    km = deployJar(ks, createKJar(ks, kproj, releaseId2, null, (String) null, drl1));
    kc.updateToVersion(releaseId2);
    clock = ksession.getSessionClock();
    clock.advanceTime(16, TimeUnit.SECONDS);
    ksession.insert(new SimpleEvent("3", "MY_CODE", 21));
    ksession.fireAllRules();
    assertEquals(5, counter1.get());
    assertEquals(1, counter2.get());
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)134 KieSession (org.kie.api.runtime.KieSession)126 Test (org.junit.Test)116 KieBase (org.kie.api.KieBase)105 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)71 ArrayList (java.util.ArrayList)67 List (java.util.List)57 Date (java.util.Date)40 Arrays.asList (java.util.Arrays.asList)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)33 EntryPoint (org.kie.api.runtime.rule.EntryPoint)23 DateFormat (java.text.DateFormat)18 SimpleDateFormat (java.text.SimpleDateFormat)18 Calendar (org.kie.api.time.Calendar)18 StockTick (org.drools.testcoverage.common.model.StockTick)17 FactHandle (org.kie.api.runtime.rule.FactHandle)17 KieHelper (org.kie.internal.utils.KieHelper)16 Ignore (org.junit.Ignore)14 KieBaseTestConfiguration (org.drools.testcoverage.common.util.KieBaseTestConfiguration)13 Locale (java.util.Locale)12