/** * * * <pre> * * * ( Cheese (type "cheddar") ) * * * </pre> * * This is currently the same as using a ReturnValueConstraint just that it doesn't need any * requiredDeclarations * * @throws IntrospectionException */ public void testLiteralConstraint() throws IntrospectionException { final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase(); final InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase.newStatefulSession(); final ClassFieldExtractor extractor = cache.getExtractor(Cheese.class, "type", getClass().getClassLoader()); final FieldValue field = FieldFactory.getFieldValue("cheddar"); final Evaluator evaluator = ValueType.STRING_TYPE.getEvaluator(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.getObject(), workingMemory, context)); final Cheese stilton = new Cheese("stilton", 5); final InternalFactHandle stiltonHandle = (InternalFactHandle) workingMemory.insert(stilton); // check constraint assertFalse(constraint.isAllowed(stiltonHandle.getObject(), workingMemory, context)); }
/** * * * <pre> * * * type == "cheddar" && price > 10 * * * </pre> * * Test the use of the composite AND constraint. Composite AND constraints are only used when * nested inside other field constraints, as the top level AND is implicit * * @throws IntrospectionException */ public void testCompositeAndConstraint() { final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase(); final InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase.newStatefulSession(); final ClassFieldExtractor extractor = cache.getExtractor(Cheese.class, "type", getClass().getClassLoader()); final FieldValue field = FieldFactory.getFieldValue("cheddar"); final Evaluator evaluator = ValueType.STRING_TYPE.getEvaluator(Operator.EQUAL); final LiteralConstraint constraint1 = new LiteralConstraint(extractor, evaluator, field); final ClassFieldExtractor priceExtractor = cache.getExtractor(Cheese.class, "price", getClass().getClassLoader()); final FieldValue priceField = FieldFactory.getFieldValue(10); final Evaluator priceEvaluator = ValueType.INTEGER_TYPE.getEvaluator(Operator.GREATER); final LiteralConstraint constraint2 = new LiteralConstraint(priceExtractor, priceEvaluator, priceField); final Cheese cheddar = new Cheese("cheddar", 15); final AndConstraint constraint = new AndConstraint(); constraint.addAlphaConstraint(constraint1); constraint.addAlphaConstraint(constraint2); final ContextEntry context = constraint.createContextEntry(); final InternalFactHandle cheddarHandle = (InternalFactHandle) workingMemory.insert(cheddar); // check constraint assertTrue(constraint.isAllowed(cheddarHandle.getObject(), workingMemory, context)); cheddar.setPrice(5); ((ShadowProxy) cheddarHandle.getObject()).updateProxy(); assertFalse(constraint.isAllowed(cheddarHandle.getObject(), workingMemory, context)); cheddar.setType("stilton"); ((ShadowProxy) cheddarHandle.getObject()).updateProxy(); assertFalse(constraint.isAllowed(cheddarHandle.getObject(), workingMemory, context)); cheddar.setPrice(15); ((ShadowProxy) cheddarHandle.getObject()).updateProxy(); assertFalse(constraint.isAllowed(cheddarHandle.getObject(), workingMemory, context)); }
/** * * * <pre> * * * Cheese( ( type == "cheddar" && price > 10) || ( type == "e;stilton"e; && price < 10 ) ) * * * </pre> * * Test the use of the composite OR constraint. * * @throws IntrospectionException */ public void testNestedCompositeConstraints() { final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase(); final InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase.newStatefulSession(); final ClassFieldExtractor typeExtractor = cache.getExtractor(Cheese.class, "type", getClass().getClassLoader()); final FieldValue cheddarField = FieldFactory.getFieldValue("cheddar"); final Evaluator stringEqual = ValueType.STRING_TYPE.getEvaluator(Operator.EQUAL); // type == 'cheddar' final LiteralConstraint constraint1 = new LiteralConstraint(typeExtractor, stringEqual, cheddarField); final ClassFieldExtractor priceExtractor = cache.getExtractor(Cheese.class, "price", getClass().getClassLoader()); final FieldValue field10 = FieldFactory.getFieldValue(10); final Evaluator integerGreater = ValueType.INTEGER_TYPE.getEvaluator(Operator.GREATER); // price > 10 final LiteralConstraint constraint2 = new LiteralConstraint(priceExtractor, integerGreater, field10); // type == 'cheddar' && price > 10 final AndConstraint and1 = new AndConstraint(); and1.addAlphaConstraint(constraint1); and1.addAlphaConstraint(constraint2); final FieldValue stiltonField = FieldFactory.getFieldValue("stilton"); // type == 'stilton' final LiteralConstraint constraint3 = new LiteralConstraint(typeExtractor, stringEqual, stiltonField); final Evaluator integerLess = ValueType.INTEGER_TYPE.getEvaluator(Operator.LESS); // price < 10 final LiteralConstraint constraint4 = new LiteralConstraint(priceExtractor, integerLess, field10); // type == 'stilton' && price < 10 final AndConstraint and2 = new AndConstraint(); and2.addAlphaConstraint(constraint3); and2.addAlphaConstraint(constraint4); // ( type == 'cheddar' && price > 10 ) || ( type == 'stilton' && price < 10 ) final OrConstraint constraint = new OrConstraint(); constraint.addAlphaConstraint(and1); constraint.addAlphaConstraint(and2); final ContextEntry context = constraint.createContextEntry(); final Cheese cheddar = new Cheese("cheddar", 15); final InternalFactHandle cheddarHandle = (InternalFactHandle) workingMemory.insert(cheddar); // check constraint assertTrue(constraint.isAllowed(cheddarHandle.getObject(), workingMemory, context)); cheddar.setPrice(5); ((ShadowProxy) cheddarHandle.getObject()).updateProxy(); assertFalse(constraint.isAllowed(cheddarHandle.getObject(), workingMemory, context)); cheddar.setType("stilton"); ((ShadowProxy) cheddarHandle.getObject()).updateProxy(); assertTrue(constraint.isAllowed(cheddarHandle.getObject(), workingMemory, context)); cheddar.setPrice(15); ((ShadowProxy) cheddarHandle.getObject()).updateProxy(); assertFalse(constraint.isAllowed(cheddarHandle.getObject(), workingMemory, context)); }
public void testQueryTerminalNode() { final ClassObjectType queryObjectType = new ClassObjectType(DroolsQuery.class); final ObjectTypeNode queryObjectTypeNode = new ObjectTypeNode(this.buildContext.getNextId(), queryObjectType, buildContext); queryObjectTypeNode.attach(); ClassFieldExtractor extractor = cache.getExtractor(DroolsQuery.class, "name", DroolsQuery.class.getClassLoader()); FieldValue field = FieldFactory.getFieldValue("query-1"); final Evaluator evaluator = ValueType.STRING_TYPE.getEvaluator(Operator.EQUAL); LiteralConstraint constraint = new LiteralConstraint(extractor, evaluator, field); AlphaNode alphaNode = new AlphaNode(this.buildContext.getNextId(), constraint, queryObjectTypeNode, buildContext); alphaNode.attach(); final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(this.buildContext.getNextId(), alphaNode, this.buildContext); liaNode.attach(); final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class); final ObjectTypeNode cheeseObjectTypeNode = new ObjectTypeNode(this.buildContext.getNextId(), cheeseObjectType, buildContext); cheeseObjectTypeNode.attach(); extractor = cache.getExtractor(Cheese.class, "type", getClass().getClassLoader()); field = FieldFactory.getFieldValue("stilton"); constraint = new LiteralConstraint(extractor, evaluator, field); alphaNode = new AlphaNode( this.buildContext.getNextId(), constraint, cheeseObjectTypeNode, buildContext); alphaNode.attach(); BuildContext buildContext = new BuildContext(ruleBase, ruleBase.getReteooBuilder().getIdGenerator()); buildContext.setTupleMemoryEnabled(false); final JoinNode joinNode = new JoinNode( this.buildContext.getNextId(), liaNode, alphaNode, EmptyBetaConstraints.getInstance(), buildContext); joinNode.attach(); final Query query = new Query("query-1"); final QueryTerminalNode queryNode = new QueryTerminalNode(this.buildContext.getNextId(), joinNode, query, query.getLhs()); queryNode.attach(); final org.drools.rule.Package pkg = new org.drools.rule.Package("com.drools.test"); pkg.addRule(query); try { final Field pkgField = ruleBase.getClass().getSuperclass().getDeclaredField("pkgs"); pkgField.setAccessible(true); final Map pkgs = (Map) pkgField.get(ruleBase); pkgs.put(pkg.getName(), pkg); } catch (final Exception e) { Assert.fail("Should not throw any exception: " + e.getMessage()); } final WorkingMemory workingMemory = ruleBase.newStatefulSession(); QueryResults results = workingMemory.getQueryResults("query-1"); assertEquals(0, results.size()); final Cheese stilton1 = new Cheese("stilton", 100); final FactHandle handle1 = workingMemory.insert(stilton1); results = workingMemory.getQueryResults("query-1"); assertEquals(1, results.size()); final Cheese cheddar = new Cheese("cheddar", 55); workingMemory.insert(cheddar); results = workingMemory.getQueryResults("query-1"); assertEquals(1, results.size()); final Cheese stilton2 = new Cheese("stilton", 5); final FactHandle handle2 = workingMemory.insert(stilton2); results = workingMemory.getQueryResults("query-1"); assertEquals(2, results.size()); QueryResult result = results.get(0); assertEquals(1, result.size()); assertEquals(stilton2, result.get(0)); result = results.get(1); assertEquals(1, result.size()); assertEquals(stilton1, result.get(0)); int i = 0; for (final Iterator it = results.iterator(); it.hasNext(); ) { result = (QueryResult) it.next(); assertEquals(1, result.size()); if (i == 1) { assertSame(stilton1, result.get(0)); } else { assertSame(stilton2, result.get(0)); } i++; } workingMemory.retract(handle1); results = workingMemory.getQueryResults("query-1"); assertEquals(1, results.size()); workingMemory.retract(handle2); results = workingMemory.getQueryResults("query-1"); assertEquals(0, results.size()); }