accDescr.setActionCode( "total += $cheese.price;" );
accDescr.setReverseCode( "total -= $cheese.price;" );
accDescr.setResultCode( "new Integer(total)" );
final MVELAccumulateBuilder builder = new MVELAccumulateBuilder();
final Accumulate acc = (Accumulate) builder.build( context,
accDescr );
((MVELCompileable) acc.getAccumulators()[0]).compile( (MVELDialectRuntimeData) pkgBuilder.getPackageRegistry( pkg.getName() ).getDialectRuntimeRegistry().getDialectData( "mvel" ) );
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newStatefulKnowledgeSession();
MockLeftTupleSink sink = new MockLeftTupleSink();
final Cheese cheddar1 = new Cheese( "cheddar",
10 );
final Cheese cheddar2 = new Cheese( "cheddar",
8 );
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert( new InitialFactImpl() );
final InternalFactHandle f1 = (InternalFactHandle) ksession.insert( cheddar1 );
final InternalFactHandle f2 = (InternalFactHandle) ksession.insert( cheddar2 );
final LeftTupleImpl tuple = new LeftTupleImpl( f0,
sink,
true );
Object wmContext = acc.createWorkingMemoryContext();
Object accContext = acc.createContext();
acc.init( wmContext,
accContext,
tuple,
ksession );
acc.accumulate( wmContext,
accContext,
tuple,
f1,
ksession );
acc.accumulate( wmContext,
accContext,
tuple,
f2,
ksession );
assertEquals( new Integer( 18 ),
acc.getResult( wmContext,
accContext,
tuple,
ksession ) );
acc.reverse( wmContext,
accContext,
tuple,
f1,
ksession );
assertEquals( new Integer( 8 ),
acc.getResult( wmContext,
accContext,
tuple,
ksession ) );
}