📄 constrainttest.java
字号:
* <pre>
*
*
* (Cheese (price ?price1 )
* (Cheese (price ?price2&:(= ?price2 (* 2 ?price1) )
*
*
* </pre>
*/
public void testBooleanExpressionConstraint()
{
InstrumentedWorkingMemoryImpl workingMemory = new InstrumentedWorkingMemoryImpl( );
ObjectType integerObjectType = new ClassObjectType( Integer.class );
/* Determines how the bound value is extracted from the column */
Extractor priceOfCheeseExtractor = new Extractor( ) {
public Object getValue(Object object)
{
return new Integer( ((Cheese) object).getPrice( ) );
}
};
/* Bind the extractor to a decleration */
/* Declarations know the column they derive their value form */
Declaration price1Declaration = new Declaration( 0,
"price1",
integerObjectType,
priceOfCheeseExtractor,
0 );
/* Bind the extractor to a decleration */
/* Declarations know the column they derive their value form */
Declaration price2Declaration = new Declaration( 1,
"price2",
integerObjectType,
priceOfCheeseExtractor,
1 );
BooleanExpressionConstraint isDoubleThePrice = new BooleanExpressionConstraint( ) {
public boolean isAllowed(Object object,
FactHandle handle,
Declaration declaration, // ?price2
Declaration[] declarations, // ?price1
Tuple tuple)
{
int price1 = ((Integer) tuple.get( declarations[0] )).intValue( );
int price2 = ((Integer) tuple.get( declaration )).intValue( );
return (price2 == (price1 * 2));
}
};
BooleanConstraint constraint1 = new BooleanConstraint( isDoubleThePrice,
price2Declaration,
new Declaration[]{price1Declaration} );
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",
10 );
FactHandle f1 = workingMemory.createFactHandle( 1 );
workingMemory.putObject( f1,
cheddar1 );
tuple = new InstrumentedReteTuple( tuple,
new InstrumentedReteTuple( 1,
f1,
workingMemory ) );
assertTrue( constraint1.isAllowed( cheddar1,
f1,
tuple ) );
}
/**
* <pre>
*
*
* (Cheese (price ?price )
* (Cheese (price =(* 2 ?price) )
* (Cheese (price >(* 2 ?price) )
*
*
* </pre>
*/
public void testReturnValueConstraint()
{
InstrumentedWorkingMemoryImpl workingMemory = new InstrumentedWorkingMemoryImpl( );
ObjectType integerObjectType = new ClassObjectType( Integer.class );
/* Determines how the bound value is extracted from the column */
Extractor priceOfCheeseExtractor = new Extractor( ) {
public Object getValue(Object object)
{
return new Integer( ((Cheese) object).getPrice( ) );
}
};
/* Bind the extractor to a decleration */
/* Declarations know the column they derive their value form */
Declaration priceDeclaration = new Declaration( 0,
"price",
integerObjectType,
priceOfCheeseExtractor,
0 );
ReturnValueExpressionConstraint isDoubleThePrice = new ReturnValueExpressionConstraint( ) {
public boolean isAllowed(Object object,
FactHandle handle,
Declaration[] declarations, // ?price
Tuple tuple,
ConstraintComparator comparator)
{
int price = ((Integer) tuple.get( declarations[0] )).intValue( );
return comparator.compare( new Integer( ((Cheese) object).getPrice( ) ),
new Integer( 2 * price ) );
}
};
ReturnValueConstraint constraint1 = new ReturnValueConstraint( isDoubleThePrice,
new Declaration[]{priceDeclaration},
new NumericConstraintComparator( ConstraintComparator.EQUAL ) );
ReturnValueConstraint constraint2 = new ReturnValueConstraint( isDoubleThePrice,
new Declaration[]{priceDeclaration},
new NumericConstraintComparator( ConstraintComparator.GREATER ) );
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",
10 );
FactHandle f1 = workingMemory.createFactHandle( 1 );
workingMemory.putObject( f1,
cheddar1 );
tuple = new InstrumentedReteTuple( tuple,
new InstrumentedReteTuple( 0,
f1,
workingMemory ) );
assertTrue( constraint1.isAllowed( cheddar1,
f1,
tuple ) );
assertFalse( constraint2.isAllowed( cheddar1,
f1,
tuple ) );
}
static public class Cheese
{
private String type;
private int price;
public Cheese(String type,
int price)
{
this.type = type;
this.price = price;
}
public String getType()
{
return this.type;
}
public int getPrice()
{
return this.price;
}
public boolean equals(Object object)
{
return this.type.equals( ((Cheese) object).getType( ) );
}
}
static public class Person
{
private String name;
public Person(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -