001: /*
002: * <copyright>
003: *
004: * Copyright 2003-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.logistics.plugin.trans.tools;
027:
028: import org.cougaar.glm.ldm.asset.GLMAsset;
029: import org.cougaar.glm.ldm.asset.NewSeaLinkPG;
030: import org.cougaar.glm.ldm.asset.SeaLinkPGImpl;
031: import org.cougaar.glm.ldm.asset.TransportationLink;
032: import org.cougaar.glm.ldm.asset.TransportationNode;
033: import org.cougaar.glm.ldm.asset.TransportationRoute;
034: import org.cougaar.glm.ldm.plan.GeolocLocation;
035: import org.cougaar.glm.ldm.plan.NewGeolocLocation;
036: import org.cougaar.glm.ldm.plan.NewNamedPosition;
037: import org.cougaar.glm.ldm.plan.GeolocLocationImpl;
038: import org.cougaar.glm.util.GLMMeasure;
039: import org.cougaar.planning.ldm.measure.Distance;
040: import org.cougaar.planning.ldm.measure.Latitude;
041: import org.cougaar.planning.ldm.measure.Longitude;
042: import org.cougaar.planning.ldm.asset.AbstractAsset;
043: import org.cougaar.planning.ldm.asset.Asset;
044:
045: import org.cougaar.logistics.plugin.seanet.Location;
046: import org.cougaar.logistics.plugin.seanet.LocationImpl;
047: import org.cougaar.logistics.plugin.seanet.Network;
048: import org.cougaar.logistics.plugin.seanet.Node;
049:
050: import java.util.*;
051:
052: import org.cougaar.planning.ldm.PlanningFactory;
053: import org.cougaar.util.log.Logger;
054:
055: public class RouteFinder {
056: Network network = new Network();
057: Map geolocToGeolocToRoute = new HashMap();
058: int numRoutes = 0;
059: int numNodes = 0;
060: int numLinks = 0;
061: PlanningFactory factory;
062: Logger logger;
063:
064: public RouteFinder(Logger logger) {
065: measureHelper = new GLMMeasure(logger);
066: this .logger = logger;
067: }
068:
069: public void setFactory(PlanningFactory factory) {
070: this .factory = factory;
071: }
072:
073: /** caches routes after getting them from the handy seanet facility! */
074: public Distance getDistance(GeolocLocation from, GeolocLocation to) {
075: return getRoute(from, to).getLength();
076: }
077:
078: public TransportationRoute getRoute(GeolocLocation from,
079: GeolocLocation to) {
080: Map toGeolocRouteMap = (Map) geolocToGeolocToRoute.get(from
081: .getGeolocCode());
082: TransportationRoute route = null;
083: if (toGeolocRouteMap != null) {
084: route = (TransportationRoute) toGeolocRouteMap.get(to
085: .getGeolocCode());
086: } else {
087: geolocToGeolocToRoute.put(from,
088: (toGeolocRouteMap = new HashMap()));
089: }
090:
091: if (route != null)
092: return route;
093:
094: Location fromLoc = getLocation(from);
095: Location toLoc = getLocation(to);
096: network.setFrom(fromLoc.getLatitude(), fromLoc.getLongitude());
097: network.setTo(toLoc.getLatitude(), toLoc.getLongitude());
098:
099: route = makeRoute();
100:
101: Vector nodes = new Vector(33);
102:
103: Iterator routeIter;
104:
105: routeIter = network.routeNoSourceOrDestination();
106:
107: // create list of nodes
108: /*
109: System.out.print ("Full Route is ");
110: for (Iterator iter = network.route ();iter.hasNext ();) {
111: Location node = (Location) iter.next ();
112: System.out.print ("(" + node.getLatitude () + "-" + node.getLongitude() + ")\t");
113: }
114: System.out.println ("");
115: */
116:
117: /*
118: System.out.print ("Partial Route is ");
119: for (Iterator iter = network.routeNoSourceOrDestination (); iter.hasNext ();) {
120: Location node = (Location) iter.next ();
121: String name = (node instanceof Node) ? ((Node)node).getName () : "anon";
122:
123: System.out.println ("(" + name + " " + node.getLatitude () + "-" + node.getLongitude() + ")\t");
124: }
125: */
126:
127: for (; routeIter.hasNext();) {
128: Object nextObj = routeIter.next();
129: nodes.add(0, getNode((Location) nextObj));
130: }
131:
132: route = makeRouteFromNodes(nodes);
133:
134: if (nodes.size() != route.getNodes().size())
135: logger.error("ERROR - nodes in " + nodes.size()
136: + " != nodes out " + route.getNodes().size());
137:
138: // for (int i = 0; i < route.getNodes().size (); i++)
139: // System.out.println ("Out : " + ((TransportationNode) route.getNodes().get(i)).getGeolocLocation());
140:
141: return route;
142: }
143:
144: /** this is inefficient, but not awful... */
145: public TransportationRoute makeRouteWithPOEandPOD(
146: TransportationRoute route, GeolocLocation POE,
147: GeolocLocation POD) {
148: Vector nodes = route.getNodes();
149:
150: nodes.add(0, getNode(getLocation(POE)));
151: nodes.add(getNode(getLocation(POD)));
152:
153: TransportationRoute routeOut = makeRouteFromNodes(POE, POD,
154: nodes);
155:
156: if (nodes.size() != routeOut.getNodes().size())
157: logger.error("ERROR 2 - nodes in " + nodes.size()
158: + " != nodes out " + routeOut.getNodes().size());
159:
160: // for (int i = 0; i < routeOut.getNodes().size (); i++)
161: // System.out.println ("Out 2 : " + ((TransportationNode) routeOut.getNodes().get(i)).getGeolocLocation());
162:
163: return routeOut;
164: }
165:
166: /** this is inefficient, but not awful... */
167: protected TransportationRoute makeRouteFromNodes(
168: GeolocLocation from, GeolocLocation to, Collection nodes) {
169: TransportationNode source = null, destination = null;
170: TransportationNode last = null;
171: TransportationNode node = null;
172: Vector links = new Vector(); // has to be a vector because setLinks expects one...
173:
174: TransportationRoute route = makeRoute();
175: // now link them
176: for (Iterator iter = nodes.iterator(); iter.hasNext();) {
177: node = (TransportationNode) iter.next();
178: route.addNode(node);
179:
180: if (last != null)
181: links.add(linkNodes(last, node));
182:
183: if (source == null) {
184: source = node;
185: ((NewGeolocLocation) source.getGeolocLocation())
186: .setGeolocCode(from.getGeolocCode());
187: ((NewNamedPosition) source.getGeolocLocation())
188: .setName(from.getName());
189: }
190:
191: last = node;
192: }
193:
194: destination = node;
195: ((NewGeolocLocation) destination.getGeolocLocation())
196: .setGeolocCode(to.getGeolocCode());
197: ((NewNamedPosition) destination.getGeolocLocation()).setName(to
198: .getName());
199:
200: route.setLinks(links);
201: route.setSource(source);
202: route.setDestination(destination);
203:
204: return route;
205: }
206:
207: protected TransportationRoute makeRouteFromNodes(Collection nodes) {
208: TransportationNode source = null, destination = null;
209: TransportationNode last = null;
210: TransportationNode node = null;
211: Vector links = new Vector(); // has to be a vector because setLinks expects one...
212:
213: TransportationRoute route = makeRoute();
214: // now link them
215: for (Iterator iter = nodes.iterator(); iter.hasNext();) {
216: node = (TransportationNode) iter.next();
217: route.addNode(node);
218:
219: if (last != null)
220: links.add(linkNodes(last, node));
221:
222: if (source == null) {
223: source = node;
224: }
225:
226: last = node;
227: }
228:
229: destination = node;
230:
231: route.setLinks(links);
232: route.setSource(source);
233: route.setDestination(destination);
234:
235: return route;
236: }
237:
238: protected Location getLocation(GeolocLocation geoloc) {
239: return new LocationImpl(geoloc.getLatitude().getDegrees(),
240: geoloc.getLongitude().getDegrees());
241: }
242:
243: public int i = 0;
244:
245: protected TransportationNode getNode(Location loc) {
246: TransportationNode node = makeNode();
247:
248: String name = (loc instanceof Node) ? ((Node) loc).getName()
249: : "node-" + i++;
250: node.setGeolocLocation(new GeolocLocationImpl(Latitude
251: .newLatitude(loc.getLatitude()), Longitude
252: .newLongitude(loc.getLongitude()), name));
253: return node;
254: }
255:
256: protected TransportationLink linkNodes(TransportationNode first,
257: TransportationNode second) {
258: TransportationLink link = makeLink();
259: link.setOrigin(first);
260: link.setDestination(second);
261: first.addLink(link);
262: NewSeaLinkPG seaLinkPG = new SeaLinkPGImpl();
263: seaLinkPG.setLinkLength(measureHelper.distanceBetween(first
264: .getGeolocLocation(), second.getGeolocLocation()));
265: link.setSeaLinkPG(seaLinkPG);
266: return link;
267: }
268:
269: protected TransportationRoute makeRoute() {
270: return (TransportationRoute) factory.createInstance(
271: "TRANSPORT_ROUTE", "Route-" + numRoutes++);
272: }
273:
274: protected TransportationNode makeNode() {
275: TransportationNode node = (TransportationNode) factory
276: .createInstance("TRANSPORT_NODE", "Node-" + numNodes++);
277:
278: return node;
279: }
280:
281: protected TransportationLink makeLink() {
282: TransportationLink link = (TransportationLink) factory
283: .createInstance("TRANSPORT_SEALINK", "Link-"
284: + numLinks++);
285:
286: return link;
287: }
288:
289: protected GLMMeasure measureHelper;
290: }
|