PageRenderTime 72ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/machinelearning/5.0.x/drools-compiler/src/main/java/org/drools/lang/MVELDumper.java

https://github.com/etirelli/droolsjbpm-contributed-experiments
Java | 187 lines | 145 code | 21 blank | 21 comment | 34 complexity | 5b9b748e198c8197411d119102cf3b82 MD5 | raw file
  1. package org.drools.lang;
  2. /*
  3. * Author Jayaram C S
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import java.util.Iterator;
  18. import org.drools.base.evaluators.Operator;
  19. import org.drools.lang.descr.FieldBindingDescr;
  20. import org.drools.lang.descr.FieldConstraintDescr;
  21. import org.drools.lang.descr.LiteralRestrictionDescr;
  22. import org.drools.lang.descr.PredicateDescr;
  23. import org.drools.lang.descr.QualifiedIdentifierRestrictionDescr;
  24. import org.drools.lang.descr.RestrictionConnectiveDescr;
  25. import org.drools.lang.descr.ReturnValueRestrictionDescr;
  26. import org.drools.lang.descr.VariableRestrictionDescr;
  27. import org.drools.util.ReflectiveVisitor;
  28. /**
  29. *
  30. * @author <a href="mailto:jayaramcs@gmail.com">Author Jayaram C S</a>
  31. *
  32. */
  33. public class MVELDumper extends ReflectiveVisitor {
  34. private StringBuffer mvelDump;
  35. private boolean isDateField;
  36. private static final String eol = System.getProperty( "line.separator" );
  37. private String template;
  38. private String fieldName;
  39. public String dump(FieldConstraintDescr fieldConstr) {
  40. return this.dump( fieldConstr, false );
  41. }
  42. public String dump(FieldConstraintDescr fieldConstr, boolean isDateField ) {
  43. mvelDump = new StringBuffer();
  44. this.isDateField = isDateField;
  45. this.visit( fieldConstr );
  46. return mvelDump.toString();
  47. }
  48. public void visitFieldConstraintDescr(final FieldConstraintDescr descr) {
  49. if ( !descr.getRestrictions().isEmpty() ) {
  50. this.fieldName = descr.getFieldName();
  51. mvelDump.append( processFieldConstraint( descr.getRestriction() ) );
  52. }
  53. }
  54. public void visitVariableRestrictionDescr(final VariableRestrictionDescr descr) {
  55. this.template = processRestriction( descr.getEvaluator(), descr.isNegated(), descr.getIdentifier() );
  56. }
  57. public void visitFieldBindingDescr(final FieldBindingDescr descr) {
  58. // do nothing
  59. }
  60. public void visitLiteralRestrictionDescr(final LiteralRestrictionDescr descr) {
  61. String text = descr.getText();
  62. if ( text == null || descr.getType() == LiteralRestrictionDescr.TYPE_NULL ) {
  63. text = "null";
  64. } else if( descr.getType() == LiteralRestrictionDescr.TYPE_NUMBER ) {
  65. try {
  66. Integer.parseInt( text );
  67. } catch ( final NumberFormatException e ) {
  68. text = "\"" + text + "\"";
  69. }
  70. } else if( descr.getType() == LiteralRestrictionDescr.TYPE_STRING ) {
  71. text = "\"" + text + "\"";
  72. if( this.isDateField ) {
  73. text = "org.drools.util.DateUtils.parseDate( "+text+" )";
  74. }
  75. }
  76. this.template = processRestriction( descr.getEvaluator(), descr.isNegated(), text );
  77. }
  78. public void visitQualifiedIdentifierRestrictionDescr(final QualifiedIdentifierRestrictionDescr descr) {
  79. this.template = processRestriction( descr.getEvaluator(), descr.isNegated(), descr.getText() );
  80. }
  81. public void visitRestrictionConnectiveDescr(final RestrictionConnectiveDescr descr) {
  82. this.template = "( " + this.processFieldConstraint( descr ) + " )";
  83. }
  84. public void visitPredicateDescr(final PredicateDescr descr) {
  85. this.template = "eval( " + descr.getContent() + " )";
  86. }
  87. public void visitReturnValueRestrictionDescr(final ReturnValueRestrictionDescr descr) {
  88. this.template = processRestriction( descr.getEvaluator(), descr.isNegated(), "( "+descr.getContent().toString()+" )" );
  89. }
  90. private String processFieldConstraint(final RestrictionConnectiveDescr restriction) {
  91. String descrString = "";
  92. String connective = null;
  93. if ( restriction.getConnective() == RestrictionConnectiveDescr.OR ) {
  94. connective = " || ";
  95. } else {
  96. connective = " && ";
  97. }
  98. for ( final Iterator it = restriction.getRestrictions().iterator(); it.hasNext(); ) {
  99. final Object temp = it.next();
  100. visit( temp );
  101. descrString += this.template;
  102. if ( it.hasNext() ) {
  103. descrString += connective;
  104. }
  105. }
  106. return descrString;
  107. }
  108. private String processRestriction(String evaluator,
  109. boolean isNegated,
  110. String value) {
  111. Operator op = Operator.determineOperator( evaluator, isNegated );
  112. if( op == Operator.determineOperator( "memberOf", false ) ) {
  113. evaluator = "contains";
  114. return evaluatorPrefix( evaluator ) +
  115. value + " " +
  116. evaluator( evaluator ) + " " +
  117. this.fieldName + evaluatorSufix( evaluator );
  118. } else if(op == Operator.determineOperator( "memberOf", true )) {
  119. evaluator = "not contains";
  120. return evaluatorPrefix( evaluator ) +
  121. value + " " +
  122. evaluator( evaluator ) + " " +
  123. this.fieldName + evaluatorSufix( evaluator );
  124. } else if(op == Operator.determineOperator( "excludes", false ) ) {
  125. evaluator = "not contains";
  126. return evaluatorPrefix( evaluator ) +
  127. this.fieldName + " " +
  128. evaluator( evaluator ) + " " +
  129. value + evaluatorSufix( evaluator );
  130. } else if(op == Operator.determineOperator( "matches", false )) {
  131. evaluator = "~=";
  132. return evaluatorPrefix( evaluator ) +
  133. this.fieldName + " " +
  134. evaluator( evaluator ) + " " +
  135. value.replaceAll( "\\\\", "\\\\\\\\" ) + evaluatorSufix( evaluator );
  136. } else if(op == Operator.determineOperator( "matches", true )) {
  137. evaluator = "not ~=";
  138. return evaluatorPrefix( evaluator ) +
  139. this.fieldName + " " +
  140. evaluator( evaluator ) + " " +
  141. value.replaceAll( "\\\\", "\\\\\\\\" ) + evaluatorSufix( evaluator );
  142. }
  143. return evaluatorPrefix( evaluator ) +
  144. this.fieldName + " " +
  145. evaluator( evaluator ) + " " +
  146. value + evaluatorSufix( evaluator );
  147. }
  148. private String evaluatorPrefix(String evaluator) {
  149. if ( evaluator.startsWith( "not" ) ) {
  150. return "!( ";
  151. }
  152. return "";
  153. }
  154. private String evaluator(String evaluator) {
  155. if ( evaluator.startsWith( "not" ) ) {
  156. return evaluator.substring( 4 );
  157. }
  158. return evaluator;
  159. }
  160. private String evaluatorSufix(String evaluator) {
  161. if ( evaluator.startsWith( "not" ) ) {
  162. return " )";
  163. }
  164. return "";
  165. }
  166. }