@Test public void testDeclaredLengthWindowInRule() { EntryPoint entryPoint = ksession.getEntryPoint("EventStream"); List<Long> result = new ArrayList<Long>(); ksession.setGlobal("result", result); TestEvent event; for (int i = 1; i <= 10; i++) { event = new TestEvent(null, "lengthDec", null); entryPoint.insert(event); ksession.fireAllRules(); assertEquals((i < 5 ? i : 5), result.get(result.size() - 1) .longValue()); } }
/** * Inserts events using multiple threads into one EntryPoint. The insert * operation is synchronized on corresponding SessionEntryPoint instance. */ @Test public void testOneEntryPoint() throws Exception { final EntryPoint firstThreadEntryPoint = kieSession.getEntryPoint("FirstStream"); final int numInsertersInEachEntryPoint = 10; final ExecutorService executorService = Executors.newFixedThreadPool(numInsertersInEachEntryPoint); try { final List<Future<?>> futures = new ArrayList<>(); for (int i = 0; i < numInsertersInEachEntryPoint; i++) { // future for exception watching final Future<?> futureForFirstThread = executorService.submit( new TestInserter(kieSession, firstThreadEntryPoint)); futures.add(futureForFirstThread); } for (final Future<?> f : futures) { f.get(30, TimeUnit.SECONDS); } } finally { executorService.shutdownNow(); } }
private void playAnEvent(final String key) { final AnEvent discardCardIrr = new AnEvent(key); ksession.getEntryPoint("game stream").insert(discardCardIrr); giveTheRuleThreadSomeTimeToFinishComputation(300); // give some time the other thread where rules run finished with the execution }
@Test(timeout = 1000000) public void testSlidingTimeWindows() { final String str = "package org.drools\n" + final KieBase kbase = loadKnowledgeBaseFromString(kbconf, str); final KieSession ksession = kbase.newKieSession(); final EntryPoint ep = ksession.getEntryPoint("X"); final List list = new ArrayList(); ksession.setGlobal("list", list); if (currentTimeInMillis > lastTimeInserted) { lastTimeInserted = currentTimeInMillis; ep.insert(new StockTick(j++, s, 0.0, 0));
try { for ( int j = 0; j < N; j++ ) { ks.getEntryPoint( "x" ).insert( new Integer( j ) ); assertEquals( N, list.size() );
@Test public void testDeclaredTimeWindowInRule() throws InterruptedException { final long results[] = new long[] { 1, 2, 3, 4, 5, 5, 5, 5, 5, 5 }; EntryPoint entryPoint = ksession.getEntryPoint("EventStream"); List<Long> result = new ArrayList<Long>(); ksession.setGlobal("result", result); TestEvent event; for (int i = 0; i < 10; i++) { event = new TestEvent(null, "timeDec", null); entryPoint.insert(event); ksession.fireAllRules(); assertEquals(results[i], result.get(result.size() - 1).longValue()); clock.advanceTime(10, TimeUnit.MILLISECONDS); } }
/** * Inserts events using multiple threads into two EntryPoints. The insert * operation is synchronized on corresponding SessionEntryPoint instance. */ @Test public void testTwoEntryPoints() throws Exception { final EntryPoint firstThreadEntryPoint = kieSession.getEntryPoint("FirstStream"); final EntryPoint secondThreadEntryPoint = kieSession.getEntryPoint("SecondStream"); final int numInsertersInEachEntryPoint = 10; final int numThreadPoolCapacity = numInsertersInEachEntryPoint * 2; final ExecutorService executorService = Executors.newFixedThreadPool(numThreadPoolCapacity); try { final List<Future<?>> futures = new ArrayList<>(); for (int i = 0; i < numInsertersInEachEntryPoint; i++) { // working only with first stream, future for exception watching final Future<?> futureForFirstThread = executorService.submit(new TestInserter(kieSession, firstThreadEntryPoint)); futures.add(futureForFirstThread); // working only with second stream, future for exception watching final Future<?> futureForSecondThread = executorService.submit(new TestInserter(kieSession, secondThreadEntryPoint)); futures.add(futureForSecondThread); } for (final Future<?> f : futures) { f.get(30, TimeUnit.SECONDS); } } finally { executorService.shutdownNow(); } }
@Test(timeout = 5000) public void testRaceOnAccumulateNodeSimple() throws InterruptedException { final EntryPoint ep01 = session.getEntryPoint("ep01"); ep01.insert(new IntEvent((int) i)); //Thread.sleep (0x1); } if (i % 1000 == 0) { System.out.println(i);
public Boolean call() throws Exception { EntryPoint ep = ksession.getEntryPoint("EP" + index); FactHandle[] fhs = new FactHandle[15]; for (int j = 0; j < 3; j++) { for (int i = 0; i < 5; i++) { fhs[i * 3] = ep.insert("" + i); fhs[i * 3 + 1] = ep.insert(new Long(i)); fhs[i * 3 + 2] = ep.insert(new Integer(i)); } for (int i = 0; i < 5; i++) { if (i == index+j) continue; ep.delete(fhs[i * 3]); ep.delete(fhs[i * 3 + 1]); ep.delete(fhs[i * 3 + 2]); } } return true; } }
@Test public void testDeclaredLengthWindowInQuery() { EntryPoint entryPoint = ksession.getEntryPoint("EventStream"); TestEvent event; for (int i = 1; i <= 10; i++) { event = new TestEvent(null, "lengthDec", null); entryPoint.insert(event); assertEquals((i < 5 ? i : 5), ((Long) ksession .getQueryResults("TestDeclaredLengthWindow") .iterator().next().get("$eventCount")).intValue()); } }
@Test public void testDifferentEntryPointsOnSameFact() { FactHandle f1 = ksession.insert( "f1" ); EntryPoint ep = ksession.getEntryPoint( "xxx" ); try { ep.update( f1, "s1" );
public Boolean call() throws Exception { List results = new ArrayList(); ksession.setGlobal("results" + index, results); ksession.insert("ACME" + index); EntryPoint ep = ksession.getEntryPoint("EP" + index); for (int i = 0; i < 12; i++) { ep.insert(new StockTick(1, "ACME" + index, i - 50)); ep.insert(new StockTick(2, "DROO" + index, i)); ep.insert(new StockTick(3, "ACME" + index, i)); } return true; } }
@Test public void testLengthWindow() { EntryPoint entryPoint = ksession.getEntryPoint("EventStream"); TestEvent event; for (int i = 1; i <= 20; i++) { event = new TestEvent(null, "length", null); entryPoint.insert(event); assertEquals((i < 10 ? i : 10), ((Long) ksession.getQueryResults("TestLengthWindow") .iterator().next().get("$eventCount")).intValue()); } }
public Boolean call() throws Exception { EntryPoint ep = ksession.getEntryPoint("EP" + index); Thread.sleep(RANDOM.nextInt(100)); if (fh == null) { fh = ep.insert("" + index); } else { if ( RANDOM.nextInt(100) < 70 ) { fh = ep.insert("" + index);
@Test public void testTimeWindow() throws InterruptedException { EntryPoint entryPoint = ksession.getEntryPoint("EventStream"); final long results[] = new long[] { 1, 2, 3, 3, 3 }; TestEvent event; for (int i = 0; i < 5; i++) { event = new TestEvent(null, "time", null); entryPoint.insert(event); assertEquals(results[i], ksession.getQueryResults("TestTimeWindow") .iterator().next().get("$eventCount")); clock.advanceTime(100, TimeUnit.MILLISECONDS); } }
@Test public void testExpires() throws Exception { EntryPoint entryPoint = kieSession.getEntryPoint("EventStream"); StockTick event = new StockTick(); FactHandle handle = entryPoint.insert(event); assertTrue(entryPoint.getFactHandles().contains(handle)); kieSession.fireAllRules(); assertTrue(entryPoint.getFactHandles().contains(handle)); advanceTime(5, TimeUnit.SECONDS); kieSession.fireAllRules(); assertFalse(entryPoint.getFactHandles().contains(handle)); }
@Test public void testDeclaredTimeWindowInQuery() throws InterruptedException { final long results[] = new long[] { 1, 2, 3, 4, 5, 5, 5, 5, 5, 5 }; EntryPoint entryPoint = ksession.getEntryPoint("EventStream"); TestEvent event; for (int i = 0; i < 10; i++) { event = new TestEvent(null, "timeDec", null); entryPoint.insert(event); assertEquals(results[i], ksession.getQueryResults("TestDeclaredTimeWindow") .iterator().next().get("$eventCount")); clock.advanceTime(10, TimeUnit.MILLISECONDS); } }
@Test public void testFactLeak() throws InterruptedException { //DROOLS-131 String drl = "package org.drools.test; \n" + "global java.util.List list; \n" + "" + "" + "rule Intx when\n" + " $x : Integer() from entry-point \"x\" \n" + "then\n" + " list.add( $x ); \n" + "end"; int N = 1100; KieBase kb = loadKnowledgeBaseFromString( drl ); final KieSession ks = kb.newKieSession(); ArrayList list = new ArrayList(); ks.setGlobal( "list", list ); new Thread(ks::fireUntilHalt).start(); try { for ( int j = 0; j < N; j++ ) { ks.getEntryPoint( "x" ).insert( new Integer( j ) ); } Thread.sleep( 1000 ); } finally { ks.halt(); ks.dispose(); assertEquals( N, list.size() ); } }
@Test public void testFireUntilHaltFailingAcrossEntryPoints() throws Exception { String rule1 = "package org.drools.compiler\n"; final EntryPoint ep = ksession.getEntryPoint("testep2"); ksession.fireAllRules(); ep.insert(new Person("darth")); Thread.sleep(500); ksession.halt(); assertEquals(1, list.size());
@Test (timeout=10000) public void testDynamicRuleAdditionsWithEntryPoints() throws Exception { Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicWithEntryPoint.drl" ); InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase(); KieSession ksession = createKnowledgeSession( kbase ); // now lets add some knowledge to the kbase kbase.addPackages( kpkgs ); List<StockTick> results = new ArrayList<StockTick>(); ksession.setGlobal( "results", results ); EntryPoint ep = ksession.getEntryPoint( "in-channel" ); ep.insert( new StockTick( 1, "RHT", 20, 10000 ) ); ep.insert( new StockTick( 2, "RHT", 21, 15000 ) ); ep.insert( new StockTick( 3, "RHT", 22, 20000 ) ); ksession.fireAllRules(); assertEquals( 3, results.size() ); }