str += "end\n";
KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
StatefulKnowledgeSession ksession = createKnowledgeSession( kbase );
Cheese stilton1 = new Cheese( "stilton",
1 );
Cheese cheddar1 = new Cheese( "cheddar",
1 );
Cheese stilton2 = new Cheese( "stilton",
2 );
Cheese cheddar2 = new Cheese( "cheddar",
2 );
Cheese stilton3 = new Cheese( "stilton",
3 );
Cheese cheddar3 = new Cheese( "cheddar",
3 );
FactHandle s1Fh = ksession.insert( stilton1 );
ksession.insert( stilton2 );
ksession.insert( stilton3 );
ksession.insert( cheddar1 );
ksession.insert( cheddar2 );
FactHandle c3Fh = ksession.insert( cheddar3 );
final List<Object[]> updated = new ArrayList<Object[]>();
final List<Object[]> removed = new ArrayList<Object[]>();
final List<Object[]> added = new ArrayList<Object[]>();
ViewChangedEventListener listener = new ViewChangedEventListener() {
public void rowUpdated( Row row ) {
Object[] array = new Object[6];
array[0] = row.get( "stilton" );
array[1] = row.get( "cheddar" );
array[2] = row.get( "$sprice" );
array[3] = row.get( "$cprice" );
array[4] = row.get( "$type1" );
array[5] = row.get( "$type2" );
updated.add( array );
}
public void rowDeleted( Row row ) {
Object[] array = new Object[6];
array[0] = row.get( "stilton" );
array[1] = row.get( "cheddar" );
array[2] = row.get( "$sprice" );
array[3] = row.get( "$cprice" );
array[4] = row.get( "$type1" );
array[5] = row.get( "$type2" );
removed.add( array );
}
public void rowInserted( Row row ) {
Object[] array = new Object[6];
array[0] = row.get( "stilton" );
array[1] = row.get( "cheddar" );
array[2] = row.get( "$sprice" );
array[3] = row.get( "$cprice" );
array[4] = row.get( "$type1" );
array[5] = row.get( "$type2" );
added.add( array );
}
};
// Open the LiveQuery
LiveQuery query = ksession.openLiveQuery( "cheeses",
new Object[]{"stilton", "cheddar"},
listener );
ksession.fireAllRules();
// Assert that on opening we have three rows added
assertEquals( 3,
added.size() );
assertEquals( 0,
removed.size() );
assertEquals( 0,
updated.size() );
// Assert that the identifiers where retrievable
assertSame( stilton1,
added.get( 2 )[0] );
assertSame( cheddar1,
added.get( 2 )[1] );
assertEquals( 1,
added.get( 2 )[2] );
assertEquals( 1,
added.get( 2 )[3] );
assertEquals( "stilton",
added.get( 2 )[4] );
assertEquals( "cheddar",
added.get( 2 )[5] );
// And that we have correct values from those rows
assertEquals( 3,
added.get( 0 )[3] );
assertEquals( 2,
added.get( 1 )[3] );
assertEquals( 1,
added.get( 2 )[3] );
// Do an update that causes a match to become untrue, thus triggering a removed
cheddar3.setPrice( 4 );
ksession.update( c3Fh,
cheddar3 );
ksession.fireAllRules();
assertEquals( 3,
added.size() );
assertEquals( 1,
removed.size() );
assertEquals( 0,
updated.size() );
assertEquals( 4,
removed.get( 0 )[3] );
// Now make that partial true again, and thus another added
cheddar3.setPrice( 3 );
ksession.update( c3Fh,
cheddar3 );
ksession.fireAllRules();
assertEquals( 4,
added.size() );
assertEquals( 1,
removed.size() );
assertEquals( 0,
updated.size() );
assertEquals( 3,
added.get( 3 )[3] );
// check a standard update
cheddar3.setOldPrice( 0 );
ksession.update( c3Fh,
cheddar3 );
ksession.fireAllRules();
assertEquals( 4,