Skip to content

Commit 7f2bff4

Browse files
author
Peter
committed
no default vehicle for algorithms, use RoutingAlgorithmFactory methods for simple defaults instead
1 parent c349d01 commit 7f2bff4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+337
-237
lines changed

src/main/java/com/graphhopper/GHRequest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
package com.graphhopper;
2020

2121
import com.graphhopper.routing.util.CarFlagEncoder;
22-
import com.graphhopper.routing.util.VehicleFlagEncoder;
22+
import com.graphhopper.routing.util.VehicleEncoder;
2323
import com.graphhopper.routing.util.ShortestCalc;
2424
import com.graphhopper.routing.util.WeightCalculation;
2525
import com.graphhopper.util.shapes.GHPoint;
@@ -35,7 +35,7 @@ public class GHRequest {
3535
private GHPoint from;
3636
private GHPoint to;
3737
private double precision = 1;
38-
private VehicleFlagEncoder encoder = new CarFlagEncoder();
38+
private VehicleEncoder encoder = new CarFlagEncoder();
3939
private WeightCalculation weightCalc = new ShortestCalc();
4040

4141
/**
@@ -112,12 +112,12 @@ public WeightCalculation type() {
112112
return weightCalc;
113113
}
114114

115-
public GHRequest vehicle(VehicleFlagEncoder encoder) {
115+
public GHRequest vehicle(VehicleEncoder encoder) {
116116
this.encoder = encoder;
117117
return this;
118118
}
119119

120-
public VehicleFlagEncoder vehicle() {
120+
public VehicleEncoder vehicle() {
121121
return encoder;
122122
}
123123
}

src/main/java/com/graphhopper/GraphHopper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import com.graphhopper.routing.util.AlgorithmPreparation;
2727
import com.graphhopper.routing.util.CarFlagEncoder;
2828
import com.graphhopper.routing.util.FastestCalc;
29-
import com.graphhopper.routing.util.VehicleFlagEncoder;
29+
import com.graphhopper.routing.util.VehicleEncoder;
3030
import com.graphhopper.routing.util.FootFlagEncoder;
3131
import com.graphhopper.routing.util.NoOpAlgorithmPreparation;
3232
import com.graphhopper.routing.util.ShortestCalc;
@@ -173,7 +173,7 @@ else if (new File(graphHopperFile + ".osm").exists())
173173
storage = new LevelGraphStorage(dir);
174174
PrepareContractionHierarchies tmpPrepareCH = new PrepareContractionHierarchies();
175175

176-
VehicleFlagEncoder encoder;
176+
VehicleEncoder encoder;
177177
if (acceptWay.acceptsCar())
178178
encoder = new CarFlagEncoder();
179179
else
@@ -246,9 +246,9 @@ else if (request.algorithm().equals("astarbi"))
246246
else
247247
throw new IllegalStateException("Only dijkstrabi and astarbi is supported for levelgraph/CH!");
248248
} else {
249-
prepare = NoOpAlgorithmPreparation.createAlgoPrepare(graph, request.algorithm());
249+
prepare = NoOpAlgorithmPreparation.createAlgoPrepare(graph, request.algorithm(), request.vehicle());
250250
algo = prepare.createAlgo();
251-
algo.type(request.type()).vehicle(request.vehicle());
251+
algo.type(request.type());
252252
}
253253
debug += ", algoInit:" + sw.stop().getSeconds() + "s";
254254

src/main/java/com/graphhopper/reader/OSMReader.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.graphhopper.routing.util.FastestCalc;
2424
import com.graphhopper.routing.ch.PrepareContractionHierarchies;
2525
import com.graphhopper.routing.util.CarFlagEncoder;
26-
import com.graphhopper.routing.util.VehicleFlagEncoder;
26+
import com.graphhopper.routing.util.VehicleEncoder;
2727
import com.graphhopper.routing.util.NoOpAlgorithmPreparation;
2828
import com.graphhopper.routing.util.PrepareRoutingSubnetworks;
2929
import com.graphhopper.routing.util.RoutingAlgorithmSpecialAreaTests;
@@ -69,12 +69,13 @@ public static void main(String[] strs) throws Exception {
6969
tests.start();
7070

7171
if (args.getBool("osmreader.runshortestpath", false)) {
72-
72+
String type = args.get("osmreader.type", "CAR");
73+
VehicleEncoder encoder = AcceptWay.parse(type).firstEncoder();
7374
String algo = args.get("osmreader.algo", "dijkstra");
7475
int iters = args.getInt("osmreader.algoIterations", 50);
75-
//warmup
76-
tests.runShortestPathPerf(iters / 10, NoOpAlgorithmPreparation.createAlgoPrepare(g, algo));
77-
tests.runShortestPathPerf(iters, NoOpAlgorithmPreparation.createAlgoPrepare(g, algo));
76+
//warmup
77+
tests.runShortestPathPerf(iters / 10, NoOpAlgorithmPreparation.createAlgoPrepare(g, algo, encoder));
78+
tests.runShortestPathPerf(iters, NoOpAlgorithmPreparation.createAlgoPrepare(g, algo, encoder));
7879
}
7980
}
8081
private static Logger logger = LoggerFactory.getLogger(OSMReader.class);
@@ -146,10 +147,12 @@ public static OSMReader osm2Graph(final CmdArgs args) throws IOException {
146147
public static OSMReader osm2Graph(OSMReader osmReader, CmdArgs args) throws IOException {
147148
osmReader.indexCapacity(args.getInt("osmreader.locationIndexCapacity", -1));
148149
String type = args.get("osmreader.type", "CAR");
149-
osmReader.acceptStreet(new AcceptWay(type.contains("CAR"),
150-
type.contains("BIKE"), type.contains("FOOT")));
150+
AcceptWay acceptWay = AcceptWay.parse(type);
151+
osmReader.acceptStreet(acceptWay);
151152
final String algoStr = args.get("osmreader.algo", "astar");
152-
osmReader.defaultAlgoPrepare(NoOpAlgorithmPreparation.createAlgoPrepare(algoStr));
153+
AlgorithmPreparation algoPrepare = NoOpAlgorithmPreparation.
154+
createAlgoPrepare(algoStr, acceptWay.firstEncoder());
155+
osmReader.defaultAlgoPrepare(algoPrepare);
153156
osmReader.sort(args.getBool("osmreader.sortGraph", false));
154157
osmReader.setCHShortcuts(args.get("osmreader.chShortcuts", "no"));
155158
if (!osmReader.loadExisting()) {
@@ -175,7 +178,7 @@ public OSMReader(GraphStorage storage, long expectedNodes) {
175178
this.graphStorage = storage;
176179
this.expectedNodes = expectedNodes;
177180
helper = createDoubleParseHelper();
178-
helper.acceptWays(new AcceptWay(true, false, false));
181+
helper.acceptWay(new AcceptWay(true, false, false));
179182
}
180183

181184
boolean loadExisting() {
@@ -202,7 +205,7 @@ else if (file.getAbsolutePath().endsWith(".zip"))
202205

203206
void osm2Graph(File osmXmlFile) throws IOException {
204207
logger.info("using " + helper.getStorageInfo(graphStorage) + ", accepts:"
205-
+ helper.acceptWays() + ", memory:" + Helper.getMemInfo());
208+
+ helper.acceptWay() + ", memory:" + Helper.getMemInfo());
206209
helper.preProcess(createInputStream(osmXmlFile));
207210
writeOsm2Graph(createInputStream(osmXmlFile));
208211
cleanUp();
@@ -225,7 +228,8 @@ void optimize() {
225228

226229
logger.info("calling prepare.doWork ... (" + Helper.getMemInfo() + ")");
227230
if (prepare == null)
228-
defaultAlgoPrepare(NoOpAlgorithmPreparation.createAlgoPrepare("astar"));
231+
defaultAlgoPrepare(NoOpAlgorithmPreparation.createAlgoPrepare("dijkstrabi",
232+
helper.acceptWay().firstEncoder()));
229233
else
230234
prepare.doWork();
231235
}
@@ -358,7 +362,7 @@ public Graph graph() {
358362
* Specify the type of the path calculation (car, bike, ...).
359363
*/
360364
public OSMReader acceptStreet(AcceptWay acceptWays) {
361-
helper.acceptWays(acceptWays);
365+
helper.acceptWay(acceptWays);
362366
return this;
363367
}
364368

@@ -376,10 +380,10 @@ public OSMReader setCHShortcuts(String chShortcuts) {
376380
if (chShortcuts.isEmpty() || "no".equals(chShortcuts) || "false".equals(chShortcuts))
377381
return this;
378382

379-
VehicleFlagEncoder encoder = new CarFlagEncoder();
383+
VehicleEncoder encoder = new CarFlagEncoder();
380384
int tmpIndex = chShortcuts.indexOf(",");
381385
if (tmpIndex >= 0)
382-
encoder = Helper.getEncoder(chShortcuts.substring(tmpIndex + 1).trim());
386+
encoder = Helper.getVehicleEncoder(chShortcuts.substring(tmpIndex + 1).trim());
383387
if ("true".equals(chShortcuts) || "fastest".equals(chShortcuts)) {
384388
prepare = new PrepareContractionHierarchies().type(new FastestCalc(encoder)).vehicle(encoder);
385389
} else if ("shortest".equals(chShortcuts)) {

src/main/java/com/graphhopper/reader/OSMReaderHelper.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public abstract class OSMReaderHelper {
4747
protected final Graph g;
4848
protected final long expectedNodes;
4949
private DistanceCalc callback = new DistanceCalc();
50-
private AcceptWay acceptWays;
50+
private AcceptWay acceptWay;
5151
protected TLongArrayList wayNodes = new TLongArrayList(10);
5252
private Map<String, Object> osmProperties = new HashMap<String, Object>();
5353
private Map<String, Object> outProperties = new HashMap<String, Object>();
@@ -57,13 +57,13 @@ public OSMReaderHelper(Graph g, long expectedNodes) {
5757
this.expectedNodes = expectedNodes;
5858
}
5959

60-
public OSMReaderHelper acceptWays(AcceptWay acceptWays) {
61-
this.acceptWays = acceptWays;
60+
public OSMReaderHelper acceptWay(AcceptWay acceptWay) {
61+
this.acceptWay = acceptWay;
6262
return this;
6363
}
6464

65-
public AcceptWay acceptWays() {
66-
return acceptWays;
65+
public AcceptWay acceptWay() {
66+
return acceptWay;
6767
}
6868

6969
public void callback(DistanceCalc callback) {
@@ -137,7 +137,7 @@ void startWayProcessing() {
137137
public void processWay(XMLStreamReader sReader) throws XMLStreamException {
138138
boolean valid = parseWay(sReader);
139139
if (valid) {
140-
int flags = acceptWays.toFlags(outProperties);
140+
int flags = acceptWay.toFlags(outProperties);
141141
int successfullAdded = addEdge(wayNodes, flags);
142142
edgeCount += successfullAdded;
143143
}
@@ -174,7 +174,7 @@ boolean parseWay(XMLStreamReader sReader) throws XMLStreamException {
174174
}
175175
}
176176

177-
boolean isWay = acceptWays.handleTags(outProperties, osmProperties, wayNodes);
177+
boolean isWay = acceptWay.handleTags(outProperties, osmProperties, wayNodes);
178178
boolean hasNodes = wayNodes.size() > 1;
179179
return isWay && hasNodes;
180180
}

src/main/java/com/graphhopper/routing/AStar.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.graphhopper.coll.MyBitSet;
2222
import com.graphhopper.coll.MyBitSetImpl;
23+
import com.graphhopper.routing.util.VehicleEncoder;
2324
import com.graphhopper.storage.EdgeEntry;
2425
import com.graphhopper.storage.Graph;
2526
import com.graphhopper.util.DistanceCalc;
@@ -43,8 +44,8 @@ public class AStar extends AbstractRoutingAlgorithm {
4344
private boolean alreadyRun;
4445
private MyBitSet closedSet;
4546

46-
public AStar(Graph g) {
47-
super(g);
47+
public AStar(Graph g, VehicleEncoder encoder) {
48+
super(g, encoder);
4849
}
4950

5051
/**

src/main/java/com/graphhopper/routing/AStarBidirection.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.graphhopper.coll.MyBitSetImpl;
2323
import com.graphhopper.routing.AStar.AStarEdge;
2424
import com.graphhopper.routing.util.EdgeFilter;
25+
import com.graphhopper.routing.util.VehicleEncoder;
2526
import com.graphhopper.storage.Graph;
2627
import com.graphhopper.util.DistanceCalc;
2728
import com.graphhopper.util.DistancePlaneProjection;
@@ -78,8 +79,8 @@ public class AStarBidirection extends AbstractRoutingAlgorithm {
7879
private CoordTrig toCoord;
7980
protected double approximationFactor;
8081

81-
public AStarBidirection(Graph graph) {
82-
super(graph);
82+
public AStarBidirection(Graph graph, VehicleEncoder encoder) {
83+
super(graph, encoder);
8384
int nodes = Math.max(20, graph.nodes());
8485
initCollections(nodes);
8586
approximation(false);

src/main/java/com/graphhopper/routing/AbstractRoutingAlgorithm.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
*/
1919
package com.graphhopper.routing;
2020

21-
import com.graphhopper.routing.util.CarFlagEncoder;
2221
import com.graphhopper.routing.util.DefaultEdgeFilter;
2322
import com.graphhopper.routing.util.EdgeFilter;
24-
import com.graphhopper.routing.util.VehicleFlagEncoder;
23+
import com.graphhopper.routing.util.VehicleEncoder;
2524
import com.graphhopper.routing.util.ShortestCalc;
2625
import com.graphhopper.routing.util.WeightCalculation;
2726
import com.graphhopper.storage.EdgeEntry;
@@ -34,27 +33,23 @@
3433
public abstract class AbstractRoutingAlgorithm implements RoutingAlgorithm {
3534

3635
protected Graph graph;
37-
protected WeightCalculation weightCalc;
38-
protected EdgeFilter outEdgeFilter;
39-
protected EdgeFilter inEdgeFilter;
40-
protected VehicleFlagEncoder flagEncoder;
4136
private EdgeFilter additionalEdgeFilter;
37+
protected WeightCalculation weightCalc;
38+
protected final EdgeFilter outEdgeFilter;
39+
protected final EdgeFilter inEdgeFilter;
40+
protected final VehicleEncoder flagEncoder;
4241

43-
public AbstractRoutingAlgorithm(Graph graph) {
42+
public AbstractRoutingAlgorithm(Graph graph, VehicleEncoder encoder) {
4443
this.graph = graph;
4544
this.additionalEdgeFilter = new EdgeFilter() {
4645
@Override public final boolean accept(EdgeIterator iter) {
4746
return true;
4847
}
4948
};
50-
type(new ShortestCalc()).vehicle(new CarFlagEncoder());
51-
}
52-
53-
@Override public RoutingAlgorithm vehicle(VehicleFlagEncoder encoder) {
49+
type(new ShortestCalc());
5450
this.flagEncoder = encoder;
5551
outEdgeFilter = new DefaultEdgeFilter(encoder, false, true);
56-
inEdgeFilter = new DefaultEdgeFilter(encoder, true, false);
57-
return this;
52+
inEdgeFilter = new DefaultEdgeFilter(encoder, true, false);
5853
}
5954

6055
public RoutingAlgorithm edgeFilter(EdgeFilter additionalEdgeFilter) {

src/main/java/com/graphhopper/routing/DijkstraBidirection.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.graphhopper.coll.MyBitSet;
2323
import com.graphhopper.coll.MyBitSetImpl;
2424
import com.graphhopper.routing.util.EdgeFilter;
25+
import com.graphhopper.routing.util.VehicleEncoder;
2526
import com.graphhopper.storage.Graph;
2627
import com.graphhopper.util.EdgeIterator;
2728
import com.graphhopper.util.EdgeWrapper;
@@ -60,8 +61,8 @@ public class DijkstraBidirection extends AbstractRoutingAlgorithm {
6061
private EdgeWrapper wrapperTo;
6162
private boolean alreadyRun;
6263

63-
public DijkstraBidirection(Graph graph) {
64-
super(graph);
64+
public DijkstraBidirection(Graph graph, VehicleEncoder encoder) {
65+
super(graph, encoder);
6566
int locs = Math.max(20, graph.nodes());
6667
visitedFrom = new MyBitSetImpl(locs);
6768
openSetFrom = new IntDoubleBinHeap(locs / 10);

src/main/java/com/graphhopper/routing/DijkstraBidirectionRef.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.graphhopper.coll.MyBitSet;
2222
import com.graphhopper.coll.MyBitSetImpl;
2323
import com.graphhopper.routing.util.EdgeFilter;
24+
import com.graphhopper.routing.util.VehicleEncoder;
2425
import com.graphhopper.storage.EdgeEntry;
2526
import com.graphhopper.storage.Graph;
2627
import com.graphhopper.util.EdgeIterator;
@@ -52,8 +53,8 @@ public class DijkstraBidirectionRef extends AbstractRoutingAlgorithm {
5253
protected TIntObjectMap<EdgeEntry> shortestWeightMapOther;
5354
public PathBidirRef shortest;
5455

55-
public DijkstraBidirectionRef(Graph graph) {
56-
super(graph);
56+
public DijkstraBidirectionRef(Graph graph, VehicleEncoder encoder) {
57+
super(graph, encoder);
5758
initCollections(Math.max(20, graph.nodes()));
5859
}
5960

src/main/java/com/graphhopper/routing/DijkstraSimple.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.graphhopper.coll.MyBitSet;
2222
import com.graphhopper.coll.MyTBitSet;
23+
import com.graphhopper.routing.util.VehicleEncoder;
2324
import com.graphhopper.storage.EdgeEntry;
2425
import com.graphhopper.storage.Graph;
2526
import com.graphhopper.util.EdgeIterator;
@@ -39,8 +40,8 @@ public class DijkstraSimple extends AbstractRoutingAlgorithm {
3940
private TIntObjectMap<EdgeEntry> map = new TIntObjectHashMap<EdgeEntry>();
4041
private PriorityQueue<EdgeEntry> heap = new PriorityQueue<EdgeEntry>();
4142

42-
public DijkstraSimple(Graph graph) {
43-
super(graph);
43+
public DijkstraSimple(Graph graph, VehicleEncoder encoder) {
44+
super(graph, encoder);
4445
}
4546

4647
@Override

src/main/java/com/graphhopper/routing/Path.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package com.graphhopper.routing;
2020

21-
import com.graphhopper.routing.util.VehicleFlagEncoder;
21+
import com.graphhopper.routing.util.VehicleEncoder;
2222
import com.graphhopper.storage.EdgeEntry;
2323
import com.graphhopper.storage.Graph;
2424
import com.graphhopper.util.EdgeIterator;
@@ -41,7 +41,7 @@
4141
public class Path {
4242

4343
protected Graph graph;
44-
protected VehicleFlagEncoder encoder;
44+
protected VehicleEncoder encoder;
4545
protected double distance;
4646
// we go upwards (via EdgeEntry.parent) from the goal node to the origin node
4747
protected boolean reverseOrder = true;
@@ -54,7 +54,7 @@ public class Path {
5454
private PointList cachedPoints;
5555
private double weight;
5656

57-
public Path(Graph graph, VehicleFlagEncoder encoder) {
57+
public Path(Graph graph, VehicleEncoder encoder) {
5858
this.weight = Double.MAX_VALUE;
5959
this.graph = graph;
6060
this.encoder = encoder;

src/main/java/com/graphhopper/routing/PathBidir.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package com.graphhopper.routing;
2020

21-
import com.graphhopper.routing.util.VehicleFlagEncoder;
21+
import com.graphhopper.routing.util.VehicleEncoder;
2222
import com.graphhopper.storage.Graph;
2323
import com.graphhopper.util.EdgeWrapper;
2424

@@ -36,7 +36,7 @@ public class PathBidir extends Path {
3636
private EdgeWrapper edgeWFrom;
3737
private EdgeWrapper edgeWTo;
3838

39-
public PathBidir(Graph g, VehicleFlagEncoder encoder,
39+
public PathBidir(Graph g, VehicleEncoder encoder,
4040
EdgeWrapper edgesFrom, EdgeWrapper edgesTo) {
4141
super(g, encoder);
4242
this.edgeWFrom = edgesFrom;

0 commit comments

Comments
 (0)