001: package hero.entity;
002:
003: import hero.interfaces.Constants;
004: import hero.interfaces.BnNodeLocal;
005: import hero.interfaces.BnEdgeLocal;
006: import hero.util.BonitaProjectLocator;
007: import hero.util.BonitaServiceException;
008: import hero.util.BonitaServiceValue;
009: import hero.util.HeroException;
010: import hero.util.values.BonitaEdgeValue;
011: import hero.util.values.BonitaProjectValue;
012:
013: import java.util.*;
014:
015: public class OrEdgeState extends EdgeState {
016:
017: private static OrEdgeState nd;
018:
019: public OrEdgeState() {
020: }
021:
022: public static OrEdgeState getInstance() {
023: if (nd == null) {
024: nd = new OrEdgeState();
025: }
026: return nd;
027: }
028:
029: public int state(BnNodeLocal node) {
030: Collection edgesInstance = new ArrayList();
031: Collection edgesModel = new ArrayList();
032:
033: try {
034: edgesInstance = BonitaServiceValue.getNode(
035: node.getBnProject(), node.getName()).getInEdges();
036: } catch (Exception e) {
037: e.printStackTrace();
038: }
039:
040: // Instances consistency (may be some activities are not yet created in the instance)
041: String nodeName = node.getName();
042: if (isInstance(node.getName()))
043: nodeName = getModel(nodeName);
044: try {
045: BonitaProjectValue model = BonitaProjectLocator
046: .getInstance().getModelValue(
047: this
048: .getModel(node.getBnProject()
049: .getName()),
050: node.getBnProject().getVersion());
051: if (node.getBnProject().getType().equals(
052: Constants.Pj.COOPERATIVE))
053: edgesModel = BonitaServiceValue.getNode(
054: model.getName(), model.getVersion(), nodeName)
055: .getInEdges();
056: else
057: edgesModel = BonitaServiceValue.getNodeFromCache(model,
058: nodeName).getInEdges();
059: } catch (Exception e) {
060: edgesModel = new ArrayList();
061: }
062:
063: if ((edgesInstance.size() > 0) || (edgesModel.size() > 0)) {
064: edgesInstance = concatEdges(edgesInstance, edgesModel);
065: }
066:
067: Iterator i = edgesInstance.iterator();
068: int max = 0;
069: int result = Constants.Nd.EDGEINITIAL;
070: while (i.hasNext()) {
071: BonitaEdgeValue edge = (BonitaEdgeValue) i.next();
072:
073: if (edge.getState() == Constants.Ed.DEAD && max <= 0) {
074: max = 0;
075: result = Constants.Nd.CANCEL;
076: } else if (edge.getState() == Constants.Ed.INITIAL
077: && max <= 1) {
078: max = 1;
079: result = Constants.Nd.EDGEINITIAL;
080: } else if (edge.getState() == Constants.Ed.ANTICIPATING
081: && max <= 2) {
082: max = 2;
083: result = Constants.Nd.ANTACTIVE;
084: } else if (edge.getState() == Constants.Ed.ACTIVE
085: && max <= 3) {
086: max = 3;
087: result = Constants.Nd.ACTIVE;
088:
089: // As the highest possible value is reached
090: break;
091: }
092: }
093:
094: return result;
095: }
096:
097: private String getModel(String instanceName) {
098: int i = instanceName.indexOf("_instance");
099: return (instanceName.substring(0, i));
100: }
101:
102: private boolean isInstance(String name) {
103: return (name.matches(".*_instance.*"));
104: }
105:
106: /**
107: * Concatenates model edges that doesn't exist in the instances edges (without repetition)
108: *
109: * @param edgesInstance
110: * @param edgesModel
111: * @return
112: */
113: private Collection concatEdges(Collection edgesInstance,
114: Collection edgesModel) {
115: Collection result = new ArrayList();
116: boolean found = false;
117:
118: result = edgesInstance;
119:
120: Iterator it = edgesModel.iterator();
121: while (it.hasNext()) {
122: found = false;
123: BonitaEdgeValue el = (BonitaEdgeValue) it.next();
124: String nodeName = el.getInNode();
125: Iterator instanceEdgesIt = edgesInstance.iterator();
126: while (instanceEdgesIt.hasNext()) {
127: BonitaEdgeValue insEd = (BonitaEdgeValue) instanceEdgesIt
128: .next();
129: String node = insEd.getInNode();
130: if (this .isInstance(node)) {
131: node = this .getModel(node);
132: }
133: if (nodeName.equals(node)) {
134: found = true;
135: break;
136: }
137: }
138: if (!found) {
139: result.add(el);
140: }
141: }
142: return result;
143: }
144:
145: }
|