* really is actually a constraint, ie making sure it has the same value as
* derived from the first column's type field.
*/
public void testDoubleBoundConstraint()
{
InstrumentedWorkingMemoryImpl workingMemory = new InstrumentedWorkingMemoryImpl( );
ObjectType stringObjectType = new ClassObjectType( String.class );
/* Determines how the bound value is extracted from the column */
Extractor typeOfCheeseExtractor = new Extractor( ) {
public Object getValue(Object object)
{
return ((Cheese) object).getType( );
}
};
/* Bind the extractor to a decleration */
/* Declarations know the column they derive their value form */
Declaration typeOfCheeseDeclaration = new Declaration( 0,
"typeOfCheese",
stringObjectType,
typeOfCheeseExtractor,
0 );
ReturnValueExpressionConstraint isCheddar = new ReturnValueExpressionConstraint( ) {
public boolean isAllowed(Object object,
FactHandle handle,
Declaration[] declarations,
Tuple tuple,
ConstraintComparator comparator)
{
return comparator.compare( ((Cheese) object).getType( ),
tuple.get( declarations[0] ) );
}
};
/*
* Creates a constraint with an expression and an array of required
* declarations
*/
ReturnValueConstraint constraint1 = new ReturnValueConstraint( isCheddar,
new Declaration[]{typeOfCheeseDeclaration},
new StringConstraintComparator( ConstraintComparator.EQUAL ) );
Cheese cheddar0 = new Cheese( "cheddar",
5 );
FactHandle f0 = workingMemory.createFactHandle( 0 );
workingMemory.putObject( f0,
cheddar0 );
InstrumentedReteTuple tuple = new InstrumentedReteTuple( 0,
f0,
workingMemory );
Cheese cheddar1 = new Cheese( "cheddar",
5 );
FactHandle f1 = workingMemory.createFactHandle( 1 );
workingMemory.putObject( f1,
cheddar1 );
tuple = new InstrumentedReteTuple( tuple,
new InstrumentedReteTuple( 1,
f1,
workingMemory ) );
assertTrue( constraint1.isAllowed( cheddar1,
f1,
tuple ) );
cheddar1 = new Cheese( "stilton",
5 );
workingMemory.putObject( f1,
cheddar1 );
/*
* simulate a modify, so we can check for a false assertion.
*/