@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()); }
@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); }
/** * Initialisation of Drools stateful session without convention and kodmule files. * * @param ruleFiles * DRL files */ public static synchronized void initFromRulesFiles(Set < String > ruleFiles) { if (isInitialized()) { throw new IllegalStateException("This Factory has already be initialized once"); } _instance = new FF4jDroolsService(); _instance.ruleFiles = ruleFiles; KieHelper helper = new KieHelper(); KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId())); //ruleFiles.stream().forEach(drlFile -> helper.addContent(loadResourceAsString(drlFile), ResourceType.determineResourceType(drlFile))); for (String drlFile : ruleFiles) { String fileContent = loadResourceAsString(drlFile); ResourceType typeFile = ResourceType.determineResourceType(drlFile); helper.addContent(fileContent, typeFile); } _instance.ksession = helper.build(EventProcessingOption.STREAM).newKieSession(sessionConfig, null); }
@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 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() ) );
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; }
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 ); }
@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() ) );
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 testBetaLeftExpired2() { "end"; KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); ksession.setGlobal( "counter", counter ); ksession.insert( new A(1) ); ksession.insert( new B(1) ); 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 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 testBeta() { "end"; KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); ksession.setGlobal( "counter", counter ); ksession.insert( new A(1) ); ksession.insert( new B(1) ); ksession.insert( new A(2) ); ksession.insert( new B(2) ); ksession.fireAllRules(); assertEquals(2, counter.get());
@Test(timeout = 40000L) public void testEventsExpiration() { StringBuilder sb = new StringBuilder( 400 ); KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); ksession.insert( new MyEvent( i, i*2L ) ); ksession.fireAllRules(); ksession.fireAllRules(); assertEquals( 5L, ksession.getFactCount() ); ksession.fireAllRules(); assertEquals( 0L, ksession.getFactCount() );
@Test public void testSnapshotRecoveryScheduledRulesPlain() throws Exception { "end\n"; KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); ksconf.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); ksconf.setOption( TimedRuleExecutionOption.YES ); ksconf.setOption(TimerJobFactoryOption.get("trackable")); ksconf.setOption(ClockTypeOption.get("pseudo")); ksession.setGlobal("list2", list2); ksession.insert("Hello World1"); ksession.insert("Hello World2"); ksession.fireAllRules(); timeService.advanceTime(10500, TimeUnit.MILLISECONDS); ksession.fireAllRules();
@Test(timeout = 40000L) public void testFireUntilHaltWithExpiration() { StringBuilder sb = new StringBuilder( 400 ); KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) ); ksession.insert( new MyEvent( i, i*2L ) ); }} ); ksession.insert( 1 ); sessionClock.advanceTime( 29, TimeUnit.MILLISECONDS );