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(timeout=10000) public void testEagerEvaluation() throws Exception { String str = "package org.simple \n" + "rule xxx @Propagation(EAGER) \n" + "when \n" + " $s : String()\n" + "then \n" + "end \n" + "rule yyy @Propagation(EAGER) \n" + "when \n" + " $s : String()\n" + "then \n" + "end \n"; KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); conf.setOption( ForceEagerActivationOption.YES ); KieBase kbase = loadKnowledgeBaseFromString(str); KieSession ksession = createKnowledgeSession(kbase, conf); final List list = new ArrayList(); AgendaEventListener agendaEventListener = new DefaultAgendaEventListener() { public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) { list.add("activated"); } }; ksession.addEventListener(agendaEventListener); ksession.insert("test"); assertEquals(2, list.size()); }
@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 testRegisterHandlerWithKsessionUsingConfiguration() { KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); Properties props = new Properties(); props.setProperty("drools.workItemHandlers", "CustomWorkItemHandlers.conf"); KieSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(props); KieSession ksession = kbase.newKieSession(config, EnvironmentFactory.newEnvironment()); assertNotNull(ksession); // this test would fail on creation of the work item manager if injecting session is not supported WorkItemManager manager = ksession.getWorkItemManager(); assertNotNull(manager); Map<String, WorkItemHandler> handlers = ((SessionConfiguration)config).getWorkItemHandlers(); assertNotNull(handlers); assertEquals(1, handlers.size()); assertTrue(handlers.containsKey("Custom")); }
@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 testMergeConfig() { // JBRULES-3155 KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); Properties properties = new Properties(); properties.put("drools.processInstanceManagerFactory", "com.example.CustomInfinispanProcessInstanceManagerFactory"); KieSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(properties); StatefulKnowledgeSession ksession = InfinispanKnowledgeService.newStatefulKnowledgeSession( kbase, config, env ); SessionConfiguration sessionConfig = (SessionConfiguration)ksession.getSessionConfiguration(); assertEquals("com.example.CustomInfinispanProcessInstanceManagerFactory", sessionConfig.getProcessInstanceManagerFactory()); }
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(); } }
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 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 testMergeConfig() { KieSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(properties); assertEquals("com.example.CustomInfinispanProcessInstanceManagerFactory", sessionConfig.getProcessInstanceManagerFactory());
KieBase kbase = loadKnowledgeBaseFromString(kconf, drlContentString); KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); ksessionConfig.setOption(ClockTypeOption.get("pseudo")); ksessionConfig.setProperty("keep.reference", "true"); final KieSession ksession = kbase.newKieSession(ksessionConfig, null); ksession.addEventListener(agendaEventListener); PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock(); 100 * stIndex, 100 * stIndex); ksession.insert(st); Thread.sleep( 100 );
@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); }
private void checkPrice( String drl, double expectedPrice ) { KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL ) .build( EventProcessingOption.STREAM ) .newKieSession( sessionConfig, null ); List<Double> list = new ArrayList<Double>(); ksession.setGlobal( "list", list ); ksession.insert( "RHT" ); ksession.insert( new StockTick( 1L, "RHT", 10.0 ) ); ksession.insert( new StockTick( 2L, "RHT", 10.0 ) ); ksession.insert( new StockTick( 3L, "ABC", 20.0 ) ); ksession.insert( new StockTick( 4L, "RHT", 10.0 ) ); ksession.insert( new StockTick( 5L, "XYZ", 20.0 ) ); ksession.insert( new StockTick( 6L, "XYZ", 20.0 ) ); ksession.insert( new StockTick( 7L, "RHT", 10.0 ) ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertEquals( expectedPrice, (double)list.get(0), 0.01 ); }
@Before public void initialization() { KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem(); kfs.write("src/main/resources/kbase1/window_test.drl", drl); KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs); kbuilder.buildAll(); List<Message> res = kbuilder.getResults().getMessages(Level.ERROR); assertEquals(res.toString(), 0, res.size()); KieBaseConfiguration kbconf = KnowledgeBaseFactory .newKnowledgeBaseConfiguration(); kbconf.setOption(EventProcessingOption.STREAM); KieBase kbase = KieServices.Factory.get() .newKieContainer(kbuilder.getKieModule().getReleaseId()) .newKieBase(kbconf); KieSessionConfiguration ksconfig = KnowledgeBaseFactory .newKnowledgeSessionConfiguration(); ksconfig.setOption(ClockTypeOption.get("pseudo")); ksession = kbase.newKieSession(ksconfig, null); clock = ksession.getSessionClock(); }
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 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 A(1) ); ksession.insert( new B(1) ); ksession.insert( new C(1) ); ksession.fireAllRules(); assertEquals(0, counter.get());
@Test public void testSimulationCommand() { PathFinder finder = PathFinderFactory.getInstance(this.getClass().getResourceAsStream("/BPMN2-ExclusiveSplit.bpmn2")); List<SimulationPath> paths = finder.findPaths(new SimulationFilterPathFormatConverter()); SimulationContext context = SimulationContextFactory.newContext(new HardCodedSimulationDataProvider()); KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(); builder.add(ResourceFactory.newClassPathResource("BPMN2-ExclusiveSplit.bpmn2"), ResourceType.BPMN2); KieBase kbase = builder.newKieBase(); KieSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); config.setOption(ClockTypeOption.get("pseudo") ); for (SimulationPath sp : paths) { KieSession session = kbase.newKieSession(config, KieServices.get().newEnvironment()); ((SessionPseudoClock) session.getSessionClock()).advanceTime(System.currentTimeMillis(), TimeUnit.MILLISECONDS); session.execute(new SimulateProcessPathCommand("com.sample.test", context, sp)); } } }
private StatefulKnowledgeSession disposeAndReloadSession(StatefulKnowledgeSession ksession, KieBase kbase) { long ksessionId = ksession.getIdentifier(); ksession.dispose(); final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); conf.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); StatefulKnowledgeSession newksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession( ksessionId, kbase, conf, createEnvironment(context) ); return newksession; }
@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 testSimulationCommand() { PathFinder finder = PathFinderFactory.getInstance(this.getClass().getResourceAsStream("/BPMN2-ExclusiveSplit.bpmn2")); List<SimulationPath> paths = finder.findPaths(new SimulationFilterPathFormatConverter()); SimulationContext context = SimulationContextFactory.newContext(new HardCodedSimulationDataProvider()); KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(); builder.add(ResourceFactory.newClassPathResource("BPMN2-ExclusiveSplit.bpmn2"), ResourceType.BPMN2); KieBase kbase = builder.newKieBase(); KieSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); config.setOption(ClockTypeOption.get("pseudo") ); for (SimulationPath sp : paths) { KieSession session = kbase.newKieSession(config, KieServices.get().newEnvironment()); ((SessionPseudoClock) session.getSessionClock()).advanceTime(System.currentTimeMillis(), TimeUnit.MILLISECONDS); session.execute(new SimulateProcessPathCommand("com.sample.test", context, sp)); } } }