001: package org.drools.lang;
002:
003: /*
004: * Author Jayaram C S
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Iterator;
020:
021: import org.drools.base.evaluators.Operator;
022: import org.drools.lang.descr.FieldBindingDescr;
023: import org.drools.lang.descr.FieldConstraintDescr;
024: import org.drools.lang.descr.LiteralRestrictionDescr;
025: import org.drools.lang.descr.PredicateDescr;
026: import org.drools.lang.descr.QualifiedIdentifierRestrictionDescr;
027: import org.drools.lang.descr.RestrictionConnectiveDescr;
028: import org.drools.lang.descr.ReturnValueRestrictionDescr;
029: import org.drools.lang.descr.VariableRestrictionDescr;
030: import org.drools.util.ReflectiveVisitor;
031:
032: /**
033: *
034: * @author <a href="mailto:jayaramcs@gmail.com">Author Jayaram C S</a>
035: *
036: */
037: public class MVELDumper extends ReflectiveVisitor {
038:
039: private StringBuffer mvelDump;
040: private static final String eol = System
041: .getProperty("line.separator");
042: private String template;
043: private String fieldName;
044:
045: public String dump(FieldConstraintDescr fieldConstr) {
046: mvelDump = new StringBuffer();
047: this .visit(fieldConstr);
048: return mvelDump.toString();
049: }
050:
051: public void visitFieldConstraintDescr(
052: final FieldConstraintDescr descr) {
053: if (!descr.getRestrictions().isEmpty()) {
054: this .fieldName = descr.getFieldName();
055: mvelDump.append(processFieldConstraint(descr
056: .getRestriction()));
057: }
058: }
059:
060: public void visitVariableRestrictionDescr(
061: final VariableRestrictionDescr descr) {
062: this .template = processRestriction(descr.getEvaluator(), descr
063: .getIdentifier());
064: }
065:
066: public void visitFieldBindingDescr(final FieldBindingDescr descr) {
067: // do nothing
068: }
069:
070: public void visitLiteralRestrictionDescr(
071: final LiteralRestrictionDescr descr) {
072: String text = descr.getText();
073: if (text == null
074: || descr.getType() == LiteralRestrictionDescr.TYPE_NULL) {
075: text = "null";
076: } else if (descr.getType() == LiteralRestrictionDescr.TYPE_NUMBER) {
077: try {
078: Integer.parseInt(text);
079: } catch (final NumberFormatException e) {
080: text = "\"" + text + "\"";
081: }
082: } else if (descr.getType() == LiteralRestrictionDescr.TYPE_STRING) {
083: text = "\"" + text + "\"";
084: }
085: this .template = processRestriction(descr.getEvaluator(), text);
086: }
087:
088: public void visitQualifiedIdentifierRestrictionDescr(
089: final QualifiedIdentifierRestrictionDescr descr) {
090: this .template = processRestriction(descr.getEvaluator(), descr
091: .getText());
092: }
093:
094: public void visitRestrictionConnectiveDescr(
095: final RestrictionConnectiveDescr descr) {
096: this .template = "( " + this .processFieldConstraint(descr)
097: + " )";
098: }
099:
100: public void visitPredicateDescr(final PredicateDescr descr) {
101: this .template = "eval( " + descr.getContent() + " )";
102: }
103:
104: public void visitReturnValueRestrictionDescr(
105: final ReturnValueRestrictionDescr descr) {
106: this .template = processRestriction(descr.getEvaluator(), "( "
107: + descr.getContent().toString() + " )");
108: }
109:
110: private String processFieldConstraint(
111: final RestrictionConnectiveDescr restriction) {
112: String descrString = "";
113: String connective = null;
114:
115: if (restriction.getConnective() == RestrictionConnectiveDescr.OR) {
116: connective = " || ";
117: } else {
118: connective = " && ";
119: }
120: for (final Iterator it = restriction.getRestrictions()
121: .iterator(); it.hasNext();) {
122: final Object temp = it.next();
123: visit(temp);
124: descrString += this .template;
125: if (it.hasNext()) {
126: descrString += connective;
127: }
128: }
129: return descrString;
130: }
131:
132: private String processRestriction(String evaluator, String value) {
133: Operator op = Operator.determineOperator(evaluator);
134: if (op == Operator.MEMBEROF) {
135: evaluator = "contains";
136: return evaluatorPrefix(evaluator) + value + " "
137: + evaluator(evaluator) + " " + this .fieldName
138: + evaluatorSufix(evaluator);
139: } else if (op == Operator.NOTMEMBEROF) {
140: evaluator = "not contains";
141: return evaluatorPrefix(evaluator) + value + " "
142: + evaluator(evaluator) + " " + this .fieldName
143: + evaluatorSufix(evaluator);
144: } else if (op == Operator.EXCLUDES) {
145: evaluator = "not contains";
146: return evaluatorPrefix(evaluator) + this .fieldName + " "
147: + evaluator(evaluator) + " " + value
148: + evaluatorSufix(evaluator);
149: } else if (op == Operator.MATCHES) {
150: evaluator = "~=";
151: return evaluatorPrefix(evaluator) + this .fieldName + " "
152: + evaluator(evaluator) + " " + value
153: + evaluatorSufix(evaluator);
154: } else if (op == Operator.NOT_MATCHES) {
155: evaluator = "not ~=";
156: return evaluatorPrefix(evaluator) + this .fieldName + " "
157: + evaluator(evaluator) + " " + value
158: + evaluatorSufix(evaluator);
159: }
160: return evaluatorPrefix(evaluator) + this .fieldName + " "
161: + evaluator(evaluator) + " " + value
162: + evaluatorSufix(evaluator);
163: }
164:
165: private String evaluatorPrefix(String evaluator) {
166: if (evaluator.startsWith("not")) {
167: return "!( ";
168: }
169: return "";
170: }
171:
172: private String evaluator(String evaluator) {
173: if (evaluator.startsWith("not")) {
174: return evaluator.substring(4);
175: }
176: return evaluator;
177: }
178:
179: private String evaluatorSufix(String evaluator) {
180: if (evaluator.startsWith("not")) {
181: return " )";
182: }
183: return "";
184: }
185: }
|