Source Code Cross Referenced for PathsFinder.java in  » Code-Analyzer » classycle » classycle » graph » 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 » Code Analyzer » classycle » classycle.graph 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2003-2008, Franz-Josef Elmer, All rights reserved.
003:         *
004:         * Redistribution and use in source and binary forms, with or without 
005:         * modification, are permitted provided that the following conditions are met:
006:         * 
007:         * - Redistributions of source code must retain the above copyright notice, 
008:         *   this list of conditions and the following disclaimer.
009:         * - Redistributions in binary form must reproduce the above copyright notice, 
010:         *   this list of conditions and the following disclaimer in the documentation 
011:         *   and/or other materials provided with the distribution.
012:         *
013:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
014:         * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
015:         * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
016:         * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
017:         * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
018:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
019:         * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
020:         * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
021:         * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
022:         * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
023:         * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
024:         */
025:        package classycle.graph;
026:
027:        import java.util.HashSet;
028:
029:        /**
030:         * Class searching for all (or only the shortest) paths between classes
031:         * of a start set and classes of a final set.
032:         * 
033:         * @author  Franz-Josef Elmer
034:         */
035:        public class PathsFinder {
036:            private final VertexCondition _startSetCondition;
037:            private final VertexCondition _finalSetCondition;
038:            private final boolean _shortestPathsOnly;
039:            private final boolean _directPathsOnly;
040:
041:            /**
042:             * Creates an instance for the specified vertex conditions.
043:             * @param startSetCondition Condition defining the start set.
044:             * @param finalSetCondition Condition defining the final set.
045:             * @param shortestPathsOnly if <code>true</code> only the shortest
046:             *        paths are returned.
047:             */
048:            public PathsFinder(VertexCondition startSetCondition,
049:                    VertexCondition finalSetCondition, boolean shortestPathsOnly) {
050:                this (startSetCondition, finalSetCondition, shortestPathsOnly,
051:                        false);
052:            }
053:
054:            /**
055:             * Creates an instance for the specified vertex conditions.
056:             * @param startSetCondition Condition defining the start set.
057:             * @param finalSetCondition Condition defining the final set.
058:             * @param shortestPathsOnly if <code>true</code> only the shortest
059:             *        paths are returned.
060:             * @param directPathsOnly if <code>true</code> only paths of length 1
061:             *        are returned.
062:             */
063:            public PathsFinder(VertexCondition startSetCondition,
064:                    VertexCondition finalSetCondition,
065:                    boolean shortestPathsOnly, boolean directPathsOnly) {
066:                _startSetCondition = startSetCondition;
067:                _finalSetCondition = finalSetCondition;
068:                _shortestPathsOnly = shortestPathsOnly;
069:                _directPathsOnly = directPathsOnly;
070:            }
071:
072:            public VertexCondition getFinalSetCondition() {
073:                return _finalSetCondition;
074:            }
075:
076:            public boolean isShortestPathsOnly() {
077:                return _shortestPathsOnly;
078:            }
079:
080:            public VertexCondition getStartSetCondition() {
081:                return _startSetCondition;
082:            }
083:
084:            /**
085:             * Finds all paths from the specified start vertices to the vertices
086:             * fullfilling the specified condition.
087:             * @param graph Complete graph.
088:             * @return All vertices including start and end vertices defining the
089:             *         subgraph with all paths.
090:             */
091:            public AtomicVertex[] findPaths(AtomicVertex[] graph) {
092:                prepareGraph(graph);
093:                HashSet pathVertices = new HashSet();
094:                HashSet currentPath = new HashSet();
095:                for (int i = 0; i < graph.length; i++) {
096:                    AtomicVertex vertex = graph[i];
097:                    if (_startSetCondition.isFulfilled(vertex)) {
098:                        if (_directPathsOnly) {
099:                            findDirectPaths(vertex, pathVertices);
100:                        } else {
101:                            prepareIfFinal(vertex);
102:                            int pathLength = calculateShortestPath(vertex,
103:                                    currentPath);
104:                            if (pathLength < Integer.MAX_VALUE) {
105:                                vertex.setOrder(pathLength);
106:                                followPaths(vertex, pathVertices);
107:                            }
108:                        }
109:                    }
110:                }
111:                return (AtomicVertex[]) pathVertices
112:                        .toArray(new AtomicVertex[pathVertices.size()]);
113:            }
114:
115:            private void findDirectPaths(AtomicVertex vertex,
116:                    HashSet pathVertices) {
117:                if (_finalSetCondition.isFulfilled(vertex)) {
118:                    pathVertices.add(vertex);
119:                } else {
120:                    for (int i = 0, n = vertex.getNumberOfOutgoingArcs(); i < n; i++) {
121:                        Vertex headVertex = vertex.getHeadVertex(i);
122:                        if (_finalSetCondition.isFulfilled(headVertex)) {
123:                            pathVertices.add(vertex);
124:                            pathVertices.add(headVertex);
125:                        }
126:                    }
127:                }
128:            }
129:
130:            private void prepareGraph(AtomicVertex[] graph) {
131:                for (int i = 0; i < graph.length; i++) {
132:                    AtomicVertex vertex = graph[i];
133:                    prepareVertex(vertex);
134:                    for (int j = 0, n = vertex.getNumberOfOutgoingArcs(); j < n; j++) {
135:                        prepareVertex((AtomicVertex) vertex.getHeadVertex(j));
136:                    }
137:                }
138:            }
139:
140:            private void prepareVertex(AtomicVertex vertex) {
141:                vertex.reset();
142:                vertex.setOrder(Integer.MAX_VALUE);
143:                if (_startSetCondition.isFulfilled(vertex)) {
144:                    vertex.visit();
145:                }
146:            }
147:
148:            private int calculateShortestPath(AtomicVertex vertex,
149:                    HashSet currentPath) {
150:                currentPath.add(vertex);
151:                int shortestPath = Integer.MAX_VALUE;
152:                for (int i = 0, n = vertex.getNumberOfOutgoingArcs(); i < n; i++) {
153:                    AtomicVertex nextVertex = (AtomicVertex) vertex
154:                            .getHeadVertex(i);
155:                    prepareIfFinal(nextVertex);
156:                    int pathLength = _startSetCondition.isFulfilled(nextVertex) ? Integer.MAX_VALUE
157:                            : nextVertex.getOrder();
158:                    if (!currentPath.contains(nextVertex)
159:                            && !nextVertex.isVisited()) {
160:                        pathLength = calculateShortestPath(nextVertex,
161:                                currentPath);
162:                        nextVertex.setOrder(pathLength);
163:                        nextVertex.visit();
164:                    }
165:                    shortestPath = Math.min(shortestPath, pathLength);
166:                }
167:                currentPath.remove(vertex);
168:                if (shortestPath < Integer.MAX_VALUE) {
169:                    shortestPath++;
170:                }
171:                return shortestPath;
172:            }
173:
174:            private void prepareIfFinal(AtomicVertex vertex) {
175:                if (_finalSetCondition.isFulfilled(vertex)) {
176:                    vertex.visit();
177:                    vertex.setOrder(0);
178:                }
179:            }
180:
181:            private void followPaths(AtomicVertex vertex, HashSet pathVertices) {
182:                pathVertices.add(vertex);
183:                int shortestPathLength = vertex.getOrder() - 1;
184:                for (int i = 0, n = vertex.getNumberOfOutgoingArcs(); i < n; i++) {
185:                    AtomicVertex nextVertex = (AtomicVertex) vertex
186:                            .getHeadVertex(i);
187:                    int pathLength = nextVertex.getOrder();
188:                    if (pathLength < Integer.MAX_VALUE
189:                            && !pathVertices.contains(nextVertex)) {
190:                        if (!_shortestPathsOnly
191:                                || pathLength == shortestPathLength) {
192:                            pathVertices.add(nextVertex);
193:                            if (pathLength > 0) {
194:                                followPaths(nextVertex, pathVertices);
195:                            }
196:                        }
197:                    }
198:                }
199:            }
200:
201:            //  private int search(AtomicVertex vertex, HashSet currentPath, 
202:            //                     HashSet pathVertices)
203:            //  {
204:            //    currentPath.add(vertex);
205:            //    boolean found = false;
206:            //    int shortestPath = Integer.MAX_VALUE;
207:            //    for (int i = 0, n = vertex.getNumberOfOutgoingArcs(); i < n; i++)
208:            //    {
209:            //      AtomicVertex nextVertex = (AtomicVertex) vertex.getHeadVertex(i);
210:            //      int pathLength = nextVertex.getOrder();
211:            //      if (!nextVertex.isVisited() && !currentPath.contains(nextVertex))
212:            //      {
213:            //        pathLength = search(nextVertex, currentPath, pathVertices);
214:            //        nextVertex.setOrder(pathLength);
215:            //        nextVertex.visit();
216:            //      }
217:            //      shortestPath = Math.min(shortestPath, pathLength);
218:            //    }
219:            //    for (int i = 0, n = vertex.getNumberOfOutgoingArcs(); i < n; i++)
220:            //    {
221:            //      AtomicVertex nextVertex = (AtomicVertex) vertex.getHeadVertex(i);
222:            //      int pathLength = nextVertex.getOrder();
223:            //      if (pathLength < Integer.MAX_VALUE)
224:            //      {
225:            //        if (!_shortestPathsOnly || pathLength == shortestPath)
226:            //        {
227:            //          pathVertices.add(nextVertex);
228:            //        }
229:            //      }
230:            //    }
231:            //    currentPath.remove(vertex);
232:            //    if (shortestPath < Integer.MAX_VALUE)
233:            //    {
234:            //      shortestPath++;
235:            //    }
236:            //    return shortestPath;
237:            //  }
238:
239:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.