Skip to content

Commit

Permalink
[DROOLS-106] Using the same pattern in and out of a sliding windows c…
Browse files Browse the repository at this point in the history
…auses an NPE if a join is involved
  • Loading branch information
sotty committed Apr 30, 2013
1 parent 152843a commit 3666611
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
Expand Up @@ -12,6 +12,7 @@
import java.io.Serializable;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -2675,15 +2676,16 @@ public void testExpireEventOnEndTimestamp() throws Exception {


@Test
@Ignore
public void testSlidingWindowsAccumulateExternalJoin() throws Exception {

// DROOLS-106
// The logic may not be optimal, but was used to detect a WM corruption
String str =
"package testing2;\n" +
"\n" +
"import java.util.*;\n" +
"import org.drools.StockTick;\n" +
"\n" +
"" +
"global List list;\n" +
"" +
"declare StockTick\n" +
" @role( event )\n" +
Expand All @@ -2692,40 +2694,51 @@ public void testSlidingWindowsAccumulateExternalJoin() throws Exception {
"\n" +
"rule test\n" +
"when\n" +
" $primary : StockTick( $name : company ) over window:length(4)\n" +
" $primary : StockTick( $name : company ) over window:length(1)\n" +
" accumulate ( " +
" $tick : StockTick( company == $name ), " +
" $list : count( $tick ) )\n" +
" $tick : StockTick( company == $name ) , " +
" $num : count( $tick ) )\n" +

"then\n" +
" System.out.println(\"Found name: \" + $primary + \" with \" );\n" +
" System.out.println(\"Found name: \" + $primary + \" with \" +$num );\n" +
" list.add( $num.intValue() ); \n" +
"end\n" +
""
;

KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
config.setOption(EventProcessingOption.CLOUD);
KnowledgeBase kbase = loadKnowledgeBaseFromString(config, str);

KnowledgeSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) );
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(conf, null);

PseudoClockScheduler clock = (PseudoClockScheduler) ksession.getSessionClock();

int seq = 0;
List list = new ArrayList();
ksession.setGlobal( "list", list );

clock.advanceTime( 10, TimeUnit.MILLISECONDS );
ksession.insert( new StockTick( seq++, "x", 10.0, 10L ) );
ksession.insert( new StockTick( seq++, "y", 10.0, 10L ) );
ksession.insert( new StockTick( seq++, "z", 10.0, 10L ) );
ksession.insert( new StockTick( seq++, "AAA", 10.0, 10L ) );
ksession.fireAllRules();
assertEquals( list, Arrays.asList( 1 ) );

ksession.insert( new StockTick( seq++, "AAA", 15.0, 10L ) );
ksession.fireAllRules();
assertEquals( list, Arrays.asList( 1, 2 ) );

ksession.insert( new StockTick( seq++, "CCC", 10.0, 10L ) );
ksession.fireAllRules();
assertEquals( list, Arrays.asList( 1, 2, 1 ) );

System.out.println(" ___________________________________- ");

ksession.insert( new StockTick( seq++, "z", 13.0, 20L ) );
ksession.insert( new StockTick( seq++, "x", 11.0, 20L ) );
ksession.insert( new StockTick( seq++, "DDD", 13.0, 20L ) );
ksession.fireAllRules();
assertEquals( list, Arrays.asList( 1, 2, 1, 1 ) );

ksession.insert( new StockTick( seq++, "AAA", 11.0, 20L ) );
ksession.fireAllRules();
assertEquals( list, Arrays.asList( 1, 2, 1, 1, 3 ) );

// NPE Here
ksession.fireAllRules();
Expand Down
4 changes: 3 additions & 1 deletion drools-core/src/main/java/org/drools/reteoo/BetaNode.java
Expand Up @@ -703,9 +703,11 @@ public RightTuple createRightTuple(InternalFactHandle handle,
return new RightTuple( handle,
sink );
} else {
return new WindowTuple( handle,
WindowTuple tuple = new WindowTuple( handle,
sink,
context.getActiveWindowTupleList() );
context.setActiveWindowTupleList( null );
return tuple;
}
} else {
return new ConcurrentRightTuple( handle,
Expand Down

0 comments on commit 3666611

Please sign in to comment.