/** * * * <pre> * * * ( Cheese (type "cheddar") ) * * * </pre> * * This is currently the same as using a ReturnValueConstraint just that it doesn't need any * requiredDeclarations * * @throws IntrospectionException */ @Test public void testLiteralConstraint() throws IntrospectionException { final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase(); final InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase.newStatefulSession(); final ClassFieldReader extractor = store.getReader(Cheese.class, "type", getClass().getClassLoader()); final FieldValue field = FieldFactory.getFieldValue("cheddar"); final Evaluator evaluator = equals.getEvaluator(ValueType.STRING_TYPE, Operator.EQUAL); final LiteralConstraint constraint = new LiteralConstraint(extractor, evaluator, field); final ContextEntry context = constraint.createContextEntry(); final Cheese cheddar = new Cheese("cheddar", 5); final InternalFactHandle cheddarHandle = (InternalFactHandle) workingMemory.insert(cheddar); // check constraint assertTrue(constraint.isAllowed(cheddarHandle, workingMemory, context)); final Cheese stilton = new Cheese("stilton", 5); final InternalFactHandle stiltonHandle = (InternalFactHandle) workingMemory.insert(stilton); // check constraint assertFalse(constraint.isAllowed(stiltonHandle, workingMemory, context)); }
/** * * * <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)); }