} catch ( final Exception e ) {
fail( "Should not throw any exception: " + e.getMessage() );
}
final WorkingMemory workingMemory = ruleBase.newStatefulSession();
QueryResults results = workingMemory.getQueryResults( "query-1" );
assertEquals( 0,
results.size() );
final Cheese stilton1 = new Cheese( "stilton",
100 );
final FactHandle handle1 = workingMemory.insert( stilton1 );
results = workingMemory.getQueryResults( "query-1" );
assertEquals( 1,
results.size() );
final Cheese cheddar = new Cheese( "cheddar",
55 );
workingMemory.insert( cheddar );
results = workingMemory.getQueryResults( "query-1" );
assertEquals( 1,
results.size() );
final Cheese stilton2 = new Cheese( "stilton",
5 );
final FactHandle handle2 = workingMemory.insert( stilton2 );
results = workingMemory.getQueryResults( "query-1" );
assertEquals( 2,
results.size() );
QueryResult result = results.get( 0 );
assertEquals( 1,
result.size() );
assertEquals( stilton1,
result.get( 0 ) );
result = results.get( 1 );
assertEquals( 1,
result.size() );
assertEquals( stilton2,
result.get( 0 ) );
int i = 0;
for ( final Iterator it = results.iterator(); it.hasNext(); ) {
result = (QueryResult) it.next();
assertEquals( 1,
result.size() );
if ( i == 1 ) {
assertSame( stilton2,
result.get( 0 ) );
} else {
assertSame( stilton1,
result.get( 0 ) );
}
i++;
}
workingMemory.retract( handle1 );
results = workingMemory.getQueryResults( "query-1" );
assertEquals( 1,
results.size() );
workingMemory.retract( handle2 );
results = workingMemory.getQueryResults( "query-1" );
assertEquals( 0,
results.size() );
}