[jboss-svn-commits] JBL Code SVN: r32314 - in labs/jbossrules/trunk/drools-factconstraint/src: main/java/org/drools/factconstraints/server/predefined and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Mar 30 16:20:29 EDT 2010
Author: eaa
Date: 2010-03-30 16:20:27 -0400 (Tue, 30 Mar 2010)
New Revision: 32314
Added:
labs/jbossrules/trunk/drools-factconstraint/src/test/java/org/drools/factconstraint/RangeConstraintMultipleOperatorsTest.java
labs/jbossrules/trunk/drools-factconstraint/src/test/resources/RangeConstraintMultiOperator.drl
Modified:
labs/jbossrules/trunk/drools-factconstraint/src/main/java/org/drools/factconstraint/server/DefaultConstraintImpl.java
labs/jbossrules/trunk/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/RangeConstraint.java
labs/jbossrules/trunk/drools-factconstraint/src/test/java/org/drools/factconstraint/RangeConstraintSingleOperatorTest.java
Log:
Merge between Multiple Operators support and package's name change
Modified: labs/jbossrules/trunk/drools-factconstraint/src/main/java/org/drools/factconstraint/server/DefaultConstraintImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-factconstraint/src/main/java/org/drools/factconstraint/server/DefaultConstraintImpl.java 2010-03-30 19:55:23 UTC (rev 32313)
+++ labs/jbossrules/trunk/drools-factconstraint/src/main/java/org/drools/factconstraint/server/DefaultConstraintImpl.java 2010-03-30 20:20:27 UTC (rev 32314)
@@ -32,7 +32,7 @@
private String concatRule(ConstraintConfiguration config, Map<String, Object> context) {
StringBuilder rule = new StringBuilder();
-
+
rule.append(this.getVerifierPackagePrefixTemplate(config, context));
rule.append(this.getVerifierPackageTemplate(config, context));
rule.append(this.getVerifierPackageSufixTemplate(config, context));
@@ -119,15 +119,15 @@
}
protected Map<String, Object> createContext() {
- return new HashMap<String, Object>();
+ return new HashMap<String, Object>();
}
-
+
public final String getVerifierRule(ConstraintConfiguration config) {
return internalVerifierRule(config, createContext());
}
abstract protected String internalVerifierRule(ConstraintConfiguration config, Map<String, Object> context);
-
+
public String getConstraintName() {
return getClass().getName().substring(getClass().getName().lastIndexOf('.') + 1);
}
@@ -290,7 +290,7 @@
protected String getVerifierRuleWhenTemplate(ConstraintConfiguration config, Map<String, Object> context) {
return " when\n";
}
-
+
public List<String> getArgumentKeys() {
return new ArrayList<String>();
}
Modified: labs/jbossrules/trunk/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/RangeConstraint.java
===================================================================
--- labs/jbossrules/trunk/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/RangeConstraint.java 2010-03-30 19:55:23 UTC (rev 32313)
+++ labs/jbossrules/trunk/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/RangeConstraint.java 2010-03-30 20:20:27 UTC (rev 32314)
@@ -19,140 +19,159 @@
*/
public class RangeConstraint extends DefaultMultiRulesConstraintImpl {
- private static final long serialVersionUID = 501L;
- public static final String NAME = "RangeConstraint";
+ private static final long serialVersionUID = 501L;
+ public static final String NAME = "RangeConstraint";
+ public static final String RANGE_CONSTRAINT_MIN = "Min.value";
+ public static final String RANGE_CONSTRAINT_MAX = "Max.value";
+ public static final String CURRENT_OPERATOR = "currOperator";
- public static final String RANGE_CONSTRAINT_MIN = "Min.value";
- public static final String RANGE_CONSTRAINT_MAX = "Max.value";
-
- public static final String CURRENT_OPERATOR = "currOperator";
-
// private Operator currentOperator;
+ @Override
+ protected String internalVerifierRule(ConstraintConfiguration config, Map<String, Object> context) {
- @Override
- protected String internalVerifierRule(ConstraintConfiguration config, Map<String, Object> context) {
+ this.resetRuleCount(context);
- this.resetRuleCount(context);
+ StringBuilder rules = new StringBuilder();
+ for (Operator operator : RangeConstraint.supportedOperators) {
+ setCurrentOperator(context, operator);
+ rules.append(this.createVerifierRuleTemplate(config, context,
+ "Range_Field_Constraint_" + operator.getOperatorString(),
+ Collections.<String>emptyList(), this.getResultMessage(config, context)));
+ this.incrementRuleCount(context);
+ }
- StringBuilder rules = new StringBuilder();
- for (Operator operator : RangeConstraint.supportedOperators) {
- setCurrentOperator(context, operator);
- rules.append(this.createVerifierRuleTemplate(config, context,
- "Range_Field_Constraint_" + operator.getOperatorString(),
- Collections.<String> emptyList(), this.getResultMessage(config, context)));
- this.incrementRuleCount(context);
- }
+ return rules.toString();
+ }
- return rules.toString();
- }
+ private String getResultMessage(ConstraintConfiguration conf, Map<String, Object> context) {
+ if (getCurrentOperator(context).equals(Operator.NOT_EQUAL)) {
+ return "Impossible value. Possible values are from " + getMin(conf) + " to " + getMax(conf); //I18N
+ } else if (!getCurrentOperator(context).equals(Operator.EQUAL)) {
+ return "Missing range"; //I18N
+ } else {
+ return "The value must be between " + getMin(conf) + " and " + getMax(conf); //I18N
+ }
+ }
- private String getResultMessage(ConstraintConfiguration conf, Map<String, Object> context) {
- if (getCurrentOperator(context).getOperatorString().equals(Operator.NOT_EQUAL.getOperatorString())) {
- return "The value must be between " + getMin(conf) + " and " + getMax(conf); // I18N
- } else {
- return "The value must be between " + getMin(conf) + " and " + getMax(conf); // I18N
- }
- }
+ @Override
+ public ValidationResult validate(Object value, ConstraintConfiguration config) {
+ ValidationResult result = new ValidationResult();
- @Override
- public ValidationResult validate(Object value, ConstraintConfiguration config) {
- ValidationResult result = new ValidationResult();
+ try {
+ if (value == null || !(value instanceof Number || value instanceof String)) {
+ result.setSuccess(false);
+ if (value == null) {
+ result.setMessage("The value is null"); // TODO: I18N
+ } else {
+ result.setMessage("Invalid value type " + value.getClass().getName()); // TODO:
+ // I18N
+ }
+ } else {
+ double min = Double.parseDouble(getMin(config));
+ double max = Double.parseDouble(getMax(config));
+ double d = Double.parseDouble(value.toString());
+ result.setSuccess(d > min && d < max);
+ if (!result.isSuccess()) {
+ result.setMessage("The value should be between " + min + " and " + max); // TODO:
+ // I18N
+ }
+ }
+ } catch (Throwable t) {
+ result.setSuccess(false);
+ result.setMessage(t.getMessage()); // TODO: I18N
+ }
- try {
- if (value == null || !(value instanceof Number || value instanceof String)) {
- result.setSuccess(false);
- if (value == null) {
- result.setMessage("The value is null"); // TODO: I18N
- } else {
- result.setMessage("Invalid value type " + value.getClass().getName()); // TODO:
- // I18N
- }
- } else {
- double min = Double.parseDouble(getMin(config));
- double max = Double.parseDouble(getMax(config));
- double d = Double.parseDouble(value.toString());
- result.setSuccess(d > min && d < max);
- if (!result.isSuccess()) {
- result.setMessage("The value should be between " + min + " and " + max); // TODO:
- // I18N
- }
- }
- } catch (Throwable t) {
- result.setSuccess(false);
- result.setMessage(t.getMessage()); // TODO: I18N
- }
+ return result;
+ }
- return result;
- }
+ public String getMin(ConstraintConfiguration conf) {
+ try {
+ return (String) this.getMandatoryArgument(RANGE_CONSTRAINT_MIN, conf);
+ } catch (ArgumentNotSetException e) {
+ throw new IllegalStateException(e);
+ }
+ }
- public String getMin(ConstraintConfiguration conf) {
- try {
- return (String) this.getMandatoryArgument(RANGE_CONSTRAINT_MIN, conf);
- } catch (ArgumentNotSetException e) {
- throw new IllegalStateException(e);
- }
- }
+ public String getMax(ConstraintConfiguration conf) {
+ try {
+ return (String) this.getMandatoryArgument(RANGE_CONSTRAINT_MAX, conf);
+ } catch (ArgumentNotSetException e) {
+ throw new IllegalStateException(e);
+ }
+ }
- public String getMax(ConstraintConfiguration conf) {
- try {
- return (String) this.getMandatoryArgument(RANGE_CONSTRAINT_MAX, conf);
- } catch (ArgumentNotSetException e) {
- throw new IllegalStateException(e);
- }
- }
+ @Override
+ protected String getVerifierRestrictionPatternTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ String valuePattern = this.getValuePattern(config,context);
- @Override
- protected String getVerifierRestrictionPatternTemplate(ConstraintConfiguration config, Map<String, Object> context) {
- StringBuilder restrictionPattern = new StringBuilder();
+ StringBuilder restrictionPattern = new StringBuilder();
- restrictionPattern.append(" ($restriction :LiteralRestriction(\n");
- restrictionPattern.append(" fieldPath == $field.path,\n");
- restrictionPattern.append(this.getOperatorPattern(context));
- restrictionPattern.append(" ((valueType == Field.INT && (intValue < ")
- .append(getMin(config)).append(" || > ").append(getMax(config)).append(")) ");
- restrictionPattern.append(" || \n");
- restrictionPattern.append(" (valueType == Field.DOUBLE && (doubleValue < ")
- .append(getMin(config)).append(" || > ").append(getMax(config)).append(")) ");
- restrictionPattern.append(" )))\n");
+ restrictionPattern.append(" ($restriction :LiteralRestriction(\n");
+ restrictionPattern.append(" fieldPath == $field.path,\n");
+ restrictionPattern.append(" operator.operatorString == '" + this.getCurrentOperator(context).getOperatorString() + "'");
+ if (!valuePattern.isEmpty()){
+ restrictionPattern.append(",\n");
+ restrictionPattern.append(valuePattern);
+ }
+ restrictionPattern.append(" ))\n");
- return restrictionPattern.toString();
- }
+ return restrictionPattern.toString();
+ }
- private String getOperatorPattern(Map<String, Object> context) {
- return " operator.operatorString == '" + getCurrentOperator(context).getOperatorString() + "',\n";
- }
+ private String getValuePattern(ConstraintConfiguration conf, Map<String, Object> context) {
+ StringBuilder pattern = new StringBuilder();
- @Override
- protected String getVerifierActionTemplate(ConstraintConfiguration config, Map<String, Object> context) {
- StringBuilder verifierActionTemplate = new StringBuilder();
+ // >, <, <=, >= operators doesn't need to evaluate the value. They
+ //will always fail because of missing range.
+ if (this.getCurrentOperator(context).equals(Operator.EQUAL) || this.getCurrentOperator(context).equals(Operator.NOT_EQUAL)) {
+ pattern.append(" ((valueType == Field.INT && (intValue < " + getMin(conf) + " || > " + getMax(conf) + ")) ");
+ pattern.append(" || ");
+ pattern.append(" (valueType == Field.DOUBLE && (doubleValue < " + getMin(conf) + " || > " + getMax(conf) + "))) ");
+ }
- if (getCurrentOperator(context).getOperatorString().equals(Operator.EQUAL.getOperatorString())) {
- verifierActionTemplate.append(this.addResult(Severity.ERROR));
- } else if (getCurrentOperator(context).getOperatorString().equals(Operator.NOT_EQUAL.getOperatorString())) {
- verifierActionTemplate.append(this.addResult(Severity.WARNING));
- } else {
- return super.getVerifierActionTemplate(config, context);
- }
+ return pattern.toString();
+ }
- return verifierActionTemplate.toString();
- }
+ @Override
+ protected String getVerifierActionTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ StringBuilder verifierActionTemplate = new StringBuilder();
- public List<String> getArgumentKeys() {
- return Arrays.asList(new String[] { RANGE_CONSTRAINT_MIN, RANGE_CONSTRAINT_MAX });
- }
-
- private Operator getCurrentOperator(Map<String, Object> context) {
- return (Operator) context.get(CURRENT_OPERATOR);
- }
-
- private void setCurrentOperator(Map<String, Object> context, Operator operator) {
- context.put(CURRENT_OPERATOR, operator);
- }
-
- public static ConstraintConfiguration getEmptyConfiguration() {
- ConstraintConfiguration config = new SimpleConstraintConfigurationImpl();
- config.setArgumentValue("Min.value", "0");
- config.setArgumentValue("Max.value", "0");
- return config;
- }
+
+ if (this.getCurrentOperator(context).equals(Operator.EQUAL)) {
+ verifierActionTemplate.append(this.addResult(Severity.ERROR));
+ } else if (this.getCurrentOperator(context).equals(Operator.NOT_EQUAL)) {
+ verifierActionTemplate.append(this.addResult(Severity.WARNING));
+ } else if (this.getCurrentOperator(context).equals(Operator.GREATER)) {
+ verifierActionTemplate.append(this.addResult(Severity.WARNING));
+ } else if (this.getCurrentOperator(context).equals(Operator.LESS)) {
+ verifierActionTemplate.append(this.addResult(Severity.WARNING));
+ } else if (this.getCurrentOperator(context).equals(Operator.GREATER_OR_EQUAL)) {
+ verifierActionTemplate.append(this.addResult(Severity.WARNING));
+ } else if (this.getCurrentOperator(context).equals(Operator.LESS_OR_EQUAL)) {
+ verifierActionTemplate.append(this.addResult(Severity.WARNING));
+ } else {
+ return super.getVerifierActionTemplate(config,context);
+ }
+
+ return verifierActionTemplate.toString();
+ }
+
+ public List<String> getArgumentKeys() {
+ return Arrays.asList(new String[]{RANGE_CONSTRAINT_MIN, RANGE_CONSTRAINT_MAX});
+ }
+
+ private Operator getCurrentOperator(Map<String, Object> context) {
+ return (Operator) context.get(CURRENT_OPERATOR);
+ }
+
+ private void setCurrentOperator(Map<String, Object> context, Operator operator) {
+ context.put(CURRENT_OPERATOR, operator);
+ }
+
+ public static ConstraintConfiguration getEmptyConfiguration() {
+ ConstraintConfiguration config = new SimpleConstraintConfigurationImpl();
+ config.setArgumentValue("Min.value", "0");
+ config.setArgumentValue("Max.value", "0");
+ return config;
+ }
}
Added: labs/jbossrules/trunk/drools-factconstraint/src/test/java/org/drools/factconstraint/RangeConstraintMultipleOperatorsTest.java
===================================================================
--- labs/jbossrules/trunk/drools-factconstraint/src/test/java/org/drools/factconstraint/RangeConstraintMultipleOperatorsTest.java (rev 0)
+++ labs/jbossrules/trunk/drools-factconstraint/src/test/java/org/drools/factconstraint/RangeConstraintMultipleOperatorsTest.java 2010-03-30 20:20:27 UTC (rev 32314)
@@ -0,0 +1,185 @@
+package org.drools.factconstraint;
+
+import java.util.Collection;
+import org.drools.builder.ResourceType;
+import org.drools.factconstraint.server.Constraint;
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.config.SimpleConstraintConfigurationImpl;
+import org.drools.factconstraints.server.predefined.RangeConstraint;
+import org.drools.io.ResourceFactory;
+import org.drools.verifier.Verifier;
+import org.drools.verifier.VerifierConfiguration;
+import org.drools.verifier.VerifierConfigurationImpl;
+import org.drools.verifier.VerifierError;
+import org.drools.verifier.builder.VerifierBuilder;
+import org.drools.verifier.builder.VerifierBuilderFactory;
+import org.drools.verifier.data.VerifierReport;
+import org.drools.verifier.report.components.Severity;
+import org.drools.verifier.report.components.VerifierMessageBase;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class RangeConstraintMultipleOperatorsTest {
+
+ private Verifier verifier;
+ private Constraint cons;
+ private ConstraintConfiguration conf;
+
+ @Before
+ public void setup() {
+ cons = new RangeConstraint();
+ conf = new SimpleConstraintConfigurationImpl();
+ conf.setFactType("Person");
+ conf.setFieldName("age");
+
+ conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MIN, "0");
+ conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MAX, "120");
+ //System.out.println("Validation Rule:\n" + cons.getVerifierRule(conf) + "\n\n");
+
+ }
+
+ @After
+ public void dispose() {
+ if (verifier != null) {
+ verifier.dispose();
+ }
+ }
+
+ @Test
+ public void test() {
+
+ String rulesToVerify = "";
+ int fail = 0;
+ int warn = 0;
+
+ //OK
+ rulesToVerify += "package org.drools.factconstraint.test\n\n";
+ rulesToVerify += "import org.drools.factconstraint.model.*\n";
+ rulesToVerify += "rule \"rule1\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age > 1, age < 100)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+
+ //FAIL
+ rulesToVerify += "rule \"rule2\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age > -1, age < 60)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ fail++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule3\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age > -1, age < 150)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ fail++;
+
+ //FAIL-Impossible
+ rulesToVerify += "rule \"rule4\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age > 150, age < -1)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ fail++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule5\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age > -10, age < -1)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ fail++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule6\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age > 18, age < 150)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ fail++;
+
+ //FAIL-Impossible
+ rulesToVerify += "rule \"rule7\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age > 18, age < -1)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ fail++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule8\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age > 130, age < 150)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ fail++;
+
+ //FAIL-Impossible
+ rulesToVerify += "rule \"rule9\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age > 130, age < 18)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ fail++;
+
+ VerifierReport result = this.verify(rulesToVerify);
+
+ Collection<VerifierMessageBase> errors = result.getBySeverity(Severity.ERROR);
+ Collection<VerifierMessageBase> warnings = result.getBySeverity(Severity.WARNING);
+
+ System.out.println(warnings);
+ System.out.println(errors);
+
+ Assert.assertEquals(warn, warnings.size());
+ Assert.assertEquals(fail, errors.size());
+ }
+
+ private VerifierReport verify(String rulesToVerify) {
+ VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
+
+ //VerifierConfiguration conf = new DefaultVerifierConfiguration();
+ VerifierConfiguration conf = new VerifierConfigurationImpl();
+
+ conf.getVerifyingResources().put(ResourceFactory.newClassPathResource("RangeConstraintMultiOperator.drl"), ResourceType.DRL);
+
+ //conf.getVerifyingResources().put(ResourceFactory.newByteArrayResource(cons.getVerifierRule().getBytes()), ResourceType.DRL);
+
+ verifier = vBuilder.newVerifier(conf);
+
+ verifier.addResourcesToVerify(ResourceFactory.newByteArrayResource(rulesToVerify.getBytes()),
+ ResourceType.DRL);
+
+ if (verifier.hasErrors()) {
+ for (VerifierError error : verifier.getErrors()) {
+ System.out.println(error.getMessage());
+ }
+ throw new RuntimeException("Error building verifier");
+ }
+
+ Assert.assertFalse(verifier.hasErrors());
+
+ boolean noProblems = verifier.fireAnalysis();
+ Assert.assertTrue(noProblems);
+
+ return verifier.getResult();
+
+ }
+}
Modified: labs/jbossrules/trunk/drools-factconstraint/src/test/java/org/drools/factconstraint/RangeConstraintSingleOperatorTest.java
===================================================================
--- labs/jbossrules/trunk/drools-factconstraint/src/test/java/org/drools/factconstraint/RangeConstraintSingleOperatorTest.java 2010-03-30 19:55:23 UTC (rev 32313)
+++ labs/jbossrules/trunk/drools-factconstraint/src/test/java/org/drools/factconstraint/RangeConstraintSingleOperatorTest.java 2010-03-30 20:20:27 UTC (rev 32314)
@@ -38,6 +38,10 @@
conf = new SimpleConstraintConfigurationImpl();
conf.setFactType("Person");
conf.setFieldName("age");
+
+ conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MIN, "0");
+ conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MAX, "120");
+ System.out.println("Validation Rule:\n" + cons.getVerifierRule(conf) + "\n\n");
}
@After
@@ -50,12 +54,6 @@
@Test
public void testEq() {
- //age constraint
- conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MIN, "0");
- conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MAX, "120");
- System.out.println("Validation Rule:\n" + cons.getVerifierRule(conf) + "\n\n");
-
-
String rulesToVerify = "";
int fail = 0;
@@ -99,11 +97,6 @@
@Test
public void testNotEq() {
- //age constraint
- conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MIN, "0");
- conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MAX, "120");
- System.out.println("Validation Rule:\n" + cons.getVerifierRule(conf) + "\n\n");
-
String rulesToVerify = "";
int warning = 0;
@@ -144,6 +137,186 @@
Assert.assertEquals(warning, warnings.size());
}
+ @Test
+ public void testGT() {
+
+ String rulesToVerify = "";
+ int fail = 0;
+ int warning = 0;
+
+ //FAIL
+ rulesToVerify += "package org.drools.factconstraint.test\n\n";
+ rulesToVerify += "import org.drools.factconstraint.model.*\n";
+ rulesToVerify += "rule \"rule1\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age > -5)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ warning++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule2\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age > 10)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ warning++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule3\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age > 130)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n";
+ warning++;
+
+ VerifierReport result = this.verify(rulesToVerify);
+
+ Collection<VerifierMessageBase> warnings = result.getBySeverity(Severity.WARNING);
+
+ System.out.println(warnings);
+
+ Assert.assertEquals(warning, warnings.size());
+ }
+
+ @Test
+ public void testGE() {
+
+ String rulesToVerify = "";
+ int fail = 0;
+ int warning = 0;
+
+ //FAIL
+ rulesToVerify += "package org.drools.factconstraint.test\n\n";
+ rulesToVerify += "import org.drools.factconstraint.model.*\n";
+ rulesToVerify += "rule \"rule1\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age >= -5)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ warning++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule2\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age >= 10)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ warning++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule3\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age >= 130)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n";
+ warning++;
+
+ VerifierReport result = this.verify(rulesToVerify);
+
+ Collection<VerifierMessageBase> warnings = result.getBySeverity(Severity.WARNING);
+
+ System.out.println(warnings);
+
+ Assert.assertEquals(warning, warnings.size());
+ }
+
+ @Test
+ public void testLT() {
+
+ String rulesToVerify = "";
+ int fail = 0;
+ int warning = 0;
+
+ //FAIL
+ rulesToVerify += "package org.drools.factconstraint.test\n\n";
+ rulesToVerify += "import org.drools.factconstraint.model.*\n";
+ rulesToVerify += "rule \"rule1\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age < -5)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ warning++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule2\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age < 10)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ warning++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule3\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age < 130)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n";
+ warning++;
+
+ VerifierReport result = this.verify(rulesToVerify);
+
+ Collection<VerifierMessageBase> warnings = result.getBySeverity(Severity.WARNING);
+
+ System.out.println(warnings);
+
+ Assert.assertEquals(warning, warnings.size());
+ }
+
+ @Test
+ public void testLE() {
+
+ String rulesToVerify = "";
+ int fail = 0;
+ int warning = 0;
+
+ //FAIL
+ rulesToVerify += "package org.drools.factconstraint.test\n\n";
+ rulesToVerify += "import org.drools.factconstraint.model.*\n";
+ rulesToVerify += "rule \"rule1\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age <= -5)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ warning++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule2\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age <= 10)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ warning++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule3\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age <= 130)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n";
+ warning++;
+
+ VerifierReport result = this.verify(rulesToVerify);
+
+ Collection<VerifierMessageBase> warnings = result.getBySeverity(Severity.WARNING);
+
+ System.out.println(warnings);
+
+ Assert.assertEquals(warning, warnings.size());
+ }
+
private VerifierReport verify(String rulesToVerify){
VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
Added: labs/jbossrules/trunk/drools-factconstraint/src/test/resources/RangeConstraintMultiOperator.drl
===================================================================
--- labs/jbossrules/trunk/drools-factconstraint/src/test/resources/RangeConstraintMultiOperator.drl (rev 0)
+++ labs/jbossrules/trunk/drools-factconstraint/src/test/resources/RangeConstraintMultiOperator.drl 2010-03-30 20:20:27 UTC (rev 32314)
@@ -0,0 +1,43 @@
+package org.drools.verifier.consequence
+import org.drools.verifier.components.*;
+import java.util.Map;
+import java.util.HashMap;
+import org.drools.verifier.report.components.VerifierMessage;
+import org.drools.verifier.data.VerifierReport;
+import org.drools.verifier.report.components.Severity;
+import org.drools.verifier.report.components.MessageType;
+import org.drools.base.evaluators.Operator;
+
+global VerifierReport result;
+
+rule "abcdef"
+when
+ $field :Field(
+ objectTypeName == "Person",
+ name == "age"
+ )
+ ($restriction1 :LiteralRestriction(
+ $rulePath: rulePath,
+ fieldPath == $field.path,
+ operator == Operator.GREATER,
+ (valueType == Field.INT || == Field.DOUBLE),
+ $v1: doubleValue))
+ ($restriction2 :LiteralRestriction(
+ fieldPath == $field.path,
+ rulePath == $rulePath,
+ operator == Operator.LESS,
+ $v2: doubleValue))
+then
+ System.out.println("Fired");
+
+ if (!($v1 > 0 && $v1 < 120 && $v2 > 0 && $v2 < 120)){
+ Map<String,String> impactedRules = new HashMap<String,String>();
+ result.add(new VerifierMessage(
+ impactedRules,
+ Severity.ERROR,
+ MessageType.NOT_SPECIFIED,
+ $restriction1,
+ "Missing range" ) );
+ }
+
+end
\ No newline at end of file
More information about the jboss-svn-commits
mailing list