001: package hero.entity;
002:
003: import hero.interfaces.BnNodeLocal;
004: import hero.interfaces.Constants;
005: import hero.util.BonitaProjectLocator;
006: import hero.util.BonitaServiceValue;
007: import hero.util.values.BonitaEdgeValue;
008: import hero.util.values.BonitaProjectValue;
009:
010: import java.util.ArrayList;
011: import java.util.Collection;
012: import java.util.Iterator;
013:
014: /*
015: * 02/01/2002 - 15:24:07
016: *
017: * AndEdgeState.java -
018: * Copyright (C) 2002 Ecoo Team
019: * charoy@loria.fr
020: *
021: *
022: * This program is free software; you can redistribute it and/or
023: * modify it under the terms of the GNU Lesser General Public License
024: * as published by the Free Software Foundation; either version 2
025: * of the License, or (at your option) any later version.
026: *
027: * This program is distributed in the hope that it will be useful,
028: * but WITHOUT ANY WARRANTY; without even the implied warranty of
029: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
030: * GNU Lesser General Public License for more details.
031: *
032: * You should have received a copy of the GNU Lesser General Public License
033: * along with this program; if not, write to the Free Software
034: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
035: */
036:
037: public class AndEdgeState extends EdgeState implements
038: java.io.Serializable {
039:
040: private static AndEdgeState nd;
041:
042: public AndEdgeState() {
043: }
044:
045: public static AndEdgeState getInstance() {
046: if (nd == null) {
047: nd = new AndEdgeState();
048: }
049: return nd;
050: }
051:
052: public int state(BnNodeLocal node) {
053: Collection edgesInstance = new ArrayList();
054: Collection edgesModel = new ArrayList();
055:
056: try {
057: edgesInstance = BonitaServiceValue.getNode(
058: node.getBnProject(), node.getName()).getInEdges();
059: } catch (Exception e) {
060: e.printStackTrace();
061: }
062:
063: // Instances consistency (may be some activities are not yet created in the instance)
064: String nodeName = node.getName();
065: if (isInstance(node.getName()))
066: nodeName = getModel(nodeName);
067:
068: try {
069: String modelName = this .getModel(node.getBnProject()
070: .getName());
071: String modelVersion = node.getBnProject().getVersion();
072: BonitaProjectValue model = BonitaProjectLocator
073: .getInstance().getModelValue(modelName,
074: modelVersion);
075: if (node.getBnProject().getType().equals(
076: Constants.Pj.COOPERATIVE))
077: edgesModel = BonitaServiceValue.getNode(
078: model.getName(), model.getVersion(), nodeName)
079: .getInEdges();
080: else
081: edgesModel = BonitaServiceValue.getNodeFromCache(model,
082: nodeName).getInEdges();
083: } catch (Exception e) {
084: edgesModel = new ArrayList();
085: }
086:
087: if ((edgesInstance.size() > 0) || (edgesModel.size() > 0)) {
088: edgesInstance = concatEdges(edgesInstance, edgesModel);
089: }
090:
091: // If some of the edges state is DEAD in an AND -> return CANCEL over the next node
092: Iterator it = edgesInstance.iterator();
093: while (it.hasNext()) {
094: BonitaEdgeValue edge = (BonitaEdgeValue) it.next();
095: if ((edge.getState() == Constants.Ed.DEAD)) {
096: return Constants.Nd.CANCEL;
097: }
098: }
099:
100: it = edgesInstance.iterator();
101: int result = Constants.Nd.ACTIVE;
102: while (it.hasNext()) {
103: BonitaEdgeValue edge = (BonitaEdgeValue) it.next();
104: if (edge.getState() == Constants.Ed.INITIAL) {
105: result = Constants.Nd.EDGEINITIAL;
106: break;
107: }
108: if ((edge.getState() == Constants.Ed.ANTICIPATING)) {
109: result = Constants.Nd.ANTACTIVE;
110: }
111: }
112: return result;
113: }
114:
115: private String getModel(String instanceName) {
116: int i = instanceName.indexOf("_instance");
117: return (instanceName.substring(0, i));
118: }
119:
120: private boolean isInstance(String name) {
121: return (name.matches(".*_instance.*"));
122: }
123:
124: /**
125: * Concatenates model edges that doesn't exist in the instances edges (without repetition)
126: *
127: * @param edgesInstance
128: * @param edgesModel
129: * @return
130: */
131: private Collection concatEdges(Collection edgesInstance,
132: Collection edgesModel) {
133: Collection result = new ArrayList();
134: boolean found = false;
135:
136: result = edgesInstance;
137:
138: Iterator it = edgesModel.iterator();
139: while (it.hasNext()) {
140: found = false;
141: BonitaEdgeValue el = (BonitaEdgeValue) it.next();
142: String nodeName = el.getInNode();
143: Iterator instanceEdgesIt = edgesInstance.iterator();
144: while (instanceEdgesIt.hasNext()) {
145: BonitaEdgeValue insEd = (BonitaEdgeValue) instanceEdgesIt
146: .next();
147: String node = insEd.getInNode();
148: if (this .isInstance(node)) {
149: node = this .getModel(node);
150: }
151: if (nodeName.equals(node)) {
152: found = true;
153: break;
154: }
155: }
156: if (!found) {
157: result.add(el);
158: }
159: }
160: return result;
161: }
162: }
|