List results = new ArrayList();
StatefulSession session = ruleBase.newStatefulSession();
session.setGlobal( "results",
results );
InternalFactHandle stilton1 = (InternalFactHandle) session.insert( new Cheese( "stilton",
10 ) );
session.insert( new Cheese( "brie",
10 ) );
InternalFactHandle bob = (InternalFactHandle) session.insert( new Person( "bob",
10 ) );
// fire rules
session.fireAllRules();
// check the results are correct
assertEquals( 1,
results.size() );
assertEquals( stilton1.getObject(),
results.get( 0 ) );
// serialize session and rulebase out
byte[] serializedSession = serializeOut( session );
byte[] serializedRulebase = serializeOut( ruleBase );
session.dispose();
// now deserialize the rulebase, deserialize the session and test it
ruleBase = (RuleBase) serializeIn( serializedRulebase );
session = ruleBase.newStatefulSession( new ByteArrayInputStream( serializedSession ) );
// dynamically add a new package
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 ) );
session.insert( new Cheese( "brie",
20 ) );
InternalFactHandle mark = (InternalFactHandle) session.insert( new Person( "mark",
20 ) );
session.fireAllRules();