/** * Reconstruct the path from start node to end node * * @param result * @param endNode * @param traceMap * @return the path or {@code null} if error occurred */ public static PathInfo reconstructPath( NodeInfo endNode, Map<Integer, Integer> traceMap, LogicNetwork network) { PathInfo result = new PathInfo(); int current = endNode.getNodeId(); ArrayList<LinkInfo> tmpLinks = new ArrayList<>(); // the links in the reverse order while (traceMap.containsKey(current)) { int previous = traceMap.get(current); LinkInfo linkInfo = getLink(previous, current, network); tmpLinks.add(linkInfo); current = previous; } // put the links in the correct order for (int i = tmpLinks.size() - 1; 0 <= i; i--) { result.getLinks().add(tmpLinks.get(i)); } // calculate values properties of the path double pathCost = 0.0; double attractiveness = 0.0; for (LinkInfo linkInfo : result.getLinks()) { pathCost += linkInfo.getCost(); attractiveness += linkInfo.getAttractiveness(); result.getNodes().add(network.getNodes().get(linkInfo.getStartNode())); } result.setPathCost(pathCost); result.setAttractiveness(attractiveness); result.setNumLinks(result.getLinks().size()); result.setSimple(true); result.getNodes().add(endNode); return result; }