PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/droolsjbpm-ide-common/src/main/java/org/drools/ide/common/server/factconstraints/DefaultFieldConstraintImpl.java

https://github.com/matnil/guvnor
Java | 327 lines | 220 code | 65 blank | 42 comment | 17 complexity | d7975c1dcf4fc4ceaae90a4eab12f765 MD5 | raw file
  1. /*
  2. * Copyright 2010 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.drools.ide.common.server.factconstraints;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import org.drools.base.evaluators.Operator;
  22. import org.drools.ide.common.server.factconstraints.Constraint;
  23. import org.drools.ide.common.client.factconstraints.ArgumentNotSetException;
  24. import org.drools.ide.common.client.factconstraints.ConstraintConfiguration;
  25. import org.drools.ide.common.client.factconstraints.ValidationResult;
  26. import org.drools.verifier.report.components.Severity;
  27. public abstract class DefaultFieldConstraintImpl implements Constraint {
  28. private static final long serialVersionUID = 501l;
  29. private long ruleNum = 0;
  30. public static final List<Operator> supportedOperators = new ArrayList<Operator>();
  31. static{
  32. supportedOperators.add(Operator.EQUAL);
  33. supportedOperators.add(Operator.NOT_EQUAL);
  34. supportedOperators.add(Operator.GREATER);
  35. supportedOperators.add(Operator.GREATER_OR_EQUAL);
  36. supportedOperators.add(Operator.LESS);
  37. supportedOperators.add(Operator.LESS_OR_EQUAL);
  38. }
  39. /**
  40. * Method used to create the field Restriction. It returns the class name
  41. * of the Restriction used in the generated rule. By default, it returns
  42. * "LiteralRestriction", but subclasses could override this method in order
  43. * to use other subclasses of org.drools.verifier.components.Restriction
  44. * @return
  45. */
  46. protected String getFieldRestrictionClassName(){
  47. return "LiteralRestriction";
  48. }
  49. private String concatRule(ConstraintConfiguration config, Map<String, Object> context) {
  50. StringBuilder rule = new StringBuilder();
  51. rule.append(this.getVerifierPackagePrefixTemplate(config, context));
  52. rule.append(this.getVerifierPackageTemplate(config, context));
  53. rule.append(this.getVerifierPackageSufixTemplate(config, context));
  54. rule.append(this.getVerifierImportsPrefixTemplate(config, context));
  55. rule.append(this.getVerifierImportsTemplate(config, context));
  56. rule.append(this.getVerifierImportsSufixTemplate(config, context));
  57. rule.append(this.getVerifierGlobalsPrefixTemplate(config, context));
  58. rule.append(this.getVerifierGlobalsTemplate(config, context));
  59. rule.append(this.getVerifierGlobalsSufixTemplate(config, context));
  60. rule.append(this.getVerifierRuleNamePrefixTemplate(config, context));
  61. rule.append(this.getVerifierRuleNameTemplate(config, context));
  62. rule.append(this.getVerifierRuleNameSufixTemplate(config, context));
  63. rule.append(this.getVerifierRuleWhenTemplate(config, context));
  64. rule.append(this.getVerifierFieldPatternPrefixTemplate(config, context));
  65. rule.append(this.getVerifierFieldPatternTemplate(config, context));
  66. rule.append(this.getVerifierFieldPatternSufixTemplate(config, context));
  67. rule.append(this.getVerifierRestrictionPatternPrefixTemplate(config, context));
  68. rule.append(this.getVerifierRestrictionPatternTemplate(config, context));
  69. rule.append(this.getVerifierRestrictionPatternSufixTemplate(config, context));
  70. rule.append(this.getVerifierRuleThenTemplate(config, context));
  71. rule.append(this.getVerifierActionPrefixTemplate(config, context));
  72. rule.append(this.getVerifierActionTemplate(config, context));
  73. rule.append(this.getVerifierActionSufixTemplate(config, context));
  74. rule.append(this.getVerifierRuleEndTemplate(config, context));
  75. rule.append(this.getVerifierRuleEndSufixTemplate(config, context));
  76. return rule.toString();
  77. }
  78. protected String createVerifierRuleTemplate(ConstraintConfiguration config, Map<String, Object> context, String ruleName, List<String> constraints, String message) {
  79. if (ruleName == null) {
  80. ruleName = "Constraint_rule";
  81. }
  82. ruleName += "_" + ruleNum++;
  83. String rule = this.concatRule(config, context).replace("${ruleName}", ruleName);
  84. rule = rule.replace("${factType}", config.getFactType());
  85. rule = rule.replace("${fieldName}", config.getFieldName());
  86. StringBuilder constraintsTxt = new StringBuilder();
  87. if (constraints != null && !constraints.isEmpty()) {
  88. for (String c : constraints) {
  89. constraintsTxt.append(",\n");
  90. constraintsTxt.append(c);
  91. }
  92. }
  93. rule = rule.replace("${constraints}", constraintsTxt);
  94. rule = rule.replace("${message}", (message == null || message.equals("")) ? "Invalid Value" : message);
  95. return rule;
  96. }
  97. protected Object getMandatoryArgument(String key, ConstraintConfiguration conf) throws ArgumentNotSetException {
  98. if (!conf.containsArgument(key)) {
  99. throw new ArgumentNotSetException("The argument " + key + " doesn't exist.");
  100. }
  101. Object value = conf.getArgumentValue(key);
  102. if (value == null) {
  103. throw new ArgumentNotSetException("The argument " + key + " is null.");
  104. }
  105. return value;
  106. }
  107. /**
  108. * Used for on-demand validation
  109. * @param value
  110. * @param config
  111. * @return
  112. * @deprecated Use drools-verifier instead of this method:
  113. * {@link #getVerifierRule(org.drools.ide.common.factconstraints.client.ConstraintConfiguration) }
  114. */
  115. @Deprecated
  116. public ValidationResult validate(Object value, ConstraintConfiguration config) {
  117. ValidationResult result = new ValidationResult();
  118. result.setSuccess(true);
  119. return result;
  120. }
  121. protected Map<String, Object> createContext() {
  122. return new HashMap<String, Object>();
  123. }
  124. public final String getVerifierRule(ConstraintConfiguration config) {
  125. return internalVerifierRule(config, createContext());
  126. }
  127. abstract protected String internalVerifierRule(ConstraintConfiguration config, Map<String, Object> context);
  128. public String getConstraintName() {
  129. return getClass().getName().substring(getClass().getName().lastIndexOf('.') + 1);
  130. }
  131. /* Action */
  132. protected String getVerifierActionTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  133. StringBuilder verifierActionTemplate = new StringBuilder();
  134. //by default, add an ERROR
  135. verifierActionTemplate.append(this.addResult(Severity.ERROR));
  136. // verifierActionTemplate.append(" System.out.println(\"doubleValue= \"+$restriction.getDoubleValue());\n");
  137. // verifierActionTemplate.append(" System.out.println(\"intValue= \"+$restriction.getIntValue());\n");
  138. return verifierActionTemplate.toString();
  139. }
  140. protected String addResult(Severity severity){
  141. StringBuilder addString = new StringBuilder();
  142. addString.append(" result.add(new VerifierMessage(\n");
  143. addString.append(" impactedRules,\n");
  144. if (severity.compareTo(Severity.ERROR) == 0){
  145. addString.append(" Severity.ERROR,\n");
  146. }else if(severity.compareTo(Severity.NOTE) == 0){
  147. addString.append(" Severity.NOTE,\n");
  148. }else if(severity.compareTo(Severity.WARNING) == 0){
  149. addString.append(" Severity.WARNING,\n");
  150. }
  151. addString.append(" MessageType.NOT_SPECIFIED,\n");
  152. addString.append(" $restriction,\n");
  153. addString.append(" \"${message}\" ) );\n");
  154. return addString.toString();
  155. }
  156. protected String getVerifierActionPrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  157. StringBuilder verifierActionPrefixTemplate = new StringBuilder();
  158. verifierActionPrefixTemplate.append(" Map<String,String> impactedRules = new HashMap<String,String>();\n")
  159. .append(" impactedRules.put( $restriction.getPackagePath(), $restriction.getRuleName());\n");
  160. return verifierActionPrefixTemplate.toString();
  161. }
  162. protected String getVerifierActionSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  163. return "";
  164. }
  165. /* Field Pattern */
  166. protected String getVerifierFieldPatternTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  167. StringBuilder verifierFieldPatternTemplate = new StringBuilder();
  168. verifierFieldPatternTemplate.append(" $field :Field(\n");
  169. verifierFieldPatternTemplate.append(" objectTypeName == \"${factType}\",\n");
  170. verifierFieldPatternTemplate.append(" name == \"${fieldName}\"\n");
  171. verifierFieldPatternTemplate.append(" )\n");
  172. return verifierFieldPatternTemplate.toString();
  173. }
  174. protected String getVerifierFieldPatternPrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  175. return "";
  176. }
  177. protected String getVerifierFieldPatternSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  178. return "";
  179. }
  180. /* Globals*/
  181. protected String getVerifierGlobalsTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  182. return "global VerifierReport result;\n";
  183. }
  184. protected String getVerifierGlobalsPrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  185. return "";
  186. }
  187. protected String getVerifierGlobalsSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  188. return "";
  189. }
  190. /* Imports */
  191. protected String getVerifierImportsTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  192. StringBuilder verifierImportsTemplate = new StringBuilder();
  193. verifierImportsTemplate.append("import org.drools.verifier.components.*;\n");
  194. verifierImportsTemplate.append("import java.util.Map;\n");
  195. verifierImportsTemplate.append("import java.util.HashMap;\n");
  196. verifierImportsTemplate.append("import org.drools.verifier.report.components.VerifierMessage;\n");
  197. verifierImportsTemplate.append("import org.drools.verifier.data.VerifierReport;\n");
  198. verifierImportsTemplate.append("import org.drools.verifier.report.components.Severity;\n");
  199. verifierImportsTemplate.append("import org.drools.verifier.report.components.MessageType;\n");
  200. return verifierImportsTemplate.toString();
  201. }
  202. protected String getVerifierImportsPrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  203. return "";
  204. }
  205. protected String getVerifierImportsSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  206. return "";
  207. }
  208. protected String getVerifierPackageTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  209. return "package org.drools.verifier.consequence\n";
  210. }
  211. protected String getVerifierPackagePrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  212. return "";
  213. }
  214. protected String getVerifierPackageSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  215. return "";
  216. }
  217. /* Restriction Pattern */
  218. protected String getVerifierRestrictionPatternTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  219. StringBuilder verifierRestrictionPatternTemplate = new StringBuilder();
  220. verifierRestrictionPatternTemplate.append(" $restriction :");
  221. verifierRestrictionPatternTemplate.append(this.getFieldRestrictionClassName());
  222. verifierRestrictionPatternTemplate.append(" (\n");
  223. verifierRestrictionPatternTemplate.append(" fieldPath == $field.path\n");
  224. verifierRestrictionPatternTemplate.append(" ${constraints}\n");
  225. verifierRestrictionPatternTemplate.append(" )\n");
  226. return verifierRestrictionPatternTemplate.toString();
  227. }
  228. protected String getVerifierRestrictionPatternPrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  229. return "";
  230. }
  231. protected String getVerifierRestrictionPatternSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  232. return "";
  233. }
  234. /* end */
  235. protected String getVerifierRuleEndTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  236. return "end\n";
  237. }
  238. protected String getVerifierRuleEndSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  239. return "";
  240. }
  241. /* Rule Name */
  242. protected String getVerifierRuleNameTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  243. return "rule \"${ruleName}\"\n";
  244. }
  245. protected String getVerifierRuleNamePrefixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  246. return "";
  247. }
  248. protected String getVerifierRuleNameSufixTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  249. return "";
  250. }
  251. /* then */
  252. protected String getVerifierRuleThenTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  253. return " then\n";
  254. }
  255. /* when */
  256. protected String getVerifierRuleWhenTemplate(ConstraintConfiguration config, Map<String, Object> context) {
  257. return " when\n";
  258. }
  259. public List<String> getArgumentKeys() {
  260. return new ArrayList<String>();
  261. }
  262. }