AbstractOneToManyRoutingAlgorithm.java

  1. /*  This file is part of Openrouteservice.
  2.  *
  3.  *  Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the
  4.  *  GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1
  5.  *  of the License, or (at your option) any later version.

  6.  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  7.  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8.  *  See the GNU Lesser General Public License for more details.

  9.  *  You should have received a copy of the GNU Lesser General Public License along with this library;
  10.  *  if not, see <https://www.gnu.org/licenses/>.
  11.  */
  12. package org.heigit.ors.routing.algorithms;

  13. import com.graphhopper.routing.SPTEntry;
  14. import com.graphhopper.routing.util.AccessFilter;
  15. import com.graphhopper.routing.util.EdgeFilter;
  16. import com.graphhopper.routing.util.FlagEncoder;
  17. import com.graphhopper.routing.util.TraversalMode;
  18. import com.graphhopper.routing.weighting.Weighting;
  19. import com.graphhopper.storage.Graph;
  20. import com.graphhopper.storage.NodeAccess;
  21. import com.graphhopper.util.EdgeExplorer;
  22. import com.graphhopper.util.EdgeIterator;
  23. import org.heigit.ors.exceptions.MaxVisitedNodesExceededException;

  24. public abstract class AbstractOneToManyRoutingAlgorithm implements OneToManyRoutingAlgorithm {
  25.     protected final Graph graph;
  26.     protected final Weighting weighting;
  27.     protected final FlagEncoder flagEncoder;
  28.     protected final TraversalMode traversalMode;
  29.     protected NodeAccess nodeAccess;
  30.     protected EdgeExplorer inEdgeExplorer;
  31.     protected EdgeExplorer outEdgeExplorer;
  32.     protected int maxVisitedNodes = Integer.MAX_VALUE;
  33.     private EdgeFilter additionalEdgeFilter;

  34.     /**
  35.      * @param graph         specifies the graph where this algorithm will run on
  36.      * @param weighting     set the used weight calculation (e.g. fastest, shortest).
  37.      * @param traversalMode how the graph is traversed e.g. if via nodes or edges.
  38.      */
  39.     public AbstractOneToManyRoutingAlgorithm(Graph graph, Weighting weighting, TraversalMode traversalMode) {
  40.         this.weighting = weighting;
  41.         this.flagEncoder = weighting.getFlagEncoder();
  42.         this.traversalMode = traversalMode;
  43.         this.graph = graph;
  44.         this.nodeAccess = graph.getNodeAccess();
  45.         outEdgeExplorer = graph.createEdgeExplorer(AccessFilter.outEdges(flagEncoder.getAccessEnc()));
  46.         inEdgeExplorer = graph.createEdgeExplorer(AccessFilter.inEdges(flagEncoder.getAccessEnc()));
  47.     }

  48.     @Override
  49.     public void setMaxVisitedNodes(int numberOfNodes) {
  50.         this.maxVisitedNodes = numberOfNodes;
  51.     }

  52.     public AbstractOneToManyRoutingAlgorithm setEdgeFilter(EdgeFilter additionalEdgeFilter) {
  53.         this.additionalEdgeFilter = additionalEdgeFilter;
  54.         return this;
  55.     }

  56.     protected boolean accept(EdgeIterator iter, int prevOrNextEdgeId) {
  57.         return additionalEdgeFilter == null || additionalEdgeFilter.accept(iter);
  58.     }

  59.     protected SPTEntry createSPTEntry(int node, double weight) {
  60.         return new SPTEntry(EdgeIterator.NO_EDGE, node, weight);
  61.     }

  62.     @Override
  63.     public String getName() {
  64.         return getClass().getSimpleName();
  65.     }

  66.     @Override
  67.     public String toString() {
  68.         return getName() + "|" + weighting;
  69.     }

  70.     protected boolean isMaxVisitedNodesExceeded() {
  71.         return maxVisitedNodes < getVisitedNodes();
  72.     }
  73. }