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: /* hand generated! */
028:
029: package org.cougaar.glm.ldm.asset;
030:
031: import java.beans.IntrospectionException;
032: import java.beans.PropertyDescriptor;
033: import java.io.IOException;
034: import java.io.ObjectInputStream;
035: import java.io.ObjectOutputStream;
036: import java.util.Enumeration;
037: import java.util.Vector;
038:
039: import org.cougaar.planning.ldm.asset.Asset;
040: import org.cougaar.planning.ldm.measure.Distance;
041:
042: // Lots of stuff from TransportationGraph won't work with this because
043: // of the way links are handled. Links were removed and then readded.
044:
045: public class TransportationRoute extends TransportationGraph {
046: // Instance Variables
047: private TransportationNode mySource;
048: private TransportationNode myDestination;
049: private Vector myLinks;
050:
051: // Constructors
052: public TransportationRoute() {
053: mySource = null;
054: myDestination = null;
055: myLinks = new Vector();
056: }
057:
058: public TransportationRoute(TransportationRoute prototype) {
059: super (prototype);
060: mySource = null;
061: myDestination = null;
062: myLinks = new Vector();
063: }
064:
065: public TransportationRoute(TransportationNode source,
066: TransportationNode destination, Vector nodes) {
067: myLinks = new Vector();
068: addGraphByNodes(nodes);
069: mySource = source;
070: myDestination = destination;
071: }
072:
073: /** For infrastructure only - use LdmFactory.copyInstance instead. **/
074: public Object clone() throws CloneNotSupportedException {
075: TransportationRoute _thing = (TransportationRoute) super
076: .clone();
077: if (mySource != null)
078: _thing.setSource(mySource);
079: if (myDestination != null)
080: _thing.setDestination(myDestination);
081: if (myLinks != null)
082: _thing.setLinks(myLinks);
083: return _thing;
084: }
085:
086: /** create an instance of the right class for copy operations **/
087: public Asset instanceForCopy() {
088: return new TransportationRoute();
089: }
090:
091: /** create an instance of this prototype **/
092: public Asset createInstance() {
093: return new TransportationRoute(this );
094: }
095:
096: // Get Accessors
097: public TransportationNode getSource() {
098: return mySource;
099: }
100:
101: public TransportationNode getDestination() {
102: return myDestination;
103: }
104:
105: public Vector getLinks() {
106: return myLinks;
107: }
108:
109: public void setLinks(Vector arg_Links) {
110: myLinks = new Vector(arg_Links);
111: }
112:
113: public Distance getLength() {
114: double total_length = 0.0;
115: for (int i = 0; i < myLinks.size(); i++) {
116: TransportationLink current_link = (TransportationLink) myLinks
117: .elementAt(i);
118: if (current_link instanceof TransportationRoadLink) {
119: total_length += ((TransportationRoadLink) current_link)
120: .getRoadLinkPG().getLinkLength().getMiles();
121: } else if (current_link instanceof TransportationRailLink) {
122: total_length += ((TransportationRailLink) current_link)
123: .getRailLinkPG().getLinkLength().getMiles();
124: } else if (current_link instanceof TransportationSeaLink) {
125: total_length += ((TransportationSeaLink) current_link)
126: .getSeaLinkPG().getLinkLength().getMiles();
127: } else if (current_link instanceof TransportationAirLink) {
128: total_length += ((TransportationAirLink) current_link)
129: .getAirLinkPG().getLinkLength().getMiles();
130: }
131: }
132: return Distance.newMiles(total_length);
133: }
134:
135: // Set Accessors
136:
137: /**
138: * Sets the route to the nodes in the vector <code>newnodes</code>.
139: * Also sets the source and destination to be the first and last
140: * nodes in the vector, respectively.
141: * @param newnodes Vector of nodes that mark the route
142: */
143: public void addGraphByNodes(Vector newnodes) {
144: // precondition
145: TransportationNode lastNode = null;
146: TransportationNode this Node = null;
147: if (newnodes.size() < 2)
148: throw new RuntimeException(
149: "Route expects at least 2 nodes. " + "Only got "
150: + newnodes.size() + ".");
151:
152: for (int i = 0; i < newnodes.size(); i++) {
153: try {
154: this Node = (TransportationNode) newnodes.elementAt(i);
155: addNode(this Node);
156: if (lastNode != null) {
157: Vector listLinks = this Node.getLinks();
158: for (int j = 0; j < listLinks.size(); j++) {
159: if (((TransportationLink) listLinks
160: .elementAt(j)).otherEnd(this Node) == lastNode) {
161: myLinks.addElement(listLinks.elementAt(j));
162: }
163: }
164: }
165: lastNode = this Node;
166: } catch (Exception e) {
167: e.printStackTrace();
168: System.err.println("!!!error: got invalid node "
169: + "while setting nodes in TransportationGraph");
170: }
171: }
172: setSource((TransportationNode) newnodes.firstElement());
173: setDestination((TransportationNode) newnodes.lastElement());
174: }
175:
176: /**
177: * DANGER- this allows the use of the underlying Graph methods;
178: * then call recomputeRoute() to compute the route from
179: * the available graph.
180: * We avoid cycles and other nastiness by assuming that the route
181: * is to be followed in the order that the NODES are stored in the VECTOR.
182: */
183: public void recomputeRoute() {
184: Vector currentNodes = this .getNodes();
185: if (currentNodes.size() < 2)
186: return;
187:
188: // Copy the nodes safely so we don't lose them when we removeNode
189: // (I think this is a problem; i.e. that currentNodes is actually the
190: // same reference as the vector that removeNode is removing from)
191: // and don't use an Enumeration; we want to preserve ordering
192: Vector copiedNodes = new Vector(currentNodes.size());
193: for (int i = 0; i < currentNodes.size(); i++)
194: copiedNodes.addElement(currentNodes.elementAt(i));
195:
196: Enumeration n_enum = currentNodes.elements();
197: while (n_enum.hasMoreElements()) {
198: TransportationNode next_node = (TransportationNode) n_enum
199: .nextElement();
200: this .removeNode(next_node);
201: }
202:
203: myLinks = new Vector();
204: addGraphByNodes(copiedNodes);
205: }
206:
207: public void setSource(TransportationNode source) {
208: mySource = source;
209: }
210:
211: public void setDestination(TransportationNode destination) {
212: myDestination = destination;
213: }
214:
215: // Methods
216:
217: public boolean CheckRoute() {
218: if (mySource == myDestination)
219: return false;
220: TransportationNode current_node = mySource;
221: TransportationLink current_link;
222: Vector current_node_links;
223: Vector myNodes = getNodes();
224: int link_count;
225: while (current_node != myDestination) {
226: current_link = null;
227: current_node_links = current_node.getLinks();
228: link_count = 0;
229: for (int i = 0; i < current_node_links.size(); i++) {
230: if (myNodes
231: .contains(((TransportationLink) current_node_links
232: .elementAt(i)).otherEnd(current_node))
233: && myLinks
234: .contains((TransportationLink) current_node_links
235: .elementAt(i))) {
236: current_link = (TransportationLink) current_node_links
237: .elementAt(i);
238: link_count++;
239: }
240: }
241: if (current_link == null || link_count != 1)
242: return false;
243: current_node = current_link.otherEnd(current_node);
244: }
245: return true;
246: }
247:
248: public boolean useLink(TransportationLink link) {
249: return myLinks.contains(link);
250: }
251:
252: // Read/Write
253: private void writeObject(ObjectOutputStream out) throws IOException {
254: out.defaultWriteObject();
255: out.writeObject(mySource);
256: out.writeObject(myDestination);
257: out.writeObject(myLinks);
258: }
259:
260: private void readObject(ObjectInputStream in)
261: throws ClassNotFoundException, IOException {
262: in.defaultReadObject();
263: mySource = (TransportationNode) in.readObject();
264: myDestination = (TransportationNode) in.readObject();
265: myLinks = (Vector) in.readObject();
266: }
267:
268: // beaninfo support
269: private static PropertyDescriptor properties[];
270: static {
271: try {
272: properties = new PropertyDescriptor[3];
273: properties[0] = new PropertyDescriptor("Source",
274: TransportationRoute.class, "getSource", null);
275: properties[1] = new PropertyDescriptor("Destination",
276: TransportationRoute.class, "getDestination", null);
277: properties[2] = new PropertyDescriptor("Links",
278: TransportationRoute.class, "getLinks", null);
279: } catch (IntrospectionException ie) {
280: }
281: }
282:
283: public PropertyDescriptor[] getPropertyDescriptors() {
284: PropertyDescriptor[] pds = super .getPropertyDescriptors();
285: PropertyDescriptor[] ps = new PropertyDescriptor[pds.length + 3];
286: System.arraycopy(pds, 0, ps, 0, pds.length);
287: System.arraycopy(properties, 0, ps, pds.length, 3);
288: return ps;
289: }
290: }
|