001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.engine.node;
018:
019: import java.util.HashSet;
020: import java.util.Iterator;
021: import java.util.Set;
022: import java.util.StringTokenizer;
023:
024: import edu.iu.uis.eden.KEWServiceLocator;
025: import edu.iu.uis.eden.engine.RouteContext;
026: import edu.iu.uis.eden.util.Utilities;
027:
028: /**
029: * A basic implementation of the JoinEngine which handles join setup and makes determinations
030: * as to when a join condition has been satisfied.
031: *
032: * @author ewestfal
033: */
034: public class BasicJoinEngine implements JoinEngine {
035:
036: public static final String EXPECTED_JOINERS = "ExpectedJoiners";
037: public static final String ACTUAL_JOINERS = "ActualJoiners";
038:
039: public void createExpectedJoinState(RouteContext context,
040: RouteNodeInstance joinInstance,
041: RouteNodeInstance previousNodeInstance) {
042: RouteNodeInstance splitNode = previousNodeInstance.getBranch()
043: .getSplitNode();
044: for (Iterator iter = splitNode.getNextNodeInstances()
045: .iterator(); iter.hasNext();) {
046: RouteNodeInstance splitNodeNextNode = (RouteNodeInstance) iter
047: .next();
048: splitNodeNextNode.getBranch().setJoinNode(joinInstance);
049: saveBranch(context, splitNodeNextNode.getBranch());
050: addExpectedJoiner(joinInstance, splitNodeNextNode
051: .getBranch());
052: }
053: joinInstance.setBranch(splitNode.getBranch());
054: joinInstance.setProcess(splitNode.getProcess());
055: }
056:
057: public void addExpectedJoiner(RouteNodeInstance nodeInstance,
058: Branch branch) {
059: addJoinState(nodeInstance, branch, EXPECTED_JOINERS);
060: }
061:
062: public void addActualJoiner(RouteNodeInstance nodeInstance,
063: Branch branch) {
064: addJoinState(nodeInstance, branch, ACTUAL_JOINERS);
065: }
066:
067: private void addJoinState(RouteNodeInstance nodeInstance,
068: Branch branch, String key) {
069: NodeState state = nodeInstance.getNodeState(key);
070: if (state == null) {
071: state = new NodeState();
072: state.setKey(key);
073: state.setValue("");
074: state.setNodeInstance(nodeInstance);
075: nodeInstance.addNodeState(state);
076: }
077: state.setValue(state.getValue() + branch.getBranchId() + ",");
078: }
079:
080: public boolean isJoined(RouteNodeInstance nodeInstance) {
081: NodeState expectedState = nodeInstance
082: .getNodeState(EXPECTED_JOINERS);
083: if (expectedState == null
084: || Utilities.isEmpty(expectedState.getValue())) {
085: return true;
086: }
087: NodeState actualState = nodeInstance
088: .getNodeState(ACTUAL_JOINERS);
089: Set expectedSet = loadIntoSet(expectedState);
090: Set actualSet = loadIntoSet(actualState);
091: for (Iterator iterator = expectedSet.iterator(); iterator
092: .hasNext();) {
093: String value = (String) iterator.next();
094: if (actualSet.contains(value)) {
095: iterator.remove();
096: }
097: }
098: return expectedSet.size() == 0;
099: }
100:
101: private Set loadIntoSet(NodeState state) {
102: Set set = new HashSet();
103: StringTokenizer tokenizer = new StringTokenizer(state
104: .getValue(), ",");
105: while (tokenizer.hasMoreTokens()) {
106: set.add(tokenizer.nextToken());
107: }
108: return set;
109: }
110:
111: private void saveBranch(RouteContext context, Branch branch) {
112: if (!context.isSimulation()) {
113: KEWServiceLocator.getRouteNodeService().save(branch);
114: }
115: }
116:
117: }
|