001: /*
002: * ====================================================================
003: *
004: * XFLOW - Process Management System
005: * Copyright (C) 2003 Rob Tan
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions, and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions, and the disclaimer that follows
017: * these conditions in the documentation and/or other materials
018: * provided with the distribution.
019: *
020: * 3. The name "XFlow" must not be used to endorse or promote products
021: * derived from this software without prior written permission. For
022: * written permission, please contact rcktan@yahoo.com
023: *
024: * 4. Products derived from this software may not be called "XFlow", nor
025: * may "XFlow" appear in their name, without prior written permission
026: * from the XFlow Project Management (rcktan@yahoo.com)
027: *
028: * In addition, we request (but do not require) that you include in the
029: * end-user documentation provided with the redistribution and/or in the
030: * software itself an acknowledgement equivalent to the following:
031: * "This product includes software developed by the
032: * XFlow Project (http://xflow.sourceforge.net/)."
033: * Alternatively, the acknowledgment may be graphical using the logos
034: * available at http://xflow.sourceforge.net/
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE XFLOW AUTHORS OR THE PROJECT
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: * ====================================================================
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the XFlow Project and was originally
052: * created by Rob Tan (rcktan@yahoo.com)
053: * For more information on the XFlow Project, please see:
054: * <http://xflow.sourceforge.net/>.
055: * ====================================================================
056: */
057:
058: package xflow.server.controller;
059:
060: import java.lang.reflect.*;
061: import java.util.*;
062: import org.apache.log4j.Logger;
063:
064: public class ExpressionEval {
065:
066: private static Logger log = Logger.getLogger(ExpressionEval.class);
067:
068: public boolean evaluateRule(Object object, String rule) {
069: boolean result = true;
070:
071: log.info("Evaluating rule on Java object: " + rule);
072:
073: // Break the rule string into components:
074: // LHS class, LHS method, operator, literal (TBD RHS class & method)
075: StringTokenizer strTok = new StringTokenizer(rule, " ");
076: String lhsTok = null;
077: String opTok = null;
078: String litTok = null;
079: if (strTok.hasMoreTokens()) {
080: lhsTok = strTok.nextToken();
081: //if (lhsTok.startsWith ("get") == false) {
082: // lhsTok = "get" + lhsTok;
083: //}
084: }
085: if (strTok.hasMoreTokens()) {
086: opTok = strTok.nextToken();
087: }
088: if (strTok.hasMoreTokens()) {
089: litTok = strTok.nextToken();
090: if (litTok.startsWith("'")) {
091: StringTokenizer tk2 = new StringTokenizer(rule, "'");
092: String dontCare = tk2.nextToken();
093: litTok = tk2.nextToken();
094: }
095: }
096:
097: // Must have a well-formed rule string
098: if (lhsTok == null || opTok == null || litTok == null) {
099: log.error("No rule or malformed rule");
100: return false;
101: }
102:
103: // Reflect on workflow object
104: try {
105: Class workflowObjectClass = object.getClass();
106:
107: // Get the method and return type for workflow object
108: // System.out.println ("Invoking " + lhsMethodTok + " on " +
109: // lhsClassTok);
110: Method method = workflowObjectClass.getMethod(lhsTok,
111: new Class[] {});
112: Object resultObj = method.invoke(object, new Object[] {});
113: //System.out.println ("Result is " + resultObj);
114: result = applyRule(resultObj, opTok, litTok);
115: } catch (Exception e) {
116: e.printStackTrace();
117: }
118:
119: return result;
120: }
121:
122: public boolean applyRule(Object o, String op, String lit) {
123: Class oClass = o.getClass();
124: String className = oClass.getName();
125: boolean result = false;
126:
127: //System.out.println ("className = " + className);
128: log.info("Applying rule: " + o + " " + op + " " + lit);
129:
130: if (className.equals("java.lang.Integer")) {
131: Integer iobj = (Integer) o;
132: int lhsVal = iobj.intValue();
133: iobj = new Integer(lit);
134: int rhsVal = iobj.intValue();
135: if (op.equals("==")) {
136: result = (lhsVal == rhsVal);
137: } else if (op.equals("!=")) {
138: result = (lhsVal != rhsVal);
139: } else if (op.equals(">=")) {
140: result = (lhsVal >= rhsVal);
141: } else if (op.equals("<=")) {
142: result = (lhsVal <= rhsVal);
143: } else if (op.equals(">")) {
144: result = (lhsVal > rhsVal);
145: } else if (op.equals("<")) {
146: result = (lhsVal < rhsVal);
147: }
148: } else if (className.equals("java.lang.Float")) {
149: Float iobj = (Float) o;
150: float lhsVal = iobj.floatValue();
151: iobj = new Float(lit);
152: float rhsVal = iobj.floatValue();
153: if (op.equals("==")) {
154: result = (lhsVal == rhsVal);
155: } else if (op.equals("!=")) {
156: result = (lhsVal != rhsVal);
157: } else if (op.equals(">=")) {
158: result = (lhsVal >= rhsVal);
159: } else if (op.equals("<=")) {
160: result = (lhsVal <= rhsVal);
161: } else if (op.equals(">")) {
162: result = (lhsVal > rhsVal);
163: } else if (op.equals("<")) {
164: result = (lhsVal < rhsVal);
165: }
166: } else if (className.equals("java.lang.Double")) {
167: Double iobj = (Double) o;
168: double lhsVal = iobj.doubleValue();
169: iobj = new Double(lit);
170: double rhsVal = iobj.doubleValue();
171: if (op.equals("==")) {
172: result = (lhsVal == rhsVal);
173: } else if (op.equals("!=")) {
174: result = (lhsVal != rhsVal);
175: } else if (op.equals(">=")) {
176: result = (lhsVal >= rhsVal);
177: } else if (op.equals("<=")) {
178: result = (lhsVal <= rhsVal);
179: } else if (op.equals(">")) {
180: result = (lhsVal > rhsVal);
181: } else if (op.equals("<")) {
182: result = (lhsVal < rhsVal);
183: }
184: } else if (className.equals("java.lang.Boolean")) {
185: Boolean iobj = (Boolean) o;
186: boolean lhsVal = iobj.booleanValue();
187: iobj = new Boolean(lit);
188: boolean rhsVal = iobj.booleanValue();
189: if (op.equals("==")) {
190: result = (lhsVal == rhsVal);
191: } else if (op.equals("!=")) {
192: result = (lhsVal != rhsVal);
193: }
194: } else if (className.equals("java.lang.String")) {
195: String lhsVal = (String) o;
196: String rhsVal = lit;
197: if (op.equals("==")) {
198: result = (lhsVal.equals(rhsVal));
199: } else if (op.equals("!=")) {
200: result = (!lhsVal.equals(rhsVal));
201: }
202: }
203:
204: log.info("apply Rule returning: " + result);
205: return result;
206: }
207: }
|