List results = new ArrayList();
StatefulSession session = ruleBase.newStatefulSession();
session.setGlobal( "results",
results );
InternalFactHandle stilton1 = (InternalFactHandle) session.insert( new Cheese( "stilton",
10 ) );
InternalFactHandle brie1 = (InternalFactHandle) session.insert( new Cheese( "brie",
10 ) );
session.fireAllRules();
byte[] serializedSession = serializeOut( session );
byte[] serializedRulebase = serializeOut( ruleBase );
session.dispose();
assertEquals( 1,
results.size() );
assertEquals( stilton1.getObject(),
results.get( 0 ) );
// now recreate the rulebase, deserialize the session and test it
ruleBase = (RuleBase) serializeIn( serializedRulebase );
session = ruleBase.newStatefulSession( new ByteArrayInputStream( serializedSession ) );
results = (List) session.getGlobal( "results" );
builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_Dynamic3_0.drl" ) ) );
pkg = serialisePackage( builder.getPackage() );
ruleBase.addPackage( pkg );
InternalFactHandle stilton2 = (InternalFactHandle) session.insert( new Cheese( "stilton", 20 ) );
InternalFactHandle brie2 = (InternalFactHandle) session.insert( new Cheese( "brie", 20 ) );
InternalFactHandle bob1 = (InternalFactHandle) session.insert( new Person( "bob", 20 ) );
InternalFactHandle bob2 = (InternalFactHandle) session.insert( new Person( "bob", 30 ) );
session.fireAllRules();
assertEquals( 4,
results.size() );
assertEquals( stilton2.getObject(),
results.get( 1 ) );
assertEquals( bob2.getObject(),
results.get( 2 ) );
assertEquals( bob1.getObject(),
results.get( 3 ) );
serializedSession = null;
serializedRulebase = null;
serializedSession = serializeOut( session );
serializedRulebase = serializeOut( ruleBase );
session.dispose();
// now recreate the rulebase, deserialize the session and test it
ruleBase = (RuleBase) serializeIn( serializedRulebase );
session = ruleBase.newStatefulSession( new ByteArrayInputStream( serializedSession ) );
results = (List) session.getGlobal( "results" );
builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_Dynamic1_2.drl" ) ) );
pkg = serialisePackage( builder.getPackage() );
ruleBase.addPackage( pkg );
InternalFactHandle stilton3 = (InternalFactHandle) session.insert( new Cheese( "stilton", 40 ) );
InternalFactHandle brie3 = (InternalFactHandle) session.insert( new Cheese( "brie", 40 ) );
InternalFactHandle bob3 = (InternalFactHandle) session.insert( new Person( "bob", 40 ) );
InternalFactHandle bob4 = (InternalFactHandle) session.insert( new Person( "bob", 40 ) );
InternalFactHandle addr1 = (InternalFactHandle) session.insert( new Address( "bangalore" ) );
InternalFactHandle addr2 = (InternalFactHandle) session.insert( new Address( "India" ) );
session.fireAllRules();
assertEquals( 9,
results.size() );
assertEquals( stilton3.getObject(),
results.get( 4 ) );
assertEquals( bob4.getObject(),
results.get( 5 ) );
assertEquals( bob3.getObject(),
results.get( 6 ) );
assertEquals( addr2.getObject(),
results.get( 7 ) );
assertEquals( addr1.getObject(),
results.get( 8 ) );
serializedSession = null;
serializedRulebase = null;
serializedSession = serializeOut( session );
serializedRulebase = serializeOut( ruleBase );
session.dispose();
// now recreate the rulebase, deserialize the session and test it
ruleBase = (RuleBase) serializeIn( serializedRulebase );
session = ruleBase.newStatefulSession( new ByteArrayInputStream( serializedSession ) );
results = (List) session.getGlobal( "results" );
InternalFactHandle stilton4 = (InternalFactHandle) session.insert( new Cheese( "stilton", 50 ) );
InternalFactHandle brie4 = (InternalFactHandle) session.insert( new Cheese( "brie", 50 ) );
InternalFactHandle bob5 = (InternalFactHandle) session.insert( new Person( "bob", 50 ) );
InternalFactHandle bob6 = (InternalFactHandle) session.insert( new Person( "bob", 50 ) );
InternalFactHandle addr3 = (InternalFactHandle) session.insert( new Address( "Tripura" ) );
InternalFactHandle addr4 = (InternalFactHandle) session.insert( new Address( "Agartala" ) );
session.fireAllRules();
assertEquals( 14,
results.size() );
assertEquals( stilton4.getObject(),
results.get( 9 ) );
assertEquals( bob6.getObject(),
results.get( 10 ) );
assertEquals( bob5.getObject(),
results.get( 11 ) );
assertEquals( addr4.getObject(),
results.get( 12 ) );
assertEquals( addr3.getObject(),
results.get( 13 ) );
serializedSession = null;