public void execTestAccumulateSum( String fileName ) throws Exception {
// read in the source
final Reader reader = new InputStreamReader( getClass().getResourceAsStream( fileName ) );
final RuleBase ruleBase = loadRuleBase( reader );
StatefulSession session = ruleBase.newStatefulSession();
DataSet data = new DataSet();
data.results = new ArrayList<Object>();
session.setGlobal( "results",
data.results );
data.cheese = new Cheese[]{new Cheese( "stilton",
8,
0 ), new Cheese( "stilton",
10,
1 ), new Cheese( "stilton",
9,
2 ), new Cheese( "brie",
11,
3 ), new Cheese( "brie",
4,
4 ), new Cheese( "provolone",
8,
5 )};
data.bob = new Person( "Bob",
"stilton" );
data.cheeseHandles = new FactHandle[data.cheese.length];
for ( int i = 0; i < data.cheese.length; i++ ) {
data.cheeseHandles[i] = session.insert( data.cheese[i] );
}
data.bobHandle = session.insert( data.bob );
// ---------------- 1st scenario
session.fireAllRules();
assertEquals( 1,
data.results.size() );
assertEquals( 27,
((Number) data.results.get( data.results.size() - 1 )).intValue() );
session = SerializationHelper.getSerialisedStatefulSession( session,
ruleBase );
updateReferences( session,
data );
// ---------------- 2nd scenario
final int index = 1;
data.cheese[index].setPrice( 3 );
session.update( data.cheeseHandles[index],
data.cheese[index] );
session.fireAllRules();
assertEquals( 2,
data.results.size() );
assertEquals( 20,
((Number) data.results.get( data.results.size() - 1 )).intValue() );
// ---------------- 3rd scenario
data.bob.setLikes( "brie" );
session.update( data.bobHandle,
data.bob );
session.fireAllRules();
assertEquals( 3,
data.results.size() );
assertEquals( 15,
((Number) data.results.get( data.results.size() - 1 )).intValue() );
// ---------------- 4th scenario
session.retract( data.cheeseHandles[3] );
session.fireAllRules();
// should not have fired as per constraint
assertEquals( 3,
data.results.size() );