private void createConstraints( BuildContext context, BuildUtils utils, Pattern pattern, List<Constraint> alphaConstraints, List<Constraint> betaConstraints) { final List<?> constraints = pattern.getConstraints(); // check if cross products for identity patterns should be disabled checkRemoveIdentities(context, pattern, betaConstraints); // checks if this pattern is nested inside a NOT CE final boolean isNegative = isNegative(context); for (final Iterator<?> it = constraints.iterator(); it.hasNext(); ) { final Object object = it.next(); // Check if its a declaration if (object instanceof Declaration) { // nothing to be done continue; } final Constraint constraint = (Constraint) object; if (constraint.getType().equals(Constraint.ConstraintType.ALPHA)) { alphaConstraints.add(constraint); } else if (constraint.getType().equals(Constraint.ConstraintType.BETA)) { betaConstraints.add(constraint); if (isNegative && context.getRuleBase().getConfiguration().getEventProcessingMode() == EventProcessingOption.STREAM && pattern.getObjectType().isEvent() && constraint.isTemporal()) { checkDelaying(context, constraint); } } else { throw new RuntimeDroolsException( "Unknown constraint type: " + constraint.getType() + ". This is a bug. Please contact development team."); } } }
@Test public void test1() { KnowledgePackage pkg1 = getKnowledgePackage1(); KnowledgePackage pkg2 = getKnowledgePackage1(); KnowledgePackage pkg3 = getKnowledgePackage2(); Rule rule1 = ((KnowledgePackageImp) pkg1).pkg.getRule("rule1"); Rule rule2 = ((KnowledgePackageImp) pkg2).pkg.getRule("rule1"); Rule rule3 = ((KnowledgePackageImp) pkg3).pkg.getRule("rule1"); // test return value Pattern p1 = (Pattern) rule1.getLhs().getChildren().get(0); VariableConstraint rvc1 = (VariableConstraint) p1.getConstraints().get(0); ReturnValueExpression rve1 = ((ReturnValueRestriction) rvc1.getRestriction()).getExpression(); Pattern p2 = (Pattern) rule2.getLhs().getChildren().get(0); VariableConstraint rvc2 = (VariableConstraint) p2.getConstraints().get(0); ReturnValueExpression rve2 = ((ReturnValueRestriction) rvc2.getRestriction()).getExpression(); assertNotSame(rve1, rve2); assertEquals(rve1, rve2); Pattern p3 = (Pattern) rule3.getLhs().getChildren().get(0); VariableConstraint rvc3 = (VariableConstraint) p3.getConstraints().get(0); ReturnValueExpression rve3 = ((ReturnValueRestriction) rvc3.getRestriction()).getExpression(); assertNotSame(rve1, rve3); assertThat(rve1, not(equalTo(rve3))); // test inline eval PredicateConstraint pc1 = (PredicateConstraint) p1.getConstraints().get(1); PredicateExpression pe1 = (PredicateExpression) pc1.getPredicateExpression(); PredicateConstraint pc2 = (PredicateConstraint) p2.getConstraints().get(1); PredicateExpression pe2 = (PredicateExpression) pc2.getPredicateExpression(); assertNotSame(pe1, pe2); assertEquals(pe1, pe2); PredicateConstraint pc3 = (PredicateConstraint) p3.getConstraints().get(1); PredicateExpression pe3 = (PredicateExpression) pc3.getPredicateExpression(); assertNotSame(pe1, pe3); assertThat(pe1, not(equalTo(pe3))); // test eval EvalCondition ec1 = (EvalCondition) rule1.getLhs().getChildren().get(1); EvalExpression ee1 = (EvalExpression) ec1.getEvalExpression(); EvalCondition ec2 = (EvalCondition) rule2.getLhs().getChildren().get(1); EvalExpression ee2 = (EvalExpression) ec2.getEvalExpression(); assertNotSame(ee1, ee2); assertEquals(ee1, ee2); EvalCondition ec3 = (EvalCondition) rule3.getLhs().getChildren().get(1); EvalExpression ee3 = (EvalExpression) ec3.getEvalExpression(); assertNotSame(ee1, ee3); assertThat(ee1, not(equalTo(ee3))); // test consequence assertNotSame(rule1.getConsequence(), rule2.getConsequence()); assertEquals(rule1.getConsequence(), rule2.getConsequence()); assertNotSame(rule1.getConsequence(), rule3.getConsequence()); assertThat(rule1.getConsequence(), not(equalTo(rule3.getConsequence()))); // check LHS equals assertNotSame(rule1.getLhs(), rule2.getLhs()); assertEquals(rule1.getLhs(), rule2.getLhs()); assertNotSame(rule1.getLhs(), rule3.getLhs()); assertThat(rule1.getLhs(), not(equalTo(rule3.getLhs()))); }
@SuppressWarnings("unchecked") @Test public void testSimpleTemplate() throws Exception { TemplateContainer tc = new TemplateContainer() { private Column[] columns = new Column[] { new LongColumn("column1"), new LongColumn("column2"), new StringColumn("column3") }; public Column[] getColumns() { return columns; } public String getHeader() { return null; } public Map<String, RuleTemplate> getTemplates() { Map<String, RuleTemplate> templates = new HashMap<String, RuleTemplate>(); RuleTemplate ruleTemplate = new RuleTemplate("template1", this); ruleTemplate.addColumn("column1 == 10"); ruleTemplate.addColumn("column2 < 5 || > 20"); ruleTemplate.addColumn("column3 == \"xyz\""); templates.put("template1", ruleTemplate); return templates; } public Column getColumn(String name) { return columns[Integer.parseInt(name.substring(6)) - 1]; } }; DefaultTemplateRuleBase ruleBase = new DefaultTemplateRuleBase(tc); Package[] packages = ruleBase.newStatefulSession().getRuleBase().getPackages(); assertEquals(1, packages.length); Map<String, String> globals = packages[0].getGlobals(); assertEquals(DefaultGenerator.class.getName(), globals.get("generator")); Rule[] rules = packages[0].getRules(); assertEquals(1, rules.length); assertEquals("template1", rules[0].getName()); GroupElement lhs = rules[0].getLhs(); // when // r : Row() // column1 : Column(name == "column1") // exists LongCell(row == r, column == column1, value == 10) // column2 : Column(name == "column2") // exists LongCell(row == r, column == column2, value < 5 | > 20) // column3 : Column(name == "column3") // exists StringCell(row == r, column == column3, value == "xyz") assertEquals(7, lhs.getChildren().size()); org.drools.rule.Pattern pattern = (org.drools.rule.Pattern) lhs.getChildren().get(1); assertEquals(1, pattern.getConstraints().size()); Constraint constraint = pattern.getConstraints().get(0); GroupElement exists = (GroupElement) lhs.getChildren().get(2); pattern = (org.drools.rule.Pattern) exists.getChildren().get(0); assertEquals(3, pattern.getConstraints().size()); IndexableConstraint vconstraint = (IndexableConstraint) pattern.getConstraints().get(1); assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass()); assertEquals("column1", vconstraint.getRequiredDeclarations()[0].getIdentifier()); pattern = (org.drools.rule.Pattern) lhs.getChildren().get(3); assertEquals(1, pattern.getConstraints().size()); constraint = pattern.getConstraints().get(0); exists = (GroupElement) lhs.getChildren().get(4); pattern = (org.drools.rule.Pattern) exists.getChildren().get(0); assertEquals(3, pattern.getConstraints().size()); vconstraint = (IndexableConstraint) pattern.getConstraints().get(1); assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass()); assertEquals("column2", vconstraint.getRequiredDeclarations()[0].getIdentifier()); pattern = (org.drools.rule.Pattern) lhs.getChildren().get(5); assertEquals(1, pattern.getConstraints().size()); constraint = pattern.getConstraints().get(0); exists = (GroupElement) lhs.getChildren().get(6); pattern = (org.drools.rule.Pattern) exists.getChildren().get(0); assertEquals(3, pattern.getConstraints().size()); vconstraint = (IndexableConstraint) pattern.getConstraints().get(1); assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass()); assertEquals("column3", vconstraint.getRequiredDeclarations()[0].getIdentifier()); }