@Test public void testMultipleTimers() throws Exception { Reader source = new StringReader( conf.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); SessionPseudoClock clock = ( SessionPseudoClock) session.getSessionClock(); clock.advanceTime( 300, TimeUnit.MILLISECONDS ); assertEquals(0, myList.size()); assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState()); assertEquals(2, ((InternalProcessRuntime) ((InternalWorkingMemory) session).getProcessRuntime()).getTimerManager().getTimers().size()); clock = ( SessionPseudoClock) session.getSessionClock(); clock.advanceTime( 500, TimeUnit.MILLISECONDS );
@Before public void setUp() throws Exception { baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); // use stream mode to enable proper event processing (see Drools Fusion 5.5.0 Doc "Event Processing Modes") baseConfig.setOption( EventProcessingOption.STREAM ); kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig); // config sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); // use a pseudo clock, which starts at 0 and can be advanced manually sessionConfig.setOption( ClockTypeOption.get("pseudo") ); // create and return session session = kbase.newKieSession(sessionConfig, null); clock = session.getSessionClock(); }
@Test public void testWorkingMemorySimulationRepositoryCEPRule() { KieSession session = TestUtils.createSession("BPMN2-TwoUserTasks.bpmn2"); context.setClock((SessionPseudoClock) session.getSessionClock()); assertEquals(3, results.size());
@Test public void testMarshallEntryPointsWithNot() throws Exception { String str = ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); ksconf.setOption( TimerJobFactoryOption.get("trackable") ); KieSession ksession = kBase.newStatefulKnowledgeSession( ksconf, null ); PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock(); timeService.advanceTime( 3, TimeUnit.SECONDS ); ksession.fireAllRules();
@Before public void init() { final KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem(); KieModuleModel kmodule = ks.newKieModuleModel(); KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase") .setDefault(true) .setEventProcessingMode(EventProcessingOption.STREAM); baseModel.newKieSessionModel("defaultKSession") .setDefault(true) .setClockType(ClockTypeOption.get("pseudo")); kfs.writeKModuleXML(kmodule.toXML()); kfs.write(ks.getResources().newClassPathResource("calendar_timer.xls", this.getClass())); // README when path is set then test works KieBuilder kieBuilder = ks.newKieBuilder( kfs ).buildAll(); assertEquals( 0, kieBuilder.getResults().getMessages( org.kie.api.builder.Message.Level.ERROR ).size() ); ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession(); clock = ksession.getSessionClock(); }
@Test public void testBetaRightExpired() { // DROOLS-1329 String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "declare B @role( event ) @expires(11ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + " $a: A( $Aid: id > 0 )\n" + " $b: B( id == $Aid )\n" + "then\n" + " System.out.println(\"[\" + $a + \",\" + $b + \"]\");" + " counter.incrementAndGet();\n" + "end"; KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); KieHelper helper = new KieHelper(); helper.addContent( drl, ResourceType.DRL ); KieBase kbase = helper.build( EventProcessingOption.STREAM ); KieSession ksession = kbase.newKieSession( sessionConfig, null ); PseudoClockScheduler sessionClock = ksession.getSessionClock(); AtomicInteger counter = new AtomicInteger( 0 ); ksession.setGlobal( "counter", counter ); ksession.insert( new A(1) ); sessionClock.advanceTime( 20, TimeUnit.MILLISECONDS ); ksession.insert( new B(1) ); ksession.fireAllRules(); assertEquals(0, counter.get()); }
@Test public void testStatefulPseudoClockKieSession() throws Exception { KieSession ksession = context.getBean("statefulSessionPseudo", KieSession.class); assertNotNull(ksession); assertEquals("Session has configured different clock type", ClockTypeOption.get("pseudo"), ksession.getSessionConfiguration().getOption(ClockTypeOption.class)); assertTrue(String.format("Session clock not an instance of '~s', but '~s'.", SessionPseudoClock.class.getSimpleName(), ksession.getSessionClock().getClass().getSimpleName()), ksession.getSessionClock() instanceof SessionPseudoClock); }
@Test public void testWorkingMemorySimulationRepositoryCEPRule() { KieSession session = TestUtils.createSession("BPMN2-TwoUserTasks.bpmn2"); context.setClock((SessionPseudoClock) session.getSessionClock()); assertEquals(3, results.size());
@Test @Ignore("beta4 phreak") public void testMarshallEntryPointsWithSlidingTimeWindow() throws Exception { String str = ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); ksconf.setOption( TimerJobFactoryOption.get("trackable") ); KieSession ksession = createKnowledgeSession(kbase, ksconf); ksession.fireAllRules(); ksession = marsallStatefulKnowledgeSession( ksession ); assertEquals( 2, ((List) list.get( 0 )).size() ); PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock(); timeService.advanceTime( 15, TimeUnit.SECONDS ); ksession = marsallStatefulKnowledgeSession( ksession ); ksession.fireAllRules(); ksession = marsallStatefulKnowledgeSession( ksession ); assertEquals( 4, ((List) list.get( 0 )).size() ); timeService = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock(); timeService.advanceTime( 20, TimeUnit.SECONDS ); ksession = marsallStatefulKnowledgeSession( ksession ); ksession.fireAllRules(); assertEquals( 2, ((List) list.get( 0 )).size() );
@Test public void testBetaLeftExpired() { // DROOLS-1329 String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "declare B @role( event ) @expires(11ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + " $a: A( $Aid: id > 0 )\n" + " $b: B( id == $Aid )\n" + "then\n" + " System.out.println(\"[\" + $a + \",\" + $b + \"]\");" + " counter.incrementAndGet();\n" + "end"; KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); KieHelper helper = new KieHelper(); helper.addContent( drl, ResourceType.DRL ); KieBase kbase = helper.build( EventProcessingOption.STREAM ); KieSession ksession = kbase.newKieSession( sessionConfig, null ); PseudoClockScheduler sessionClock = ksession.getSessionClock(); AtomicInteger counter = new AtomicInteger( 0 ); ksession.setGlobal( "counter", counter ); ksession.insert( new B(1) ); sessionClock.advanceTime( 20, TimeUnit.MILLISECONDS ); ksession.insert( new A(1) ); ksession.fireAllRules(); assertEquals(0, counter.get()); }
@Test public void testStatefulRealTimeClockKieSession() throws Exception { KieSession ksession = context.getBean("statefulSessionRealTime", KieSession.class); assertNotNull(ksession); assertEquals("Session has configured different clock type", ClockTypeOption.get("realtime"), ksession.getSessionConfiguration().getOption(ClockTypeOption.class)); assertTrue(String.format("Session clock not an instance of '~s', but: '~s'.", JDKTimerService.class.getSimpleName(), ksession.getSessionClock().getClass().getSimpleName()), ksession.getSessionClock() instanceof JDKTimerService); }
@Test public void testMarshallEntryPointsWithExpires() throws Exception { String str = ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); ksconf.setOption( TimerJobFactoryOption.get("trackable") ); KieSession ksession = kBase.newStatefulKnowledgeSession( ksconf, null ); ksession.fireAllRules(); assertEquals( 1, cep.getFactHandles().size() ); PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock(); timeService.advanceTime( 11, TimeUnit.SECONDS ); ksession.fireAllRules();
@Test(timeout = 40000L) public void testEventsExpiration() { StringBuilder sb = new StringBuilder( 400 ); KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); PseudoClockScheduler sessionClock = ksession.getSessionClock(); sessionClock.setStartupTime(0); ksession.fireAllRules(); assertEquals(10, list.size()); assertEquals( 10L, ksession.getFactCount() ); ksession.fireAllRules(); assertEquals( 5L, ksession.getFactCount() ); ksession.fireAllRules(); assertEquals( 0L, ksession.getFactCount() );
@Test public void testStatefulPseudoClockKieSession() throws Exception { KieSession ksession = context.getBean("statefulSessionPseudo", KieSession.class); assertNotNull(ksession); assertEquals("Session has configured different clock type", ClockTypeOption.get("pseudo"), ksession.getSessionConfiguration().getOption(ClockTypeOption.class)); assertTrue(String.format("Session clock not an instance of '~s', but '~s'.", SessionPseudoClock.class.getSimpleName(), ksession.getSessionClock().getClass().getSimpleName()), ksession.getSessionClock() instanceof SessionPseudoClock); }
@Test public void testMarshallWithTimedRule() { EventProcessingOption.STREAM ); KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( "pseudo" ) ); KieSession ksession = kbase.newKieSession(sessionConfig, null); assertEquals( 1, ksession.getFactCount() ); ksession.fireAllRules(); assertEquals( 2, ksession.getFactCount() ); SessionPseudoClock clock = ksession.getSessionClock(); clock.advanceTime( 4, TimeUnit.SECONDS ); assertEquals( 2, ksession.getFactCount() ); ksession.fireAllRules(); assertEquals( 2, ksession.getFactCount() ); clock = ksession.getSessionClock(); assertEquals( 2, ksession.getFactCount() ); ksession.fireAllRules(); assertEquals( 0, ksession.getFactCount() );
@Test public void testStatefulRealTimeClockKieSession() throws Exception { KieSession ksession = context.getBean("statefulSessionRealTime", KieSession.class); assertNotNull(ksession); assertEquals("Session has configured different clock type", ClockTypeOption.get("realtime"), ksession.getSessionConfiguration().getOption(ClockTypeOption.class)); assertTrue(String.format("Session clock not an instance of '~s', but: '~s'.", JDKTimerService.class.getSimpleName(), ksession.getSessionClock().getClass().getSimpleName()), ksession.getSessionClock() instanceof JDKTimerService); }
@Test public void testAlpha() { String drl = "import " + A.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + " $a: A( $Aid: id > 0 )\n" + "then\n" + " System.out.println(\"[\" + $a + \"]\");" + " counter.incrementAndGet();\n" + "end"; KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); KieHelper helper = new KieHelper(); helper.addContent( drl, ResourceType.DRL ); KieBase kbase = helper.build( EventProcessingOption.STREAM ); KieSession ksession = kbase.newKieSession( sessionConfig, null ); PseudoClockScheduler sessionClock = ksession.getSessionClock(); AtomicInteger counter = new AtomicInteger( 0 ); ksession.setGlobal( "counter", counter ); ksession.insert( new A(1) ); sessionClock.advanceTime( 10, TimeUnit.MILLISECONDS ); ksession.insert( new A(2) ); sessionClock.advanceTime( 10, TimeUnit.MILLISECONDS ); ksession.fireAllRules(); assertEquals(2, counter.get()); }
@Test public void testBetaLeftExpired2() { "end"; KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); KieSession ksession = kbase.newKieSession( sessionConfig, null ); PseudoClockScheduler sessionClock = ksession.getSessionClock(); ksession.insert( new C(1) ); ksession.fireAllRules(); assertEquals(0, counter.get());
private void testEventsExpiredInThePast(final String drl) { final KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); final KieHelper helper = new KieHelper(); helper.addContent( drl, ResourceType.DRL ); final KieBase kieBase = helper.build( EventProcessingOption.STREAM ); final KieSession kieSession = kieBase.newKieSession( sessionConfig, null ); PseudoClockScheduler clock = kieSession.getSessionClock(); final long currentTime = clock.getCurrentTime(); clock.advanceTime(100, TimeUnit.MILLISECONDS); kieSession.insert(new BasicEvent(new Date(currentTime + 20), 10L, "20ms-30ms")); clock.advanceTime(1, TimeUnit.MILLISECONDS); kieSession.insert(new BasicEvent(new Date(currentTime + 20), 20L, "20ms-40ms")); clock.advanceTime(100, TimeUnit.MILLISECONDS); Assertions.assertThat(kieSession.fireAllRules()).isEqualTo(1); clock.advanceTime(10, TimeUnit.MILLISECONDS); Assertions.assertThat(kieSession.getObjects()).isEmpty(); } }
@Test public void testMarshallEntryPointsWithNot() throws Exception { String str = KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); ksconf.setOption( TimerJobFactoryOption.get("trackable") ); KieSession ksession = kBase.newKieSession( ksconf, null ); PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock(); timeService.advanceTime( 3, TimeUnit.SECONDS ); ksession.fireAllRules(); assertEquals( 0, list.size() );