/** * Apply selected routing algorithm to the graph. * * @param nodes Nodes used to calculate path. * @param algorithm Algorithm used to compute the path, * RoutingGraph.Algorithm.ROUTING_ALG_DIJKSTRA or * RoutingGraph.Algorithm.ROUTING_ALG_BELLMANFORD * @return new path. */ public List<OsmEdge> applyAlgorithm(List<Node> nodes, Algorithm algorithm) { List<OsmEdge> path = new ArrayList<>(); Graph<Node, OsmEdge> g; double totalWeight = 0; RoutingLayer layer = (RoutingLayer) Main.map.mapView.getActiveLayer(); RoutingModel routingModel = layer.getRoutingModel(); if (graph == null || routingModel.getOnewayChanged()) this.createGraph(); logger.debug("apply algorithm between nodes "); for (Node node : nodes) { logger.debug(node.getId()); } logger.debug("-----------------------------------"); // Assign the graph to g g = graph; switch (algorithm) { case ROUTING_ALG_DIJKSTRA: logger.debug("Using Dijkstra algorithm"); DijkstraShortestPath<Node, OsmEdge> routingk = null; for (int index = 1; index < nodes.size(); ++index) { routingk = new DijkstraShortestPath<>(g, nodes.get(index - 1), nodes.get(index)); if (routingk.getPathEdgeList() == null) { logger.debug("no path found!"); break; } path.addAll(routingk.getPathEdgeList()); totalWeight += routingk.getPathLength(); } break; case ROUTING_ALG_BELLMANFORD: logger.debug("Using Bellman Ford algorithm"); for (int index = 1; index < nodes.size(); ++index) { path = BellmanFordShortestPath.findPathBetween( rgDelegator, nodes.get(index - 1), nodes.get(index)); if (path == null) { logger.debug("no path found!"); return null; } } break; default: logger.debug("Wrong algorithm"); break; } logger.debug("shortest path found: " + path + "\nweight: " + totalWeight); return path; }