@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 );
private StatefulKnowledgeSessionImpl createWorkingMemory(InternalKnowledgeBase kBase) { // WorkingMemoryEntryPoint KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); SessionConfiguration sessionConf = ((SessionConfiguration) ksconf); StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase, true, sessionConf, EnvironmentFactory.newEnvironment()); return wm; }
@Test public void testClockTypeConfiguration() { // setting the option using the type safe method config.setOption( ClockTypeOption.get("pseudo") ); // checking the type safe getOption() method assertEquals( ClockTypeOption.get("pseudo"), config.getOption( ClockTypeOption.class ) ); // checking the string based getProperty() method assertEquals( "pseudo", config.getProperty( ClockTypeOption.PROPERTY_NAME ) ); // setting the options using the string based setProperty() method config.setProperty( ClockTypeOption.PROPERTY_NAME, "realtime" ); // checking the type safe getOption() method assertEquals( ClockTypeOption.get("realtime"), config.getOption( ClockTypeOption.class ) ); // checking the string based getProperty() method assertEquals( "realtime", config.getProperty( ClockTypeOption.PROPERTY_NAME ) ); }
@Test public void testEventOffset() throws Exception { String drl = "package org.drools.compiler.test\n" + KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel( KSESSION1 ).setDefault( true ) .setType( KieSessionModel.KieSessionType.STATEFUL ) .setClockType( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); assertEquals( 10001, ((Number) expOffset).longValue() );
config.setOption(ClockTypeOption.get(getClockType())); ret.fireAllRules();
@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 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 testContainerMBeans() throws Exception { String drl = "package org.drools.compiler.test\n" + KieSessionModel ksessionModel1 = kieBaseModel1.newKieSessionModel( KSESSION1 ).setDefault( true ) .setType( KieSessionModel.KieSessionType.STATEFUL ) .setClockType( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); DroolsManagementAgent.createObjectNameBy(kc1ID), KieContainerMonitorMXBean.class); assertEquals(releaseId1.toExternalForm(), c1Monitor.getConfiguredReleaseIdStr()); assertEquals(releaseId1.toExternalForm(), c1Monitor.getResolvedReleaseIdStr()); assertEquals(releaseId1.getVersion(), c1Monitor.getConfiguredReleaseId().getVersion()); assertEquals(releaseId1.getVersion(), c1Monitor.getResolvedReleaseId().getVersion());
private KieSession marsallStatefulKnowledgeSession(KieSession ksession) throws IOException, ClassNotFoundException { Globals globals = ksession.getGlobals(); KieBase kbase = ksession.getKieBase(); ByteArrayOutputStream out = new ByteArrayOutputStream(); MarshallerFactory.newMarshaller( kbase ).marshall( out, ksession ); KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); ksconf.setOption( TimerJobFactoryOption.get("trackable") ); ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); Environment env = EnvironmentFactory.newEnvironment(); env.set( EnvironmentName.GLOBALS, globals ); ksession = MarshallerFactory.newMarshaller( kbase ).unmarshall( new ByteArrayInputStream( out.toByteArray() ), ksconf, env ); return ksession; }
@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 testGetKieBaseAfterKieSessionCreation() { final String KBASE_NAME = "kieBase"; final String KSESSION_NAME = "kieSession"; final String drl = "declare TestEvent\n" + " @role( event )\n" + " name : String\n" + "end\n" + "\n" + "declare window DeclaredTimeWindow\n" + " TestEvent ( name == \"timeDec\" ) over window:time( 50ms ) from entry-point EventStream\n" + "end"; final KieServices ks = KieServices.Factory.get(); final KieFileSystem kfs = ks.newKieFileSystem(); kfs.write( "src/main/resources/window.drl", drl ); final KieModuleModel kmoduleModel = ks.newKieModuleModel(); kmoduleModel.newKieBaseModel( KBASE_NAME ) .addPackage( "*" ) .newKieSessionModel( KSESSION_NAME ) .setDefault( true ) .setClockType( ClockTypeOption.get( "pseudo" ) ); kfs.writeKModuleXML( kmoduleModel.toXML() ); final KieBuilder builder = ks.newKieBuilder( kfs ).buildAll(); assertEquals( 0, builder.getResults().getMessages().size() ); ks.getRepository().addKieModule( builder.getKieModule() ); final KieSession kieSession = ks.newKieContainer( ks.getRepository().getDefaultReleaseId() ).newKieSession( KSESSION_NAME ); assertNotNull( kieSession ); final KieBase kieBase = ks.newKieContainer( ks.getRepository().getDefaultReleaseId() ).getKieBase( KBASE_NAME ); assertNotNull( kieBase ); }
@Before public void setUp() { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( drl.getBytes() ), ResourceType.DRL); if (kbuilder.hasErrors()) { System.err.println(kbuilder.getErrors().toString()); } final KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( EventProcessingOption.STREAM); final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(config); kbase.addPackages(kbuilder.getKnowledgePackages()); final KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() )); this.statefulSession = kbase.newKieSession(sessionConfig, null); this.stockFactory = new StockFactory(kbase); }
@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 testMarshallingUnmarshalling() { KieServices ks = KieServices.Factory.get(); .setClockType(ClockTypeOption.get("realtime")) .setBeliefSystem(BeliefSystemTypeOption.get("jtms")) .setFileLogger("drools.log", 10, true) assertEquals(LanguageLevelOption.DRL6_STRICT.toString(), kprojXml.getConfigurationProperty(LanguageLevelOption.PROPERTY_NAME)); assertEquals(PropertySpecificOption.ALWAYS.toString(), kprojXml.getConfigurationProperty(PropertySpecificOption.PROPERTY_NAME)); assertEquals(EqualityBehaviorOption.EQUALITY, kieBaseModelXML.getEqualsBehavior()); assertEquals(EventProcessingOption.STREAM, kieBaseModelXML.getEventProcessingMode()); assertEquals(DeclarativeAgendaOption.ENABLED, kieBaseModelXML.getDeclarativeAgenda()); assertSame(kieBaseModelXML, ((KieSessionModelImpl)kieSessionModelXML).getKieBaseModel()); assertEquals(KieSessionType.STATEFUL, kieSessionModelXML.getType()); assertEquals(ClockTypeOption.get("realtime"), kieSessionModelXML.getClockType()); assertEquals(BeliefSystemTypeOption.get("jtms"), kieSessionModelXML.getBeliefSystem()); assertEquals("org.domain.Monday", kieSessionModelXML.getCalendars().get("monday"));
private StatefulKnowledgeSession createSession(KieBase kbase) { final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); conf.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); StatefulKnowledgeSession ksession = InfinispanKnowledgeService.newStatefulKnowledgeSession( kbase, conf, createEnvironment(context) ); return ksession; }
@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() );