reteooRuleBase.getPackages().length );
}
}
public void testDynamicFunction() throws Exception {
PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_DynamicFunction1.drl" ) ) );
//FIXME JBRULES-1258 serialising a package breaks function removal -- left the serialisation commented out for now
//final Package pkg = serialisePackage( builder.getPackage() );
final Package pkg = builder.getPackage();
final RuleBase ruleBase = getRuleBase();
ruleBase.addPackage( pkg );
final WorkingMemory workingMemory = ruleBase.newStatefulSession();
final List list = new ArrayList();
workingMemory.setGlobal( "list",
list );
final Cheese stilton = new Cheese( "stilton",
5 );
workingMemory.insert( stilton );
workingMemory.fireAllRules();
assertEquals( new Integer( 5 ),
list.get( 0 ) );
// Check a function can be removed from a package.
// Once removed any efforts to use it should throw an Exception
ruleBase.removeFunction( "org.drools.test",
"addFive" );
final Cheese cheddar = new Cheese( "cheddar",
5 );
workingMemory.insert( cheddar );
try {
workingMemory.fireAllRules();
fail( "Function should have been removed and NoClassDefFoundError thrown from the Consequence" );
} catch ( final NoClassDefFoundError e ) {
}
// Check a new function can be added to replace an old function
builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_DynamicFunction2.drl" ) ) );
ruleBase.addPackage( serialisePackage( builder.getPackage() ) );
final Cheese brie = new Cheese( "brie",
5 );
workingMemory.insert( brie );
workingMemory.fireAllRules();
assertEquals( new Integer( 6 ),
list.get( 1 ) );
builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_DynamicFunction3.drl" ) ) );
ruleBase.addPackage( serialisePackage( builder.getPackage() ) );
final Cheese feta = new Cheese( "feta",
5 );
workingMemory.insert( feta );