Source Code Cross Referenced for TransportationRoute.java in  » Science » Cougaar12_4 » org » cougaar » glm » ldm » asset » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Cougaar12_4 » org.cougaar.glm.ldm.asset 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.