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.util.*;
061: import java.util.regex.*;
062: import java.io.*;
063:
064: import xflow.common.*;
065: import org.apache.log4j.Logger;
066:
067: public class RuleEngine {
068:
069: private static Logger log = Logger.getLogger(RuleEngine.class);
070:
071: private static String clean(String exp) {
072:
073: Pattern p = Pattern.compile("\\[ *");
074: Matcher m = p.matcher(exp);
075: String r = m.replaceAll("\\[");
076: p = Pattern.compile(" *\\]");
077: m = p.matcher(r);
078: r = m.replaceAll("\\]");
079: return r;
080: }
081:
082: public static boolean evaluate(WorkItem witem, String r)
083: throws XflowException {
084:
085: boolean result = true;
086:
087: ExpressionEval exeval = new ExpressionEval();
088: String rule = clean(r);
089:
090: rule = rule.substring(1);
091: StringTokenizer strtok = new StringTokenizer(rule, "]");
092: rule = strtok.nextToken();
093:
094: strtok = new StringTokenizer(rule, " ");
095: String tok = strtok.nextToken();
096:
097: StringTokenizer strtok2 = new StringTokenizer(tok, ".");
098: tok = strtok2.nextToken();
099:
100: if (tok != null && tok.equals("property")) {
101: String propName = strtok2.nextToken();
102: String oper = strtok.nextToken();
103: String propValue = strtok.nextToken();
104: log.info("propName = " + propName);
105: log.info("oper = " + oper);
106: log.info("propValue = " + propValue);
107: Object prop = witem.getProperty(propName);
108: if (prop == null) {
109: throw new XflowException("Property does not exist: "
110: + propName);
111: }
112: result = new ExpressionEval().applyRule(prop, oper,
113: propValue);
114:
115: } else {
116: String payloadType = witem.getPayloadType();
117: if (payloadType == null) {
118: throw new XflowException(
119: "Payload type not defined in work item");
120: }
121:
122: // If payload is XML, evaluate rule on XML payload
123: if (payloadType.equals(WorkItem.XML)) {
124: result = evaluateRuleOnXmlPayload((String) witem
125: .getPayload(), r);
126: // Java Payload
127: } else if (payloadType.equals(WorkItem.JAVA_OBJECT)) {
128: result = new ExpressionEval().evaluateRule(witem
129: .getPayload(), rule);
130: }
131: }
132: return result;
133: }
134:
135: public static boolean evaluateRuleOnXmlPayload(String xml,
136: String rule) throws XflowException {
137:
138: boolean result = true;
139: XPathRuleEngine re = new XPathRuleEngine();
140: return re.executeRule(xml, rule, null); // Not name space aware for now
141: }
142:
143: }
|