/** * * * <pre> * * * (Cheese (price ?price ) * (Cheese (price =(* 2 ?price) ) * (Cheese (price >(* 2 ?price) ) * * * </pre> * * @throws IntrospectionException */ public void testReturnValueConstraint() throws IntrospectionException { final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase(); final InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase.newStatefulSession(); final FieldExtractor priceExtractor = cache.getExtractor(Cheese.class, "price", getClass().getClassLoader()); final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class)); // Bind the extractor to a decleration // Declarations know the pattern they derive their value form final Declaration priceDeclaration = new Declaration("price1", priceExtractor, pattern); final ReturnValueExpression isDoubleThePrice = new ReturnValueExpression() { /** */ private static final long serialVersionUID = 400L; public FieldValue evaluate( Object object, Tuple tuple, // ?price Declaration[] previousDeclarations, Declaration[] localDeclarations, WorkingMemory workingMemory, Object context) { int price = ((Number) previousDeclarations[0].getValue( (InternalWorkingMemory) workingMemory, workingMemory.getObject(tuple.get(previousDeclarations[0])))) .intValue(); return FieldFactory.getFieldValue(2 * price); } public Object createContext() { return null; } }; final ReturnValueRestriction restriction1 = new ReturnValueRestriction( priceExtractor, isDoubleThePrice, new Declaration[] {priceDeclaration}, new Declaration[0], new String[0], ValueType.INTEGER_TYPE.getEvaluator(Operator.EQUAL)); final ReturnValueConstraint constraint1 = new ReturnValueConstraint(priceExtractor, restriction1); final ReturnValueRestriction restriction2 = new ReturnValueRestriction( priceExtractor, isDoubleThePrice, new Declaration[] {priceDeclaration}, new Declaration[0], new String[0], ValueType.INTEGER_TYPE.getEvaluator(Operator.GREATER)); final ReturnValueConstraint constraint2 = new ReturnValueConstraint(priceExtractor, restriction2); final Cheese cheddar0 = new Cheese("cheddar", 5); final FactHandle f0 = workingMemory.insert(cheddar0); InstrumentedReteTuple tuple = new InstrumentedReteTuple(f0); final Cheese cheddar1 = new Cheese("cheddar", 10); final InternalFactHandle f1 = (InternalFactHandle) workingMemory.insert(cheddar1); tuple = new InstrumentedReteTuple(tuple, f1); final ReturnValueContextEntry context1 = (ReturnValueContextEntry) constraint1.createContextEntry(); context1.updateFromTuple(workingMemory, tuple); assertTrue(constraint1.isAllowedCachedLeft(context1, f1.getObject())); final ReturnValueContextEntry context2 = (ReturnValueContextEntry) constraint2.createContextEntry(); context2.updateFromTuple(workingMemory, tuple); assertFalse(constraint2.isAllowedCachedLeft(context2, f1.getObject())); final Cheese cheddar2 = new Cheese("cheddar", 11); final InternalFactHandle f2 = (InternalFactHandle) workingMemory.insert(cheddar2); assertTrue(constraint2.isAllowedCachedLeft(context2, f2.getObject())); }
/** * * * <pre> * * * (Cheese (price ?price ) * (Cheese (price =(* 2 ?price) ) * (Cheese (price >(* 2 ?price) ) * * * </pre> * * @throws IntrospectionException */ @Test public void testReturnValueConstraint() throws IntrospectionException { final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase(); final InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase.newStatefulSession(); final InternalReadAccessor priceExtractor = store.getReader(Cheese.class, "price", getClass().getClassLoader()); final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class)); // Bind the extractor to a decleration // Declarations know the pattern they derive their value form final Declaration priceDeclaration = new Declaration("price1", priceExtractor, pattern); final ReturnValueExpression isDoubleThePrice = new ReturnValueExpression() { private static final long serialVersionUID = 510l; public FieldValue evaluate( Object object, Tuple tuple, // ?price Declaration[] previousDeclarations, Declaration[] localDeclarations, WorkingMemory workingMemory, Object context) { int price = ((Number) previousDeclarations[0].getValue( (InternalWorkingMemory) workingMemory, workingMemory.getObject(tuple.get(previousDeclarations[0])))) .intValue(); return FieldFactory.getFieldValue(2 * price); } public Object createContext() { return null; } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {} public void writeExternal(ObjectOutput out) throws IOException {} public void replaceDeclaration(Declaration declaration, Declaration resolved) {} }; final ReturnValueRestriction restriction1 = new ReturnValueRestriction( priceExtractor, isDoubleThePrice, new Declaration[] {priceDeclaration}, new Declaration[0], new String[0], equals.getEvaluator(ValueType.INTEGER_TYPE, Operator.EQUAL)); final ReturnValueConstraint constraint1 = new ReturnValueConstraint(priceExtractor, restriction1); final ReturnValueRestriction restriction2 = new ReturnValueRestriction( priceExtractor, isDoubleThePrice, new Declaration[] {priceDeclaration}, new Declaration[0], new String[0], comparables.getEvaluator(ValueType.INTEGER_TYPE, Operator.GREATER)); final ReturnValueConstraint constraint2 = new ReturnValueConstraint(priceExtractor, restriction2); final Cheese cheddar0 = new Cheese("cheddar", 5); final InternalFactHandle f0 = (InternalFactHandle) workingMemory.insert(cheddar0); LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true); final Cheese cheddar1 = new Cheese("cheddar", 10); final InternalFactHandle f1 = (InternalFactHandle) workingMemory.insert(cheddar1); tuple = new LeftTupleImpl(tuple, new RightTuple(f1, null), null, true); final ReturnValueContextEntry context1 = (ReturnValueContextEntry) constraint1.createContextEntry(); context1.updateFromTuple(workingMemory, tuple); assertTrue(constraint1.isAllowedCachedLeft(context1, f1)); final ReturnValueContextEntry context2 = (ReturnValueContextEntry) constraint2.createContextEntry(); context2.updateFromTuple(workingMemory, tuple); assertFalse(constraint2.isAllowedCachedLeft(context2, f1)); final Cheese cheddar2 = new Cheese("cheddar", 11); final InternalFactHandle f2 = (InternalFactHandle) workingMemory.insert(cheddar2); assertTrue(constraint2.isAllowedCachedLeft(context2, f2)); }