org.kie.api.runtime.rule.FactHandle c2Fh = ksession.insert( cheddar2 );
org.kie.api.runtime.rule.FactHandle c3Fh = ksession.insert( cheddar3 );
DroolsEventList list = new DroolsEventList();
// Open the LiveQuery
LiveQuery query = ksession.openLiveQuery( "cheeses", new Object[] { "cheddar", "stilton" } , list );
SortedList<Row> sorted = new SortedList<Row>( list, new Comparator<Row>() {
public int compare(Row r1,
Row r2) {
Cheese c1 = ( Cheese ) r1.get( "stilton" );
Cheese c2 = ( Cheese ) r2.get( "stilton" );
return c1.getPrice() - c2.getPrice();
}
});
assertEquals( 3, sorted.size() );
assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
assertEquals( 2, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
assertEquals( 3, ((Cheese)sorted.get( 2 ).get( "stilton" )).getPrice() );
// alter the price to remove the last row
stilton3.setPrice( 4 );
ksession.update( s3Fh, stilton3 );
ksession.fireAllRules();
assertEquals( 2, sorted.size() );
assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
assertEquals( 2, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
// alter the price to put the last row back in
stilton3.setPrice( 3 );
ksession.update( s3Fh, stilton3 );
ksession.fireAllRules();
assertEquals( 3, sorted.size() );
assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
assertEquals( 2, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
assertEquals( 3, ((Cheese)sorted.get( 2 ).get( "stilton" )).getPrice() );
// alter the price to remove the middle row
stilton2.setPrice( 4 );
ksession.update( s2Fh, stilton2 );
ksession.fireAllRules();
assertEquals( 2, sorted.size() );
assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
assertEquals( 3, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
// alter the price to add the previous middle rows to the end
cheddar2.setPrice( 4 );
ksession.update( c2Fh, cheddar2 );
ksession.fireAllRules();
assertEquals( 3, sorted.size() );
assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
assertEquals( 3, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
assertEquals( 4, ((Cheese)sorted.get( 2 ).get( "stilton" )).getPrice() );
// Check a standard retract
ksession.retract( s1Fh );
ksession.fireAllRules();
assertEquals( 2, sorted.size() );
assertEquals( 3, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
assertEquals( 4, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
// Close the query, we should get removed events for each row
query.close();
assertEquals( 0, sorted.size() );
}