001: package org.enhydra.jawe.base.transitionhandler;
002:
003: import java.util.Iterator;
004: import java.util.List;
005: import java.util.Set;
006:
007: import org.enhydra.jawe.JaWEComponent;
008: import org.enhydra.jawe.JaWEManager;
009: import org.enhydra.shark.utilities.SequencedHashMap;
010: import org.enhydra.shark.xpdl.XMLCollectionElement;
011: import org.enhydra.shark.xpdl.XMLUtil;
012: import org.enhydra.shark.xpdl.elements.Activities;
013: import org.enhydra.shark.xpdl.elements.Activity;
014: import org.enhydra.shark.xpdl.elements.Transition;
015: import org.enhydra.shark.xpdl.elements.Transitions;
016:
017: /**
018: * Checks if it is allowed to add activity as a source or a target for a
019: * transition, and if it is allowed to connect two activities.
020: *
021: * @author Sasa Bojanic
022: */
023: public class TransitionHandler {
024:
025: protected TransitionHandlerSettings settings;
026:
027: public TransitionHandler() {
028: settings = new TransitionHandlerSettings();
029: settings.init((JaWEComponent) null);
030: }
031:
032: public TransitionHandler(TransitionHandlerSettings settings) {
033: this .settings = settings;
034: this .settings.init((JaWEComponent) null);
035: }
036:
037: public boolean acceptsSource(Activity act, boolean isExcTrans) {
038: boolean retVal = true;
039: String type = JaWEManager.getInstance().getJaWEController()
040: .getTypeResolver().getJaWEType(act).getTypeId();
041: boolean moreOutgoing = settings
042: .canHaveMoreOutgoingTransition(type);
043:
044: if (!moreOutgoing) {
045: Set nonExcOutgTras = XMLUtil
046: .getNonExceptionalOutgoingTransitions(act);
047: if (nonExcOutgTras.size() > 0 && !moreOutgoing
048: && !isExcTrans) {
049: retVal = false;
050: }
051: }
052:
053: return retVal;
054: }
055:
056: protected boolean acceptsSource(Activity act, Transitions tras,
057: boolean isExcTrans) {
058: boolean retVal = true;
059: String type = JaWEManager.getInstance().getJaWEController()
060: .getTypeResolver().getJaWEType(act).getTypeId();
061: boolean moreOutgoing = settings
062: .canHaveMoreOutgoingTransition(type);
063:
064: if (!moreOutgoing) {
065: Set nonExcOutgTras = XMLUtil
066: .getNonExceptionalOutgoingTransitions(act, tras);
067:
068: if (nonExcOutgTras.size() > 0 && !moreOutgoing
069: && !isExcTrans) {
070: retVal = false;
071: }
072: }
073:
074: return retVal;
075: }
076:
077: public boolean acceptsTarget(Activity act) {
078: boolean retVal = true;
079: String type = JaWEManager.getInstance().getJaWEController()
080: .getTypeResolver().getJaWEType(act).getTypeId();
081: boolean moreIncoming = settings
082: .canHaveMoreIncomingTransition(type);
083:
084: if (!moreIncoming) {
085: Set incTras = XMLUtil.getIncomingTransitions(act);
086:
087: if (incTras.size() > 0 && !moreIncoming) {
088: retVal = false;
089: }
090: }
091:
092: return retVal;
093: }
094:
095: protected boolean acceptsTarget(Activity act, Transitions tras) {
096: boolean retVal = true;
097: String type = JaWEManager.getInstance().getJaWEController()
098: .getTypeResolver().getJaWEType(act).getTypeId();
099: boolean moreIncoming = settings
100: .canHaveMoreIncomingTransition(type);
101:
102: if (!moreIncoming) {
103: Set incTras = XMLUtil.getIncomingTransitions(act);
104:
105: if (incTras.size() > 0 && !moreIncoming) {
106: retVal = false;
107: }
108: }
109:
110: return retVal;
111: }
112:
113: /**
114: *
115: * @param act1
116: * First activity
117: * @param act2
118: * Last activity
119: * @param status
120: * List with first parameter as Integer with status. 0 - everything
121: * is OK, 1 - source activity can't have more outgoing transitions,
122: * 2 - target activity can't have more incoming transitions. 3 -
123: * can't connect two activities twice.
124: * @return true if can connect these two activities.
125: */
126: public boolean allowsConnection(Activity act1, Activity act2,
127: Transition t, boolean isExcTra, List status) {
128: if (status != null)
129: status.add(new Integer(0));
130: String type1 = JaWEManager.getInstance().getJaWEController()
131: .getTypeResolver().getJaWEType(act1).getTypeId();
132: String type2 = JaWEManager.getInstance().getJaWEController()
133: .getTypeResolver().getJaWEType(act2).getTypeId();
134:
135: boolean srcMoreOutgoing = settings
136: .canHaveMoreOutgoingTransition(type1);
137: boolean targetMoreIncoming = settings
138: .canHaveMoreIncomingTransition(type2);
139:
140: // boolean isExceptionalTransition= (t==null ||
141: // XMLUtil.isExceptionalTransition(t) || isExcTra);
142: Set nonExcOutgTras1 = XMLUtil
143: .getNonExceptionalOutgoingTransitions(act1);
144: if (t != null)
145: nonExcOutgTras1.remove(t);
146: if (nonExcOutgTras1.size() > 0 && !srcMoreOutgoing && !isExcTra) {
147: if (status != null)
148: status.set(0, new Integer(1));
149: return false;
150: }
151:
152: Set incTras2 = XMLUtil.getIncomingTransitions(act2);
153: if (t != null)
154: incTras2.remove(t);
155: if (incTras2.size() > 0 && !targetMoreIncoming) {
156: if (status != null)
157: status.set(0, new Integer(2));
158: return false;
159: }
160:
161: Set outgTras = XMLUtil.getOutgoingTransitions(act1);
162: if (t != null)
163: outgTras.remove(t);
164: Iterator it = outgTras.iterator();
165: while (it.hasNext()) {
166: Transition tr = (Transition) it.next();
167: if (tr.getTo().equals(act2.getId())) {
168: if (status != null)
169: status.set(0, new Integer(3));
170: return false;
171: }
172: }
173:
174: return true;
175: }
176:
177: /**
178: * @return true if transition is properly connected.
179: */
180: public boolean isProperlyConnected(Transition t) {
181:
182: String tFrom = t.getFrom();
183: Activity act1 = ((Activities) ((XMLCollectionElement) t
184: .getParent().getParent()).get("Activities"))
185: .getActivity(tFrom);
186:
187: String tTo = t.getTo();
188: Activity act2 = ((Activities) ((XMLCollectionElement) t
189: .getParent().getParent()).get("Activities"))
190: .getActivity(tTo);
191:
192: if (act1 == null || act2 == null)
193: return false;
194:
195: String type1 = JaWEManager.getInstance().getJaWEController()
196: .getTypeResolver().getJaWEType(act1).getTypeId();
197: String type2 = JaWEManager.getInstance().getJaWEController()
198: .getTypeResolver().getJaWEType(act2).getTypeId();
199:
200: boolean srcMoreOutgoing = settings
201: .canHaveMoreOutgoingTransition(type1);
202: boolean targetMoreIncoming = settings
203: .canHaveMoreIncomingTransition(type2);
204:
205: Set nonExcOutgTras1 = XMLUtil
206: .getNonExceptionalOutgoingTransitions(act1,
207: (Transitions) t.getParent());
208: if (nonExcOutgTras1.size() > 1 && !srcMoreOutgoing
209: && !XMLUtil.isExceptionalTransition(t)) {
210: return false;
211: }
212:
213: Set incTras2 = XMLUtil.getIncomingTransitions(act2,
214: (Transitions) t.getParent());
215: if (incTras2.size() > 1 && !targetMoreIncoming) {
216: return false;
217: }
218:
219: Iterator it = XMLUtil.getOutgoingTransitions(act1,
220: (Transitions) t.getParent()).iterator();
221: int count = 0;
222: while (it.hasNext()) {
223: t = (Transition) it.next();
224: if (t.getTo().equals(act2.getId())) {
225: count++;
226: if (count > 1)
227: return false;
228: }
229: }
230:
231: return true;
232: }
233:
234: /**
235: * 0 - everything is OK,
236: * 1 - activity can't have more outgoing transitions,
237: * 2 - activity can't have more incoming transitions,
238: * 3 - both, 1 and 2
239: */
240: public int isProperlyConnected(Activity act) {
241: int ret = 0;
242:
243: String type = JaWEManager.getInstance().getJaWEController()
244: .getTypeResolver().getJaWEType(act).getTypeId();
245:
246: boolean moreOutgoing = settings
247: .canHaveMoreOutgoingTransition(type);
248: boolean moreIncoming = settings
249: .canHaveMoreIncomingTransition(type);
250:
251: Set nonExcOutgTras1 = XMLUtil
252: .getNonExceptionalOutgoingTransitions(act);
253: if (nonExcOutgTras1.size() > 1 && !moreOutgoing) {
254: ret = 1;
255: }
256:
257: Set incTras = XMLUtil.getIncomingTransitions(act);
258: if (incTras.size() > 1 && !moreIncoming) {
259: ret = ret + 2;
260: }
261:
262: return ret;
263: }
264:
265: public SequencedHashMap getPossibleSourceActivities(Transition tra) {
266: SequencedHashMap toRet = new SequencedHashMap();
267: List acts = ((Activities) ((XMLCollectionElement) tra
268: .getParent().getParent()).get("Activities"))
269: .toElements();
270: Iterator it = acts.iterator();
271: while (it.hasNext()) {
272: Activity act = (Activity) it.next();
273: if (acceptsSource(act, (Transitions) tra.getParent(),
274: XMLUtil.isExceptionalTransition(tra))) {
275: toRet.put(act.getId(), act);
276: }
277: }
278: return toRet;
279: }
280:
281: public SequencedHashMap getPossibleTargetActivities(Transition tra) {
282: SequencedHashMap toRet = new SequencedHashMap();
283: List acts = ((Activities) ((XMLCollectionElement) tra
284: .getParent().getParent()).get("Activities"))
285: .toElements();
286: Iterator it = acts.iterator();
287: while (it.hasNext()) {
288: Activity act = (Activity) it.next();
289: if (acceptsTarget(act, (Transitions) tra.getParent())) {
290: toRet.put(act.getId(), act);
291: }
292: }
293: return toRet;
294: }
295:
296: }
|