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()); }
@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 testBeliefSystemType() { config.setOption( BeliefSystemTypeOption.get( BeliefSystemType.JTMS.toString() ) ); assertEquals( BeliefSystemTypeOption.get( BeliefSystemType.JTMS.toString() ), config.getOption( BeliefSystemTypeOption.class ) ); // checking the string based getProperty() method assertEquals( BeliefSystemType.JTMS.getId(), config.getProperty( BeliefSystemTypeOption.PROPERTY_NAME ) ); // setting the options using the string based setProperty() method config.setProperty( BeliefSystemTypeOption.PROPERTY_NAME, BeliefSystemType.DEFEASIBLE.getId() ); // checking the type safe getOption() method assertEquals( BeliefSystemTypeOption.get( BeliefSystemType.DEFEASIBLE.getId() ), config.getOption( BeliefSystemTypeOption.class ) ); // checking the string based getProperty() method assertEquals( BeliefSystemType.DEFEASIBLE.getId(), config.getProperty( BeliefSystemTypeOption.PROPERTY_NAME ) ); }
@Test(timeout = 10000) public void testFireUntilHaltAndDispose() throws InterruptedException { final KieBase kbase = helper.build(EventProcessingOption.STREAM); final KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration(); ksconf.setOption(TimedRuleExecutionOption.YES); final KieSession ksession = kbase.newKieSession(ksconf, null); ksession.insert("xxx");
@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(); }
config.setOption(ClockTypeOption.get(getClockType())); ret.fireAllRules();
@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 testNotAfterOnDeclaration() { sessionConfig.setOption( ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()) ); StatefulKnowledgeSession kSession = kbase.newStatefulKnowledgeSession( sessionConfig, null ); assertEquals("1 facts in session", 1, kSession.getFactCount()); clock.advanceTime(3, TimeUnit.SECONDS); assertEquals("2 facts in session", 2, kSession.getFactCount());
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 testEventTimestamp2() { sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); assertEquals( 1, list.size() ); long time = (Long) list.get( 0 );
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); }
@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 testEventTimestamp() { sessionConfig.setOption( ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()) ); assertEquals( 1, list.size() ); Long time = (Long) list.get( 0 );
@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(); }