001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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: package org.cougaar.servicediscovery.description;
027:
028: import org.cougaar.core.util.UniqueObject;
029: import org.cougaar.planning.ldm.plan.Role;
030: import org.cougaar.planning.ldm.plan.Schedule;
031: import org.cougaar.servicediscovery.Constants;
032:
033: import java.util.List;
034:
035: /**
036: * A Collection which maintains an ordered list of agent names - reflecting
037: * the superior/subordinate chain from a root agent.
038: * Iteration starts with the current agent and goes to the root.
039: */
040:
041: public abstract class Lineage implements java.io.Serializable,
042: UniqueObject {
043:
044: public static final int UNDEFINED = -1;
045: public static final int ADCON = 1;
046: public static final int OPCON = 2;
047: public static final int SUPPORT = 3;
048:
049: public static final String UNDEFINED_STRING = "Undefined";
050: public static final String ADCON_STRING = "ADCON";
051: public static final String OPCON_STRING = "OPCON";
052: public static final String SUPPORT_STRING = "SUPPORT";
053:
054: /* returns null if type is not recognized */
055: public static Role typeToRole(int type) {
056: switch (type) {
057: case ADCON:
058: return Constants.Roles.ADMINISTRATIVESUPERIOR;
059: case OPCON:
060: return Constants.Roles.OPERATIONALSUPERIOR;
061: case SUPPORT:
062: return Constants.Roles.SUPPORTSUPERIOR;
063: default:
064: return null;
065: }
066: }
067:
068: public static int roleToType(Role role) {
069: if ((role.equals(Constants.Roles.ADMINISTRATIVESUPERIOR))
070: || (role.equals(Constants.Roles.SUPERIOR))) {
071: return ADCON;
072: } else if (role.equals(Constants.Roles.OPERATIONALSUPERIOR)) {
073: return OPCON;
074: } else if (role.equals(Constants.Roles.SUPPORTSUPERIOR)) {
075: return SUPPORT;
076: } else {
077: return UNDEFINED;
078: }
079: }
080:
081: public static boolean validType(int lineageType) {
082: switch (lineageType) {
083: case (ADCON):
084: case (OPCON):
085: case (SUPPORT):
086: return true;
087: default:
088: return false;
089: }
090: }
091:
092: public static int countHops(List lineageList, String startingAgent,
093: String endingAgent) {
094: int listSize = lineageList.size();
095: String leaf = (String) ((listSize > 0) ? (lineageList
096: .get(listSize - 1)) : null);
097: String root = (String) ((listSize > 0) ? lineageList.get(0)
098: : null);
099:
100: if (startingAgent.equals(leaf) && endingAgent.equals(root)) {
101: return listSize - 1;
102: }
103:
104: int start = lineageList.indexOf(startingAgent);
105: int end = lineageList.indexOf(endingAgent);
106:
107: if ((start == -1) || (end == -1) || (end > start)) {
108: return -1;
109: } else {
110: return start - end;
111: }
112: }
113:
114: /**
115: * @return the type of the lineage.
116: * Should be one of the defined lineage types.
117: **/
118: abstract public int getType();
119:
120: /**
121: * @return the name of the agent at the end of the lineage.
122: */
123: abstract public String getLeaf();
124:
125: /**
126: * @return the name of the agent at the root of the lineage.
127: */
128: abstract public String getRoot();
129:
130: /**
131: * @return the number of links in the lineage between the two agents.
132: * Returns -1 if the agents are not linked.
133: */
134: abstract public int countHops(String startingAgent,
135: String endingAgent);
136:
137: /**
138: * @return an unmodifiable list of agents in the lineage
139: */
140: abstract public List getList();
141:
142: /**
143: * @return the time periods for which the lineage is valid
144: */
145: abstract public Schedule getSchedule();
146: }
|