[jboss-svn-commits] JBL Code SVN: r36703 - in labs/jbossrules/tags/5.2.0.M1: drools-factconstraint and 31 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Feb 24 11:41:18 EST 2011
Author: ge0ffrey
Date: 2011-02-24 11:41:17 -0500 (Thu, 24 Feb 2011)
New Revision: 36703
Added:
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/.gitignore
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/pom.xml
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/ArgumentNotSetException.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/ConstraintConfiguration.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/ValidationResult.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/config/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/config/SimpleConstraintConfigurationImpl.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/customform/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/customform/CustomFormConfiguration.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/customform/predefined/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/customform/predefined/DefaultCustomFormImplementation.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/dataprovider/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/dataprovider/FieldDataProvider.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/helper/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/helper/ConstraintsContainer.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/helper/CustomFormsContainer.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/Constraint.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/DefaultFieldConstraintImpl.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/DefaultMultiRulesConstraintImpl.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/factory/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/factory/ConstraintsFactory.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/DefaultFieldDataProviderImpl.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/IntegerConstraint.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/InvalidFieldConstraint.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/MatchesConstraint.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/NotMatchesConstraint.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/NotNullConstraint.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/RangeConstraint.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/resources/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/resources/org/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/resources/org/drools/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/resources/org/drools/factconstraints/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/resources/org/drools/factconstraints/FactConstraints.gwt.xml
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraint/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraint/model/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraint/model/Person.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/IntegerConstraintTest.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/InvalidFieldConstraintTest.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/NotMatchesConstraintTest.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/NotNullConstraintTest.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/RangeConstraintMultipleOperatorsTest.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/RangeConstraintSingleOperatorTest.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/RangeConstraintTest.java
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/test/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/test/client/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/test/client/helper/
labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/test/client/helper/ConstraintsConstrainerTest.java
Log:
All monolothic build versions (<= 5.2.0.M1) stay in subversion
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/.gitignore
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/.gitignore (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/.gitignore 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,10 @@
+/target
+/local
+
+# Eclipse, Netbeans and IntelliJ files
+/.*
+!.gitignore
+/nbproject
+/*.ipr
+/*.iws
+/*.iml
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/pom.xml
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/pom.xml (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/pom.xml 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.drools</groupId>
+ <artifactId>drools</artifactId>
+ <version>5.2.0.M1</version>
+ </parent>
+ <artifactId>drools-factconstraint</artifactId>
+ <packaging>jar</packaging>
+ <name>Drools :: Fact Constraint</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-verifier</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-core</artifactId>
+ </dependency>
+
+ </dependencies>
+</project>
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/ArgumentNotSetException.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/ArgumentNotSetException.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/ArgumentNotSetException.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,43 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.client;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class ArgumentNotSetException extends Exception {
+
+ private static final long serialVersionUID = 501l;
+
+ public ArgumentNotSetException() {
+ super();
+ }
+
+ public ArgumentNotSetException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public ArgumentNotSetException(String message) {
+ super(message);
+ }
+
+ public ArgumentNotSetException(Throwable cause) {
+ super(cause);
+ }
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/ConstraintConfiguration.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/ConstraintConfiguration.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/ConstraintConfiguration.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,41 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.client;
+
+import java.io.Serializable;
+import java.util.Set;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ * @author baunax at gmail.com
+ */
+public interface ConstraintConfiguration extends Serializable {
+ public String getFactType();
+ public void setFactType(String factType);
+
+ public String getFieldName();
+ public void setFieldName(String fieldName);
+
+ public Set<String> getArgumentKeys();
+ public Object getArgumentValue(String key);
+ public void setArgumentValue(String key, String value);
+ public boolean containsArgument(String key);
+
+ public String getConstraintName();
+ public void setConstraintName(String name);
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/ValidationResult.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/ValidationResult.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/ValidationResult.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,46 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.client;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class ValidationResult {
+ private boolean success;
+ private String message;
+
+ public boolean isSuccess() {
+ return success;
+ }
+
+ public void setSuccess(boolean success) {
+ this.success = success;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+
+
+
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/config/SimpleConstraintConfigurationImpl.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/config/SimpleConstraintConfigurationImpl.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/config/SimpleConstraintConfigurationImpl.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,98 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.client.config;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.drools.factconstraints.client.ConstraintConfiguration;
+
+public class SimpleConstraintConfigurationImpl implements ConstraintConfiguration {
+
+ private static final long serialVersionUID = 501l;
+ private Map<String, String> args = new HashMap<String, String>();
+ private String constraintName = null;
+ private String factType;
+ private String fieldName;
+
+ public SimpleConstraintConfigurationImpl(ConstraintConfiguration constraintConfiguration) {
+ copyFrom(constraintConfiguration);
+ }
+
+ public SimpleConstraintConfigurationImpl() {
+ }
+
+ public Set<String> getArgumentKeys() {
+ return args.keySet();
+ }
+
+ public Object getArgumentValue(String key) {
+ return args.get(key);
+ }
+
+ public String getConstraintName() {
+ return constraintName;
+ }
+
+ public void setConstraintName(String constraintName) {
+ this.constraintName = constraintName;
+ }
+
+ public String getFactType() {
+ return factType;
+ }
+
+ public String getFieldName() {
+ return fieldName;
+ }
+
+ public void setArgumentValue(String key, String value) {
+ args.put(key, value);
+ }
+
+ public void setFactType(String factType) {
+ this.factType = factType;
+ }
+
+ public void setFieldName(String fieldName) {
+ this.fieldName = fieldName;
+ }
+
+ public boolean containsArgument(String key) {
+ return args.containsKey(key);
+ }
+
+ @Override
+ public String toString() {
+ return "SimpleConstraintConfigurationImpl [args=" + args + ", constraintName=" + constraintName + ", factType="
+ + factType + ", fieldName=" + fieldName + "]";
+ }
+
+ private void copyFrom(ConstraintConfiguration other) {
+ if (constraintName != null) {
+ throw new RuntimeException("can't copy configuration on a configured instance");
+ }
+ this.constraintName = other.getConstraintName();
+ this.factType = other.getFactType();
+ this.fieldName = other.getFieldName();
+ this.args = new HashMap<String, String>();
+ for (String argName : other.getArgumentKeys()) {
+ this.args.put(argName, (String) other.getArgumentValue(argName));
+ }
+ }
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/customform/CustomFormConfiguration.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/customform/CustomFormConfiguration.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/customform/CustomFormConfiguration.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,42 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.client.customform;
+
+import java.io.Serializable;
+
+/**
+ *
+ * @author esteban
+ */
+public interface CustomFormConfiguration extends Serializable{
+
+ String getFactType();
+ void setFactType(String factType);
+
+ String getFieldName();
+ void setFieldName(String fieldName);
+
+ String getCustomFormURL();
+ void setCustomFormURL(String url);
+
+ int getCustomFormHeight();
+ void setCustomFormHeight(int height);
+
+ int getCustomFormWidth();
+ void setCustomFormWidth(int width);
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/customform/predefined/DefaultCustomFormImplementation.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/customform/predefined/DefaultCustomFormImplementation.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/customform/predefined/DefaultCustomFormImplementation.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,74 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.client.customform.predefined;
+
+import java.io.Serializable;
+import org.drools.factconstraints.client.customform.CustomFormConfiguration;
+
+/**
+ *
+ * @author esteban
+ */
+public class DefaultCustomFormImplementation implements CustomFormConfiguration, Serializable{
+
+ private int width = 200;
+ private int height = 200;
+ private String factType;
+ private String fieldName;
+ private String url;
+
+ public String getFactType() {
+ return this.factType;
+ }
+
+ public void setFactType(String factType) {
+ this.factType = factType;
+ }
+
+ public String getFieldName() {
+ return this.fieldName;
+ }
+
+ public void setFieldName(String fieldName) {
+ this.fieldName = fieldName;
+ }
+
+ public String getCustomFormURL() {
+ return this.url;
+ }
+
+ public void setCustomFormURL(String url) {
+ this.url = url;
+ }
+
+ public int getCustomFormHeight() {
+ return height;
+ }
+
+ public void setCustomFormHeight(int height) {
+ this.height = height;
+ }
+
+ public int getCustomFormWidth() {
+ return width;
+ }
+
+ public void setCustomFormWidth(int width) {
+ this.width = width;
+ }
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/dataprovider/FieldDataProvider.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/dataprovider/FieldDataProvider.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/dataprovider/FieldDataProvider.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,35 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.client.dataprovider;
+
+import java.util.Map;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public interface FieldDataProvider {
+ public void setFactTYpe(String factType);
+ public void setFieldName(String fieldName);
+
+ public String[] getArgumentKeys();
+ public Object getArgumentValue(String key);
+ public void setArgumentValue(String key, Object value);
+
+ public Map<Object,String> getData();
+ public Object getDefault();
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/helper/ConstraintsContainer.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/helper/ConstraintsContainer.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/helper/ConstraintsContainer.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,132 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.client.helper;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.config.SimpleConstraintConfigurationImpl;
+
+public class ConstraintsContainer {
+ private static final Map<String, ConstraintConfiguration> constraintConfigs = new HashMap<String, ConstraintConfiguration>();
+
+ static {
+ ConstraintConfiguration config = new SimpleConstraintConfigurationImpl();
+ config.setConstraintName("NotNull");
+ constraintConfigs.put(config.getConstraintName(), config);
+
+ config = new SimpleConstraintConfigurationImpl();
+ config.setConstraintName("IntegerConstraint");
+ constraintConfigs.put(config.getConstraintName(), config);
+
+ config = new SimpleConstraintConfigurationImpl();
+ config.setConstraintName("RangeConstraint");
+ config.setArgumentValue("Min.value", "0");
+ config.setArgumentValue("Max.value", "0");
+ constraintConfigs.put(config.getConstraintName(), config);
+
+ config = new SimpleConstraintConfigurationImpl();
+ config.setConstraintName("NotMatches");
+ config.setArgumentValue("matches", "");
+ constraintConfigs.put(config.getConstraintName(), config);
+
+ config = new SimpleConstraintConfigurationImpl();
+ config.setConstraintName("Matches");
+ config.setArgumentValue("matches", "");
+ constraintConfigs.put(config.getConstraintName(), config);
+
+ config = new SimpleConstraintConfigurationImpl();
+ config.setConstraintName("IvalidFieldConstraint");
+ constraintConfigs.put(config.getConstraintName(), config);
+ }
+
+ private Map<String, List<ConstraintConfiguration>> constraints = new HashMap<String, List<ConstraintConfiguration>>();
+
+ public ConstraintsContainer(ConstraintConfiguration[] constraints) {
+ this(Arrays.asList(constraints));
+ }
+
+ public ConstraintsContainer(Collection<ConstraintConfiguration> constraints) {
+ if (constraints != null && !constraints.isEmpty()) {
+ for (ConstraintConfiguration c : constraints) {
+ addConstraint(c);
+ }
+ }
+ }
+
+ public ConstraintsContainer() {
+
+ }
+
+ public List<ConstraintConfiguration> removeConstraint(ConstraintConfiguration c) {
+ List<ConstraintConfiguration> list = constraints.get(c.getFactType());
+ if (list != null) {
+ list.remove(c);
+ }
+ return list;
+ }
+
+ public void addConstraint(ConstraintConfiguration c) {
+ List<ConstraintConfiguration> list = constraints.get(c.getFactType());
+ if (list == null) {
+ list = new LinkedList<ConstraintConfiguration>();
+ constraints.put(c.getFactType(), list);
+ }
+ list.add(c);
+ }
+
+ public List<ConstraintConfiguration> getConstraints(String factType) {
+ return Collections.unmodifiableList(constraints.get(factType));
+ }
+
+ public List<ConstraintConfiguration> getConstraints(String factType, String fieldName) {
+
+ List<ConstraintConfiguration> list = constraints.get(factType);
+ if (list == null || list.isEmpty()) {
+ return Collections.emptyList();
+ }
+ List<ConstraintConfiguration> res = new LinkedList<ConstraintConfiguration>();
+ for (ConstraintConfiguration c : list) {
+ if (fieldName.equals(c.getFieldName())) {
+ res.add(c);
+ }
+ }
+ return res;
+ }
+
+ public boolean hasConstraints(String FactType) {
+ return constraints.containsKey(FactType);
+ }
+
+ public static Map<String, ConstraintConfiguration> getAllConfigurations() {
+ return constraintConfigs;
+ }
+
+ public static ConstraintConfiguration getEmptyConfiguration(String constraintName) {
+ return copyConfig(getAllConfigurations().get(constraintName));
+ }
+
+ private static ConstraintConfiguration copyConfig(ConstraintConfiguration constraintConfiguration) {
+ return new SimpleConstraintConfigurationImpl(constraintConfiguration);
+ }
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/helper/CustomFormsContainer.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/helper/CustomFormsContainer.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/client/helper/CustomFormsContainer.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,90 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.client.helper;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.factconstraints.client.customform.CustomFormConfiguration;
+import org.drools.factconstraints.client.customform.predefined.DefaultCustomFormImplementation;
+
+public class CustomFormsContainer {
+
+ //because a FactType.field can only have one customForm,
+ //this map is: <"FactType.field", CustomForm>
+ private Map<String, CustomFormConfiguration> customForms = new HashMap<String, CustomFormConfiguration>();
+
+ public CustomFormsContainer(CustomFormConfiguration[] customFormsConfigs) {
+ this(Arrays.asList(customFormsConfigs));
+ }
+
+ public CustomFormsContainer(Collection<CustomFormConfiguration> customFormsConfigs) {
+ if (customFormsConfigs != null && !customFormsConfigs.isEmpty()) {
+ for (CustomFormConfiguration c : customFormsConfigs) {
+ putCustomForm(c);
+ }
+ }
+ }
+
+// public CustomFormsContainer() {
+//
+// }
+ public void removeCustomForm(CustomFormConfiguration cfc) {
+ this.customForms.remove(this.createMapKey(cfc));
+ }
+
+ /**
+ * If cfc.getCustomFormURL() is empty, the CustomFormConfiguration is removed.
+ * @param cfc
+ */
+ public final void putCustomForm(CustomFormConfiguration cfc) {
+ if (cfc.getCustomFormURL().trim().equals("")){
+ this.customForms.remove(this.createMapKey(cfc));
+ }else{
+ this.customForms.put(this.createMapKey(cfc), cfc);
+ }
+ }
+
+ public CustomFormConfiguration getCustomForm(String factType, String fieldName) {
+ return this.customForms.get(this.createMapKey(factType, fieldName));
+ }
+
+ public List<CustomFormConfiguration> getCustomForms(){
+ return new ArrayList<CustomFormConfiguration>(this.customForms.values());
+ }
+
+ public boolean containsCustomFormFor(String factType, String fieldName){
+ return this.getCustomForm(factType, fieldName) != null;
+ }
+
+ private String createMapKey(String factType, String fieldName){
+ return factType+"."+fieldName;
+ }
+
+ private String createMapKey(CustomFormConfiguration cfc) {
+ return this.createMapKey(cfc.getFactType(), cfc.getFieldName());
+ }
+
+ public static CustomFormConfiguration getEmptyCustomFormConfiguration() {
+ return new DefaultCustomFormImplementation();
+ }
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/Constraint.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/Constraint.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/Constraint.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,37 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server;
+
+import java.io.Serializable;
+import java.util.List;
+
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.ValidationResult;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ * @author baunax at gmail.com
+ */
+public interface Constraint extends Serializable {
+ public List<String> getArgumentKeys();
+
+ public ValidationResult validate(Object value, ConstraintConfiguration config);
+ public String getVerifierRule(ConstraintConfiguration config);
+ public String getConstraintName();
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/DefaultFieldConstraintImpl.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/DefaultFieldConstraintImpl.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/DefaultFieldConstraintImpl.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,331 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.base.evaluators.Operator;
+import org.drools.factconstraints.client.ArgumentNotSetException;
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.ValidationResult;
+import org.drools.verifier.report.components.Severity;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ * @author baunax at gmail.com
+ */
+public abstract class DefaultFieldConstraintImpl implements Constraint {
+
+ private static final long serialVersionUID = 501l;
+ private long ruleNum = 0;
+
+ public static final List<Operator> supportedOperators = new ArrayList<Operator>();
+ static{
+ supportedOperators.add(Operator.EQUAL);
+ supportedOperators.add(Operator.NOT_EQUAL);
+ supportedOperators.add(Operator.GREATER);
+ supportedOperators.add(Operator.GREATER_OR_EQUAL);
+ supportedOperators.add(Operator.LESS);
+ supportedOperators.add(Operator.LESS_OR_EQUAL);
+ }
+
+ /**
+ * Method used to create the field Restriction. It returns the class name
+ * of the Restriction used in the generated rule. By default, it returns
+ * "LiteralRestriction", but subclasses could override this method in order
+ * to use other subclasses of org.drools.verifier.components.Restriction
+ * @return
+ */
+ protected String getFieldRestrictionClassName(){
+ return "LiteralRestriction";
+ }
+
+ 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));
+
+ rule.append(this.getVerifierImportsPrefixTemplate(config, context));
+ rule.append(this.getVerifierImportsTemplate(config, context));
+ rule.append(this.getVerifierImportsSufixTemplate(config, context));
+
+ rule.append(this.getVerifierGlobalsPrefixTemplate(config, context));
+ rule.append(this.getVerifierGlobalsTemplate(config, context));
+ rule.append(this.getVerifierGlobalsSufixTemplate(config, context));
+
+ rule.append(this.getVerifierRuleNamePrefixTemplate(config, context));
+ rule.append(this.getVerifierRuleNameTemplate(config, context));
+ rule.append(this.getVerifierRuleNameSufixTemplate(config, context));
+
+ rule.append(this.getVerifierRuleWhenTemplate(config, context));
+
+ rule.append(this.getVerifierFieldPatternPrefixTemplate(config, context));
+ rule.append(this.getVerifierFieldPatternTemplate(config, context));
+ rule.append(this.getVerifierFieldPatternSufixTemplate(config, context));
+
+ rule.append(this.getVerifierRestrictionPatternPrefixTemplate(config, context));
+ rule.append(this.getVerifierRestrictionPatternTemplate(config, context));
+ rule.append(this.getVerifierRestrictionPatternSufixTemplate(config, context));
+
+ rule.append(this.getVerifierRuleThenTemplate(config, context));
+
+ rule.append(this.getVerifierActionPrefixTemplate(config, context));
+ rule.append(this.getVerifierActionTemplate(config, context));
+ rule.append(this.getVerifierActionSufixTemplate(config, context));
+
+ rule.append(this.getVerifierRuleEndTemplate(config, context));
+ rule.append(this.getVerifierRuleEndSufixTemplate(config, context));
+
+ return rule.toString();
+
+ }
+
+ protected String createVerifierRuleTemplate(ConstraintConfiguration config, Map<String, Object> context, String ruleName, List<String> constraints, String message) {
+ if (ruleName == null) {
+ ruleName = "Constraint_rule";
+
+ }
+ ruleName += "_" + ruleNum++;
+ String rule = this.concatRule(config, context).replace("${ruleName}", ruleName);
+ rule = rule.replace("${factType}", config.getFactType());
+ rule = rule.replace("${fieldName}", config.getFieldName());
+ StringBuilder constraintsTxt = new StringBuilder();
+ if (constraints != null && !constraints.isEmpty()) {
+ for (String c : constraints) {
+ constraintsTxt.append(",\n");
+ constraintsTxt.append(c);
+ }
+ }
+ rule = rule.replace("${constraints}", constraintsTxt);
+ rule = rule.replace("${message}", (message == null || message.equals("")) ? "Invalid Value" : message);
+
+ return rule;
+ }
+
+ protected Object getMandatoryArgument(String key, ConstraintConfiguration conf) throws ArgumentNotSetException {
+ if (!conf.containsArgument(key)) {
+ throw new ArgumentNotSetException("The argument " + key + " doesn't exist.");
+ }
+
+ Object value = conf.getArgumentValue(key);
+
+ if (value == null) {
+ throw new ArgumentNotSetException("The argument " + key + " is null.");
+ }
+
+ return value;
+ }
+
+ /**
+ * Used for on-demand validation
+ * @param value
+ * @param config
+ * @return
+ * @deprecated Use drools-verifier instead of this method:
+ * {@link #getVerifierRule(org.drools.factconstraints.client.ConstraintConfiguration) }
+ */
+ @Deprecated
+ public ValidationResult validate(Object value, ConstraintConfiguration config) {
+ ValidationResult result = new ValidationResult();
+ result.setSuccess(true);
+
+ return result;
+ }
+
+ protected Map<String, Object> createContext() {
+ 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);
+ }
+
+ /* Action */
+ protected String getVerifierActionTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ StringBuilder verifierActionTemplate = new StringBuilder();
+
+ //by default, add an ERROR
+ verifierActionTemplate.append(this.addResult(Severity.ERROR));
+
+// verifierActionTemplate.append(" System.out.println(\"doubleValue= \"+$restriction.getDoubleValue());\n");
+// verifierActionTemplate.append(" System.out.println(\"intValue= \"+$restriction.getIntValue());\n");
+
+ return verifierActionTemplate.toString();
+ }
+
+ protected String addResult(Severity severity){
+ StringBuilder addString = new StringBuilder();
+ addString.append(" result.add(new VerifierMessage(\n");
+ addString.append(" impactedRules,\n");
+ if (severity.compareTo(Severity.ERROR) == 0){
+ addString.append(" Severity.ERROR,\n");
+ }else if(severity.compareTo(Severity.NOTE) == 0){
+ addString.append(" Severity.NOTE,\n");
+ }else if(severity.compareTo(Severity.WARNING) == 0){
+ addString.append(" Severity.WARNING,\n");
+ }
+ addString.append(" MessageType.NOT_SPECIFIED,\n");
+ addString.append(" $restriction,\n");
+ addString.append(" \"${message}\" ) );\n");
+ return addString.toString();
+ }
+
+ protected String getVerifierActionPrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ StringBuilder verifierActionPrefixTemplate = new StringBuilder();
+ verifierActionPrefixTemplate.append(" Map<String,String> impactedRules = new HashMap<String,String>();\n")
+ .append(" impactedRules.put( $restriction.getPackagePath(), $restriction.getRuleName());\n");
+ return verifierActionPrefixTemplate.toString();
+ }
+
+ protected String getVerifierActionSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ /* Field Pattern */
+ protected String getVerifierFieldPatternTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ StringBuilder verifierFieldPatternTemplate = new StringBuilder();
+ verifierFieldPatternTemplate.append(" $field :Field(\n");
+ verifierFieldPatternTemplate.append(" objectTypeName == \"${factType}\",\n");
+ verifierFieldPatternTemplate.append(" name == \"${fieldName}\"\n");
+ verifierFieldPatternTemplate.append(" )\n");
+ return verifierFieldPatternTemplate.toString();
+ }
+
+ protected String getVerifierFieldPatternPrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ protected String getVerifierFieldPatternSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ /* Globals*/
+ protected String getVerifierGlobalsTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "global VerifierReport result;\n";
+ }
+
+ protected String getVerifierGlobalsPrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ protected String getVerifierGlobalsSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ /* Imports */
+ protected String getVerifierImportsTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ StringBuilder verifierImportsTemplate = new StringBuilder();
+ verifierImportsTemplate.append("import org.drools.verifier.components.*;\n");
+ verifierImportsTemplate.append("import java.util.Map;\n");
+ verifierImportsTemplate.append("import java.util.HashMap;\n");
+ verifierImportsTemplate.append("import org.drools.verifier.report.components.VerifierMessage;\n");
+ verifierImportsTemplate.append("import org.drools.verifier.data.VerifierReport;\n");
+ verifierImportsTemplate.append("import org.drools.verifier.report.components.Severity;\n");
+ verifierImportsTemplate.append("import org.drools.verifier.report.components.MessageType;\n");
+
+ return verifierImportsTemplate.toString();
+
+ }
+
+ protected String getVerifierImportsPrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ protected String getVerifierImportsSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ protected String getVerifierPackageTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "package org.drools.verifier.consequence\n";
+ }
+
+ protected String getVerifierPackagePrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ protected String getVerifierPackageSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ /* Restriction Pattern */
+ protected String getVerifierRestrictionPatternTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ StringBuilder verifierRestrictionPatternTemplate = new StringBuilder();
+ verifierRestrictionPatternTemplate.append(" $restriction :");
+ verifierRestrictionPatternTemplate.append(this.getFieldRestrictionClassName());
+ verifierRestrictionPatternTemplate.append(" (\n");
+ verifierRestrictionPatternTemplate.append(" fieldPath == $field.path\n");
+ verifierRestrictionPatternTemplate.append(" ${constraints}\n");
+ verifierRestrictionPatternTemplate.append(" )\n");
+
+ return verifierRestrictionPatternTemplate.toString();
+ }
+
+ protected String getVerifierRestrictionPatternPrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ protected String getVerifierRestrictionPatternSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ /* end */
+ protected String getVerifierRuleEndTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "end\n";
+ }
+
+ protected String getVerifierRuleEndSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ /* Rule Name */
+ protected String getVerifierRuleNameTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "rule \"${ruleName}\"\n";
+ }
+
+ protected String getVerifierRuleNamePrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ protected String getVerifierRuleNameSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return "";
+ }
+
+ /* then */
+ protected String getVerifierRuleThenTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return " then\n";
+ }
+
+ /* when */
+ protected String getVerifierRuleWhenTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ return " when\n";
+ }
+
+ public List<String> getArgumentKeys() {
+ return new ArrayList<String>();
+ }
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/DefaultMultiRulesConstraintImpl.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/DefaultMultiRulesConstraintImpl.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/DefaultMultiRulesConstraintImpl.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,120 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server;
+
+import java.util.Map;
+
+import org.drools.factconstraints.client.ConstraintConfiguration;
+
+/**
+ * Default implementation for constraints that creates more than one rule
+ * in its {@link #getVerifierRule()} method.
+ * This class contains an internal counter to avoid multiple declaration of
+ * package, imports and globals.
+ * The counter must be handled by subclasses.
+ * @author esteban.aliverti at gmail.com
+ */
+public abstract class DefaultMultiRulesConstraintImpl extends DefaultFieldConstraintImpl {
+
+ private static final long serialVersionUID = 501l;
+ protected static final String RULE_COUNT = "ruleCount";
+
+ protected void resetRuleCount(Map<String, Object> context){
+ context.put(RULE_COUNT, 0);
+ }
+
+ protected void incrementRuleCount(Map<String, Object> context){
+ int rc = (Integer) context.get(RULE_COUNT);
+ context.put(RULE_COUNT, ++rc);
+ }
+
+ protected int getRuleCount(Map<String, Object> context){
+ return (Integer) context.get(RULE_COUNT);
+ }
+
+ @Override
+ protected String getVerifierGlobalsPrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ if (getRuleCount(context) == 0) {
+ return super.getVerifierGlobalsPrefixTemplate(config, context);
+ }
+ return "";
+ }
+
+ @Override
+ protected String getVerifierGlobalsSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ if (getRuleCount(context) == 0) {
+ return super.getVerifierGlobalsSufixTemplate(config, context);
+ }
+ return "";
+ }
+
+ @Override
+ protected String getVerifierGlobalsTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ if (getRuleCount(context) == 0) {
+ return super.getVerifierGlobalsTemplate(config, context);
+ }
+ return "";
+ }
+
+ @Override
+ protected String getVerifierImportsPrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ if (getRuleCount(context) == 0) {
+ return super.getVerifierImportsPrefixTemplate(config, context);
+ }
+ return "";
+ }
+
+ @Override
+ protected String getVerifierImportsSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ if (getRuleCount(context) == 0) {
+ return super.getVerifierImportsSufixTemplate(config, context);
+ }
+ return "";
+ }
+
+ @Override
+ protected String getVerifierImportsTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ if (getRuleCount(context) == 0) {
+ return super.getVerifierImportsTemplate(config, context);
+ }
+ return "";
+ }
+
+ @Override
+ protected String getVerifierPackagePrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ if (getRuleCount(context) == 0) {
+ return super.getVerifierPackagePrefixTemplate(config, context);
+ }
+ return "";
+ }
+
+ @Override
+ protected String getVerifierPackageSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ if (getRuleCount(context) == 0) {
+ return super.getVerifierPackageSufixTemplate(config, context);
+ }
+ return "";
+ }
+
+ @Override
+ protected String getVerifierPackageTemplate(ConstraintConfiguration config, Map<String, Object> context) {
+ if (getRuleCount(context) == 0) {
+ return super.getVerifierPackageTemplate(config, context);
+ }
+ return "";
+ }
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/factory/ConstraintsFactory.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/factory/ConstraintsFactory.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/factory/ConstraintsFactory.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,58 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.factory;
+
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.server.Constraint;
+import org.drools.factconstraints.server.predefined.IntegerConstraint;
+import org.drools.factconstraints.server.predefined.InvalidFieldConstraint;
+import org.drools.factconstraints.server.predefined.MatchesConstraint;
+import org.drools.factconstraints.server.predefined.NotMatchesConstraint;
+import org.drools.factconstraints.server.predefined.NotNullConstraint;
+import org.drools.factconstraints.server.predefined.RangeConstraint;
+
+public class ConstraintsFactory {
+ private final static ConstraintsFactory INSTANCE = new ConstraintsFactory();
+
+ public static ConstraintsFactory getInstance() {
+ return INSTANCE;
+ }
+
+ private ConstraintsFactory() {}
+
+ public Constraint buildConstraint(ConstraintConfiguration config) {
+ if (NotNullConstraint.NAME.equals(config.getConstraintName())) {
+ return new NotNullConstraint();
+ } else if (IntegerConstraint.NAME.equals(config.getConstraintName())) {
+ return new IntegerConstraint();
+ } else if (RangeConstraint.NAME.equals(config.getConstraintName())) {
+ return new RangeConstraint();
+ } else if (NotMatchesConstraint.NAME.equals(config.getConstraintName())) {
+ return new NotMatchesConstraint();
+ }else if (MatchesConstraint.NAME.equals(config.getConstraintName())) {
+ return new MatchesConstraint();
+ }else if (InvalidFieldConstraint.NAME.equals(config.getConstraintName())) {
+ return new InvalidFieldConstraint();
+ } else {
+ throw new IllegalArgumentException("Constraint unknown: " + config.getConstraintName());
+ }
+ }
+
+ public String getVerifierRule(ConstraintConfiguration config) {
+ return buildConstraint(config).getVerifierRule(config);
+ }
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/DefaultFieldDataProviderImpl.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/DefaultFieldDataProviderImpl.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/DefaultFieldDataProviderImpl.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,80 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.drools.factconstraints.client.ArgumentNotSetException;
+import org.drools.factconstraints.client.dataprovider.FieldDataProvider;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public abstract class DefaultFieldDataProviderImpl implements FieldDataProvider {
+
+ private String factType;
+ private String fieldName;
+ private Map<String, Object> arguments = new HashMap<String, Object>();
+
+ public DefaultFieldDataProviderImpl() {
+
+ }
+
+ protected Object getMandatoryArgument(String key) throws ArgumentNotSetException {
+ if (!this.arguments.containsKey(key)) {
+ throw new ArgumentNotSetException("The argument " + key + " doesn't exist.");
+ }
+
+ Object value = this.getArgumentValue(key);
+
+ if (value == null) {
+ throw new ArgumentNotSetException("The argument " + key + " is null.");
+ }
+
+ return value;
+ }
+
+ public void setFactType(String factType) {
+ this.factType = factType;
+ }
+
+ public void setFieldName(String fieldName) {
+ this.fieldName = fieldName;
+ }
+
+ public String getFactType() {
+ return factType;
+ }
+
+ public String getFieldName() {
+ return fieldName;
+ }
+
+ public String[] getArgumentKeys() {
+ return this.arguments.keySet().toArray(new String[this.arguments.size()]);
+ }
+
+ public Object getArgumentValue(String key) {
+ return this.arguments.get(key);
+ }
+
+ public void setArgumentValue(String key, Object value) {
+ this.arguments.put(key, value);
+ }
+}
\ No newline at end of file
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/IntegerConstraint.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/IntegerConstraint.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/IntegerConstraint.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,70 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.ValidationResult;
+import org.drools.factconstraints.server.DefaultFieldConstraintImpl;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class IntegerConstraint extends DefaultFieldConstraintImpl {
+ private static final long serialVersionUID = 501l;
+ public static final String NAME = "IntegerConstraint";
+
+ public IntegerConstraint(){}
+
+ @Override
+ protected String internalVerifierRule(ConstraintConfiguration config,
+ Map<String, Object> context) {
+ List<String> constraints = new ArrayList<String>();
+ constraints.add("valueType != Field.INT");
+
+ return this.createVerifierRuleTemplate(config, context, "Integer_Field_Constraint", constraints, "The value must be an integer"); //I18N
+ }
+
+ @Override
+ public ValidationResult validate(Object value, ConstraintConfiguration config) {
+ ValidationResult result = new ValidationResult();
+
+ if (value == null){
+ result.setSuccess(false);
+ result.setMessage("The value is null"); //TODO: I18N
+ }else if (value instanceof Integer){
+ result.setSuccess(true);
+ }else if (value instanceof String){
+ try{
+ Integer.parseInt((String)value);
+ result.setSuccess(true);
+ } catch(NumberFormatException ex){
+ result.setSuccess(false);
+ result.setMessage(ex.getMessage()); //TODO: I18N
+ }
+ }else{
+ result.setSuccess(false);
+ result.setMessage("Invalid value type "+value.getClass().getName()); //TODO: I18N
+ }
+
+ return result;
+ }
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/InvalidFieldConstraint.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/InvalidFieldConstraint.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/InvalidFieldConstraint.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,53 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.ValidationResult;
+import org.drools.factconstraints.server.DefaultFieldConstraintImpl;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class InvalidFieldConstraint extends DefaultFieldConstraintImpl {
+ private static final long serialVersionUID = 501l;
+ public static final String NAME = "IvalidFieldConstraint";
+
+ public InvalidFieldConstraint(){}
+
+ @Override
+ protected String internalVerifierRule(ConstraintConfiguration config,
+ Map<String, Object> context) {
+ return this.createVerifierRuleTemplate(config, context, "Invalid_Field_Constraint", null, "The field can not be used in this rule"); //I18N
+ }
+
+ @Override
+ public ValidationResult validate(Object value, ConstraintConfiguration config) {
+ return null;
+ }
+
+ @Override
+ protected String getFieldRestrictionClassName(){
+ return "Restriction";
+ }
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/MatchesConstraint.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/MatchesConstraint.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/MatchesConstraint.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,42 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.server.DefaultFieldConstraintImpl;
+
+public class MatchesConstraint extends DefaultFieldConstraintImpl {
+
+ public static final String MATCHES_ARGUMENT = "matches";
+ private static final long serialVersionUID = 501l;
+ public static final String NAME = "Matches";
+
+ @Override
+ protected String internalVerifierRule(ConstraintConfiguration config, Map<String, Object> context) {
+ List<String> constraints = new ArrayList<String>();
+ constraints.add("valueAsString not matches \"" + config.getArgumentValue(MATCHES_ARGUMENT) + "\"");
+
+ return this.createVerifierRuleTemplate(config, context,
+ "Matches_Field_Constraint", constraints,
+ "The value must match: " + config.getArgumentValue(MATCHES_ARGUMENT)); // I18N
+ }
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/NotMatchesConstraint.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/NotMatchesConstraint.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/NotMatchesConstraint.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,49 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.server.DefaultFieldConstraintImpl;
+
+public class NotMatchesConstraint extends DefaultFieldConstraintImpl {
+
+ public static final String NOT_MATCHES_ARGUMENT = "matches";
+ private static final long serialVersionUID = 501l;
+ public static final String NAME = "NotMatches";
+
+ @Override
+ protected String internalVerifierRule(ConstraintConfiguration config, Map<String, Object> context) {
+ List<String> constraints = new ArrayList<String>();
+ constraints.add("valueAsString matches \"" + config.getArgumentValue(NOT_MATCHES_ARGUMENT) + "\"");
+
+ return this.createVerifierRuleTemplate(config, context,
+ "Matches_Field_Constraint", constraints,
+ "The value must not match: " + config.getArgumentValue(NOT_MATCHES_ARGUMENT)); // I18N
+ }
+
+// @Override
+// public ValidationResult validate(Object value, ConstraintConfiguration config) {
+// Pattern p = Pattern.compile((String) config.getArgumentValue(NOT_MATCHES_ARGUMENT));
+//
+// return p.matcher(value.toString()).matches();
+// }
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/NotNullConstraint.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/NotNullConstraint.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/NotNullConstraint.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,64 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.ValidationResult;
+import org.drools.factconstraints.server.DefaultFieldConstraintImpl;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class NotNullConstraint extends DefaultFieldConstraintImpl {
+ private static final long serialVersionUID = 501l;
+ public static final String NAME = "NotNull";
+
+ public NotNullConstraint() {
+ }
+
+ @Override
+ protected String internalVerifierRule(ConstraintConfiguration config,
+ Map<String, Object> context) {
+ List<String> constraints = new ArrayList<String>();
+ constraints.add("valueType == Field.UNKNOWN");
+
+ return this.createVerifierRuleTemplate(config, context,
+ "Not_null_Field_Constraint", constraints,
+ "The value could not be null"); // I18N
+ }
+
+ @Override
+ public ValidationResult validate(Object value,
+ ConstraintConfiguration config) {
+ ValidationResult result = new ValidationResult();
+
+ if (value == null) {
+ result.setSuccess(false);
+ result.setMessage("The value could not be null"); // TODO: I18N
+ } else {
+ result.setSuccess(true);
+ }
+
+ return result;
+ }
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/RangeConstraint.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/RangeConstraint.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/java/org/drools/factconstraints/server/predefined/RangeConstraint.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,309 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.drools.factconstraints.client.ArgumentNotSetException;
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.ValidationResult;
+import org.drools.factconstraints.server.Constraint;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class RangeConstraint implements Constraint {
+
+ 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";
+ private static String template;
+
+ static {
+ StringBuilder rules = new StringBuilder();
+ rules.append("package org.drools.verifier.consequence\n");
+
+ rules.append("import org.drools.verifier.components.*;\n");
+ rules.append("import java.util.Map;\n");
+ rules.append("import java.util.HashMap;\n");
+ rules.append("import org.drools.verifier.report.components.VerifierMessage;\n");
+ rules.append("import org.drools.verifier.data.VerifierReport;\n");
+ rules.append("import org.drools.verifier.report.components.Severity;\n");
+ rules.append("import org.drools.verifier.report.components.MessageType;\n");
+ rules.append("import org.drools.base.evaluators.Operator;\n");
+
+ rules.append("global VerifierReport result;\n");
+
+ rules.append("declare RangeConstraintCandidate{0}\n");
+ rules.append(" restriction : NumberRestriction\n");
+ rules.append(" greaterValue : double\n");
+ rules.append(" lessValue : double\n");
+ rules.append("end\n");
+
+
+ rules.append("function void addResult{0}(VerifierReport report, NumberRestriction restriction, Severity severity, String message){\n");
+ rules.append(" Map<String,String> impactedRules = new HashMap<String,String>();\n");
+ rules.append(" report.add(new VerifierMessage(\n");
+ rules.append(" impactedRules,\n");
+ rules.append(" severity,\n");
+ rules.append(" MessageType.NOT_SPECIFIED,\n");
+ rules.append(" restriction,\n");
+ rules.append(" message ) );\n");
+ rules.append("}\n");
+
+ rules.append("rule \"Range_Field_Constraint_Base_{0}\"\n");
+ rules.append(" when\n");
+ rules.append(" $field :Field(\n");
+ rules.append(" objectTypeName == \"{1}\",\n");
+ rules.append(" name == \"{2}\"\n");
+ rules.append(" )\n");
+ rules.append(" then\n");
+ rules.append("end\n");
+
+ rules.append("/* Single operators */\n");
+
+ rules.append("rule \"Range_Field_Constraint_==_{0}\" extends \"Range_Field_Constraint_Base_{0}\"\n");
+ rules.append(" when\n");
+ rules.append(" ($restriction :NumberRestriction(\n");
+ rules.append(" fieldPath == $field.path,\n");
+ rules.append(" operator == Operator.EQUAL,\n");
+ rules.append(" (value < {3} || > {4}))\n");
+ rules.append(" )\n");
+ rules.append(" then\n");
+ rules.append(" addResult{0}(result, $restriction, Severity.ERROR, \"The value must be between {3} and {4}\");\n");
+ rules.append("end\n");
+
+ rules.append("rule \"Range_Field_Constraint_!=_{0}\" extends \"Range_Field_Constraint_Base_{0}\"\n");
+ rules.append(" when\n");
+ rules.append(" ($restriction :NumberRestriction(\n");
+ rules.append(" fieldPath == $field.path,\n");
+ rules.append(" operator == Operator.NOT_EQUAL,\n");
+ rules.append(" (value < {3} || > {4}))\n");
+ rules.append(" )\n");
+ rules.append(" then\n");
+ rules.append(" addResult{0}(result, $restriction, Severity.WARNING, \"Impossible value. Possible values are from {3} to {4}\");\n");
+ rules.append("end\n");
+
+ rules.append("rule \"Range_Field_Constraint_>_{0}\" extends \"Range_Field_Constraint_Base_{0}\"\n");
+ rules.append(" when\n");
+ rules.append(" ($restriction :NumberRestriction(\n");
+ rules.append(" fieldPath == $field.path,\n");
+ rules.append(" $rulePath: rulePath,\n");
+ rules.append(" (operator == Operator.GREATER || == Operator.GREATER_OR_EQUAL))\n");
+ rules.append(" )\n");
+ rules.append(" not (NumberRestriction(\n");
+ rules.append(" fieldPath == $field.path,\n");
+ rules.append(" rulePath == $rulePath,\n");
+ rules.append(" (operator == Operator.LESS || == Operator.LESS_OR_EQUAL)\n");
+ rules.append(" )\n");
+ rules.append(" )\n");
+ rules.append(" then\n");
+ rules.append(" addResult{0}(result, $restriction, Severity.WARNING, \"Missing range\");\n");
+ rules.append("end\n");
+
+ rules.append("rule \"Range_Field_Constraint_<_{0}\" extends \"Range_Field_Constraint_Base_{0}\"\n");
+ rules.append(" when\n");
+ rules.append(" ($restriction :NumberRestriction(\n");
+ rules.append(" fieldPath == $field.path,\n");
+ rules.append(" $rulePath: rulePath,\n");
+ rules.append(" (operator == Operator.LESS || == Operator.LESS_OR_EQUAL))\n");
+ rules.append(" )\n");
+ rules.append(" not (NumberRestriction(\n");
+ rules.append(" fieldPath == $field.path,\n");
+ rules.append(" rulePath == $rulePath,\n");
+ rules.append(" (operator == Operator.GREATER || == Operator.GREATER_OR_EQUAL)\n");
+ rules.append(" )\n");
+ rules.append(" )\n");
+ rules.append(" then\n");
+ rules.append(" addResult{0}(result, $restriction, Severity.WARNING, \"Missing range\");\n");
+ rules.append("end\n");
+
+
+ rules.append("/* Multi operator */\n");
+
+ rules.append("rule \"identifyRangeConstraintCandidate{0}\" extends \"Range_Field_Constraint_Base_{0}\"\n");
+ rules.append("when\n");
+ rules.append(" ($restriction1 :NumberRestriction(\n");
+ rules.append(" $rulePath: rulePath,\n");
+ rules.append(" fieldPath == $field.path,\n");
+ rules.append(" (operator == Operator.GREATER || == Operator.GREATER_OR_EQUAL),\n");
+ rules.append(" $op1: operator,\n");
+ rules.append(" $value1: value))\n");
+ rules.append(" ($restriction2 :NumberRestriction(\n");
+ rules.append(" fieldPath == $field.path,\n");
+ rules.append(" rulePath == $rulePath,\n");
+ rules.append(" (operator == Operator.LESS || == Operator.LESS_OR_EQUAL),\n");
+ rules.append(" $op2: operator,\n");
+ rules.append(" $value2: value))\n");
+ rules.append("then\n");
+
+ rules.append(" RangeConstraintCandidate{0} rcc = new RangeConstraintCandidate{0}();\n");
+ rules.append(" rcc.setRestriction($restriction1);\n");
+ rules.append(" rcc.setGreaterValue($value1.doubleValue());\n");
+ rules.append(" rcc.setLessValue($value2.doubleValue());\n");
+
+ rules.append(" insert (rcc);\n");
+
+ rules.append("end\n");
+
+ rules.append("/*\n");
+ rules.append(" GM = the value is greater than max ( > max)\n");
+ rules.append(" LM = the value is less than min (< min)\n");
+ rules.append(" VV = the value is inside the range (>= min && <= max)\n");
+ rules.append("*/\n");
+ rules.append("rule \"processGMGM{0}\"\n");
+ rules.append("when\n");
+ rules.append(" $r: RangeConstraintCandidate{0}(greaterValue > {4} && lessValue > {4})\n");
+ rules.append("then\n");
+ rules.append(" addResult{0}(result, $r.getRestriction(), Severity.WARNING, \"Both sides are outside the range\");\n");
+ rules.append(" retract ($r);\n");
+ rules.append("end\n");
+
+ rules.append("rule \"processGMVV{0}\"\n");
+ rules.append("when\n");
+ rules.append(" $r: RangeConstraintCandidate{0}(greaterValue > {4} && lessValue >= {3} && lessValue <={4})\n");
+ rules.append("then\n");
+ rules.append(" addResult{0}(result, $r.getRestriction(), Severity.ERROR, \"Impossible condition\");\n");
+ rules.append(" retract ($r);\n");
+ rules.append("end\n");
+
+ rules.append("rule \"processGMLM{0}\"\n");
+ rules.append("when\n");
+ rules.append(" $r: RangeConstraintCandidate{0}(greaterValue > {4} && lessValue < {3})\n");
+ rules.append("then\n");
+ rules.append(" addResult{0}(result, $r.getRestriction(), Severity.ERROR, \"Impossible condition\");\n");
+ rules.append(" retract ($r);\n");
+ rules.append("end\n");
+
+ rules.append("rule \"processVVGM{0}\"\n");
+ rules.append("when\n");
+ rules.append(" $r: RangeConstraintCandidate{0}(greaterValue >= {3} && greaterValue <={4} && lessValue > {4})\n");
+ rules.append("then\n");
+ rules.append(" addResult{0}(result, $r.getRestriction(), Severity.WARNING, \"Right side is outside the range\");\n");
+ rules.append(" retract ($r);\n");
+ rules.append("end\n");
+
+ rules.append("rule \"processVVLM{0}\"\n");
+ rules.append("when\n");
+ rules.append(" $r: RangeConstraintCandidate{0}(greaterValue >= {3} && greaterValue <={4} && lessValue < {3})\n");
+ rules.append("then\n");
+ rules.append(" addResult{0}(result, $r.getRestriction(), Severity.ERROR, \"Impossible condition\");\n");
+ rules.append(" retract ($r);\n");
+ rules.append("end\n");
+
+ rules.append("rule \"processLMGM{0}\"\n");
+ rules.append("when\n");
+ rules.append(" $r: RangeConstraintCandidate{0}(greaterValue < {3} && lessValue > {4})\n");
+ rules.append("then\n");
+ rules.append(" addResult{0}(result, $r.getRestriction(), Severity.WARNING, \"Both sides are outside the range\");\n");
+ rules.append(" retract ($r);\n");
+ rules.append("end\n");
+
+ rules.append("rule \"processLMVV{0}\"\n");
+ rules.append("when\n");
+ rules.append(" $r: RangeConstraintCandidate{0}(greaterValue < {3} && lessValue >= {3} && lessValue <={4})\n");
+ rules.append("then\n");
+ rules.append(" addResult{0}(result, $r.getRestriction(), Severity.WARNING, \"Left side is outside the range\");\n");
+ rules.append(" retract ($r);\n");
+ rules.append("end\n");
+
+ rules.append("rule \"processLMLM{0}\"\n");
+ rules.append("when\n");
+ rules.append(" $r: RangeConstraintCandidate{0}(greaterValue < {3} && lessValue < {3})\n");
+ rules.append("then\n");
+ rules.append(" addResult{0}(result, $r.getRestriction(), Severity.WARNING, \"Both sides are outside the range\");\n");
+ rules.append(" retract ($r);\n");
+ rules.append("end\n");
+
+ template = rules.toString();
+ }
+
+ public String getConstraintName() {
+ return NAME;
+ }
+
+ public String getVerifierRule(ConstraintConfiguration config) {
+ return template.replaceAll("\\{0\\}", String.valueOf(System.nanoTime())).replaceAll("\\{1\\}", config.getFactType()).replaceAll("\\{2\\}", config.getFieldName()).replaceAll("\\{3\\}", this.getMin(config)).replaceAll("\\{4\\}", this.getMax(config));
+ }
+
+ 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
+ }
+
+ 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 getMax(ConstraintConfiguration conf) {
+ try {
+ return (String) this.getMandatoryArgument(RANGE_CONSTRAINT_MAX, conf);
+ } catch (ArgumentNotSetException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ public List<String> getArgumentKeys() {
+ return Arrays.asList(new String[]{RANGE_CONSTRAINT_MIN, RANGE_CONSTRAINT_MAX});
+ }
+
+ private Object getMandatoryArgument(String key, ConstraintConfiguration conf) throws ArgumentNotSetException {
+ if (!conf.containsArgument(key)) {
+ throw new ArgumentNotSetException("The argument " + key + " doesn't exist.");
+ }
+
+ Object value = conf.getArgumentValue(key);
+
+ if (value == null) {
+ throw new ArgumentNotSetException("The argument " + key + " is null.");
+ }
+
+ return value;
+ }
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/resources/org/drools/factconstraints/FactConstraints.gwt.xml
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/resources/org/drools/factconstraints/FactConstraints.gwt.xml (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/main/resources/org/drools/factconstraints/FactConstraints.gwt.xml 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<module>
+ <set-property name="gwt.suppressNonStaticFinalFieldWarnings" value="true" />
+</module>
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraint/model/Person.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraint/model/Person.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraint/model/Person.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,61 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraint.model;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class Person {
+ private String name;
+ private int age;
+ private double salary;
+
+ public Person(String name, int age,double salary) {
+ this.name = name;
+ this.age = age;
+ this.salary = salary;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public double getSalary() {
+ return salary;
+ }
+
+ public void setSalary(double salary) {
+ this.salary = salary;
+ }
+
+
+
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/IntegerConstraintTest.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/IntegerConstraintTest.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/IntegerConstraintTest.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,177 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.Collection;
+
+import org.drools.builder.ResourceType;
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.ValidationResult;
+import org.drools.factconstraints.client.config.SimpleConstraintConfigurationImpl;
+import org.drools.factconstraints.server.Constraint;
+import org.drools.factconstraints.server.predefined.IntegerConstraint;
+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.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class IntegerConstraintTest {
+
+ private ConstraintConfiguration conf;
+
+ @Before
+ public void setup() {
+ conf = new SimpleConstraintConfigurationImpl();
+ conf.setFactType("Person");
+ conf.setFieldName("age");
+ }
+
+ @Test
+ public void testValidConstraint() {
+ Constraint cons = new IntegerConstraint();
+
+ ValidationResult result = cons.validate(12, conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate(new Integer("12"), conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate("12", conf);
+ assertTrue(result.isSuccess());
+
+ }
+
+ @Test
+ public void testInvalidConstraint() {
+ Constraint cons = new IntegerConstraint();
+
+ ValidationResult result = cons.validate(new Object(), conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+ result = cons.validate("", conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+ result = cons.validate("ABC", conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+ result = cons.validate(null, conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+ result = cons.validate(new Long("12"), conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+ result = cons.validate(12L, conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+ result = cons.validate(12.8, conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+ }
+
+ @Test
+ public void testUsingVerifier() {
+
+ String ruleToVerify = "";
+
+ //FAIL
+ ruleToVerify += "package org.drools.factconstraint.test\n\n";
+ ruleToVerify += "import org.drools.factconstraint.model.*\n";
+ ruleToVerify += "rule \"rule1\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == 'abc')\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n\n";
+
+ //OK
+ ruleToVerify += "rule \"rule2\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == 12)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n\n";
+
+ //FAIL
+ ruleToVerify += "rule \"rule3\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == '')\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+
+
+ VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
+
+ //VerifierConfiguration conf = new DefaultVerifierConfiguration();
+ VerifierConfiguration vconf = new VerifierConfigurationImpl();
+
+ Constraint cons = new IntegerConstraint();
+ vconf.getVerifyingResources().put(ResourceFactory.newByteArrayResource(cons.getVerifierRule(conf).getBytes()), ResourceType.DRL);
+
+ Verifier verifier = vBuilder.newVerifier(vconf);
+
+ verifier.addResourcesToVerify(ResourceFactory.newByteArrayResource(ruleToVerify.getBytes()),
+ ResourceType.DRL);
+
+ if (verifier.hasErrors()) {
+ for (VerifierError error : verifier.getErrors()) {
+ System.out.println(error.getMessage());
+ }
+ throw new RuntimeException("Error building verifier");
+ }
+
+ assertFalse(verifier.hasErrors());
+
+ boolean noProblems = verifier.fireAnalysis();
+ assertTrue(noProblems);
+
+ VerifierReport result = verifier.getResult();
+
+ Collection<VerifierMessageBase> warnings = result.getBySeverity(Severity.ERROR);
+
+ int counter = 0;
+ for (VerifierMessageBase message : warnings) {
+ System.out.println(message);
+ counter++;
+ }
+
+ assertEquals(2, counter);
+
+ verifier.dispose();
+ }
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/InvalidFieldConstraintTest.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/InvalidFieldConstraintTest.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/InvalidFieldConstraintTest.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,163 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.Collection;
+
+import org.drools.builder.ResourceType;
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.config.SimpleConstraintConfigurationImpl;
+import org.drools.factconstraints.server.Constraint;
+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.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class InvalidFieldConstraintTest {
+
+ private ConstraintConfiguration conf;
+
+ @Before
+ public void setup() {
+ conf = new SimpleConstraintConfigurationImpl();
+ conf.setFactType("Person");
+ conf.setFieldName("age");
+ }
+
+ @Test
+ public void testUsingVerifier() {
+
+ String ruleToVerify = "";
+ int fail = 0;
+
+ //FAIL
+ ruleToVerify += "package org.drools.factconstraint.test\n\n";
+ ruleToVerify += "import org.drools.factconstraint.model.*\n";
+
+ ruleToVerify += "global Number globalAge;\n";
+
+ ruleToVerify += "rule \"rule1\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == 20)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n\n";
+ fail++;
+
+ //OK
+ ruleToVerify += "rule \"rule2\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(name == \"Peter\")\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n\n";
+
+ //FAIL
+ ruleToVerify += "rule \"rule3\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(name == \"Peter\")\n";
+ ruleToVerify += " Person(age == 20)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+ fail++;
+
+ //FAIL
+ ruleToVerify += "rule \"rule4\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Number() from collect(Person(age == 20))\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+ fail++;
+
+
+ //FAIL
+ ruleToVerify += "rule \"rule5\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person($age: age)\n";
+ ruleToVerify += " Person(age == $age)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+ fail++;
+
+ //FAIL
+ ruleToVerify += "rule \"rule6\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == globalAge)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+ fail++;
+
+ VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
+
+ //VerifierConfiguration conf = new DefaultVerifierConfiguration();
+ VerifierConfiguration vconf = new VerifierConfigurationImpl();
+
+ Constraint cons = new InvalidFieldConstraint();
+ System.out.println("\n\n\n"+cons.getVerifierRule(conf)+"\n\n\n");
+ vconf.getVerifyingResources().put(ResourceFactory.newByteArrayResource(cons.getVerifierRule(conf).getBytes()), ResourceType.DRL);
+
+ Verifier verifier = vBuilder.newVerifier(vconf);
+
+ verifier.addResourcesToVerify(ResourceFactory.newByteArrayResource(ruleToVerify.getBytes()),
+ ResourceType.DRL);
+
+ if (verifier.hasErrors()) {
+ for (VerifierError error : verifier.getErrors()) {
+ System.out.println(error.getMessage());
+ }
+ throw new RuntimeException("Error building verifier");
+ }
+
+ assertFalse(verifier.hasErrors());
+
+ boolean noProblems = verifier.fireAnalysis();
+ assertTrue(noProblems);
+
+ VerifierReport result = verifier.getResult();
+
+ Collection<VerifierMessageBase> warnings = result.getBySeverity(Severity.ERROR);
+
+ int counter = 0;
+ for (VerifierMessageBase message : warnings) {
+ System.out.println(message);
+ counter++;
+ }
+
+ assertEquals(fail, counter);
+
+ verifier.dispose();
+ }
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/NotMatchesConstraintTest.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/NotMatchesConstraintTest.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/NotMatchesConstraintTest.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,181 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.Collection;
+
+import org.drools.builder.ResourceType;
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.ValidationResult;
+import org.drools.factconstraints.client.config.SimpleConstraintConfigurationImpl;
+import org.drools.factconstraints.server.Constraint;
+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.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author baunax at gmail.com
+ */
+public class NotMatchesConstraintTest {
+
+ private ConstraintConfiguration conf;
+ private Constraint cons;
+
+ @Before
+ public void setup() {
+ conf = new SimpleConstraintConfigurationImpl();
+ conf.setFactType("Person");
+ conf.setFieldName("name");
+ conf.setArgumentValue(NotMatchesConstraint.NOT_MATCHES_ARGUMENT, "^[A-Z].*$");
+
+ cons = new NotMatchesConstraint();
+
+ System.out.println("not matches rule: " + cons.getVerifierRule(conf));
+ }
+
+ @Test @Ignore
+ public void testValidConstraint(){
+
+ ValidationResult result = cons.validate("Bart", conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate("", conf);
+ assertFalse(result.isSuccess());
+
+ result = cons.validate("bart", conf);
+ assertFalse(result.isSuccess());
+
+ result = cons.validate(new Long("12"), conf);
+ assertFalse(result.isSuccess());
+
+ result = cons.validate(12L, conf);
+ assertFalse(result.isSuccess());
+
+ result = cons.validate(12.8, conf);
+ assertFalse(result.isSuccess());
+ }
+
+ @Test @Ignore
+ public void testInvalidConstraint(){
+
+ ValidationResult result = cons.validate(null, conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: "+result.getMessage());
+
+ }
+
+ @Test
+ public void testUsingVerifier() {
+
+ String ruleToVerify = "";
+ int fails = 0;
+
+ //FAIL
+ ruleToVerify += "package org.drools.factconstraint.test\n\n";
+ ruleToVerify += "import org.drools.factconstraint.model.*\n";
+ ruleToVerify += "rule \"rule1\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(name == \"John\")\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n\n";
+ fails++;
+
+ //OK
+ ruleToVerify += "rule \"rule2\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(name == '')\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+
+ //OK
+ ruleToVerify += "rule \"pepe\"\n";
+ ruleToVerify += "dialect \"mvel\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person( name == 'pepe' )\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+
+ //OK
+ ruleToVerify += "rule \"rule3\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(name == 'bart')\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+
+ //OK
+ ruleToVerify += "rule \"rule4\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(name == '1bart')\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+
+ System.out.println(ruleToVerify);
+
+ VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
+
+ VerifierConfiguration vconf = new VerifierConfigurationImpl();
+
+ vconf.getVerifyingResources().put(ResourceFactory.newByteArrayResource(cons.getVerifierRule(this.conf).getBytes()), ResourceType.DRL);
+
+ Verifier verifier = vBuilder.newVerifier(vconf);
+
+ verifier.addResourcesToVerify(ResourceFactory.newByteArrayResource(ruleToVerify.getBytes()),
+ ResourceType.DRL);
+
+ if (verifier.hasErrors()) {
+ for (VerifierError error : verifier.getErrors()) {
+ System.out.println(error.getMessage());
+ }
+ throw new RuntimeException("Error building verifier");
+ }
+
+ assertFalse(verifier.hasErrors());
+
+ boolean noProblems = verifier.fireAnalysis();
+ assertTrue(noProblems);
+
+ VerifierReport result = verifier.getResult();
+
+ Collection<VerifierMessageBase> warnings = result.getBySeverity(Severity.ERROR);
+
+ System.out.println(warnings);
+
+ assertEquals(fails, warnings.size());
+
+ verifier.dispose();
+ }
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/NotNullConstraintTest.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/NotNullConstraintTest.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/NotNullConstraintTest.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,163 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.Collection;
+
+import org.drools.builder.ResourceType;
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.ValidationResult;
+import org.drools.factconstraints.client.config.SimpleConstraintConfigurationImpl;
+import org.drools.factconstraints.server.Constraint;
+import org.drools.factconstraints.server.predefined.NotNullConstraint;
+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.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class NotNullConstraintTest {
+
+ private ConstraintConfiguration conf;
+ private Constraint cons;
+
+ @Before
+ public void setup() {
+ conf = new SimpleConstraintConfigurationImpl();
+ conf.setFactType("Person");
+ conf.setFieldName("name");
+
+ cons = new NotNullConstraint();
+ }
+
+ @Test
+ public void testValidConstraint(){
+
+ ValidationResult result = cons.validate(new Object(), conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate("", conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate("ABC", conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate(new Long("12"), conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate(12L, conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate(12.8, conf);
+ assertTrue(result.isSuccess());
+ }
+
+ @Test
+ public void testInvalidConstraint(){
+
+ ValidationResult result = cons.validate(null, conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: "+result.getMessage());
+
+ }
+
+ @Test
+ public void testUsingVerifier() {
+
+ String ruleToVerify = "";
+
+ //OK
+ ruleToVerify += "package org.drools.factconstraint.test\n\n";
+ ruleToVerify += "import org.drools.factconstraint.model.*\n";
+ ruleToVerify += "rule \"rule1\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(name == 'John McClane')\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n\n";
+
+ //FAIL
+ ruleToVerify += "rule \"rule2\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(name == null)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n\n";
+
+ //OK
+ ruleToVerify += "rule \"rule3\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(name == '')\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+
+
+ VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
+
+ VerifierConfiguration vconf = new VerifierConfigurationImpl();
+
+ vconf.getVerifyingResources().put(ResourceFactory.newByteArrayResource(cons.getVerifierRule(this.conf).getBytes()), ResourceType.DRL);
+
+ Verifier verifier = vBuilder.newVerifier(vconf);
+
+ verifier.addResourcesToVerify(ResourceFactory.newByteArrayResource(ruleToVerify.getBytes()),
+ ResourceType.DRL);
+
+ if (verifier.hasErrors()) {
+ for (VerifierError error : verifier.getErrors()) {
+ System.out.println(error.getMessage());
+ }
+ throw new RuntimeException("Error building verifier");
+ }
+
+ assertFalse(verifier.hasErrors());
+
+ boolean noProblems = verifier.fireAnalysis();
+ assertTrue(noProblems);
+
+ VerifierReport result = verifier.getResult();
+
+ Collection<VerifierMessageBase> warnings = result.getBySeverity(Severity.ERROR);
+
+ int counter = 0;
+ for (VerifierMessageBase message : warnings) {
+ System.out.println(message);
+ counter++;
+ }
+
+ assertEquals(1,
+ counter);
+
+ verifier.dispose();
+ }
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/RangeConstraintMultipleOperatorsTest.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/RangeConstraintMultipleOperatorsTest.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/RangeConstraintMultipleOperatorsTest.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,250 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.Collection;
+import org.drools.builder.ResourceType;
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.config.SimpleConstraintConfigurationImpl;
+import org.drools.factconstraints.server.Constraint;
+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;
+
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @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 test1() {
+
+
+ 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";
+ warn++;
+
+ //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";
+ warn++;
+
+ //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";
+ warn++;
+
+ //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";
+ warn++;
+
+ //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";
+ warn++;
+
+ //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);
+
+ assertEquals(warn, warnings.size());
+ assertEquals(fail, errors.size());
+ }
+
+ @Test
+ public void test2() {
+ String rulesToVerify = "";
+ int fail = 0;
+ int warn = 0;
+
+ //OK (RangeConstraint is inclussive)
+ 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 >= 0, age <= 120)\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 >= 0, age <= 121)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ warn++;
+
+ //FAIL
+ rulesToVerify += "rule \"rule3\"\n";
+ rulesToVerify += " when\n";
+ rulesToVerify += " Person(age >= 0, age <= 121.1)\n";
+ rulesToVerify += " then\n";
+ rulesToVerify += " System.out.println(\"Rule fired\");\n";
+ rulesToVerify += "end\n\n";
+ warn++;
+
+ 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);
+
+ assertEquals(warn, warnings.size());
+ 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(this.conf).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");
+ }
+
+ assertFalse(verifier.hasErrors());
+
+ boolean noProblems = verifier.fireAnalysis();
+ assertTrue(noProblems);
+
+ return verifier.getResult();
+
+ }
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/RangeConstraintSingleOperatorTest.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/RangeConstraintSingleOperatorTest.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/RangeConstraintSingleOperatorTest.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,361 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.Collection;
+
+import org.drools.builder.ResourceType;
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.config.SimpleConstraintConfigurationImpl;
+import org.drools.factconstraints.server.Constraint;
+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;
+
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class RangeConstraintSingleOperatorTest {
+
+ 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 testEq() {
+
+ String rulesToVerify = "";
+ int fail = 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";
+ fail++;
+
+ //OK
+ 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";
+
+ //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";
+ fail++;
+
+ VerifierReport result = this.verify(rulesToVerify);
+
+ Collection<VerifierMessageBase> errors = result.getBySeverity(Severity.ERROR);
+
+ System.out.println(errors);
+
+ assertEquals(fail, errors.size());
+ }
+
+ @Test
+ public void testNotEq() {
+
+ String rulesToVerify = "";
+ 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++;
+
+ //OK
+ 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";
+
+ //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);
+
+ assertEquals(warning, warnings.size());
+ }
+
+ @Test
+ public void testGT() {
+
+ String rulesToVerify = "";
+ 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);
+
+ assertEquals(warning, warnings.size());
+ }
+
+ @Test
+ public void testGE() {
+
+ String rulesToVerify = "";
+ 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);
+
+ assertEquals(warning, warnings.size());
+ }
+
+ @Test
+ public void testLT() {
+
+ String rulesToVerify = "";
+ 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);
+
+ assertEquals(warning, warnings.size());
+ }
+
+ @Test
+ public void testLE() {
+
+ String rulesToVerify = "";
+ 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);
+
+ assertEquals(warning, warnings.size());
+ }
+
+ private VerifierReport verify(String rulesToVerify){
+ VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
+
+ //VerifierConfiguration conf = new DefaultVerifierConfiguration();
+ VerifierConfiguration vconf = new VerifierConfigurationImpl();
+
+ vconf.getVerifyingResources().put(ResourceFactory.newByteArrayResource(cons.getVerifierRule(this.conf).getBytes()), ResourceType.DRL);
+
+ verifier = vBuilder.newVerifier(vconf);
+
+ verifier.addResourcesToVerify(ResourceFactory.newByteArrayResource(rulesToVerify.getBytes()),
+ ResourceType.DRL);
+
+ boolean noProblems = verifier.fireAnalysis();
+
+ if (verifier.hasErrors()) {
+ for (VerifierError error : verifier.getErrors()) {
+ System.out.println(error.getMessage());
+ }
+ throw new RuntimeException("Error building verifier");
+ }
+
+ assertTrue(noProblems);
+
+ return verifier.getResult();
+
+ }
+
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/RangeConstraintTest.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/RangeConstraintTest.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/server/predefined/RangeConstraintTest.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,390 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.server.predefined;
+
+import java.util.Collection;
+
+import org.drools.builder.ResourceType;
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.ValidationResult;
+import org.drools.factconstraints.client.config.SimpleConstraintConfigurationImpl;
+import org.drools.factconstraints.server.Constraint;
+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.components.PatternComponent;
+import org.drools.verifier.components.RuleComponent;
+import org.drools.verifier.data.VerifierReport;
+import org.drools.verifier.report.components.Severity;
+import org.drools.verifier.report.components.VerifierMessageBase;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author esteban.aliverti at gmail.com
+ */
+public class RangeConstraintTest {
+
+ private Constraint cons;
+ private ConstraintConfiguration conf;
+
+ @Before
+ public void setup() {
+ cons = new RangeConstraint();
+ conf = new SimpleConstraintConfigurationImpl();
+ conf.setFactType("Person");
+ conf.setFieldName("age");
+ }
+
+ //@Test
+ @Test
+ public void testValidConstraint() {
+
+ conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MIN, "-0.5");
+ conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MAX, "100");
+
+ ValidationResult result = cons.validate(12, conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate(new Integer("12"), conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate("12", conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate(0.6, conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate(new Float("-0.3"), conf);
+ assertTrue(result.isSuccess());
+
+ result = cons.validate("90.76", conf);
+ assertTrue(result.isSuccess());
+
+ }
+
+ //@Test
+ @Test
+ public void testInvalidConstraint() {
+
+ conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MIN, "-0.5");
+ conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MAX, "100");
+
+ ValidationResult result = cons.validate(new Object(), conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+ result = cons.validate(null, conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+ result = cons.validate("", conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+ result = cons.validate("ABC", conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+ result = cons.validate(new Long("-100"), conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+ result = cons.validate(-0.5, conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+ result = cons.validate(100, conf);
+ assertFalse(result.isSuccess());
+ System.out.println("Message: " + result.getMessage());
+
+
+ }
+
+ //@Test
+ @Test
+ public void testUsingVerifier() {
+
+ //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");
+
+ //salary constraint
+ ConstraintConfiguration salaryCons = new SimpleConstraintConfigurationImpl();
+ salaryCons.setFactType("Person");
+ salaryCons.setFieldName("salary");
+ salaryCons.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MIN, "0");
+ salaryCons.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MAX, "1000.6");
+
+ System.out.println("Validation Rule:\n" + cons.getVerifierRule(salaryCons) + "\n\n");
+
+
+ String ruleToVerify = "";
+ int fail = 0;
+
+ //OK
+ ruleToVerify += "package org.drools.factconstraint.test\n\n";
+ ruleToVerify += "import org.drools.factconstraint.model.*\n";
+ ruleToVerify += "rule \"rule1\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == 10)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n\n";
+
+ //FAIL - 1
+ ruleToVerify += "rule \"rule2\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == -5)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n\n";
+ fail++;
+
+ //OK
+ ruleToVerify += "rule \"rule3\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == 100)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+
+ //OK
+ ruleToVerify += "rule \"rule4\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(salary == 100)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+
+ //OK
+ ruleToVerify += "rule \"rule5\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(salary == 89.67)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+
+ //FAIL - 2
+ ruleToVerify += "rule \"rule6\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(salary == 1000.7)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+ fail++;
+
+ //FAIL - 3
+ ruleToVerify += "rule \"rule7\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(salary == 1024)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+ fail++;
+
+ //OK
+ ruleToVerify += "rule \"rule8\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == 45, salary == 1000)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+
+ //FAIL: age - 4
+ ruleToVerify += "rule \"rule9\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == 40, salary == 1011)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+ fail++;
+
+ //FAIL salary - 5
+ ruleToVerify += "rule \"rule10\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == 43, salary == 1007)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+ fail++;
+
+ //FAIL both (creates 2 warnings) - 6,7
+ ruleToVerify += "rule \"rule11\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == 403, salary == 1008)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+ fail += 2;
+
+ //FAIL both (creates 2 warnings) - 8,9
+ ruleToVerify += "rule \"rule12\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == 404, salary == -0.679)\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n";
+ fail += 2;
+
+ VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
+
+ VerifierConfiguration vconf = new VerifierConfigurationImpl();
+
+ vconf.getVerifyingResources().put(ResourceFactory.newByteArrayResource(cons.getVerifierRule(this.conf).getBytes()), ResourceType.DRL);
+ vconf.getVerifyingResources().put(ResourceFactory.newByteArrayResource(cons.getVerifierRule(salaryCons).getBytes()), ResourceType.DRL);
+
+ Verifier verifier = vBuilder.newVerifier(vconf);
+
+ verifier.addResourcesToVerify(ResourceFactory.newByteArrayResource(ruleToVerify.getBytes()),
+ ResourceType.DRL);
+
+ if (verifier.hasErrors()) {
+ for (VerifierError error : verifier.getErrors()) {
+ System.out.println(error.getMessage());
+ }
+ throw new RuntimeException("Error building verifier");
+ }
+
+ assertFalse(verifier.hasErrors());
+
+ boolean noProblems = verifier.fireAnalysis();
+ assertTrue(noProblems);
+
+ VerifierReport result = verifier.getResult();
+
+ Collection<VerifierMessageBase> warnings = result.getBySeverity(Severity.ERROR);
+
+ System.out.println(warnings);
+
+ assertEquals(fail, warnings.size());
+ verifier.dispose();
+ }
+
+ @Test
+ public void testNestedPatternsUsingVerifier() {
+
+ System.out.println("\n\n\n\ntestNestedPatternsUsingVerifier\n");
+
+ //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 ruleToVerify = "";
+ int fail = 0;
+
+ //OK
+ ruleToVerify += "package org.drools.factconstraint.test\n\n";
+ ruleToVerify += "import org.drools.factconstraint.model.*\n";
+// ruleToVerify += "rule \"rule1\"\n";
+// ruleToVerify += " when\n";
+// ruleToVerify += " java.util.List() from collect(Person(age == 10))\n";
+// ruleToVerify += " then\n";
+// ruleToVerify += " System.out.println(\"Rule fired\");\n";
+// ruleToVerify += "end\n\n";
+
+ //FAIL - 1
+ ruleToVerify += "rule \"rule2\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " java.util.List() from collect(Person(age == 10))\n";
+ ruleToVerify += " java.util.List() from collect(Person(age == 130))\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n\n";
+ fail++;
+
+ //FAIL - 2
+ ruleToVerify += "rule \"rule3\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == 10)\n";
+ ruleToVerify += " Person(age == 20)\n";
+ ruleToVerify += " exists (Person (age == 130))\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n\n";
+ fail++;
+
+ ruleToVerify += "rule \"rule4\"\n";
+ ruleToVerify += " when\n";
+ ruleToVerify += " Person(age == 10)\n";
+ ruleToVerify += " exists (Person (age == 30) OR Person (age == 130))\n";
+ ruleToVerify += " then\n";
+ ruleToVerify += " System.out.println(\"Rule fired\");\n";
+ ruleToVerify += "end\n\n";
+ fail++;
+
+
+ VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
+
+ VerifierConfiguration vconf = new VerifierConfigurationImpl();
+
+ vconf.getVerifyingResources().put(ResourceFactory.newByteArrayResource(cons.getVerifierRule(this.conf).getBytes()), ResourceType.DRL);
+
+ Verifier verifier = vBuilder.newVerifier(vconf);
+
+ verifier.addResourcesToVerify(ResourceFactory.newByteArrayResource(ruleToVerify.getBytes()),
+ ResourceType.DRL);
+
+ if (verifier.hasErrors()) {
+ for (VerifierError error : verifier.getErrors()) {
+ System.out.println(error.getMessage());
+ }
+ throw new RuntimeException("Error building verifier");
+ }
+
+ assertFalse(verifier.hasErrors());
+
+ boolean noProblems = verifier.fireAnalysis();
+ assertTrue(noProblems);
+
+ VerifierReport result = verifier.getResult();
+
+ Collection<VerifierMessageBase> errors = result.getBySeverity(Severity.ERROR);
+
+ System.out.println(errors);
+
+ assertEquals(fail, errors.size());
+
+// System.out.println("\nOrders:");
+// for (VerifierMessageBase message : errors) {
+// if (message.getFaulty() instanceof PatternComponent) {
+// int rootPatternOrderNumber = this.getRootPatternOrderNumber((PatternComponent) message.getFaulty());
+// System.out.println(((PatternComponent) message.getFaulty()).getPath()+". Order= "+rootPatternOrderNumber);
+// }
+// }
+
+ verifier.dispose();
+ }
+
+// private int getRootPatternOrderNumber(RuleComponent pattern){
+// if (pattern.getParentPatternComponent() == null){
+// return (pattern instanceof PatternComponent)?((PatternComponent)pattern).getPatternOrderNumber():pattern.getOrderNumber();
+// }else{
+// return getRootPatternOrderNumber(pattern.getParentPatternComponent());
+// }
+// }
+}
Added: labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/test/client/helper/ConstraintsConstrainerTest.java
===================================================================
--- labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/test/client/helper/ConstraintsConstrainerTest.java (rev 0)
+++ labs/jbossrules/tags/5.2.0.M1/drools-factconstraint/src/test/java/org/drools/factconstraints/test/client/helper/ConstraintsConstrainerTest.java 2011-02-24 16:41:17 UTC (rev 36703)
@@ -0,0 +1,81 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.factconstraints.test.client.helper;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.util.LinkedList;
+
+import org.drools.factconstraints.client.ConstraintConfiguration;
+import org.drools.factconstraints.client.config.SimpleConstraintConfigurationImpl;
+import org.drools.factconstraints.client.helper.ConstraintsContainer;
+import org.junit.Test;
+
+
+public class ConstraintsConstrainerTest {
+
+ @Test
+ public void test() {
+ LinkedList<ConstraintConfiguration> list = new LinkedList<ConstraintConfiguration>();
+ ConstraintConfiguration conf = new SimpleConstraintConfigurationImpl();
+ conf.setFactType("Person");
+ conf.setFieldName("age");
+
+ list.add(conf);
+ ConstraintsContainer cc = new ConstraintsContainer(list);
+ assertTrue(cc.hasConstraints("Person"));
+ assertFalse(cc.hasConstraints("Person3"));
+
+ assertEquals(1, cc.getConstraints("Person").size());
+
+ conf = new SimpleConstraintConfigurationImpl();
+ conf.setFactType("Person");
+ conf.setFieldName("name");
+
+ cc.addConstraint(conf);
+ assertEquals(2, cc.getConstraints("Person").size());
+ assertEquals(1, cc.getConstraints("Person", "age").size());
+ assertSame(conf, cc.getConstraints("Person", "name").get(0));
+ assertEquals(0, cc.getConstraints("Person", "toothCount").size());
+
+ conf = new SimpleConstraintConfigurationImpl();
+ conf.setFactType("Pet");
+ conf.setFieldName("name");
+
+ cc.addConstraint(conf);
+
+ assertEquals(1, cc.getConstraints("Pet").size());
+
+ assertEquals(1, cc.getConstraints("Pet", "name").size());
+ }
+
+ @Test
+ public void newConfig() {
+ for (String confName : new String[] {"NotNull", "IntegerConstraint", "RangeConstraint", "NotMatches", "Matches"}) {
+ ConstraintConfiguration conf1 = ConstraintsContainer.getEmptyConfiguration(confName);
+ ConstraintConfiguration conf2 = ConstraintsContainer.getEmptyConfiguration(confName);
+ assertFalse(conf1.equals(conf2));
+
+ assertEquals(conf1.getArgumentKeys(), conf2.getArgumentKeys());
+ }
+
+ }
+
+}
More information about the jboss-svn-commits
mailing list