001 package nl.tudelft.tbm.eeni.owl2java.utils;
002
003 import org.apache.commons.logging.Log;
004 import org.apache.commons.logging.LogFactory;
005 import org.jgrapht.DirectedGraph;
006 import org.jgrapht.GraphPath;
007 import org.jgrapht.alg.DijkstraShortestPath;
008 import org.jgrapht.alg.KShortestPaths;
009
010 import java.util.List;
011
012 public class GraphPathUtils<V, E> {
013
014 @SuppressWarnings("unused")
015 private static Log log = LogFactory.getLog(GraphPathUtils.class);
016 private DirectedGraph<V, E> graph;
017
018 public GraphPathUtils(DirectedGraph<V, E> graph) {
019 this.graph = graph;
020 }
021
022
023 public GraphPath<V, E> findShortestPath(V from, V to) {
024 KShortestPaths<V, E> kShortestPath = new KShortestPaths<V, E>(graph, from, 1);
025
026 List<GraphPath<V, E>> paths = kShortestPath.getPaths(to);
027 if (paths == null)
028 return null;
029 if (paths.isEmpty())
030 return null;
031 return paths.get(0);
032 }
033
034 public List<E> findShortestPathDijkstra(V from, V to) {
035 return DijkstraShortestPath.findPathBetween(graph, from, to);
036 }
037
038 public V getNextVertex(GraphPath<V, E> graphPath, V vertex) {
039 List<E> edges = graphPath.getEdgeList();
040 for (E edge : edges) {
041 V source = graph.getEdgeSource(edge);
042 V target = graph.getEdgeTarget(edge);
043
044 if (source.equals(vertex))
045 return target;
046 }
047 return null;
048 }
049
050 public boolean hasVertex(GraphPath<V, E> graphPath, V vertex) {
051 List<E> edges = graphPath.getEdgeList();
052 for (E edge : edges) {
053 V source = graph.getEdgeSource(edge);
054 V target = graph.getEdgeTarget(edge);
055
056 if (source.equals(vertex))
057 return true;
058 if (target.equals(vertex))
059 return true;
060 }
061 return false;
062 }
063
064
065 }