001: /*
002: * Bossa Workflow System
003: *
004: * $Id: Edge.java,v 1.11 2004/01/15 22:13:32 gdvieira Exp $
005: *
006: * Copyright (C) 2003,2004 OpenBR Sistemas S/C Ltda.
007: *
008: * This file is part of Bossa.
009: *
010: * Bossa is free software; you can redistribute it and/or modify it
011: * under the terms of version 2 of the GNU General Public License as
012: * published by the Free Software Foundation.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public
020: * License along with this program; if not, write to the
021: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
022: * Boston, MA 02111-1307, USA.
023: */
024:
025: package com.bigbross.bossa.wfnet;
026:
027: import java.io.Serializable;
028:
029: /**
030: * This class represents one edge of one transition. It may be an input or
031: * output edge, mapping the precondition or postcondition of one transition. <p>
032: *
033: * @author <a href="http://www.bigbross.com">BigBross Team</a>
034: */
035: public abstract class Edge implements Serializable {
036:
037: protected static final Integer INACTIVE = new Integer(0);
038:
039: protected Object expression = INACTIVE;
040:
041: private Place place;
042:
043: /**
044: * Creates a new edge, with the provided weight expression and
045: * connected place. For an input edge the connected place will be a
046: * source place, and for an output edge it will be a sink place. <p>
047: *
048: * @param place the connected place.
049: * @param expression the weight expression.
050: */
051: protected Edge(Place place, String expression) {
052: this .place = place;
053: try {
054: this .expression = Integer.valueOf(expression);
055: } catch (NumberFormatException e) {
056: this .expression = expression;
057: }
058: }
059:
060: /**
061: * Returns the place this edge is connected to. For an input edge the
062: * connected place will be a source place, and for an output edge it
063: * will be a sink place. <p>
064: *
065: * @return the place this edge is connected to.
066: */
067: Place getPlace() {
068: return this .place;
069: }
070:
071: /**
072: * Returns the weight of this edge. <p>
073: *
074: * @return A negative, zero, or a positive integer as this edge is a
075: * precondition, no-condition, or a postcondition.
076: * @exception EvaluationException if an expression evaluation error
077: * occurs.
078: */
079: int weight(Case caze) throws EvaluationException {
080: return INACTIVE.intValue();
081: }
082:
083: /**
084: * Returns the weight of a precondition edge. <p>
085: *
086: * @return A positive integer if this edge is a precondition,
087: * zero otherwise.
088: * @exception EvaluationException if an expression evaluation error
089: * occurs.
090: */
091: int input(Case caze) throws EvaluationException {
092: return INACTIVE.intValue();
093: }
094:
095: /**
096: * Returns the weight of a postcondition edge. <p>
097: *
098: * @return A positive integer if this edge is a precondition,
099: * zero otherwise.
100: * @exception EvaluationException if an expression evaluation error
101: * occurs.
102: */
103: int output(Case caze) throws EvaluationException {
104: return INACTIVE.intValue();
105: }
106:
107: /**
108: * Evaluates an integer expression representing the edge weight
109: * using the case attributes. <p>
110: *
111: * @param caze the case that contains the attributes.
112: * @return The value of the expression, a positive integer.
113: * @exception EvaluationException if an expression evaluation error
114: * occurs.
115: */
116: protected int eval(Case caze) throws EvaluationException {
117: int result;
118:
119: if (expression instanceof Integer) {
120: result = ((Integer) expression).intValue();
121: } else {
122: result = caze.eval((String) expression);
123: }
124:
125: if (result < 0) {
126: throw new EvaluationException(
127: "The edge absolute weight must "
128: + "be positive. Was: " + result);
129: }
130:
131: return result;
132: }
133:
134: /**
135: * Creates a new input edge, with the transition precondition
136: * weight expression and source place. <p>
137: *
138: * @param place the connected place.
139: * @param expression the weight expression.
140: * @return the input edge.
141: */
142: static Edge newInput(Place place, String expression) {
143: return new Edge(place, expression) {
144: int weight(Case caze) throws EvaluationException {
145: return -this .eval(caze);
146: }
147:
148: int input(Case caze) throws EvaluationException {
149: return this .eval(caze);
150: }
151:
152: public String toString() {
153: return "-" + this .expression;
154: }
155: };
156: }
157:
158: /**
159: * Creates a new output edge, with the transition postcondition
160: * weight expression and sink place. <p>
161: *
162: * @param place the connected place.
163: * @param expression the expression.
164: * @return the output edge.
165: */
166: static Edge newOutput(Place place, String expression) {
167: return new Edge(place, expression) {
168: int weight(Case caze) throws EvaluationException {
169: return this .eval(caze);
170: }
171:
172: int output(Case caze) throws EvaluationException {
173: return this .eval(caze);
174: }
175: };
176: }
177:
178: /**
179: * Returns a string with the condition expression. <p>
180: *
181: * @return a string representation of this edge.
182: */
183: public String toString() {
184: return expression.toString();
185: }
186: }
|