001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.examples.mobility.test;
028:
029: import java.io.Serializable;
030: import java.util.Collection;
031: import java.util.Iterator;
032: import org.cougaar.core.mobility.ldm.MoveAgent;
033: import org.cougaar.core.mobility.Ticket;
034: import org.cougaar.util.UnaryPredicate;
035:
036: /**
037: * Ugly testing code to force rapid agent mobility.
038: */
039: public class QuickMoverPlugin extends AbstractMoverPlugin {
040:
041: private static final String DEFAULT_ID = "id";
042:
043: // whole bunch o' moves
044: // fixme to be read from a file, etc...
045: private static final String[][] MOVES = {
046: { "AgentA", null, "NodeA", "true" },
047: { "AgentA", null, "NodeB", "false" },
048: { "AgentB", null, "NodeA", "true" },
049: { "AgentA", null, "NodeB", "false" },
050: { "AgentA", null, "NodeA", "false" },
051: { "AgentA", null, "NodeB", "true" },
052: { "AgentB", null, "NodeB", "false" },
053: { "AgentA", null, "NodeA", "false" },
054: { "AgentB", null, "NodeB", "false" },
055: { "AgentA", null, "NodeA", "true" },
056: { "AgentA", null, "NodeB", "true" },
057: { "AgentA", null, "NodeB", "true" },
058: { "AgentB", null, "NodeA", "true" },
059: { "AgentB", null, "NodeB", "true" },
060: { "AgentB", null, "NodeA", "true" },
061: { "AgentA", null, "NodeB", "true" }, };
062:
063: private String id;
064: private MyState state;
065:
066: // remember where we are in the list of MOVES
067: private static class MyState implements Serializable {
068: String id;
069: int i;
070: }
071:
072: protected void setupSubscriptions() {
073: super .setupSubscriptions();
074:
075: id = DEFAULT_ID;
076: Collection c = getParameters();
077: if (c.size() > 0) {
078: id = (String) c.iterator().next();
079: }
080:
081: state = findState();
082:
083: if (blackboard.didRehydrate()) {
084: if (state == null) {
085: log.error("Unable to find state with id \"" + id
086: + "\"!");
087: }
088: } else {
089: if (state != null) {
090: log.error("Found prior state for id \"" + id
091: + "\", multiple conflicting plugins?");
092: }
093: state = new MyState();
094: state.id = id;
095: blackboard.publishAdd(state);
096: nextMove(null);
097: }
098: }
099:
100: private MyState findState() {
101: UnaryPredicate pred = new UnaryPredicate() {
102: public boolean execute(Object o) {
103: return ((o instanceof MyState) && (id
104: .equals(((MyState) o).id)));
105: }
106: };
107: Collection real = blackboard.query(pred);
108: int n = real.size();
109: if (n > 0) {
110: for (Iterator iter = real.iterator(); iter.hasNext();) {
111: Object o = iter.next();
112: if (o instanceof MyState) {
113: return ((MyState) o);
114: }
115: }
116: }
117: return null;
118: }
119:
120: private void nextMove(MoveAgent ma) {
121: if (state == null) {
122: log.error("No state");
123: return;
124: }
125:
126: int j = state.i;
127: if (j < 0) {
128: log.error("Mover \"" + id + "\" move " + (-j)
129: + " with prior error");
130: return;
131: } else if (j >= MOVES.length) {
132: log.info("Mover \"" + id + "\" finished all " + j
133: + " moves");
134: return;
135: }
136:
137: if (ma != null) {
138: blackboard.publishRemove(ma);
139: }
140:
141: ++state.i;
142: blackboard.publishChange(state);
143:
144: String[] sa = MOVES[j];
145:
146: Ticket t = createTicket(sa[0], sa[1], sa[2], "true"
147: .equals(sa[3]));
148: requestMove(t);
149:
150: log.info("Mover \"" + id + "\" submitting new move "
151: + t.getIdentifier() + " with ticket: " + t);
152: }
153:
154: protected void handleCompletedMove(MoveAgent ma) {
155: MoveAgent.Status mstat = ma.getStatus();
156: if ((mstat == null)
157: || (mstat.getCode() != MoveAgent.Status.OKAY)) {
158: if (state.i > 0) {
159: state.i = -(state.i);
160: blackboard.publishChange(state);
161: }
162: log.error("Mover \"" + id + "\" failed move " + ma.getUID()
163: + " with status " + mstat + " and exception", mstat
164: .getThrowable());
165: return;
166: }
167: log.info("Mover \"" + id + "\" successful move " + ma.getUID()
168: + " with status " + mstat);
169:
170: nextMove(ma);
171: }
172:
173: }
|