From 7bd6bf79a0535b5a466a1366f0c1b3c4a496bfce Mon Sep 17 00:00:00 2001 From: easbar Date: Wed, 22 Jun 2022 13:12:20 +0200 Subject: [PATCH 01/28] Remove FlagEncoder from FastestWeighting and subclasses --- .../routing/DefaultWeightingFactory.java | 24 ++++++++--- .../routing/weighting/CurvatureWeighting.java | 29 ++++++------- .../routing/weighting/FastestWeighting.java | 42 +++++++++++-------- .../routing/weighting/PriorityWeighting.java | 14 ++++--- .../weighting/ShortFastestWeighting.java | 21 +++------- .../java/com/graphhopper/util/GHUtility.java | 19 ++------- .../algorithm/ShortestPathTreeTest.java | 17 ++++---- .../routing/AlternativeRouteCHTest.java | 2 +- .../routing/AlternativeRouteEdgeCHTest.java | 4 +- .../routing/AlternativeRouteTest.java | 2 +- .../DefaultBidirPathExtractorTest.java | 4 +- .../routing/DijkstraBidirectionCHTest.java | 6 +-- .../DirectedBidirectionalDijkstraTest.java | 4 +- .../routing/DirectedRoutingTest.java | 8 ++-- .../EdgeBasedRoutingAlgorithmTest.java | 6 +-- .../com/graphhopper/routing/PathTest.java | 4 +- .../routing/PriorityRoutingTest.java | 2 +- .../routing/QueryRoutingCHGraphTest.java | 2 +- .../routing/RandomCHRoutingTest.java | 8 ++-- .../routing/RandomizedRoutingTest.java | 12 +++--- .../routing/RoundTripRoutingTest.java | 2 +- .../routing/RoutingAlgorithmTest.java | 14 +++---- .../routing/RoutingCHGraphImplTest.java | 24 +++++------ ...fficChangeWithNodeOrderingReusingTest.java | 2 +- .../routing/ch/CHTurnCostTest.java | 6 +-- .../ch/NodeBasedNodeContractorTest.java | 6 ++- .../ch/PrepareContractionHierarchiesTest.java | 14 +++---- .../routing/lm/LMApproximatorTest.java | 2 +- .../graphhopper/routing/lm/LMIssueTest.java | 2 +- .../routing/lm/LMPreparationHandlerTest.java | 2 +- .../routing/lm/LandmarkStorageTest.java | 18 ++++---- .../routing/lm/PrepareLandmarksTest.java | 4 +- .../routing/querygraph/QueryGraphTest.java | 2 +- .../PrepareRoutingSubnetworksTest.java | 2 +- .../weighting/BlockAreaWeightingTest.java | 18 +++++--- .../weighting/FastestWeightingTest.java | 30 ++++++------- .../weighting/ShortFastestWeightingTest.java | 10 ++--- .../weighting/custom/CustomWeightingTest.java | 6 +-- .../storage/ShortcutUnpackerTest.java | 2 +- .../graphhopper/util/InstructionListTest.java | 18 ++++---- 40 files changed, 208 insertions(+), 206 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java index df708867a5a..d359484b542 100644 --- a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java +++ b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java @@ -19,6 +19,8 @@ package com.graphhopper.routing; import com.graphhopper.config.Profile; +import com.graphhopper.routing.ev.EnumEncodedValue; +import com.graphhopper.routing.ev.RoadAccess; import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.weighting.*; @@ -80,16 +82,26 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis } else if ("shortest".equalsIgnoreCase(weightingStr)) { weighting = new ShortestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), turnCostProvider); } else if ("fastest".equalsIgnoreCase(weightingStr)) { + if (!encodingManager.hasEncodedValue(RoadAccess.KEY)) + throw new IllegalArgumentException("fastest weighting requires road_access"); + EnumEncodedValue roadAccessEnc = encodingManager.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class); if (encoder.getPriorityEnc() != null) - weighting = new PriorityWeighting(encoder, hints, turnCostProvider); + weighting = new PriorityWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), encoder.getPriorityEnc(), roadAccessEnc, hints, turnCostProvider); else - weighting = new FastestWeighting(encoder, hints, turnCostProvider); + weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), roadAccessEnc, hints, turnCostProvider); } else if ("curvature".equalsIgnoreCase(weightingStr)) { - if (encoder.getCurvatureEnc() != null) - weighting = new CurvatureWeighting(encoder, hints, turnCostProvider); - + if (encoder.getCurvatureEnc() == null || encoder.getPriorityEnc() == null) + throw new IllegalArgumentException("curvature weighting requires curvature and priority, but not found for " + encoder.getName()); + if (!encodingManager.hasEncodedValue(RoadAccess.KEY)) + throw new IllegalArgumentException("curvature weighting requires road_access"); + EnumEncodedValue roadAccessEnc = encodingManager.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class); + weighting = new CurvatureWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), encoder.getPriorityEnc(), + encoder.getCurvatureEnc(), roadAccessEnc, hints, turnCostProvider); } else if ("short_fastest".equalsIgnoreCase(weightingStr)) { - weighting = new ShortFastestWeighting(encoder, hints, turnCostProvider); + if (!encodingManager.hasEncodedValue(RoadAccess.KEY)) + throw new IllegalArgumentException("curvature weighting requires road_access"); + EnumEncodedValue roadAccessEnc = encodingManager.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class); + weighting = new ShortFastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), roadAccessEnc, hints, turnCostProvider); } if (weighting == null) diff --git a/core/src/main/java/com/graphhopper/routing/weighting/CurvatureWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/CurvatureWeighting.java index 8528277264a..fd5f6cf0c32 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/CurvatureWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/CurvatureWeighting.java @@ -17,37 +17,32 @@ */ package com.graphhopper.routing.weighting; +import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.ev.EnumEncodedValue; +import com.graphhopper.routing.ev.RoadAccess; import com.graphhopper.routing.util.PriorityCode; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.PMap; import static com.graphhopper.routing.util.PriorityCode.BEST; -import static com.graphhopper.routing.weighting.TurnCostProvider.NO_TURN_COST_PROVIDER; /** * This Class uses bendiness parameter to prefer curvy routes. */ public class CurvatureWeighting extends PriorityWeighting { private final double minFactor; - private final DecimalEncodedValue priorityEnc; private final DecimalEncodedValue curvatureEnc; - private final DecimalEncodedValue avSpeedEnc; - public CurvatureWeighting(FlagEncoder flagEncoder, PMap pMap) { - this(flagEncoder, pMap, NO_TURN_COST_PROVIDER); - } - - public CurvatureWeighting(FlagEncoder flagEncoder, PMap pMap, TurnCostProvider turnCostProvider) { - super(flagEncoder, pMap, turnCostProvider); - - priorityEnc = flagEncoder.getDecimalEncodedValue(EncodingManager.getKey(flagEncoder, "priority")); - curvatureEnc = flagEncoder.getDecimalEncodedValue(EncodingManager.getKey(flagEncoder, "curvature")); - avSpeedEnc = flagEncoder.getDecimalEncodedValue(EncodingManager.getKey(flagEncoder, "average_speed")); + public CurvatureWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, DecimalEncodedValue priorityEnc, + DecimalEncodedValue curvatureEnc, EnumEncodedValue roadAccessEnc, PMap pMap, TurnCostProvider turnCostProvider) { + super(accessEnc, speedEnc, priorityEnc, roadAccessEnc, pMap, turnCostProvider); + this.curvatureEnc = curvatureEnc; double minBendiness = 1; // see correctErrors - minFactor = minBendiness / Math.log(flagEncoder.getMaxSpeed()) / PriorityCode.getValue(BEST.getValue()); + // todonow: take max speed from speed EV +// double maxSpeed = flagEncoder.getMaxSpeed(); + double maxSpeed = 300; + minFactor = minBendiness / Math.log(maxSpeed) / PriorityCode.getValue(BEST.getValue()); } @Override @@ -68,7 +63,7 @@ public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) { } protected double getRoadSpeed(EdgeIteratorState edge, boolean reverse) { - return reverse ? edge.getReverse(avSpeedEnc) : edge.get(avSpeedEnc); + return reverse ? edge.getReverse(speedEnc) : edge.get(speedEnc); } @Override diff --git a/core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java index c302384c51c..73e614c67b1 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java @@ -17,9 +17,10 @@ */ package com.graphhopper.routing.weighting; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; import com.graphhopper.routing.ev.EnumEncodedValue; import com.graphhopper.routing.ev.RoadAccess; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.PMap; import com.graphhopper.util.Parameters.Routing; @@ -46,33 +47,38 @@ public class FastestWeighting extends AbstractWeighting { // this factor puts a penalty on roads with a "destination"-only or private access, see #733 and #1936 private final double destinationPenalty, privatePenalty; - public FastestWeighting(FlagEncoder encoder) { - this(encoder, new PMap(0)); + public FastestWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { + this(accessEnc, speedEnc, NO_TURN_COST_PROVIDER); } - public FastestWeighting(FlagEncoder encoder, TurnCostProvider turnCostProvider) { - this(encoder, new PMap(0), turnCostProvider); + public FastestWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, TurnCostProvider turnCostProvider) { + this(accessEnc, speedEnc, null, new PMap(0), turnCostProvider); } - public FastestWeighting(FlagEncoder encoder, PMap map) { - this(encoder, map, NO_TURN_COST_PROVIDER); - } - - public FastestWeighting(FlagEncoder encoder, PMap map, TurnCostProvider turnCostProvider) { - super(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), turnCostProvider); + public FastestWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, EnumEncodedValue roadAccessEnc, PMap map, TurnCostProvider turnCostProvider) { + super(accessEnc, speedEnc, turnCostProvider); headingPenalty = map.getDouble(Routing.HEADING_PENALTY, Routing.DEFAULT_HEADING_PENALTY); headingPenaltyMillis = Math.round(headingPenalty * 1000); - maxSpeed = encoder.getMaxSpeed() / SPEED_CONV; - - if (!encoder.hasEncodedValue(RoadAccess.KEY)) - throw new IllegalArgumentException("road_access is not available but expected for FastestWeighting"); + // todonow: take max speed from speedEnc +// maxSpeed = encoder.getMaxSpeed() / SPEED_CONV; + maxSpeed = 140 / SPEED_CONV; // ensure that we do not need to change getMinWeight, i.e. road_access_factor >= 1 - double defaultDestinationFactor = encoder.isMotorVehicle() ? 10 : 1; + // todonow: maybe setup these factors in weighting factory? +// double defaultDestinationFactor = encoder.isMotorVehicle() ? 10 : 1; + double defaultDestinationFactor = 10; destinationPenalty = checkBounds("road_access_destination_factor", map.getDouble("road_access_destination_factor", defaultDestinationFactor), 1, 10); - double defaultPrivateFactor = encoder.isMotorVehicle() ? 10 : 1.2; + // todonow: maybe setup these factors in weighting factory? +// double defaultPrivateFactor = encoder.isMotorVehicle() ? 10 : 1.2; + double defaultPrivateFactor = 10; privatePenalty = checkBounds("road_access_private_factor", map.getDouble("road_access_private_factor", defaultPrivateFactor), 1, 10); - roadAccessEnc = destinationPenalty > 1 || privatePenalty > 1 ? encoder.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class) : null; + if (destinationPenalty > 1 || privatePenalty > 1) { + // todonow: maybe only error when factors are given *explicitly* +// if (roadAccessEnc == null) +// throw new IllegalArgumentException("road_access must not be null when destination or private penalties are > 1"); + this.roadAccessEnc = roadAccessEnc; + } else + this.roadAccessEnc = null; } @Override diff --git a/core/src/main/java/com/graphhopper/routing/weighting/PriorityWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/PriorityWeighting.java index 6e3c5094505..43f33a43d16 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/PriorityWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/PriorityWeighting.java @@ -17,9 +17,10 @@ */ package com.graphhopper.routing.weighting; +import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.ev.EnumEncodedValue; +import com.graphhopper.routing.ev.RoadAccess; import com.graphhopper.routing.util.PriorityCode; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.PMap; @@ -35,11 +36,12 @@ public class PriorityWeighting extends FastestWeighting { private final double minFactor; private final double maxPrio; - private final DecimalEncodedValue priorityEnc; + protected final DecimalEncodedValue priorityEnc; - public PriorityWeighting(FlagEncoder encoder, PMap pMap, TurnCostProvider turnCostProvider) { - super(encoder, pMap, turnCostProvider); - priorityEnc = encoder.getDecimalEncodedValue(EncodingManager.getKey(encoder, "priority")); + public PriorityWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, DecimalEncodedValue priorityEnc, + EnumEncodedValue roadAccessEnc, PMap pMap, TurnCostProvider turnCostProvider) { + super(accessEnc, speedEnc, roadAccessEnc, pMap, turnCostProvider); + this.priorityEnc = priorityEnc; minFactor = 1 / PriorityCode.getValue(BEST.getValue()); maxPrio = PriorityCode.getFactor(BEST.getValue()); } diff --git a/core/src/main/java/com/graphhopper/routing/weighting/ShortFastestWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/ShortFastestWeighting.java index cc6ec82b949..90bdf80be12 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/ShortFastestWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/ShortFastestWeighting.java @@ -17,12 +17,13 @@ */ package com.graphhopper.routing.weighting; -import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.EnumEncodedValue; +import com.graphhopper.routing.ev.RoadAccess; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.PMap; -import static com.graphhopper.routing.weighting.TurnCostProvider.NO_TURN_COST_PROVIDER; - /** * Calculates the fastest route with distance influence controlled by a new parameter. *

@@ -36,8 +37,8 @@ public class ShortFastestWeighting extends FastestWeighting { private final double distanceFactor; private final double timeFactor; - public ShortFastestWeighting(FlagEncoder encoder, PMap map, TurnCostProvider turnCostProvider) { - super(encoder, map, turnCostProvider); + public ShortFastestWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, EnumEncodedValue roadAccessEnc, PMap map, TurnCostProvider turnCostProvider) { + super(accessEnc, speedEnc, roadAccessEnc, map, turnCostProvider); timeFactor = checkBounds(TIME_FACTOR, map.getDouble(TIME_FACTOR, 1), 0, 10); // default value derived from the cost for time e.g. 25€/hour and for distance 0.5€/km @@ -47,16 +48,6 @@ public ShortFastestWeighting(FlagEncoder encoder, PMap map, TurnCostProvider tur throw new IllegalArgumentException("[" + NAME + "] one of distance_factor or time_factor has to be non-zero"); } - public ShortFastestWeighting(FlagEncoder encoder, double distanceFactor) { - this(encoder, distanceFactor, NO_TURN_COST_PROVIDER); - } - - public ShortFastestWeighting(FlagEncoder encoder, double distanceFactor, TurnCostProvider turnCostProvider) { - super(encoder, new PMap(), turnCostProvider); - this.distanceFactor = checkBounds(DISTANCE_FACTOR, distanceFactor, 0, 10); - this.timeFactor = 1; - } - @Override public double getMinWeight(double distance) { return super.getMinWeight(distance) * timeFactor + distance * distanceFactor; diff --git a/core/src/main/java/com/graphhopper/util/GHUtility.java b/core/src/main/java/com/graphhopper/util/GHUtility.java index 8ef7448fdbd..619c126e246 100644 --- a/core/src/main/java/com/graphhopper/util/GHUtility.java +++ b/core/src/main/java/com/graphhopper/util/GHUtility.java @@ -24,7 +24,10 @@ import com.graphhopper.coll.GHBitSet; import com.graphhopper.coll.GHBitSetImpl; import com.graphhopper.routing.ev.*; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.AccessFilter; +import com.graphhopper.routing.util.AllEdgesIterator; +import com.graphhopper.routing.util.CustomArea; +import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.*; import com.graphhopper.storage.index.LocationIndex; @@ -149,19 +152,11 @@ public static List getEdgeIds(EdgeIterator iter) { return list; } - public static void printGraphForUnitTest(Graph g, FlagEncoder encoder) { - printGraphForUnitTest(g, encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); - } - public static void printGraphForUnitTest(Graph g, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { printGraphForUnitTest(g, accessEnc, speedEnc, new BBox( Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); } - public static void printGraphForUnitTest(Graph g, FlagEncoder encoder, BBox bBox) { - printGraphForUnitTest(g, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), bBox); - } - public static void printGraphForUnitTest(Graph g, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, BBox bBox) { System.out.println("WARNING: printGraphForUnitTest does not pay attention to custom edge speeds at the moment"); NodeAccess na = g.getNodeAccess(); @@ -267,12 +262,6 @@ public static double getDistance(int from, int to, NodeAccess nodeAccess) { return DistancePlaneProjection.DIST_PLANE.calcDist(fromLat, fromLon, toLat, toLon); } - public static void addRandomTurnCosts(Graph graph, long seed, EncodingManager em, FlagEncoder encoder, int maxTurnCost, TurnCostStorage turnCostStorage) { - DecimalEncodedValue turnCostEnc = em.getDecimalEncodedValue(TurnCost.key(encoder.toString())); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - addRandomTurnCosts(graph, seed, accessEnc, turnCostEnc, maxTurnCost, turnCostStorage); - } - public static void addRandomTurnCosts(Graph graph, long seed, BooleanEncodedValue accessEnc, DecimalEncodedValue turnCostEnc, int maxTurnCost, TurnCostStorage turnCostStorage) { Random random = new Random(seed); double pNodeHasTurnCosts = 0.3; diff --git a/core/src/test/java/com/graphhopper/isochrone/algorithm/ShortestPathTreeTest.java b/core/src/test/java/com/graphhopper/isochrone/algorithm/ShortestPathTreeTest.java index 29fc51753c3..83a4204c086 100644 --- a/core/src/test/java/com/graphhopper/isochrone/algorithm/ShortestPathTreeTest.java +++ b/core/src/test/java/com/graphhopper/isochrone/algorithm/ShortestPathTreeTest.java @@ -11,7 +11,6 @@ import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.Graph; import com.graphhopper.util.GHUtility; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -120,7 +119,7 @@ public void tearDown() { @Test public void testSPTAndIsochrone25Seconds() { List result = new ArrayList<>(); - ShortestPathTree instance = new ShortestPathTree(graph, new FastestWeighting(carEncoder, new PMap()), false, TraversalMode.NODE_BASED); + ShortestPathTree instance = new ShortestPathTree(graph, new FastestWeighting(accessEnc, speedEnc), false, TraversalMode.NODE_BASED); instance.setTimeLimit(25_000); instance.search(0, result::add); assertEquals(3, result.size()); @@ -136,7 +135,7 @@ public void testSPTAndIsochrone25Seconds() { @Test public void testSPT26Seconds() { List result = new ArrayList<>(); - ShortestPathTree instance = new ShortestPathTree(graph, new FastestWeighting(carEncoder, new PMap()), false, TraversalMode.NODE_BASED); + ShortestPathTree instance = new ShortestPathTree(graph, new FastestWeighting(accessEnc, speedEnc), false, TraversalMode.NODE_BASED); instance.setTimeLimit(26_000); instance.search(0, result::add); assertEquals(4, result.size()); @@ -151,7 +150,7 @@ public void testSPT26Seconds() { @Test public void testNoTimeLimit() { List result = new ArrayList<>(); - ShortestPathTree instance = new ShortestPathTree(graph, new FastestWeighting(carEncoder, new PMap()), false, TraversalMode.NODE_BASED); + ShortestPathTree instance = new ShortestPathTree(graph, new FastestWeighting(accessEnc, speedEnc), false, TraversalMode.NODE_BASED); instance.setTimeLimit(Double.MAX_VALUE); instance.search(0, result::add); assertEquals(9, result.size()); @@ -171,7 +170,7 @@ public void testNoTimeLimit() { @Test public void testEdgeBasedWithFreeUTurns() { List result = new ArrayList<>(); - ShortestPathTree instance = new ShortestPathTree(graph, new FastestWeighting(carEncoder, new PMap()), false, TraversalMode.EDGE_BASED); + ShortestPathTree instance = new ShortestPathTree(graph, new FastestWeighting(accessEnc, speedEnc), false, TraversalMode.EDGE_BASED); instance.setTimeLimit(Double.MAX_VALUE); instance.search(0, result::add); // The origin, and every end of every directed edge, are traversed. @@ -203,7 +202,7 @@ public void testEdgeBasedWithFreeUTurns() { @Test public void testEdgeBasedWithForbiddenUTurns() { - FastestWeighting fastestWeighting = new FastestWeighting(carEncoder, new PMap(), FORBIDDEN_UTURNS); + FastestWeighting fastestWeighting = new FastestWeighting(accessEnc, speedEnc, FORBIDDEN_UTURNS); List result = new ArrayList<>(); ShortestPathTree instance = new ShortestPathTree(graph, fastestWeighting, false, TraversalMode.EDGE_BASED); instance.setTimeLimit(Double.MAX_VALUE); @@ -237,7 +236,7 @@ public void testEdgeBasedWithForbiddenUTurns() { @Test public void testEdgeBasedWithFinitePositiveUTurnCost() { TimeBasedUTurnCost turnCost = new TimeBasedUTurnCost(80000); - FastestWeighting fastestWeighting = new FastestWeighting(carEncoder, new PMap(), turnCost); + FastestWeighting fastestWeighting = new FastestWeighting(accessEnc, speedEnc, turnCost); List result = new ArrayList<>(); ShortestPathTree instance = new ShortestPathTree(graph, fastestWeighting, false, TraversalMode.EDGE_BASED); instance.setTimeLimit(Double.MAX_VALUE); @@ -272,7 +271,7 @@ public void testEdgeBasedWithFinitePositiveUTurnCost() { @Test public void testEdgeBasedWithSmallerUTurnCost() { TimeBasedUTurnCost turnCost = new TimeBasedUTurnCost(20000); - FastestWeighting fastestWeighting = new FastestWeighting(carEncoder, new PMap(), turnCost); + FastestWeighting fastestWeighting = new FastestWeighting(accessEnc, speedEnc, turnCost); List result = new ArrayList<>(); ShortestPathTree instance = new ShortestPathTree(graph, fastestWeighting, false, TraversalMode.EDGE_BASED); instance.setTimeLimit(Double.MAX_VALUE); @@ -307,7 +306,7 @@ public void testEdgeBasedWithSmallerUTurnCost() { @Test public void testSearchByDistance() { List result = new ArrayList<>(); - ShortestPathTree instance = new ShortestPathTree(graph, new FastestWeighting(carEncoder, new PMap()), false, TraversalMode.NODE_BASED); + ShortestPathTree instance = new ShortestPathTree(graph, new FastestWeighting(accessEnc, speedEnc), false, TraversalMode.NODE_BASED); instance.setDistanceLimit(110.0); instance.search(5, result::add); assertEquals(6, result.size()); diff --git a/core/src/test/java/com/graphhopper/routing/AlternativeRouteCHTest.java b/core/src/test/java/com/graphhopper/routing/AlternativeRouteCHTest.java index d9082ed71e0..e83e16fe928 100644 --- a/core/src/test/java/com/graphhopper/routing/AlternativeRouteCHTest.java +++ b/core/src/test/java/com/graphhopper/routing/AlternativeRouteCHTest.java @@ -81,7 +81,7 @@ private RoutingCHGraph prepareCH(BaseGraph graph) { // meet on all four possible paths from 5 to 10 // 5 ---> 11 will be reachable via shortcuts, as 11 is on shortest path 5 --> 12 final int[] nodeOrdering = new int[]{0, 10, 12, 4, 3, 2, 5, 1, 6, 7, 8, 9, 11}; - CHConfig chConfig = CHConfig.nodeBased("p", new FastestWeighting(carFE)); + CHConfig chConfig = CHConfig.nodeBased("p", new FastestWeighting(carFE.getAccessEnc(), carFE.getAverageSpeedEnc())); PrepareContractionHierarchies contractionHierarchies = PrepareContractionHierarchies.fromGraph(graph, chConfig); contractionHierarchies.useFixedNodeOrdering(NodeOrderingProvider.fromArray(nodeOrdering)); PrepareContractionHierarchies.Result res = contractionHierarchies.doWork(); diff --git a/core/src/test/java/com/graphhopper/routing/AlternativeRouteEdgeCHTest.java b/core/src/test/java/com/graphhopper/routing/AlternativeRouteEdgeCHTest.java index 0bf2c254a6f..48253407f85 100644 --- a/core/src/test/java/com/graphhopper/routing/AlternativeRouteEdgeCHTest.java +++ b/core/src/test/java/com/graphhopper/routing/AlternativeRouteEdgeCHTest.java @@ -93,7 +93,7 @@ public BaseGraph createTestGraph(EncodingManager tmpEM) { private RoutingCHGraph prepareCH(BaseGraph graph) { TurnCostProvider turnCostProvider = new DefaultTurnCostProvider(carFE.getTurnCostEnc(), graph.getTurnCostStorage()); - CHConfig chConfig = CHConfig.edgeBased("profile", new FastestWeighting(carFE, turnCostProvider)); + CHConfig chConfig = CHConfig.edgeBased("profile", new FastestWeighting(carFE.getAccessEnc(), carFE.getAverageSpeedEnc(), turnCostProvider)); PrepareContractionHierarchies contractionHierarchies = PrepareContractionHierarchies.fromGraph(graph, chConfig); PrepareContractionHierarchies.Result res = contractionHierarchies.doWork(); return RoutingCHGraphImpl.fromGraph(graph, res.getCHStorage(), res.getCHConfig()); @@ -103,7 +103,7 @@ private RoutingCHGraph prepareCH(BaseGraph graph) { public void testAssumptions() { BaseGraph g = createTestGraph(em); TurnCostProvider turnCostProvider = new DefaultTurnCostProvider(carFE.getTurnCostEnc(), g.getTurnCostStorage()); - CHConfig chConfig = CHConfig.edgeBased("profile", new FastestWeighting(carFE, turnCostProvider)); + CHConfig chConfig = CHConfig.edgeBased("profile", new FastestWeighting(carFE.getAccessEnc(), carFE.getAverageSpeedEnc(), turnCostProvider)); CHStorage chStorage = CHStorage.fromGraph(g, chConfig); RoutingCHGraph chGraph = RoutingCHGraphImpl.fromGraph(g, chStorage, chConfig); DijkstraBidirectionEdgeCHNoSOD router = new DijkstraBidirectionEdgeCHNoSOD(chGraph); diff --git a/core/src/test/java/com/graphhopper/routing/AlternativeRouteTest.java b/core/src/test/java/com/graphhopper/routing/AlternativeRouteTest.java index 7e9f8ab0c66..728bb2d8ba5 100644 --- a/core/src/test/java/com/graphhopper/routing/AlternativeRouteTest.java +++ b/core/src/test/java/com/graphhopper/routing/AlternativeRouteTest.java @@ -58,7 +58,7 @@ public Fixture(TraversalMode tMode) { TurnCostProvider turnCostProvider = tMode.isEdgeBased() ? new DefaultTurnCostProvider(carFE.getTurnCostEnc(), graph.getTurnCostStorage()) : TurnCostProvider.NO_TURN_COST_PROVIDER; - weighting = new FastestWeighting(carFE, turnCostProvider); + weighting = new FastestWeighting(carFE.getAccessEnc(), carFE.getAverageSpeedEnc(), turnCostProvider); } @Override diff --git a/core/src/test/java/com/graphhopper/routing/DefaultBidirPathExtractorTest.java b/core/src/test/java/com/graphhopper/routing/DefaultBidirPathExtractorTest.java index 8d7cebad2df..2095be98811 100644 --- a/core/src/test/java/com/graphhopper/routing/DefaultBidirPathExtractorTest.java +++ b/core/src/test/java/com/graphhopper/routing/DefaultBidirPathExtractorTest.java @@ -55,7 +55,7 @@ public void testExtract() { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 2).setDistance(10)); SPTEntry fwdEntry = new SPTEntry(0, 2, 0, new SPTEntry(1, 10)); SPTEntry bwdEntry = new SPTEntry(2, 0); - Path p = DefaultBidirPathExtractor.extractPath(graph, new FastestWeighting(carEncoder), fwdEntry, bwdEntry, 0); + Path p = DefaultBidirPathExtractor.extractPath(graph, new FastestWeighting(accessEnc, speedEnc), fwdEntry, bwdEntry, 0); assertEquals(IntArrayList.from(1, 2), p.calcNodes()); assertEquals(10, p.getDistance(), 1e-4); } @@ -75,7 +75,7 @@ public void testExtract2() { SPTEntry fwdEntry = new SPTEntry(0, 2, 0.6, new SPTEntry(1, 0)); SPTEntry bwdEntry = new SPTEntry(1, 2, 1.2, new SPTEntry(3, 0)); - Path p = DefaultBidirPathExtractor.extractPath(graph, new FastestWeighting(carEncoder, new DefaultTurnCostProvider(carEncoder.getTurnCostEnc(), turnCostStorage)), fwdEntry, bwdEntry, 0); + Path p = DefaultBidirPathExtractor.extractPath(graph, new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(carEncoder.getTurnCostEnc(), turnCostStorage)), fwdEntry, bwdEntry, 0); p.setWeight(5 + 1.8); assertEquals(IntArrayList.from(1, 2, 3), p.calcNodes()); diff --git a/core/src/test/java/com/graphhopper/routing/DijkstraBidirectionCHTest.java b/core/src/test/java/com/graphhopper/routing/DijkstraBidirectionCHTest.java index 5c7fb6c11a5..b5d31342c00 100644 --- a/core/src/test/java/com/graphhopper/routing/DijkstraBidirectionCHTest.java +++ b/core/src/test/java/com/graphhopper/routing/DijkstraBidirectionCHTest.java @@ -77,8 +77,8 @@ public void testBaseGraphMultipleVehicles() { EncodingManager em = EncodingManager.create("foot,car"); FlagEncoder footEncoder = em.getEncoder("foot"); FlagEncoder carEncoder = em.getEncoder("car"); - FastestWeighting footWeighting = new FastestWeighting(footEncoder); - FastestWeighting carWeighting = new FastestWeighting(carEncoder); + FastestWeighting footWeighting = new FastestWeighting(footEncoder.getAccessEnc(), footEncoder.getAverageSpeedEnc()); + FastestWeighting carWeighting = new FastestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); CHConfig carConfig = CHConfig.nodeBased("p_car", carWeighting); BaseGraph g = new BaseGraph.Builder(em).create(); @@ -184,7 +184,7 @@ private void runTestWithDirectionDependentEdgeSpeed(double speed, double revSpee GHUtility.setSpeed(encoder.getMaxSpeed() / 2, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(1, 2).setDistance(1)); graph.freeze(); - FastestWeighting weighting = new FastestWeighting(encoder); + FastestWeighting weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); CHConfig chConfig = CHConfig.nodeBased(weighting.getName(), weighting); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); new CHStorageBuilder(chStore).setIdentityLevels(); diff --git a/core/src/test/java/com/graphhopper/routing/DirectedBidirectionalDijkstraTest.java b/core/src/test/java/com/graphhopper/routing/DirectedBidirectionalDijkstraTest.java index 20385628f9d..ad50b0083fd 100644 --- a/core/src/test/java/com/graphhopper/routing/DirectedBidirectionalDijkstraTest.java +++ b/core/src/test/java/com/graphhopper/routing/DirectedBidirectionalDijkstraTest.java @@ -62,7 +62,7 @@ public void setup() { } private Weighting createWeighting(int uTurnCosts) { - return new FastestWeighting(encoder, new DefaultTurnCostProvider(turnCostEnc, turnCostStorage, uTurnCosts)); + return new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, turnCostStorage, uTurnCosts)); } @Test @@ -396,7 +396,7 @@ private void compare_with_dijkstra(Weighting w) { int numNodes = 100; GHUtility.buildRandomGraph(graph, rnd, numNodes, 2.2, true, true, accessEnc, speedEnc, null, 0.7, 0.8, 0.8); - GHUtility.addRandomTurnCosts(graph, seed, encodingManager, encoder, maxTurnCosts, turnCostStorage); + GHUtility.addRandomTurnCosts(graph, seed, accessEnc, turnCostEnc, maxTurnCosts, turnCostStorage); long numStrictViolations = 0; for (int i = 0; i < numQueries; i++) { diff --git a/core/src/test/java/com/graphhopper/routing/DirectedRoutingTest.java b/core/src/test/java/com/graphhopper/routing/DirectedRoutingTest.java index 07bd7a1fb35..4558ca748c2 100644 --- a/core/src/test/java/com/graphhopper/routing/DirectedRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/DirectedRoutingTest.java @@ -110,10 +110,10 @@ public Fixture(Algo algo, int uTurnCosts, boolean prepareCH, boolean prepareLM) encodingManager = EncodingManager.start().add(encoder).add(Subnetwork.create("c2")).build(); graph = new BaseGraph.Builder(encodingManager).setDir(dir).withTurnCosts(true).create(); turnCostStorage = graph.getTurnCostStorage(); - weighting = new FastestWeighting(encoder, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), turnCostStorage, uTurnCosts)); + weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), new DefaultTurnCostProvider(encoder.getTurnCostEnc(), turnCostStorage, uTurnCosts)); chConfig = CHConfig.edgeBased("p1", weighting); // important: for LM preparation we need to use a weighting without turn costs #1960 - lmConfig = new LMConfig("c2", new FastestWeighting(encoder)); + lmConfig = new LMConfig("c2", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc())); } @Override @@ -217,7 +217,7 @@ public void randomGraph(Fixture f) { Random rnd = new Random(seed); GHUtility.buildRandomGraph(f.graph, rnd, 100, 2.2, true, true, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.8, 0.8); - GHUtility.addRandomTurnCosts(f.graph, seed, f.encodingManager, f.encoder, f.maxTurnCosts, f.turnCostStorage); + GHUtility.addRandomTurnCosts(f.graph, seed, f.encoder.getAccessEnc(), f.encoder.getTurnCostEnc(), f.maxTurnCosts, f.turnCostStorage); // GHUtility.printGraphForUnitTest(f.graph, f.encoder); f.preProcessGraph(); List strictViolations = new ArrayList<>(); @@ -259,7 +259,7 @@ public void randomGraph_withQueryGraph(Fixture f) { Random rnd = new Random(seed); GHUtility.buildRandomGraph(f.graph, rnd, 50, 2.2, true, true, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.8, pOffset); - GHUtility.addRandomTurnCosts(f.graph, seed, f.encodingManager, f.encoder, f.maxTurnCosts, f.turnCostStorage); + GHUtility.addRandomTurnCosts(f.graph, seed, f.encoder.getAccessEnc(), f.encoder.getTurnCostEnc(), f.maxTurnCosts, f.turnCostStorage); // GHUtility.printGraphForUnitTest(graph, encoder); f.preProcessGraph(); LocationIndexTree index = new LocationIndexTree(f.graph, f.dir); diff --git a/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java b/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java index 99cc4769034..a0ee238d911 100644 --- a/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java +++ b/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java @@ -157,7 +157,7 @@ private Weighting createWeighting() { } private Weighting createWeighting(int uTurnCosts) { - return new FastestWeighting(carEncoder, new DefaultTurnCostProvider(turnCostEnc, tcs, uTurnCosts)); + return new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, tcs, uTurnCosts)); } @ParameterizedTest @@ -170,7 +170,7 @@ public void testRandomGraph(String algoStr) { BaseGraph g = createStorage(em); GHUtility.buildRandomGraph(g, rnd, 50, 2.2, true, true, accessEnc, speedEnc, null, 0.8, 0.8, 0.8); - GHUtility.addRandomTurnCosts(g, seed, em, carEncoder, 3, tcs); + GHUtility.addRandomTurnCosts(g, seed, carEncoder.getAccessEnc(), carEncoder.getTurnCostEnc(), 3, tcs); g.freeze(); int numPathsNotFound = 0; // todo: reduce redundancy with RandomCHRoutingTest @@ -418,7 +418,7 @@ public void testTurnCostsBug_991(String algoStr) { setTurnCost(g, 2, 5, 6, 3); setTurnCost(g, 1, 6, 7, 4); - FastestWeighting weighting = new FastestWeighting(carEncoder, new DefaultTurnCostProvider(turnCostEnc, tcs) { + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, tcs) { @Override public double calcTurnWeight(int edgeFrom, int nodeVia, int edgeTo) { if (edgeFrom >= 0) diff --git a/core/src/test/java/com/graphhopper/routing/PathTest.java b/core/src/test/java/com/graphhopper/routing/PathTest.java index 1f788bfecf7..807fa426c9c 100644 --- a/core/src/test/java/com/graphhopper/routing/PathTest.java +++ b/core/src/test/java/com/graphhopper/routing/PathTest.java @@ -78,7 +78,7 @@ public void testWayList() { edge2.setWayGeometry(Helper.createPointList(11, 1, 10, 1)); SPTEntry e1 = new SPTEntry(edge2.getEdge(), 2, 1, new SPTEntry(edge1.getEdge(), 1, 1, new SPTEntry(0, 1))); - FastestWeighting weighting = new FastestWeighting(encoder); + FastestWeighting weighting = new FastestWeighting(carAccessEnc, carAvSpeedEnc); Path path = extractPath(g, weighting, e1); // 0-1-2 assertPList(Helper.createPointList(0, 0.1, 8, 1, 9, 1, 1, 0.1, 10, 1, 11, 1, 2, 0.1), path.calcPoints()); @@ -191,7 +191,7 @@ public void testFindInstruction() { new SPTEntry(edge1.getEdge(), 1, 1, new SPTEntry(0, 1) )))); - FastestWeighting weighting = new FastestWeighting(encoder); + FastestWeighting weighting = new FastestWeighting(carAccessEnc, carAvSpeedEnc); Path path = extractPath(g, weighting, e1); InstructionList il = InstructionsFromEdges.calcInstructions(path, path.graph, weighting, carManager, tr); diff --git a/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java b/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java index e91480ca35a..37aaec7f5c7 100644 --- a/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java @@ -73,7 +73,7 @@ void testMaxPriority() { // A* and Dijkstra should yield the same path (the max priority must be taken into account by weighting.getMinWeight) { - PriorityWeighting weighting = new PriorityWeighting(encoder, new PMap(), TurnCostProvider.NO_TURN_COST_PROVIDER); + PriorityWeighting weighting = new PriorityWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), encoder.getPriorityEnc(), null, new PMap(), TurnCostProvider.NO_TURN_COST_PROVIDER); Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); assertEquals(pathDijkstra.calcNodes(), pathAStar.calcNodes()); diff --git a/core/src/test/java/com/graphhopper/routing/QueryRoutingCHGraphTest.java b/core/src/test/java/com/graphhopper/routing/QueryRoutingCHGraphTest.java index caf2aa32d64..7ce674a10c8 100644 --- a/core/src/test/java/com/graphhopper/routing/QueryRoutingCHGraphTest.java +++ b/core/src/test/java/com/graphhopper/routing/QueryRoutingCHGraphTest.java @@ -55,7 +55,7 @@ public void setup() { encoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", 5).putObject("speed_two_directions", true)); encodingManager = EncodingManager.create(encoder); graph = new BaseGraph.Builder(encodingManager).create(); - weighting = new FastestWeighting(encoder, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage())); + weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage())); na = graph.getNodeAccess(); } diff --git a/core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java b/core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java index 840347c8947..386a9805706 100644 --- a/core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java @@ -63,8 +63,8 @@ private static final class Fixture { void freeze() { graph.freeze(); chConfig = traversalMode.isEdgeBased() - ? CHConfig.edgeBased("p", new FastestWeighting(encoder, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage(), uTurnCosts))) - : CHConfig.nodeBased("p", new FastestWeighting(encoder)); + ? CHConfig.edgeBased("p", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage(), uTurnCosts))) + : CHConfig.nodeBased("p", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc())); weighting = chConfig.getWeighting(); } @@ -105,7 +105,7 @@ public void random(Fixture f) { GHUtility.buildRandomGraph(f.graph, rnd, numNodes, 2.5, true, true, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.9, pOffset); if (f.traversalMode.isEdgeBased()) { - GHUtility.addRandomTurnCosts(f.graph, seed, f.encodingManager, f.encoder, f.maxTurnCosts, f.graph.getTurnCostStorage()); + GHUtility.addRandomTurnCosts(f.graph, seed, f.encoder.getAccessEnc(), f.encoder.getTurnCostEnc(), f.maxTurnCosts, f.graph.getTurnCostStorage()); } runRandomTest(f, rnd, 20); } @@ -154,7 +154,7 @@ public void issue1593(Fixture f) { Random rnd = new Random(seed); GHUtility.buildRandomGraph(f.graph, rnd, 50, 2.5, true, true, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.9, 0.0); - GHUtility.addRandomTurnCosts(f.graph, seed, f.encodingManager, f.encoder, f.maxTurnCosts, f.graph.getTurnCostStorage()); + GHUtility.addRandomTurnCosts(f.graph, seed, f.encoder.getAccessEnc(), f.encoder.getTurnCostEnc(), f.maxTurnCosts, f.graph.getTurnCostStorage()); runRandomTest(f, rnd, 20); } diff --git a/core/src/test/java/com/graphhopper/routing/RandomizedRoutingTest.java b/core/src/test/java/com/graphhopper/routing/RandomizedRoutingTest.java index 8c1ef5bd1c9..978b6fd95d3 100644 --- a/core/src/test/java/com/graphhopper/routing/RandomizedRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/RandomizedRoutingTest.java @@ -124,8 +124,8 @@ private static class Fixture { .create(); turnCostStorage = graph.getTurnCostStorage(); chConfigs = Arrays.asList( - CHConfig.nodeBased("p1", new FastestWeighting(encoder)), - CHConfig.edgeBased("p2", new FastestWeighting(encoder, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage()))) + CHConfig.nodeBased("p1", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc())), + CHConfig.edgeBased("p2", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage()))) ); // important: for LM preparation we need to use a weighting without turn costs #1960 lmConfig = new LMConfig("car", chConfigs.get(0).getWeighting()); @@ -304,8 +304,8 @@ public void randomGraph(Supplier fixtureSupplier) { Random rnd = new Random(seed); GHUtility.buildRandomGraph(f.graph, rnd, 100, 2.2, true, true, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.8, 0.8); - GHUtility.addRandomTurnCosts(f.graph, seed, f.encodingManager, f.encoder, f.maxTurnCosts, f.turnCostStorage); -// GHUtility.printGraphForUnitTest(f.graph, f.encoder); + GHUtility.addRandomTurnCosts(f.graph, seed, f.encoder.getAccessEnc(), f.encoder.getTurnCostEnc(), f.maxTurnCosts, f.turnCostStorage); +// GHUtility.printGraphForUnitTest(f.graph, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc()); f.preProcessGraph(); List strictViolations = new ArrayList<>(); for (int i = 0; i < numQueries; i++) { @@ -342,8 +342,8 @@ public void randomGraph_withQueryGraph(Supplier fixtureSupplier) { Random rnd = new Random(seed); GHUtility.buildRandomGraph(f.graph, rnd, 50, 2.2, true, true, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.8, pOffset); - GHUtility.addRandomTurnCosts(f.graph, seed, f.encodingManager, f.encoder, f.maxTurnCosts, f.turnCostStorage); -// GHUtility.printGraphForUnitTest(f.graph, f.encoder); + GHUtility.addRandomTurnCosts(f.graph, seed, f.encoder.getAccessEnc(), f.encoder.getTurnCostEnc(), f.maxTurnCosts, f.turnCostStorage); +// GHUtility.printGraphForUnitTest(f.graph, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc()); f.preProcessGraph(); LocationIndexTree index = new LocationIndexTree(f.graph, f.dir); index.prepareIndex(); diff --git a/core/src/test/java/com/graphhopper/routing/RoundTripRoutingTest.java b/core/src/test/java/com/graphhopper/routing/RoundTripRoutingTest.java index 3089c6e1c34..fae9951487a 100644 --- a/core/src/test/java/com/graphhopper/routing/RoundTripRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/RoundTripRoutingTest.java @@ -48,7 +48,7 @@ public class RoundTripRoutingTest { private final FlagEncoder carFE = FlagEncoders.createCar(); private final EncodingManager em = EncodingManager.create(carFE); - private final Weighting fastestWeighting = new FastestWeighting(carFE); + private final Weighting fastestWeighting = new FastestWeighting(carFE.getAccessEnc(), carFE.getAverageSpeedEnc()); // TODO private final TraversalMode tMode = TraversalMode.EDGE_BASED; private final TraversalMode tMode = TraversalMode.NODE_BASED; private final GHPoint ghPoint1 = new GHPoint(0, 0); diff --git a/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmTest.java b/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmTest.java index 9cb1bcfe23f..29936da675d 100644 --- a/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmTest.java +++ b/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmTest.java @@ -283,7 +283,7 @@ public void testBidirectionalLinear(Fixture f) { @ParameterizedTest @ArgumentsSource(FixtureProvider.class) public void testCalcFastestPath(Fixture f) { - FastestWeighting fastestWeighting = new FastestWeighting(f.carEncoder); + FastestWeighting fastestWeighting = new FastestWeighting(f.carAccessEnc, f.carSpeedEnc); BaseGraph graph = f.createGHStorage(false); initDirectedAndDiffSpeed(graph, f.carEncoder); @@ -883,7 +883,7 @@ public void testViaEdges_SpecialCases(Fixture f) { @ParameterizedTest @ArgumentsSource(FixtureProvider.class) public void testQueryGraphAndFastest(Fixture f) { - Weighting weighting = new FastestWeighting(f.carEncoder); + Weighting weighting = new FastestWeighting(f.carAccessEnc, f.carSpeedEnc); BaseGraph graph = f.createGHStorage(false); initDirectedAndDiffSpeed(graph, f.carEncoder); Path p = f.calcPath(graph, weighting, new GHPoint(0.002, 0.0005), new GHPoint(0.0017, 0.0031)); @@ -894,7 +894,7 @@ public void testQueryGraphAndFastest(Fixture f) { @ParameterizedTest @ArgumentsSource(FixtureProvider.class) public void testTwoWeightsPerEdge(Fixture f) { - FastestWeighting fastestWeighting = new FastestWeighting(f.bike2Encoder); + FastestWeighting fastestWeighting = new FastestWeighting(f.bike2Encoder.getAccessEnc(), f.bike2Encoder.getAverageSpeedEnc()); BaseGraph graph = f.createGHStorage(true); initEleGraph(graph, f.bike2Encoder, 18); // force the other path @@ -933,7 +933,7 @@ public void test0SpeedButUnblocked_Issue242(Fixture f) { public void testTwoWeightsPerEdge2(Fixture f) { // other direction should be different! Weighting fakeWeighting = new Weighting() { - private final Weighting tmpW = new FastestWeighting(f.carEncoder); + private final Weighting tmpW = new FastestWeighting(f.carAccessEnc, f.carSpeedEnc); @Override public double getMinWeight(double distance) { @@ -1014,7 +1014,7 @@ public String toString() { @ArgumentsSource(FixtureProvider.class) public void testRandomGraph(Fixture f) { // todo: use speed both directions - FastestWeighting fastestWeighting = new FastestWeighting(f.carEncoder); + FastestWeighting fastestWeighting = new FastestWeighting(f.carAccessEnc, f.carSpeedEnc); BaseGraph graph = f.createGHStorage(false); final long seed = System.nanoTime(); LOGGER.info("testRandomGraph - using seed: " + seed); @@ -1037,8 +1037,8 @@ public void testRandomGraph(Fixture f) { @ParameterizedTest @ArgumentsSource(FixtureProvider.class) public void testMultipleVehicles_issue548(Fixture f) { - FastestWeighting footWeighting = new FastestWeighting(f.footEncoder); - FastestWeighting carWeighting = new FastestWeighting(f.carEncoder); + FastestWeighting footWeighting = new FastestWeighting(f.footEncoder.getAccessEnc(), f.footEncoder.getAverageSpeedEnc()); + FastestWeighting carWeighting = new FastestWeighting(f.carAccessEnc, f.carSpeedEnc); BaseGraph graph = f.createGHStorage(false); initFootVsCar(f.carEncoder, f.footEncoder, graph); diff --git a/core/src/test/java/com/graphhopper/routing/RoutingCHGraphImplTest.java b/core/src/test/java/com/graphhopper/routing/RoutingCHGraphImplTest.java index 7713ce75ff1..7bb907efe1f 100644 --- a/core/src/test/java/com/graphhopper/routing/RoutingCHGraphImplTest.java +++ b/core/src/test/java/com/graphhopper/routing/RoutingCHGraphImplTest.java @@ -45,7 +45,7 @@ public void testBaseAndCHEdges() { graph.edge(8, 9); graph.freeze(); - CHConfig chConfig = CHConfig.nodeBased("p", new FastestWeighting(carEncoder)); + CHConfig chConfig = CHConfig.nodeBased("p", new FastestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc())); CHStorage store = CHStorage.fromGraph(graph, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(store); chBuilder.setIdentityLevels(); @@ -83,7 +83,7 @@ void testShortcutConnection() { GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(4, 1).setDistance(30)); graph.freeze(); - CHConfig chConfig = CHConfig.nodeBased("ch", new FastestWeighting(encoder)); + CHConfig chConfig = CHConfig.nodeBased("ch", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc())); CHStorage store = CHStorage.fromGraph(graph, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(store); chBuilder.setIdentityLevels(); @@ -120,7 +120,7 @@ public void testGetWeight() { EdgeIteratorState edge2 = graph.edge(1, 2); graph.freeze(); - CHConfig chConfig = CHConfig.nodeBased("ch", new FastestWeighting(encoder)); + CHConfig chConfig = CHConfig.nodeBased("ch", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc())); CHStorage store = CHStorage.fromGraph(graph, chConfig); RoutingCHGraph g = RoutingCHGraphImpl.fromGraph(graph, store, chConfig); assertFalse(g.getEdgeIteratorState(edge1.getEdge(), Integer.MIN_VALUE).isShortcut()); @@ -146,7 +146,7 @@ public void testGetWeightIfAdvancedEncoder() { ghStorage.edge(0, 3); ghStorage.freeze(); - FastestWeighting weighting = new FastestWeighting(customEncoder); + FastestWeighting weighting = new FastestWeighting(customEncoder.getAccessEnc(), customEncoder.getAverageSpeedEnc()); CHConfig chConfig = CHConfig.nodeBased("p1", weighting); CHStorage chStore = CHStorage.fromGraph(ghStorage, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(chStore); @@ -176,7 +176,7 @@ public void testWeightExact() { GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(1, 2).setDistance(1)); graph.freeze(); - CHConfig chConfig = CHConfig.nodeBased("ch", new FastestWeighting(encoder)); + CHConfig chConfig = CHConfig.nodeBased("ch", new FastestWeighting(accessEnc, speedEnc)); CHStorage store = CHStorage.fromGraph(graph, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(store); chBuilder.setIdentityLevels(); @@ -203,7 +203,7 @@ public void testSimpleShortcutCreationAndTraversal() { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(3, 4).setDistance(10)); graph.freeze(); - FastestWeighting weighting = new FastestWeighting(encoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased("p1", weighting); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(chStore); @@ -228,7 +228,7 @@ public void testAddShortcutSkippedEdgesWriteRead() { final EdgeIteratorState edge2 = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(3, 4).setDistance(10)); graph.freeze(); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased("p1", weighting); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(chStore); @@ -251,7 +251,7 @@ public void testSkippedEdges() { final EdgeIteratorState edge2 = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(3, 4).setDistance(10)); graph.freeze(); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased("p1", weighting); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(chStore); @@ -273,7 +273,7 @@ public void testAddShortcut_edgeBased_throwsIfNotConfiguredForEdgeBased() { GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(1, 2).setDistance(1)); graph.freeze(); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased("p1", weighting); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(chStore); @@ -292,7 +292,7 @@ public void testAddShortcut_edgeBased() { GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(1, 2).setDistance(3)); graph.freeze(); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.edgeBased("p1", weighting); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); @@ -309,7 +309,7 @@ public void outOfBounds() { EncodingManager em = EncodingManager.create(carEncoder); BaseGraph graph = new BaseGraph.Builder(em).set3D(true).create(); graph.freeze(); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); CHConfig chConfig = CHConfig.nodeBased("p1", weighting); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); RoutingCHGraph lg = RoutingCHGraphImpl.fromGraph(graph, chStore, chConfig); @@ -327,7 +327,7 @@ public void testGetEdgeIterator() { GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(1, 2).setDistance(1)); graph.freeze(); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.edgeBased("p1", weighting); CHStorage store = CHStorage.fromGraph(graph, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(store); diff --git a/core/src/test/java/com/graphhopper/routing/TrafficChangeWithNodeOrderingReusingTest.java b/core/src/test/java/com/graphhopper/routing/TrafficChangeWithNodeOrderingReusingTest.java index d0ea3eaf111..14c11af08b9 100644 --- a/core/src/test/java/com/graphhopper/routing/TrafficChangeWithNodeOrderingReusingTest.java +++ b/core/src/test/java/com/graphhopper/routing/TrafficChangeWithNodeOrderingReusingTest.java @@ -57,7 +57,7 @@ public Fixture(int maxDeviationPercentage) { carParser.init(new DateRangeParser()); osmParsers = new OSMParsers() .addVehicleTagParser(carParser); - baseCHConfig = CHConfig.nodeBased("base", new FastestWeighting(encoder)); + baseCHConfig = CHConfig.nodeBased("base", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc())); trafficCHConfig = CHConfig.nodeBased("traffic", new RandomDeviationWeighting(baseCHConfig.getWeighting(), encoder, maxDeviationPercentage)); graph = new BaseGraph.Builder(em).create(); } diff --git a/core/src/test/java/com/graphhopper/routing/ch/CHTurnCostTest.java b/core/src/test/java/com/graphhopper/routing/ch/CHTurnCostTest.java index a0975512b0a..425af6cbd0f 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/CHTurnCostTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/CHTurnCostTest.java @@ -1209,7 +1209,7 @@ private void compareWithDijkstraOnRandomGraph(long seed) { // for larger graphs preparation takes much longer the higher the degree is! GHUtility.buildRandomGraph(graph, rnd, 20, 3.0, true, true, accessEnc, speedEnc, null, 0.7, 0.9, 0.8); - GHUtility.addRandomTurnCosts(graph, seed, encodingManager, encoder, maxCost, turnCostStorage); + GHUtility.addRandomTurnCosts(graph, seed, accessEnc, turnCostEnc, maxCost, turnCostStorage); graph.freeze(); checkStrict = false; IntArrayList contractionOrder = getRandomIntegerSequence(graph.getNodes(), rnd); @@ -1237,7 +1237,7 @@ public void testFindPath_heuristic_compareWithDijkstra_finiteUTurnCost() { private void compareWithDijkstraOnRandomGraph_heuristic(long seed) { GHUtility.buildRandomGraph(graph, new Random(seed), 20, 3.0, true, true, accessEnc, speedEnc, null, 0.7, 0.9, 0.8); - GHUtility.addRandomTurnCosts(graph, seed, encodingManager, encoder, maxCost, turnCostStorage); + GHUtility.addRandomTurnCosts(graph, seed, accessEnc, turnCostEnc, maxCost, turnCostStorage); graph.freeze(); checkStrict = false; automaticCompareCHWithDijkstra(100); @@ -1351,7 +1351,7 @@ private void compareCHQueryWithDijkstra(int from, int to) { } if (algosDisagree) { System.out.println("Graph that produced error:"); - GHUtility.printGraphForUnitTest(graph, encoder); + GHUtility.printGraphForUnitTest(graph, accessEnc, speedEnc); fail("Dijkstra and CH did not find equal shortest paths for route from " + from + " to " + to + "\n" + " dijkstra: weight: " + dijkstraPath.getWeight() + ", distance: " + dijkstraPath.getDistance() + ", time: " + dijkstraPath.getTime() + ", nodes: " + dijkstraPath.calcNodes() + "\n" + diff --git a/core/src/test/java/com/graphhopper/routing/ch/NodeBasedNodeContractorTest.java b/core/src/test/java/com/graphhopper/routing/ch/NodeBasedNodeContractorTest.java index 58ff8b62df1..61b3ae7fe5c 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/NodeBasedNodeContractorTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/NodeBasedNodeContractorTest.java @@ -280,7 +280,7 @@ public void testNodeContraction_shortcutWeightRounding() { GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(2, 3).setDistance(distances[3])); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(3, 4).setDistance(distances[4])); graph.freeze(); - Weighting weighting = new FastestWeighting(encoder); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased("p1", weighting); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); setMaxLevelOnAllNodes(chStore); @@ -307,6 +307,8 @@ public void testNodeContraction_preventUnnecessaryShortcutWithLoop() { // see also #1583 FlagEncoder encoder = FlagEncoders.createCar(); EncodingManager encodingManager = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = encoder.getAccessEnc(); + DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); // 0 - 1 - 2 - 3 // o o @@ -317,7 +319,7 @@ public void testNodeContraction_preventUnnecessaryShortcutWithLoop() { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(3, 3).setDistance(1)); graph.freeze(); - Weighting weighting = new FastestWeighting(encoder); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased("p1", weighting); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); setMaxLevelOnAllNodes(chStore); diff --git a/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java b/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java index 997af933d0d..e05540acff0 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java @@ -331,8 +331,10 @@ public void testStallOnDemandViaVirtuaNode_issue1574() { // here we will construct a special case where a connection is not found without the fix in #1574. g = createGraph(); + BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); + DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); // use fastest weighting in this test to be able to fine-tune some weights via the speed (see below) - Weighting fastestWeighting = new FastestWeighting(carEncoder); + Weighting fastestWeighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased("c", fastestWeighting); // the following graph reproduces the issue. note that we will use the node ids as ch levels, so there will // be a shortcut 3->2 visible at node 2 and another one 3->4 visible at node 3. @@ -344,8 +346,6 @@ public void testStallOnDemandViaVirtuaNode_issue1574() { // start 0 - 3 - x - 1 - 2 // \ | // sc ---- 4 - 5 - 6 - 7 finish - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 3).setDistance(1)); EdgeIteratorState edge31 = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(3, 1).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(1)); @@ -509,8 +509,8 @@ public void testMultiplePreparationsDifferentView() { FlagEncoder tmpBikeEncoder = FlagEncoders.createBike(); EncodingManager tmpEncodingManager = EncodingManager.create(tmpCarEncoder, tmpBikeEncoder); - CHConfig carConfig = CHConfig.nodeBased("c1", new FastestWeighting(tmpCarEncoder)); - CHConfig bikeConfig = CHConfig.nodeBased("c2", new FastestWeighting(tmpBikeEncoder)); + CHConfig carConfig = CHConfig.nodeBased("c1", new FastestWeighting(tmpCarEncoder.getAccessEnc(), tmpCarEncoder.getAverageSpeedEnc())); + CHConfig bikeConfig = CHConfig.nodeBased("c2", new FastestWeighting(tmpBikeEncoder.getAccessEnc(), tmpBikeEncoder.getAverageSpeedEnc())); BaseGraph graph = new BaseGraph.Builder(tmpEncodingManager).create(); initShortcutsGraph(graph, tmpCarEncoder); @@ -534,8 +534,8 @@ public void testReusingNodeOrdering() { FlagEncoder car1FlagEncoder = FlagEncoders.createCar(new PMap("name=car1|turn_costs=true|speed_two_directions=true")); FlagEncoder car2FlagEncoder = FlagEncoders.createCar(new PMap("name=car2|turn_costs=true|speed_two_directions=true")); EncodingManager em = EncodingManager.create(car1FlagEncoder, car2FlagEncoder); - CHConfig car1Config = CHConfig.nodeBased("c1", new FastestWeighting(car1FlagEncoder)); - CHConfig car2Config = CHConfig.nodeBased("c2", new FastestWeighting(car2FlagEncoder)); + CHConfig car1Config = CHConfig.nodeBased("c1", new FastestWeighting(car1FlagEncoder.getAccessEnc(), car1FlagEncoder.getAverageSpeedEnc())); + CHConfig car2Config = CHConfig.nodeBased("c2", new FastestWeighting(car2FlagEncoder.getAccessEnc(), car2FlagEncoder.getAverageSpeedEnc())); BaseGraph graph = new BaseGraph.Builder(em).create(); int numNodes = 5_000; diff --git a/core/src/test/java/com/graphhopper/routing/lm/LMApproximatorTest.java b/core/src/test/java/com/graphhopper/routing/lm/LMApproximatorTest.java index c5686aede95..adf8aa9f77c 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/LMApproximatorTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/LMApproximatorTest.java @@ -53,7 +53,7 @@ private void run(long seed) { GHUtility.buildRandomGraph(graph, rnd, 100, 2.2, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), null, 0.7, 0.8, 0.8); - Weighting weighting = new FastestWeighting(encoder); + Weighting weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); PrepareLandmarks lm = new PrepareLandmarks(dir, graph, encodingManager, new LMConfig("car", weighting), 16); lm.setMaximumWeight(10000); diff --git a/core/src/test/java/com/graphhopper/routing/lm/LMIssueTest.java b/core/src/test/java/com/graphhopper/routing/lm/LMIssueTest.java index b32f8ce3c75..6734b7e61d7 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/LMIssueTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/LMIssueTest.java @@ -71,7 +71,7 @@ public void init() { graph = new BaseGraph.Builder(encodingManager) .setDir(dir) .create(); - weighting = new FastestWeighting(encoder); + weighting = new FastestWeighting(accessEnc, speedEnc); } private void preProcessGraph() { diff --git a/core/src/test/java/com/graphhopper/routing/lm/LMPreparationHandlerTest.java b/core/src/test/java/com/graphhopper/routing/lm/LMPreparationHandlerTest.java index c8968e5b3c2..8fe2d7e389e 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/LMPreparationHandlerTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/LMPreparationHandlerTest.java @@ -38,7 +38,7 @@ public void maximumLMWeight() { FlagEncoder car = FlagEncoders.createCar(); EncodingManager em = EncodingManager.create(car); List lmConfigs = Arrays.asList( - new LMConfig("conf1", new FastestWeighting(car)), + new LMConfig("conf1", new FastestWeighting(car.getAccessEnc(), car.getAverageSpeedEnc())), new LMConfig("conf2", new ShortestWeighting(car.getAccessEnc(), car.getAverageSpeedEnc())) ); List preparations = handler.createPreparations(lmConfigs, new BaseGraph.Builder(em).build(), em, null); diff --git a/core/src/test/java/com/graphhopper/routing/lm/LandmarkStorageTest.java b/core/src/test/java/com/graphhopper/routing/lm/LandmarkStorageTest.java index 389df1ebb66..f07a0c5e64f 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/LandmarkStorageTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/LandmarkStorageTest.java @@ -75,7 +75,7 @@ public void tearDown() { public void testInfiniteWeight() { Directory dir = new RAMDirectory(); EdgeIteratorState edge = graph.edge(0, 1); - int res = new LandmarkStorage(graph, encodingManager, dir, new LMConfig("c1", new FastestWeighting(encoder) { + int res = new LandmarkStorage(graph, encodingManager, dir, new LMConfig("c1", new FastestWeighting(accessEnc, speedEnc) { @Override public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) { return Integer.MAX_VALUE * 2L; @@ -84,7 +84,7 @@ public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) { assertEquals(Integer.MAX_VALUE, res); dir = new RAMDirectory(); - res = new LandmarkStorage(graph, encodingManager, dir, new LMConfig("c2", new FastestWeighting(encoder) { + res = new LandmarkStorage(graph, encodingManager, dir, new LMConfig("c2", new FastestWeighting(accessEnc, speedEnc) { @Override public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) { return Double.POSITIVE_INFINITY; @@ -97,7 +97,7 @@ public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) { public void testSetGetWeight() { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(40.1)); Directory dir = new RAMDirectory(); - LandmarkStorage lms = new LandmarkStorage(graph, encodingManager, dir, new LMConfig("c1", new FastestWeighting(encoder)), 4). + LandmarkStorage lms = new LandmarkStorage(graph, encodingManager, dir, new LMConfig("c1", new FastestWeighting(accessEnc, speedEnc)), 4). setMaximumWeight(LandmarkStorage.PRECISION); lms._getInternalDA().create(2000); // 2^16=65536, use -1 for infinity and -2 for maximum @@ -128,7 +128,7 @@ public void testWithSubnetworks() { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(4, 5).setDistance(10.5)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(5, 6).setDistance(10.6)); - Weighting weighting = new FastestWeighting(encoder); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc); // 1 means => 2 allowed edge keys => excludes the node 6 subnetworkRemoval(weighting, 1); @@ -150,7 +150,7 @@ public void testWithStronglyConnectedComponent() { GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(3, 2).setDistance(10.2)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(3, 4).setDistance(10.4)); - Weighting weighting = new FastestWeighting(encoder); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc); // 3 nodes => 6 allowed edge keys but still do not exclude 3 & 4 as strongly connected and not a too small subnetwork! subnetworkRemoval(weighting, 4); @@ -181,7 +181,7 @@ public void testWithOnewaySubnetworks() { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(4, 5).setDistance(10.5)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(5, 2).setDistance(10.2)); - Weighting weighting = new FastestWeighting(encoder); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc); // 1 allowed node => 2 allowed edge keys (exclude 2 and 3 because they are separate too small oneway subnetworks) subnetworkRemoval(weighting, 1); @@ -201,7 +201,7 @@ public void testWeightingConsistence1() { GHUtility.setSpeed(30, true, true, accessEnc, speedEnc, graph.edge(1, 2).setDistance(10)); graph.edge(2, 3).setDistance(10.1).set(accessEnc, true, true); - LandmarkStorage storage = new LandmarkStorage(graph, encodingManager, new RAMDirectory(), new LMConfig("car", new FastestWeighting(encoder)), 2); + LandmarkStorage storage = new LandmarkStorage(graph, encodingManager, new RAMDirectory(), new LMConfig("car", new FastestWeighting(accessEnc, speedEnc)), 2); storage.setMinimumNodes(2); storage.createLandmarks(); @@ -215,7 +215,7 @@ public void testWeightingConsistence2() { graph.edge(2, 3).setDistance(10.1).set(accessEnc, true, true); GHUtility.setSpeed(30, true, true, accessEnc, speedEnc, graph.edge(2, 3).setDistance(10)); - LandmarkStorage storage = new LandmarkStorage(graph, encodingManager, new RAMDirectory(), new LMConfig("car", new FastestWeighting(encoder)), 2); + LandmarkStorage storage = new LandmarkStorage(graph, encodingManager, new RAMDirectory(), new LMConfig("car", new FastestWeighting(accessEnc, speedEnc)), 2); storage.setMinimumNodes(2); storage.createLandmarks(); @@ -228,7 +228,7 @@ public void testWeightingConsistence2() { public void testWithBorderBlocking() { RoutingAlgorithmTest.initBiGraph(graph, encoder); - LandmarkStorage storage = new LandmarkStorage(graph, encodingManager, new RAMDirectory(), new LMConfig("car", new FastestWeighting(encoder)), 2); + LandmarkStorage storage = new LandmarkStorage(graph, encodingManager, new RAMDirectory(), new LMConfig("car", new FastestWeighting(accessEnc, speedEnc)), 2); final SplitArea right = new SplitArea(emptyList()); final SplitArea left = new SplitArea(emptyList()); final AreaIndex areaIndex = new AreaIndex(emptyList()) { diff --git a/core/src/test/java/com/graphhopper/routing/lm/PrepareLandmarksTest.java b/core/src/test/java/com/graphhopper/routing/lm/PrepareLandmarksTest.java index eb4f63d4f39..3640461d841 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/PrepareLandmarksTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/PrepareLandmarksTest.java @@ -100,7 +100,7 @@ public void testLandmarkStorageAndRouting() { index.prepareIndex(); int lm = 5, activeLM = 2; - Weighting weighting = new FastestWeighting(encoder); + Weighting weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); LMConfig lmConfig = new LMConfig("car", weighting); LandmarkStorage store = new LandmarkStorage(graph, encodingManager, dir, lmConfig, lm); store.setMinimumNodes(2); @@ -190,7 +190,7 @@ public void testStoreAndLoad() { Helper.removeDir(new File(fileStr)); Directory dir = new RAMDirectory(fileStr, true).create(); - Weighting weighting = new FastestWeighting(encoder); + Weighting weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); LMConfig lmConfig = new LMConfig("car", weighting); PrepareLandmarks plm = new PrepareLandmarks(dir, graph, encodingManager, lmConfig, 2); plm.setMinimumNodes(2); diff --git a/core/src/test/java/com/graphhopper/routing/querygraph/QueryGraphTest.java b/core/src/test/java/com/graphhopper/routing/querygraph/QueryGraphTest.java index 5cec19ae916..edf794e9ebe 100644 --- a/core/src/test/java/com/graphhopper/routing/querygraph/QueryGraphTest.java +++ b/core/src/test/java/com/graphhopper/routing/querygraph/QueryGraphTest.java @@ -512,7 +512,7 @@ public void testTurnCostsProperlyPropagated_Issue282() { EdgeIteratorState edge0 = GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graphWithTurnCosts.edge(0, 1).setDistance(10)); EdgeIteratorState edge1 = GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graphWithTurnCosts.edge(2, 1).setDistance(10)); - Weighting weighting = new FastestWeighting(encoder, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graphWithTurnCosts.getTurnCostStorage())); + Weighting weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graphWithTurnCosts.getTurnCostStorage())); // no turn costs initially assertEquals(0, weighting.calcTurnWeight(edge0.getEdge(), 1, edge1.getEdge()), .1); diff --git a/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java b/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java index 16e1a19feb5..046db6abe66 100644 --- a/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java +++ b/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java @@ -273,7 +273,7 @@ private static EncodingManager createEncodingManager(String flagEncodersStr) { private static PrepareRoutingSubnetworks.PrepareJob createJob(EncodingManager em, FlagEncoder encoder, TurnCostProvider turnCostProvider) { return new PrepareRoutingSubnetworks.PrepareJob(em.getBooleanEncodedValue(Subnetwork.key(encoder.toString())), - new FastestWeighting(encoder, turnCostProvider)); + new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), turnCostProvider)); } } diff --git a/core/src/test/java/com/graphhopper/routing/weighting/BlockAreaWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/BlockAreaWeightingTest.java index 410f129e1ba..69e9ae13a75 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/BlockAreaWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/BlockAreaWeightingTest.java @@ -1,6 +1,8 @@ package com.graphhopper.routing.weighting; import com.graphhopper.coll.GHIntHashSet; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.routing.util.EncodingManager; @@ -25,15 +27,19 @@ public class BlockAreaWeightingTest { private FlagEncoder encoder = FlagEncoders.createCar(); private EncodingManager em; + private BooleanEncodedValue accessEnc; + private DecimalEncodedValue speedEnc; private BaseGraph graph; @BeforeEach public void setUp() { encoder = FlagEncoders.createCar(); em = EncodingManager.create(encoder); + accessEnc = encoder.getAccessEnc(); + speedEnc = encoder.getAverageSpeedEnc(); graph = new BaseGraph.Builder(em).create(); // 0-1 - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(0, 1).setDistance(1)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1)); updateDistancesFor(graph, 0, 0.00, 0.00); updateDistancesFor(graph, 1, 0.01, 0.01); } @@ -42,13 +48,13 @@ public void setUp() { public void testBlockedById() { GraphEdgeIdFinder.BlockArea bArea = new GraphEdgeIdFinder.BlockArea(graph); EdgeIteratorState edge = graph.getEdgeIteratorState(0, 1); - BlockAreaWeighting instance = new BlockAreaWeighting(new FastestWeighting(encoder), bArea); + BlockAreaWeighting instance = new BlockAreaWeighting(new FastestWeighting(accessEnc, speedEnc), bArea); assertEquals(94.35, instance.calcEdgeWeight(edge, false), .01); GHIntHashSet set = new GHIntHashSet(); set.add(0); bArea.add(null, set); - instance = new BlockAreaWeighting(new FastestWeighting(encoder), bArea); + instance = new BlockAreaWeighting(new FastestWeighting(accessEnc, speedEnc), bArea); assertEquals(Double.POSITIVE_INFINITY, instance.calcEdgeWeight(edge, false), .01); } @@ -56,14 +62,14 @@ public void testBlockedById() { public void testBlockedByShape() { EdgeIteratorState edge = graph.getEdgeIteratorState(0, 1); GraphEdgeIdFinder.BlockArea bArea = new GraphEdgeIdFinder.BlockArea(graph); - BlockAreaWeighting instance = new BlockAreaWeighting(new FastestWeighting(encoder), bArea); + BlockAreaWeighting instance = new BlockAreaWeighting(new FastestWeighting(accessEnc, speedEnc), bArea); assertEquals(94.35, instance.calcEdgeWeight(edge, false), 0.01); bArea.add(new Circle(0.01, 0.01, 100)); assertEquals(Double.POSITIVE_INFINITY, instance.calcEdgeWeight(edge, false), .01); bArea = new GraphEdgeIdFinder.BlockArea(graph); - instance = new BlockAreaWeighting(new FastestWeighting(encoder), bArea); + instance = new BlockAreaWeighting(new FastestWeighting(accessEnc, speedEnc), bArea); // Do not match 1,1 of edge bArea.add(new Circle(0.1, 0.1, 100)); assertEquals(94.35, instance.calcEdgeWeight(edge, false), .01); @@ -81,7 +87,7 @@ public void testBlockVirtualEdges_QueryGraph() { Snap snap = index.findClosest(0.005, 0.005, EdgeFilter.ALL_EDGES); QueryGraph queryGraph = QueryGraph.create(graph, snap); - BlockAreaWeighting instance = new BlockAreaWeighting(new FastestWeighting(encoder), bArea); + BlockAreaWeighting instance = new BlockAreaWeighting(new FastestWeighting(accessEnc, speedEnc), bArea); EdgeIterator iter = queryGraph.createEdgeExplorer().setBaseNode(snap.getClosestNode()); int blockedEdges = 0, totalEdges = 0; while (iter.next()) { diff --git a/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java index 625784a0732..6b928a7d610 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java @@ -45,17 +45,17 @@ public class FastestWeightingTest { @Test public void testMinWeightHasSameUnitAs_getWeight() { - Weighting instance = new FastestWeighting(encoder); - IntsRef flags = GHUtility.setSpeed(encoder.getMaxSpeed(), 0, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), encodingManager.createEdgeFlags()); + Weighting instance = new FastestWeighting(accessEnc, speedEnc); + IntsRef flags = GHUtility.setSpeed(encoder.getMaxSpeed(), 0, accessEnc, speedEnc, encodingManager.createEdgeFlags()); assertEquals(instance.getMinWeight(10), instance.calcEdgeWeight(createMockedEdgeIteratorState(10, flags), false), 1e-8); } @Test public void testWeightWrongHeading() { - Weighting instance = new FastestWeighting(encoder, new PMap().putObject(Parameters.Routing.HEADING_PENALTY, 100)); + Weighting instance = new FastestWeighting(accessEnc, speedEnc, null, new PMap().putObject(Parameters.Routing.HEADING_PENALTY, 100), TurnCostProvider.NO_TURN_COST_PROVIDER); VirtualEdgeIteratorState virtEdge = new VirtualEdgeIteratorState(0, GHUtility.createEdgeKey(1, false, false), 1, 2, 10, - GHUtility.setSpeed(10, 0, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), encodingManager.createEdgeFlags()), "test", Helper.createPointList(51, 0, 51, 1), false); + GHUtility.setSpeed(10, 0, accessEnc, speedEnc, encodingManager.createEdgeFlags()), "test", Helper.createPointList(51, 0, 51, 1), false); double time = instance.calcEdgeWeight(virtEdge, false); virtEdge.setUnfavored(true); @@ -70,13 +70,13 @@ public void testWeightWrongHeading() { // test default penalty virtEdge.setUnfavored(true); - instance = new FastestWeighting(encoder); + instance = new FastestWeighting(accessEnc, speedEnc); assertEquals(time + Routing.DEFAULT_HEADING_PENALTY, instance.calcEdgeWeight(virtEdge, false), 1e-8); } @Test public void testSpeed0() { - Weighting instance = new FastestWeighting(encoder); + Weighting instance = new FastestWeighting(accessEnc, speedEnc); IntsRef edgeFlags = encodingManager.createEdgeFlags(); encoder.getAverageSpeedEnc().setDecimal(false, edgeFlags, 0); assertEquals(1.0 / 0, instance.calcEdgeWeight(createMockedEdgeIteratorState(10, edgeFlags), false), 1e-8); @@ -90,7 +90,7 @@ public void testTime() { FlagEncoder tmpEnc = FlagEncoders.createBike2(); EncodingManager em = EncodingManager.create(tmpEnc); BaseGraph g = new BaseGraph.Builder(em).create(); - Weighting w = new FastestWeighting(tmpEnc); + Weighting w = new FastestWeighting(tmpEnc.getAccessEnc(), tmpEnc.getAverageSpeedEnc()); IntsRef edgeFlags = GHUtility.setSpeed(15, 15, tmpEnc.getAccessEnc(), tmpEnc.getAverageSpeedEnc(), em.createEdgeFlags()); tmpEnc.getAverageSpeedEnc().setDecimal(true, edgeFlags, 10.0); @@ -106,8 +106,8 @@ public void testTime() { @Test public void calcWeightAndTime_withTurnCosts() { BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); - Weighting weighting = new FastestWeighting(encoder, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage())); - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(0, 1).setDistance(100)); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage())); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(100)); EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(1, 2).setDistance(100)); // turn costs are given in seconds setTurnCost(graph, 0, 1, 2, 5); @@ -118,8 +118,8 @@ public void calcWeightAndTime_withTurnCosts() { @Test public void calcWeightAndTime_uTurnCosts() { BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); - Weighting weighting = new FastestWeighting(encoder, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage(), 40)); - EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(0, 1).setDistance(100)); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage(), 40)); + EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(100)); assertEquals(6 + 40, GHUtility.calcWeightWithTurnWeight(weighting, edge, false, 0), 1.e-6); assertEquals((6 + 40) * 1000, GHUtility.calcMillisWithTurnMillis(weighting, edge, false, 0), 1.e-6); } @@ -153,8 +153,8 @@ public void testDestinationTag() { edge.set(bikeEncoder.getAverageSpeedEnc(), 18); EnumEncodedValue roadAccessEnc = em.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class); - FastestWeighting weighting = new FastestWeighting(carEncoder); - FastestWeighting bikeWeighting = new FastestWeighting(bikeEncoder); + FastestWeighting weighting = new FastestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); + FastestWeighting bikeWeighting = new FastestWeighting(bikeEncoder.getAccessEnc(), bikeEncoder.getAverageSpeedEnc()); edge.set(roadAccessEnc, RoadAccess.YES); assertEquals(60, weighting.calcEdgeWeight(edge, false), 1.e-6); @@ -181,8 +181,8 @@ public void testPrivateTag() { edge.set(bikeEncoder.getAverageSpeedEnc(), 18); EnumEncodedValue roadAccessEnc = em.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class); - FastestWeighting weighting = new FastestWeighting(carEncoder); - FastestWeighting bikeWeighting = new FastestWeighting(bikeEncoder); + FastestWeighting weighting = new FastestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); + FastestWeighting bikeWeighting = new FastestWeighting(bikeEncoder.getAccessEnc(), bikeEncoder.getAverageSpeedEnc()); ReaderWay way = new ReaderWay(1); way.setTag("highway", "secondary"); diff --git a/core/src/test/java/com/graphhopper/routing/weighting/ShortFastestWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/ShortFastestWeightingTest.java index 56cd3d69493..a94b8c51bd6 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/ShortFastestWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/ShortFastestWeightingTest.java @@ -38,18 +38,18 @@ public class ShortFastestWeightingTest { @Test public void testShort() { EdgeIteratorState edge = createMockedEdgeIteratorState(10, GHUtility.setSpeed(50, 0, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), encodingManager.createEdgeFlags())); - Weighting instance = new ShortFastestWeighting(encoder, 0.03); - assertEquals(1.02, instance.calcEdgeWeight(edge, false), 1e-8); + Weighting instance = new ShortFastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), null, new PMap("short_fastest.distance_factor=0.03"), TurnCostProvider.NO_TURN_COST_PROVIDER); + assertEquals(1.02, instance.calcEdgeWeight(edge, false), 1e-6); // more influence from distance - instance = new ShortFastestWeighting(encoder, 0.1); - assertEquals(1.72, instance.calcEdgeWeight(edge, false), 1e-8); + instance = new ShortFastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), null, new PMap("short_fastest.distance_factor=0.1"), TurnCostProvider.NO_TURN_COST_PROVIDER); + assertEquals(1.72, instance.calcEdgeWeight(edge, false), 1e-6); } @Test public void testTooSmall() { try { - new ShortFastestWeighting(encoder, new PMap("short_fastest.distance_factor=0|short_fastest.time_factor=0"), + new ShortFastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), null, new PMap("short_fastest.distance_factor=0|short_fastest.time_factor=0"), TurnCostProvider.NO_TURN_COST_PROVIDER); fail(); } catch (Exception ex) { diff --git a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java index 87e960a943e..83f619c6561 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java @@ -65,9 +65,9 @@ public void withPriority() { set(roadClassEnc, SECONDARY); // without priority costs fastest weighting is the same as custom weighting - assertEquals(144, new FastestWeighting(carFE, NO_TURN_COST_PROVIDER).calcEdgeWeight(slow, false), .1); - assertEquals(72, new FastestWeighting(carFE, NO_TURN_COST_PROVIDER).calcEdgeWeight(medium, false), .1); - assertEquals(36, new FastestWeighting(carFE, NO_TURN_COST_PROVIDER).calcEdgeWeight(fast, false), .1); + assertEquals(144, new FastestWeighting(accessEnc, avSpeedEnc, NO_TURN_COST_PROVIDER).calcEdgeWeight(slow, false), .1); + assertEquals(72, new FastestWeighting(accessEnc, avSpeedEnc, NO_TURN_COST_PROVIDER).calcEdgeWeight(medium, false), .1); + assertEquals(36, new FastestWeighting(accessEnc, avSpeedEnc, NO_TURN_COST_PROVIDER).calcEdgeWeight(fast, false), .1); CustomModel model = new CustomModel().setDistanceInfluence(0); assertEquals(144, createWeighting(model).calcEdgeWeight(slow, false), .1); diff --git a/core/src/test/java/com/graphhopper/storage/ShortcutUnpackerTest.java b/core/src/test/java/com/graphhopper/storage/ShortcutUnpackerTest.java index 6add1ee811c..9ce8c83c1a8 100644 --- a/core/src/test/java/com/graphhopper/storage/ShortcutUnpackerTest.java +++ b/core/src/test/java/com/graphhopper/storage/ShortcutUnpackerTest.java @@ -54,7 +54,7 @@ public String toString() { private void freeze() { graph.freeze(); TurnCostProvider turnCostProvider = edgeBased ? new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage()) : NO_TURN_COST_PROVIDER; - CHConfig chConfig = new CHConfig("profile", new FastestWeighting(encoder, turnCostProvider), edgeBased); + CHConfig chConfig = new CHConfig("profile", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), turnCostProvider), edgeBased); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); chBuilder = new CHStorageBuilder(chStore); routingCHGraph = RoutingCHGraphImpl.fromGraph(graph, chStore, chConfig); diff --git a/core/src/test/java/com/graphhopper/util/InstructionListTest.java b/core/src/test/java/com/graphhopper/util/InstructionListTest.java index 9e4be7e6d14..ae1fe1deb75 100644 --- a/core/src/test/java/com/graphhopper/util/InstructionListTest.java +++ b/core/src/test/java/com/graphhopper/util/InstructionListTest.java @@ -132,7 +132,7 @@ Graph createTestGraph() { public void testWayList() { Graph g = createTestGraph(); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); Path p = new Dijkstra(g, weighting, TraversalMode.NODE_BASED).calcPath(0, 10); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR); List tmpList = getTurnDescriptions(wayList); @@ -201,7 +201,7 @@ public void testWayList2() { list.add(10.20, 10.05); iter.setWayGeometry(list); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); Path p = new Dijkstra(g, weighting, tMode).calcPath(2, 3); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR); @@ -240,7 +240,7 @@ public void testNoInstructionIfSameStreet() { list.add(10.20, 10.05); iter.setWayGeometry(list); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); Path p = new Dijkstra(g, weighting, tMode).calcPath(2, 3); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR); List tmpList = getTurnDescriptions(wayList); @@ -269,7 +269,7 @@ public void testNoInstructionIfSlightTurnAndAlternativeIsSharp() { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(2, 3).setDistance(10)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(2, 4).setDistance(10)); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); Path p = new Dijkstra(g, weighting, tMode).calcPath(1, 3); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR); List tmpList = getTurnDescriptions(wayList); @@ -298,7 +298,7 @@ public void testNoInstructionIfSlightTurnAndAlternativeIsSharp2() { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(2, 3).setDistance(10)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(2, 4).setDistance(10)); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); Path p = new Dijkstra(g, weighting, tMode).calcPath(1, 3); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR); List tmpList = getTurnDescriptions(wayList); @@ -334,7 +334,7 @@ public void testNoInstructionIfSlightTurnAndAlternativeIsSharp3() { g.edge(2, 3).set(rcEV, RoadClass.RESIDENTIAL).setName("pfarr"); g.edge(2, 4).set(rcEV, RoadClass.PEDESTRIAN).setName("markt"); - FastestWeighting weighting = new FastestWeighting(bike); + FastestWeighting weighting = new FastestWeighting(bike.getAccessEnc(), bike.getAverageSpeedEnc()); Path p = new Dijkstra(g, weighting, tMode).calcPath(1, 3); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, tmpEM, usTR); List tmpList = getTurnDescriptions(wayList); @@ -372,7 +372,7 @@ public void testInstructionIfTurn() { g.edge(2, 3).set(rcEV, RoadClass.SECONDARY).setName("ring"); g.edge(2, 4).set(rcEV, RoadClass.SECONDARY).setName("ring"); - FastestWeighting weighting = new FastestWeighting(bike); + FastestWeighting weighting = new FastestWeighting(bike.getAccessEnc(), bike.getAverageSpeedEnc()); Path p = new Dijkstra(g, weighting, tMode).calcPath(1, 4); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, tmpEM, usTR); List tmpList = getTurnDescriptions(wayList); @@ -469,7 +469,7 @@ public void testInstructionWithHighlyCustomProfileWithRoadsBase() { public void testEmptyList() { BaseGraph g = new BaseGraph.Builder(carManager).create(); g.getNodeAccess().setNode(1, 0, 0); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); Path p = new Dijkstra(g, weighting, tMode).calcPath(0, 1); InstructionList il = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR); assertEquals(0, il.size()); @@ -501,7 +501,7 @@ public void testFind() { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(3, 7).setDistance(10000)).setName("3-7"); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(4, 5).setDistance(10000)).setName("4-5"); - FastestWeighting weighting = new FastestWeighting(carEncoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); Path p = new Dijkstra(g, weighting, tMode).calcPath(1, 5); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR); From cceb167ed0f1b004ea0babbae1e395f542bd7c38 Mon Sep 17 00:00:00 2001 From: easbar Date: Wed, 22 Jun 2022 18:45:36 +0200 Subject: [PATCH 02/28] Start removing flag encoders in tests --- .../routing/DefaultWeightingFactory.java | 3 +- .../weighting/custom/CustomModelParser.java | 13 +- .../dem/BridgeElevationInterpolatorTest.java | 4 +- .../dem/EdgeElevationInterpolatorTest.java | 15 +-- .../dem/TunnelElevationInterpolatorTest.java | 2 +- .../routing/AStarBidirectionTest.java | 13 +- .../routing/AlternativeRouteCHTest.java | 15 ++- .../routing/AlternativeRouteEdgeCHTest.java | 28 ++--- .../routing/AlternativeRouteTest.java | 34 +++--- .../routing/CHQueryWithTurnCostsTest.java | 22 ++-- .../DefaultBidirPathExtractorTest.java | 20 ++-- .../routing/DijkstraBidirectionCHTest.java | 76 ++++++------ .../routing/DijkstraOneToManyTest.java | 31 +++-- .../DirectedBidirectionalDijkstraTest.java | 21 ++-- .../routing/DirectedRoutingTest.java | 34 +++--- .../DirectionResolverOnQueryGraphTest.java | 21 ++-- .../routing/DirectionResolverTest.java | 21 ++-- .../EdgeBasedRoutingAlgorithmTest.java | 26 ++-- .../routing/PriorityRoutingTest.java | 7 +- .../routing/RoundTripRoutingTest.java | 2 +- .../routing/RoutingAlgorithmTest.java | 111 ++++++++---------- .../routing/ch/CHTurnCostTest.java | 22 ++-- .../ch/EdgeBasedNodeContractorTest.java | 20 ++-- .../routing/ev/DecimalEncodedValueTest.java | 37 ++---- .../routing/lm/LandmarkStorageTest.java | 2 +- .../routing/util/AccessFilterTest.java | 13 +- .../routing/util/CarTagParserTest.java | 6 + .../weighting/BlockAreaWeightingTest.java | 13 +- .../weighting/FastestWeightingTest.java | 28 ++--- .../custom/CustomModelParserTest.java | 53 ++++----- .../weighting/custom/CustomWeightingTest.java | 31 ++--- .../com/graphhopper/util/GHUtilityTest.java | 6 +- .../graphhopper/util/InstructionListTest.java | 7 +- 33 files changed, 357 insertions(+), 400 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java index d359484b542..103b8926e39 100644 --- a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java +++ b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java @@ -78,7 +78,8 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis CustomModel queryCustomModel = requestHints.getObject(CustomModel.KEY, null); CustomProfile customProfile = (CustomProfile) profile; queryCustomModel = CustomModel.merge(customProfile.getCustomModel(), queryCustomModel); - weighting = CustomModelParser.createWeighting(encoder, encodingManager, turnCostProvider, queryCustomModel); + weighting = CustomModelParser.createWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + encoder.getPriorityEnc(), encoder.getMaxSpeed(), encodingManager, turnCostProvider, queryCustomModel); } else if ("shortest".equalsIgnoreCase(weightingStr)) { weighting = new ShortestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), turnCostProvider); } else if ("fastest".equalsIgnoreCase(weightingStr)) { diff --git a/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomModelParser.java b/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomModelParser.java index 82c64f28ab8..0acaf447c88 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomModelParser.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomModelParser.java @@ -20,7 +20,6 @@ import com.graphhopper.json.Statement; import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.weighting.TurnCostProvider; import com.graphhopper.util.CustomModel; import com.graphhopper.util.EdgeIteratorState; @@ -70,17 +69,13 @@ private CustomModelParser() { // utility class } - public static CustomWeighting createWeighting(FlagEncoder baseFlagEncoder, EncodedValueLookup lookup, + public static CustomWeighting createWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, DecimalEncodedValue priorityEnc, + double maxSpeed, EncodedValueLookup lookup, TurnCostProvider turnCostProvider, CustomModel customModel) { if (customModel == null) throw new IllegalStateException("CustomModel cannot be null"); - DecimalEncodedValue avgSpeedEnc = lookup.getDecimalEncodedValue(EncodingManager.getKey(baseFlagEncoder.toString(), "average_speed")); - final String pKey = EncodingManager.getKey(baseFlagEncoder.toString(), "priority"); - DecimalEncodedValue priorityEnc = lookup.hasEncodedValue(pKey) ? lookup.getDecimalEncodedValue(pKey) : null; - - CustomWeighting.Parameters parameters = createWeightingParameters(customModel, lookup, - avgSpeedEnc, baseFlagEncoder.getMaxSpeed(), priorityEnc); - return new CustomWeighting(baseFlagEncoder.getAccessEnc(), baseFlagEncoder.getAverageSpeedEnc(), turnCostProvider, parameters); + CustomWeighting.Parameters parameters = createWeightingParameters(customModel, lookup, speedEnc, maxSpeed, priorityEnc); + return new CustomWeighting(accessEnc, speedEnc, turnCostProvider, parameters); } /** diff --git a/core/src/test/java/com/graphhopper/reader/dem/BridgeElevationInterpolatorTest.java b/core/src/test/java/com/graphhopper/reader/dem/BridgeElevationInterpolatorTest.java index 10259d5231b..ef21e3d019c 100644 --- a/core/src/test/java/com/graphhopper/reader/dem/BridgeElevationInterpolatorTest.java +++ b/core/src/test/java/com/graphhopper/reader/dem/BridgeElevationInterpolatorTest.java @@ -19,7 +19,6 @@ import com.graphhopper.coll.GHIntHashSet; import com.graphhopper.routing.ev.RoadEnvironment; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.storage.IntsRef; import com.graphhopper.storage.NodeAccess; import com.graphhopper.util.*; @@ -71,9 +70,8 @@ public void interpolatesElevationOfPillarNodes() { na.setNode(8, 30, 10, 10); na.setNode(9, 40, 10, 0); - FlagEncoder encoder = encodingManager.getEncoder("car"); EdgeIteratorState edge01, edge12, edge23, edge34, edge56, edge67, edge78, edge89, edge17, edge27, edge37; - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, edge01 = graph.edge(0, 1).setDistance(10), edge12 = graph.edge(1, 2).setDistance(10), edge23 = graph.edge(2, 3).setDistance(10), diff --git a/core/src/test/java/com/graphhopper/reader/dem/EdgeElevationInterpolatorTest.java b/core/src/test/java/com/graphhopper/reader/dem/EdgeElevationInterpolatorTest.java index 044b34f3838..f2010d99e49 100644 --- a/core/src/test/java/com/graphhopper/reader/dem/EdgeElevationInterpolatorTest.java +++ b/core/src/test/java/com/graphhopper/reader/dem/EdgeElevationInterpolatorTest.java @@ -19,13 +19,8 @@ import com.graphhopper.coll.GHBitSetImpl; import com.graphhopper.coll.GHIntHashSet; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.EnumEncodedValue; -import com.graphhopper.routing.ev.RoadEnvironment; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.IntsRef; import com.graphhopper.util.EdgeIteratorState; @@ -43,7 +38,6 @@ public abstract class EdgeElevationInterpolatorTest { protected BaseGraph graph; protected EnumEncodedValue roadEnvEnc; - protected FlagEncoder encoder; protected BooleanEncodedValue accessEnc; protected DecimalEncodedValue speedEnc; protected EncodingManager encodingManager; @@ -52,10 +46,9 @@ public abstract class EdgeElevationInterpolatorTest { @SuppressWarnings("resource") @BeforeEach public void setUp() { - encoder = FlagEncoders.createCar(); - encodingManager = EncodingManager.create(encoder); - accessEnc = encoder.getAccessEnc(); - speedEnc = encoder.getAverageSpeedEnc(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); graph = new BaseGraph.Builder(encodingManager).set3D(true).create(); roadEnvEnc = encodingManager.getEnumEncodedValue(RoadEnvironment.KEY, RoadEnvironment.class); edgeElevationInterpolator = createEdgeElevationInterpolator(); diff --git a/core/src/test/java/com/graphhopper/reader/dem/TunnelElevationInterpolatorTest.java b/core/src/test/java/com/graphhopper/reader/dem/TunnelElevationInterpolatorTest.java index 22d75773cfc..55f470ec729 100644 --- a/core/src/test/java/com/graphhopper/reader/dem/TunnelElevationInterpolatorTest.java +++ b/core/src/test/java/com/graphhopper/reader/dem/TunnelElevationInterpolatorTest.java @@ -253,7 +253,7 @@ public void interpolatesElevationOfTunnelWithFourOuterNodes() { na.setNode(9, 40, 10, 0); EdgeIteratorState edge01, edge12, edge23, edge34, edge56, edge67, edge78, edge89, edge27; - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, edge01 = graph.edge(0, 1).setDistance(10), edge12 = graph.edge(1, 2).setDistance(10), edge23 = graph.edge(2, 3).setDistance(10), diff --git a/core/src/test/java/com/graphhopper/routing/AStarBidirectionTest.java b/core/src/test/java/com/graphhopper/routing/AStarBidirectionTest.java index a65ccbe324d..33b260e7c85 100644 --- a/core/src/test/java/com/graphhopper/routing/AStarBidirectionTest.java +++ b/core/src/test/java/com/graphhopper/routing/AStarBidirectionTest.java @@ -21,9 +21,9 @@ import com.carrotsearch.hppc.IntArrayList; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.routing.weighting.WeightApproximator; @@ -37,8 +37,6 @@ class AStarBidirectionTest { @Test void infeasibleApproximator_noException() { - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); // An infeasible approximator means that the weight of the entries polled from the priority queue does not // increase monotonically. Here we deliberately choose the approximations and edge distances such that the fwd // search first explores the 0-1-2-3-4 branch, then polls node 10 which causes an update for node 2, but the @@ -47,12 +45,13 @@ void infeasibleApproximator_noException() { // This means the resulting path contains the invalid search tree branch 2(old)-3-4 and is not the shortest path, // because the SPTEntry for node 3 still points to the outdated/deleted entry for node 2. // We do not expect an exception, though, because for an infeasible approximator we cannot expect optimal paths. + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); + BaseGraph graph = new BaseGraph.Builder(em).create(); // 0-1----2-3-4----5-6-7-8-9 // \ / // 10 - BaseGraph graph = new BaseGraph.Builder(em).create(); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(0, 1).setDistance(100)); // the distance 1-2 is longer than 1-10-2 // we deliberately use 2-1 as storage direction, even though the edge points from 1 to 2, because this way diff --git a/core/src/test/java/com/graphhopper/routing/AlternativeRouteCHTest.java b/core/src/test/java/com/graphhopper/routing/AlternativeRouteCHTest.java index e83e16fe928..daf70797c94 100644 --- a/core/src/test/java/com/graphhopper/routing/AlternativeRouteCHTest.java +++ b/core/src/test/java/com/graphhopper/routing/AlternativeRouteCHTest.java @@ -19,9 +19,11 @@ import com.graphhopper.routing.ch.NodeOrderingProvider; import com.graphhopper.routing.ch.PrepareContractionHierarchies; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.CHConfig; @@ -36,8 +38,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class AlternativeRouteCHTest { - private final FlagEncoder carFE = FlagEncoders.createCar(); - private final EncodingManager em = EncodingManager.create(carFE); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + private final EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); public BaseGraph createTestGraph(EncodingManager tmpEM) { final BaseGraph graph = new BaseGraph.Builder(tmpEM).create(); @@ -56,7 +59,7 @@ public BaseGraph createTestGraph(EncodingManager tmpEM) { // has to be locally-shortest to be considered. // So we get all three alternatives. - GHUtility.setSpeed(60, 60, carFE.getAccessEnc(), carFE.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, graph.edge(5, 6).setDistance(10000), graph.edge(6, 3).setDistance(10000), graph.edge(3, 4).setDistance(10000), @@ -81,7 +84,7 @@ private RoutingCHGraph prepareCH(BaseGraph graph) { // meet on all four possible paths from 5 to 10 // 5 ---> 11 will be reachable via shortcuts, as 11 is on shortest path 5 --> 12 final int[] nodeOrdering = new int[]{0, 10, 12, 4, 3, 2, 5, 1, 6, 7, 8, 9, 11}; - CHConfig chConfig = CHConfig.nodeBased("p", new FastestWeighting(carFE.getAccessEnc(), carFE.getAverageSpeedEnc())); + CHConfig chConfig = CHConfig.nodeBased("p", new FastestWeighting(accessEnc, speedEnc)); PrepareContractionHierarchies contractionHierarchies = PrepareContractionHierarchies.fromGraph(graph, chConfig); contractionHierarchies.useFixedNodeOrdering(NodeOrderingProvider.fromArray(nodeOrdering)); PrepareContractionHierarchies.Result res = contractionHierarchies.doWork(); diff --git a/core/src/test/java/com/graphhopper/routing/AlternativeRouteEdgeCHTest.java b/core/src/test/java/com/graphhopper/routing/AlternativeRouteEdgeCHTest.java index 48253407f85..47b9eab6cfa 100644 --- a/core/src/test/java/com/graphhopper/routing/AlternativeRouteEdgeCHTest.java +++ b/core/src/test/java/com/graphhopper/routing/AlternativeRouteEdgeCHTest.java @@ -19,12 +19,8 @@ import com.carrotsearch.hppc.IntArrayList; import com.graphhopper.routing.ch.PrepareContractionHierarchies; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.TurnCostProvider; @@ -41,8 +37,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class AlternativeRouteEdgeCHTest { - private final FlagEncoder carFE = FlagEncoders.createCar(new PMap().putObject("turn_costs", true)); - private final EncodingManager em = EncodingManager.create(carFE); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + private final DecimalEncodedValue turnCostEnc = TurnCost.create("car", 1); + private final EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); public BaseGraph createTestGraph(EncodingManager tmpEM) { final BaseGraph graph = new BaseGraph.Builder(tmpEM).withTurnCosts(true).create(); @@ -61,9 +59,6 @@ public BaseGraph createTestGraph(EncodingManager tmpEM) { // has to be locally-shortest to be considered. // So we get all three alternatives. - FlagEncoder encoder = carFE; - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(5, 6).setDistance(10000)); EdgeIteratorState e6_3 = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(6, 3).setDistance(10000)); EdgeIteratorState e3_4 = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(3, 4).setDistance(10000)); @@ -83,17 +78,16 @@ public BaseGraph createTestGraph(EncodingManager tmpEM) { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(12, 10).setDistance(10000)); TurnCostStorage turnCostStorage = graph.getTurnCostStorage(); - DecimalEncodedValue carTurnCost = em.getDecimalEncodedValue(TurnCost.key(carFE.toString())); - turnCostStorage.set(carTurnCost, e3_4.getEdge(), 4, e4_11.getEdge(), Double.POSITIVE_INFINITY); - turnCostStorage.set(carTurnCost, e6_3.getEdge(), 3, e3_4.getEdge(), Double.POSITIVE_INFINITY); + turnCostStorage.set(turnCostEnc, e3_4.getEdge(), 4, e4_11.getEdge(), Double.POSITIVE_INFINITY); + turnCostStorage.set(turnCostEnc, e6_3.getEdge(), 3, e3_4.getEdge(), Double.POSITIVE_INFINITY); graph.freeze(); return graph; } private RoutingCHGraph prepareCH(BaseGraph graph) { - TurnCostProvider turnCostProvider = new DefaultTurnCostProvider(carFE.getTurnCostEnc(), graph.getTurnCostStorage()); - CHConfig chConfig = CHConfig.edgeBased("profile", new FastestWeighting(carFE.getAccessEnc(), carFE.getAverageSpeedEnc(), turnCostProvider)); + TurnCostProvider turnCostProvider = new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage()); + CHConfig chConfig = CHConfig.edgeBased("profile", new FastestWeighting(accessEnc, speedEnc, turnCostProvider)); PrepareContractionHierarchies contractionHierarchies = PrepareContractionHierarchies.fromGraph(graph, chConfig); PrepareContractionHierarchies.Result res = contractionHierarchies.doWork(); return RoutingCHGraphImpl.fromGraph(graph, res.getCHStorage(), res.getCHConfig()); @@ -102,8 +96,8 @@ private RoutingCHGraph prepareCH(BaseGraph graph) { @Test public void testAssumptions() { BaseGraph g = createTestGraph(em); - TurnCostProvider turnCostProvider = new DefaultTurnCostProvider(carFE.getTurnCostEnc(), g.getTurnCostStorage()); - CHConfig chConfig = CHConfig.edgeBased("profile", new FastestWeighting(carFE.getAccessEnc(), carFE.getAverageSpeedEnc(), turnCostProvider)); + TurnCostProvider turnCostProvider = new DefaultTurnCostProvider(turnCostEnc, g.getTurnCostStorage()); + CHConfig chConfig = CHConfig.edgeBased("profile", new FastestWeighting(accessEnc, speedEnc, turnCostProvider)); CHStorage chStorage = CHStorage.fromGraph(g, chConfig); RoutingCHGraph chGraph = RoutingCHGraphImpl.fromGraph(g, chStorage, chConfig); DijkstraBidirectionEdgeCHNoSOD router = new DijkstraBidirectionEdgeCHNoSOD(chGraph); diff --git a/core/src/test/java/com/graphhopper/routing/AlternativeRouteTest.java b/core/src/test/java/com/graphhopper/routing/AlternativeRouteTest.java index 728bb2d8ba5..7aa702b0420 100644 --- a/core/src/test/java/com/graphhopper/routing/AlternativeRouteTest.java +++ b/core/src/test/java/com/graphhopper/routing/AlternativeRouteTest.java @@ -18,9 +18,8 @@ package com.graphhopper.routing; import com.carrotsearch.hppc.IntArrayList; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; @@ -48,17 +47,22 @@ private static final class Fixture { final Weighting weighting; final TraversalMode traversalMode; final BaseGraph graph; - final FlagEncoder carFE; + final BooleanEncodedValue accessEnc; + final DecimalEncodedValue speedEnc; + final DecimalEncodedValue turnCostEnc; public Fixture(TraversalMode tMode) { this.traversalMode = tMode; - carFE = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(carFE); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + turnCostEnc = TurnCost.create("car", 1); + + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).add(turnCostEnc).build(); graph = new BaseGraph.Builder(em).withTurnCosts(true).create(); TurnCostProvider turnCostProvider = tMode.isEdgeBased() - ? new DefaultTurnCostProvider(carFE.getTurnCostEnc(), graph.getTurnCostStorage()) + ? new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage()) : TurnCostProvider.NO_TURN_COST_PROVIDER; - weighting = new FastestWeighting(carFE.getAccessEnc(), carFE.getAverageSpeedEnc(), turnCostProvider); + weighting = new FastestWeighting(accessEnc, speedEnc, turnCostProvider); } @Override @@ -77,7 +81,7 @@ public Stream provideArguments(ExtensionContext context) th } } - public static void initTestGraph(Graph graph, FlagEncoder encoder) { + public static void initTestGraph(Graph graph, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { /* 9 _/\ 1 2-3-4-10 @@ -85,7 +89,7 @@ public static void initTestGraph(Graph graph, FlagEncoder encoder) { 5--6-7---8 */ - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, graph.edge(1, 9).setDistance(1), graph.edge(9, 2).setDistance(1), graph.edge(2, 3).setDistance(1), @@ -114,7 +118,7 @@ public static void initTestGraph(Graph graph, FlagEncoder encoder) { @ParameterizedTest @ArgumentsSource(FixtureProvider.class) public void testCalcAlternatives(Fixture f) { - initTestGraph(f.graph, f.carFE); + initTestGraph(f.graph, f.accessEnc, f.speedEnc); PMap hints = new PMap(). putObject("alternative_route.max_share_factor", 0.5). putObject("alternative_route.max_weight_factor", 2). @@ -145,7 +149,7 @@ public void testCalcAlternatives(Fixture f) { @ParameterizedTest @ArgumentsSource(FixtureProvider.class) public void testCalcAlternatives2(Fixture f) { - initTestGraph(f.graph, f.carFE); + initTestGraph(f.graph, f.accessEnc, f.speedEnc); PMap hints = new PMap().putObject("alternative_route.max_paths", 3). putObject("alternative_route.max_share_factor", 0.7). putObject("alternative_route.min_plateau_factor", 0.15). @@ -179,14 +183,14 @@ private void checkAlternatives(List alternativ @ParameterizedTest @ArgumentsSource(FixtureProvider.class) - public void testDisconnectedAreas(Fixture p) { - initTestGraph(p.graph, p.carFE); + public void testDisconnectedAreas(Fixture f) { + initTestGraph(f.graph, f.accessEnc, f.speedEnc); // one single disconnected node - updateDistancesFor(p.graph, 20, 0.00, -0.01); + updateDistancesFor(f.graph, 20, 0.00, -0.01); PMap hints = new PMap().putObject("alternative_route.max_exploration_factor", 1); - AlternativeRoute altDijkstra = new AlternativeRoute(p.graph, p.weighting, p.traversalMode, hints); + AlternativeRoute altDijkstra = new AlternativeRoute(f.graph, f.weighting, f.traversalMode, hints); Path path = altDijkstra.calcPath(1, 20); assertFalse(path.isFound()); diff --git a/core/src/test/java/com/graphhopper/routing/CHQueryWithTurnCostsTest.java b/core/src/test/java/com/graphhopper/routing/CHQueryWithTurnCostsTest.java index 497a9eb8a83..769c00b088b 100644 --- a/core/src/test/java/com/graphhopper/routing/CHQueryWithTurnCostsTest.java +++ b/core/src/test/java/com/graphhopper/routing/CHQueryWithTurnCostsTest.java @@ -20,19 +20,13 @@ import com.carrotsearch.hppc.IntArrayList; import com.graphhopper.routing.ch.PrepareEncoder; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.EncodedValueLookup; -import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.storage.*; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.GHUtility; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -56,10 +50,9 @@ public class CHQueryWithTurnCostsTest { private static class Fixture { private final int maxCost = 10; - private final FlagEncoder encoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", maxCost).putObject("speed_two_directions", true)); - private final BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - private final DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); - private final EncodingManager encodingManager = EncodingManager.create(encoder); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, true); + private final DecimalEncodedValue turnCostEnc = TurnCost.create("car", maxCost); private final BaseGraph graph; private final CHConfig chConfig; private final String algoString; @@ -68,8 +61,9 @@ private static class Fixture { public Fixture(String algoString) { this.algoString = algoString; - graph = new BaseGraph.Builder(encodingManager).create(); - chConfig = CHConfig.edgeBased("profile", new ShortestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage()))); + EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); + graph = new BaseGraph.Builder(encodingManager).withTurnCosts(true).create(); + chConfig = CHConfig.edgeBased("profile", new ShortestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage()))); } @Override @@ -103,7 +97,7 @@ private void setTurnCost(int from, int via, int to, double cost) { } private void setTurnCost(EdgeIteratorState edge1, EdgeIteratorState edge2, int viaNode, double costs) { - graph.getTurnCostStorage().set(((EncodedValueLookup) encodingManager).getDecimalEncodedValue(TurnCost.key(encoder.toString())), edge1.getEdge(), viaNode, edge2.getEdge(), costs); + graph.getTurnCostStorage().set(turnCostEnc, edge1.getEdge(), viaNode, edge2.getEdge(), costs); } private void setRestriction(int from, int via, int to) { diff --git a/core/src/test/java/com/graphhopper/routing/DefaultBidirPathExtractorTest.java b/core/src/test/java/com/graphhopper/routing/DefaultBidirPathExtractorTest.java index 2095be98811..df84341d82a 100644 --- a/core/src/test/java/com/graphhopper/routing/DefaultBidirPathExtractorTest.java +++ b/core/src/test/java/com/graphhopper/routing/DefaultBidirPathExtractorTest.java @@ -18,19 +18,14 @@ package com.graphhopper.routing; import com.carrotsearch.hppc.IntArrayList; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.Graph; import com.graphhopper.storage.TurnCostStorage; import com.graphhopper.util.GHUtility; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -40,13 +35,13 @@ * @author easbar */ public class DefaultBidirPathExtractorTest { - private final FlagEncoder carEncoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", 10)); - private final BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - private final DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); - private final EncodingManager encodingManager = EncodingManager.create(carEncoder); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + private final DecimalEncodedValue turnCostEnc = TurnCost.create("car", 10); + private final EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); BaseGraph createGraph() { - return new BaseGraph.Builder(encodingManager).create(); + return new BaseGraph.Builder(encodingManager).withTurnCosts(true).create(); } @Test @@ -69,13 +64,12 @@ public void testExtract2() { // add some turn costs at node 2 where fwd&bwd searches meet. these costs have to be included in the // weight and the time of the path TurnCostStorage turnCostStorage = graph.getTurnCostStorage(); - DecimalEncodedValue turnCostEnc = encodingManager.getDecimalEncodedValue(TurnCost.key(carEncoder.toString())); turnCostStorage.set(turnCostEnc, 0, 2, 1, 5); SPTEntry fwdEntry = new SPTEntry(0, 2, 0.6, new SPTEntry(1, 0)); SPTEntry bwdEntry = new SPTEntry(1, 2, 1.2, new SPTEntry(3, 0)); - Path p = DefaultBidirPathExtractor.extractPath(graph, new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(carEncoder.getTurnCostEnc(), turnCostStorage)), fwdEntry, bwdEntry, 0); + Path p = DefaultBidirPathExtractor.extractPath(graph, new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, turnCostStorage)), fwdEntry, bwdEntry, 0); p.setWeight(5 + 1.8); assertEquals(IntArrayList.from(1, 2, 3), p.calcNodes()); diff --git a/core/src/test/java/com/graphhopper/routing/DijkstraBidirectionCHTest.java b/core/src/test/java/com/graphhopper/routing/DijkstraBidirectionCHTest.java index b5d31342c00..aa59b8e0c7f 100644 --- a/core/src/test/java/com/graphhopper/routing/DijkstraBidirectionCHTest.java +++ b/core/src/test/java/com/graphhopper/routing/DijkstraBidirectionCHTest.java @@ -20,13 +20,14 @@ import com.carrotsearch.hppc.IntArrayList; import com.graphhopper.routing.ch.CHRoutingAlgorithmFactory; import com.graphhopper.routing.ch.PrepareContractionHierarchies; +import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.storage.*; -import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.GHUtility; import com.graphhopper.util.PMap; import org.junit.jupiter.api.Test; @@ -45,24 +46,34 @@ public class DijkstraBidirectionCHTest { private final EncodingManager encodingManager; - private final FlagEncoder carEncoder; - private final FlagEncoder bike2Encoder; - private final FlagEncoder motorCycleEncoder; + private final BooleanEncodedValue carAccessEnc; + private final DecimalEncodedValue carSpeedEnc; + private final BooleanEncodedValue bike2AccessEnc; + private final DecimalEncodedValue bike2SpeedEnc; + private final BooleanEncodedValue motorcycleAccessEnc; + private final DecimalEncodedValue motorcycleSpeedEnc; public DijkstraBidirectionCHTest() { - encodingManager = EncodingManager.create("car,foot,bike2,motorcycle"); - carEncoder = encodingManager.getEncoder("car"); - bike2Encoder = encodingManager.getEncoder("bike2"); - motorCycleEncoder = encodingManager.getEncoder("motorcycle"); + carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + bike2AccessEnc = new SimpleBooleanEncodedValue("bike2_access", true); + bike2SpeedEnc = new DecimalEncodedValueImpl("bike2_speed", 4, 2, true); + motorcycleAccessEnc = new SimpleBooleanEncodedValue("motorcycle_access", true); + motorcycleSpeedEnc = new DecimalEncodedValueImpl("motorcycle_speed", 5, 5, true); + encodingManager = EncodingManager.start() + .add(carAccessEnc).add(carSpeedEnc) + .add(bike2AccessEnc).add(bike2SpeedEnc) + .add(motorcycleAccessEnc).add(motorcycleSpeedEnc) + .build(); } @Test public void testBaseGraph() { BaseGraph graph = createGHStorage(); - RoutingAlgorithmTest.initDirectedAndDiffSpeed(graph, carEncoder); + RoutingAlgorithmTest.initDirectedAndDiffSpeed(graph, carAccessEnc, carSpeedEnc); // do CH preparation for car - ShortestWeighting weighting = new ShortestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); + ShortestWeighting weighting = new ShortestWeighting(carAccessEnc, carSpeedEnc); prepareCH(graph, CHConfig.nodeBased(weighting.getName(), weighting)); // use base graph for solving normal Dijkstra @@ -74,15 +85,17 @@ public void testBaseGraph() { @Test public void testBaseGraphMultipleVehicles() { - EncodingManager em = EncodingManager.create("foot,car"); - FlagEncoder footEncoder = em.getEncoder("foot"); - FlagEncoder carEncoder = em.getEncoder("car"); - FastestWeighting footWeighting = new FastestWeighting(footEncoder.getAccessEnc(), footEncoder.getAverageSpeedEnc()); - FastestWeighting carWeighting = new FastestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); + SimpleBooleanEncodedValue footAccessEnc = new SimpleBooleanEncodedValue("foot_access", true); + DecimalEncodedValueImpl footSpeedEnc = new DecimalEncodedValueImpl("foot_speed", 4, 1, false); + SimpleBooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValueImpl carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(footAccessEnc).add(footSpeedEnc).add(carAccessEnc).add(carSpeedEnc).build(); + FastestWeighting footWeighting = new FastestWeighting(footAccessEnc, footSpeedEnc); + FastestWeighting carWeighting = new FastestWeighting(carAccessEnc, carSpeedEnc); CHConfig carConfig = CHConfig.nodeBased("p_car", carWeighting); BaseGraph g = new BaseGraph.Builder(em).create(); - RoutingAlgorithmTest.initFootVsCar(carEncoder, footEncoder, g); + RoutingAlgorithmTest.initFootVsCar(carAccessEnc, carSpeedEnc, footAccessEnc, footSpeedEnc, g); // do CH preparation for car RoutingCHGraph chGraph = prepareCH(g, carConfig); @@ -116,7 +129,7 @@ public void testBaseGraphMultipleVehicles() { @Test public void testStallingNodesReducesNumberOfVisitedNodes() { BaseGraph graph = createGHStorage(); - GHUtility.setSpeed(60, 0, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 0, carAccessEnc, carSpeedEnc, graph.edge(8, 9).setDistance(100), graph.edge(8, 3).setDistance(2), graph.edge(8, 5).setDistance(1), @@ -126,13 +139,13 @@ public void testStallingNodesReducesNumberOfVisitedNodes() { graph.edge(1, 8).setDistance(1), graph.edge(2, 3).setDistance(3)); for (int i = 3; i < 7; ++i) { - GHUtility.setSpeed(60, true, false, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc(), graph.edge(i, i + 1).setDistance(1)); + GHUtility.setSpeed(60, true, false, carAccessEnc, carSpeedEnc, graph.edge(i, i + 1).setDistance(1)); } - GHUtility.setSpeed(60, true, false, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc(), graph.edge(9, 0).setDistance(1)); - GHUtility.setSpeed(60, true, false, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc(), graph.edge(3, 9).setDistance(200)); + GHUtility.setSpeed(60, true, false, carAccessEnc, carSpeedEnc, graph.edge(9, 0).setDistance(1)); + GHUtility.setSpeed(60, true, false, carAccessEnc, carSpeedEnc, graph.edge(3, 9).setDistance(200)); graph.freeze(); - ShortestWeighting weighting = new ShortestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); + ShortestWeighting weighting = new ShortestWeighting(carAccessEnc, carSpeedEnc); CHConfig chConfig = CHConfig.nodeBased(weighting.getName(), weighting); CHStorage store = CHStorage.fromGraph(graph, chConfig); @@ -162,8 +175,8 @@ public void testStallingNodesReducesNumberOfVisitedNodes() { // \--<---| @Test public void testDirectionDependentSpeedFwdSearch() { - runTestWithDirectionDependentEdgeSpeed(10, 20, 0, 2, IntArrayList.from(0, 1, 2), motorCycleEncoder); - runTestWithDirectionDependentEdgeSpeed(10, 20, 0, 2, IntArrayList.from(0, 1, 2), bike2Encoder); + runTestWithDirectionDependentEdgeSpeed(10, 20, 0, 2, IntArrayList.from(0, 1, 2), motorcycleAccessEnc, motorcycleSpeedEnc); + runTestWithDirectionDependentEdgeSpeed(10, 20, 0, 2, IntArrayList.from(0, 1, 2), bike2AccessEnc, bike2SpeedEnc); } // s(0)--fast->1--t(2) @@ -172,19 +185,16 @@ public void testDirectionDependentSpeedFwdSearch() { // \--<---| @Test public void testDirectionDependentSpeedBwdSearch() { - runTestWithDirectionDependentEdgeSpeed(20, 10, 2, 0, IntArrayList.from(2, 1, 0), motorCycleEncoder); - runTestWithDirectionDependentEdgeSpeed(20, 10, 2, 0, IntArrayList.from(2, 1, 0), bike2Encoder); + runTestWithDirectionDependentEdgeSpeed(20, 10, 2, 0, IntArrayList.from(2, 1, 0), motorcycleAccessEnc, motorcycleSpeedEnc); + runTestWithDirectionDependentEdgeSpeed(20, 10, 2, 0, IntArrayList.from(2, 1, 0), bike2AccessEnc, bike2SpeedEnc); } - private void runTestWithDirectionDependentEdgeSpeed(double speed, double revSpeed, int from, int to, IntArrayList expectedPath, FlagEncoder encoder) { + private void runTestWithDirectionDependentEdgeSpeed(double speed, double revSpeed, int from, int to, IntArrayList expectedPath, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { BaseGraph graph = createGHStorage(); - EdgeIteratorState edge = GHUtility.setSpeed(encoder.getMaxSpeed() / 2, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(0, 1).setDistance(2)); - DecimalEncodedValue avSpeedEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey(encoder, "average_speed")); - edge.set(avSpeedEnc, speed, revSpeed); - - GHUtility.setSpeed(encoder.getMaxSpeed() / 2, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(1, 2).setDistance(1)); + GHUtility.setSpeed(speed, revSpeed, accessEnc, speedEnc, graph.edge(0, 1).setDistance(2)); + GHUtility.setSpeed(20, true, true, accessEnc, speedEnc, graph.edge(1, 2).setDistance(1)); graph.freeze(); - FastestWeighting weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased(weighting.getName(), weighting); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); new CHStorageBuilder(chStore).setIdentityLevels(); diff --git a/core/src/test/java/com/graphhopper/routing/DijkstraOneToManyTest.java b/core/src/test/java/com/graphhopper/routing/DijkstraOneToManyTest.java index 204045bc984..fe3d0339b70 100644 --- a/core/src/test/java/com/graphhopper/routing/DijkstraOneToManyTest.java +++ b/core/src/test/java/com/graphhopper/routing/DijkstraOneToManyTest.java @@ -18,8 +18,11 @@ package com.graphhopper.routing; import com.carrotsearch.hppc.IntArrayList; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.routing.weighting.Weighting; @@ -40,17 +43,19 @@ */ public class DijkstraOneToManyTest { + private final BooleanEncodedValue accessEnc; + private final DecimalEncodedValue speedEnc; private final EncodingManager encodingManager; - private final FlagEncoder encoder; - private Weighting defaultWeighting; + private final Weighting defaultWeighting; public DijkstraOneToManyTest() { - encodingManager = EncodingManager.create("car"); - encoder = encodingManager.getEncoder("car"); - defaultWeighting = new ShortestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); + defaultWeighting = new ShortestWeighting(accessEnc, speedEnc); } - private static void initGraphWeightLimit(Graph graph, FlagEncoder encoder) { + private static void initGraphWeightLimit(Graph graph, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { // 0----1 // / | // 7-- | @@ -59,7 +64,7 @@ private static void initGraphWeightLimit(Graph graph, FlagEncoder encoder) { // | | | // 4---3---2 - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1), graph.edge(1, 2).setDistance(1), graph.edge(3, 2).setDistance(1), @@ -87,7 +92,7 @@ public void testIssue182() { @Test public void testIssue239_and362() { BaseGraph graph = createGHStorage(); - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1), graph.edge(1, 2).setDistance(1), graph.edge(2, 0).setDistance(1), @@ -106,7 +111,7 @@ public void testIssue239_and362() { @Test public void testUseCache() { BaseGraph graph = createGHStorage(); - initTestStorage(graph, encoder); + initTestStorage(graph, accessEnc, speedEnc); RoutingAlgorithm algo = createAlgo(graph); Path p = algo.calcPath(0, 4); assertEquals(IntArrayList.from(0, 4), p.calcNodes()); @@ -125,7 +130,7 @@ private void initGraph(Graph graph) { // | / // 7-10---- // \-8 - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1), graph.edge(1, 2).setDistance(1), graph.edge(2, 3).setDistance(1), @@ -139,7 +144,7 @@ private void initGraph(Graph graph) { @Test public void testWeightLimit_issue380() { BaseGraph graph = createGHStorage(); - initGraphWeightLimit(graph, encoder); + initGraphWeightLimit(graph, accessEnc, speedEnc); DijkstraOneToMany algo = createAlgo(graph); algo.setWeightLimit(3); @@ -156,7 +161,7 @@ public void testWeightLimit_issue380() { @Test public void testUseCacheZeroPath_issue707() { BaseGraph graph = createGHStorage(); - initTestStorage(graph, encoder); + initTestStorage(graph, accessEnc, speedEnc); RoutingAlgorithm algo = createAlgo(graph); Path p = algo.calcPath(0, 0); diff --git a/core/src/test/java/com/graphhopper/routing/DirectedBidirectionalDijkstraTest.java b/core/src/test/java/com/graphhopper/routing/DirectedBidirectionalDijkstraTest.java index ad50b0083fd..6e1cbb7995f 100644 --- a/core/src/test/java/com/graphhopper/routing/DirectedBidirectionalDijkstraTest.java +++ b/core/src/test/java/com/graphhopper/routing/DirectedBidirectionalDijkstraTest.java @@ -2,10 +2,11 @@ import com.carrotsearch.hppc.IntArrayList; import com.carrotsearch.hppc.IntHashSet; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.querygraph.QueryGraph; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.AvoidEdgesWeighting; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; @@ -18,7 +19,6 @@ import com.graphhopper.storage.index.Snap; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.GHUtility; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; @@ -41,21 +41,18 @@ public class DirectedBidirectionalDijkstraTest { private TurnCostStorage turnCostStorage; private int maxTurnCosts; private BaseGraph graph; - private FlagEncoder encoder; private BooleanEncodedValue accessEnc; private DecimalEncodedValue speedEnc; - private EncodingManager encodingManager; private Weighting weighting; private DecimalEncodedValue turnCostEnc; @BeforeEach public void setup() { maxTurnCosts = 10; - encoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", maxTurnCosts)); - encodingManager = EncodingManager.create(encoder); - accessEnc = encoder.getAccessEnc(); - speedEnc = encoder.getAverageSpeedEnc(); - turnCostEnc = encoder.getTurnCostEnc(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + turnCostEnc = TurnCost.create("car", maxTurnCosts); + EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); graph = new BaseGraph.Builder(encodingManager).withTurnCosts(true).create(); turnCostStorage = graph.getTurnCostStorage(); weighting = createWeighting(Weighting.INFINITE_U_TURN_COSTS); @@ -238,7 +235,7 @@ public void directedRouting() { // | / \ | // 8 = 7 6 = 5 EdgeIteratorState rightNorth, rightSouth, leftSouth, leftNorth; - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1), graph.edge(1, 2).setDistance(1), graph.edge(2, 3).setDistance(1), diff --git a/core/src/test/java/com/graphhopper/routing/DirectedRoutingTest.java b/core/src/test/java/com/graphhopper/routing/DirectedRoutingTest.java index 4558ca748c2..b9a5fee8528 100644 --- a/core/src/test/java/com/graphhopper/routing/DirectedRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/DirectedRoutingTest.java @@ -20,9 +20,7 @@ import com.graphhopper.routing.ch.CHRoutingAlgorithmFactory; import com.graphhopper.routing.ch.PrepareContractionHierarchies; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.Subnetwork; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.lm.LMConfig; import com.graphhopper.routing.lm.LMRoutingAlgorithmFactory; import com.graphhopper.routing.lm.LandmarkStorage; @@ -30,7 +28,9 @@ import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.querygraph.QueryRoutingCHGraph; import com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.Weighting; @@ -87,7 +87,9 @@ private static class Fixture { private final BaseGraph graph; private final CHConfig chConfig; private final LMConfig lmConfig; - private final FlagEncoder encoder; + private final BooleanEncodedValue accessEnc; + private final DecimalEncodedValue speedEnc; + private final DecimalEncodedValue turnCostEnc; private final TurnCostStorage turnCostStorage; private final int maxTurnCosts; private final Weighting weighting; @@ -106,14 +108,16 @@ public Fixture(Algo algo, int uTurnCosts, boolean prepareCH, boolean prepareLM) // todo: this test only works with speedTwoDirections=false (as long as loops are enabled), otherwise it will // fail sometimes for edge-based algorithms, #1631, but maybe we can should disable different fwd/bwd speeds // only for loops instead? - encoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", maxTurnCosts)); - encodingManager = EncodingManager.start().add(encoder).add(Subnetwork.create("c2")).build(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + turnCostEnc = TurnCost.create("car", maxTurnCosts); + encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).add(Subnetwork.create("c2")).build(); graph = new BaseGraph.Builder(encodingManager).setDir(dir).withTurnCosts(true).create(); turnCostStorage = graph.getTurnCostStorage(); - weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), new DefaultTurnCostProvider(encoder.getTurnCostEnc(), turnCostStorage, uTurnCosts)); + weighting = new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, turnCostStorage, uTurnCosts)); chConfig = CHConfig.edgeBased("p1", weighting); // important: for LM preparation we need to use a weighting without turn costs #1960 - lmConfig = new LMConfig("c2", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc())); + lmConfig = new LMConfig("c2", new FastestWeighting(accessEnc, speedEnc)); } @Override @@ -216,8 +220,8 @@ public void randomGraph(Fixture f) { final int numQueries = 50; Random rnd = new Random(seed); GHUtility.buildRandomGraph(f.graph, rnd, 100, 2.2, true, true, - f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.8, 0.8); - GHUtility.addRandomTurnCosts(f.graph, seed, f.encoder.getAccessEnc(), f.encoder.getTurnCostEnc(), f.maxTurnCosts, f.turnCostStorage); + f.accessEnc, f.speedEnc, null, 0.7, 0.8, 0.8); + GHUtility.addRandomTurnCosts(f.graph, seed, f.accessEnc, f.turnCostEnc, f.maxTurnCosts, f.turnCostStorage); // GHUtility.printGraphForUnitTest(f.graph, f.encoder); f.preProcessGraph(); List strictViolations = new ArrayList<>(); @@ -258,8 +262,8 @@ public void randomGraph_withQueryGraph(Fixture f) { double pOffset = 0; Random rnd = new Random(seed); GHUtility.buildRandomGraph(f.graph, rnd, 50, 2.2, true, true, - f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.8, pOffset); - GHUtility.addRandomTurnCosts(f.graph, seed, f.encoder.getAccessEnc(), f.encoder.getTurnCostEnc(), f.maxTurnCosts, f.turnCostStorage); + f.accessEnc, f.speedEnc, null, 0.7, 0.8, pOffset); + GHUtility.addRandomTurnCosts(f.graph, seed, f.accessEnc, f.turnCostEnc, f.maxTurnCosts, f.turnCostStorage); // GHUtility.printGraphForUnitTest(graph, encoder); f.preProcessGraph(); LocationIndexTree index = new LocationIndexTree(f.graph, f.dir); @@ -311,8 +315,8 @@ public void issue_2581() { // 3-0=1-2=7-5 // | // 4 - BooleanEncodedValue accessEnc = f.encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = f.encoder.getAverageSpeedEnc(); + BooleanEncodedValue accessEnc = f.accessEnc; + DecimalEncodedValue speedEnc = f.speedEnc; GHUtility.setSpeed(60, 60, accessEnc, speedEnc, f.graph.edge(0, 1).setDistance(300.186000)); // edgeId=0 GHUtility.setSpeed(60, 60, accessEnc, speedEnc, f.graph.edge(0, 4).setDistance(751.113000)); // edgeId=1 GHUtility.setSpeed(60, 60, accessEnc, speedEnc, f.graph.edge(7, 2).setDistance(113.102000)); // edgeId=2 diff --git a/core/src/test/java/com/graphhopper/routing/DirectionResolverOnQueryGraphTest.java b/core/src/test/java/com/graphhopper/routing/DirectionResolverOnQueryGraphTest.java index 6e1f9fbbe57..7d35788bc2f 100644 --- a/core/src/test/java/com/graphhopper/routing/DirectionResolverOnQueryGraphTest.java +++ b/core/src/test/java/com/graphhopper/routing/DirectionResolverOnQueryGraphTest.java @@ -19,8 +19,13 @@ import com.carrotsearch.hppc.IntArrayList; import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.querygraph.QueryGraph; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.AccessFilter; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.NodeAccess; import com.graphhopper.storage.RAMDirectory; @@ -48,14 +53,17 @@ public class DirectionResolverOnQueryGraphTest { private QueryGraph queryGraph; private NodeAccess na; - private FlagEncoder encoder; + private BooleanEncodedValue accessEnc; + private DecimalEncodedValue speedEnc; private BaseGraph graph; private LocationIndexTree locationIndex; @BeforeEach public void setup() { - encoder = FlagEncoders.createCar(); - graph = new BaseGraph.Builder(EncodingManager.create(encoder)).create(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); + graph = new BaseGraph.Builder(em).create(); na = graph.getNodeAccess(); } @@ -294,7 +302,7 @@ private void addNode(int nodeId, double lat, double lon) { } private EdgeIteratorState addEdge(int from, int to, boolean bothDirections) { - return GHUtility.setSpeed(60, true, bothDirections, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(from, to).setDistance(1)); + return GHUtility.setSpeed(60, true, bothDirections, accessEnc, speedEnc, graph.edge(from, to).setDistance(1)); } private void init() { @@ -349,7 +357,7 @@ private DirectionResolverResult restrictedDirection(ExpectedResult restriction) } private int findEdge(int from, int to) { - EdgeExplorer explorer = queryGraph.createEdgeExplorer(AccessFilter.outEdges(encoder.getAccessEnc())); + EdgeExplorer explorer = queryGraph.createEdgeExplorer(AccessFilter.outEdges(accessEnc)); EdgeIterator iter = explorer.setBaseNode(from); while (iter.next()) { if (iter.getAdjNode() == to) { @@ -360,7 +368,6 @@ private int findEdge(int from, int to) { } private boolean isAccessible(EdgeIteratorState edge, boolean reverse) { - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); return reverse ? edge.getReverse(accessEnc) : edge.get(accessEnc); } diff --git a/core/src/test/java/com/graphhopper/routing/DirectionResolverTest.java b/core/src/test/java/com/graphhopper/routing/DirectionResolverTest.java index 84152715c70..9149e5cf5d7 100644 --- a/core/src/test/java/com/graphhopper/routing/DirectionResolverTest.java +++ b/core/src/test/java/com/graphhopper/routing/DirectionResolverTest.java @@ -18,11 +18,12 @@ package com.graphhopper.routing; import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.util.AccessFilter; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.NodeAccess; import com.graphhopper.util.EdgeExplorer; @@ -43,14 +44,17 @@ * @see DirectionResolverOnQueryGraphTest for tests that include direction resolving for virtual nodes and edges */ public class DirectionResolverTest { - private FlagEncoder encoder; + private BooleanEncodedValue accessEnc; + private DecimalEncodedValue speedEnc; private BaseGraph graph; private NodeAccess na; @BeforeEach public void setup() { - encoder = FlagEncoders.createCar(); - graph = new BaseGraph.Builder(EncodingManager.create(encoder)).create(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); + graph = new BaseGraph.Builder(em).create(); na = graph.getNodeAccess(); } @@ -70,7 +74,7 @@ public void isolated_nodes_blocked_edge() { addNode(0, 0, 0); addNode(1, 0.1, 0.1); // with edges without access flags (blocked edges) - graph.edge(0, 1).set(encoder.getAccessEnc(), false, false); + graph.edge(0, 1).set(accessEnc, false, false); checkResult(0, impossible()); checkResult(1, impossible()); @@ -382,11 +386,10 @@ private void addNode(int nodeId, double lat, double lon) { } private EdgeIteratorState addEdge(int from, int to, boolean bothDirections) { - return GHUtility.setSpeed(60, true, bothDirections, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(from, to).setDistance(1)); + return GHUtility.setSpeed(60, true, bothDirections, accessEnc, speedEnc, graph.edge(from, to).setDistance(1)); } private boolean isAccessible(EdgeIteratorState edge, boolean reverse) { - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); return reverse ? edge.getReverse(accessEnc) : edge.get(accessEnc); } @@ -400,7 +403,7 @@ private void checkResult(int node, double lat, double lon, DirectionResolverResu } private int edge(int from, int to) { - EdgeExplorer explorer = graph.createEdgeExplorer(AccessFilter.outEdges(encoder.getAccessEnc())); + EdgeExplorer explorer = graph.createEdgeExplorer(AccessFilter.outEdges(accessEnc)); EdgeIterator iter = explorer.setBaseNode(from); while (iter.next()) { if (iter.getAdjNode() == to) { diff --git a/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java b/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java index a0ee238d911..0675629765c 100644 --- a/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java +++ b/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java @@ -19,12 +19,8 @@ import com.carrotsearch.hppc.IntArrayList; import com.carrotsearch.hppc.cursors.IntCursor; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; @@ -34,7 +30,6 @@ import com.graphhopper.storage.TurnCostStorage; import com.graphhopper.util.GHUtility; import com.graphhopper.util.Helper; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -58,7 +53,6 @@ */ public class EdgeBasedRoutingAlgorithmTest { private static final Logger LOGGER = LoggerFactory.getLogger(EdgeBasedRoutingAlgorithmTest.class); - private FlagEncoder carEncoder; private BooleanEncodedValue accessEnc; private DecimalEncodedValue speedEnc; private DecimalEncodedValue turnCostEnc; @@ -98,12 +92,10 @@ private void initGraph(Graph graph) { } private EncodingManager createEncodingManager(boolean restrictedOnly) { - carEncoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", restrictedOnly ? 1 : 3)); - EncodingManager em = EncodingManager.create(carEncoder); - accessEnc = carEncoder.getAccessEnc(); - speedEnc = carEncoder.getAverageSpeedEnc(); - turnCostEnc = getTurnCostEnc(carEncoder); - return em; + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + turnCostEnc = TurnCost.create("car", restrictedOnly ? 1 : 3); + return EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); } public Path calcPath(Graph g, int from, int to, String algoStr) { @@ -118,15 +110,11 @@ public RoutingAlgorithm createAlgo(Graph g, Weighting weighting, String algoStr, } private BaseGraph createStorage(EncodingManager em) { - BaseGraph graph = new BaseGraph.Builder(em).create(); + BaseGraph graph = new BaseGraph.Builder(em).withTurnCosts(true).create(); tcs = graph.getTurnCostStorage(); return graph; } - private DecimalEncodedValue getTurnCostEnc(FlagEncoder encoder) { - return encoder.getDecimalEncodedValue(TurnCost.key(encoder.toString())); - } - private void initTurnRestrictions(BaseGraph g) { // only forward from 2-3 to 3-4 => limit 2,3->3,6 and 2,3->3,1 setTurnRestriction(g, 2, 3, 6); @@ -170,7 +158,7 @@ public void testRandomGraph(String algoStr) { BaseGraph g = createStorage(em); GHUtility.buildRandomGraph(g, rnd, 50, 2.2, true, true, accessEnc, speedEnc, null, 0.8, 0.8, 0.8); - GHUtility.addRandomTurnCosts(g, seed, carEncoder.getAccessEnc(), carEncoder.getTurnCostEnc(), 3, tcs); + GHUtility.addRandomTurnCosts(g, seed, accessEnc, turnCostEnc, 3, tcs); g.freeze(); int numPathsNotFound = 0; // todo: reduce redundancy with RandomCHRoutingTest diff --git a/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java b/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java index 37aaec7f5c7..a55756e10fd 100644 --- a/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java @@ -82,7 +82,9 @@ void testMaxPriority() { { CustomModel customModel = new CustomModel(); - CustomWeighting weighting = CustomModelParser.createWeighting(encoder, em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); + CustomWeighting weighting = CustomModelParser.createWeighting(encoder.getAccessEnc(), + encoder.getAverageSpeedEnc(), encoder.getPriorityEnc(), encoder.getMaxSpeed(), em, + TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); assertEquals(pathDijkstra.calcNodes(), pathAStar.calcNodes()); @@ -93,7 +95,8 @@ void testMaxPriority() { CustomModel customModel = new CustomModel(); // now we even increase the priority in the custom model, which also needs to be accounted for in weighting.getMinWeight customModel.addToPriority(Statement.If("road_class == MOTORWAY", Statement.Op.MULTIPLY, 3)); - CustomWeighting weighting = CustomModelParser.createWeighting(encoder, em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); + CustomWeighting weighting = CustomModelParser.createWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + encoder.getPriorityEnc(), encoder.getMaxSpeed(), em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); assertEquals(pathDijkstra.calcNodes(), pathAStar.calcNodes()); diff --git a/core/src/test/java/com/graphhopper/routing/RoundTripRoutingTest.java b/core/src/test/java/com/graphhopper/routing/RoundTripRoutingTest.java index fae9951487a..82fe58b49c4 100644 --- a/core/src/test/java/com/graphhopper/routing/RoundTripRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/RoundTripRoutingTest.java @@ -122,7 +122,7 @@ public void testCalcRoundTrip() { private BaseGraph createTestGraph() { BaseGraph graph = new BaseGraph.Builder(em).withTurnCosts(true).create(); - AlternativeRouteTest.initTestGraph(graph, carFE); + AlternativeRouteTest.initTestGraph(graph, carFE.getAccessEnc(), carFE.getAverageSpeedEnc()); return graph; } diff --git a/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmTest.java b/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmTest.java index 29936da675d..3fec2fa2542 100644 --- a/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmTest.java +++ b/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmTest.java @@ -23,11 +23,12 @@ import com.graphhopper.routing.ch.PrepareContractionHierarchies; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.querygraph.QueryRoutingCHGraph; import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.ShortestWeighting; @@ -87,11 +88,12 @@ public class RoutingAlgorithmTest { private static class Fixture { private final EncodingManager encodingManager; - private final FlagEncoder carEncoder; - private final FlagEncoder footEncoder; - private final FlagEncoder bike2Encoder; private final BooleanEncodedValue carAccessEnc; + private final BooleanEncodedValue footAccessEnc; + private final BooleanEncodedValue bike2AccessEnc; private final DecimalEncodedValue carSpeedEnc; + private final DecimalEncodedValue footSpeedEnc; + private final DecimalEncodedValue bike2SpeedEnc; private final PathCalculator pathCalculator; private final TraversalMode traversalMode; private final Weighting defaultWeighting; @@ -100,13 +102,16 @@ private static class Fixture { public Fixture(PathCalculator pathCalculator, TraversalMode traversalMode) { this.pathCalculator = pathCalculator; this.traversalMode = traversalMode; - // vehicles used in this test - encodingManager = EncodingManager.create("car,foot,bike2"); - carEncoder = encodingManager.getEncoder("car"); - footEncoder = encodingManager.getEncoder("foot"); - bike2Encoder = encodingManager.getEncoder("bike2"); - carAccessEnc = carEncoder.getAccessEnc(); - carSpeedEnc = carEncoder.getAverageSpeedEnc(); + carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + footAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + bike2AccessEnc = new SimpleBooleanEncodedValue("car_access", true); + carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + footSpeedEnc = new DecimalEncodedValueImpl("foot_speed", 4, 1, false); + bike2SpeedEnc = new DecimalEncodedValueImpl("bike2_speed", 4, 2, true); + encodingManager = EncodingManager.start() + .add(carAccessEnc).add(carSpeedEnc) + .add(footAccessEnc).add(footSpeedEnc) + .add(bike2AccessEnc).add(bike2SpeedEnc).build(); // most tests use the default weighting, but this can be chosen for each test separately defaultWeighting = new ShortestWeighting(carAccessEnc, carSpeedEnc); // most tests do not limit the number of visited nodes, but this can be chosen for each test separately @@ -230,7 +235,7 @@ public Stream provideArguments(ExtensionContext context) th @ArgumentsSource(FixtureProvider.class) public void testCalcShortestPath(Fixture f) { BaseGraph graph = f.createGHStorage(); - initTestStorage(graph, f.carEncoder); + initTestStorage(graph, f.carAccessEnc, f.carSpeedEnc); Path p = f.calcPath(graph, 0, 7); assertEquals(nodes(0, 4, 5, 7), p.calcNodes(), p.toString()); assertEquals(62.1, p.getDistance(), .1, p.toString()); @@ -285,7 +290,7 @@ public void testBidirectionalLinear(Fixture f) { public void testCalcFastestPath(Fixture f) { FastestWeighting fastestWeighting = new FastestWeighting(f.carAccessEnc, f.carSpeedEnc); BaseGraph graph = f.createGHStorage(false); - initDirectedAndDiffSpeed(graph, f.carEncoder); + initDirectedAndDiffSpeed(graph, f.carAccessEnc, f.carSpeedEnc); Path p1 = f.calcPath(graph, f.defaultWeighting, 0, 3); assertEquals(nodes(0, 1, 5, 2, 3), p1.calcNodes()); @@ -303,9 +308,7 @@ public void testCalcFastestPath(Fixture f) { // 4-5-- | // |/ \--7 // 6----/ - static void initDirectedAndDiffSpeed(Graph graph, FlagEncoder enc) { - BooleanEncodedValue accessEnc = enc.getAccessEnc(); - DecimalEncodedValue speedEnc = enc.getAverageSpeedEnc(); + static void initDirectedAndDiffSpeed(Graph graph, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { GHUtility.setSpeed(10, true, false, accessEnc, speedEnc, graph.edge(0, 1)); GHUtility.setSpeed(100, true, false, accessEnc, speedEnc, graph.edge(0, 4)); @@ -343,21 +346,17 @@ static void initDirectedAndDiffSpeed(Graph graph, FlagEncoder enc) { @ParameterizedTest @ArgumentsSource(FixtureProvider.class) public void testCalcFootPath(Fixture f) { - ShortestWeighting shortestWeighting = new ShortestWeighting(f.footEncoder.getAccessEnc(), f.footEncoder.getAverageSpeedEnc()); + ShortestWeighting shortestWeighting = new ShortestWeighting(f.footAccessEnc, f.footSpeedEnc); BaseGraph graph = f.createGHStorage(false); - initFootVsCar(f.carEncoder, f.footEncoder, graph); + initFootVsCar(f.carAccessEnc, f.carSpeedEnc, f.footAccessEnc, f.footSpeedEnc, graph); Path p1 = f.calcPath(graph, shortestWeighting, 0, 7); assertEquals(17000, p1.getDistance(), 1e-6, p1.toString()); assertEquals(12240 * 1000, p1.getTime(), p1.toString()); assertEquals(nodes(0, 4, 5, 7), p1.calcNodes()); } - static void initFootVsCar(FlagEncoder carEncoder, FlagEncoder footEncoder, Graph graph) { - BooleanEncodedValue footAccessEnc = footEncoder.getAccessEnc(); - DecimalEncodedValue footSpeedEnc = footEncoder.getAverageSpeedEnc(); - BooleanEncodedValue carAccessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue carSpeedEnc = carEncoder.getAverageSpeedEnc(); - + static void initFootVsCar(BooleanEncodedValue carAccessEnc, DecimalEncodedValue carSpeedEnc, + BooleanEncodedValue footAccessEnc, DecimalEncodedValue footSpeedEnc, Graph graph) { EdgeIteratorState edge = graph.edge(0, 1).setDistance(7000); GHUtility.setSpeed(5, true, true, footAccessEnc, footSpeedEnc, edge); GHUtility.setSpeed(10, true, false, carAccessEnc, carSpeedEnc, edge); @@ -395,9 +394,7 @@ static void initFootVsCar(FlagEncoder carEncoder, FlagEncoder footEncoder, Graph } // see test-graph.svg ! - static void initTestStorage(Graph graph, FlagEncoder encoder) { - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); + static void initTestStorage(Graph graph, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(7)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 4).setDistance(6)); @@ -468,7 +465,7 @@ public void testNoPathFound(Fixture f) { @ArgumentsSource(FixtureProvider.class) public void testWikipediaShortestPath(Fixture f) { BaseGraph graph = f.createGHStorage(); - initWikipediaTestGraph(graph, f.carEncoder); + initWikipediaTestGraph(graph, f.carAccessEnc, f.carSpeedEnc); Path p = f.calcPath(graph, 0, 4); assertEquals(nodes(0, 2, 5, 4), p.calcNodes(), p.toString()); assertEquals(20, p.getDistance(), 1e-4, p.toString()); @@ -478,16 +475,14 @@ public void testWikipediaShortestPath(Fixture f) { @ArgumentsSource(FixtureProvider.class) public void testCalcIf1EdgeAway(Fixture f) { BaseGraph graph = f.createGHStorage(); - initTestStorage(graph, f.carEncoder); + initTestStorage(graph, f.carAccessEnc, f.carSpeedEnc); Path p = f.calcPath(graph, 1, 2); assertEquals(nodes(1, 2), p.calcNodes()); assertEquals(35.1, p.getDistance(), .1, p.toString()); } // see wikipedia-graph.svg ! - private void initWikipediaTestGraph(Graph graph, FlagEncoder encoder) { - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); + private void initWikipediaTestGraph(Graph graph, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(7)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 2).setDistance(9)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 5).setDistance(14)); @@ -503,7 +498,7 @@ private void initWikipediaTestGraph(Graph graph, FlagEncoder encoder) { @ArgumentsSource(FixtureProvider.class) public void testBidirectional(Fixture f) { BaseGraph graph = f.createGHStorage(); - initBiGraph(graph, f.carEncoder); + initBiGraph(graph, f.carAccessEnc, f.carSpeedEnc); Path p = f.calcPath(graph, 0, 4); assertEquals(nodes(0, 7, 6, 8, 3, 4), p.calcNodes(), p.toString()); @@ -520,10 +515,8 @@ public void testBidirectional(Fixture f) { // | 8 | // \ / | // 7-6----5 - public static void initBiGraph(Graph graph, FlagEncoder encoder) { + public static void initBiGraph(Graph graph, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { // distance will be overwritten in second step as we need to calculate it from lat,lon - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 2).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(2, 3).setDistance(1)); @@ -582,7 +575,7 @@ public void testCreateAlgoTwice(Fixture f) { @ArgumentsSource(FixtureProvider.class) public void testMaxVisitedNodes(Fixture f) { BaseGraph graph = f.createGHStorage(); - initBiGraph(graph, f.carEncoder); + initBiGraph(graph, f.carAccessEnc, f.carSpeedEnc); Path p = f.calcPath(graph, 0, 4); assertTrue(p.isFound()); @@ -595,21 +588,19 @@ public void testMaxVisitedNodes(Fixture f) { @ArgumentsSource(FixtureProvider.class) public void testBidirectional2(Fixture f) { BaseGraph graph = f.createGHStorage(); - initBidirGraphManualDistances(graph, f.carEncoder); + initBidirGraphManualDistances(graph, f.carAccessEnc, f.carSpeedEnc); Path p = f.calcPath(graph, 0, 4); assertEquals(40, p.getDistance(), 1e-4, p.toString()); assertEquals(5, p.calcNodes().size(), p.toString()); assertEquals(nodes(0, 7, 6, 5, 4), p.calcNodes()); } - private void initBidirGraphManualDistances(BaseGraph graph, FlagEncoder encoder) { + private void initBidirGraphManualDistances(BaseGraph graph, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { // 0-1-2-3-4 // | / | // | 8 | // \ / / // 7-6-5-/ - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(100)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 2).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(2, 3).setDistance(1)); @@ -627,7 +618,7 @@ private void initBidirGraphManualDistances(BaseGraph graph, FlagEncoder encoder) public void testRekeyBugOfIntBinHeap(Fixture f) { // using Dijkstra + IntBinHeap then rekey loops endlessly BaseGraph matrixGraph = f.createGHStorage(); - initMatrixALikeGraph(matrixGraph, f.carEncoder); + initMatrixALikeGraph(matrixGraph, f.carAccessEnc, f.carSpeedEnc); Path p = f.calcPath(matrixGraph, 36, 91); assertEquals(12, p.calcNodes().size()); @@ -642,7 +633,7 @@ public void testRekeyBugOfIntBinHeap(Fixture f) { testCorrectWeight(f, matrixGraph); } - private static void initMatrixALikeGraph(BaseGraph tmpGraph, FlagEncoder encoder) { + private static void initMatrixALikeGraph(BaseGraph tmpGraph, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { int WIDTH = 10; int HEIGHT = 15; int[][] matrix = new int[WIDTH][HEIGHT]; @@ -664,7 +655,7 @@ private static void initMatrixALikeGraph(BaseGraph tmpGraph, FlagEncoder encoder if (print) System.out.print(" " + (int) dist + "\t "); - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), tmpGraph.edge(matrix[w][h], matrix[w][h - 1]).setDistance(dist)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, tmpGraph.edge(matrix[w][h], matrix[w][h - 1]).setDistance(dist)); } } if (print) { @@ -682,7 +673,7 @@ private static void initMatrixALikeGraph(BaseGraph tmpGraph, FlagEncoder encoder float dist = 5 + Math.abs(rand.nextInt(5)); if (print) System.out.print("-- " + (int) dist + "\t-- "); - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), tmpGraph.edge(matrix[w][h], matrix[w - 1][h]).setDistance(dist)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, tmpGraph.edge(matrix[w][h], matrix[w - 1][h]).setDistance(dist)); } if (print) System.out.print("(" + matrix[w][h] + ")\t"); @@ -794,7 +785,7 @@ public void testWithCoordinates(Fixture f) { @ArgumentsSource(FixtureProvider.class) public void testCalcIfEmptyWay(Fixture f) { BaseGraph graph = f.createGHStorage(); - initTestStorage(graph, f.carEncoder); + initTestStorage(graph, f.carAccessEnc, f.carSpeedEnc); Path p = f.calcPath(graph, 0, 0); assertPathFromEqualsTo(p, 0); } @@ -803,7 +794,7 @@ public void testCalcIfEmptyWay(Fixture f) { @ArgumentsSource(FixtureProvider.class) public void testViaEdges_FromEqualsTo(Fixture f) { BaseGraph graph = f.createGHStorage(); - initTestStorage(graph, f.carEncoder); + initTestStorage(graph, f.carAccessEnc, f.carSpeedEnc); // identical tower nodes Path p = f.calcPath(graph, new GHPoint(0.001, 0.000), new GHPoint(0.001, 0.000)); assertPathFromEqualsTo(p, 0); @@ -822,7 +813,7 @@ public void testViaEdges_FromEqualsTo(Fixture f) { @ArgumentsSource(FixtureProvider.class) public void testViaEdges_BiGraph(Fixture f) { BaseGraph graph = f.createGHStorage(); - initBiGraph(graph, f.carEncoder); + initBiGraph(graph, f.carAccessEnc, f.carSpeedEnc); // 0-7 to 4-3 Path p = f.calcPath(graph, new GHPoint(0.0009, 0), new GHPoint(0.001, 0.001105)); @@ -839,7 +830,7 @@ public void testViaEdges_BiGraph(Fixture f) { @ArgumentsSource(FixtureProvider.class) public void testViaEdges_WithCoordinates(Fixture f) { BaseGraph graph = f.createGHStorage(); - initTestStorage(graph, f.carEncoder); + initTestStorage(graph, f.carAccessEnc, f.carSpeedEnc); Path p = f.calcPath(graph, f.defaultWeighting, 0, 1, 2, 3); assertEquals(nodes(8, 1, 2, 9), p.calcNodes()); assertEquals(56.7, p.getDistance(), .1, p.toString()); @@ -885,7 +876,7 @@ public void testViaEdges_SpecialCases(Fixture f) { public void testQueryGraphAndFastest(Fixture f) { Weighting weighting = new FastestWeighting(f.carAccessEnc, f.carSpeedEnc); BaseGraph graph = f.createGHStorage(false); - initDirectedAndDiffSpeed(graph, f.carEncoder); + initDirectedAndDiffSpeed(graph, f.carAccessEnc, f.carSpeedEnc); Path p = f.calcPath(graph, weighting, new GHPoint(0.002, 0.0005), new GHPoint(0.0017, 0.0031)); assertEquals(nodes(8, 1, 5, 3, 9), p.calcNodes()); assertEquals(602.98, p.getDistance(), 1e-1); @@ -894,11 +885,11 @@ public void testQueryGraphAndFastest(Fixture f) { @ParameterizedTest @ArgumentsSource(FixtureProvider.class) public void testTwoWeightsPerEdge(Fixture f) { - FastestWeighting fastestWeighting = new FastestWeighting(f.bike2Encoder.getAccessEnc(), f.bike2Encoder.getAverageSpeedEnc()); + FastestWeighting fastestWeighting = new FastestWeighting(f.bike2AccessEnc, f.bike2SpeedEnc); BaseGraph graph = f.createGHStorage(true); - initEleGraph(graph, f.bike2Encoder, 18); + initEleGraph(graph, 18, f.bike2AccessEnc, f.bike2SpeedEnc); // force the other path - GHUtility.setSpeed(10, false, true, f.bike2Encoder.getAccessEnc(), f.bike2Encoder.getAverageSpeedEnc(), GHUtility.getEdge(graph, 0, 3)); + GHUtility.setSpeed(10, false, true, f.bike2AccessEnc, f.bike2SpeedEnc, GHUtility.getEdge(graph, 0, 3)); // for two weights per edge it happened that Path (and also the Weighting) read the wrong side // of the speed and read 0 => infinity weight => overflow of millis => negative millis! @@ -997,12 +988,12 @@ public String toString() { }; BaseGraph graph = f.createGHStorage(true); - initEleGraph(graph, f.carEncoder, 60); + initEleGraph(graph, 60, f.carAccessEnc, f.carSpeedEnc); Path p = f.calcPath(graph, 0, 10); assertEquals(nodes(0, 4, 6, 10), p.calcNodes()); graph = f.createGHStorage(true); - initEleGraph(graph, f.carEncoder, 60); + initEleGraph(graph, 60, f.carAccessEnc, f.carSpeedEnc); p = f.calcPath(graph, fakeWeighting, 3, 0, 10, 9); assertEquals(nodes(12, 0, 1, 2, 11, 7, 10, 13), p.calcNodes()); assertEquals(37009621, p.getTime()); @@ -1037,11 +1028,11 @@ public void testRandomGraph(Fixture f) { @ParameterizedTest @ArgumentsSource(FixtureProvider.class) public void testMultipleVehicles_issue548(Fixture f) { - FastestWeighting footWeighting = new FastestWeighting(f.footEncoder.getAccessEnc(), f.footEncoder.getAverageSpeedEnc()); + FastestWeighting footWeighting = new FastestWeighting(f.footAccessEnc, f.footSpeedEnc); FastestWeighting carWeighting = new FastestWeighting(f.carAccessEnc, f.carSpeedEnc); BaseGraph graph = f.createGHStorage(false); - initFootVsCar(f.carEncoder, f.footEncoder, graph); + initFootVsCar(f.carAccessEnc, f.carSpeedEnc, f.footAccessEnc, f.footSpeedEnc, graph); // normal path would be 0-4-6-7 for car: Path carPath1 = f.calcPath(graph, carWeighting, 0, 7); @@ -1055,7 +1046,7 @@ public void testMultipleVehicles_issue548(Fixture f) { // ... but now we block 4-6 for car. note that we have to recreate the storage to create a new directory so // we can create new CHs :( graph = f.createGHStorage(false); - initFootVsCar(f.carEncoder, f.footEncoder, graph); + initFootVsCar(f.carAccessEnc, f.carSpeedEnc, f.footAccessEnc, f.footSpeedEnc, graph); GHUtility.setSpeed(20, false, false, f.carAccessEnc, f.carSpeedEnc, GHUtility.getEdge(graph, 4, 6)); f.resetCH(); @@ -1076,9 +1067,7 @@ public void testMultipleVehicles_issue548(Fixture f) { // 5-6-7 // | |\| // 8-9-10 - private void initEleGraph(Graph graph, FlagEncoder encoder, double s) { - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); + private void initEleGraph(Graph graph, double s, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { GHUtility.setSpeed(s, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(10)); GHUtility.setSpeed(s, true, true, accessEnc, speedEnc, graph.edge(0, 4).setDistance(12)); GHUtility.setSpeed(s, true, true, accessEnc, speedEnc, graph.edge(0, 3).setDistance(5)); diff --git a/core/src/test/java/com/graphhopper/routing/ch/CHTurnCostTest.java b/core/src/test/java/com/graphhopper/routing/ch/CHTurnCostTest.java index 425af6cbd0f..edd12db6bd7 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/CHTurnCostTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/CHTurnCostTest.java @@ -22,13 +22,13 @@ import com.graphhopper.routing.DijkstraBidirectionEdgeCHNoSOD; import com.graphhopper.routing.Path; import com.graphhopper.routing.RoutingAlgorithm; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.EncodedValueLookup; -import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.querygraph.QueryRoutingCHGraph; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.AccessFilter; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.routing.weighting.Weighting; @@ -68,7 +68,6 @@ public class CHTurnCostTest { private static final Logger LOGGER = LoggerFactory.getLogger(CHTurnCostTest.class); private int maxCost; - private FlagEncoder encoder; private BooleanEncodedValue accessEnc; private DecimalEncodedValue speedEnc; private DecimalEncodedValue turnCostEnc; @@ -83,12 +82,11 @@ public class CHTurnCostTest { @BeforeEach public void init() { maxCost = 10; - encoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", maxCost)); - accessEnc = encoder.getAccessEnc(); - speedEnc = encoder.getAverageSpeedEnc(); - turnCostEnc = encoder.getTurnCostEnc(); - encodingManager = EncodingManager.create(encoder); - graph = new BaseGraph.Builder(encodingManager).build(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + turnCostEnc = TurnCost.create("car", maxCost); + encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); + graph = new BaseGraph.Builder(encodingManager).withTurnCosts(true).build(); turnCostStorage = graph.getTurnCostStorage(); chConfigs = createCHConfigs(); // the default CH profile with infinite u-turn costs, can be reset in tests that should run with finite u-turn diff --git a/core/src/test/java/com/graphhopper/routing/ch/EdgeBasedNodeContractorTest.java b/core/src/test/java/com/graphhopper/routing/ch/EdgeBasedNodeContractorTest.java index 15f2baac8b9..4e723b6bf95 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/EdgeBasedNodeContractorTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/EdgeBasedNodeContractorTest.java @@ -18,12 +18,8 @@ package com.graphhopper.routing.ch; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.routing.weighting.Weighting; @@ -49,7 +45,6 @@ */ public class EdgeBasedNodeContractorTest { private final int maxCost = 10; - private FlagEncoder encoder; private BooleanEncodedValue accessEnc; private DecimalEncodedValue speedEnc; private DecimalEncodedValue turnCostEnc; @@ -66,12 +61,11 @@ public void setup() { } private void initialize() { - encoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", maxCost)); - EncodingManager encodingManager = EncodingManager.create(encoder); - accessEnc = encoder.getAccessEnc(); - speedEnc = encoder.getAverageSpeedEnc(); - turnCostEnc = encoder.getTurnCostEnc(); - graph = new BaseGraph.Builder(encodingManager).create(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + turnCostEnc = TurnCost.create("car", maxCost); + EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); + graph = new BaseGraph.Builder(encodingManager).withTurnCosts(true).create(); chConfigs = Arrays.asList( CHConfig.edgeBased("p1", new ShortestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage()))), CHConfig.edgeBased("p2", new ShortestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage(), 60))), @@ -1483,7 +1477,7 @@ private void setTurnCost(int from, int via, int to, double cost) { private void setTurnCost(EdgeIteratorState inEdge, EdgeIteratorState outEdge, int viaNode, double cost) { double cost1 = cost >= maxCost ? Double.POSITIVE_INFINITY : cost; - graph.getTurnCostStorage().set(encoder.getDecimalEncodedValue(TurnCost.key("car")), inEdge.getEdge(), viaNode, outEdge.getEdge(), cost1); + graph.getTurnCostStorage().set(turnCostEnc, inEdge.getEdge(), viaNode, outEdge.getEdge(), cost1); } private EdgeIteratorState getEdge(int from, int to) { diff --git a/core/src/test/java/com/graphhopper/routing/ev/DecimalEncodedValueTest.java b/core/src/test/java/com/graphhopper/routing/ev/DecimalEncodedValueTest.java index 37ca18a0904..4aeb1d5ed6b 100644 --- a/core/src/test/java/com/graphhopper/routing/ev/DecimalEncodedValueTest.java +++ b/core/src/test/java/com/graphhopper/routing/ev/DecimalEncodedValueTest.java @@ -1,17 +1,11 @@ package com.graphhopper.routing.ev; -import com.graphhopper.reader.ReaderWay; -import com.graphhopper.reader.osm.conditional.DateRangeParser; -import com.graphhopper.routing.util.CarTagParser; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.storage.IntsRef; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; public class DecimalEncodedValueTest { @@ -26,33 +20,18 @@ public void testInit() { @Test public void testMaxValue() { - FlagEncoder carEncoder = FlagEncoders.createCar(new PMap("speed_bits=10|speed_factor=0.5")); - EncodingManager em = EncodingManager.create(carEncoder); - DecimalEncodedValue carAverageSpeedEnc = em.getDecimalEncodedValue(EncodingManager.getKey(carEncoder, "average_speed")); - CarTagParser carTagParser = new CarTagParser(em, new PMap()); - carTagParser.init(new DateRangeParser()); - - ReaderWay way = new ReaderWay(1); - way.setTag("highway", "motorway_link"); - way.setTag("maxspeed", "70 mph"); - IntsRef flags = carTagParser.handleWayTags(em.createEdgeFlags(), way); - assertEquals(101.5, carAverageSpeedEnc.getDecimal(true, flags), 1e-1); - - DecimalEncodedValue instance1 = new DecimalEncodedValueImpl("test1", 8, 0.5, false); - instance1.init(new EncodedValue.InitializerConfig()); - flags = em.createEdgeFlags(); - instance1.setDecimal(false, flags, 100d); - assertEquals(100, instance1.getDecimal(false, flags), 1e-1); + DecimalEncodedValue ev = new DecimalEncodedValueImpl("test1", 8, 0.5, false); + EncodingManager em = EncodingManager.start().add(ev).build(); + ev.init(new EncodedValue.InitializerConfig()); + IntsRef flags = em.createEdgeFlags(); + ev.setDecimal(false, flags, 100d); + assertEquals(100, ev.getDecimal(false, flags), 1e-1); } @Test public void testNegativeBounds() { DecimalEncodedValue prop = new DecimalEncodedValueImpl("test", 10, 5, false); prop.init(new EncodedValue.InitializerConfig()); - try { - prop.setDecimal(false, new IntsRef(1), -1); - assertTrue(false); - } catch (Exception ex) { - } + assertThrows(Exception.class, () -> prop.setDecimal(false, new IntsRef(1), -1)); } } \ No newline at end of file diff --git a/core/src/test/java/com/graphhopper/routing/lm/LandmarkStorageTest.java b/core/src/test/java/com/graphhopper/routing/lm/LandmarkStorageTest.java index f07a0c5e64f..70c5170ae3f 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/LandmarkStorageTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/LandmarkStorageTest.java @@ -226,7 +226,7 @@ public void testWeightingConsistence2() { @Test public void testWithBorderBlocking() { - RoutingAlgorithmTest.initBiGraph(graph, encoder); + RoutingAlgorithmTest.initBiGraph(graph, encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); LandmarkStorage storage = new LandmarkStorage(graph, encodingManager, new RAMDirectory(), new LMConfig("car", new FastestWeighting(accessEnc, speedEnc)), 2); final SplitArea right = new SplitArea(emptyList()); diff --git a/core/src/test/java/com/graphhopper/routing/util/AccessFilterTest.java b/core/src/test/java/com/graphhopper/routing/util/AccessFilterTest.java index ff4606da89c..1376773011e 100644 --- a/core/src/test/java/com/graphhopper/routing/util/AccessFilterTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/AccessFilterTest.java @@ -20,8 +20,7 @@ import com.carrotsearch.hppc.IntHashSet; import com.carrotsearch.hppc.IntSet; import com.graphhopper.routing.ch.PrepareEncoder; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.storage.*; @@ -31,10 +30,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class AccessFilterTest { - private final FlagEncoder encoder = FlagEncoders.createCar(); - private final BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - private final DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); - private final EncodingManager encodingManager = EncodingManager.create(encoder); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, true); + private final DecimalEncodedValue turnCostEnc = TurnCost.create("car", 1); + private final EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); private final BaseGraph graph = new BaseGraph.Builder(encodingManager) .withTurnCosts(true) .create(); @@ -49,7 +48,7 @@ public void testAccept_fwdLoopShortcut_acceptedByInExplorer() { GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(2, 0).setDistance(3)); graph.freeze(); // add loop shortcut in 'fwd' direction - CHConfig chConfig = CHConfig.edgeBased("profile", new ShortestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage()))); + CHConfig chConfig = CHConfig.edgeBased("profile", new ShortestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage()))); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(chStore); chBuilder.setIdentityLevels(); diff --git a/core/src/test/java/com/graphhopper/routing/util/CarTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/CarTagParserTest.java index 31f3ddb5bee..33db42dcd6b 100644 --- a/core/src/test/java/com/graphhopper/routing/util/CarTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/CarTagParserTest.java @@ -267,6 +267,12 @@ public void testMaxSpeed() { way.setTag("maxspeed", "none"); edgeFlags = parser.handleWayTags(edgeFlags, way); assertEquals(135, avSpeedEnc.getDecimal(false, edgeFlags), .1); + + way = new ReaderWay(1); + way.setTag("highway", "motorway_link"); + way.setTag("maxspeed", "70 mph"); + IntsRef flags = parser.handleWayTags(em.createEdgeFlags(), way); + assertEquals(100, avSpeedEnc.getDecimal(true, flags), 1e-1); } @Test diff --git a/core/src/test/java/com/graphhopper/routing/weighting/BlockAreaWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/BlockAreaWeightingTest.java index 69e9ae13a75..f9b4cb3b3e4 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/BlockAreaWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/BlockAreaWeightingTest.java @@ -3,11 +3,11 @@ import com.graphhopper.coll.GHIntHashSet; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.GraphEdgeIdFinder; import com.graphhopper.storage.index.LocationIndex; @@ -25,18 +25,15 @@ public class BlockAreaWeightingTest { - private FlagEncoder encoder = FlagEncoders.createCar(); - private EncodingManager em; private BooleanEncodedValue accessEnc; private DecimalEncodedValue speedEnc; private BaseGraph graph; @BeforeEach public void setUp() { - encoder = FlagEncoders.createCar(); - em = EncodingManager.create(encoder); - accessEnc = encoder.getAccessEnc(); - speedEnc = encoder.getAverageSpeedEnc(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); graph = new BaseGraph.Builder(em).create(); // 0-1 GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1)); diff --git a/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java index 6b928a7d610..d5d59e978fe 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java @@ -38,15 +38,15 @@ * @author Peter Karich */ public class FastestWeightingTest { - private final FlagEncoder encoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", 10)); - private final EncodingManager encodingManager = EncodingManager.create(encoder); - private final BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - private final DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + private final DecimalEncodedValue turnCostEnc = TurnCost.create("car", 10); + private final EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); @Test public void testMinWeightHasSameUnitAs_getWeight() { Weighting instance = new FastestWeighting(accessEnc, speedEnc); - IntsRef flags = GHUtility.setSpeed(encoder.getMaxSpeed(), 0, accessEnc, speedEnc, encodingManager.createEdgeFlags()); + IntsRef flags = GHUtility.setSpeed(140, 0, accessEnc, speedEnc, encodingManager.createEdgeFlags()); assertEquals(instance.getMinWeight(10), instance.calcEdgeWeight(createMockedEdgeIteratorState(10, flags), false), 1e-8); } @@ -78,7 +78,7 @@ public void testWeightWrongHeading() { public void testSpeed0() { Weighting instance = new FastestWeighting(accessEnc, speedEnc); IntsRef edgeFlags = encodingManager.createEdgeFlags(); - encoder.getAverageSpeedEnc().setDecimal(false, edgeFlags, 0); + speedEnc.setDecimal(false, edgeFlags, 0); assertEquals(1.0 / 0, instance.calcEdgeWeight(createMockedEdgeIteratorState(10, edgeFlags), false), 1e-8); // 0 / 0 returns NaN but calcWeight should not return NaN! @@ -105,10 +105,10 @@ public void testTime() { @Test public void calcWeightAndTime_withTurnCosts() { - BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); - Weighting weighting = new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage())); + BaseGraph graph = new BaseGraph.Builder(encodingManager).withTurnCosts(true).create(); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage())); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(100)); - EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(1, 2).setDistance(100)); + EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 2).setDistance(100)); // turn costs are given in seconds setTurnCost(graph, 0, 1, 2, 5); assertEquals(6 + 5, GHUtility.calcWeightWithTurnWeight(weighting, edge, false, 0), 1.e-6); @@ -117,8 +117,8 @@ public void calcWeightAndTime_withTurnCosts() { @Test public void calcWeightAndTime_uTurnCosts() { - BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); - Weighting weighting = new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage(), 40)); + BaseGraph graph = new BaseGraph.Builder(encodingManager).withTurnCosts(true).create(); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage(), 40)); EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(100)); assertEquals(6 + 40, GHUtility.calcWeightWithTurnWeight(weighting, edge, false, 0), 1.e-6); assertEquals((6 + 40) * 1000, GHUtility.calcMillisWithTurnMillis(weighting, edge, false, 0), 1.e-6); @@ -126,8 +126,8 @@ public void calcWeightAndTime_uTurnCosts() { @Test public void calcWeightAndTime_withTurnCosts_shortest() { - BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); - Weighting weighting = new ShortestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage())); + BaseGraph graph = new BaseGraph.Builder(encodingManager).withTurnCosts(true).create(); + Weighting weighting = new ShortestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage())); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(100)); EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 2).setDistance(100)); // turn costs are given in seconds @@ -198,7 +198,7 @@ public void testPrivateTag() { } private void setTurnCost(Graph graph, int from, int via, int to, double turnCost) { - graph.getTurnCostStorage().set(((EncodedValueLookup) encodingManager).getDecimalEncodedValue(TurnCost.key(encoder.toString())), getEdge(graph, from, via).getEdge(), via, getEdge(graph, via, to).getEdge(), turnCost); + graph.getTurnCostStorage().set(turnCostEnc, getEdge(graph, from, via).getEdge(), via, getEdge(graph, via, to).getEdge(), turnCost); } } diff --git a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomModelParserTest.java b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomModelParserTest.java index 79aefa7a3b1..429fb7b7e81 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomModelParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomModelParserTest.java @@ -20,8 +20,6 @@ import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.storage.BaseGraph; import com.graphhopper.util.CustomModel; import com.graphhopper.util.EdgeIteratorState; @@ -41,22 +39,23 @@ import static org.junit.jupiter.api.Assertions.*; class CustomModelParserTest { - - FlagEncoder encoder; BaseGraph graph; EncodingManager encodingManager; EnumEncodedValue roadClassEnc; + BooleanEncodedValue accessEnc; DecimalEncodedValue avgSpeedEnc; StringEncodedValue countryEnc; + double maxSpeed; @BeforeEach void setup() { - encoder = FlagEncoders.createCar(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + avgSpeedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); countryEnc = new StringEncodedValue("country", 10); - encodingManager = new EncodingManager.Builder().add(encoder).add(countryEnc).add(new EnumEncodedValue<>(Surface.KEY, Surface.class)).build(); + encodingManager = new EncodingManager.Builder().add(accessEnc).add(avgSpeedEnc).add(countryEnc).add(new EnumEncodedValue<>(Surface.KEY, Surface.class)).build(); graph = new BaseGraph.Builder(encodingManager).create(); - avgSpeedEnc = encoder.getAverageSpeedEnc(); roadClassEnc = encodingManager.getEnumEncodedValue(RoadClass.KEY, RoadClass.class); + maxSpeed = 140; } @Test @@ -64,7 +63,7 @@ void setPriorityForRoadClass() { CustomModel customModel = new CustomModel(); customModel.addToPriority(If("road_class == PRIMARY", MULTIPLY, 0.5)); CustomWeighting.EdgeToDoubleMapping priorityMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager, - avgSpeedEnc, encoder.getMaxSpeed(), null).getEdgeToPriorityMapping(); + avgSpeedEnc, maxSpeed, null).getEdgeToPriorityMapping(); BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); EdgeIteratorState edge1 = graph.edge(0, 1).setDistance(100).set(roadClassEnc, RoadClass.PRIMARY); @@ -77,11 +76,11 @@ void setPriorityForRoadClass() { @Test void testPriority() { EdgeIteratorState primary = graph.edge(0, 1).setDistance(10). - set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80).set(encoder.getAccessEnc(), true, true); + set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80).set(accessEnc, true, true); EdgeIteratorState secondary = graph.edge(1, 2).setDistance(10). - set(roadClassEnc, SECONDARY).set(avgSpeedEnc, 70).set(encoder.getAccessEnc(), true, true); + set(roadClassEnc, SECONDARY).set(avgSpeedEnc, 70).set(accessEnc, true, true); EdgeIteratorState tertiary = graph.edge(1, 2).setDistance(10). - set(roadClassEnc, TERTIARY).set(avgSpeedEnc, 70).set(encoder.getAccessEnc(), true, true); + set(roadClassEnc, TERTIARY).set(avgSpeedEnc, 70).set(accessEnc, true, true); CustomModel customModel = new CustomModel(); customModel.addToPriority(If("road_class == PRIMARY", MULTIPLY, 0.5)); @@ -90,7 +89,7 @@ void testPriority() { customModel.addToPriority(If("road_environment != FERRY", MULTIPLY, 0.8)); CustomWeighting.EdgeToDoubleMapping priorityMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager, - avgSpeedEnc, encoder.getMaxSpeed(), null).getEdgeToPriorityMapping(); + avgSpeedEnc, maxSpeed, null).getEdgeToPriorityMapping(); assertEquals(0.5 * 0.8, priorityMapping.get(primary, false), 0.01); assertEquals(0.7 * 0.8, priorityMapping.get(secondary, false), 0.01); @@ -101,22 +100,22 @@ void testPriority() { customModel.addToPriority(If("road_class == PRIMARY", MULTIPLY, 1)); customModel.addToPriority(If("road_class == SECONDARY", MULTIPLY, 0.9)); priorityMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager, - avgSpeedEnc, encoder.getMaxSpeed(), null).getEdgeToPriorityMapping(); + avgSpeedEnc, maxSpeed, null).getEdgeToPriorityMapping(); assertEquals(1, priorityMapping.get(primary, false), 0.01); assertEquals(0.9, priorityMapping.get(secondary, false), 0.01); } @Test public void testBrackets() { - EdgeIteratorState primary = graph.edge(0, 1).setDistance(10).set(encoder.getAccessEnc(), true, true). + EdgeIteratorState primary = graph.edge(0, 1).setDistance(10).set(accessEnc, true, true). set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80); - EdgeIteratorState secondary = graph.edge(0, 1).setDistance(10).set(encoder.getAccessEnc(), true, true). + EdgeIteratorState secondary = graph.edge(0, 1).setDistance(10).set(accessEnc, true, true). set(roadClassEnc, SECONDARY).set(avgSpeedEnc, 40); CustomModel customModel = new CustomModel(); customModel.addToPriority(If("(road_class == PRIMARY || car_access == true) && car_average_speed > 50", MULTIPLY, 0.9)); CustomWeighting.Parameters parameters = CustomModelParser.createWeightingParameters(customModel, encodingManager, - avgSpeedEnc, encoder.getMaxSpeed(), null); + avgSpeedEnc, maxSpeed, null); assertEquals(0.9, parameters.getEdgeToPriorityMapping().get(primary, false), 0.01); assertEquals(1, parameters.getEdgeToPriorityMapping().get(secondary, false), 0.01); } @@ -124,15 +123,15 @@ public void testBrackets() { @Test public void testSpeedFactorAndPriorityAndMaxSpeed() { EdgeIteratorState primary = graph.edge(0, 1).setDistance(10). - set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80).set(encoder.getAccessEnc(), true, true); + set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80).set(accessEnc, true, true); EdgeIteratorState secondary = graph.edge(1, 2).setDistance(10). - set(roadClassEnc, SECONDARY).set(avgSpeedEnc, 70).set(encoder.getAccessEnc(), true, true); + set(roadClassEnc, SECONDARY).set(avgSpeedEnc, 70).set(accessEnc, true, true); CustomModel customModel = new CustomModel(); customModel.addToPriority(If("road_class == PRIMARY", MULTIPLY, 0.9)); customModel.addToSpeed(If("road_class == PRIMARY", MULTIPLY, 0.8)); CustomWeighting.Parameters parameters = CustomModelParser.createWeightingParameters(customModel, encodingManager, - avgSpeedEnc, encoder.getMaxSpeed(), null); + avgSpeedEnc, maxSpeed, null); assertEquals(0.9, parameters.getEdgeToPriorityMapping().get(primary, false), 0.01); assertEquals(64, parameters.getEdgeToSpeedMapping().get(primary, false), 0.01); @@ -141,7 +140,7 @@ public void testSpeedFactorAndPriorityAndMaxSpeed() { customModel.addToSpeed(If("road_class != PRIMARY", LIMIT, 50)); CustomWeighting.EdgeToDoubleMapping speedMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager, - avgSpeedEnc, encoder.getMaxSpeed(), null).getEdgeToSpeedMapping(); + avgSpeedEnc, maxSpeed, null).getEdgeToSpeedMapping(); assertEquals(64, speedMapping.get(primary, false), 0.01); assertEquals(50, speedMapping.get(secondary, false), 0.01); } @@ -149,16 +148,16 @@ public void testSpeedFactorAndPriorityAndMaxSpeed() { @Test public void testString() { EdgeIteratorState deu = graph.edge(0, 1).setDistance(10). - set(countryEnc, "DEU").set(avgSpeedEnc, 80).set(encoder.getAccessEnc(), true, true); + set(countryEnc, "DEU").set(avgSpeedEnc, 80).set(accessEnc, true, true); EdgeIteratorState blup = graph.edge(1, 2).setDistance(10). - set(countryEnc, "blup").set(avgSpeedEnc, 70).set(encoder.getAccessEnc(), true, true); + set(countryEnc, "blup").set(avgSpeedEnc, 70).set(accessEnc, true, true); CustomModel customModel = new CustomModel(); customModel.addToPriority(If("country == \"DEU\"", MULTIPLY, 0.9)); customModel.addToPriority(ElseIf("country == \"blup\"", MULTIPLY, 0.7)); customModel.addToPriority(Else(MULTIPLY, 0.5)); CustomWeighting.EdgeToDoubleMapping priorityMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager, - avgSpeedEnc, encoder.getMaxSpeed(), null).getEdgeToPriorityMapping(); + avgSpeedEnc, maxSpeed, null).getEdgeToPriorityMapping(); assertEquals(0.9, priorityMapping.get(deu, false), 0.01); assertEquals(0.7, priorityMapping.get(blup, false), 0.01); } @@ -169,13 +168,13 @@ void testIllegalOrder() { customModel.addToPriority(Else(MULTIPLY, 0.9)); customModel.addToPriority(If("road_environment != FERRY", MULTIPLY, 0.8)); assertThrows(IllegalArgumentException.class, () -> CustomModelParser.createWeightingParameters(customModel, encodingManager, - avgSpeedEnc, encoder.getMaxSpeed(), null)); + avgSpeedEnc, maxSpeed, null)); CustomModel customModel2 = new CustomModel(); customModel2.addToPriority(ElseIf("road_environment != FERRY", MULTIPLY, 0.9)); customModel2.addToPriority(If("road_class != PRIMARY", MULTIPLY, 0.8)); assertThrows(IllegalArgumentException.class, () -> CustomModelParser.createWeightingParameters(customModel2, encodingManager, - avgSpeedEnc, encoder.getMaxSpeed(), null)); + avgSpeedEnc, maxSpeed, null)); } @Test @@ -215,7 +214,7 @@ public void multipleAreas() { // No exception is thrown during createWeightingParameters assertAll(() -> CustomModelParser.createWeightingParameters(customModel, encodingManager, - avgSpeedEnc, encoder.getMaxSpeed(), null)); + avgSpeedEnc, maxSpeed, null)); CustomModel customModel2 = new CustomModel(); customModel2.setAreas(areas); @@ -227,6 +226,6 @@ public void multipleAreas() { assertThrows(IllegalArgumentException.class, () -> CustomModelParser.createWeightingParameters(customModel2, encodingManager, - avgSpeedEnc, encoder.getMaxSpeed(), null)); + avgSpeedEnc, maxSpeed, null)); } } \ No newline at end of file diff --git a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java index 83f619c6561..daf27f0a873 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java @@ -5,12 +5,13 @@ import com.graphhopper.json.Statement; import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.BaseGraph; -import com.graphhopper.util.*; +import com.graphhopper.util.CustomModel; +import com.graphhopper.util.EdgeIteratorState; +import com.graphhopper.util.GHUtility; +import com.graphhopper.util.JsonFeature; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -28,21 +29,21 @@ class CustomWeightingTest { DecimalEncodedValue maxSpeedEnc; EnumEncodedValue roadClassEnc; EncodingManager encodingManager; - FlagEncoder carFE; + double maxSpeed; @BeforeEach public void setup() { - carFE = FlagEncoders.createCar(new PMap().putObject("speed_two_directions", true)); - encodingManager = new EncodingManager.Builder().add(carFE) + accessEnc = new SimpleBooleanEncodedValue("car_access", true); + avSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, true); + encodingManager = new EncodingManager.Builder().add(accessEnc).add(avSpeedEnc) .add(new EnumEncodedValue<>(Toll.KEY, Toll.class)) .add(new EnumEncodedValue<>(Hazmat.KEY, Hazmat.class)) .add(new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class)) .build(); - avSpeedEnc = carFE.getAverageSpeedEnc(); - accessEnc = carFE.getAccessEnc(); maxSpeedEnc = encodingManager.getDecimalEncodedValue(MaxSpeed.KEY); roadClassEnc = encodingManager.getEnumEncodedValue(KEY, RoadClass.class); graph = new BaseGraph.Builder(encodingManager).create(); + maxSpeed = 140; } @Test @@ -117,19 +118,21 @@ public void testSpeedFactorBooleanEV() { @Test public void testBoolean() { - carFE = FlagEncoders.createCar(); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue avSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); BooleanEncodedValue specialEnc = new SimpleBooleanEncodedValue("special", true); - encodingManager = new EncodingManager.Builder().add(carFE).add(specialEnc).build(); + encodingManager = new EncodingManager.Builder().add(accessEnc).add(avSpeedEnc).add(specialEnc).build(); graph = new BaseGraph.Builder(encodingManager).create(); EdgeIteratorState edge = graph.edge(0, 1).set(accessEnc, true).setReverse(accessEnc, true). set(avSpeedEnc, 15).set(specialEnc, false).setReverse(specialEnc, true).setDistance(10); CustomModel vehicleModel = new CustomModel(); - assertEquals(3.1, createWeighting(vehicleModel).calcEdgeWeight(edge, false), 0.01); + Weighting weighting = CustomModelParser.createWeighting(accessEnc, avSpeedEnc, null, maxSpeed, encodingManager, NO_TURN_COST_PROVIDER, vehicleModel); + assertEquals(3.1, weighting.calcEdgeWeight(edge, false), 0.01); vehicleModel.addToPriority(If("special == true", MULTIPLY, 0.8)); vehicleModel.addToPriority(If("special == false", MULTIPLY, 0.4)); - Weighting weighting = createWeighting(vehicleModel); + weighting = CustomModelParser.createWeighting(accessEnc, avSpeedEnc, null, maxSpeed, encodingManager, NO_TURN_COST_PROVIDER, vehicleModel); assertEquals(6.7, weighting.calcEdgeWeight(edge, false), 0.01); assertEquals(3.7, weighting.calcEdgeWeight(edge, true), 0.01); } @@ -242,7 +245,7 @@ public void testArea() throws Exception { @Test public void testMaxSpeed() { - assertEquals(140, carFE.getMaxSpeed(), 0.1); + assertEquals(140, maxSpeed, 0.1); assertEquals(1000.0 / 72 * 3.6, createWeighting(new CustomModel(). addToSpeed(If("true", LIMIT, 72)).setDistanceInfluence(0)).getMinWeight(1000)); @@ -300,6 +303,6 @@ public void maxSpeedViolated_bug_2307() { } private Weighting createWeighting(CustomModel vehicleModel) { - return CustomModelParser.createWeighting(carFE, encodingManager, NO_TURN_COST_PROVIDER, vehicleModel); + return CustomModelParser.createWeighting(accessEnc, avSpeedEnc, null, maxSpeed, encodingManager, NO_TURN_COST_PROVIDER, vehicleModel); } } \ No newline at end of file diff --git a/core/src/test/java/com/graphhopper/util/GHUtilityTest.java b/core/src/test/java/com/graphhopper/util/GHUtilityTest.java index aed7727c678..fedbc43b96a 100644 --- a/core/src/test/java/com/graphhopper/util/GHUtilityTest.java +++ b/core/src/test/java/com/graphhopper/util/GHUtilityTest.java @@ -49,7 +49,7 @@ BaseGraph createGraph() { // 6 \1 // ______/ // 0/ - Graph initUnsorted(Graph g, FlagEncoder encoder) { + Graph initUnsorted(Graph g, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { NodeAccess na = g.getNodeAccess(); na.setNode(0, 0, 1); na.setNode(1, 2.5, 4.5); @@ -60,8 +60,6 @@ Graph initUnsorted(Graph g, FlagEncoder encoder) { na.setNode(6, 2.3, 2.2); na.setNode(7, 5, 1.5); na.setNode(8, 4.6, 4); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(8, 2).setDistance(0.5)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(7, 3).setDistance(2.1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(1, 0).setDistance(3.9)); @@ -86,7 +84,7 @@ Graph initUnsorted(Graph g, FlagEncoder encoder) { @Test public void testSort() { - Graph g = initUnsorted(createGraph(), carEncoder); + Graph g = initUnsorted(createGraph(), carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); Graph newG = GHUtility.sortDFS(g, createGraph()); assertEquals(g.getNodes(), newG.getNodes()); assertEquals(g.getEdges(), newG.getEdges()); diff --git a/core/src/test/java/com/graphhopper/util/InstructionListTest.java b/core/src/test/java/com/graphhopper/util/InstructionListTest.java index ae1fe1deb75..67346b66800 100644 --- a/core/src/test/java/com/graphhopper/util/InstructionListTest.java +++ b/core/src/test/java/com/graphhopper/util/InstructionListTest.java @@ -410,7 +410,9 @@ public void testInstructionIfSlightTurnForCustomProfile() { pointList.add(43.729627, 7.41749); GHUtility.setSpeed(5, true, true, foot.getAccessEnc(), foot.getAverageSpeedEnc(), g.edge(2, 4).setDistance(20).setName("myroad").set(priorityEnc, 1).setWayGeometry(pointList)); - Weighting weighting = CustomModelParser.createWeighting(foot, tmpEM, DefaultTurnCostProvider.NO_TURN_COST_PROVIDER, new CustomModel().setDistanceInfluence(0)); + Weighting weighting = CustomModelParser.createWeighting(foot.getAccessEnc(), foot.getAverageSpeedEnc(), + foot.getPriorityEnc(), foot.getMaxSpeed(), tmpEM, DefaultTurnCostProvider.NO_TURN_COST_PROVIDER, + new CustomModel().setDistanceInfluence(0)); Path p = new Dijkstra(g, weighting, tMode).calcPath(4, 3); assertTrue(p.isFound()); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, tmpEM, usTR); @@ -458,7 +460,8 @@ public void testInstructionWithHighlyCustomProfileWithRoadsBase() { CustomModel customModel = new CustomModel(); customModel.addToPriority(Statement.If("road_class == PEDESTRIAN", Statement.Op.MULTIPLY, 0)); - Weighting weighting = CustomModelParser.createWeighting(roads, tmpEM, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); + Weighting weighting = CustomModelParser.createWeighting(roadsAccessEnc, roadsSpeedEnc, roads.getPriorityEnc(), + roads.getMaxSpeed(), tmpEM, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); Path p = new Dijkstra(g, weighting, tMode).calcPath(3, 4); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, tmpEM, usTR); List tmpList = getTurnDescriptions(wayList); From e1085443395e7d4dad18983e6bac137321af5d6a Mon Sep 17 00:00:00 2001 From: easbar Date: Wed, 22 Jun 2022 20:28:30 +0200 Subject: [PATCH 03/28] Remove more flag encoders from tests --- .../algorithm/ShortestPathTreeTest.java | 10 +- .../routing/RoutingCHGraphImplTest.java | 92 +++++++++---------- ...fficChangeWithNodeOrderingReusingTest.java | 22 +++-- .../ch/NodeBasedNodeContractorTest.java | 25 ++--- .../ch/PrepareContractionHierarchiesTest.java | 28 ++---- .../routing/lm/LandmarkStorageTest.java | 16 +--- .../util/NameSimilarityEdgeFilterTest.java | 22 +++-- .../weighting/ShortFastestWeightingTest.java | 26 +++--- .../storage/GraphEdgeIdFinderTest.java | 20 ++-- .../storage/ShortcutUnpackerTest.java | 30 +++--- .../graphhopper/gpx/GpxConversionsTest.java | 14 +-- 11 files changed, 154 insertions(+), 151 deletions(-) diff --git a/core/src/test/java/com/graphhopper/isochrone/algorithm/ShortestPathTreeTest.java b/core/src/test/java/com/graphhopper/isochrone/algorithm/ShortestPathTreeTest.java index 83a4204c086..dc1939bdf31 100644 --- a/core/src/test/java/com/graphhopper/isochrone/algorithm/ShortestPathTreeTest.java +++ b/core/src/test/java/com/graphhopper/isochrone/algorithm/ShortestPathTreeTest.java @@ -2,9 +2,10 @@ import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.AllEdgesIterator; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.TurnCostProvider; @@ -59,10 +60,9 @@ public long calcTurnMillis(int inEdge, int viaNode, int outEdge) { }; - private final EncodingManager encodingManager = EncodingManager.create("car"); - private final FlagEncoder carEncoder = encodingManager.getEncoder("car"); - private final BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - private final DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + private final EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); private BaseGraph graph; diff --git a/core/src/test/java/com/graphhopper/routing/RoutingCHGraphImplTest.java b/core/src/test/java/com/graphhopper/routing/RoutingCHGraphImplTest.java index 7bb907efe1f..0d07bb0a463 100644 --- a/core/src/test/java/com/graphhopper/routing/RoutingCHGraphImplTest.java +++ b/core/src/test/java/com/graphhopper/routing/RoutingCHGraphImplTest.java @@ -21,10 +21,10 @@ import com.graphhopper.routing.ch.PrepareEncoder; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.AccessFilter; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.storage.*; import com.graphhopper.util.EdgeExplorer; @@ -38,14 +38,15 @@ public class RoutingCHGraphImplTest { @Test public void testBaseAndCHEdges() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(carEncoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); graph.edge(1, 0); graph.edge(8, 9); graph.freeze(); - CHConfig chConfig = CHConfig.nodeBased("p", new FastestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc())); + CHConfig chConfig = CHConfig.nodeBased("p", new FastestWeighting(accessEnc, speedEnc)); CHStorage store = CHStorage.fromGraph(graph, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(store); chBuilder.setIdentityLevels(); @@ -76,14 +77,15 @@ void testShortcutConnection() { // 4 ------ 1 > 0 // ^ \ // 3 2 - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); - EdgeExplorer baseCarOutExplorer = graph.createEdgeExplorer(AccessFilter.outEdges(encoder.getAccessEnc())); - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(4, 1).setDistance(30)); + EdgeExplorer baseCarOutExplorer = graph.createEdgeExplorer(AccessFilter.outEdges(accessEnc)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(4, 1).setDistance(30)); graph.freeze(); - CHConfig chConfig = CHConfig.nodeBased("ch", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc())); + CHConfig chConfig = CHConfig.nodeBased("ch", new FastestWeighting(accessEnc, speedEnc)); CHStorage store = CHStorage.fromGraph(graph, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(store); chBuilder.setIdentityLevels(); @@ -113,14 +115,15 @@ void testShortcutConnection() { @Test public void testGetWeight() { - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); EdgeIteratorState edge1 = graph.edge(0, 1); EdgeIteratorState edge2 = graph.edge(1, 2); graph.freeze(); - CHConfig chConfig = CHConfig.nodeBased("ch", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc())); + CHConfig chConfig = CHConfig.nodeBased("ch", new FastestWeighting(accessEnc, speedEnc)); CHStorage store = CHStorage.fromGraph(graph, chConfig); RoutingCHGraph g = RoutingCHGraphImpl.fromGraph(graph, store, chConfig); assertFalse(g.getEdgeIteratorState(edge1.getEdge(), Integer.MIN_VALUE).isShortcut()); @@ -140,13 +143,14 @@ public void testGetWeight() { @Test public void testGetWeightIfAdvancedEncoder() { - FlagEncoder customEncoder = FlagEncoders.createBike2(); - EncodingManager em = EncodingManager.create(customEncoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 4, 2, true); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph ghStorage = new BaseGraph.Builder(em).create(); ghStorage.edge(0, 3); ghStorage.freeze(); - FastestWeighting weighting = new FastestWeighting(customEncoder.getAccessEnc(), customEncoder.getAverageSpeedEnc()); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased("p1", weighting); CHStorage chStore = CHStorage.fromGraph(ghStorage, chConfig); CHStorageBuilder chBuilder = new CHStorageBuilder(chStore); @@ -167,11 +171,10 @@ public void testGetWeightIfAdvancedEncoder() { @Test public void testWeightExact() { - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(1, 2).setDistance(1)); graph.freeze(); @@ -193,12 +196,11 @@ public void testWeightExact() { @Test public void testSimpleShortcutCreationAndTraversal() { - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 3).setDistance(10)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(3, 4).setDistance(10)); graph.freeze(); @@ -219,11 +221,10 @@ public void testSimpleShortcutCreationAndTraversal() { @Test public void testAddShortcutSkippedEdgesWriteRead() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(carEncoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); final EdgeIteratorState edge1 = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 3).setDistance(10)); final EdgeIteratorState edge2 = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(3, 4).setDistance(10)); graph.freeze(); @@ -242,11 +243,10 @@ public void testAddShortcutSkippedEdgesWriteRead() { @Test public void testSkippedEdges() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(carEncoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); final EdgeIteratorState edge1 = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 3).setDistance(10)); final EdgeIteratorState edge2 = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(3, 4).setDistance(10)); graph.freeze(); @@ -263,12 +263,11 @@ public void testSkippedEdges() { @Test public void testAddShortcut_edgeBased_throwsIfNotConfiguredForEdgeBased() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(carEncoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(1, 2).setDistance(1)); graph.freeze(); @@ -283,11 +282,10 @@ public void testAddShortcut_edgeBased_throwsIfNotConfiguredForEdgeBased() { @Test public void testAddShortcut_edgeBased() { // 0 -> 1 -> 2 - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(carEncoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).set3D(true).create(); - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(1, 2).setDistance(3)); graph.freeze(); @@ -305,11 +303,12 @@ public void testAddShortcut_edgeBased() { @Test public void outOfBounds() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(carEncoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).set3D(true).create(); graph.freeze(); - FastestWeighting weighting = new FastestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased("p1", weighting); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); RoutingCHGraph lg = RoutingCHGraphImpl.fromGraph(graph, chStore, chConfig); @@ -318,11 +317,10 @@ public void outOfBounds() { @Test public void testGetEdgeIterator() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(carEncoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).set3D(true).create(); - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, graph.edge(1, 2).setDistance(1)); graph.freeze(); diff --git a/core/src/test/java/com/graphhopper/routing/TrafficChangeWithNodeOrderingReusingTest.java b/core/src/test/java/com/graphhopper/routing/TrafficChangeWithNodeOrderingReusingTest.java index 14c11af08b9..e677b9d9149 100644 --- a/core/src/test/java/com/graphhopper/routing/TrafficChangeWithNodeOrderingReusingTest.java +++ b/core/src/test/java/com/graphhopper/routing/TrafficChangeWithNodeOrderingReusingTest.java @@ -4,7 +4,14 @@ import com.graphhopper.reader.osm.conditional.DateRangeParser; import com.graphhopper.routing.ch.CHRoutingAlgorithmFactory; import com.graphhopper.routing.ch.PrepareContractionHierarchies; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; +import com.graphhopper.routing.util.CarTagParser; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.OSMParsers; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.AbstractWeighting; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.TurnCostProvider; @@ -51,14 +58,15 @@ private static class Fixture { public Fixture(int maxDeviationPercentage) { this.maxDeviationPercentage = maxDeviationPercentage; - FlagEncoder encoder = FlagEncoders.createCar(); - em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); CarTagParser carParser = new CarTagParser(em, new PMap()); carParser.init(new DateRangeParser()); osmParsers = new OSMParsers() .addVehicleTagParser(carParser); - baseCHConfig = CHConfig.nodeBased("base", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc())); - trafficCHConfig = CHConfig.nodeBased("traffic", new RandomDeviationWeighting(baseCHConfig.getWeighting(), encoder, maxDeviationPercentage)); + baseCHConfig = CHConfig.nodeBased("base", new FastestWeighting(accessEnc, speedEnc)); + trafficCHConfig = CHConfig.nodeBased("traffic", new RandomDeviationWeighting(baseCHConfig.getWeighting(), accessEnc, speedEnc, maxDeviationPercentage)); graph = new BaseGraph.Builder(em).create(); } @@ -193,8 +201,8 @@ private static class RandomDeviationWeighting extends AbstractWeighting { private final Weighting baseWeighting; private final double maxDeviationPercentage; - public RandomDeviationWeighting(Weighting baseWeighting, FlagEncoder encoder, double maxDeviationPercentage) { - super(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), TurnCostProvider.NO_TURN_COST_PROVIDER); + public RandomDeviationWeighting(Weighting baseWeighting, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, double maxDeviationPercentage) { + super(accessEnc, speedEnc, TurnCostProvider.NO_TURN_COST_PROVIDER); this.baseWeighting = baseWeighting; this.maxDeviationPercentage = maxDeviationPercentage; } diff --git a/core/src/test/java/com/graphhopper/routing/ch/NodeBasedNodeContractorTest.java b/core/src/test/java/com/graphhopper/routing/ch/NodeBasedNodeContractorTest.java index 61b3ae7fe5c..fa5bf1b4bf3 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/NodeBasedNodeContractorTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/NodeBasedNodeContractorTest.java @@ -22,7 +22,11 @@ import com.graphhopper.routing.Path; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; +import com.graphhopper.routing.util.AllEdgesIterator; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.routing.weighting.Weighting; @@ -42,10 +46,9 @@ import static org.junit.jupiter.api.Assertions.*; public class NodeBasedNodeContractorTest { - private final FlagEncoder encoder = FlagEncoders.createCar(); - private final EncodingManager encodingManager = EncodingManager.create(encoder); - private final BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - private final DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + private final EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); private final Weighting weighting = new ShortestWeighting(accessEnc, speedEnc); private final BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); private final CHConfig chConfig = CHConfig.nodeBased("profile", weighting); @@ -266,8 +269,9 @@ public void testNodeContraction_shortcutDistanceRounding() { */ @Test public void testNodeContraction_shortcutWeightRounding() { - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager encodingManager = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); // 0 ------------> 4 // \ / @@ -305,10 +309,9 @@ public void testNodeContraction_shortcutWeightRounding() { public void testNodeContraction_preventUnnecessaryShortcutWithLoop() { // there should not be shortcuts where one of the skipped edges is a loop at the node to be contracted, // see also #1583 - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager encodingManager = EncodingManager.create(encoder); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); // 0 - 1 - 2 - 3 // o o diff --git a/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java b/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java index e05540acff0..92533b95b99 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java @@ -54,9 +54,7 @@ public class PrepareContractionHierarchiesTest { // | ^ \ // | | | // 17-16-...-11<-/ - private static void initDirected2(Graph g, FlagEncoder encoder) { - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); + private static void initDirected2(Graph g, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 1).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(2, 3).setDistance(1)); @@ -79,9 +77,7 @@ private static void initDirected2(Graph g, FlagEncoder encoder) { } // prepare-routing.svg - private static void initShortcutsGraph(Graph g, FlagEncoder encoder) { - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); + private static void initShortcutsGraph(Graph g, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 1).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 2).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(1)); @@ -106,15 +102,13 @@ private static void initShortcutsGraph(Graph g, FlagEncoder encoder) { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(14, 16).setDistance(1)); } - private static void initExampleGraph(Graph g, FlagEncoder encoder) { + private static void initExampleGraph(Graph g, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { //5-1-----2 // \ __/| // 0 | // / | // 4-----3 // - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 1).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 2).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 4).setDistance(3)); @@ -142,7 +136,7 @@ public void testReturnsCorrectWeighting() { @Test public void testAddShortcuts() { - initExampleGraph(g, carEncoder); + initExampleGraph(g, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g); useNodeOrdering(prepare, new int[]{5, 3, 4, 0, 1, 2}); PrepareContractionHierarchies.Result res = prepare.doWork(); @@ -151,7 +145,7 @@ public void testAddShortcuts() { @Test public void testMoreComplexGraph() { - initShortcutsGraph(g, carEncoder); + initShortcutsGraph(g, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g); useNodeOrdering(prepare, new int[]{0, 5, 6, 7, 8, 10, 11, 13, 15, 1, 3, 9, 14, 16, 12, 4, 2}); PrepareContractionHierarchies.Result res = prepare.doWork(); @@ -183,7 +177,7 @@ public void testDirectedGraph() { @Test public void testDirectedGraph2() { - initDirected2(g, carEncoder); + initDirected2(g, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); int oldCount = g.getEdges(); assertEquals(19, oldCount); PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g); @@ -203,7 +197,7 @@ public void testDirectedGraph2() { assertEquals(IntArrayList.from(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), p.calcNodes()); } - private static void initRoundaboutGraph(Graph g, FlagEncoder encoder) { + private static void initRoundaboutGraph(Graph g, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { // roundabout: //16-0-9-10--11 12<-13 // \ \ / \ @@ -211,8 +205,6 @@ private static void initRoundaboutGraph(Graph g, FlagEncoder encoder) { // -15-1--2--3--4 / / // / \-5->6/ / // -14 \________/ - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(16, 0).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 9).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 17).setDistance(1)); @@ -257,7 +249,7 @@ private static void initRoundaboutGraph(Graph g, FlagEncoder encoder) { @Test public void testRoundaboutUnpacking() { - initRoundaboutGraph(g, carEncoder); + initRoundaboutGraph(g, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); int oldCount = g.getEdges(); PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g); useNodeOrdering(prepare, new int[]{26, 6, 12, 13, 2, 3, 8, 9, 10, 11, 14, 15, 16, 17, 18, 20, 21, 23, 24, 25, 19, 22, 27, 5, 29, 30, 31, 28, 7, 1, 0, 4}); @@ -492,7 +484,7 @@ public void testMultiplePreparationsIdenticalView() { CHConfig bikeProfile = CHConfig.nodeBased("c2", new ShortestWeighting(tmpBikeEncoder.getAccessEnc(), tmpBikeEncoder.getAverageSpeedEnc())); BaseGraph graph = new BaseGraph.Builder(tmpEncodingManager).create(); - initShortcutsGraph(graph, tmpCarEncoder); + initShortcutsGraph(graph, tmpCarEncoder.getAccessEnc(), tmpCarEncoder.getAverageSpeedEnc()); AllEdgesIterator iter = graph.getAllEdges(); while (iter.next()) { GHUtility.setSpeed(18, true, true, tmpBikeEncoder.getAccessEnc(), tmpBikeEncoder.getAverageSpeedEnc(), iter); @@ -513,7 +505,7 @@ public void testMultiplePreparationsDifferentView() { CHConfig bikeConfig = CHConfig.nodeBased("c2", new FastestWeighting(tmpBikeEncoder.getAccessEnc(), tmpBikeEncoder.getAverageSpeedEnc())); BaseGraph graph = new BaseGraph.Builder(tmpEncodingManager).create(); - initShortcutsGraph(graph, tmpCarEncoder); + initShortcutsGraph(graph, tmpCarEncoder.getAccessEnc(), tmpCarEncoder.getAverageSpeedEnc()); AllEdgesIterator iter = graph.getAllEdges(); while (iter.next()) { GHUtility.setSpeed(18, true, true, tmpBikeEncoder.getAccessEnc(), tmpBikeEncoder.getAverageSpeedEnc(), iter); diff --git a/core/src/test/java/com/graphhopper/routing/lm/LandmarkStorageTest.java b/core/src/test/java/com/graphhopper/routing/lm/LandmarkStorageTest.java index 70c5170ae3f..cac17ab4678 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/LandmarkStorageTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/LandmarkStorageTest.java @@ -18,14 +18,10 @@ package com.graphhopper.routing.lm; import com.graphhopper.routing.RoutingAlgorithmTest; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.Subnetwork; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks; import com.graphhopper.routing.util.AreaIndex; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.BaseGraph; @@ -49,7 +45,6 @@ */ public class LandmarkStorageTest { private BaseGraph graph; - private FlagEncoder encoder; private BooleanEncodedValue subnetworkEnc; private EncodingManager encodingManager; private BooleanEncodedValue accessEnc; @@ -57,12 +52,11 @@ public class LandmarkStorageTest { @BeforeEach public void setUp() { - encoder = FlagEncoders.createCar(); subnetworkEnc = Subnetwork.create("car"); - encodingManager = new EncodingManager.Builder().add(encoder).add(subnetworkEnc).build(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(subnetworkEnc).build(); graph = new BaseGraph.Builder(encodingManager).create(); - accessEnc = encoder.getAccessEnc(); - speedEnc = encoder.getAverageSpeedEnc(); } @AfterEach @@ -226,7 +220,7 @@ public void testWeightingConsistence2() { @Test public void testWithBorderBlocking() { - RoutingAlgorithmTest.initBiGraph(graph, encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); + RoutingAlgorithmTest.initBiGraph(graph, accessEnc, speedEnc); LandmarkStorage storage = new LandmarkStorage(graph, encodingManager, new RAMDirectory(), new LMConfig("car", new FastestWeighting(accessEnc, speedEnc)), 2); final SplitArea right = new SplitArea(emptyList()); diff --git a/core/src/test/java/com/graphhopper/routing/util/NameSimilarityEdgeFilterTest.java b/core/src/test/java/com/graphhopper/routing/util/NameSimilarityEdgeFilterTest.java index 41ddc7ed379..9b11170f4f9 100644 --- a/core/src/test/java/com/graphhopper/routing/util/NameSimilarityEdgeFilterTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/NameSimilarityEdgeFilterTest.java @@ -17,9 +17,15 @@ */ package com.graphhopper.routing.util; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.NodeAccess; -import com.graphhopper.util.*; +import com.graphhopper.util.EdgeIteratorState; +import com.graphhopper.util.FetchMode; +import com.graphhopper.util.GHUtility; +import com.graphhopper.util.PointList; import com.graphhopper.util.shapes.GHPoint; import org.junit.jupiter.api.Test; @@ -81,8 +87,7 @@ public void testAccept() { @Test public void testDistanceFiltering() { - FlagEncoder encoder = FlagEncoders.createCar(); - BaseGraph g = new BaseGraph.Builder(EncodingManager.create(encoder)).create(); + BaseGraph g = new BaseGraph.Builder(1).create(); NodeAccess na = g.getNodeAccess(); GHPoint pointFarAway = new GHPoint(49.458629, 11.146124); @@ -272,8 +277,9 @@ public void curvedWayGeometry_issue2319() { // ----- // // 2 -- 3 - FlagEncoder encoder = FlagEncoders.createCar(new PMap().putObject("speed_two_directions", true)); - EncodingManager em = EncodingManager.create(encoder); + SimpleBooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, true); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); PointList pointList = new PointList(20, false); pointList.add(43.844377, -79.264005); @@ -302,14 +308,14 @@ public void curvedWayGeometry_issue2319() { pointList.add(43.842711, -79.264588); graph.getNodeAccess().setNode(0, 43.844521, -79.263976); graph.getNodeAccess().setNode(1, 43.842775, -79.264649); - EdgeIteratorState doubtfire = graph.edge(0, 1).setWayGeometry(pointList).set(encoder.getAccessEnc(), true, true).set(encoder.getAverageSpeedEnc(), 60, 60).setName("Doubtfire Crescent"); - EdgeIteratorState golden = graph.edge(0, 1).set(encoder.getAccessEnc(), true, true).set(encoder.getAverageSpeedEnc(), 60, 60).setName("Golden Avenue"); + EdgeIteratorState doubtfire = graph.edge(0, 1).setWayGeometry(pointList).set(accessEnc, true, true).set(speedEnc, 60, 60).setName("Doubtfire Crescent"); + EdgeIteratorState golden = graph.edge(0, 1).set(accessEnc, true, true).set(speedEnc, 60, 60).setName("Golden Avenue"); graph.getNodeAccess().setNode(2, 43.841501560244744, -79.26366394602502); graph.getNodeAccess().setNode(3, 43.842247922172724, -79.2605663670726); PointList pointList2 = new PointList(1, false); pointList2.add(43.84191413615452, -79.261912128223); - EdgeIteratorState denison = graph.edge(2, 3).setWayGeometry(pointList2).set(encoder.getAccessEnc(), true, true).set(encoder.getAverageSpeedEnc(), 60, 60).setName("Denison Street"); + EdgeIteratorState denison = graph.edge(2, 3).setWayGeometry(pointList2).set(accessEnc, true, true).set(speedEnc, 60, 60).setName("Denison Street"); double qlat = 43.842122; double qLon = -79.262162; diff --git a/core/src/test/java/com/graphhopper/routing/weighting/ShortFastestWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/ShortFastestWeightingTest.java index a94b8c51bd6..289a9406c1d 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/ShortFastestWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/ShortFastestWeightingTest.java @@ -17,8 +17,11 @@ */ package com.graphhopper.routing.weighting; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.GHUtility; import com.graphhopper.util.PMap; @@ -26,33 +29,30 @@ import static com.graphhopper.util.GHUtility.createMockedEdgeIteratorState; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * @author Peter Karich */ public class ShortFastestWeightingTest { - EncodingManager encodingManager = EncodingManager.create("car"); - private final FlagEncoder encoder = encodingManager.getEncoder("car"); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + private final EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); @Test public void testShort() { - EdgeIteratorState edge = createMockedEdgeIteratorState(10, GHUtility.setSpeed(50, 0, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), encodingManager.createEdgeFlags())); - Weighting instance = new ShortFastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), null, new PMap("short_fastest.distance_factor=0.03"), TurnCostProvider.NO_TURN_COST_PROVIDER); + EdgeIteratorState edge = createMockedEdgeIteratorState(10, GHUtility.setSpeed(50, 0, accessEnc, speedEnc, encodingManager.createEdgeFlags())); + Weighting instance = new ShortFastestWeighting(accessEnc, speedEnc, null, new PMap("short_fastest.distance_factor=0.03"), TurnCostProvider.NO_TURN_COST_PROVIDER); assertEquals(1.02, instance.calcEdgeWeight(edge, false), 1e-6); // more influence from distance - instance = new ShortFastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), null, new PMap("short_fastest.distance_factor=0.1"), TurnCostProvider.NO_TURN_COST_PROVIDER); + instance = new ShortFastestWeighting(accessEnc, speedEnc, null, new PMap("short_fastest.distance_factor=0.1"), TurnCostProvider.NO_TURN_COST_PROVIDER); assertEquals(1.72, instance.calcEdgeWeight(edge, false), 1e-6); } @Test public void testTooSmall() { - try { - new ShortFastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), null, new PMap("short_fastest.distance_factor=0|short_fastest.time_factor=0"), - TurnCostProvider.NO_TURN_COST_PROVIDER); - fail(); - } catch (Exception ex) { - } + assertThrows(Exception.class, () -> new ShortFastestWeighting(accessEnc, speedEnc, null, new PMap("short_fastest.distance_factor=0|short_fastest.time_factor=0"), + TurnCostProvider.NO_TURN_COST_PROVIDER)); } } diff --git a/core/src/test/java/com/graphhopper/storage/GraphEdgeIdFinderTest.java b/core/src/test/java/com/graphhopper/storage/GraphEdgeIdFinderTest.java index 116a2474e9b..c608b529dea 100644 --- a/core/src/test/java/com/graphhopper/storage/GraphEdgeIdFinderTest.java +++ b/core/src/test/java/com/graphhopper/storage/GraphEdgeIdFinderTest.java @@ -19,7 +19,11 @@ import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; +import com.graphhopper.routing.util.AccessFilter; +import com.graphhopper.routing.util.AllEdgesIterator; +import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.storage.index.LocationIndexTree; import com.graphhopper.util.GHUtility; @@ -39,14 +43,13 @@ public class GraphEdgeIdFinderTest { @Test public void testParseStringHints() { - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); // 0-1-2 // | | // 3-4 - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 2).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(3, 4).setDistance(1)); @@ -73,8 +76,9 @@ public void testParseStringHints() { @Test public void testBlockAreasWithPolygon() { - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); // 00-01-02-03 @@ -82,8 +86,6 @@ public void testBlockAreasWithPolygon() { // 04-05-06-07 // | | // 08-09-10-11 - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(1)); // 0 GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 2).setDistance(1)); // 1 GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(2, 3).setDistance(1)); // 2 diff --git a/core/src/test/java/com/graphhopper/storage/ShortcutUnpackerTest.java b/core/src/test/java/com/graphhopper/storage/ShortcutUnpackerTest.java index 9ce8c83c1a8..8653aa956f8 100644 --- a/core/src/test/java/com/graphhopper/storage/ShortcutUnpackerTest.java +++ b/core/src/test/java/com/graphhopper/storage/ShortcutUnpackerTest.java @@ -4,17 +4,13 @@ import com.carrotsearch.hppc.IntArrayList; import com.graphhopper.routing.ch.PrepareEncoder; import com.graphhopper.routing.ch.ShortcutUnpacker; -import com.graphhopper.routing.ev.EncodedValueLookup; -import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.TurnCostProvider; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.GHUtility; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -34,16 +30,20 @@ public class ShortcutUnpackerTest { private static final class Fixture { private final boolean edgeBased; private final EncodingManager encodingManager; - private final FlagEncoder encoder; + private final BooleanEncodedValue accessEnc; + private final DecimalEncodedValue speedEnc; + private final DecimalEncodedValue turnCostEnc; private final BaseGraph graph; private CHStorageBuilder chBuilder; private RoutingCHGraph routingCHGraph; Fixture(boolean edgeBased) { this.edgeBased = edgeBased; - encoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", 10).putObject("speed_two_directions", true)); - encodingManager = EncodingManager.create(encoder); - graph = new BaseGraph.Builder(encodingManager).create(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, true); + turnCostEnc = TurnCost.create("car", 10); + encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); + graph = new BaseGraph.Builder(encodingManager).withTurnCosts(true).create(); } @Override @@ -53,8 +53,8 @@ public String toString() { private void freeze() { graph.freeze(); - TurnCostProvider turnCostProvider = edgeBased ? new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage()) : NO_TURN_COST_PROVIDER; - CHConfig chConfig = new CHConfig("profile", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), turnCostProvider), edgeBased); + TurnCostProvider turnCostProvider = edgeBased ? new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage()) : NO_TURN_COST_PROVIDER; + CHConfig chConfig = new CHConfig("profile", new FastestWeighting(accessEnc, speedEnc, turnCostProvider), edgeBased); CHStorage chStore = CHStorage.fromGraph(graph, chConfig); chBuilder = new CHStorageBuilder(chStore); routingCHGraph = RoutingCHGraphImpl.fromGraph(graph, chStore, chConfig); @@ -79,7 +79,7 @@ private ShortcutUnpacker createShortcutUnpacker(ShortcutUnpacker.Visitor visitor } private void setTurnCost(int fromEdge, int viaNode, int toEdge, double cost) { - graph.getTurnCostStorage().set(((EncodedValueLookup) encodingManager).getDecimalEncodedValue(TurnCost.key(encoder.toString())), fromEdge, viaNode, toEdge, cost); + graph.getTurnCostStorage().set(turnCostEnc, fromEdge, viaNode, toEdge, cost); } private void shortcut(int baseNode, int adjNode, int skip1, int skip2, int origKeyFirst, int origKeyLast, boolean reverse) { @@ -108,7 +108,7 @@ public Stream provideArguments(ExtensionContext context) { @ArgumentsSource(FixtureProvider.class) public void testUnpacking(Fixture f) { // 0-1-2-3-4-5-6 - GHUtility.setSpeed(60, 30, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 30, f.accessEnc, f.speedEnc, f.graph.edge(0, 1).setDistance(1), f.graph.edge(1, 2).setDistance(1), f.graph.edge(2, 3).setDistance(1), @@ -197,7 +197,7 @@ public void loopShortcut(Fixture f) { // 2 4 // \ / // 0 - 1 - 5 - GHUtility.setSpeed(60, 30, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 30, f.accessEnc, f.speedEnc, f.graph.edge(0, 1).setDistance(1), f.graph.edge(1, 2).setDistance(1), f.graph.edge(2, 3).setDistance(1), @@ -274,7 +274,7 @@ public void withTurnWeighting(Fixture f) { // prev 0-1-2-3-4-5-6 next // 1 0 1 4 2 3 2 turn costs <- EdgeIteratorState edge0, edge1, edge2, edge3, edge4, edge5; - GHUtility.setSpeed(60, 30, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 30, f.accessEnc, f.speedEnc, edge0 = f.graph.edge(0, 1).setDistance(1), edge1 = f.graph.edge(1, 2).setDistance(1), edge2 = f.graph.edge(2, 3).setDistance(1), diff --git a/web-bundle/src/test/java/com/graphhopper/gpx/GpxConversionsTest.java b/web-bundle/src/test/java/com/graphhopper/gpx/GpxConversionsTest.java index 9053834d1f2..d37052b2c1b 100644 --- a/web-bundle/src/test/java/com/graphhopper/gpx/GpxConversionsTest.java +++ b/web-bundle/src/test/java/com/graphhopper/gpx/GpxConversionsTest.java @@ -23,9 +23,9 @@ import com.graphhopper.routing.Path; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.storage.BaseGraph; @@ -50,14 +50,16 @@ public class GpxConversionsTest { + private BooleanEncodedValue accessEnc; + private DecimalEncodedValue speedEnc; private EncodingManager carManager; - private FlagEncoder carEncoder; private TranslationMap trMap; @BeforeEach public void setUp() { - carEncoder = FlagEncoders.createCar(); - carManager = EncodingManager.create(carEncoder); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + carManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); trMap = new TranslationMap().doImport(); } @@ -78,8 +80,6 @@ public void testInstructionsWithTimeAndPlace() { na.setNode(6, 15.1, 10.1); na.setNode(7, 15.1, 9.8); - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); GHUtility.setSpeed(63, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(7000).setName("1-2")); GHUtility.setSpeed(72, true, true, accessEnc, speedEnc, g.edge(2, 3).setDistance(8000).setName("2-3")); GHUtility.setSpeed(9, true, true, accessEnc, speedEnc, g.edge(2, 6).setDistance(10000).setName("2-6")); From 64ac653a7670c3987c7e0650a7b8bb39c4359734 Mon Sep 17 00:00:00 2001 From: easbar Date: Wed, 22 Jun 2022 21:23:27 +0200 Subject: [PATCH 04/28] Remove even more flag encoders from tests --- .../routing/RandomCHRoutingTest.java | 50 ++++++++++--------- .../routing/RandomizedRoutingTest.java | 40 ++++++++------- 2 files changed, 48 insertions(+), 42 deletions(-) diff --git a/core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java b/core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java index 386a9805706..e1f361e0536 100644 --- a/core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java @@ -2,10 +2,12 @@ import com.graphhopper.routing.ch.CHRoutingAlgorithmFactory; import com.graphhopper.routing.ch.PrepareContractionHierarchies; -import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.querygraph.QueryRoutingCHGraph; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.Weighting; @@ -43,28 +45,29 @@ private static final class Fixture { private final TraversalMode traversalMode; private final int maxTurnCosts; private final int uTurnCosts; - private final Directory dir; - private final FlagEncoder encoder; - private final EncodingManager encodingManager; + private final BooleanEncodedValue accessEnc; + private final DecimalEncodedValue speedEnc; + private final DecimalEncodedValue turnCostEnc; private Weighting weighting; - private BaseGraph graph; + private final BaseGraph graph; private CHConfig chConfig; Fixture(TraversalMode traversalMode, int uTurnCosts) { this.traversalMode = traversalMode; this.maxTurnCosts = 10; this.uTurnCosts = uTurnCosts; - dir = new RAMDirectory(); - encoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", maxTurnCosts)); - encodingManager = EncodingManager.create(encoder); - graph = new BaseGraph.Builder(encodingManager).create(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + turnCostEnc = TurnCost.create("car", maxTurnCosts); + EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); + graph = new BaseGraph.Builder(encodingManager).withTurnCosts(true).create(); } void freeze() { graph.freeze(); chConfig = traversalMode.isEdgeBased() - ? CHConfig.edgeBased("p", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage(), uTurnCosts))) - : CHConfig.nodeBased("p", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc())); + ? CHConfig.edgeBased("p", new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage(), uTurnCosts))) + : CHConfig.nodeBased("p", new FastestWeighting(accessEnc, speedEnc)); weighting = chConfig.getWeighting(); } @@ -103,9 +106,9 @@ public void random(Fixture f) { // the same as taking the direct edge! double pOffset = 0; GHUtility.buildRandomGraph(f.graph, rnd, numNodes, 2.5, true, true, - f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.9, pOffset); + f.accessEnc, f.speedEnc, null, 0.7, 0.9, pOffset); if (f.traversalMode.isEdgeBased()) { - GHUtility.addRandomTurnCosts(f.graph, seed, f.encoder.getAccessEnc(), f.encoder.getTurnCostEnc(), f.maxTurnCosts, f.graph.getTurnCostStorage()); + GHUtility.addRandomTurnCosts(f.graph, seed, f.accessEnc, f.turnCostEnc, f.maxTurnCosts, f.graph.getTurnCostStorage()); } runRandomTest(f, rnd, 20); } @@ -115,7 +118,7 @@ public void random(Fixture f) { public void issue1574_1(Fixture f) { assumeFalse(f.traversalMode.isEdgeBased()); Random rnd = new Random(9348906923700L); - buildRandomGraphLegacy(f.graph, f.encoder, rnd, 50, 2.5, false, true, 0.9); + buildRandomGraphLegacy(f.graph, f.accessEnc, f.speedEnc, rnd, 50, 2.5, false, true, 0.9); runRandomTest(f, rnd, 20); } @@ -124,7 +127,7 @@ public void issue1574_1(Fixture f) { public void issue1574_2(Fixture f) { assumeFalse(f.traversalMode.isEdgeBased()); Random rnd = new Random(10093639220394L); - buildRandomGraphLegacy(f.graph, f.encoder, rnd, 50, 2.5, false, true, 0.9); + buildRandomGraphLegacy(f.graph, f.accessEnc, f.speedEnc, rnd, 50, 2.5, false, true, 0.9); runRandomTest(f, rnd, 20); } @@ -133,7 +136,7 @@ public void issue1574_2(Fixture f) { public void issue1582(Fixture f) { assumeFalse(f.traversalMode.isEdgeBased()); Random rnd = new Random(4111485945982L); - buildRandomGraphLegacy(f.graph, f.encoder, rnd, 10, 2.5, false, true, 0.9); + buildRandomGraphLegacy(f.graph, f.accessEnc, f.speedEnc, rnd, 10, 2.5, false, true, 0.9); runRandomTest(f, rnd, 100); } @@ -142,7 +145,7 @@ public void issue1582(Fixture f) { public void issue1583(Fixture f) { assumeFalse(f.traversalMode.isEdgeBased()); Random rnd = new Random(10785899964423L); - buildRandomGraphLegacy(f.graph, f.encoder, rnd, 50, 2.5, true, true, 0.9); + buildRandomGraphLegacy(f.graph, f.accessEnc, f.speedEnc, rnd, 50, 2.5, true, true, 0.9); runRandomTest(f, rnd, 20); } @@ -153,13 +156,13 @@ public void issue1593(Fixture f) { long seed = 60643479675316L; Random rnd = new Random(seed); GHUtility.buildRandomGraph(f.graph, rnd, 50, 2.5, true, true, - f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.9, 0.0); - GHUtility.addRandomTurnCosts(f.graph, seed, f.encoder.getAccessEnc(), f.encoder.getTurnCostEnc(), f.maxTurnCosts, f.graph.getTurnCostStorage()); + f.accessEnc, f.speedEnc, null, 0.7, 0.9, 0.0); + GHUtility.addRandomTurnCosts(f.graph, seed, f.accessEnc, f.turnCostEnc, f.maxTurnCosts, f.graph.getTurnCostStorage()); runRandomTest(f, rnd, 20); } private void runRandomTest(Fixture f, Random rnd, int numVirtualNodes) { - LocationIndexTree locationIndex = new LocationIndexTree(f.graph, f.dir); + LocationIndexTree locationIndex = new LocationIndexTree(f.graph, f.graph.getDirectory()); locationIndex.prepareIndex(); f.freeze(); @@ -225,7 +228,7 @@ private void runRandomTest(Fixture f, Random rnd, int numVirtualNodes) { * More or less does the same as {@link GHUtility#buildRandomGraph}, but since some special seeds * are used in a few tests above this code is kept here. Do not use it for new tests. */ - private void buildRandomGraphLegacy(Graph graph, FlagEncoder encoder, Random random, int numNodes, double meanDegree, boolean allowLoops, boolean allowZeroDistance, double pBothDir) { + private void buildRandomGraphLegacy(Graph graph, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, Random random, int numNodes, double meanDegree, boolean allowLoops, boolean allowZeroDistance, double pBothDir) { for (int i = 0; i < numNodes; ++i) { double lat = 49.4 + (random.nextDouble() * 0.0001); double lon = 9.7 + (random.nextDouble() * 0.0001); @@ -251,10 +254,9 @@ private void buildRandomGraphLegacy(Graph graph, FlagEncoder encoder, Random ran maxDist = Math.max(maxDist, distance); // using bidirectional edges will increase mean degree of graph above given value boolean bothDirections = random.nextDouble() < pBothDir; - EdgeIteratorState edge = GHUtility.setSpeed(60, true, bothDirections, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(from, to).setDistance(distance)); + EdgeIteratorState edge = GHUtility.setSpeed(60, true, bothDirections, accessEnc, speedEnc, graph.edge(from, to).setDistance(distance)); double fwdSpeed = 10 + random.nextDouble() * 120; double bwdSpeed = 10 + random.nextDouble() * 120; - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); edge.set(speedEnc, fwdSpeed); if (speedEnc.isStoreTwoDirections()) edge.setReverse(speedEnc, bwdSpeed); diff --git a/core/src/test/java/com/graphhopper/routing/RandomizedRoutingTest.java b/core/src/test/java/com/graphhopper/routing/RandomizedRoutingTest.java index 978b6fd95d3..0d194da0bc4 100644 --- a/core/src/test/java/com/graphhopper/routing/RandomizedRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/RandomizedRoutingTest.java @@ -22,11 +22,13 @@ import com.carrotsearch.hppc.IntIndexedContainer; import com.graphhopper.routing.ch.CHRoutingAlgorithmFactory; import com.graphhopper.routing.ch.PrepareContractionHierarchies; -import com.graphhopper.routing.ev.Subnetwork; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.lm.*; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.querygraph.QueryRoutingCHGraph; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.Weighting; @@ -95,11 +97,12 @@ private static class Fixture { private final boolean prepareCH; private final boolean prepareLM; private final TraversalMode traversalMode; - private final Directory dir; private final BaseGraph graph; private final List chConfigs; private final LMConfig lmConfig; - private final FlagEncoder encoder; + private final BooleanEncodedValue accessEnc; + private final DecimalEncodedValue speedEnc; + private final DecimalEncodedValue turnCostEnc; private final TurnCostStorage turnCostStorage; private final int maxTurnCosts; private final Weighting weighting; @@ -113,19 +116,20 @@ private static class Fixture { this.prepareLM = prepareLM; this.traversalMode = traversalMode; maxTurnCosts = 10; - dir = new RAMDirectory(); // todo: this test only works with speedTwoDirections=false (as long as loops are enabled), otherwise it will // fail sometimes for edge-based algorithms, #1631, but maybe we can should disable different fwd/bwd speeds // only for loops instead? - encoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", maxTurnCosts)); - encodingManager = new EncodingManager.Builder().add(encoder).add(Subnetwork.create("car")).build(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + turnCostEnc = TurnCost.create("car", maxTurnCosts); + encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).add(Subnetwork.create("car")).build(); graph = new BaseGraph.Builder(encodingManager) - .setDir(dir) + .withTurnCosts(true) .create(); turnCostStorage = graph.getTurnCostStorage(); chConfigs = Arrays.asList( - CHConfig.nodeBased("p1", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc())), - CHConfig.edgeBased("p2", new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage()))) + CHConfig.nodeBased("p1", new FastestWeighting(accessEnc, speedEnc)), + CHConfig.edgeBased("p2", new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage()))) ); // important: for LM preparation we need to use a weighting without turn costs #1960 lmConfig = new LMConfig("car", chConfigs.get(0).getWeighting()); @@ -146,7 +150,7 @@ private void preProcessGraph() { routingCHGraph = RoutingCHGraphImpl.fromGraph(graph, res.getCHStorage(), res.getCHConfig()); } if (prepareLM) { - PrepareLandmarks prepare = new PrepareLandmarks(dir, graph, encodingManager, lmConfig, 16); + PrepareLandmarks prepare = new PrepareLandmarks(graph.getDirectory(), graph, encodingManager, lmConfig, 16); prepare.setMaximumWeight(10000); prepare.doWork(); lm = prepare.getLandmarkStorage(); @@ -303,9 +307,9 @@ public void randomGraph(Supplier fixtureSupplier) { final int numQueries = 50; Random rnd = new Random(seed); GHUtility.buildRandomGraph(f.graph, rnd, 100, 2.2, true, true, - f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.8, 0.8); - GHUtility.addRandomTurnCosts(f.graph, seed, f.encoder.getAccessEnc(), f.encoder.getTurnCostEnc(), f.maxTurnCosts, f.turnCostStorage); -// GHUtility.printGraphForUnitTest(f.graph, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc()); + f.accessEnc, f.speedEnc, null, 0.7, 0.8, 0.8); + GHUtility.addRandomTurnCosts(f.graph, seed, f.accessEnc, f.turnCostEnc, f.maxTurnCosts, f.turnCostStorage); +// GHUtility.printGraphForUnitTest(f.graph, f.accessEnc, f.speedEnc); f.preProcessGraph(); List strictViolations = new ArrayList<>(); for (int i = 0; i < numQueries; i++) { @@ -341,11 +345,11 @@ public void randomGraph_withQueryGraph(Supplier fixtureSupplier) { double pOffset = 0; Random rnd = new Random(seed); GHUtility.buildRandomGraph(f.graph, rnd, 50, 2.2, true, true, - f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.8, pOffset); - GHUtility.addRandomTurnCosts(f.graph, seed, f.encoder.getAccessEnc(), f.encoder.getTurnCostEnc(), f.maxTurnCosts, f.turnCostStorage); -// GHUtility.printGraphForUnitTest(f.graph, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc()); + f.accessEnc, f.speedEnc, null, 0.7, 0.8, pOffset); + GHUtility.addRandomTurnCosts(f.graph, seed, f.accessEnc, f.turnCostEnc, f.maxTurnCosts, f.turnCostStorage); +// GHUtility.printGraphForUnitTest(f.graph, f.accessEnc, f.speedEnc); f.preProcessGraph(); - LocationIndexTree index = new LocationIndexTree(f.graph, f.dir); + LocationIndexTree index = new LocationIndexTree(f.graph, f.graph.getDirectory()); index.prepareIndex(); List strictViolations = new ArrayList<>(); for (int i = 0; i < numQueries; i++) { From 8e4a642612125625025ebff0650398e5a42c01f0 Mon Sep 17 00:00:00 2001 From: easbar Date: Thu, 23 Jun 2022 08:55:55 +0200 Subject: [PATCH 05/28] Remove more FlagEncoders, 162 to go... --- .../routing/HeadingResolverTest.java | 27 +++---- .../routing/HeadingRoutingTest.java | 74 +++++++++---------- .../routing/lm/LMApproximatorTest.java | 22 +++--- .../graphhopper/routing/lm/LMIssueTest.java | 17 ++--- .../routing/util/HeadingEdgeFilterTest.java | 14 ++-- .../weighting/FastestWeightingTest.java | 63 +++++++--------- .../com/graphhopper/util/GHUtilityTest.java | 15 ++-- 7 files changed, 112 insertions(+), 120 deletions(-) diff --git a/core/src/test/java/com/graphhopper/routing/HeadingResolverTest.java b/core/src/test/java/com/graphhopper/routing/HeadingResolverTest.java index 72debe7f8df..8689b05e2a6 100644 --- a/core/src/test/java/com/graphhopper/routing/HeadingResolverTest.java +++ b/core/src/test/java/com/graphhopper/routing/HeadingResolverTest.java @@ -21,10 +21,10 @@ import com.carrotsearch.hppc.IntArrayList; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.NodeAccess; import com.graphhopper.storage.index.Snap; @@ -45,8 +45,9 @@ public void straightEdges() { // 7 -- 8 --- 3 // /|\ // 6 5 4 - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 49.5073, 1.5545); @@ -59,8 +60,6 @@ public void straightEdges() { na.setNode(7, 48.8611, 1.2194); na.setNode(8, 48.8538, 2.3950); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(8, 0).setDistance(10)); // edge 0 GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(8, 1).setDistance(10)); // edge 1 GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(8, 2).setDistance(10)); // edge 2 @@ -89,16 +88,17 @@ public void curvyEdge() { // 1 -| // |- 0 -| // |- 2 - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); NodeAccess na = graph.getNodeAccess(); na.setNode(1, 0.01, 0.00); na.setNode(0, 0.00, 0.00); na.setNode(2, -0.01, 0.00); - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(0, 1).setDistance(10)). + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(10)). setWayGeometry(Helper.createPointList(0.00, 0.01, 0.01, 0.01)); - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(0, 2).setDistance(10)). + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 2).setDistance(10)). setWayGeometry(Helper.createPointList(0.00, -0.01, -0.01, -0.01)); HeadingResolver resolver = new HeadingResolver(graph); resolver.setTolerance(120); @@ -112,14 +112,15 @@ public void curvyEdge() { public void withQueryGraph() { // 2 // 0 -x- 1 - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 48.8611, 1.2194); na.setNode(1, 48.8538, 2.3950); - EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(0, 1).setDistance(10)); + EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(10)); Snap snap = createSnap(edge, 48.859, 2.00, 0); QueryGraph queryGraph = QueryGraph.create(graph, snap); HeadingResolver resolver = new HeadingResolver(queryGraph); diff --git a/core/src/test/java/com/graphhopper/routing/HeadingRoutingTest.java b/core/src/test/java/com/graphhopper/routing/HeadingRoutingTest.java index 17c5f899a8a..499fafe29ec 100644 --- a/core/src/test/java/com/graphhopper/routing/HeadingRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/HeadingRoutingTest.java @@ -22,12 +22,8 @@ import com.graphhopper.GHResponse; import com.graphhopper.ResponsePath; import com.graphhopper.config.Profile; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.Subnetwork; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.Graph; import com.graphhopper.storage.NodeAccess; @@ -58,9 +54,10 @@ class HeadingRoutingTest { @Test public void headingTest1() { // Test enforce start direction - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager encodingManager = new EncodingManager.Builder().add(carEncoder).add(Subnetwork.create("profile")).build(); - BaseGraph graph = createSquareGraph(encodingManager); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); + BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); // Start in middle of edge 4-5 @@ -81,9 +78,10 @@ public void headingTest1() { @Test public void headingTest2() { // Test enforce south start direction and east end direction - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager encodingManager = new EncodingManager.Builder().add(carEncoder).add(Subnetwork.create("profile")).build(); - BaseGraph graph = createSquareGraph(encodingManager); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); + BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); // Start in middle of edge 4-5 @@ -108,9 +106,10 @@ public void headingTest2() { @Test public void headingTest3() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager encodingManager = new EncodingManager.Builder().add(carEncoder).add(Subnetwork.create("profile")).build(); - BaseGraph graph = createSquareGraph(encodingManager); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); + BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); // Start in middle of edge 4-5 @@ -133,9 +132,10 @@ public void headingTest3() { @Test public void headingTest4() { // Test straight via routing - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager encodingManager = new EncodingManager.Builder().add(carEncoder).add(Subnetwork.create("profile")).build(); - BaseGraph graph = createSquareGraph(encodingManager); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); + BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); // Start in middle of edge 4-5 @@ -158,9 +158,10 @@ public void headingTest4() { @Test public void headingTest5() { // Test independence of previous enforcement for subsequent paths - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager encodingManager = new EncodingManager.Builder().add(carEncoder).add(Subnetwork.create("profile")).build(); - BaseGraph graph = createSquareGraph(encodingManager); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); + BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); // Start in middle of edge 4-5 @@ -182,9 +183,10 @@ public void headingTest5() { @Test public void testHeadingWithSnapFilter() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager encodingManager = new EncodingManager.Builder().add(carEncoder).add(Subnetwork.create("profile")).build(); - BaseGraph graph = createSquareGraphWithTunnel(encodingManager); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); + BaseGraph graph = createSquareGraphWithTunnel(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); // Start at 8 (slightly north to make it independent on some edge ordering and always use 8-3 or 3-8 as fallback) GHPoint start = new GHPoint(0.0011, 0.001); @@ -244,9 +246,10 @@ public void testHeadingWithSnapFilter() { @Test public void testHeadingWithSnapFilter2() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager encodingManager = new EncodingManager.Builder().add(carEncoder).add(Subnetwork.create("profile")).build(); - BaseGraph graph = createSquareGraphWithTunnel(encodingManager); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); + BaseGraph graph = createSquareGraphWithTunnel(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); // Start at 8 (slightly east to snap to edge 1->5 per default) GHPoint start = new GHPoint(0.001, 0.0011); @@ -277,9 +280,10 @@ public void testHeadingWithSnapFilter2() { @Test public void headingTest6() { // Test if snaps at tower nodes are ignored - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager encodingManager = new EncodingManager.Builder().add(carEncoder).add(Subnetwork.create("profile")).build(); - BaseGraph graph = createSquareGraph(encodingManager); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); + BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); // QueryPoints directly on TowerNodes @@ -306,10 +310,8 @@ private Router createRouter(BaseGraph graph, EncodingManager encodingManager) { new DefaultWeightingFactory(graph.getBaseGraph(), encodingManager), Collections.emptyMap(), Collections.emptyMap()); } - private BaseGraph createSquareGraph(EncodingManager encodingManager) { + private BaseGraph createSquareGraph(EncodingManager encodingManager, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { BaseGraph g = new BaseGraph.Builder(encodingManager).create(); - FlagEncoder carEncoder = encodingManager.getEncoder("car"); - // 2---3---4 // | | | // 1---8---5 @@ -326,8 +328,6 @@ private BaseGraph createSquareGraph(EncodingManager encodingManager) { na.setNode(7, 0.000, 0.001); na.setNode(8, 0.001, 0.001); - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 1).setDistance(100)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(100)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(2, 3).setDistance(100)); @@ -345,10 +345,8 @@ private BaseGraph createSquareGraph(EncodingManager encodingManager) { return g; } - private BaseGraph createSquareGraphWithTunnel(EncodingManager encodingManager) { + private BaseGraph createSquareGraphWithTunnel(EncodingManager encodingManager, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { BaseGraph g = new BaseGraph.Builder(encodingManager).create(); - FlagEncoder carEncoder = encodingManager.getEncoder("car"); - // 2----3---4 // | | | // 1->- 8 >-5 (edge 1->5 is not connected to 8) @@ -365,8 +363,6 @@ private BaseGraph createSquareGraphWithTunnel(EncodingManager encodingManager) { na.setNode(7, 0.000, 0.001); na.setNode(8, 0.001, 0.001); - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 1).setDistance(100)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(100)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(2, 3).setDistance(100)); diff --git a/core/src/test/java/com/graphhopper/routing/lm/LMApproximatorTest.java b/core/src/test/java/com/graphhopper/routing/lm/LMApproximatorTest.java index adf8aa9f77c..760f0014065 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/LMApproximatorTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/LMApproximatorTest.java @@ -20,15 +20,16 @@ import com.graphhopper.routing.Dijkstra; import com.graphhopper.routing.Path; -import com.graphhopper.routing.ev.Subnetwork; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.ev.*; +import com.graphhopper.routing.util.AccessFilter; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.*; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.Directory; import com.graphhopper.storage.RAMDirectory; import com.graphhopper.util.EdgeIterator; import com.graphhopper.util.GHUtility; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.RepeatedTest; import java.util.Random; @@ -45,15 +46,16 @@ public void randomGraph() { private void run(long seed) { Directory dir = new RAMDirectory(); - FlagEncoder encoder = FlagEncoders.createCar(new PMap("turn_costs=true")); - EncodingManager encodingManager = new EncodingManager.Builder().add(encoder).add(Subnetwork.create("car")).build(); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + DecimalEncodedValue turnCostEnc = TurnCost.create("car", 1); + EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).add(Subnetwork.create("car")).build(); BaseGraph graph = new BaseGraph.Builder(encodingManager).setDir(dir).withTurnCosts(true).create(); Random rnd = new Random(seed); - GHUtility.buildRandomGraph(graph, rnd, 100, 2.2, true, true, - encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), null, 0.7, 0.8, 0.8); + GHUtility.buildRandomGraph(graph, rnd, 100, 2.2, true, true, accessEnc, speedEnc, null, 0.7, 0.8, 0.8); - Weighting weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc); PrepareLandmarks lm = new PrepareLandmarks(dir, graph, encodingManager, new LMConfig("car", weighting), 16); lm.setMaximumWeight(10000); @@ -106,7 +108,7 @@ private void run(long seed) { // That's a requirement for normal A*-implementations, because if it is violated, // the heap-weight of settled nodes can decrease, and that would mean our // stopping criterion is not sufficient. - EdgeIterator neighbors = graph.createEdgeExplorer(AccessFilter.outEdges(encoder.getAccessEnc())).setBaseNode(v); + EdgeIterator neighbors = graph.createEdgeExplorer(AccessFilter.outEdges(accessEnc)).setBaseNode(v); while (neighbors.next()) { int w = neighbors.getAdjNode(); double vw = weighting.calcEdgeWeight(neighbors, false); @@ -117,7 +119,7 @@ private void run(long seed) { } } - neighbors = graph.createEdgeExplorer(AccessFilter.outEdges(encoder.getAccessEnc())).setBaseNode(v); + neighbors = graph.createEdgeExplorer(AccessFilter.outEdges(accessEnc)).setBaseNode(v); while (neighbors.next()) { int w = neighbors.getAdjNode(); double vw = weighting.calcEdgeWeight(neighbors, false); diff --git a/core/src/test/java/com/graphhopper/routing/lm/LMIssueTest.java b/core/src/test/java/com/graphhopper/routing/lm/LMIssueTest.java index 6734b7e61d7..40c75c2011c 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/LMIssueTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/LMIssueTest.java @@ -19,12 +19,8 @@ package com.graphhopper.routing.lm; import com.graphhopper.routing.*; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.Subnetwork; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.BaseGraph; @@ -32,7 +28,6 @@ import com.graphhopper.storage.NodeAccess; import com.graphhopper.storage.RAMDirectory; import com.graphhopper.util.GHUtility; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; @@ -45,7 +40,6 @@ public class LMIssueTest { private Directory dir; private BaseGraph graph; - private FlagEncoder encoder; private BooleanEncodedValue accessEnc; private DecimalEncodedValue speedEnc; private Weighting weighting; @@ -64,11 +58,12 @@ private enum Algo { @BeforeEach public void init() { dir = new RAMDirectory(); - encoder = FlagEncoders.createCar(new PMap("turn_costs=true")); - encodingManager = new EncodingManager.Builder().add(encoder).add(Subnetwork.create("car")).build(); - accessEnc = encoder.getAccessEnc(); - speedEnc = encoder.getAverageSpeedEnc(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + DecimalEncodedValue turnCostEnc = TurnCost.create("car", 1); + encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).add(Subnetwork.create("car")).build(); graph = new BaseGraph.Builder(encodingManager) + .withTurnCosts(true) .setDir(dir) .create(); weighting = new FastestWeighting(accessEnc, speedEnc); diff --git a/core/src/test/java/com/graphhopper/routing/util/HeadingEdgeFilterTest.java b/core/src/test/java/com/graphhopper/routing/util/HeadingEdgeFilterTest.java index 80054eacb8c..b720b89fddd 100644 --- a/core/src/test/java/com/graphhopper/routing/util/HeadingEdgeFilterTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/HeadingEdgeFilterTest.java @@ -1,5 +1,9 @@ package com.graphhopper.routing.util; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.storage.BaseGraph; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.shapes.GHPoint; @@ -12,14 +16,14 @@ class HeadingEdgeFilterTest { @Test public void getHeading() { GHPoint point = new GHPoint(55.67093, 12.577294); - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager encodingManager = new EncodingManager.Builder().add(carEncoder).build(); - BaseGraph g = new BaseGraph.Builder(encodingManager).create(); + BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(carAccessEnc).add(carSpeedEnc).build(); + BaseGraph g = new BaseGraph.Builder(em).create(); EdgeIteratorState edge = g.edge(0, 1); g.getNodeAccess().setNode(0, 55.671044, 12.5771583); g.getNodeAccess().setNode(1, 55.6704136, 12.5784324); - // GHUtility.setSpeed(50, 0, carEncoder, edge.getFlags()); - + // GHUtility.setSpeed(50, 0, carAccessEnc, carSpeedEnc, edge.getFlags()); assertEquals(131.2, HeadingEdgeFilter.getHeadingOfGeometryNearPoint(edge, point, 20), .1); } } \ No newline at end of file diff --git a/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java index d5d59e978fe..14b63ec0d26 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java @@ -21,8 +21,6 @@ import com.graphhopper.routing.ev.*; import com.graphhopper.routing.querygraph.VirtualEdgeIteratorState; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.Graph; import com.graphhopper.storage.IntsRef; @@ -87,20 +85,15 @@ public void testSpeed0() { @Test public void testTime() { - FlagEncoder tmpEnc = FlagEncoders.createBike2(); - EncodingManager em = EncodingManager.create(tmpEnc); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 4, 2, true); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph g = new BaseGraph.Builder(em).create(); - Weighting w = new FastestWeighting(tmpEnc.getAccessEnc(), tmpEnc.getAverageSpeedEnc()); - - IntsRef edgeFlags = GHUtility.setSpeed(15, 15, tmpEnc.getAccessEnc(), tmpEnc.getAverageSpeedEnc(), em.createEdgeFlags()); - tmpEnc.getAverageSpeedEnc().setDecimal(true, edgeFlags, 10.0); - - EdgeIteratorState edge = GHUtility.createMockedEdgeIteratorState(100000, edgeFlags); - + Weighting w = new FastestWeighting(accessEnc, speedEnc); + EdgeIteratorState edge = g.edge(0, 1).setDistance(100_000); + GHUtility.setSpeed(15, 10, accessEnc, speedEnc, edge); assertEquals(375 * 60 * 1000, w.calcEdgeMillis(edge, false)); assertEquals(600 * 60 * 1000, w.calcEdgeMillis(edge, true)); - - g.close(); } @Test @@ -140,21 +133,21 @@ public void calcWeightAndTime_withTurnCosts_shortest() { @Test public void testDestinationTag() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - FlagEncoder bikeEncoder = FlagEncoders.createBike(); - EncodingManager em = EncodingManager.create(carEncoder, bikeEncoder); + BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); + DecimalEncodedValue bikeSpeedEnc = new DecimalEncodedValueImpl("bike_speed", 4, 2, false); + EncodingManager em = EncodingManager.start().add(carAccessEnc).add(carSpeedEnc).add(bikeAccessEnc).add(bikeSpeedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); EdgeIteratorState edge = graph.edge(0, 1).setDistance(1000); - for (FlagEncoder encoder : em.fetchEdgeEncoders()) { - edge.set(encoder.getAccessEnc(), true); - edge.setReverse(encoder.getAccessEnc(), true); - } - edge.set(carEncoder.getAverageSpeedEnc(), 60); - edge.set(bikeEncoder.getAverageSpeedEnc(), 18); + edge.set(carAccessEnc, true, true); + edge.set(bikeAccessEnc, true, true); + edge.set(carSpeedEnc, 60); + edge.set(bikeSpeedEnc, 18); EnumEncodedValue roadAccessEnc = em.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class); - FastestWeighting weighting = new FastestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); - FastestWeighting bikeWeighting = new FastestWeighting(bikeEncoder.getAccessEnc(), bikeEncoder.getAverageSpeedEnc()); + FastestWeighting weighting = new FastestWeighting(carAccessEnc, carSpeedEnc); + FastestWeighting bikeWeighting = new FastestWeighting(bikeAccessEnc, bikeSpeedEnc); edge.set(roadAccessEnc, RoadAccess.YES); assertEquals(60, weighting.calcEdgeWeight(edge, false), 1.e-6); @@ -168,21 +161,21 @@ public void testDestinationTag() { @Test public void testPrivateTag() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - FlagEncoder bikeEncoder = FlagEncoders.createBike(); - EncodingManager em = EncodingManager.create(carEncoder, bikeEncoder); + BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); + DecimalEncodedValue bikeSpeedEnc = new DecimalEncodedValueImpl("bike_speed", 4, 2, false); + EncodingManager em = EncodingManager.start().add(carAccessEnc).add(carSpeedEnc).add(bikeAccessEnc).add(bikeSpeedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); EdgeIteratorState edge = graph.edge(0, 1).setDistance(1000); - for (FlagEncoder encoder : em.fetchEdgeEncoders()) { - edge.set(encoder.getAccessEnc(), true); - edge.setReverse(encoder.getAccessEnc(), true); - } - edge.set(carEncoder.getAverageSpeedEnc(), 60); - edge.set(bikeEncoder.getAverageSpeedEnc(), 18); + edge.set(carAccessEnc, true, true); + edge.set(bikeAccessEnc, true, true); + edge.set(carSpeedEnc, 60); + edge.set(bikeSpeedEnc, 18); EnumEncodedValue roadAccessEnc = em.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class); - FastestWeighting weighting = new FastestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); - FastestWeighting bikeWeighting = new FastestWeighting(bikeEncoder.getAccessEnc(), bikeEncoder.getAverageSpeedEnc()); + FastestWeighting weighting = new FastestWeighting(carAccessEnc, carSpeedEnc); + FastestWeighting bikeWeighting = new FastestWeighting(bikeAccessEnc, bikeSpeedEnc); ReaderWay way = new ReaderWay(1); way.setTag("highway", "secondary"); diff --git a/core/src/test/java/com/graphhopper/util/GHUtilityTest.java b/core/src/test/java/com/graphhopper/util/GHUtilityTest.java index fedbc43b96a..bc60fd2b423 100644 --- a/core/src/test/java/com/graphhopper/util/GHUtilityTest.java +++ b/core/src/test/java/com/graphhopper/util/GHUtilityTest.java @@ -20,10 +20,10 @@ import com.graphhopper.coll.GHIntLongHashMap; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.AllEdgesIterator; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.Graph; import com.graphhopper.storage.NodeAccess; @@ -35,8 +35,9 @@ * @author Peter Karich */ public class GHUtilityTest { - private final FlagEncoder carEncoder = FlagEncoders.createCar(); - private final EncodingManager encodingManager = EncodingManager.create(carEncoder); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + private final EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph createGraph() { return new BaseGraph.Builder(encodingManager).create(); @@ -84,7 +85,7 @@ Graph initUnsorted(Graph g, BooleanEncodedValue accessEnc, DecimalEncodedValue s @Test public void testSort() { - Graph g = initUnsorted(createGraph(), carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); + Graph g = initUnsorted(createGraph(), accessEnc, speedEnc); Graph newG = GHUtility.sortDFS(g, createGraph()); assertEquals(g.getNodes(), newG.getNodes()); assertEquals(g.getEdges(), newG.getEdges()); @@ -118,8 +119,8 @@ public void testSortDirected() { na.setNode(0, 0, 1); na.setNode(1, 2.5, 2); na.setNode(2, 3.5, 3); - GHUtility.setSpeed(60, true, false, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc(), g.edge(0, 1).setDistance(1.1)); - GHUtility.setSpeed(60, true, false, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc(), g.edge(2, 1).setDistance(1.1)); + GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(0, 1).setDistance(1.1)); + GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(2, 1).setDistance(1.1)); GHUtility.sortDFS(g, createGraph()); } From 67101dfc4d0d784f3901f263ec548fd9c4568ce3 Mon Sep 17 00:00:00 2001 From: easbar Date: Thu, 23 Jun 2022 20:16:22 +0200 Subject: [PATCH 06/28] a few more, 159 --- .../ch/PrepareContractionHierarchiesTest.java | 102 +++++++++--------- .../routing/querygraph/QueryGraphTest.java | 53 +++++---- 2 files changed, 80 insertions(+), 75 deletions(-) diff --git a/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java b/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java index 92533b95b99..782be23861e 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java @@ -22,8 +22,7 @@ import com.graphhopper.routing.Dijkstra; import com.graphhopper.routing.Path; import com.graphhopper.routing.RoutingAlgorithm; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.util.*; import com.graphhopper.routing.weighting.FastestWeighting; @@ -44,9 +43,10 @@ * @author Peter Karich */ public class PrepareContractionHierarchiesTest { - private final FlagEncoder carEncoder = FlagEncoders.createCar(new PMap().putObject("speed_two_directions", true)); - private final EncodingManager encodingManager = EncodingManager.create(carEncoder); - private final Weighting weighting = new ShortestWeighting(carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, true); + private final EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); + private final Weighting weighting = new ShortestWeighting(accessEnc, speedEnc); private final CHConfig chConfig = CHConfig.nodeBased("c", weighting); private BaseGraph g; @@ -136,7 +136,7 @@ public void testReturnsCorrectWeighting() { @Test public void testAddShortcuts() { - initExampleGraph(g, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); + initExampleGraph(g, accessEnc, speedEnc); PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g); useNodeOrdering(prepare, new int[]{5, 3, 4, 0, 1, 2}); PrepareContractionHierarchies.Result res = prepare.doWork(); @@ -145,7 +145,7 @@ public void testAddShortcuts() { @Test public void testMoreComplexGraph() { - initShortcutsGraph(g, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); + initShortcutsGraph(g, accessEnc, speedEnc); PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g); useNodeOrdering(prepare, new int[]{0, 5, 6, 7, 8, 10, 11, 13, 15, 1, 3, 9, 14, 16, 12, 4, 2}); PrepareContractionHierarchies.Result res = prepare.doWork(); @@ -154,8 +154,6 @@ public void testMoreComplexGraph() { @Test public void testDirectedGraph() { - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(5, 4).setDistance(3)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(4, 5).setDistance(10)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(2, 4).setDistance(1)); @@ -177,7 +175,7 @@ public void testDirectedGraph() { @Test public void testDirectedGraph2() { - initDirected2(g, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); + initDirected2(g, accessEnc, speedEnc); int oldCount = g.getEdges(); assertEquals(19, oldCount); PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g); @@ -249,7 +247,7 @@ private static void initRoundaboutGraph(Graph g, BooleanEncodedValue accessEnc, @Test public void testRoundaboutUnpacking() { - initRoundaboutGraph(g, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc()); + initRoundaboutGraph(g, accessEnc, speedEnc); int oldCount = g.getEdges(); PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g); useNodeOrdering(prepare, new int[]{26, 6, 12, 13, 2, 3, 8, 9, 10, 11, 14, 15, 16, 17, 18, 20, 21, 23, 24, 25, 19, 22, 27, 5, 29, 30, 31, 28, 7, 1, 0, 4}); @@ -274,8 +272,6 @@ public void testDisconnects() { // 2 // v // 7 - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(8, 3).setDistance(1)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(3, 6).setDistance(1)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(6, 1).setDistance(1)); @@ -323,8 +319,6 @@ public void testStallOnDemandViaVirtuaNode_issue1574() { // here we will construct a special case where a connection is not found without the fix in #1574. g = createGraph(); - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); // use fastest weighting in this test to be able to fine-tune some weights via the speed (see below) Weighting fastestWeighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased("c", fastestWeighting); @@ -402,7 +396,7 @@ private double getWeight(Graph graph, Weighting w, int from, int to, boolean inc } private EdgeIteratorState getEdge(Graph graph, int from, int to, boolean incoming) { - EdgeFilter filter = incoming ? AccessFilter.inEdges(carEncoder.getAccessEnc()) : AccessFilter.outEdges(carEncoder.getAccessEnc()); + EdgeFilter filter = incoming ? AccessFilter.inEdges(accessEnc) : AccessFilter.outEdges(accessEnc); EdgeIterator iter = graph.createEdgeExplorer(filter).setBaseNode(from); while (iter.next()) { if (iter.getAdjNode() == to) { @@ -427,8 +421,6 @@ public void testCircleBug() { // /--1 // -0--/ // | - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 1).setDistance(10)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 1).setDistance(4)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 2).setDistance(10)); @@ -445,8 +437,6 @@ public void testBug178() { // 0-1->-2--3--4 // \-<-/ // - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(1, 2).setDistance(1)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(2, 1).setDistance(1)); @@ -475,19 +465,24 @@ public void testBits() { @Test public void testMultiplePreparationsIdenticalView() { - FlagEncoder tmpCarEncoder = FlagEncoders.createCar(); - FlagEncoder tmpBikeEncoder = FlagEncoders.createBike(); - EncodingManager tmpEncodingManager = EncodingManager.create(tmpCarEncoder, tmpBikeEncoder); + BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); + DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + DecimalEncodedValue bikeSpeedEnc = new DecimalEncodedValueImpl("bike_speed", 4, 2, false); + EncodingManager tmpEncodingManager = EncodingManager.start() + .add(carAccessEnc).add(carSpeedEnc) + .add(bikeAccessEnc).add(bikeSpeedEnc) + .build(); // FastestWeighting would lead to different shortcuts due to different default speeds for bike and car - CHConfig carProfile = CHConfig.nodeBased("c1", new ShortestWeighting(tmpCarEncoder.getAccessEnc(), tmpCarEncoder.getAverageSpeedEnc())); - CHConfig bikeProfile = CHConfig.nodeBased("c2", new ShortestWeighting(tmpBikeEncoder.getAccessEnc(), tmpBikeEncoder.getAverageSpeedEnc())); + CHConfig carProfile = CHConfig.nodeBased("c1", new ShortestWeighting(carAccessEnc, carSpeedEnc)); + CHConfig bikeProfile = CHConfig.nodeBased("c2", new ShortestWeighting(bikeAccessEnc, bikeSpeedEnc)); BaseGraph graph = new BaseGraph.Builder(tmpEncodingManager).create(); - initShortcutsGraph(graph, tmpCarEncoder.getAccessEnc(), tmpCarEncoder.getAverageSpeedEnc()); + initShortcutsGraph(graph, carAccessEnc, carSpeedEnc); AllEdgesIterator iter = graph.getAllEdges(); while (iter.next()) { - GHUtility.setSpeed(18, true, true, tmpBikeEncoder.getAccessEnc(), tmpBikeEncoder.getAverageSpeedEnc(), iter); + GHUtility.setSpeed(18, true, true, bikeAccessEnc, bikeSpeedEnc, iter); } graph.freeze(); @@ -497,22 +492,27 @@ public void testMultiplePreparationsIdenticalView() { @Test public void testMultiplePreparationsDifferentView() { - FlagEncoder tmpCarEncoder = FlagEncoders.createCar(); - FlagEncoder tmpBikeEncoder = FlagEncoders.createBike(); - EncodingManager tmpEncodingManager = EncodingManager.create(tmpCarEncoder, tmpBikeEncoder); - - CHConfig carConfig = CHConfig.nodeBased("c1", new FastestWeighting(tmpCarEncoder.getAccessEnc(), tmpCarEncoder.getAverageSpeedEnc())); - CHConfig bikeConfig = CHConfig.nodeBased("c2", new FastestWeighting(tmpBikeEncoder.getAccessEnc(), tmpBikeEncoder.getAverageSpeedEnc())); + BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); + DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + DecimalEncodedValue bikeSpeedEnc = new DecimalEncodedValueImpl("bike_speed", 4, 2, false); + EncodingManager tmpEncodingManager = EncodingManager.start() + .add(carAccessEnc).add(carSpeedEnc) + .add(bikeAccessEnc).add(bikeSpeedEnc) + .build(); + + CHConfig carConfig = CHConfig.nodeBased("c1", new FastestWeighting(carAccessEnc, carSpeedEnc)); + CHConfig bikeConfig = CHConfig.nodeBased("c2", new FastestWeighting(bikeAccessEnc, bikeSpeedEnc)); BaseGraph graph = new BaseGraph.Builder(tmpEncodingManager).create(); - initShortcutsGraph(graph, tmpCarEncoder.getAccessEnc(), tmpCarEncoder.getAverageSpeedEnc()); + initShortcutsGraph(graph, carAccessEnc, carSpeedEnc); AllEdgesIterator iter = graph.getAllEdges(); while (iter.next()) { - GHUtility.setSpeed(18, true, true, tmpBikeEncoder.getAccessEnc(), tmpBikeEncoder.getAverageSpeedEnc(), iter); + GHUtility.setSpeed(18, true, true, bikeAccessEnc, bikeSpeedEnc, iter); } GHUtility.getEdge(graph, 9, 14). - set(tmpBikeEncoder.getAccessEnc(), false). - setReverse(tmpBikeEncoder.getAccessEnc(), false); + set(bikeAccessEnc, false). + setReverse(bikeAccessEnc, false); graph.freeze(); @@ -523,25 +523,31 @@ public void testMultiplePreparationsDifferentView() { @Test public void testReusingNodeOrdering() { - FlagEncoder car1FlagEncoder = FlagEncoders.createCar(new PMap("name=car1|turn_costs=true|speed_two_directions=true")); - FlagEncoder car2FlagEncoder = FlagEncoders.createCar(new PMap("name=car2|turn_costs=true|speed_two_directions=true")); - EncodingManager em = EncodingManager.create(car1FlagEncoder, car2FlagEncoder); - CHConfig car1Config = CHConfig.nodeBased("c1", new FastestWeighting(car1FlagEncoder.getAccessEnc(), car1FlagEncoder.getAverageSpeedEnc())); - CHConfig car2Config = CHConfig.nodeBased("c2", new FastestWeighting(car2FlagEncoder.getAccessEnc(), car2FlagEncoder.getAverageSpeedEnc())); + BooleanEncodedValue car1AccessEnc = new SimpleBooleanEncodedValue("car1_access", true); + BooleanEncodedValue car2AccessEnc = new SimpleBooleanEncodedValue("car2_access", true); + DecimalEncodedValue car1SpeedEnc = new DecimalEncodedValueImpl("car1_speed", 5, 5, true); + DecimalEncodedValue car2SpeedEnc = new DecimalEncodedValueImpl("car2_speed", 5, 5, true); + DecimalEncodedValue car1TurnCostEnc = TurnCost.create("car1", 1); + DecimalEncodedValue car2TurnCostEnc = TurnCost.create("car2", 1); + EncodingManager em = EncodingManager.start() + .add(car1AccessEnc).add(car1SpeedEnc).addTurnCostEncodedValue(car1TurnCostEnc) + .add(car2AccessEnc).add(car2SpeedEnc).addTurnCostEncodedValue(car2TurnCostEnc) + .build(); + CHConfig car1Config = CHConfig.nodeBased("c1", new FastestWeighting(car1AccessEnc, car1SpeedEnc)); + CHConfig car2Config = CHConfig.nodeBased("c2", new FastestWeighting(car2AccessEnc, car2SpeedEnc)); BaseGraph graph = new BaseGraph.Builder(em).create(); int numNodes = 5_000; int numQueries = 100; long seed = System.nanoTime(); Random rnd = new Random(seed); - GHUtility.buildRandomGraph(graph, rnd, numNodes, 1.3, true, true, - car1FlagEncoder.getAccessEnc(), null, null, 0.7, 0.9, 0.8); + GHUtility.buildRandomGraph(graph, rnd, numNodes, 1.3, true, true, car1AccessEnc, null, null, 0.7, 0.9, 0.8); AllEdgesIterator iter = graph.getAllEdges(); while (iter.next()) { - iter.set(car1FlagEncoder.getAccessEnc(), rnd.nextDouble() > 0.05, rnd.nextDouble() > 0.05); - iter.set(car2FlagEncoder.getAccessEnc(), rnd.nextDouble() > 0.05, rnd.nextDouble() > 0.05); - iter.set(car1FlagEncoder.getAverageSpeedEnc(), rnd.nextDouble() * 100, rnd.nextDouble() * 100); - iter.set(car2FlagEncoder.getAverageSpeedEnc(), rnd.nextDouble() * 100, rnd.nextDouble() * 100); + iter.set(car1AccessEnc, rnd.nextDouble() > 0.05, rnd.nextDouble() > 0.05); + iter.set(car2AccessEnc, rnd.nextDouble() > 0.05, rnd.nextDouble() > 0.05); + iter.set(car1SpeedEnc, rnd.nextDouble() * 100, rnd.nextDouble() * 100); + iter.set(car2SpeedEnc, rnd.nextDouble() * 100, rnd.nextDouble() * 100); } graph.freeze(); diff --git a/core/src/test/java/com/graphhopper/routing/querygraph/QueryGraphTest.java b/core/src/test/java/com/graphhopper/routing/querygraph/QueryGraphTest.java index edf794e9ebe..5dce6b05b38 100644 --- a/core/src/test/java/com/graphhopper/routing/querygraph/QueryGraphTest.java +++ b/core/src/test/java/com/graphhopper/routing/querygraph/QueryGraphTest.java @@ -20,10 +20,10 @@ import com.carrotsearch.hppc.IntArrayList; import com.carrotsearch.hppc.IntObjectMap; import com.graphhopper.routing.HeadingResolver; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.TurnCost; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.ev.*; +import com.graphhopper.routing.util.AccessFilter; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.Weighting; @@ -51,17 +51,15 @@ */ public class QueryGraphTest { private EncodingManager encodingManager; - private FlagEncoder encoder; private BooleanEncodedValue accessEnc; private DecimalEncodedValue speedEnc; private BaseGraph g; @BeforeEach public void setUp() { - encoder = FlagEncoders.createCar(); - encodingManager = EncodingManager.create(encoder); - accessEnc = encoder.getAccessEnc(); - speedEnc = encoder.getAverageSpeedEnc(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); g = new BaseGraph.Builder(encodingManager).create(); } @@ -439,8 +437,8 @@ public Snap createLocationResult(double lat, double lon, @Test public void testIteration_Issue163() { - EdgeFilter outEdgeFilter = AccessFilter.outEdges(encodingManager.getEncoder("car").getAccessEnc()); - EdgeFilter inEdgeFilter = AccessFilter.inEdges(encodingManager.getEncoder("car").getAccessEnc()); + EdgeFilter outEdgeFilter = AccessFilter.outEdges(accessEnc); + EdgeFilter inEdgeFilter = AccessFilter.inEdges(accessEnc); EdgeExplorer inExplorer = g.createEdgeExplorer(inEdgeFilter); EdgeExplorer outExplorer = g.createEdgeExplorer(outEdgeFilter); @@ -499,20 +497,21 @@ private void assertEdgeIdsStayingEqual(EdgeExplorer inExplorer, EdgeExplorer out @Test public void testTurnCostsProperlyPropagated_Issue282() { - FlagEncoder encoder = FlagEncoders.createCar(new PMap("max_turn_costs=15")); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + DecimalEncodedValue turnCostEnc = TurnCost.create("car", 15); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); BaseGraph graphWithTurnCosts = new BaseGraph.Builder(em).withTurnCosts(true).create(); TurnCostStorage turnExt = graphWithTurnCosts.getTurnCostStorage(); - DecimalEncodedValue turnCostEnc = em.getDecimalEncodedValue(TurnCost.key(encoder.toString())); NodeAccess na = graphWithTurnCosts.getNodeAccess(); na.setNode(0, .00, .00); na.setNode(1, .00, .01); na.setNode(2, .01, .01); - EdgeIteratorState edge0 = GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graphWithTurnCosts.edge(0, 1).setDistance(10)); - EdgeIteratorState edge1 = GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graphWithTurnCosts.edge(2, 1).setDistance(10)); + EdgeIteratorState edge0 = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graphWithTurnCosts.edge(0, 1).setDistance(10)); + EdgeIteratorState edge1 = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graphWithTurnCosts.edge(2, 1).setDistance(10)); - Weighting weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graphWithTurnCosts.getTurnCostStorage())); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, graphWithTurnCosts.getTurnCostStorage())); // no turn costs initially assertEquals(0, weighting.calcTurnWeight(edge0.getEdge(), 1, edge1.getEdge()), .1); @@ -827,15 +826,15 @@ public void testVirtualEdgeIds() { // virtual nodes: 2 // 0 - x - 1 // virtual edges: 1 2 - FlagEncoder encoder = FlagEncoders.createCar(new PMap().putObject("speed_two_directions", true)); - EncodingManager encodingManager = EncodingManager.create(encoder); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); - BaseGraph g = new BaseGraph.Builder(encodingManager).create(); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, true); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); + BaseGraph g = new BaseGraph.Builder(em).create(); NodeAccess na = g.getNodeAccess(); na.setNode(0, 50.00, 10.10); na.setNode(1, 50.00, 10.20); double dist = DistanceCalcEarth.DIST_EARTH.calcDist(na.getLat(0), na.getLon(0), na.getLat(1), na.getLon(1)); - EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), g.edge(0, 1).setDistance(dist)); + EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 1).setDistance(dist)); edge.set(speedEnc, 50); edge.setReverse(speedEnc, 100); @@ -901,16 +900,16 @@ public void testVirtualEdgeIds_reverse() { // virtual nodes: 2 // 0 - x - 1 // virtual edges: 1 2 - FlagEncoder encoder = FlagEncoders.createCar(new PMap().putObject("speed_two_directions", true)); - EncodingManager encodingManager = EncodingManager.create(encoder); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); - BaseGraph g = new BaseGraph.Builder(encodingManager).create(); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, true); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); + BaseGraph g = new BaseGraph.Builder(em).create(); NodeAccess na = g.getNodeAccess(); na.setNode(0, 50.00, 10.10); na.setNode(1, 50.00, 10.20); double dist = DistanceCalcEarth.DIST_EARTH.calcDist(na.getLat(0), na.getLon(0), na.getLat(1), na.getLon(1)); // this time we store the edge the other way - EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), g.edge(1, 0).setDistance(dist)); + EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(1, 0).setDistance(dist)); edge.set(speedEnc, 100, 50); // query graph From f4b98f3f0f916a0f68abe3c0bc735e55a9a43948 Mon Sep 17 00:00:00 2001 From: easbar Date: Thu, 23 Jun 2022 20:53:57 +0200 Subject: [PATCH 07/28] Another --- .../storage/TurnCostStorageTest.java | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/core/src/test/java/com/graphhopper/storage/TurnCostStorageTest.java b/core/src/test/java/com/graphhopper/storage/TurnCostStorageTest.java index 8570ec03808..6da4ff09932 100644 --- a/core/src/test/java/com/graphhopper/storage/TurnCostStorageTest.java +++ b/core/src/test/java/com/graphhopper/storage/TurnCostStorageTest.java @@ -18,13 +18,9 @@ package com.graphhopper.storage; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.util.GHUtility; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -40,12 +36,20 @@ public class TurnCostStorageTest { private EncodingManager manager; + private DecimalEncodedValue carTurnCostEnc; + private DecimalEncodedValue bikeTurnCostEnc; + private BooleanEncodedValue accessEnc; + private DecimalEncodedValue speedEnc; @BeforeEach public void setup() { - FlagEncoder carEncoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", 3)); - FlagEncoder bikeEncoder = FlagEncoders.createBike(new PMap().putObject("max_turn_costs", 3)); - manager = EncodingManager.create(carEncoder, bikeEncoder); + accessEnc = new SimpleBooleanEncodedValue("car_access", true); + speedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + carTurnCostEnc = TurnCost.create("car", 3); + bikeTurnCostEnc = TurnCost.create("bike", 3); + manager = EncodingManager.start() + .add(accessEnc).add(speedEnc) + .addTurnCostEncodedValue(carTurnCostEnc).addTurnCostEncodedValue(bikeTurnCostEnc).build(); } // 0---1 @@ -53,8 +57,8 @@ public void setup() { // 2--3 // | // 4 - public static void initGraph(BaseGraph g, FlagEncoder encoder) { - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + public static void initGraph(BaseGraph g, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, g.edge(0, 1).setDistance(3), g.edge(0, 2).setDistance(1), g.edge(1, 3).setDistance(1), @@ -67,12 +71,12 @@ public static void initGraph(BaseGraph g, FlagEncoder encoder) { */ @Test public void testMultipleTurnCosts() { - BaseGraph g = new BaseGraph.Builder(manager).create(); - initGraph(g, manager.getEncoder("car")); + BaseGraph g = new BaseGraph.Builder(manager).withTurnCosts(true).create(); + initGraph(g, accessEnc, speedEnc); TurnCostStorage turnCostStorage = g.getTurnCostStorage(); - DecimalEncodedValue carEnc = manager.getDecimalEncodedValue(TurnCost.key("car")); - DecimalEncodedValue bikeEnc = manager.getDecimalEncodedValue(TurnCost.key("bike")); + DecimalEncodedValue carEnc = carTurnCostEnc; + DecimalEncodedValue bikeEnc = bikeTurnCostEnc; int edge42 = getEdge(g, 4, 2).getEdge(); int edge23 = getEdge(g, 2, 3).getEdge(); int edge31 = getEdge(g, 3, 1).getEdge(); @@ -124,12 +128,12 @@ public void testMultipleTurnCosts() { @Test public void testMergeFlagsBeforeAdding() { - BaseGraph g = new BaseGraph.Builder(manager).create(); - initGraph(g, manager.getEncoder("car")); + BaseGraph g = new BaseGraph.Builder(manager).withTurnCosts(true).create(); + initGraph(g, accessEnc, speedEnc); TurnCostStorage turnCostStorage = g.getTurnCostStorage(); - DecimalEncodedValue carEnc = manager.getDecimalEncodedValue(TurnCost.key("car")); - DecimalEncodedValue bikeEnc = manager.getDecimalEncodedValue(TurnCost.key("bike")); + DecimalEncodedValue carEnc = carTurnCostEnc; + DecimalEncodedValue bikeEnc = bikeTurnCostEnc; int edge23 = getEdge(g, 2, 3).getEdge(); int edge02 = getEdge(g, 0, 2).getEdge(); @@ -153,8 +157,8 @@ public void testMergeFlagsBeforeAdding() { @Test public void testIterateEmptyStore() { - BaseGraph g = new BaseGraph.Builder(manager).create(); - initGraph(g, manager.getEncoder("car")); + BaseGraph g = new BaseGraph.Builder(manager).withTurnCosts(true).create(); + initGraph(g, accessEnc, speedEnc); TurnCostStorage turnCostStorage = g.getTurnCostStorage(); TurnCostStorage.TurnRelationIterator iterator = turnCostStorage.getAllTurnRelations(); From 1e0de74c2d878a755f53247ad2220cfd44a14901 Mon Sep 17 00:00:00 2001 From: easbar Date: Sat, 25 Jun 2022 08:40:41 +0200 Subject: [PATCH 08/28] 107 --- .../com/graphhopper/routing/PathTest.java | 183 ++++++++---------- .../routing/PriorityRoutingTest.java | 45 +++-- .../routing/QueryRoutingCHGraphTest.java | 37 ++-- .../routing/RoundTripRoutingTest.java | 20 +- .../routing/lm/LMPreparationHandlerTest.java | 15 +- .../routing/lm/PrepareLandmarksTest.java | 34 ++-- .../routing/util/FootTagParserTest.java | 21 +- .../parsers/OSMTurnRelationParserTest.java | 30 ++- .../storage/index/LocationIndexTreeTest.java | 127 ++++++------ .../graphhopper/util/InstructionListTest.java | 70 ++++--- .../util/PathSimplificationTest.java | 11 +- 11 files changed, 294 insertions(+), 299 deletions(-) diff --git a/core/src/test/java/com/graphhopper/routing/PathTest.java b/core/src/test/java/com/graphhopper/routing/PathTest.java index 807fa426c9c..aa3effc2f5f 100644 --- a/core/src/test/java/com/graphhopper/routing/PathTest.java +++ b/core/src/test/java/com/graphhopper/routing/PathTest.java @@ -19,8 +19,6 @@ import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.ShortestWeighting; @@ -44,14 +42,17 @@ * @author Peter Karich */ public class PathTest { - private final FlagEncoder encoder = FlagEncoders.createCar(); - private final EncodingManager carManager = EncodingManager.create(encoder); - private final BooleanEncodedValue carAccessEnc = encoder.getAccessEnc(); - private final DecimalEncodedValue carAvSpeedEnc = encoder.getAverageSpeedEnc(); - private final EncodingManager mixedEncoders = EncodingManager.create(FlagEncoders.createCar(), FlagEncoders.createFoot()); + private final BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue carAvSpeedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + private final EncodingManager carManager = EncodingManager.start().add(carAccessEnc).add(carAvSpeedEnc).build(); + private final BooleanEncodedValue mixedCarAccessEnc = new SimpleBooleanEncodedValue("mixed_car_access", true); + private final DecimalEncodedValue mixedCarSpeedEnc = new DecimalEncodedValueImpl("mixed_car_speed", 5, 5, false); + private final BooleanEncodedValue mixedFootAccessEnc = new SimpleBooleanEncodedValue("mixed_foot_access", true); + private final DecimalEncodedValue mixedFootSpeedEnc = new DecimalEncodedValueImpl("mixed_foot_speed", 4, 1, false); + private final EncodingManager mixedEncodingManager = EncodingManager.start().add(mixedCarAccessEnc).add(mixedCarSpeedEnc).add(mixedFootAccessEnc).add(mixedFootSpeedEnc).build(); private final TranslationMap trMap = TranslationMapTest.SINGLETON; private final Translation tr = trMap.getWithFallBack(Locale.US); - private final RoundaboutGraph roundaboutGraph = new RoundaboutGraph(mixedEncoders); + private final RoundaboutGraph roundaboutGraph = new RoundaboutGraph(); private final Graph pathDetailGraph = generatePathDetailsGraph(); @Test @@ -207,49 +208,51 @@ public void testFindInstruction() { * Test roundabout instructions for different profiles */ @Test - public void testCalcInstructionsRoundabout() { - for (FlagEncoder encoder : mixedEncoders.fetchEdgeEncoders()) { - ShortestWeighting weighting = new ShortestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); - Path p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED) - .calcPath(1, 8); - assertTrue(p.isFound()); - assertEquals("[1, 2, 3, 4, 5, 8]", p.calcNodes().toString()); - InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncoders, tr); - // Test instructions - List tmpList = getTurnDescriptions(wayList); - assertEquals(Arrays.asList("continue onto MainStreet 1 2", - "At roundabout, take exit 3 onto 5-8", - "arrive at destination"), - tmpList); - // Test Radian - double delta = roundaboutGraph.getAngle(1, 2, 5, 8); - RoundaboutInstruction instr = (RoundaboutInstruction) wayList.get(1); - assertEquals(delta, instr.getTurnAngle(), 0.01); - - // case of continuing a street through a roundabout - p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED). - calcPath(1, 7); - wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncoders, tr); - tmpList = getTurnDescriptions(wayList); - assertEquals(Arrays.asList("continue onto MainStreet 1 2", - "At roundabout, take exit 2 onto MainStreet 4 7", - "arrive at destination"), - tmpList); - // Test Radian - delta = roundaboutGraph.getAngle(1, 2, 4, 7); - instr = (RoundaboutInstruction) wayList.get(1); - assertEquals(delta, instr.getTurnAngle(), 0.01); - } + void testCalcInstructionsRoundabout() { + calcInstructionsRoundabout(mixedCarAccessEnc, mixedCarSpeedEnc); + calcInstructionsRoundabout(mixedFootAccessEnc, mixedFootSpeedEnc); + } + + public void calcInstructionsRoundabout(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { + ShortestWeighting weighting = new ShortestWeighting(accessEnc, speedEnc); + Path p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED) + .calcPath(1, 8); + assertTrue(p.isFound()); + assertEquals("[1, 2, 3, 4, 5, 8]", p.calcNodes().toString()); + InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncodingManager, tr); + // Test instructions + List tmpList = getTurnDescriptions(wayList); + assertEquals(Arrays.asList("continue onto MainStreet 1 2", + "At roundabout, take exit 3 onto 5-8", + "arrive at destination"), + tmpList); + // Test Radian + double delta = roundaboutGraph.getAngle(1, 2, 5, 8); + RoundaboutInstruction instr = (RoundaboutInstruction) wayList.get(1); + assertEquals(delta, instr.getTurnAngle(), 0.01); + + // case of continuing a street through a roundabout + p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED). + calcPath(1, 7); + wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncodingManager, tr); + tmpList = getTurnDescriptions(wayList); + assertEquals(Arrays.asList("continue onto MainStreet 1 2", + "At roundabout, take exit 2 onto MainStreet 4 7", + "arrive at destination"), + tmpList); + // Test Radian + delta = roundaboutGraph.getAngle(1, 2, 4, 7); + instr = (RoundaboutInstruction) wayList.get(1); + assertEquals(delta, instr.getTurnAngle(), 0.01); } @Test public void testCalcInstructionsRoundaboutBegin() { - FlagEncoder encoder = mixedEncoders.getEncoder("car"); - ShortestWeighting weighting = new ShortestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); + ShortestWeighting weighting = new ShortestWeighting(mixedCarAccessEnc, mixedCarSpeedEnc); Path p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED) .calcPath(2, 8); assertTrue(p.isFound()); - InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncoders, tr); + InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncodingManager, tr); List tmpList = getTurnDescriptions(wayList); assertEquals(Arrays.asList("At roundabout, take exit 3 onto 5-8", "arrive at destination"), @@ -259,12 +262,11 @@ public void testCalcInstructionsRoundaboutBegin() { @Test public void testCalcInstructionsRoundaboutDirectExit() { roundaboutGraph.inverse3to9(); - FlagEncoder encoder = mixedEncoders.getEncoder("car"); - ShortestWeighting weighting = new ShortestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); + ShortestWeighting weighting = new ShortestWeighting(mixedCarAccessEnc, mixedCarSpeedEnc); Path p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED) .calcPath(6, 8); assertTrue(p.isFound()); - InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncoders, tr); + InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncodingManager, tr); List tmpList = getTurnDescriptions(wayList); assertEquals(Arrays.asList("continue onto 3-6", "At roundabout, take exit 3 onto 5-8", @@ -451,12 +453,11 @@ public void testCalcDistanceDetails() { @Test public void testCalcInstructionsRoundabout2() { roundaboutGraph.inverse3to6(); - FlagEncoder mixedCar = mixedEncoders.getEncoder("car"); - ShortestWeighting weighting = new ShortestWeighting(mixedCar.getAccessEnc(), mixedCar.getAverageSpeedEnc()); + ShortestWeighting weighting = new ShortestWeighting(mixedCarAccessEnc, mixedCarSpeedEnc); Path p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED) .calcPath(1, 8); assertTrue(p.isFound()); - InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncoders, tr); + InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncodingManager, tr); List tmpList = getTurnDescriptions(wayList); assertEquals(Arrays.asList("continue onto MainStreet 1 2", "At roundabout, take exit 2 onto 5-8", @@ -538,12 +539,11 @@ public void testCalcInstructionsRoundaboutIssue353() { @Test public void testCalcInstructionsRoundaboutClockwise() { roundaboutGraph.setRoundabout(true); - FlagEncoder mixedCar = mixedEncoders.getEncoder("car"); - ShortestWeighting weighting = new ShortestWeighting(mixedCar.getAccessEnc(), mixedCar.getAverageSpeedEnc()); + ShortestWeighting weighting = new ShortestWeighting(mixedCarAccessEnc, mixedCarSpeedEnc); Path p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED) .calcPath(1, 8); assertTrue(p.isFound()); - InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncoders, tr); + InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncodingManager, tr); List tmpList = getTurnDescriptions(wayList); assertEquals(Arrays.asList("continue onto MainStreet 1 2", "At roundabout, take exit 1 onto 5-8", @@ -558,12 +558,11 @@ public void testCalcInstructionsRoundaboutClockwise() { @Test public void testCalcInstructionsIgnoreContinue() { // Follow a couple of straight edges, including a name change - FlagEncoder mixedCar = mixedEncoders.getEncoder("car"); - ShortestWeighting weighting = new ShortestWeighting(mixedCar.getAccessEnc(), mixedCar.getAverageSpeedEnc()); + ShortestWeighting weighting = new ShortestWeighting(mixedCarAccessEnc, mixedCarSpeedEnc); Path p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED) .calcPath(4, 11); assertTrue(p.isFound()); - InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncoders, tr); + InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncodingManager, tr); // Contain only start and finish instruction, no CONTINUE assertEquals(2, wayList.size()); @@ -572,12 +571,11 @@ public void testCalcInstructionsIgnoreContinue() { @Test public void testCalcInstructionsIgnoreTurnIfNoAlternative() { // The street turns left, but there is not turn - FlagEncoder mixedCar = mixedEncoders.getEncoder("car"); - ShortestWeighting weighting = new ShortestWeighting(mixedCar.getAccessEnc(), mixedCar.getAverageSpeedEnc()); + ShortestWeighting weighting = new ShortestWeighting(mixedCarAccessEnc, mixedCarSpeedEnc); Path p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED) .calcPath(10, 12); assertTrue(p.isFound()); - InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncoders, tr); + InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncodingManager, tr); // Contain only start and finish instruction assertEquals(2, wayList.size()); @@ -853,12 +851,12 @@ public void testUTurnLeft() { na.setNode(6, 48.402422, 9.996067); na.setNode(7, 48.402604, 9.994962); - GHUtility.setSpeed(60, 0, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 0, carAccessEnc, carAvSpeedEnc, g.edge(1, 2).setDistance(5).setName("Olgastraße"), g.edge(2, 3).setDistance(5).setName("Olgastraße"), g.edge(6, 5).setDistance(5).setName("Olgastraße"), g.edge(5, 4).setDistance(5).setName("Olgastraße")); - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, carAccessEnc, carAvSpeedEnc, g.edge(2, 5).setDistance(5).setName("Neithardtstraße"), g.edge(5, 7).setDistance(5).setName("Neithardtstraße")); @@ -891,12 +889,12 @@ public void testUTurnRight() { na.setNode(6, -33.885692, 151.181445); na.setNode(7, -33.885692, 151.181445); - GHUtility.setSpeed(60, 0, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 0, carAccessEnc, carAvSpeedEnc, g.edge(1, 2).setDistance(5).setName("Parramatta Road"), g.edge(2, 3).setDistance(5).setName("Parramatta Road"), g.edge(4, 5).setDistance(5).setName("Parramatta Road"), g.edge(5, 6).setDistance(5).setName("Parramatta Road")); - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, carAccessEnc, carAvSpeedEnc, g.edge(2, 5).setDistance(5).setName("Larkin Street"), g.edge(5, 7).setDistance(5).setName("Larkin Street")); @@ -913,12 +911,11 @@ public void testUTurnRight() { @Test public void testCalcInstructionsForTurn() { // The street turns left, but there is not turn - FlagEncoder mixedCar = mixedEncoders.getEncoder("car"); - ShortestWeighting weighting = new ShortestWeighting(mixedCar.getAccessEnc(), mixedCar.getAverageSpeedEnc()); + ShortestWeighting weighting = new ShortestWeighting(mixedCarAccessEnc, mixedCarSpeedEnc); Path p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED) .calcPath(11, 13); assertTrue(p.isFound()); - InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncoders, tr); + InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncodingManager, tr); // Contain start, turn, and finish instruction assertEquals(3, wayList.size()); @@ -929,12 +926,11 @@ public void testCalcInstructionsForTurn() { @Test public void testCalcInstructionsForSlightTurnWithOtherSlightTurn() { // Test for a fork with two sligh turns. Since there are two sligh turns, show the turn instruction - FlagEncoder mixedCar = mixedEncoders.getEncoder("car"); - ShortestWeighting weighting = new ShortestWeighting(mixedCar.getAccessEnc(), mixedCar.getAverageSpeedEnc()); + ShortestWeighting weighting = new ShortestWeighting(mixedCarAccessEnc, mixedCarSpeedEnc); Path p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED) .calcPath(12, 16); assertTrue(p.isFound()); - InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncoders, tr); + InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncodingManager, tr); // Contain start, turn, and finish instruction assertEquals(3, wayList.size()); @@ -974,12 +970,11 @@ public void testCalcInstructionsForSlightTurnOntoDifferentStreet() { @Test public void testIgnoreInstructionsForSlightTurnWithOtherTurn() { // Test for a fork with one sligh turn and one actual turn. We are going along the slight turn. No turn instruction needed in this case - FlagEncoder mixedCar = mixedEncoders.getEncoder("car"); - ShortestWeighting weighting = new ShortestWeighting(mixedCar.getAccessEnc(), mixedCar.getAverageSpeedEnc()); + ShortestWeighting weighting = new ShortestWeighting(mixedCarAccessEnc, mixedCarSpeedEnc); Path p = new Dijkstra(roundaboutGraph.g, weighting, TraversalMode.NODE_BASED) .calcPath(16, 19); assertTrue(p.isFound()); - InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncoders, tr); + InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, mixedEncodingManager, tr); // Contain start, and finish instruction assertEquals(2, wayList.size()); @@ -1012,17 +1007,15 @@ private Graph generatePathDetailsGraph() { return graph; } - private static class RoundaboutGraph { + private class RoundaboutGraph { final BaseGraph g; final NodeAccess na; final EdgeIteratorState edge3to6, edge3to9; - final EncodingManager em; boolean clockwise = false; List roundaboutEdges = new LinkedList<>(); - private RoundaboutGraph(EncodingManager em) { - this.em = em; - g = new BaseGraph.Builder(em).create(); + private RoundaboutGraph() { + g = new BaseGraph.Builder(mixedEncodingManager).create(); na = g.getNodeAccess(); // 18 // 8 14 | @@ -1082,40 +1075,36 @@ private RoundaboutGraph(EncodingManager em) { bothDir.add(g.edge(17, 18).setDistance(5)); bothDir.add(g.edge(17, 19).setDistance(5)); - for (FlagEncoder encoder : em.fetchEdgeEncoders()) { - double speed = encoder.getMaxSpeed() / 2; - GHUtility.setSpeed(speed, speed, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), bothDir); - GHUtility.setSpeed(speed, 0, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), oneDir); + for (EdgeIteratorState edge : bothDir) { + GHUtility.setSpeed(70, 70, mixedCarAccessEnc, mixedCarSpeedEnc, edge); + GHUtility.setSpeed(7, 7, mixedFootAccessEnc, mixedFootSpeedEnc, edge); + } + for (EdgeIteratorState edge : oneDir) { + GHUtility.setSpeed(70, 0, mixedCarAccessEnc, mixedCarSpeedEnc, edge); + GHUtility.setSpeed(7, 0, mixedFootAccessEnc, mixedFootSpeedEnc, edge); } - setRoundabout(clockwise); inverse3to9(); } public void setRoundabout(boolean clockwise) { - BooleanEncodedValue mixedRoundabout = em.getBooleanEncodedValue(Roundabout.KEY); - for (FlagEncoder encoder : em.fetchEdgeEncoders()) { - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - for (EdgeIteratorState edge : roundaboutEdges) { - edge.set(accessEnc, clockwise).setReverse(accessEnc, !clockwise); - edge.set(mixedRoundabout, true); - } + BooleanEncodedValue mixedRoundabout = mixedEncodingManager.getBooleanEncodedValue(Roundabout.KEY); + for (EdgeIteratorState edge : roundaboutEdges) { + edge.set(mixedCarAccessEnc, clockwise).setReverse(mixedCarAccessEnc, !clockwise); + edge.set(mixedFootAccessEnc, clockwise).setReverse(mixedFootAccessEnc, !clockwise); + edge.set(mixedRoundabout, true); } this.clockwise = clockwise; } public void inverse3to9() { - for (FlagEncoder encoder : em.fetchEdgeEncoders()) { - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - edge3to9.set(accessEnc, !edge3to9.get(accessEnc)).setReverse(accessEnc, false); - } + edge3to9.set(mixedCarAccessEnc, !edge3to9.get(mixedCarAccessEnc)).setReverse(mixedCarAccessEnc, false); + edge3to9.set(mixedFootAccessEnc, !edge3to9.get(mixedFootAccessEnc)).setReverse(mixedFootAccessEnc, false); } public void inverse3to6() { - for (FlagEncoder encoder : em.fetchEdgeEncoders()) { - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - edge3to6.set(accessEnc, !edge3to6.get(accessEnc)).setReverse(accessEnc, true); - } + edge3to6.set(mixedCarAccessEnc, !edge3to6.get(mixedCarAccessEnc)).setReverse(mixedCarAccessEnc, true); + edge3to6.set(mixedFootAccessEnc, !edge3to6.get(mixedFootAccessEnc)).setReverse(mixedFootAccessEnc, true); } private double getAngle(int n1, int n2, int n3, int n4) { diff --git a/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java b/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java index a55756e10fd..21e07c15465 100644 --- a/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java @@ -20,11 +20,10 @@ import com.carrotsearch.hppc.IntArrayList; import com.graphhopper.json.Statement; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.EnumEncodedValue; -import com.graphhopper.routing.ev.RoadClass; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.ev.*; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.PriorityCode; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.PriorityWeighting; import com.graphhopper.routing.weighting.TurnCostProvider; import com.graphhopper.routing.weighting.custom.CustomModelParser; @@ -43,8 +42,10 @@ public class PriorityRoutingTest { @Test void testMaxPriority() { - FlagEncoder encoder = FlagEncoders.createBike(); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", false); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 4, 2, false); + DecimalEncodedValue priorityEnc = new DecimalEncodedValueImpl("priority", 4, PriorityCode.getFactor(1), false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).add(priorityEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 48.0, 11.0); @@ -55,16 +56,17 @@ void testMaxPriority() { na.setNode(5, 48.2, 11.1); // 0 - 1 - 2 - 3 // \- 4 - 5 -/ + final double maxSpeed = speedEnc.getNextStorableValue(30); double dist1 = 0; - dist1 += maxSpeedEdge(em, graph, 0, 1, encoder, 1.0).getDistance(); - dist1 += maxSpeedEdge(em, graph, 1, 2, encoder, 1.0).getDistance(); - dist1 += maxSpeedEdge(em, graph, 2, 3, encoder, 1.0).getDistance(); + dist1 += addEdge(em, graph, 0, 1, 1.0, accessEnc, speedEnc, priorityEnc, maxSpeed).getDistance(); + dist1 += addEdge(em, graph, 1, 2, 1.0, accessEnc, speedEnc, priorityEnc, maxSpeed).getDistance(); + dist1 += addEdge(em, graph, 2, 3, 1.0, accessEnc, speedEnc, priorityEnc, maxSpeed).getDistance(); final double maxPrio = PriorityCode.getFactor(PriorityCode.BEST.getValue()); double dist2 = 0; - dist2 += maxSpeedEdge(em, graph, 0, 4, encoder, maxPrio).getDistance(); - dist2 += maxSpeedEdge(em, graph, 4, 5, encoder, maxPrio).getDistance(); - dist2 += maxSpeedEdge(em, graph, 5, 3, encoder, maxPrio).getDistance(); + dist2 += addEdge(em, graph, 0, 4, maxPrio, accessEnc, speedEnc, priorityEnc, maxSpeed).getDistance(); + dist2 += addEdge(em, graph, 4, 5, maxPrio, accessEnc, speedEnc, priorityEnc, maxSpeed).getDistance(); + dist2 += addEdge(em, graph, 5, 3, maxPrio, accessEnc, speedEnc, priorityEnc, maxSpeed).getDistance(); // the routes 0-1-2-3 and 0-4-5-3 have similar distances (and use max speed everywhere) // ... but the shorter route 0-1-2-3 has smaller priority @@ -73,7 +75,7 @@ void testMaxPriority() { // A* and Dijkstra should yield the same path (the max priority must be taken into account by weighting.getMinWeight) { - PriorityWeighting weighting = new PriorityWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), encoder.getPriorityEnc(), null, new PMap(), TurnCostProvider.NO_TURN_COST_PROVIDER); + PriorityWeighting weighting = new PriorityWeighting(accessEnc, speedEnc, priorityEnc, null, new PMap(), TurnCostProvider.NO_TURN_COST_PROVIDER); Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); assertEquals(pathDijkstra.calcNodes(), pathAStar.calcNodes()); @@ -82,8 +84,8 @@ void testMaxPriority() { { CustomModel customModel = new CustomModel(); - CustomWeighting weighting = CustomModelParser.createWeighting(encoder.getAccessEnc(), - encoder.getAverageSpeedEnc(), encoder.getPriorityEnc(), encoder.getMaxSpeed(), em, + CustomWeighting weighting = CustomModelParser.createWeighting(accessEnc, + speedEnc, priorityEnc, maxSpeed, em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); @@ -95,8 +97,8 @@ void testMaxPriority() { CustomModel customModel = new CustomModel(); // now we even increase the priority in the custom model, which also needs to be accounted for in weighting.getMinWeight customModel.addToPriority(Statement.If("road_class == MOTORWAY", Statement.Op.MULTIPLY, 3)); - CustomWeighting weighting = CustomModelParser.createWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), - encoder.getPriorityEnc(), encoder.getMaxSpeed(), em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); + CustomWeighting weighting = CustomModelParser.createWeighting(accessEnc, speedEnc, + priorityEnc, maxSpeed, em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); assertEquals(pathDijkstra.calcNodes(), pathAStar.calcNodes()); @@ -104,14 +106,11 @@ void testMaxPriority() { } } - private EdgeIteratorState maxSpeedEdge(EncodingManager em, BaseGraph graph, int p, int q, FlagEncoder encoder, double prio) { - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); - DecimalEncodedValue priorityEnc = em.getDecimalEncodedValue(EncodingManager.getKey(encoder, "priority")); + private EdgeIteratorState addEdge(EncodingManager em, BaseGraph graph, int p, int q, double prio, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, DecimalEncodedValue priorityEnc, double maxSpeed) { EnumEncodedValue roadClassEnc = em.getEnumEncodedValue(RoadClass.KEY, RoadClass.class); return graph.edge(p, q) .set(accessEnc, true) - .set(speedEnc, encoder.getMaxSpeed()) + .set(speedEnc, maxSpeed) .set(priorityEnc, prio) .set(roadClassEnc, RoadClass.MOTORWAY) .setDistance(calcDist(graph, p, q)); diff --git a/core/src/test/java/com/graphhopper/routing/QueryRoutingCHGraphTest.java b/core/src/test/java/com/graphhopper/routing/QueryRoutingCHGraphTest.java index 7ce674a10c8..f4d740a77b5 100644 --- a/core/src/test/java/com/graphhopper/routing/QueryRoutingCHGraphTest.java +++ b/core/src/test/java/com/graphhopper/routing/QueryRoutingCHGraphTest.java @@ -19,13 +19,10 @@ package com.graphhopper.routing; import com.graphhopper.routing.ch.PrepareEncoder; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.querygraph.QueryRoutingCHGraph; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.storage.*; @@ -33,7 +30,6 @@ import com.graphhopper.util.DistancePlaneProjection; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.GHUtility; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -44,7 +40,9 @@ import static org.junit.jupiter.api.Assertions.*; class QueryRoutingCHGraphTest { - private FlagEncoder encoder; + private BooleanEncodedValue accessEnc; + private DecimalEncodedValue speedEnc; + private DecimalEncodedValue turnCostEnc; private EncodingManager encodingManager; private FastestWeighting weighting; private BaseGraph graph; @@ -52,18 +50,20 @@ class QueryRoutingCHGraphTest { @BeforeEach public void setup() { - encoder = FlagEncoders.createCar(new PMap().putObject("max_turn_costs", 5).putObject("speed_two_directions", true)); - encodingManager = EncodingManager.create(encoder); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, true); + turnCostEnc = TurnCost.create("car", 5); + encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); graph = new BaseGraph.Builder(encodingManager).create(); - weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage())); + weighting = new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage())); na = graph.getNodeAccess(); } @Test public void basic() { // 0-1-2 - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(0, 1).setDistance(10)); - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(1, 2).setDistance(10)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(10)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 2).setDistance(10)); graph.freeze(); assertEquals(2, graph.getEdges()); @@ -100,8 +100,8 @@ public void basic() { public void withShortcuts() { // 0-1-2 // \-/ - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(0, 1).setDistance(10)); - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(1, 2).setDistance(10)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(10)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 2).setDistance(10)); graph.freeze(); assertEquals(2, graph.getEdges()); @@ -266,7 +266,7 @@ public void withVirtualEdgesAndShortcuts() { @Test public void getBaseGraph() { - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(0, 1).setDistance(10)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(10)); graph.freeze(); CHConfig chConfig = CHConfig.edgeBased("x", weighting); @@ -370,7 +370,7 @@ public void getWeight() { na.setNode(2, 50.00, 10.20); EdgeIteratorState edge = addEdge(graph, 0, 1) // use different speeds for the two directions - .set(encoder.getAverageSpeedEnc(), 90, 30); + .set(speedEnc, 90, 30); addEdge(graph, 1, 2); graph.freeze(); @@ -471,8 +471,8 @@ public void getWeight_withAccess() { // we set the access flags, but do use direction dependent speeds to make sure we are testing whether or not the // access flags are respected and the weight calculation does not simply rely on the speed, see this forum issue // https://discuss.graphhopper.com/t/speed-and-access-when-setbothdirections-true-false/5695 - edge.set(encoder.getAccessEnc(), true, false); - edge.set(encoder.getAverageSpeedEnc(), 60, 60); + edge.set(accessEnc, true, false); + edge.set(speedEnc, 60, 60); graph.freeze(); CHConfig chConfig = CHConfig.edgeBased("x", weighting); @@ -550,7 +550,6 @@ public void getTurnCost() { na.setNode(2, 50.00, 10.20); EdgeIteratorState edge1 = addEdge(graph, 0, 1); EdgeIteratorState edge2 = addEdge(graph, 1, 2); - DecimalEncodedValue turnCostEnc = encodingManager.getDecimalEncodedValue(TurnCost.key(encoder.toString())); graph.getTurnCostStorage().set(turnCostEnc, 0, 1, 1, 5); graph.freeze(); @@ -695,7 +694,7 @@ private void assertEnd(RoutingCHEdgeIterator outIter) { private EdgeIteratorState addEdge(Graph graph, int from, int to) { NodeAccess na = graph.getNodeAccess(); double dist = DistancePlaneProjection.DIST_PLANE.calcDist(na.getLat(from), na.getLon(from), na.getLat(to), na.getLon(to)); - return GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(from, to).setDistance(dist)); + return GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(from, to).setDistance(dist)); } } \ No newline at end of file diff --git a/core/src/test/java/com/graphhopper/routing/RoundTripRoutingTest.java b/core/src/test/java/com/graphhopper/routing/RoundTripRoutingTest.java index 82fe58b49c4..f0b41076a6c 100644 --- a/core/src/test/java/com/graphhopper/routing/RoundTripRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/RoundTripRoutingTest.java @@ -18,8 +18,15 @@ package com.graphhopper.routing; import com.carrotsearch.hppc.IntArrayList; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.querygraph.QueryGraph; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.FiniteWeightFilter; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.BaseGraph; @@ -46,9 +53,10 @@ * @author Peter Karich */ public class RoundTripRoutingTest { - private final FlagEncoder carFE = FlagEncoders.createCar(); - private final EncodingManager em = EncodingManager.create(carFE); - private final Weighting fastestWeighting = new FastestWeighting(carFE.getAccessEnc(), carFE.getAverageSpeedEnc()); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + private final EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); + private final Weighting fastestWeighting = new FastestWeighting(accessEnc, speedEnc); // TODO private final TraversalMode tMode = TraversalMode.EDGE_BASED; private final TraversalMode tMode = TraversalMode.NODE_BASED; private final GHPoint ghPoint1 = new GHPoint(0, 0); @@ -122,7 +130,7 @@ public void testCalcRoundTrip() { private BaseGraph createTestGraph() { BaseGraph graph = new BaseGraph.Builder(em).withTurnCosts(true).create(); - AlternativeRouteTest.initTestGraph(graph, carFE.getAccessEnc(), carFE.getAverageSpeedEnc()); + AlternativeRouteTest.initTestGraph(graph, accessEnc, speedEnc); return graph; } @@ -135,7 +143,7 @@ private BaseGraph createSquareGraph() { // |-1 0 1 BaseGraph graph = new BaseGraph.Builder(em).create(); for (int i = 0; i < 8; ++i) { - GHUtility.setSpeed(60, true, true, carFE.getAccessEnc(), carFE.getAverageSpeedEnc(), graph.edge(i, (i + 1) % 8).setDistance(1)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(i, (i + 1) % 8).setDistance(1)); } updateDistancesFor(graph, 0, 1, -1); updateDistancesFor(graph, 1, 1, 0); diff --git a/core/src/test/java/com/graphhopper/routing/lm/LMPreparationHandlerTest.java b/core/src/test/java/com/graphhopper/routing/lm/LMPreparationHandlerTest.java index 8fe2d7e389e..30f95866607 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/LMPreparationHandlerTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/LMPreparationHandlerTest.java @@ -3,9 +3,11 @@ import com.graphhopper.GraphHopperConfig; import com.graphhopper.config.LMProfile; import com.graphhopper.config.Profile; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.storage.BaseGraph; @@ -35,11 +37,12 @@ public void maximumLMWeight() { new LMProfile("conf1").setMaximumLMWeight(65_000), new LMProfile("conf2").setMaximumLMWeight(20_000) ); - FlagEncoder car = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(car); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", false); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); List lmConfigs = Arrays.asList( - new LMConfig("conf1", new FastestWeighting(car.getAccessEnc(), car.getAverageSpeedEnc())), - new LMConfig("conf2", new ShortestWeighting(car.getAccessEnc(), car.getAverageSpeedEnc())) + new LMConfig("conf1", new FastestWeighting(accessEnc, speedEnc)), + new LMConfig("conf2", new ShortestWeighting(accessEnc, speedEnc)) ); List preparations = handler.createPreparations(lmConfigs, new BaseGraph.Builder(em).build(), em, null); assertEquals(1, preparations.get(0).getLandmarkStorage().getFactor(), .1); diff --git a/core/src/test/java/com/graphhopper/routing/lm/PrepareLandmarksTest.java b/core/src/test/java/com/graphhopper/routing/lm/PrepareLandmarksTest.java index 3640461d841..f3d9cef2a3e 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/PrepareLandmarksTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/PrepareLandmarksTest.java @@ -21,11 +21,11 @@ import com.graphhopper.routing.AlgorithmOptions; import com.graphhopper.routing.Path; import com.graphhopper.routing.RoutingAlgorithm; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.Subnetwork; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.querygraph.QueryGraph; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.BaseGraph; @@ -56,17 +56,19 @@ * @author Peter Karich */ public class PrepareLandmarksTest { + private BooleanEncodedValue accessEnc; + private DecimalEncodedValue speedEnc; + private EncodingManager encodingManager; private BaseGraph graph; - private FlagEncoder encoder; private TraversalMode tm; - private EncodingManager encodingManager; @BeforeEach public void setUp() { - encoder = FlagEncoders.createCar(); - tm = TraversalMode.NODE_BASED; - encodingManager = new EncodingManager.Builder().add(encoder).add(Subnetwork.create("car")).build(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("car")).build(); graph = new BaseGraph.Builder(encodingManager).create(); + tm = TraversalMode.NODE_BASED; } @Test @@ -77,8 +79,6 @@ public void testLandmarkStorageAndRouting() { Random rand = new Random(0); int width = 15, height = 15; - DecimalEncodedValue avSpeedEnc = encoder.getAverageSpeedEnc(); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); for (int hIndex = 0; hIndex < height; hIndex++) { for (int wIndex = 0; wIndex < width; wIndex++) { int node = wIndex + hIndex * width; @@ -86,11 +86,11 @@ public void testLandmarkStorageAndRouting() { // do not connect first with last column! double speed = 20 + rand.nextDouble() * 30; if (wIndex + 1 < width) - graph.edge(node, node + 1).set(accessEnc, true, true).set(avSpeedEnc, speed); + graph.edge(node, node + 1).set(accessEnc, true, true).set(speedEnc, speed); // avoid dead ends if (hIndex + 1 < height) - graph.edge(node, node + width).set(accessEnc, true, true).set(avSpeedEnc, speed); + graph.edge(node, node + width).set(accessEnc, true, true).set(speedEnc, speed); updateDistancesFor(graph, node, -hIndex / 50.0, wIndex / 50.0); } @@ -100,7 +100,7 @@ public void testLandmarkStorageAndRouting() { index.prepareIndex(); int lm = 5, activeLM = 2; - Weighting weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc); LMConfig lmConfig = new LMConfig("car", weighting); LandmarkStorage store = new LandmarkStorage(graph, encodingManager, dir, lmConfig, lm); store.setMinimumNodes(2); @@ -184,13 +184,13 @@ public void testLandmarkStorageAndRouting() { @Test public void testStoreAndLoad() { - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(0, 1).setDistance(80_000)); - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(1, 2).setDistance(80_000)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(80_000)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 2).setDistance(80_000)); String fileStr = "./target/tmp-lm"; Helper.removeDir(new File(fileStr)); Directory dir = new RAMDirectory(fileStr, true).create(); - Weighting weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc); LMConfig lmConfig = new LMConfig("car", weighting); PrepareLandmarks plm = new PrepareLandmarks(dir, graph, encodingManager, lmConfig, 2); plm.setMinimumNodes(2); diff --git a/core/src/test/java/com/graphhopper/routing/util/FootTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/FootTagParserTest.java index 3afa22d6461..157a8f26615 100644 --- a/core/src/test/java/com/graphhopper/routing/util/FootTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/FootTagParserTest.java @@ -20,8 +20,7 @@ import com.graphhopper.reader.ReaderNode; import com.graphhopper.reader.ReaderWay; import com.graphhopper.reader.osm.conditional.DateRangeParser; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.*; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.IntsRef; import com.graphhopper.util.*; @@ -36,13 +35,19 @@ * @author Peter Karich */ public class FootTagParserTest { - private final EncodingManager encodingManager = EncodingManager.create("car,bike,foot"); - private final FlagEncoder encoder = encodingManager.getEncoder("foot"); + private final BooleanEncodedValue footAccessEnc = new SimpleBooleanEncodedValue("foot_access", true); + private final DecimalEncodedValue footAvgSpeedEnc = new DecimalEncodedValueImpl("foot_average_speed", 4, 1, false); + private final DecimalEncodedValue footPriorityEnc = new DecimalEncodedValueImpl("foot_priority", 4, PriorityCode.getFactor(1), false); + private final BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); + private final DecimalEncodedValue bikeAvgSpeedEnc = new DecimalEncodedValueImpl("bike_average_speed", 4, 2, false); + private final BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + private final DecimalEncodedValue carAvSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + private final EncodingManager encodingManager = EncodingManager.start() + .add(footAccessEnc).add(footAvgSpeedEnc).add(footPriorityEnc).add(new EnumEncodedValue<>(FootNetwork.KEY, RouteNetwork.class)) + .add(bikeAccessEnc).add(bikeAvgSpeedEnc).add(new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class)) + .add(carAccessEnc).add(carAvSpeedEnc) + .build(); private final FootTagParser footParser = new FootTagParser(encodingManager, new PMap()); - private final DecimalEncodedValue footAvgSpeedEnc = encoder.getAverageSpeedEnc(); - private final BooleanEncodedValue footAccessEnc = encoder.getAccessEnc(); - private final DecimalEncodedValue carAvSpeedEnc = encodingManager.getEncoder("car").getAverageSpeedEnc(); - private final BooleanEncodedValue carAccessEnc = encodingManager.getEncoder("car").getAccessEnc(); public FootTagParserTest() { footParser.init(new DateRangeParser()); diff --git a/core/src/test/java/com/graphhopper/routing/util/parsers/OSMTurnRelationParserTest.java b/core/src/test/java/com/graphhopper/routing/util/parsers/OSMTurnRelationParserTest.java index c5a3fab4544..03bb29050a6 100644 --- a/core/src/test/java/com/graphhopper/routing/util/parsers/OSMTurnRelationParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/parsers/OSMTurnRelationParserTest.java @@ -1,17 +1,12 @@ package com.graphhopper.routing.util.parsers; import com.graphhopper.reader.OSMTurnRelation; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.util.TransportationMode; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.TurnCostStorage; import com.graphhopper.util.GHUtility; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.Test; import java.util.HashMap; @@ -24,7 +19,10 @@ public class OSMTurnRelationParserTest { @Test public void testGetRestrictionAsEntries() { - FlagEncoder encoder = FlagEncoders.createCar(new PMap("turn_costs=true")); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + DecimalEncodedValue turnCostEnc = TurnCost.create("car", 1); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); final Map osmNodeToInternal = new HashMap<>(); final Map internalToOSMEdge = new HashMap<>(); @@ -33,11 +31,9 @@ public void testGetRestrictionAsEntries() { internalToOSMEdge.put(3, 3L); internalToOSMEdge.put(4, 4L); - EncodingManager em = EncodingManager.create(encoder); - DecimalEncodedValue tce = encoder.getDecimalEncodedValue(TurnCost.key("car")); - OSMTurnRelationParser parser = new OSMTurnRelationParser(encoder.getAccessEnc(), tce, OSMRoadAccessParser.toOSMRestrictions(TransportationMode.CAR)); + OSMTurnRelationParser parser = new OSMTurnRelationParser(accessEnc, turnCostEnc, OSMRoadAccessParser.toOSMRestrictions(TransportationMode.CAR)); BaseGraph graph = new BaseGraph.Builder(em.getIntsForFlags()).withTurnCosts(true).create(); - initGraph(graph, encoder); + initGraph(graph, accessEnc, speedEnc); TurnCostParser.ExternalInternalMap map = new TurnCostParser.ExternalInternalMap() { @Override @@ -59,14 +55,14 @@ public long getOsmIdOfInternalEdge(int edgeId) { parser.addRelationToTCStorage(instance, map, graph); TurnCostStorage tcs = graph.getTurnCostStorage(); - assertTrue(Double.isInfinite(tcs.get(tce, 4, 3, 6))); - assertEquals(0, tcs.get(tce, 4, 3, 3), .1); - assertTrue(Double.isInfinite(tcs.get(tce, 4, 3, 2))); + assertTrue(Double.isInfinite(tcs.get(turnCostEnc, 4, 3, 6))); + assertEquals(0, tcs.get(turnCostEnc, 4, 3, 3), .1); + assertTrue(Double.isInfinite(tcs.get(turnCostEnc, 4, 3, 2))); // TYPE == NOT instance = new OSMTurnRelation(4, 3, 3, OSMTurnRelation.Type.NOT); parser.addRelationToTCStorage(instance, map, graph); - assertTrue(Double.isInfinite(tcs.get(tce, 4, 3, 3))); + assertTrue(Double.isInfinite(tcs.get(turnCostEnc, 4, 3, 3))); } // 0---1 @@ -74,9 +70,7 @@ public long getOsmIdOfInternalEdge(int edgeId) { // 2--3--4 // | | | // 5--6--7 - private static void initGraph(BaseGraph graph, FlagEncoder encoder) { - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); + private static void initGraph(BaseGraph graph, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1).setDistance(3)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 2).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 3).setDistance(1)); diff --git a/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java b/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java index 74343c16343..a7723b48960 100644 --- a/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java +++ b/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java @@ -20,7 +20,12 @@ import com.carrotsearch.hppc.IntArrayList; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; +import com.graphhopper.routing.util.AccessFilter; +import com.graphhopper.routing.util.AllEdgesIterator; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.storage.*; import com.graphhopper.util.*; import com.graphhopper.util.shapes.BBox; @@ -38,9 +43,11 @@ * @author Peter Karich */ public class LocationIndexTreeTest { - protected final EncodingManager encodingManager = EncodingManager.create("car"); + private final BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + private final DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + protected final EncodingManager encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); - public static void initSimpleGraph(Graph g, EncodingManager em) { + public static void initSimpleGraph(Graph g) { // 6 | 4 // 5 | // | 6 @@ -70,10 +77,6 @@ public static void initSimpleGraph(Graph g, EncodingManager em) { g.edge(3, 5), // make sure 6 is connected g.edge(6, 4)); - for (FlagEncoder encoder : em.fetchEdgeEncoders()) { - double speed = encoder.getMaxSpeed() / 2; - GHUtility.setSpeed(speed, speed, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), list); - } } private LocationIndexTree createIndexNoPrepare(Graph g, int resolution) { @@ -88,8 +91,7 @@ private LocationIndexTree createIndexNoPrepare(Graph g, int resolution) { // |1----3-\| // |____/ 4 // 2-------/ - Graph createTestGraph(EncodingManager em) { - FlagEncoder encoder = em.getEncoder("car"); + Graph createTestGraph(EncodingManager em, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { BaseGraph graph = new BaseGraph.Builder(em).create(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 0.5, -0.5); @@ -97,8 +99,6 @@ Graph createTestGraph(EncodingManager em) { na.setNode(2, -1, -1); na.setNode(3, -0.4, 0.9); na.setNode(4, -0.6, 1.6); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 2)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 4)); @@ -111,7 +111,7 @@ Graph createTestGraph(EncodingManager em) { @Test public void testSnappedPointAndGeometry() { - Graph graph = createTestGraph(encodingManager); + Graph graph = createTestGraph(encodingManager, accessEnc, speedEnc); LocationIndex index = createIndexNoPrepare(graph, 500000).prepareIndex(); // query directly the tower node Snap res = index.findClosest(-0.4, 0.9, EdgeFilter.ALL_EDGES); @@ -151,16 +151,13 @@ public void testBoundingBoxQuery1() { @Test public void testMoreReal() { - FlagEncoder encoder = FlagEncoders.createCar(); - BaseGraph graph = new BaseGraph.Builder(EncodingManager.create(encoder)).create(); + BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); NodeAccess na = graph.getNodeAccess(); na.setNode(1, 51.2492152, 9.4317166); na.setNode(0, 52, 9); na.setNode(2, 51.2, 9.4); na.setNode(3, 49, 10); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 0)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 2)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 3)).setWayGeometry(Helper.createPointList(51.21, 9.43)); @@ -179,15 +176,12 @@ public void testMoreReal() { // | private Graph createTestGraphWithWayGeometry() { BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); - FlagEncoder encoder = encodingManager.getEncoder("car"); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 0.5, -0.5); na.setNode(1, -0.5, -0.5); na.setNode(2, -1, -1); na.setNode(3, -0.4, 0.9); na.setNode(4, -0.6, 1.6); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(0, 2)); // insert A and B, without this we would get 0 for 0,0 @@ -212,15 +206,14 @@ public void testWayGeometry() { @Test public void testFindingWayGeometry() { BaseGraph g = new BaseGraph.Builder(encodingManager).create(); - FlagEncoder encoder = encodingManager.getEncoder("car"); NodeAccess na = g.getNodeAccess(); na.setNode(10, 51.2492152, 9.4317166); na.setNode(20, 52, 9); na.setNode(30, 51.2, 9.4); na.setNode(50, 49, 10); - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), g.edge(20, 50)).setWayGeometry(Helper.createPointList(51.25, 9.43)); - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), g.edge(10, 20)); - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), g.edge(20, 30)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(20, 50)).setWayGeometry(Helper.createPointList(51.25, 9.43)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(10, 20)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(20, 30)); LocationIndex index = createIndexNoPrepare(g, 2000).prepareIndex(); assertEquals(0, findClosestEdge(index, 51.25, 9.43)); @@ -228,7 +221,7 @@ public void testFindingWayGeometry() { @Test public void testEdgeFilter() { - Graph graph = createTestGraph(encodingManager); + Graph graph = createTestGraph(encodingManager, accessEnc, speedEnc); LocationIndexTree index = (LocationIndexTree) createIndexNoPrepare(graph, 500000).prepareIndex(); assertEquals(1, index.findClosest(-.6, -.6, EdgeFilter.ALL_EDGES).getClosestNode()); @@ -237,7 +230,6 @@ public void testEdgeFilter() { // see testgraph2.jpg Graph createTestGraph2() { - FlagEncoder encoder = encodingManager.getEncoder("car"); BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); NodeAccess na = graph.getNodeAccess(); @@ -289,7 +281,7 @@ Graph createTestGraph2() { // top right na.setNode(101, 49.96053, 11.58814); - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, graph.edge(0, 1), graph.edge(1, 2), graph.edge(2, 3), @@ -333,7 +325,7 @@ Graph createTestGraph2() { @Test public void testRMin() { - Graph graph = createTestGraph(encodingManager); + Graph graph = createTestGraph(encodingManager, accessEnc, speedEnc); LocationIndexTree index = (LocationIndexTree) createIndexNoPrepare(graph, 50000).prepareIndex(); DistanceCalc distCalc = new DistancePlaneProjection(); double rmin2 = index.calculateRMin(0.05, -0.3, 1); @@ -343,10 +335,12 @@ public void testRMin() { @Test public void testSearchWithFilter_issue318() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - FlagEncoder bikeEncoder = FlagEncoders.createBike(); + BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); + DecimalEncodedValue bikeSpeedEnc = new DecimalEncodedValueImpl("bike_speed", 4, 2, false); - EncodingManager tmpEM = EncodingManager.create(carEncoder, bikeEncoder); + EncodingManager tmpEM = EncodingManager.start().add(carAccessEnc).add(carSpeedEnc).add(bikeAccessEnc).add(bikeSpeedEnc).build(); BaseGraph graph = new BaseGraph.Builder(tmpEM).create(); NodeAccess na = graph.getNodeAccess(); @@ -357,33 +351,32 @@ public void testSearchWithFilter_issue318() { int index = lonIdx * 10 + latIdx; na.setNode(index, 0.01 * latIdx, 0.01 * lonIdx); if (latIdx < MAX - 1) - GHUtility.setSpeed(60, true, true, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc(), graph.edge(index, index + 1)); + GHUtility.setSpeed(60, true, true, carAccessEnc, carSpeedEnc, graph.edge(index, index + 1)); if (lonIdx < MAX - 1) - GHUtility.setSpeed(60, true, true, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc(), graph.edge(index, index + 10)); + GHUtility.setSpeed(60, true, true, carAccessEnc, carSpeedEnc, graph.edge(index, index + 10)); } } // reduce access for bike to two edges only AllEdgesIterator iter = graph.getAllEdges(); - BooleanEncodedValue accessEnc = bikeEncoder.getAccessEnc(); while (iter.next()) { - iter.set(accessEnc, false, false); + iter.set(bikeAccessEnc, false, false); } for (EdgeIteratorState edge : Arrays.asList(GHUtility.getEdge(graph, 0, 1), GHUtility.getEdge(graph, 1, 2))) { - edge.set(accessEnc, true, true); + edge.set(bikeAccessEnc, true, true); } LocationIndexTree index = createIndexNoPrepare(graph, 500); index.prepareIndex(); index.setMaxRegionSearch(8); - EdgeFilter carFilter = AccessFilter.allEdges(carEncoder.getAccessEnc()); + EdgeFilter carFilter = AccessFilter.allEdges(carAccessEnc); Snap snap = index.findClosest(0.03, 0.03, carFilter); assertTrue(snap.isValid()); assertEquals(33, snap.getClosestNode()); - EdgeFilter bikeFilter = AccessFilter.allEdges(bikeEncoder.getAccessEnc()); + EdgeFilter bikeFilter = AccessFilter.allEdges(bikeAccessEnc); snap = index.findClosest(0.03, 0.03, bikeFilter); assertTrue(snap.isValid()); assertEquals(2, snap.getClosestNode()); @@ -394,7 +387,6 @@ public void testSearchWithFilter_issue318() { // 4--5--6--7 @Test public void testCrossBoundaryNetwork_issue667() { - FlagEncoder encoder = encodingManager.getEncoder("car"); BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 0.1, 179.5); @@ -407,7 +399,7 @@ public void testCrossBoundaryNetwork_issue667() { na.setNode(7, 0, -179.5); // just use 1 as distance which is incorrect but does not matter in this unit case - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, graph.edge(0, 1), graph.edge(0, 4), graph.edge(1, 5), @@ -421,9 +413,9 @@ public void testCrossBoundaryNetwork_issue667() { // as last edges: create cross boundary edges // See #667 where the recommendation is to adjust the import and introduce two pillar nodes // where the connection is cross boundary and would be okay if ignored as real length is 0 - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(1, 2)).setWayGeometry(Helper.createPointList(0, 180, 0, -180)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(1, 2)).setWayGeometry(Helper.createPointList(0, 180, 0, -180)); // but this unit test succeeds even without this adjusted import: - GHUtility.setSpeed(60, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), graph.edge(5, 6)); + GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(5, 6)); LocationIndexTree index = createIndexNoPrepare(graph, 500); index.prepareIndex(); @@ -447,10 +439,14 @@ private int findClosestEdge(LocationIndex index, double lat, double lon) { @Test public void testSimpleGraph() { - EncodingManager em = EncodingManager.create("car"); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph g = new BaseGraph.Builder(em).create(); - initSimpleGraph(g, em); - + initSimpleGraph(g); + AllEdgesIterator edge = g.getAllEdges(); + while (edge.next()) + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, edge); LocationIndexTree idx = (LocationIndexTree) createIndexNoPrepare(g, 500000).prepareIndex(); assertEquals(3, findClosestEdge(idx, 5, 2)); assertEquals(3, findClosestEdge(idx, 1.5, 2)); @@ -461,9 +457,14 @@ public void testSimpleGraph() { @Test public void testSimpleGraph2() { - EncodingManager em = EncodingManager.create("car"); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph g = new BaseGraph.Builder(em).create(); - initSimpleGraph(g, em); + initSimpleGraph(g); + AllEdgesIterator edge = g.getAllEdges(); + while (edge.next()) + GHUtility.setSpeed(60, 60, accessEnc, speedEnc, edge); LocationIndexTree idx = (LocationIndexTree) createIndexNoPrepare(g, 500000).prepareIndex(); assertEquals(3, findClosestEdge(idx, 5, 2)); @@ -479,7 +480,7 @@ public void testSimpleGraph2() { @Test public void testSinglePoints120() { - BaseGraph g = createSampleGraph(EncodingManager.create("car")); + BaseGraph g = createSampleGraph(encodingManager, accessEnc, speedEnc); LocationIndexTree idx = (LocationIndexTree) createIndexNoPrepare(g, 500000).prepareIndex(); assertEquals(3, findClosestEdge(idx, 1.637, 2.23)); @@ -494,7 +495,7 @@ public void testSinglePoints120() { @Test public void testSinglePoints32() { - BaseGraph g = createSampleGraph(EncodingManager.create("car")); + BaseGraph g = createSampleGraph(encodingManager, accessEnc, speedEnc); LocationIndexTree idx = (LocationIndexTree) createIndexNoPrepare(g, 500000).prepareIndex(); assertEquals(10, findClosestEdge(idx, 3.649, 1.375)); @@ -518,7 +519,7 @@ public void testNoErrorOnEdgeCase_lastIndex() { g.close(); } - public BaseGraph createSampleGraph(EncodingManager encodingManager) { + public BaseGraph createSampleGraph(EncodingManager encodingManager, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { BaseGraph graph = new BaseGraph.Builder(encodingManager).create(); // length does not matter here but lat,lon and outgoing edges do! @@ -574,9 +575,6 @@ public BaseGraph createSampleGraph(EncodingManager encodingManager) { na.setNode(16, 5, 5); // => 17 locations - FlagEncoder encoder = encodingManager.getEncoder("car"); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(a0, b1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(c2, b1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, graph.edge(c2, d3)); @@ -602,23 +600,28 @@ public BaseGraph createSampleGraph(EncodingManager encodingManager) { @Test public void testDifferentVehicles() { - final EncodingManager encodingManager = EncodingManager.create("car,foot"); - BaseGraph g = new BaseGraph.Builder(encodingManager).create(); - initSimpleGraph(g, encodingManager); + BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + BooleanEncodedValue footAccessEnc = new SimpleBooleanEncodedValue("foot_access", true); + DecimalEncodedValue footSpeedEnc = new DecimalEncodedValueImpl("foot_speed", 4, 1, false); + EncodingManager em = EncodingManager.start().add(carAccessEnc).add(carSpeedEnc).add(footAccessEnc).add(footSpeedEnc).build(); + BaseGraph g = new BaseGraph.Builder(em).create(); + initSimpleGraph(g); + AllEdgesIterator edge = g.getAllEdges(); + while (edge.next()) { + GHUtility.setSpeed(60, 60, carAccessEnc, carSpeedEnc, edge); + GHUtility.setSpeed(10, 10, footAccessEnc, footSpeedEnc, edge); + } LocationIndexTree idx = (LocationIndexTree) createIndexNoPrepare(g, 500000).prepareIndex(); assertEquals(0, findClosestEdge(idx, 1, -1)); // now make all edges from node 1 accessible for CAR only EdgeIterator iter = g.createEdgeExplorer().setBaseNode(1); - FlagEncoder encoder = encodingManager.getEncoder("foot"); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - while (iter.next()) { - iter.set(accessEnc, false, false); - } + while (iter.next()) + iter.set(footAccessEnc, false, false); idx = (LocationIndexTree) createIndexNoPrepare(g, 500000).prepareIndex(); - FlagEncoder footEncoder = encodingManager.getEncoder("foot"); - assertEquals(2, idx.findClosest(1, -1, AccessFilter.allEdges(footEncoder.getAccessEnc())).getClosestNode()); + assertEquals(2, idx.findClosest(1, -1, AccessFilter.allEdges(footAccessEnc)).getClosestNode()); g.close(); } diff --git a/core/src/test/java/com/graphhopper/util/InstructionListTest.java b/core/src/test/java/com/graphhopper/util/InstructionListTest.java index 67346b66800..87c3b87ab06 100644 --- a/core/src/test/java/com/graphhopper/util/InstructionListTest.java +++ b/core/src/test/java/com/graphhopper/util/InstructionListTest.java @@ -22,13 +22,9 @@ import com.graphhopper.routing.Dijkstra; import com.graphhopper.routing.InstructionsFromEdges; import com.graphhopper.routing.Path; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.EnumEncodedValue; -import com.graphhopper.routing.ev.RoadClass; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; +import com.graphhopper.routing.util.PriorityCode; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; @@ -56,16 +52,14 @@ public class InstructionListTest { private static final Translation usTR = trMap.getWithFallBack(Locale.US); private final TraversalMode tMode = TraversalMode.NODE_BASED; private EncodingManager carManager; - private FlagEncoder carEncoder; private BooleanEncodedValue accessEnc; private DecimalEncodedValue speedEnc; @BeforeEach public void setUp() { - carEncoder = FlagEncoders.createCar(); - carManager = EncodingManager.create(carEncoder); - accessEnc = carEncoder.getAccessEnc(); - speedEnc = carEncoder.getAverageSpeedEnc(); + accessEnc = new SimpleBooleanEncodedValue("access", true); + speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + carManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); } private static List getTurnDescriptions(InstructionList instructionList) { @@ -307,8 +301,9 @@ public void testNoInstructionIfSlightTurnAndAlternativeIsSharp2() { @Test public void testNoInstructionIfSlightTurnAndAlternativeIsSharp3() { - FlagEncoder bike = FlagEncoders.createBike(); - EncodingManager tmpEM = new EncodingManager.Builder().add(bike).build(); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 4, 2, false); + EncodingManager tmpEM = new EncodingManager.Builder().add(accessEnc).add(speedEnc).build(); EnumEncodedValue rcEV = tmpEM.getEnumEncodedValue(RoadClass.KEY, RoadClass.class); BaseGraph g = new BaseGraph.Builder(tmpEM).create(); // real world example: https://graphhopper.com/maps/?point=48.411549,15.599567&point=48.411663%2C15.600527&profile=bike @@ -326,15 +321,15 @@ public void testNoInstructionIfSlightTurnAndAlternativeIsSharp3() { na.setNode(3, 48.411610, 15.600409); na.setNode(4, 48.411322, 15.600459); - GHUtility.setSpeed(18, true, true, bike.getAccessEnc(), bike.getAverageSpeedEnc(), g.edge(1, 2).setDistance(20)); - GHUtility.setSpeed(18, true, true, bike.getAccessEnc(), bike.getAverageSpeedEnc(), g.edge(2, 3).setDistance(20)); - GHUtility.setSpeed(4, true, true, bike.getAccessEnc(), bike.getAverageSpeedEnc(), g.edge(2, 4).setDistance(20)); + GHUtility.setSpeed(18, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(20)); + GHUtility.setSpeed(18, true, true, accessEnc, speedEnc, g.edge(2, 3).setDistance(20)); + GHUtility.setSpeed(4, true, true, accessEnc, speedEnc, g.edge(2, 4).setDistance(20)); g.edge(1, 2).set(rcEV, RoadClass.RESIDENTIAL).setName("pfarr"); g.edge(2, 3).set(rcEV, RoadClass.RESIDENTIAL).setName("pfarr"); g.edge(2, 4).set(rcEV, RoadClass.PEDESTRIAN).setName("markt"); - FastestWeighting weighting = new FastestWeighting(bike.getAccessEnc(), bike.getAverageSpeedEnc()); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); Path p = new Dijkstra(g, weighting, tMode).calcPath(1, 3); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, tmpEM, usTR); List tmpList = getTurnDescriptions(wayList); @@ -345,8 +340,9 @@ public void testNoInstructionIfSlightTurnAndAlternativeIsSharp3() { @Test public void testInstructionIfTurn() { - FlagEncoder bike = FlagEncoders.createBike(); - EncodingManager tmpEM = new EncodingManager.Builder().add(bike).build(); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 4, 2, false); + EncodingManager tmpEM = new EncodingManager.Builder().add(accessEnc).add(speedEnc).build(); EnumEncodedValue rcEV = tmpEM.getEnumEncodedValue(RoadClass.KEY, RoadClass.class); BaseGraph g = new BaseGraph.Builder(tmpEM).create(); // real world example: https://graphhopper.com/maps/?point=48.412169%2C15.604888&point=48.412251%2C15.60543&profile=bike @@ -364,15 +360,15 @@ public void testInstructionIfTurn() { na.setNode(3, 48.412614, 15.604872); na.setNode(4, 48.412148, 15.605543); - GHUtility.setSpeed(18, true, true, bike.getAccessEnc(), bike.getAverageSpeedEnc(), g.edge(1, 2).setDistance(20)); - GHUtility.setSpeed(18, true, true, bike.getAccessEnc(), bike.getAverageSpeedEnc(), g.edge(2, 3).setDistance(20)); - GHUtility.setSpeed(18, true, true, bike.getAccessEnc(), bike.getAverageSpeedEnc(), g.edge(2, 4).setDistance(20)); + GHUtility.setSpeed(18, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(20)); + GHUtility.setSpeed(18, true, true, accessEnc, speedEnc, g.edge(2, 3).setDistance(20)); + GHUtility.setSpeed(18, true, true, accessEnc, speedEnc, g.edge(2, 4).setDistance(20)); g.edge(1, 2).set(rcEV, RoadClass.RESIDENTIAL).setName("land"); g.edge(2, 3).set(rcEV, RoadClass.SECONDARY).setName("ring"); g.edge(2, 4).set(rcEV, RoadClass.SECONDARY).setName("ring"); - FastestWeighting weighting = new FastestWeighting(bike.getAccessEnc(), bike.getAverageSpeedEnc()); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); Path p = new Dijkstra(g, weighting, tMode).calcPath(1, 4); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, tmpEM, usTR); List tmpList = getTurnDescriptions(wayList); @@ -383,8 +379,10 @@ public void testInstructionIfTurn() { @Test public void testInstructionIfSlightTurnForCustomProfile() { - FlagEncoder foot = FlagEncoders.createFoot(); - EncodingManager tmpEM = new EncodingManager.Builder().add(foot).build(); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 4, 1, false); + DecimalEncodedValue priorityEnc = new DecimalEncodedValueImpl("priority", 4, PriorityCode.getFactor(1), false); + EncodingManager tmpEM = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(priorityEnc).build(); BaseGraph g = new BaseGraph.Builder(tmpEM).create(); // real world example: https://graphhopper.com/maps/?point=43.729379,7.417697&point=43.729798,7.417263&profile=foot // From 4 to 3 and 4 to 1 @@ -403,15 +401,15 @@ public void testInstructionIfSlightTurnForCustomProfile() { na.setNode(3, 43.729821, 7.41725); na.setNode(4, 43.729476, 7.417633); - DecimalEncodedValue priorityEnc = tmpEM.getDecimalEncodedValue(EncodingManager.getKey(foot.toString(), "priority")); // default is priority=0 so set it to 1 - GHUtility.setSpeed(5, true, true, foot.getAccessEnc(), foot.getAverageSpeedEnc(), g.edge(1, 2).setDistance(20).setName("myroad").set(priorityEnc, 1)); - GHUtility.setSpeed(5, true, true, foot.getAccessEnc(), foot.getAverageSpeedEnc(), g.edge(2, 3).setDistance(20).setName("myroad").set(priorityEnc, 1)); + // default is priority=0 so set it to 1 + GHUtility.setSpeed(5, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(20).setName("myroad").set(priorityEnc, 1)); + GHUtility.setSpeed(5, true, true, accessEnc, speedEnc, g.edge(2, 3).setDistance(20).setName("myroad").set(priorityEnc, 1)); PointList pointList = new PointList(); pointList.add(43.729627, 7.41749); - GHUtility.setSpeed(5, true, true, foot.getAccessEnc(), foot.getAverageSpeedEnc(), g.edge(2, 4).setDistance(20).setName("myroad").set(priorityEnc, 1).setWayGeometry(pointList)); + GHUtility.setSpeed(5, true, true, accessEnc, speedEnc, g.edge(2, 4).setDistance(20).setName("myroad").set(priorityEnc, 1).setWayGeometry(pointList)); - Weighting weighting = CustomModelParser.createWeighting(foot.getAccessEnc(), foot.getAverageSpeedEnc(), - foot.getPriorityEnc(), foot.getMaxSpeed(), tmpEM, DefaultTurnCostProvider.NO_TURN_COST_PROVIDER, + Weighting weighting = CustomModelParser.createWeighting(accessEnc, speedEnc, + priorityEnc, 15, tmpEM, DefaultTurnCostProvider.NO_TURN_COST_PROVIDER, new CustomModel().setDistanceInfluence(0)); Path p = new Dijkstra(g, weighting, tMode).calcPath(4, 3); assertTrue(p.isFound()); @@ -431,8 +429,9 @@ public void testInstructionIfSlightTurnForCustomProfile() { @Test public void testInstructionWithHighlyCustomProfileWithRoadsBase() { - FlagEncoder roads = FlagEncoders.createRoads(new PMap()); - EncodingManager tmpEM = EncodingManager.create(roads); + BooleanEncodedValue roadsAccessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue roadsSpeedEnc = new DecimalEncodedValueImpl("speed", 7, 2, true); + EncodingManager tmpEM = EncodingManager.start().add(roadsAccessEnc).add(roadsSpeedEnc).build(); EnumEncodedValue rcEV = tmpEM.getEnumEncodedValue(RoadClass.KEY, RoadClass.class); BaseGraph g = new BaseGraph.Builder(tmpEM).create(); // real world example: https://graphhopper.com/maps/?point=55.691214%2C12.57065&point=55.689957%2C12.570387 @@ -451,8 +450,6 @@ public void testInstructionWithHighlyCustomProfileWithRoadsBase() { na.setNode(4, 55.690849, 12.571004); na.setNode(5, 55.690864, 12.570886); - BooleanEncodedValue roadsAccessEnc = roads.getAccessEnc(); - DecimalEncodedValue roadsSpeedEnc = roads.getAverageSpeedEnc(); GHUtility.setSpeed(50, true, true, roadsAccessEnc, roadsSpeedEnc, g.edge(3, 2).setDistance(10)); GHUtility.setSpeed(40, true, true, roadsAccessEnc, roadsSpeedEnc, g.edge(2, 4).setDistance(10)); GHUtility.setSpeed(40, true, true, roadsAccessEnc, roadsSpeedEnc, g.edge(2, 1).setDistance(10)); @@ -460,8 +457,7 @@ public void testInstructionWithHighlyCustomProfileWithRoadsBase() { CustomModel customModel = new CustomModel(); customModel.addToPriority(Statement.If("road_class == PEDESTRIAN", Statement.Op.MULTIPLY, 0)); - Weighting weighting = CustomModelParser.createWeighting(roadsAccessEnc, roadsSpeedEnc, roads.getPriorityEnc(), - roads.getMaxSpeed(), tmpEM, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); + Weighting weighting = CustomModelParser.createWeighting(roadsAccessEnc, roadsSpeedEnc, null, 255, tmpEM, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); Path p = new Dijkstra(g, weighting, tMode).calcPath(3, 4); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, tmpEM, usTR); List tmpList = getTurnDescriptions(wayList); diff --git a/core/src/test/java/com/graphhopper/util/PathSimplificationTest.java b/core/src/test/java/com/graphhopper/util/PathSimplificationTest.java index cff4cb26800..bcd0d3d1807 100644 --- a/core/src/test/java/com/graphhopper/util/PathSimplificationTest.java +++ b/core/src/test/java/com/graphhopper/util/PathSimplificationTest.java @@ -23,9 +23,9 @@ import com.graphhopper.routing.Path; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.storage.BaseGraph; @@ -52,8 +52,9 @@ public class PathSimplificationTest { @Test public void testScenario() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - EncodingManager carManager = EncodingManager.create(carEncoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager carManager = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph g = new BaseGraph.Builder(carManager).create(); // 0-1-2 // | | | @@ -74,8 +75,6 @@ public void testScenario() { na.setNode(7, 1.0, 1.1); na.setNode(8, 1.0, 1.2); - BooleanEncodedValue accessEnc = carEncoder.getAccessEnc(); - DecimalEncodedValue speedEnc = carEncoder.getAverageSpeedEnc(); GHUtility.setSpeed(9, true, true, accessEnc, speedEnc, g.edge(0, 1).setDistance(10000)).setName("0-1"); GHUtility.setSpeed(9, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(11000)).setName("1-2"); From 81b758a86111971de2b1cdb036eb3c5e8503e373 Mon Sep 17 00:00:00 2001 From: easbar Date: Sat, 25 Jun 2022 09:21:27 +0200 Subject: [PATCH 09/28] 90 --- .../routing/util/VehicleEncodedValues.java | 2 +- .../PrepareRoutingSubnetworksTest.java | 164 +++++++++--------- .../routing/util/MotorcycleTagParserTest.java | 28 ++- .../routing/util/WheelchairTagParserTest.java | 35 ++-- 4 files changed, 117 insertions(+), 112 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValues.java b/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValues.java index b9f90151209..a8e264289c9 100644 --- a/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValues.java +++ b/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValues.java @@ -64,7 +64,7 @@ public static VehicleEncodedValues hike(PMap properties) { public static VehicleEncodedValues wheelchair(PMap properties) { if (properties.has("speed_two_directions")) - throw new IllegalArgumentException("bike2 always uses two directions"); + throw new IllegalArgumentException("wheelchair always uses two directions"); return foot(new PMap(properties) .putObject("name", properties.getString("name", "wheelchair")) .putObject("speed_two_directions", true) diff --git a/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java b/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java index 046db6abe66..bc52df7955a 100644 --- a/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java +++ b/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java @@ -18,21 +18,15 @@ package com.graphhopper.routing.subnetwork; import com.carrotsearch.hppc.IntArrayList; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.Subnetwork; -import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.AllEdgesIterator; -import com.graphhopper.routing.util.DefaultFlagEncoderFactory; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.weighting.DefaultTurnCostProvider; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.TurnCostProvider; import com.graphhopper.storage.BaseGraph; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.GHUtility; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.Test; import java.util.Arrays; @@ -48,8 +42,8 @@ */ public class PrepareRoutingSubnetworksTest { - private static BaseGraph createSubnetworkTestStorage(EncodingManager encodingManager) { - BaseGraph g = new BaseGraph.Builder(encodingManager).create(); + private static BaseGraph createSubnetworkTestStorage(EncodingManager encodingManager, BooleanEncodedValue accessEnc1, DecimalEncodedValue speedEnc1, BooleanEncodedValue accessEnc2, DecimalEncodedValue speedEnc2) { + BaseGraph g = new BaseGraph.Builder(encodingManager).withTurnCosts(true).create(); // 5 - 6 // | / // 4 @@ -74,113 +68,124 @@ private static BaseGraph createSubnetworkTestStorage(EncodingManager encodingMan // edge 3-4 gets no speed/access by default if (iter.getEdge() == 0) continue; - for (FlagEncoder encoder : encodingManager.fetchEdgeEncoders()) { - iter.set(encoder.getAverageSpeedEnc(), 10); - iter.set(encoder.getAccessEnc(), true, true); - } + iter.set(accessEnc1, true, true); + iter.set(speedEnc1, 10); + if (accessEnc2 != null) + iter.set(accessEnc2, true, true); + if (speedEnc2 != null) + iter.set(speedEnc2, 10); } return g; } @Test public void testPrepareSubnetworks_oneVehicle() { - EncodingManager em = createEncodingManager("car"); - FlagEncoder encoder = em.getEncoder("car"); - BaseGraph g = createSubnetworkTestStorage(em); - PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(createJob(em, encoder, NO_TURN_COST_PROVIDER))); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + BooleanEncodedValue subnetworkEnc = Subnetwork.create("car"); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).add(subnetworkEnc).build(); + BaseGraph g = createSubnetworkTestStorage(em, accessEnc, speedEnc, null, null); + PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(createJob(subnetworkEnc, accessEnc, speedEnc, NO_TURN_COST_PROVIDER))); // this will make the upper small network a subnetwork instance.setMinNetworkSize(4); assertEquals(3, instance.doWork()); - assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, encoder)); + assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, subnetworkEnc)); // this time we lower the threshold and the upper network won't be set to be a subnetwork - g = createSubnetworkTestStorage(em); - instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(createJob(em, encoder, NO_TURN_COST_PROVIDER))); + g = createSubnetworkTestStorage(em, accessEnc, speedEnc, null, null); + instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(createJob(subnetworkEnc, accessEnc, speedEnc, NO_TURN_COST_PROVIDER))); instance.setMinNetworkSize(3); assertEquals(0, instance.doWork()); - assertEquals(IntArrayList.from(), getSubnetworkEdges(g, encoder)); + assertEquals(IntArrayList.from(), getSubnetworkEdges(g, subnetworkEnc)); } @Test public void testPrepareSubnetworks_twoVehicles() { - EncodingManager em = createEncodingManager("car,bike"); - FlagEncoder carEncoder = em.getEncoder("car"); - FlagEncoder bikeEncoder = em.getEncoder("bike"); - BaseGraph g = createSubnetworkTestStorage(em); + BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + BooleanEncodedValue carSubnetworkEnc = Subnetwork.create("car"); + BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); + DecimalEncodedValue bikeSpeedEnc = new DecimalEncodedValueImpl("bike_speed", 4, 2, false); + BooleanEncodedValue bikeSubnetworkEnc = Subnetwork.create("bike"); + EncodingManager em = EncodingManager.start() + .add(carAccessEnc).add(carSpeedEnc).add(carSubnetworkEnc) + .add(bikeAccessEnc).add(bikeSpeedEnc).add(bikeSubnetworkEnc) + .build(); + BaseGraph g = createSubnetworkTestStorage(em, carAccessEnc, carSpeedEnc, bikeAccessEnc, bikeSpeedEnc); // first we only block the middle edge for cars. this way a subnetwork should be created but only for car EdgeIteratorState edge = GHUtility.getEdge(g, 3, 4); - GHUtility.setSpeed(10, false, false, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc(), edge); - GHUtility.setSpeed(5, true, true, bikeEncoder.getAccessEnc(), bikeEncoder.getAverageSpeedEnc(), edge); + GHUtility.setSpeed(10, false, false, carAccessEnc, carSpeedEnc, edge); + GHUtility.setSpeed(5, true, true, bikeAccessEnc, bikeSpeedEnc, edge); List prepareJobs = Arrays.asList( - createJob(em, carEncoder, NO_TURN_COST_PROVIDER), - createJob(em, bikeEncoder, NO_TURN_COST_PROVIDER) + createJob(carSubnetworkEnc, carAccessEnc, carSpeedEnc, NO_TURN_COST_PROVIDER), + createJob(bikeSubnetworkEnc, bikeAccessEnc, bikeSpeedEnc, NO_TURN_COST_PROVIDER) ); PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, prepareJobs); instance.setMinNetworkSize(5); assertEquals(3, instance.doWork()); - assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, carEncoder)); - assertEquals(IntArrayList.from(), getSubnetworkEdges(g, bikeEncoder)); + assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, carSubnetworkEnc)); + assertEquals(IntArrayList.from(), getSubnetworkEdges(g, bikeSubnetworkEnc)); // now we block the edge for both vehicles -> there should be a subnetwork for both vehicles - g = createSubnetworkTestStorage(em); + g = createSubnetworkTestStorage(em, carAccessEnc, carSpeedEnc, bikeAccessEnc, bikeSpeedEnc); edge = GHUtility.getEdge(g, 3, 4); - GHUtility.setSpeed(10, false, false, carEncoder.getAccessEnc(), carEncoder.getAverageSpeedEnc(), edge); - GHUtility.setSpeed(5, false, false, bikeEncoder.getAccessEnc(), bikeEncoder.getAverageSpeedEnc(), edge); + GHUtility.setSpeed(10, false, false, carAccessEnc, carSpeedEnc, edge); + GHUtility.setSpeed(5, false, false, bikeAccessEnc, bikeSpeedEnc, edge); instance = new PrepareRoutingSubnetworks(g, prepareJobs); instance.setMinNetworkSize(5); assertEquals(6, instance.doWork()); - assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, carEncoder)); - assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, bikeEncoder)); + assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, carSubnetworkEnc)); + assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, bikeSubnetworkEnc)); } @Test public void testPrepareSubnetwork_withTurnCosts() { - EncodingManager em = createEncodingManager("car|turn_costs=true"); - FlagEncoder encoder = em.fetchEdgeEncoders().iterator().next(); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + DecimalEncodedValue turnCostEnc = TurnCost.create("car", 1); + BooleanEncodedValue subnetworkEnc = Subnetwork.create("car"); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).add(subnetworkEnc).addTurnCostEncodedValue(turnCostEnc).build(); // since the middle edge is blocked the upper component is a subnetwork (regardless of turn costs) - BaseGraph g = createSubnetworkTestStorage(em); + BaseGraph g = createSubnetworkTestStorage(em, accessEnc, speedEnc, null, null); PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, Collections.singletonList( - createJob(em, encoder, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), g.getTurnCostStorage(), 0)))); + createJob(subnetworkEnc, accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, g.getTurnCostStorage(), 0)))); instance.setMinNetworkSize(4); assertEquals(3, instance.doWork()); - assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, encoder)); + assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, subnetworkEnc)); // if we open the edge it won't be a subnetwork anymore - g = createSubnetworkTestStorage(em); + g = createSubnetworkTestStorage(em, accessEnc, speedEnc, null, null); EdgeIteratorState edge = GHUtility.getEdge(g, 3, 4); - GHUtility.setSpeed(10, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), edge); + GHUtility.setSpeed(10, true, true, accessEnc, speedEnc, edge); instance = new PrepareRoutingSubnetworks(g, Collections.singletonList( - createJob(em, encoder, new DefaultTurnCostProvider(encoder.getTurnCostEnc(), g.getTurnCostStorage(), 0)))); + createJob(subnetworkEnc, accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, g.getTurnCostStorage(), 0)))); instance.setMinNetworkSize(4); assertEquals(0, instance.doWork()); - assertEquals(IntArrayList.from(), getSubnetworkEdges(g, encoder)); + assertEquals(IntArrayList.from(), getSubnetworkEdges(g, subnetworkEnc)); // ... and now for something interesting: if we open the edge *and* apply turn restrictions it will be a // subnetwork again - g = createSubnetworkTestStorage(em); + g = createSubnetworkTestStorage(em, accessEnc, speedEnc, null, null); edge = GHUtility.getEdge(g, 3, 4); - GHUtility.setSpeed(10, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), edge); - DecimalEncodedValue turnCostEnc = em.getDecimalEncodedValue(TurnCost.key(encoder.toString())); + GHUtility.setSpeed(10, true, true, accessEnc, speedEnc, edge); g.getTurnCostStorage().set(turnCostEnc, 0, 4, 7, 1); g.getTurnCostStorage().set(turnCostEnc, 0, 4, 9, 1); instance = new PrepareRoutingSubnetworks(g, Collections.singletonList( - createJob(em, encoder, new DefaultTurnCostProvider(turnCostEnc, g.getTurnCostStorage(), 0)))); + createJob(subnetworkEnc, accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, g.getTurnCostStorage(), 0)))); instance.setMinNetworkSize(4); assertEquals(3, instance.doWork()); - assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, encoder)); + assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, subnetworkEnc)); } - private BaseGraph createSubnetworkTestStorageWithOneWays(EncodingManager em, FlagEncoder encoder) { + private BaseGraph createSubnetworkTestStorageWithOneWays(EncodingManager em, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { if (em.fetchEdgeEncoders().size() > 1) fail("Warning: This method only sets access/speed for a single encoder, but the given encoding manager has multiple encoders"); BaseGraph g = new BaseGraph.Builder(em).create(); // 0 - 1 - 2 - 3 - 4 <- 5 - 6 - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 1).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(2, 3).setDistance(1)); @@ -197,12 +202,14 @@ private BaseGraph createSubnetworkTestStorageWithOneWays(EncodingManager em, Fla @Test public void testPrepareSubnetworks_withOneWays() { - EncodingManager em = createEncodingManager("car"); - FlagEncoder encoder = em.fetchEdgeEncoders().iterator().next(); - BaseGraph g = createSubnetworkTestStorageWithOneWays(em, encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + BooleanEncodedValue subnetworkEnc = Subnetwork.create("car"); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).add(subnetworkEnc).build(); + BaseGraph g = createSubnetworkTestStorageWithOneWays(em, accessEnc, speedEnc); assertEquals(11, g.getNodes()); - PrepareRoutingSubnetworks.PrepareJob job = createJob(em, encoder, NO_TURN_COST_PROVIDER); + PrepareRoutingSubnetworks.PrepareJob job = createJob(subnetworkEnc, accessEnc, speedEnc, NO_TURN_COST_PROVIDER); PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(job)). setMinNetworkSize(2); int subnetworkEdges = instance.doWork(); @@ -211,9 +218,9 @@ public void testPrepareSubnetworks_withOneWays() { // note that the subnetworkEV per profile is one bit per *edge*. Before we used the encoder$access with 2 bits // and got more fine grained response here (8 removed *edgeKeys*) assertEquals(3, subnetworkEdges); - assertEquals(IntArrayList.from(4, 5, 6), getSubnetworkEdges(g, encoder)); + assertEquals(IntArrayList.from(4, 5, 6), getSubnetworkEdges(g, subnetworkEnc)); - g = createSubnetworkTestStorageWithOneWays(em, encoder); + g = createSubnetworkTestStorageWithOneWays(em, accessEnc, speedEnc); assertEquals(11, g.getNodes()); instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(job)). @@ -222,58 +229,43 @@ public void testPrepareSubnetworks_withOneWays() { // due to the larger min network size this time also the (8,9,10) component is a subnetwork assertEquals(5, subnetworkEdges); - assertEquals(IntArrayList.from(4, 5, 6, 7, 8), getSubnetworkEdges(g, encoder)); + assertEquals(IntArrayList.from(4, 5, 6, 7, 8), getSubnetworkEdges(g, subnetworkEnc)); } // Previous two-pass implementation failed on 1 -> 2 -> 0 @Test public void testNodeOrderingRegression() { // 1 -> 2 -> 0 - 3 - 4 - 5 - EncodingManager em = createEncodingManager("car"); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + BooleanEncodedValue subnetworkEnc = Subnetwork.create("car"); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).add(subnetworkEnc).build(); BaseGraph g = new BaseGraph.Builder(em).create(); - FlagEncoder encoder = em.fetchEdgeEncoders().iterator().next(); - BooleanEncodedValue accessEnc = encoder.getAccessEnc(); - DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc(); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(1, 2).setDistance(1)); GHUtility.setSpeed(60, true, false, accessEnc, speedEnc, g.edge(2, 0).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 3).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(3, 4).setDistance(1)); GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(4, 5).setDistance(1)); - PrepareRoutingSubnetworks.PrepareJob job = createJob(em, encoder, NO_TURN_COST_PROVIDER); + PrepareRoutingSubnetworks.PrepareJob job = createJob(subnetworkEnc, accessEnc, speedEnc, NO_TURN_COST_PROVIDER); PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(job)). setMinNetworkSize(2); int subnetworkEdges = instance.doWork(); assertEquals(2, subnetworkEdges); - assertEquals(IntArrayList.from(0, 1), getSubnetworkEdges(g, encoder)); + assertEquals(IntArrayList.from(0, 1), getSubnetworkEdges(g, subnetworkEnc)); } - private static IntArrayList getSubnetworkEdges(BaseGraph graph, FlagEncoder encoder) { - BooleanEncodedValue subnetworkEnc = encoder.getBooleanEncodedValue(Subnetwork.key(encoder.toString())); + private static IntArrayList getSubnetworkEdges(BaseGraph graph, BooleanEncodedValue subnetworkEnc) { IntArrayList result = new IntArrayList(); AllEdgesIterator iter = graph.getAllEdges(); - while (iter.next()) { - if (iter.get(subnetworkEnc)) { + while (iter.next()) + if (iter.get(subnetworkEnc)) result.add(iter.getEdge()); - } - } return result; } - private static EncodingManager createEncodingManager(String flagEncodersStr) { - EncodingManager.Builder builder = new EncodingManager.Builder(); - for (String encoderStr : flagEncodersStr.split(",")) { - encoderStr = encoderStr.trim(); - FlagEncoder encoder = new DefaultFlagEncoderFactory().createFlagEncoder(encoderStr.split("\\|")[0], new PMap(encoderStr)); - builder.add(encoder); - builder.add(Subnetwork.create(encoder.toString())); - } - return builder.build(); - } - - private static PrepareRoutingSubnetworks.PrepareJob createJob(EncodingManager em, FlagEncoder encoder, TurnCostProvider turnCostProvider) { - return new PrepareRoutingSubnetworks.PrepareJob(em.getBooleanEncodedValue(Subnetwork.key(encoder.toString())), - new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), turnCostProvider)); + private static PrepareRoutingSubnetworks.PrepareJob createJob(BooleanEncodedValue subnetworkEnc, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, TurnCostProvider turnCostProvider) { + return new PrepareRoutingSubnetworks.PrepareJob(subnetworkEnc, new FastestWeighting(accessEnc, speedEnc, turnCostProvider)); } } diff --git a/core/src/test/java/com/graphhopper/routing/util/MotorcycleTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/MotorcycleTagParserTest.java index 4a2e6303a18..ac18ec0ad83 100644 --- a/core/src/test/java/com/graphhopper/routing/util/MotorcycleTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/MotorcycleTagParserTest.java @@ -20,6 +20,9 @@ import com.graphhopper.reader.ReaderWay; import com.graphhopper.reader.osm.conditional.DateRangeParser; import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.Graph; import com.graphhopper.storage.IntsRef; @@ -37,10 +40,17 @@ * @author Peter Karich */ public class MotorcycleTagParserTest { - private final EncodingManager em = EncodingManager.create("motorcycle,foot"); - private final FlagEncoder encoder = em.getEncoder("motorcycle"); + private final BooleanEncodedValue motorcycleAccessEnc = new SimpleBooleanEncodedValue("motorcycle_access", true); + private final DecimalEncodedValue motorcycleSpeedEnc = new DecimalEncodedValueImpl("motorcycle_average_speed", 5, 5, true); + private final DecimalEncodedValue motorcyclePriorityEnc = new DecimalEncodedValueImpl("motorcycle_priority", 4, PriorityCode.getFactor(1), false); + private final DecimalEncodedValue motorcycleCurvatureEnc = new DecimalEncodedValueImpl("motorcycle_curvature", 4, 0.1, false); + private final BooleanEncodedValue footAccessEnc = new SimpleBooleanEncodedValue("foot_access", true); + private final DecimalEncodedValue footSpeedEnc = new DecimalEncodedValueImpl("foot_speed", 4, 1, false); + private final EncodingManager em = EncodingManager.start() + .add(motorcycleAccessEnc).add(motorcycleSpeedEnc).add(motorcyclePriorityEnc).add(motorcyclePriorityEnc) + .add(footAccessEnc).add(footSpeedEnc) + .build(); private final MotorcycleTagParser parser; - private final BooleanEncodedValue accessEnc = encoder.getAccessEnc(); public MotorcycleTagParserTest() { parser = new MotorcycleTagParser(em, new PMap()); @@ -57,7 +67,7 @@ private Graph initExampleGraph() { setWayGeometry(Helper.createPointList3D(51.1, 12.0011, 49, 51.1, 12.0015, 55)); edge.setDistance(100); - edge.set(accessEnc, true, true).set(encoder.getAverageSpeedEnc(), 10.0, 15.0); + edge.set(motorcycleAccessEnc, true, true).set(motorcycleSpeedEnc, 10.0, 15.0); return gs; } @@ -150,8 +160,8 @@ public void testHandleWayTags() { @Test public void testSetSpeed0_issue367() { IntsRef edgeFlags = em.createEdgeFlags(); - accessEnc.setBool(false, edgeFlags, true); - accessEnc.setBool(true, edgeFlags, true); + motorcycleAccessEnc.setBool(false, edgeFlags, true); + motorcycleAccessEnc.setBool(true, edgeFlags, true); parser.getAverageSpeedEnc().setDecimal(false, edgeFlags, 10); parser.getAverageSpeedEnc().setDecimal(true, edgeFlags, 10); @@ -161,8 +171,8 @@ public void testSetSpeed0_issue367() { parser.setSpeed(false, edgeFlags, 0); assertEquals(0, parser.avgSpeedEnc.getDecimal(false, edgeFlags), .1); assertEquals(10, parser.avgSpeedEnc.getDecimal(true, edgeFlags), .1); - assertFalse(accessEnc.getBool(false, edgeFlags)); - assertTrue(accessEnc.getBool(true, edgeFlags)); + assertFalse(motorcycleAccessEnc.getBool(false, edgeFlags)); + assertTrue(motorcycleAccessEnc.getBool(true, edgeFlags)); } @Test @@ -191,6 +201,6 @@ private double getBendiness(EdgeIteratorState edge, double beelineDistance) { IntsRef flags = parser.handleWayTags(em.createEdgeFlags(), way); edge.setFlags(flags); parser.applyWayTags(way, edge); - return edge.get(encoder.getCurvatureEnc()); + return edge.get(motorcycleCurvatureEnc); } } diff --git a/core/src/test/java/com/graphhopper/routing/util/WheelchairTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/WheelchairTagParserTest.java index 7739ea9f407..84fe7f94f22 100644 --- a/core/src/test/java/com/graphhopper/routing/util/WheelchairTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/WheelchairTagParserTest.java @@ -20,8 +20,7 @@ import com.graphhopper.reader.ReaderNode; import com.graphhopper.reader.ReaderWay; import com.graphhopper.reader.osm.conditional.DateRangeParser; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.*; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.IntsRef; import com.graphhopper.storage.NodeAccess; @@ -37,18 +36,26 @@ * @author don-philipe */ public class WheelchairTagParserTest { - private final EncodingManager encodingManager = EncodingManager.create("car,wheelchair"); - private final WheelchairTagParser wheelchairParser; - private final DecimalEncodedValue wheelchairAvSpeedEnc; private final BooleanEncodedValue wheelchairAccessEnc; - private final DecimalEncodedValue carAvSpeedEnc = encodingManager.getEncoder("car").getAverageSpeedEnc(); - private final BooleanEncodedValue carAccessEnc = encodingManager.getEncoder("car").getAccessEnc(); + private final DecimalEncodedValue wheelchairAvSpeedEnc; + private final DecimalEncodedValue wheelchairPriorityEnc; + private final BooleanEncodedValue carAccessEnc; + private final DecimalEncodedValue carAvSpeedEnc; + private final EncodingManager encodingManager; + private final WheelchairTagParser wheelchairParser; public WheelchairTagParserTest() { + wheelchairAccessEnc = new SimpleBooleanEncodedValue("wheelchair_access", true); + wheelchairAvSpeedEnc = new DecimalEncodedValueImpl("wheelchair_average_speed", 4, 1, true); + wheelchairPriorityEnc = new DecimalEncodedValueImpl("wheelchair_priority", 4, PriorityCode.getFactor(1), false); + carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + carAvSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + encodingManager = EncodingManager.start() + .add(wheelchairAccessEnc).add(wheelchairAvSpeedEnc).add(wheelchairPriorityEnc).add(new EnumEncodedValue<>(FootNetwork.KEY, RouteNetwork.class)) + .add(carAccessEnc).add(carAvSpeedEnc) + .build(); wheelchairParser = new WheelchairTagParser(encodingManager, new PMap()); wheelchairParser.init(new DateRangeParser()); - wheelchairAvSpeedEnc = wheelchairParser.getAverageSpeedEnc(); - wheelchairAccessEnc = wheelchairParser.getAccessEnc(); } @Test @@ -63,7 +70,6 @@ public void testGetSpeed() { @Test public void testCombined() { BaseGraph g = new BaseGraph.Builder(encodingManager).create(); - FlagEncoder carEncoder = encodingManager.getEncoder("car"); EdgeIteratorState edge = g.edge(0, 1); edge.set(wheelchairAvSpeedEnc, 10.0).set(wheelchairAccessEnc, true, true); edge.set(carAvSpeedEnc, 100.0).set(carAccessEnc, true, false); @@ -504,24 +510,21 @@ public void testApplyWayTags() { na.setNode(1, 51.1, 12.0015, 55); EdgeIteratorState edge01 = graph.edge(0, 1).setWayGeometry(Helper.createPointList3D(51.1, 12.0011, 49, 51.1, 12.0015, 55)); edge01.setDistance(100); - FlagEncoder encoder2 = encodingManager.getEncoder("wheelchair"); - GHUtility.setSpeed(5, 5, encoder2.getAccessEnc(), encoder2.getAverageSpeedEnc(), edge01); + GHUtility.setSpeed(5, 5, wheelchairAccessEnc, wheelchairAvSpeedEnc, edge01); // incline of 10% & shorter edge na.setNode(2, 51.2, 12.1010, 50); na.setNode(3, 51.2, 12.1015, 60); EdgeIteratorState edge23 = graph.edge(2, 3).setWayGeometry(Helper.createPointList3D(51.2, 12.1011, 49, 51.2, 12.1015, 55)); edge23.setDistance(30); - FlagEncoder encoder1 = encodingManager.getEncoder("wheelchair"); - GHUtility.setSpeed(5, 5, encoder1.getAccessEnc(), encoder1.getAverageSpeedEnc(), edge23); + GHUtility.setSpeed(5, 5, wheelchairAccessEnc, wheelchairAvSpeedEnc, edge23); // incline of 10% & longer edge na.setNode(4, 51.2, 12.101, 50); na.setNode(5, 51.2, 12.102, 60); EdgeIteratorState edge45 = graph.edge(2, 3).setWayGeometry(Helper.createPointList3D(51.2, 12.1011, 49, 51.2, 12.1015, 55)); edge45.setDistance(100); - FlagEncoder encoder = encodingManager.getEncoder("wheelchair"); - GHUtility.setSpeed(5, 5, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), edge45); + GHUtility.setSpeed(5, 5, wheelchairAccessEnc, wheelchairAvSpeedEnc, edge45); wheelchairParser.applyWayTags(new ReaderWay(1), edge01); From fa739ae3a3759cc4169ac9f50f13e22ee2b219bf Mon Sep 17 00:00:00 2001 From: easbar Date: Sat, 25 Jun 2022 11:02:14 +0200 Subject: [PATCH 10/28] 68 --- .../PrepareRoutingSubnetworksTest.java | 3 - .../routing/util/Car4WDTagParserTest.java | 4 +- .../routing/util/CarTagParserTest.java | 66 +++++++------- .../routing/util/EncodingManagerTest.java | 20 ++--- .../routing/util/FootTagParserTest.java | 11 +-- .../routing/util/RacingBikeTagParserTest.java | 39 ++++---- .../routing/util/TagParsingTest.java | 90 ++++++++++++------- .../storage/AbstractGraphStorageTester.java | 84 +++++++++-------- .../graphhopper/storage/BaseGraphTest.java | 10 +-- .../storage/BaseGraphWithTurnCostsTest.java | 21 +++-- 10 files changed, 197 insertions(+), 151 deletions(-) diff --git a/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java b/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java index bc52df7955a..538901fd031 100644 --- a/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java +++ b/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java @@ -35,7 +35,6 @@ import static com.graphhopper.routing.weighting.TurnCostProvider.NO_TURN_COST_PROVIDER; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; /** * @author Peter Karich @@ -182,8 +181,6 @@ public void testPrepareSubnetwork_withTurnCosts() { private BaseGraph createSubnetworkTestStorageWithOneWays(EncodingManager em, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) { - if (em.fetchEdgeEncoders().size() > 1) - fail("Warning: This method only sets access/speed for a single encoder, but the given encoding manager has multiple encoders"); BaseGraph g = new BaseGraph.Builder(em).create(); // 0 - 1 - 2 - 3 - 4 <- 5 - 6 GHUtility.setSpeed(60, true, true, accessEnc, speedEnc, g.edge(0, 1).setDistance(1)); diff --git a/core/src/test/java/com/graphhopper/routing/util/Car4WDTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/Car4WDTagParserTest.java index 7a3a3c12e1d..90473eee929 100644 --- a/core/src/test/java/com/graphhopper/routing/util/Car4WDTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/Car4WDTagParserTest.java @@ -36,8 +36,8 @@ public class Car4WDTagParserTest extends CarTagParserTest { @Override - FlagEncoder createEncoder(PMap properties) { - return FlagEncoders.createCar4wd(properties); + protected String getCarName() { + return "car4wd"; } @Override diff --git a/core/src/test/java/com/graphhopper/routing/util/CarTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/CarTagParserTest.java index 33db42dcd6b..20689d698a1 100644 --- a/core/src/test/java/com/graphhopper/routing/util/CarTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/CarTagParserTest.java @@ -20,10 +20,7 @@ import com.graphhopper.reader.ReaderNode; import com.graphhopper.reader.ReaderWay; import com.graphhopper.reader.osm.conditional.DateRangeParser; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.EncodedValueLookup; -import com.graphhopper.routing.ev.Roundabout; +import com.graphhopper.routing.ev.*; import com.graphhopper.storage.IntsRef; import com.graphhopper.util.Helper; import com.graphhopper.util.PMap; @@ -38,20 +35,27 @@ * @author Peter Karich */ public class CarTagParserTest { - final FlagEncoder encoder = createEncoder(new PMap("turn_costs=true|speed_two_directions=true")); - private final EncodingManager em = new EncodingManager.Builder() - .add(encoder) - .add(FlagEncoders.createBike()) - .add(FlagEncoders.createFoot()) - .build(); + private final EncodingManager em = createEncodingManager(getCarName()); final CarTagParser parser = createParser(em, new PMap("block_fords=true")); - private final BooleanEncodedValue roundaboutEnc = em.getBooleanEncodedValue(Roundabout.KEY); - private final DecimalEncodedValue avSpeedEnc = parser.getAverageSpeedEnc(); private final BooleanEncodedValue accessEnc = parser.getAccessEnc(); + private final DecimalEncodedValue avSpeedEnc = parser.getAverageSpeedEnc(); + + protected String getCarName() { + return "car"; + } - FlagEncoder createEncoder(PMap properties) { - return FlagEncoders.createCar(properties); + private EncodingManager createEncodingManager(String carName) { + return new EncodingManager.Builder() + .add(new SimpleBooleanEncodedValue(carName + "_access", true)) + .add(new DecimalEncodedValueImpl(carName + "_average_speed", 5, 5, true)) + .addTurnCostEncodedValue(TurnCost.create(carName, 1)) + .add(new SimpleBooleanEncodedValue("bike_access", true)) + .add(new DecimalEncodedValueImpl("bike_average_speed", 4, 2, false)) + .add(new DecimalEncodedValueImpl("bike_priority", 4, PriorityCode.getFactor(1), false)) + .add(new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class)) + .add(new EnumEncodedValue<>(Smoothness.KEY, Smoothness.class)) + .build(); } CarTagParser createParser(EncodedValueLookup lookup, PMap properties) { @@ -536,10 +540,13 @@ public void testChainBarrier() { @Test public void testMaxValue() { - FlagEncoder encoder = createEncoder(new PMap("speed_bits=10|speed_factor=0.5")); - EncodingManager em = EncodingManager.create(encoder); + DecimalEncodedValueImpl smallFactorSpeedEnc = new DecimalEncodedValueImpl(getCarName() + "_average_speed", 10, 0.5, false); + EncodingManager em = new EncodingManager.Builder() + .add(new SimpleBooleanEncodedValue(getCarName() + "_access", true)) + .add(smallFactorSpeedEnc) + .addTurnCostEncodedValue(TurnCost.create(getCarName(), 1)) + .build(); CarTagParser parser = createParser(em, new PMap()); - DecimalEncodedValue avSpeedEnc = em.getDecimalEncodedValue(EncodingManager.getKey(encoder, "average_speed")); ReaderWay way = new ReaderWay(1); way.setTag("highway", "motorway_link"); way.setTag("maxspeed", "60 mph"); @@ -547,22 +554,15 @@ public void testMaxValue() { // double speed = AbstractFlagEncoder.parseSpeed("60 mph"); // => 96.56 * 0.9 => 86.9 - assertEquals(86.9, avSpeedEnc.getDecimal(false, edgeFlags), 1e-1); - assertEquals(86.9, avSpeedEnc.getDecimal(true, edgeFlags), 1e-1); + assertEquals(86.9, smallFactorSpeedEnc.getDecimal(false, edgeFlags), 1e-1); + assertEquals(86.9, smallFactorSpeedEnc.getDecimal(true, edgeFlags), 1e-1); // test that maxPossibleValue is not exceeded way = new ReaderWay(2); way.setTag("highway", "motorway_link"); way.setTag("maxspeed", "70 mph"); edgeFlags = parser.handleWayTags(em.createEdgeFlags(), way); - assertEquals(101.5, avSpeedEnc.getDecimal(false, edgeFlags), .1); - } - - @Test - public void testRegisterOnlyOnceAllowed() { - FlagEncoder instance = FlagEncoders.createCar(); - EncodingManager.create(instance); - assertThrows(IllegalStateException.class, () -> EncodingManager.create(instance)); + assertEquals(101.5, smallFactorSpeedEnc.getDecimal(false, edgeFlags), .1); } @Test @@ -587,7 +587,7 @@ public void testCombination() { bikeParser.handleWayTags(edgeFlags, way); assertFalse(accessEnc.getBool(true, edgeFlags)); assertFalse(accessEnc.getBool(false, edgeFlags)); - BooleanEncodedValue bikeAccessEnc = em.getEncoder("bike").getAccessEnc(); + BooleanEncodedValue bikeAccessEnc = bikeParser.getAccessEnc(); assertTrue(bikeAccessEnc.getBool(true, edgeFlags)); assertTrue(bikeAccessEnc.getBool(false, edgeFlags)); } @@ -612,10 +612,14 @@ public void testIssue_1256() { assertEquals(5, parser.getAverageSpeedEnc().getDecimal(false, edgeFlags), .1); // for a smaller speed factor the minimum speed is also smaller - FlagEncoder lowFactorCar = createEncoder(new PMap("speed_bits=10|speed_factor=1")); - EncodingManager lowFactorEm = EncodingManager.create(lowFactorCar); + DecimalEncodedValueImpl lowFactorSpeedEnc = new DecimalEncodedValueImpl(getCarName() + "_average_speed", 10, 1, false); + EncodingManager lowFactorEm = new EncodingManager.Builder() + .add(new SimpleBooleanEncodedValue(getCarName() + "_access", true)) + .add(lowFactorSpeedEnc) + .addTurnCostEncodedValue(TurnCost.create(getCarName(), 1)) + .build(); edgeFlags = lowFactorEm.createEdgeFlags(); createParser(lowFactorEm, new PMap()).handleWayTags(edgeFlags, way); - assertEquals(1, lowFactorCar.getAverageSpeedEnc().getDecimal(false, edgeFlags), .1); + assertEquals(1, lowFactorSpeedEnc.getDecimal(false, edgeFlags), .1); } } diff --git a/core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java b/core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java index e3c55adb50f..541a1b4ddfe 100644 --- a/core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java @@ -17,6 +17,7 @@ */ package com.graphhopper.routing.util; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; import com.graphhopper.util.PMap; import org.junit.jupiter.api.Test; @@ -41,17 +42,6 @@ public void testEncoderAcceptNoException() { assertFalse(manager.hasEncoder("foot")); } - @Test - public void testWrongEncoders() { - try { - FlagEncoder foot = FlagEncoders.createFoot(); - EncodingManager.create(foot, foot); - fail("There should have been an exception"); - } catch (Exception ex) { - assertEquals("FlagEncoder already exists: foot", ex.getMessage()); - } - } - @Test public void testSupportFords() { String flagEncoderStrings = "car,bike,foot"; @@ -89,4 +79,12 @@ public void validEV() { assertFalse(EncodingManager.isValidEncodedValue(str), str); } } + + @Test + public void testRegisterOnlyOnceAllowed() { + DecimalEncodedValueImpl speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + EncodingManager.start().add(speedEnc).build(); + assertThrows(IllegalStateException.class, () -> EncodingManager.start().add(speedEnc).build()); + } + } diff --git a/core/src/test/java/com/graphhopper/routing/util/FootTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/FootTagParserTest.java index 157a8f26615..8421bd6955d 100644 --- a/core/src/test/java/com/graphhopper/routing/util/FootTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/FootTagParserTest.java @@ -462,17 +462,18 @@ public void testBlockByDefault() { @Test public void maxSpeed() { - FlagEncoder encoder = FlagEncoders.createFoot(new PMap().putObject("speed_bits", 4).putObject("speed_factor", 2)); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("foot_speed", 4, 2, true); // The foot max speed is supposed to be 15km/h, but for speed_bits=4,speed_factor=2 as we use here 15 cannot // be stored. In fact, when we set the speed of an edge to 15 and call the getter afterwards we get a value of 16 // because of the internal (scaled) integer representation: - EncodingManager em = EncodingManager.create(encoder); + EncodingManager em = EncodingManager.start().add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).create(); - EdgeIteratorState edge = graph.edge(0, 1).setDistance(100).set(encoder.getAverageSpeedEnc(), 15); - assertEquals(16, edge.get(encoder.getAverageSpeedEnc())); + EdgeIteratorState edge = graph.edge(0, 1).setDistance(100).set(speedEnc, 15); + assertEquals(16, edge.get(speedEnc)); // ... because of this we have to make sure the max speed is set to a value that cannot be exceeded even when // such conversion occurs. in our case it must be 16 not 15! - assertEquals(16, encoder.getMaxSpeed()); + // note that this test made more sense when we used encoders that defined a max speed. + assertEquals(16, speedEnc.getNextStorableValue(15)); } } diff --git a/core/src/test/java/com/graphhopper/routing/util/RacingBikeTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/RacingBikeTagParserTest.java index 851954a62de..347e2d6337c 100644 --- a/core/src/test/java/com/graphhopper/routing/util/RacingBikeTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/RacingBikeTagParserTest.java @@ -220,7 +220,14 @@ public void testHandleWayTagsInfluencedByRelation() { public void testPriority_avoidanceOfHighMaxSpeed() { // here we test the priority that would be calculated if the way was accessible (even when it is not) // therefore we need a modified parser that always yields access=WAY - EncodingManager encodingManager = EncodingManager.create("racingbike"); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("racingbike_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("racingbike_average_speed", 4, 2, false); + DecimalEncodedValue priorityEnc = new DecimalEncodedValueImpl("racingbike_priority", 4, PriorityCode.getValue(1), false); + EncodingManager encodingManager = EncodingManager.start() + .add(accessEnc).add(speedEnc).add(priorityEnc) + .add(new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class)) + .add(new EnumEncodedValue<>(Smoothness.KEY, Smoothness.class)) + .build(); BikeCommonTagParser parser = new RacingBikeTagParser(encodingManager, new PMap("block_fords=true")) { @Override public WayAccess getAccess(ReaderWay way) { @@ -230,56 +237,54 @@ public WayAccess getAccess(ReaderWay way) { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "tertiary"); osmWay.setTag("maxspeed", "50"); - assertPriorityAndSpeed(encodingManager, parser, PREFER.getValue(), 20, osmWay); + assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parser, PREFER.getValue(), 20, osmWay); osmWay.setTag("maxspeed", "60"); - assertPriorityAndSpeed(encodingManager, parser, PREFER.getValue(), 20, osmWay); + assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parser, PREFER.getValue(), 20, osmWay); osmWay.setTag("maxspeed", "80"); - assertPriorityAndSpeed(encodingManager, parser, PREFER.getValue(), 20, osmWay); + assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parser, PREFER.getValue(), 20, osmWay); osmWay.setTag("maxspeed", "90"); - assertPriorityAndSpeed(encodingManager, parser, UNCHANGED.getValue(), 20, osmWay); + assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parser, UNCHANGED.getValue(), 20, osmWay); osmWay.setTag("maxspeed", "120"); - assertPriorityAndSpeed(encodingManager, parser, UNCHANGED.getValue(), 20, osmWay); + assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parser, UNCHANGED.getValue(), 20, osmWay); osmWay.setTag("highway", "motorway"); - assertPriorityAndSpeed(encodingManager, parser, AVOID.getValue(), 18, osmWay); + assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parser, AVOID.getValue(), 18, osmWay); osmWay.setTag("tunnel", "yes"); - assertPriorityAndSpeed(encodingManager, parser, AVOID_MORE.getValue(), 18, osmWay); + assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parser, AVOID_MORE.getValue(), 18, osmWay); osmWay.clearTags(); osmWay.setTag("highway", "motorway"); osmWay.setTag("tunnel", "yes"); osmWay.setTag("maxspeed", "80"); - assertPriorityAndSpeed(encodingManager, parser, AVOID_MORE.getValue(), 18, osmWay); + assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parser, AVOID_MORE.getValue(), 18, osmWay); osmWay.clearTags(); osmWay.setTag("highway", "motorway"); osmWay.setTag("tunnel", "yes"); osmWay.setTag("maxspeed", "120"); - assertPriorityAndSpeed(encodingManager, parser, AVOID_MORE.getValue(), 18, osmWay); + assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parser, AVOID_MORE.getValue(), 18, osmWay); osmWay.clearTags(); osmWay.setTag("highway", "notdefined"); osmWay.setTag("tunnel", "yes"); osmWay.setTag("maxspeed", "120"); - assertPriorityAndSpeed(encodingManager, parser, AVOID_MORE.getValue(), 4, osmWay); + assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parser, AVOID_MORE.getValue(), 4, osmWay); osmWay.clearTags(); osmWay.setTag("highway", "notdefined"); osmWay.setTag("maxspeed", "50"); - assertPriorityAndSpeed(encodingManager, parser, UNCHANGED.getValue(), 4, osmWay); + assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parser, UNCHANGED.getValue(), 4, osmWay); } - private void assertPriorityAndSpeed(EncodingManager encodingManager, VehicleTagParser parser, int expectedPrio, double expectedSpeed, ReaderWay way) { + private void assertPriorityAndSpeed(EncodingManager encodingManager, DecimalEncodedValue priorityEnc, DecimalEncodedValue speedEnc, VehicleTagParser parser, int expectedPrio, double expectedSpeed, ReaderWay way) { IntsRef edgeFlags = parser.handleWayTags(encodingManager.createEdgeFlags(), way); - FlagEncoder encoder = encodingManager.fetchEdgeEncoders().iterator().next(); - DecimalEncodedValue enc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey(encoder.toString(), "priority")); - assertEquals(expectedSpeed, encoder.getAverageSpeedEnc().getDecimal(false, edgeFlags), 0.1); - assertEquals(PriorityCode.getValue(expectedPrio), enc.getDecimal(false, edgeFlags), 0.01); + assertEquals(PriorityCode.getValue(expectedPrio), priorityEnc.getDecimal(false, edgeFlags), 0.01); + assertEquals(expectedSpeed, speedEnc.getDecimal(false, edgeFlags), 0.1); } @Test diff --git a/core/src/test/java/com/graphhopper/routing/util/TagParsingTest.java b/core/src/test/java/com/graphhopper/routing/util/TagParsingTest.java index 244ba6cb9d3..43dfaebfa3f 100644 --- a/core/src/test/java/com/graphhopper/routing/util/TagParsingTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/TagParsingTest.java @@ -44,27 +44,36 @@ public void testCombineRelations() { osmWay.setTag("highway", "track"); ReaderRelation osmRel = new ReaderRelation(1); - FlagEncoder defaultBike = FlagEncoders.createBike(); - FlagEncoder lessRelationCodes = FlagEncoders.createBike(new PMap("name=less_relation_bits")); - - EncodingManager em = EncodingManager.create(defaultBike, lessRelationCodes); - EnumEncodedValue bikeNetworkEnc = em.getEnumEncodedValue(BikeNetwork.KEY, RouteNetwork.class); - BikeTagParser defaultBikeParser = new BikeTagParser(em, new PMap("name=bike")); - defaultBikeParser.init(new DateRangeParser()); - BikeTagParser lessRelationCodesParser = new BikeTagParser(em, new PMap("name=less_relation_bits")) { + BooleanEncodedValue bike1AccessEnc = new SimpleBooleanEncodedValue("bike1_access", true); + DecimalEncodedValue bike1SpeedEnc = new DecimalEncodedValueImpl("bike1_average_speed", 4, 2, false); + DecimalEncodedValue bike1PriorityEnc = new DecimalEncodedValueImpl("bike1_priority", 4, PriorityCode.getFactor(1), false); + BooleanEncodedValue bike2AccessEnc = new SimpleBooleanEncodedValue("bike2_access", true); + DecimalEncodedValue bike2SpeedEnc = new DecimalEncodedValueImpl("bike2_average_speed", 4, 2, false); + DecimalEncodedValue bike2PriorityEnc = new DecimalEncodedValueImpl("bike2_priority", 4, PriorityCode.getFactor(1), false); + EnumEncodedValue bikeNetworkEnc = new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class); + EncodingManager em = EncodingManager.start() + .add(bike1AccessEnc).add(bike1SpeedEnc).add(bike1PriorityEnc) + .add(bike2AccessEnc).add(bike2SpeedEnc).add(bike2PriorityEnc) + .add(bikeNetworkEnc) + .add(new EnumEncodedValue<>(Smoothness.KEY, Smoothness.class)) + .build(); + BikeTagParser bike1Parser = new BikeTagParser(em, new PMap("name=bike1")); + bike1Parser.init(new DateRangeParser()); + BikeTagParser bike2Parser = new BikeTagParser(em, new PMap("name=bike2")) { @Override public IntsRef handleWayTags(IntsRef edgeFlags, ReaderWay way) { + // accept less relations if (bikeRouteEnc.getEnum(false, edgeFlags) != RouteNetwork.MISSING) priorityEnc.setDecimal(false, edgeFlags, PriorityCode.getFactor(2)); return edgeFlags; } }; - lessRelationCodesParser.init(new DateRangeParser()); + bike2Parser.init(new DateRangeParser()); OSMParsers osmParsers = new OSMParsers() .addRelationTagParser(relConfig -> new OSMBikeNetworkTagParser(bikeNetworkEnc, relConfig)) .addWayTagParser(new OSMRoadClassParser(em.getEnumEncodedValue(RoadClass.KEY, RoadClass.class))) - .addWayTagParser(defaultBikeParser) - .addWayTagParser(lessRelationCodesParser); + .addWayTagParser(bike1Parser) + .addWayTagParser(bike2Parser); // relation code is PREFER osmRel.setTag("route", "bicycle"); @@ -74,7 +83,7 @@ public IntsRef handleWayTags(IntsRef edgeFlags, ReaderWay way) { IntsRef edgeFlags = em.createEdgeFlags(); edgeFlags = osmParsers.handleWayTags(edgeFlags, osmWay, relFlags); assertEquals(RouteNetwork.LOCAL, bikeNetworkEnc.getEnum(false, edgeFlags)); - assertTrue(defaultBike.getPriorityEnc().getDecimal(false, edgeFlags) > lessRelationCodes.getPriorityEnc().getDecimal(false, edgeFlags)); + assertTrue(bike1PriorityEnc.getDecimal(false, edgeFlags) > bike2PriorityEnc.getDecimal(false, edgeFlags)); } @Test @@ -85,18 +94,26 @@ public void testMixBikeTypesAndRelationCombination() { ReaderRelation osmRel = new ReaderRelation(1); - FlagEncoder bikeEncoder = FlagEncoders.createBike(); - FlagEncoder mtbEncoder = FlagEncoders.createMountainBike(); - EncodingManager manager = EncodingManager.create(bikeEncoder, mtbEncoder); - - EnumEncodedValue bikeNetworkEnc = manager.getEnumEncodedValue(BikeNetwork.KEY, RouteNetwork.class); - BikeTagParser bikeTagParser = new BikeTagParser(manager, new PMap()); + BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); + DecimalEncodedValue bikeSpeedEnc = new DecimalEncodedValueImpl("bike_average_speed", 4, 2, false); + DecimalEncodedValue bikePriorityEnc = new DecimalEncodedValueImpl("bike_priority", 4, PriorityCode.getFactor(1), false); + BooleanEncodedValue mtbAccessEnc = new SimpleBooleanEncodedValue("mtb_access", true); + DecimalEncodedValue mtbSpeedEnc = new DecimalEncodedValueImpl("mtb_average_speed", 4, 2, false); + DecimalEncodedValue mtbPriorityEnc = new DecimalEncodedValueImpl("mtb_priority", 4, PriorityCode.getFactor(1), false); + EnumEncodedValue bikeNetworkEnc = new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class); + EncodingManager em = EncodingManager.start() + .add(bikeAccessEnc).add(bikeSpeedEnc).add(bikePriorityEnc) + .add(mtbAccessEnc).add(mtbSpeedEnc).add(mtbPriorityEnc) + .add(bikeNetworkEnc) + .add(new EnumEncodedValue<>(Smoothness.KEY, Smoothness.class)) + .build(); + BikeTagParser bikeTagParser = new BikeTagParser(em, new PMap()); bikeTagParser.init(new DateRangeParser()); - MountainBikeTagParser mtbTagParser = new MountainBikeTagParser(manager, new PMap()); + MountainBikeTagParser mtbTagParser = new MountainBikeTagParser(em, new PMap()); mtbTagParser.init(new DateRangeParser()); OSMParsers osmParsers = new OSMParsers() .addRelationTagParser(relConfig -> new OSMBikeNetworkTagParser(bikeNetworkEnc, relConfig)) - .addWayTagParser(new OSMRoadClassParser(manager.getEnumEncodedValue(RoadClass.KEY, RoadClass.class))) + .addWayTagParser(new OSMRoadClassParser(em.getEnumEncodedValue(RoadClass.KEY, RoadClass.class))) .addWayTagParser(bikeTagParser) .addWayTagParser(mtbTagParser); @@ -105,11 +122,11 @@ public void testMixBikeTypesAndRelationCombination() { osmRel.setTag("network", "rcn"); IntsRef relFlags = osmParsers.createRelationFlags(); relFlags = osmParsers.handleRelationTags(osmRel, relFlags); - IntsRef edgeFlags = manager.createEdgeFlags(); + IntsRef edgeFlags = em.createEdgeFlags(); edgeFlags = osmParsers.handleWayTags(edgeFlags, osmWay, relFlags); // bike: uninfluenced speed for grade but via network => NICE // mtb: uninfluenced speed only PREFER - assertTrue(bikeEncoder.getPriorityEnc().getDecimal(false, edgeFlags) > mtbEncoder.getPriorityEnc().getDecimal(false, edgeFlags)); + assertTrue(bikePriorityEnc.getDecimal(false, edgeFlags) > mtbPriorityEnc.getDecimal(false, edgeFlags)); } @Test @@ -145,7 +162,22 @@ public void testCompatibilityBug() { @Test public void testSharedEncodedValues() { - EncodingManager manager = EncodingManager.create("car,foot,bike,motorcycle,mtb"); + BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + BooleanEncodedValue footAccessEnc = new SimpleBooleanEncodedValue("foot_access", true); + BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); + BooleanEncodedValue motorcycleAccessEnc = new SimpleBooleanEncodedValue("motorcycle_access", true); + BooleanEncodedValue mtbAccessEnc = new SimpleBooleanEncodedValue("mtb_access", true); + List accessEncs = Arrays.asList(carAccessEnc, footAccessEnc, bikeAccessEnc, motorcycleAccessEnc, mtbAccessEnc); + EncodingManager manager = EncodingManager.start() + .add(carAccessEnc).add(new DecimalEncodedValueImpl("car_average_speed", 5, 5, false)) + .add(footAccessEnc).add(new DecimalEncodedValueImpl("foot_average_speed", 4, 1, true)).add(new DecimalEncodedValueImpl("foot_priority", 4, PriorityCode.getFactor(1), false)) + .add(bikeAccessEnc).add(new DecimalEncodedValueImpl("bike_average_speed", 4, 2, false)).add(new DecimalEncodedValueImpl("bike_priority", 4, PriorityCode.getFactor(1), false)) + .add(motorcycleAccessEnc).add(new DecimalEncodedValueImpl("motorcycle_average_speed", 5, 5, true)).add(new DecimalEncodedValueImpl("motorcycle_priority", 4, PriorityCode.getFactor(1), false)).add(new DecimalEncodedValueImpl("motorcycle_curvature", 5, 5, true)) + .add(mtbAccessEnc).add(new DecimalEncodedValueImpl("mtb_average_speed", 4, 2, false)).add(new DecimalEncodedValueImpl("mtb_priority", 4, PriorityCode.getFactor(1), false)) + .add(new EnumEncodedValue<>(FootNetwork.KEY, RouteNetwork.class)) + .add(new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class)) + .add(new EnumEncodedValue<>(Smoothness.KEY, Smoothness.class)) + .build(); BooleanEncodedValue roundaboutEnc = manager.getBooleanEncodedValue(Roundabout.KEY); List tagParsers = Arrays.asList( @@ -167,11 +199,9 @@ public void testSharedEncodedValues() { way.setTag("junction", "roundabout"); tagParsers.forEach(p -> p.handleWayTags(edgeFlags, way, relFlags)); - for (FlagEncoder tmp : manager.fetchEdgeEncoders()) { - BooleanEncodedValue accessEnc = tmp.getAccessEnc(); + assertTrue(roundaboutEnc.getBool(false, edgeFlags)); + for (BooleanEncodedValue accessEnc : accessEncs) assertTrue(accessEnc.getBool(false, edgeFlags)); - assertTrue(roundaboutEnc.getBool(false, edgeFlags), tmp.toString()); - } final IntsRef edgeFlags2 = manager.createEdgeFlags(); way.clearTags(); @@ -179,11 +209,9 @@ public void testSharedEncodedValues() { way.setTag("junction", "circular"); tagParsers.forEach(p -> p.handleWayTags(edgeFlags2, way, relFlags)); - for (FlagEncoder tmp : manager.fetchEdgeEncoders()) { - BooleanEncodedValue accessEnc = tmp.getAccessEnc(); + assertTrue(roundaboutEnc.getBool(false, edgeFlags)); + for (BooleanEncodedValue accessEnc : accessEncs) assertTrue(accessEnc.getBool(false, edgeFlags)); - assertTrue(roundaboutEnc.getBool(false, edgeFlags), tmp.toString()); - } } } \ No newline at end of file diff --git a/core/src/test/java/com/graphhopper/storage/AbstractGraphStorageTester.java b/core/src/test/java/com/graphhopper/storage/AbstractGraphStorageTester.java index 83cfd305a9a..96c574f79c2 100644 --- a/core/src/test/java/com/graphhopper/storage/AbstractGraphStorageTester.java +++ b/core/src/test/java/com/graphhopper/storage/AbstractGraphStorageTester.java @@ -19,7 +19,11 @@ import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; +import com.graphhopper.routing.util.AccessFilter; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.util.*; import com.graphhopper.util.shapes.BBox; import org.junit.jupiter.api.AfterEach; @@ -28,7 +32,6 @@ import java.io.File; -import static com.graphhopper.routing.util.EncodingManager.getKey; import static org.junit.jupiter.api.Assertions.*; /** @@ -42,15 +45,22 @@ public abstract class AbstractGraphStorageTester { private final String locationParent = "./target/graphstorage"; protected int defaultSize = 100; protected String defaultGraphLoc = "./target/graphstorage/default"; - protected FlagEncoder carEncoder = createCarFlagEncoder(); - protected EncodingManager encodingManager = new EncodingManager.Builder().add(carEncoder).add(FlagEncoders.createFoot()).build(); - protected BooleanEncodedValue carAccessEnc = carEncoder.getAccessEnc(); - protected DecimalEncodedValue carAvSpeedEnc = carEncoder.getAverageSpeedEnc(); - protected FlagEncoder footEncoder = encodingManager.getEncoder("foot"); - protected BooleanEncodedValue footAccessEnc = footEncoder.getAccessEnc(); + protected BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + protected DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); + protected BooleanEncodedValue footAccessEnc = new SimpleBooleanEncodedValue("foot_access", true); + protected DecimalEncodedValue footSpeedEnc = new DecimalEncodedValueImpl("foot_speed", 4, 1, true); + protected EncodingManager encodingManager = createEncodingManager(); + + protected EncodingManager createEncodingManager() { + return new EncodingManager.Builder() + .add(carAccessEnc).add(carSpeedEnc) + .add(footAccessEnc).add(footSpeedEnc) + .build(); + } + protected BaseGraph graph; - EdgeFilter carOutFilter = AccessFilter.outEdges(carEncoder.getAccessEnc()); - EdgeFilter carInFilter = AccessFilter.inEdges(carEncoder.getAccessEnc()); + EdgeFilter carOutFilter = AccessFilter.outEdges(carAccessEnc); + EdgeFilter carInFilter = AccessFilter.inEdges(carAccessEnc); EdgeExplorer carOutExplorer; EdgeExplorer carInExplorer; EdgeExplorer carAllExplorer; @@ -85,10 +95,6 @@ public static int getIdOf(Graph g, double latitude, double longitude) { throw new IllegalArgumentException("did not find node with location " + (float) latitude + "," + (float) longitude); } - FlagEncoder createCarFlagEncoder() { - return FlagEncoders.createCar(); - } - protected BaseGraph createGHStorage() { BaseGraph g = createGHStorage(defaultGraphLoc, false); carOutExplorer = g.createEdgeExplorer(carOutFilter); @@ -429,19 +435,19 @@ public void testBounds() { public void testFlags() { graph = createGHStorage(); graph.edge(0, 1).set(carAccessEnc, true, true).setDistance(10) - .set(carAvSpeedEnc, 100); + .set(carSpeedEnc, 100); graph.edge(2, 3).set(carAccessEnc, true, false).setDistance(10) - .set(carAvSpeedEnc, 10); + .set(carSpeedEnc, 10); EdgeIterator iter = carAllExplorer.setBaseNode(0); assertTrue(iter.next()); - assertEquals(100, iter.get(carAvSpeedEnc), 1); + assertEquals(100, iter.get(carSpeedEnc), 1); assertTrue(iter.get(carAccessEnc)); assertTrue(iter.getReverse(carAccessEnc)); iter = carAllExplorer.setBaseNode(2); assertTrue(iter.next()); - assertEquals(10, iter.get(carAvSpeedEnc), 1); + assertEquals(10, iter.get(carSpeedEnc), 1); assertTrue(iter.get(carAccessEnc)); assertFalse(iter.getReverse(carAccessEnc)); @@ -610,7 +616,7 @@ public void testFootMix() { EdgeIteratorState edge = graph.edge(0, 3).setDistance(10); edge.set(footAccessEnc, true, true); edge.set(carAccessEnc, true, true); - EdgeExplorer footOutExplorer = graph.createEdgeExplorer(AccessFilter.outEdges(footEncoder.getAccessEnc())); + EdgeExplorer footOutExplorer = graph.createEdgeExplorer(AccessFilter.outEdges(footAccessEnc)); assertEquals(GHUtility.asSet(3, 1), GHUtility.getNeighbors(footOutExplorer.setBaseNode(0))); assertEquals(GHUtility.asSet(3, 2), GHUtility.getNeighbors(carOutExplorer.setBaseNode(0))); } @@ -657,9 +663,15 @@ public void testEdgeKVStorage() { @Test public void test8AndMoreBytesForEdgeFlags() { - FlagEncoder car0 = FlagEncoders.createCar(new PMap("name=car0|speed_bits=29|speed_factor=0.001")); - FlagEncoder car = FlagEncoders.createCar(new PMap("speed_bits=29|speed_factor=0.001")); - EncodingManager manager = EncodingManager.create(car0, car); + BooleanEncodedValue access0Enc = new SimpleBooleanEncodedValue("car0_access", true); + DecimalEncodedValue speed0Enc = new DecimalEncodedValueImpl("car0_speed", 29, 0.001, false); + BooleanEncodedValue access1Enc = new SimpleBooleanEncodedValue("car1_access", true); + DecimalEncodedValue speed1Enc = new DecimalEncodedValueImpl("car1_speed", 29, 0.001, false); + + EncodingManager manager = EncodingManager.start() + .add(access0Enc).add(speed0Enc) + .add(access1Enc).add(speed1Enc) + .build(); graph = new BaseGraph.Builder(manager).create(); EdgeIteratorState edge = graph.edge(0, 1); @@ -672,33 +684,29 @@ public void test8AndMoreBytesForEdgeFlags() { graph = new BaseGraph.Builder(manager).create(); - DecimalEncodedValue avSpeed0Enc = manager.getDecimalEncodedValue(getKey("car0", "average_speed")); - BooleanEncodedValue access0Enc = manager.getBooleanEncodedValue(getKey("car0", "access")); - DecimalEncodedValue avSpeed1Enc = manager.getDecimalEncodedValue(getKey("car", "average_speed")); - BooleanEncodedValue access1Enc = manager.getBooleanEncodedValue(getKey("car", "access")); edge = graph.edge(0, 1); - GHUtility.setSpeed(99.123, true, true, access0Enc, avSpeed0Enc, edge); - assertEquals(99.123, edge.get(avSpeed0Enc), 1e-3); + GHUtility.setSpeed(99.123, true, true, access0Enc, speed0Enc, edge); + assertEquals(99.123, edge.get(speed0Enc), 1e-3); EdgeIteratorState edgeIter = GHUtility.getEdge(graph, 1, 0); - assertEquals(99.123, edgeIter.get(avSpeed0Enc), 1e-3); + assertEquals(99.123, edgeIter.get(speed0Enc), 1e-3); assertTrue(edgeIter.get(access0Enc)); assertTrue(edgeIter.getReverse(access0Enc)); edge = graph.edge(2, 3); - GHUtility.setSpeed(44.123, true, false, access1Enc, avSpeed1Enc, edge); - assertEquals(44.123, edge.get(avSpeed1Enc), 1e-3); + GHUtility.setSpeed(44.123, true, false, access1Enc, speed1Enc, edge); + assertEquals(44.123, edge.get(speed1Enc), 1e-3); edgeIter = GHUtility.getEdge(graph, 3, 2); - assertEquals(44.123, edgeIter.get(avSpeed1Enc), 1e-3); - assertEquals(44.123, edgeIter.getReverse(avSpeed1Enc), 1e-3); + assertEquals(44.123, edgeIter.get(speed1Enc), 1e-3); + assertEquals(44.123, edgeIter.getReverse(speed1Enc), 1e-3); assertFalse(edgeIter.get(access1Enc)); assertTrue(edgeIter.getReverse(access1Enc)); - manager = EncodingManager.create( - FlagEncoders.createCar(new PMap("name=car0|speed_bits=29|speed_factor=0.001")), - FlagEncoders.createCar(new PMap("speed_bits=29|speed_factor=0.001")), - FlagEncoders.createCar(new PMap("name=car2|speed_bits=30|speed_factor=0.001")) - ); + manager = EncodingManager.start() + .add(new SimpleBooleanEncodedValue("car0_access", true)).add(new DecimalEncodedValueImpl("car0_speed", 29, 0.001, false)) + .add(new SimpleBooleanEncodedValue("car1_access", true)).add(new DecimalEncodedValueImpl("car1_speed", 29, 0.001, false)) + .add(new SimpleBooleanEncodedValue("car2_access", true)).add(new DecimalEncodedValueImpl("car2_speed", 30, 0.001, false)) + .build(); graph = new BaseGraph.Builder(manager).create(); edgeIter = graph.edge(0, 1).set(access0Enc, true, false); assertTrue(edgeIter.get(access0Enc)); diff --git a/core/src/test/java/com/graphhopper/storage/BaseGraphTest.java b/core/src/test/java/com/graphhopper/storage/BaseGraphTest.java index 939c9d07449..ac94ea30e92 100644 --- a/core/src/test/java/com/graphhopper/storage/BaseGraphTest.java +++ b/core/src/test/java/com/graphhopper/storage/BaseGraphTest.java @@ -79,7 +79,7 @@ public void testSave_and_fileFormat() { assertEquals("named street1", graph.getEdgeIteratorState(iter1.getEdge(), iter1.getAdjNode()).getName()); assertEquals("named street2", graph.getEdgeIteratorState(iter2.getEdge(), iter2.getAdjNode()).getName()); - GHUtility.setSpeed(60, true, true, carAccessEnc, carAvSpeedEnc, graph.edge(3, 4).setDistance(123)). + GHUtility.setSpeed(60, true, true, carAccessEnc, carSpeedEnc, graph.edge(3, 4).setDistance(123)). setWayGeometry(Helper.createPointList3D(4.4, 5.5, 0, 6.6, 7.7, 0)); checkGraph(graph); } @@ -200,9 +200,9 @@ public void testDecoupledEdgeIteratorStates() { Graph graph = storage.getBaseGraph(); IntsRef ref = encodingManager.createEdgeFlags(); ref.ints[0] = 12; - GHUtility.setSpeed(60, true, true, carAccessEnc, carAvSpeedEnc, graph.edge(1, 2).setDistance(10)).setFlags(ref); + GHUtility.setSpeed(60, true, true, carAccessEnc, carSpeedEnc, graph.edge(1, 2).setDistance(10)).setFlags(ref); ref.ints[0] = 13; - GHUtility.setSpeed(60, true, true, carAccessEnc, carAvSpeedEnc, graph.edge(1, 3).setDistance(10)).setFlags(ref); + GHUtility.setSpeed(60, true, true, carAccessEnc, carSpeedEnc, graph.edge(1, 3).setDistance(10)).setFlags(ref); EdgeIterator iter = graph.createEdgeExplorer().setBaseNode(1); assertTrue(iter.next()); @@ -219,7 +219,7 @@ public void testDecoupledEdgeIteratorStates() { @Test public void testEdgeKey() { BaseGraph g = new BaseGraph.Builder(encodingManager).create(); - GHUtility.setSpeed(60, true, true, carAccessEnc, carAvSpeedEnc, g.edge(0, 1).setDistance(10)); + GHUtility.setSpeed(60, true, true, carAccessEnc, carSpeedEnc, g.edge(0, 1).setDistance(10)); // storage direction assertEdge(g.getEdgeIteratorState(0, Integer.MIN_VALUE), 0, 1, false, 0, 0); // reverse direction @@ -233,7 +233,7 @@ public void testEdgeKey() { @Test public void testEdgeKey_loop() { BaseGraph g = new BaseGraph.Builder(encodingManager).create(); - GHUtility.setSpeed(60, true, true, carAccessEnc, carAvSpeedEnc, g.edge(0, 0).setDistance(10)); + GHUtility.setSpeed(60, true, true, carAccessEnc, carSpeedEnc, g.edge(0, 0).setDistance(10)); // storage direction assertEdge(g.getEdgeIteratorState(0, Integer.MIN_VALUE), 0, 0, false, 0, 0); // reverse direction cannot be retrieved, we get forward direction anyway diff --git a/core/src/test/java/com/graphhopper/storage/BaseGraphWithTurnCostsTest.java b/core/src/test/java/com/graphhopper/storage/BaseGraphWithTurnCostsTest.java index e73beea37bd..a3ee2f91e88 100644 --- a/core/src/test/java/com/graphhopper/storage/BaseGraphWithTurnCostsTest.java +++ b/core/src/test/java/com/graphhopper/storage/BaseGraphWithTurnCostsTest.java @@ -17,13 +17,11 @@ */ package com.graphhopper.storage; -import com.graphhopper.routing.ev.EncodedValueLookup; +import com.graphhopper.routing.ev.DecimalEncodedValue; import com.graphhopper.routing.ev.TurnCost; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; +import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.Helper; -import com.graphhopper.util.PMap; import org.junit.jupiter.api.Test; import java.util.Random; @@ -35,9 +33,16 @@ * @author Karl Hübner */ public class BaseGraphWithTurnCostsTest extends BaseGraphTest { + + private DecimalEncodedValue turnCostEnc; + @Override - FlagEncoder createCarFlagEncoder() { - return FlagEncoders.createCar(new PMap().putObject("max_turn_costs", 1400)); + protected EncodingManager createEncodingManager() { + turnCostEnc = TurnCost.create("car", 1400); + return EncodingManager.start() + .add(carAccessEnc).add(carSpeedEnc).addTurnCostEncodedValue(turnCostEnc) + .add(footAccessEnc).add(footSpeedEnc) + .build(); } @Override @@ -156,10 +161,10 @@ public void testInitializeTurnCost() { } private double getTurnCost(EdgeIteratorState fromEdge, int viaNode, EdgeIteratorState toEdge) { - return graph.getTurnCostStorage().get(((EncodedValueLookup) encodingManager).getDecimalEncodedValue(TurnCost.key("car")), toEdge.getEdge(), viaNode, fromEdge.getEdge()); + return graph.getTurnCostStorage().get(turnCostEnc, toEdge.getEdge(), viaNode, fromEdge.getEdge()); } private void setTurnCost(int fromEdge, int viaNode, int toEdge, int cost) { - graph.getTurnCostStorage().set(((EncodedValueLookup) encodingManager).getDecimalEncodedValue(TurnCost.key("car")), fromEdge, viaNode, toEdge, cost); + graph.getTurnCostStorage().set(turnCostEnc, fromEdge, viaNode, toEdge, cost); } } From 750ecd3624c05d7a0dce2af150866d85ca03071a Mon Sep 17 00:00:00 2001 From: easbar Date: Sat, 25 Jun 2022 11:49:32 +0200 Subject: [PATCH 11/28] 51, most tests green --- .../routing/DefaultWeightingFactory.java | 44 +++++++++++-------- .../routing/util/EncodingManager.java | 16 +------ .../graphhopper/reader/osm/OSMReaderTest.java | 22 +++++----- .../routing/HeadingRoutingTest.java | 32 +++++++------- .../routing/QueryRoutingCHGraphTest.java | 2 +- .../routing/RoutingAlgorithmTest.java | 4 +- .../routing/ev/DecimalEncodedValueTest.java | 1 - .../routing/util/MotorcycleTagParserTest.java | 2 +- .../custom/CustomModelParserTest.java | 4 +- .../graphhopper/example/IsochroneExample.java | 8 ++-- .../example/LowLevelAPIExample.java | 33 +++++++++----- .../java/com/graphhopper/gtfs/GtfsReader.java | 8 ++-- .../gtfs/PtRouterFreeWalkImpl.java | 15 +++++-- .../com/graphhopper/gtfs/PtRouterImpl.java | 10 ++++- .../com/graphhopper/tools/Measurement.java | 7 +-- .../java/com/graphhopper/ui/MiniGraphUI.java | 2 +- .../resources/PtIsochroneResource.java | 8 ++-- 17 files changed, 122 insertions(+), 96 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java index 103b8926e39..f072d094e0a 100644 --- a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java +++ b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java @@ -19,10 +19,8 @@ package com.graphhopper.routing; import com.graphhopper.config.Profile; -import com.graphhopper.routing.ev.EnumEncodedValue; -import com.graphhopper.routing.ev.RoadAccess; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.weighting.*; import com.graphhopper.routing.weighting.custom.CustomModelParser; import com.graphhopper.routing.weighting.custom.CustomProfile; @@ -56,13 +54,14 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis hints.putAll(profile.getHints()); hints.putAll(requestHints); - FlagEncoder encoder = encodingManager.getEncoder(profile.getVehicle()); + final String vehicle = profile.getVehicle(); TurnCostProvider turnCostProvider; if (profile.isTurnCosts() && !disableTurnCosts) { - if (!encoder.supportsTurnCosts()) - throw new IllegalArgumentException("Encoder " + encoder + " does not support turn costs"); + DecimalEncodedValue turnCostEnc = encodingManager.getDecimalEncodedValue(TurnCost.key(vehicle)); + if (turnCostEnc == null) + throw new IllegalArgumentException("Vehicle " + vehicle + " does not support turn costs"); int uTurnCosts = hints.getInt(Parameters.Routing.U_TURN_COSTS, INFINITE_U_TURN_COSTS); - turnCostProvider = new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage(), uTurnCosts); + turnCostProvider = new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage(), uTurnCosts); } else { turnCostProvider = NO_TURN_COST_PROVIDER; } @@ -72,37 +71,46 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis throw new IllegalArgumentException("You have to specify a weighting"); Weighting weighting = null; + BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(EncodingManager.getKey(vehicle, "access")); + DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey(vehicle, "average_speed")); + DecimalEncodedValue priorityEnc = encodingManager.hasEncodedValue(EncodingManager.getKey(vehicle, "priority")) + ? encodingManager.getDecimalEncodedValue(EncodingManager.getKey(vehicle, "priority")) + : null; if (CustomWeighting.NAME.equalsIgnoreCase(weightingStr)) { if (!(profile instanceof CustomProfile)) throw new IllegalArgumentException("custom weighting requires a CustomProfile but was profile=" + profile.getName()); CustomModel queryCustomModel = requestHints.getObject(CustomModel.KEY, null); CustomProfile customProfile = (CustomProfile) profile; queryCustomModel = CustomModel.merge(customProfile.getCustomModel(), queryCustomModel); - weighting = CustomModelParser.createWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), - encoder.getPriorityEnc(), encoder.getMaxSpeed(), encodingManager, turnCostProvider, queryCustomModel); + // todonow: this used to be encoder.getMaxSpeed()! + double maxSpeed = speedEnc.getMaxDecimal(); + weighting = CustomModelParser.createWeighting(accessEnc, speedEnc, + priorityEnc, maxSpeed, encodingManager, turnCostProvider, queryCustomModel); } else if ("shortest".equalsIgnoreCase(weightingStr)) { - weighting = new ShortestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), turnCostProvider); + weighting = new ShortestWeighting(accessEnc, speedEnc, turnCostProvider); } else if ("fastest".equalsIgnoreCase(weightingStr)) { if (!encodingManager.hasEncodedValue(RoadAccess.KEY)) throw new IllegalArgumentException("fastest weighting requires road_access"); EnumEncodedValue roadAccessEnc = encodingManager.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class); - if (encoder.getPriorityEnc() != null) - weighting = new PriorityWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), encoder.getPriorityEnc(), roadAccessEnc, hints, turnCostProvider); + if (priorityEnc != null) + weighting = new PriorityWeighting(accessEnc, speedEnc, priorityEnc, roadAccessEnc, hints, turnCostProvider); else - weighting = new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), roadAccessEnc, hints, turnCostProvider); + weighting = new FastestWeighting(accessEnc, speedEnc, roadAccessEnc, hints, turnCostProvider); } else if ("curvature".equalsIgnoreCase(weightingStr)) { - if (encoder.getCurvatureEnc() == null || encoder.getPriorityEnc() == null) - throw new IllegalArgumentException("curvature weighting requires curvature and priority, but not found for " + encoder.getName()); + DecimalEncodedValue curvatureEnc = encodingManager.hasEncodedValue(EncodingManager.getKey(vehicle, "curvature")) + ? encodingManager.getDecimalEncodedValue(EncodingManager.getKey(vehicle, "curvature")) + : null; + if (curvatureEnc == null || priorityEnc == null) + throw new IllegalArgumentException("curvature weighting requires curvature and priority, but not found for " + vehicle); if (!encodingManager.hasEncodedValue(RoadAccess.KEY)) throw new IllegalArgumentException("curvature weighting requires road_access"); EnumEncodedValue roadAccessEnc = encodingManager.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class); - weighting = new CurvatureWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), encoder.getPriorityEnc(), - encoder.getCurvatureEnc(), roadAccessEnc, hints, turnCostProvider); + weighting = new CurvatureWeighting(accessEnc, speedEnc, priorityEnc, curvatureEnc, roadAccessEnc, hints, turnCostProvider); } else if ("short_fastest".equalsIgnoreCase(weightingStr)) { if (!encodingManager.hasEncodedValue(RoadAccess.KEY)) throw new IllegalArgumentException("curvature weighting requires road_access"); EnumEncodedValue roadAccessEnc = encodingManager.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class); - weighting = new ShortFastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), roadAccessEnc, hints, turnCostProvider); + weighting = new ShortFastestWeighting(accessEnc, speedEnc, roadAccessEnc, hints, turnCostProvider); } if (weighting == null) diff --git a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java index 99aea6e686b..eed89a64d38 100644 --- a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java +++ b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java @@ -61,21 +61,13 @@ public static EncodingManager create(FlagEncoderFactory factory, String flagEnco * Instantiate manager with the given list of encoders. */ public static EncodingManager create(FlagEncoder... flagEncoders) { - return create(Arrays.asList(flagEncoders)); - } - - /** - * Instantiate manager with the given list of encoders. - */ - public static EncodingManager create(List flagEncoders) { - return createBuilder(flagEncoders).build(); + return EncodingManager.createBuilder(Arrays.asList(flagEncoders)).build(); } private static EncodingManager.Builder createBuilder(List flagEncoders) { Builder builder = new Builder(); - for (FlagEncoder flagEncoder : flagEncoders) { + for (FlagEncoder flagEncoder : flagEncoders) builder.add(flagEncoder); - } return builder; } @@ -315,10 +307,6 @@ public T getEncodedValue(String key, Class encodedVa * All EncodedValue names that are created from a FlagEncoder should use this method to mark them as * "none-shared" across the other FlagEncoders. */ - public static String getKey(FlagEncoder encoder, String str) { - return getKey(encoder.toString(), str); - } - public static String getKey(String prefix, String str) { return prefix + "_" + str; } diff --git a/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java b/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java index 0c4081b39f0..800b580081c 100644 --- a/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java +++ b/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java @@ -64,9 +64,9 @@ public class OSMReaderTest { private final String file7 = "test-osm7.xml"; private final String fileBarriers = "test-barriers.xml"; private final String dir = "./target/tmp/test-db"; - private FlagEncoder carEncoder; private BooleanEncodedValue carAccessEnc; - private FlagEncoder footEncoder; + private DecimalEncodedValue carSpeedEnc; + private BooleanEncodedValue footAccessEnc; private EdgeExplorer carOutExplorer; private EdgeExplorer carAllExplorer; @@ -218,13 +218,13 @@ public void cleanUp() { int n80 = AbstractGraphStorageTester.getIdOf(graph, 54.1); EdgeIterator iter = carOutExplorer.setBaseNode(n80); iter.next(); - assertEquals(5, iter.get(carEncoder.getAverageSpeedEnc()), 1e-1); + assertEquals(5, iter.get(carSpeedEnc), 1e-1); // duration 01:10 is given => more precise speed calculation! // ~111km (from 54.0,10.1 to 55.0,10.2) in duration=70 minutes => 95km/h => / 1.4 => 71km/h iter = carOutExplorer.setBaseNode(n40); iter.next(); - assertEquals(70, iter.get(carEncoder.getAverageSpeedEnc()), 1e-1); + assertEquals(70, iter.get(carSpeedEnc), 1e-1); } @Test @@ -239,7 +239,7 @@ public void cleanUp() { int n60 = AbstractGraphStorageTester.getIdOf(graph, 56.0); EdgeIterator iter = carOutExplorer.setBaseNode(n60); iter.next(); - assertEquals(35, iter.get(carEncoder.getAverageSpeedEnc()), 1e-1); + assertEquals(35, iter.get(carSpeedEnc), 1e-1); } @Test @@ -309,7 +309,7 @@ public void testFoot() { assertEquals(GHUtility.asSet(n10, n30, n40), GHUtility.getNeighbors(carAllExplorer.setBaseNode(n20))); assertEquals(GHUtility.asSet(n30, n40), GHUtility.getNeighbors(carOutExplorer.setBaseNode(n20))); - EdgeExplorer footOutExplorer = graph.createEdgeExplorer(AccessFilter.outEdges(footEncoder.getAccessEnc())); + EdgeExplorer footOutExplorer = graph.createEdgeExplorer(AccessFilter.outEdges(footAccessEnc)); assertEquals(GHUtility.asSet(n20, n50), GHUtility.getNeighbors(footOutExplorer.setBaseNode(n10))); assertEquals(GHUtility.asSet(n20, n50), GHUtility.getNeighbors(footOutExplorer.setBaseNode(n30))); assertEquals(GHUtility.asSet(n10, n30), GHUtility.getNeighbors(footOutExplorer.setBaseNode(n20))); @@ -414,12 +414,12 @@ public void testFords() { Graph graph = hopper.getBaseGraph(); // our way is split into five edges, because there are two ford nodes assertEquals(5, graph.getEdges()); - FlagEncoder encoder = hopper.getEncodingManager().fetchEdgeEncoders().get(0); + BooleanEncodedValue accessEnc = hopper.getEncodingManager().getBooleanEncodedValue("car_access"); int blocked = 0; int notBlocked = 0; AllEdgesIterator edge = graph.getAllEdges(); while (edge.next()) { - if (!edge.get(encoder.getAccessEnc())) + if (!edge.get(accessEnc)) blocked++; else notBlocked++; @@ -1017,11 +1017,11 @@ protected void importOSM() { BaseGraph baseGraph = new BaseGraph.Builder(getEncodingManager()).set3D(hasElevation()).withTurnCosts(getEncodingManager().needsTurnCostsSupport()).build(); setBaseGraph(baseGraph); super.importOSM(); - carEncoder = getEncodingManager().getEncoder("car"); - footEncoder = getEncodingManager().getEncoder("foot"); - carAccessEnc = carEncoder.getAccessEnc(); + carAccessEnc = getEncodingManager().getBooleanEncodedValue("car_access"); + carSpeedEnc = getEncodingManager().getDecimalEncodedValue("car_average_speed"); carOutExplorer = getBaseGraph().createEdgeExplorer(AccessFilter.outEdges(carAccessEnc)); carAllExplorer = getBaseGraph().createEdgeExplorer(AccessFilter.allEdges(carAccessEnc)); + footAccessEnc = getEncodingManager().getBooleanEncodedValue("foot_access"); } @Override diff --git a/core/src/test/java/com/graphhopper/routing/HeadingRoutingTest.java b/core/src/test/java/com/graphhopper/routing/HeadingRoutingTest.java index 499fafe29ec..a681939e525 100644 --- a/core/src/test/java/com/graphhopper/routing/HeadingRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/HeadingRoutingTest.java @@ -54,8 +54,8 @@ class HeadingRoutingTest { @Test public void headingTest1() { // Test enforce start direction - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -78,8 +78,8 @@ public void headingTest1() { @Test public void headingTest2() { // Test enforce south start direction and east end direction - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -106,8 +106,8 @@ public void headingTest2() { @Test public void headingTest3() { - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -132,8 +132,8 @@ public void headingTest3() { @Test public void headingTest4() { // Test straight via routing - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -158,8 +158,8 @@ public void headingTest4() { @Test public void headingTest5() { // Test independence of previous enforcement for subsequent paths - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -183,8 +183,8 @@ public void headingTest5() { @Test public void testHeadingWithSnapFilter() { - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraphWithTunnel(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -246,8 +246,8 @@ public void testHeadingWithSnapFilter() { @Test public void testHeadingWithSnapFilter2() { - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraphWithTunnel(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -280,8 +280,8 @@ public void testHeadingWithSnapFilter2() { @Test public void headingTest6() { // Test if snaps at tower nodes are ignored - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); diff --git a/core/src/test/java/com/graphhopper/routing/QueryRoutingCHGraphTest.java b/core/src/test/java/com/graphhopper/routing/QueryRoutingCHGraphTest.java index f4d740a77b5..a556844f224 100644 --- a/core/src/test/java/com/graphhopper/routing/QueryRoutingCHGraphTest.java +++ b/core/src/test/java/com/graphhopper/routing/QueryRoutingCHGraphTest.java @@ -54,7 +54,7 @@ public void setup() { speedEnc = new DecimalEncodedValueImpl("speed", 5, 5, true); turnCostEnc = TurnCost.create("car", 5); encodingManager = EncodingManager.start().add(accessEnc).add(speedEnc).addTurnCostEncodedValue(turnCostEnc).build(); - graph = new BaseGraph.Builder(encodingManager).create(); + graph = new BaseGraph.Builder(encodingManager).withTurnCosts(true).create(); weighting = new FastestWeighting(accessEnc, speedEnc, new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage())); na = graph.getNodeAccess(); } diff --git a/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmTest.java b/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmTest.java index 3fec2fa2542..aeafcf8bf19 100644 --- a/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmTest.java +++ b/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmTest.java @@ -103,8 +103,8 @@ public Fixture(PathCalculator pathCalculator, TraversalMode traversalMode) { this.pathCalculator = pathCalculator; this.traversalMode = traversalMode; carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); - footAccessEnc = new SimpleBooleanEncodedValue("car_access", true); - bike2AccessEnc = new SimpleBooleanEncodedValue("car_access", true); + footAccessEnc = new SimpleBooleanEncodedValue("foot_access", true); + bike2AccessEnc = new SimpleBooleanEncodedValue("bike2_access", true); carSpeedEnc = new DecimalEncodedValueImpl("car_speed", 5, 5, false); footSpeedEnc = new DecimalEncodedValueImpl("foot_speed", 4, 1, false); bike2SpeedEnc = new DecimalEncodedValueImpl("bike2_speed", 4, 2, true); diff --git a/core/src/test/java/com/graphhopper/routing/ev/DecimalEncodedValueTest.java b/core/src/test/java/com/graphhopper/routing/ev/DecimalEncodedValueTest.java index 4aeb1d5ed6b..7445b2c1cc5 100644 --- a/core/src/test/java/com/graphhopper/routing/ev/DecimalEncodedValueTest.java +++ b/core/src/test/java/com/graphhopper/routing/ev/DecimalEncodedValueTest.java @@ -22,7 +22,6 @@ public void testInit() { public void testMaxValue() { DecimalEncodedValue ev = new DecimalEncodedValueImpl("test1", 8, 0.5, false); EncodingManager em = EncodingManager.start().add(ev).build(); - ev.init(new EncodedValue.InitializerConfig()); IntsRef flags = em.createEdgeFlags(); ev.setDecimal(false, flags, 100d); assertEquals(100, ev.getDecimal(false, flags), 1e-1); diff --git a/core/src/test/java/com/graphhopper/routing/util/MotorcycleTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/MotorcycleTagParserTest.java index ac18ec0ad83..6bab8c518cb 100644 --- a/core/src/test/java/com/graphhopper/routing/util/MotorcycleTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/MotorcycleTagParserTest.java @@ -47,7 +47,7 @@ public class MotorcycleTagParserTest { private final BooleanEncodedValue footAccessEnc = new SimpleBooleanEncodedValue("foot_access", true); private final DecimalEncodedValue footSpeedEnc = new DecimalEncodedValueImpl("foot_speed", 4, 1, false); private final EncodingManager em = EncodingManager.start() - .add(motorcycleAccessEnc).add(motorcycleSpeedEnc).add(motorcyclePriorityEnc).add(motorcyclePriorityEnc) + .add(motorcycleAccessEnc).add(motorcycleSpeedEnc).add(motorcyclePriorityEnc).add(motorcycleCurvatureEnc) .add(footAccessEnc).add(footSpeedEnc) .build(); private final MotorcycleTagParser parser; diff --git a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomModelParserTest.java b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomModelParserTest.java index 429fb7b7e81..fa45b1346fd 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomModelParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomModelParserTest.java @@ -49,8 +49,8 @@ class CustomModelParserTest { @BeforeEach void setup() { - accessEnc = new SimpleBooleanEncodedValue("access", true); - avgSpeedEnc = new DecimalEncodedValueImpl("speed", 5, 5, false); + accessEnc = new SimpleBooleanEncodedValue("car_access", true); + avgSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); countryEnc = new StringEncodedValue("country", 10); encodingManager = new EncodingManager.Builder().add(accessEnc).add(avgSpeedEnc).add(countryEnc).add(new EnumEncodedValue<>(Surface.KEY, Surface.class)).build(); graph = new BaseGraph.Builder(encodingManager).create(); diff --git a/example/src/main/java/com/graphhopper/example/IsochroneExample.java b/example/src/main/java/com/graphhopper/example/IsochroneExample.java index ff80ed904bd..26677d5001d 100644 --- a/example/src/main/java/com/graphhopper/example/IsochroneExample.java +++ b/example/src/main/java/com/graphhopper/example/IsochroneExample.java @@ -3,11 +3,12 @@ import com.graphhopper.GraphHopper; import com.graphhopper.config.Profile; import com.graphhopper.isochrone.algorithm.ShortestPathTree; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; import com.graphhopper.routing.ev.Subnetwork; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.util.DefaultSnapFilter; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.storage.index.Snap; @@ -20,10 +21,11 @@ public static void main(String[] args) { GraphHopper hopper = createGraphHopperInstance(relDir + "core/files/andorra.osm.pbf"); // get encoder from GraphHopper instance EncodingManager encodingManager = hopper.getEncodingManager(); - FlagEncoder encoder = encodingManager.getEncoder("car"); + BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(EncodingManager.getKey("car", "access")); + DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey("car", "average_speed")); // snap some GPS coordinates to the routing graph and build a query graph - FastestWeighting weighting = new FastestWeighting(encoder); + FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); Snap snap = hopper.getLocationIndex().findClosest(42.508679, 1.532078, new DefaultSnapFilter(weighting, encodingManager.getBooleanEncodedValue(Subnetwork.key("car")))); QueryGraph queryGraph = QueryGraph.create(hopper.getBaseGraph(), snap); diff --git a/example/src/main/java/com/graphhopper/example/LowLevelAPIExample.java b/example/src/main/java/com/graphhopper/example/LowLevelAPIExample.java index 4761cb58979..9e2495d1dc3 100644 --- a/example/src/main/java/com/graphhopper/example/LowLevelAPIExample.java +++ b/example/src/main/java/com/graphhopper/example/LowLevelAPIExample.java @@ -5,8 +5,14 @@ import com.graphhopper.routing.Path; import com.graphhopper.routing.ch.CHRoutingAlgorithmFactory; import com.graphhopper.routing.ch.PrepareContractionHierarchies; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.querygraph.QueryGraph; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.*; @@ -31,15 +37,16 @@ public static void main(String[] args) { public static void createAndSaveGraph() { { - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).setDir(new RAMDirectory(graphLocation, true)).create(); // Make a weighted edge between two nodes and set average speed to 50km/h - EdgeIteratorState edge = graph.edge(0, 1).setDistance(1234).set(encoder.getAverageSpeedEnc(), 50); + EdgeIteratorState edge = graph.edge(0, 1).setDistance(1234).set(speedEnc, 50); // Set node coordinates and build location index NodeAccess na = graph.getNodeAccess(); - graph.edge(0, 1).set(encoder.getAccessEnc(), true).set(encoder.getAverageSpeedEnc(), 10).setDistance(1530); + graph.edge(0, 1).set(accessEnc, true).set(speedEnc, 10).setDistance(1530); na.setNode(0, 15.15, 20.20); na.setNode(1, 15.25, 20.21); LocationIndexTree index = new LocationIndexTree(graph, graph.getDirectory()); @@ -55,8 +62,9 @@ public static void createAndSaveGraph() { { // Load the graph ... can be also in a different code location // note that the EncodingManager must be the same - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).setDir(new RAMDirectory(graphLocation, true)).build(); graph.loadExisting(); @@ -69,7 +77,7 @@ public static void createAndSaveGraph() { Snap fromSnap = index.findClosest(15.15, 20.20, EdgeFilter.ALL_EDGES); Snap toSnap = index.findClosest(15.25, 20.21, EdgeFilter.ALL_EDGES); QueryGraph queryGraph = QueryGraph.create(graph, fromSnap, toSnap); - Weighting weighting = new FastestWeighting(encoder); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc); Path path = new Dijkstra(queryGraph, weighting, TraversalMode.NODE_BASED).calcPath(fromSnap.getClosestNode(), toSnap.getClosestNode()); assert Helper.round(path.getDistance(), -2) == 1500; @@ -81,9 +89,10 @@ public static void createAndSaveGraph() { public static void useContractionHierarchiesToMakeQueriesFaster() { // Creating and saving the graph - FlagEncoder encoder = FlagEncoders.createCar(); - EncodingManager em = EncodingManager.create(encoder); - Weighting weighting = new FastestWeighting(encoder); + BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); + Weighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased("my_profile", weighting); BaseGraph graph = new BaseGraph.Builder(em) .setDir(new RAMDirectory(graphLocation, true)) @@ -92,7 +101,7 @@ public static void useContractionHierarchiesToMakeQueriesFaster() { // Set node coordinates and build location index NodeAccess na = graph.getNodeAccess(); - graph.edge(0, 1).set(encoder.getAccessEnc(), true).set(encoder.getAverageSpeedEnc(), 10).setDistance(1020); + graph.edge(0, 1).set(accessEnc, true).set(speedEnc, 10).setDistance(1020); na.setNode(0, 15.15, 20.20); na.setNode(1, 15.25, 20.21); diff --git a/reader-gtfs/src/main/java/com/graphhopper/gtfs/GtfsReader.java b/reader-gtfs/src/main/java/com/graphhopper/gtfs/GtfsReader.java index 8847de0a08a..bc47dac7147 100644 --- a/reader-gtfs/src/main/java/com/graphhopper/gtfs/GtfsReader.java +++ b/reader-gtfs/src/main/java/com/graphhopper/gtfs/GtfsReader.java @@ -22,11 +22,12 @@ import com.conveyal.gtfs.model.*; import com.google.common.collect.HashMultimap; import com.google.transit.realtime.GtfsRealtime; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; import com.graphhopper.routing.ev.Subnetwork; import com.graphhopper.routing.util.DefaultSnapFilter; import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.index.InMemConstructionIndex; @@ -104,8 +105,9 @@ static class TripWithStopTimes { } void connectStopsToStreetNetwork() { - FlagEncoder footEncoder = encodingManager.getEncoder("foot"); - final EdgeFilter filter = new DefaultSnapFilter(new FastestWeighting(footEncoder), encodingManager.getBooleanEncodedValue(Subnetwork.key("foot"))); + BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(EncodingManager.getKey("foot", "access")); + DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey("foot", "average_speed")); + final EdgeFilter filter = new DefaultSnapFilter(new FastestWeighting(accessEnc, speedEnc), encodingManager.getBooleanEncodedValue(Subnetwork.key("foot"))); for (Stop stop : feed.stops.values()) { if (stop.location_type == 0) { // Only stops. Not interested in parent stations for now. Snap locationSnap = walkNetworkIndex.findClosest(stop.stop_lat, stop.stop_lon, filter); diff --git a/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterFreeWalkImpl.java b/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterFreeWalkImpl.java index 7cd6e9f414c..d8790875909 100644 --- a/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterFreeWalkImpl.java +++ b/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterFreeWalkImpl.java @@ -64,7 +64,10 @@ public final class PtRouterFreeWalkImpl implements PtRouter { public PtRouterFreeWalkImpl(GraphHopperConfig config, TranslationMap translationMap, BaseGraph baseGraph, EncodingManager encodingManager, LocationIndex locationIndex, GtfsStorage gtfsStorage, RealtimeFeed realtimeFeed, PathDetailsBuilderFactory pathDetailsBuilderFactory) { this.config = config; this.weightingFactory = new DefaultWeightingFactory(baseGraph.getBaseGraph(), encodingManager); - this.accessEgressWeighting = new FastestWeighting(encodingManager.getEncoder("foot")); + this.accessEgressWeighting = new FastestWeighting( + encodingManager.getBooleanEncodedValue(EncodingManager.getKey("foot", "access")), + encodingManager.getDecimalEncodedValue(EncodingManager.getKey("foot", "average_speed")) + ); this.translationMap = translationMap; this.baseGraph = baseGraph; this.encodingManager = encodingManager; @@ -166,10 +169,16 @@ private class RequestHandler { requestedPathDetails = request.getPathDetails(); accessProfile = config.getProfiles().stream().filter(p -> p.getName().equals(request.getAccessProfile())).findFirst().get(); accessWeighting = weightingFactory.createWeighting(accessProfile, new PMap(), false); - accessSnapFilter = new DefaultSnapFilter(new FastestWeighting(encodingManager.getEncoder(accessProfile.getVehicle())), encodingManager.getBooleanEncodedValue(Subnetwork.key(accessProfile.getVehicle()))); + accessSnapFilter = new DefaultSnapFilter(new FastestWeighting( + encodingManager.getBooleanEncodedValue(EncodingManager.getKey(accessProfile.getVehicle(), "access")), + encodingManager.getDecimalEncodedValue(EncodingManager.getKey(accessProfile.getVehicle(), "average_speed")) + ), encodingManager.getBooleanEncodedValue(Subnetwork.key(accessProfile.getVehicle()))); egressProfile = config.getProfiles().stream().filter(p -> p.getName().equals(request.getEgressProfile())).findFirst().get(); egressWeighting = weightingFactory.createWeighting(egressProfile, new PMap(), false); - egressSnapFilter = new DefaultSnapFilter(new FastestWeighting(encodingManager.getEncoder(egressProfile.getVehicle())), encodingManager.getBooleanEncodedValue(Subnetwork.key(egressProfile.getVehicle()))); + egressSnapFilter = new DefaultSnapFilter(new FastestWeighting( + encodingManager.getBooleanEncodedValue(EncodingManager.getKey(egressProfile.getVehicle(), "access")), + encodingManager.getDecimalEncodedValue(EncodingManager.getKey(egressProfile.getVehicle(), "average_speed")) + ), encodingManager.getBooleanEncodedValue(Subnetwork.key(egressProfile.getVehicle()))); } GHResponse route() { diff --git a/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterImpl.java b/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterImpl.java index 573679469d5..4ea6123b9f5 100644 --- a/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterImpl.java +++ b/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterImpl.java @@ -166,10 +166,16 @@ private class RequestHandler { requestedPathDetails = request.getPathDetails(); accessProfile = config.getProfiles().stream().filter(p -> p.getName().equals(request.getAccessProfile())).findFirst().get(); accessWeighting = weightingFactory.createWeighting(accessProfile, new PMap(), false); - accessSnapFilter = new DefaultSnapFilter(new FastestWeighting(encodingManager.getEncoder(accessProfile.getVehicle())), encodingManager.getBooleanEncodedValue(Subnetwork.key(accessProfile.getVehicle()))); + accessSnapFilter = new DefaultSnapFilter(new FastestWeighting( + encodingManager.getBooleanEncodedValue(EncodingManager.getKey(accessProfile.getVehicle(), "access")), + encodingManager.getDecimalEncodedValue(EncodingManager.getKey(accessProfile.getVehicle(), "average_speed")) + ), encodingManager.getBooleanEncodedValue(Subnetwork.key(accessProfile.getVehicle()))); egressProfile = config.getProfiles().stream().filter(p -> p.getName().equals(request.getEgressProfile())).findFirst().get(); egressWeighting = weightingFactory.createWeighting(egressProfile, new PMap(), false); - egressSnapFilter = new DefaultSnapFilter(new FastestWeighting(encodingManager.getEncoder(egressProfile.getVehicle())), encodingManager.getBooleanEncodedValue(Subnetwork.key(egressProfile.getVehicle()))); + egressSnapFilter = new DefaultSnapFilter(new FastestWeighting( + encodingManager.getBooleanEncodedValue(EncodingManager.getKey(egressProfile.getVehicle(), "access")), + encodingManager.getDecimalEncodedValue(EncodingManager.getKey(egressProfile.getVehicle(), "average_speed")) + ), encodingManager.getBooleanEncodedValue(Subnetwork.key(egressProfile.getVehicle()))); } GHResponse route() { diff --git a/tools/src/main/java/com/graphhopper/tools/Measurement.java b/tools/src/main/java/com/graphhopper/tools/Measurement.java index 5e74ee264f2..5333a9e15ec 100644 --- a/tools/src/main/java/com/graphhopper/tools/Measurement.java +++ b/tools/src/main/java/com/graphhopper/tools/Measurement.java @@ -28,6 +28,7 @@ import com.graphhopper.config.Profile; import com.graphhopper.jackson.Jackson; import com.graphhopper.routing.ch.PrepareContractionHierarchies; +import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.Subnetwork; import com.graphhopper.routing.lm.LMConfig; import com.graphhopper.routing.lm.PrepareLandmarks; @@ -188,7 +189,7 @@ protected void importOSM() { final boolean runSlow = args.getBool("measurement.run_slow_routing", true); printGraphDetails(g, vehicle); - measureGraphTraversal(g, encoder, count * 100); + measureGraphTraversal(g, encoder.getAccessEnc(), count * 100); measureLocationIndex(g, hopper.getLocationIndex(), count); if (runSlow) { @@ -447,10 +448,10 @@ private void measureLocationIndex(Graph g, final LocationIndex idx, int count) { print("location_index", miniPerf); } - private void measureGraphTraversal(final Graph graph, final FlagEncoder encoder, int count) { + private void measureGraphTraversal(final Graph graph, BooleanEncodedValue accessEnc, int count) { final Random rand = new Random(seed); - EdgeFilter outFilter = AccessFilter.outEdges(encoder.getAccessEnc()); + EdgeFilter outFilter = AccessFilter.outEdges(accessEnc); final EdgeExplorer outExplorer = graph.createEdgeExplorer(outFilter); MiniPerfTest miniPerf = new MiniPerfTest().setIterations(count).start((warmup, run) -> { int nodeId = rand.nextInt(maxNode); diff --git a/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java b/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java index 2decd67a603..3da5adb1e4b 100644 --- a/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java +++ b/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java @@ -333,7 +333,7 @@ private RoutingAlgorithm createAlgo(GraphHopper hopper, QueryGraph qGraph) { }; AlgorithmOptions algoOpts = new AlgorithmOptions().setAlgorithm(Algorithms.ASTAR_BI). setTraversalMode(TraversalMode.EDGE_BASED); - return algoFactory.createAlgo(qGraph, new FastestWeighting(encoder), algoOpts); + return algoFactory.createAlgo(qGraph, new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()), algoOpts); } } diff --git a/web-bundle/src/main/java/com/graphhopper/resources/PtIsochroneResource.java b/web-bundle/src/main/java/com/graphhopper/resources/PtIsochroneResource.java index f364f1dff41..28d228bf628 100644 --- a/web-bundle/src/main/java/com/graphhopper/resources/PtIsochroneResource.java +++ b/web-bundle/src/main/java/com/graphhopper/resources/PtIsochroneResource.java @@ -25,10 +25,11 @@ import com.graphhopper.isochrone.algorithm.ContourBuilder; import com.graphhopper.isochrone.algorithm.ReadableTriangulation; import com.graphhopper.jackson.ResponsePathSerializer; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; import com.graphhopper.routing.ev.Subnetwork; import com.graphhopper.routing.util.DefaultSnapFilter; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.BaseGraph; @@ -94,8 +95,9 @@ public Response doGet( double targetZ = seconds * 1000; GeometryFactory geometryFactory = new GeometryFactory(); - final FlagEncoder footEncoder = encodingManager.getEncoder("foot"); - final Weighting weighting = new FastestWeighting(footEncoder); + BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(EncodingManager.getKey("foot", "access")); + DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey("foot", "average_speed")); + final Weighting weighting = new FastestWeighting(accessEnc, speedEnc); DefaultSnapFilter snapFilter = new DefaultSnapFilter(weighting, encodingManager.getBooleanEncodedValue(Subnetwork.key("foot"))); PtLocationSnapper.Result snapResult = new PtLocationSnapper(baseGraph, locationIndex, gtfsStorage).snapAll(Arrays.asList(location), Arrays.asList(snapFilter)); From 69fdf0b7dc99084a2f608824daca9f270c91ea5a Mon Sep 17 00:00:00 2001 From: easbar Date: Sat, 25 Jun 2022 12:55:30 +0200 Subject: [PATCH 12/28] 48, Remove EncodingManager#hasEncoder and EncodingManager#getEncoder --- .../java/com/graphhopper/GraphHopper.java | 14 +++++++----- .../graphhopper/routing/ProfileResolver.java | 22 ++++++++++++++----- .../routing/util/EncodingManager.java | 17 +------------- .../routing/ProfileResolverTest.java | 8 +++++++ .../routing/util/EncodingManagerTest.java | 7 ------ 5 files changed, 35 insertions(+), 33 deletions(-) diff --git a/core/src/main/java/com/graphhopper/GraphHopper.java b/core/src/main/java/com/graphhopper/GraphHopper.java index 8582b1f9e00..69423861e91 100644 --- a/core/src/main/java/com/graphhopper/GraphHopper.java +++ b/core/src/main/java/com/graphhopper/GraphHopper.java @@ -983,11 +983,15 @@ private void checkProfilesConsistency() { throw new IllegalArgumentException("There has to be at least one profile"); EncodingManager encodingManager = getEncodingManager(); for (Profile profile : profilesByName.values()) { - if (!encodingManager.hasEncoder(profile.getVehicle())) { - throw new IllegalArgumentException("Unknown vehicle '" + profile.getVehicle() + "' in profile: " + profile + ". Make sure all vehicles used in 'profiles' exist in 'graph.flag_encoders'"); - } - FlagEncoder encoder = encodingManager.getEncoder(profile.getVehicle()); - if (profile.isTurnCosts() && !encoder.supportsTurnCosts()) { + String accessEncName = EncodingManager.getKey(profile.getVehicle(), "access"); + String speedEncName = EncodingManager.getKey(profile.getVehicle(), "average_speed"); + if (!encodingManager.hasEncodedValue(accessEncName) || !encodingManager.hasEncodedValue(speedEncName)) + throw new IllegalArgumentException("Unknown vehicle '" + profile.getVehicle() + "' in profile: " + profile + ". " + + "Encoded values " + accessEncName + " and " + speedEncName + " are required"); + DecimalEncodedValue turnCostEnc = encodingManager.hasEncodedValue(TurnCost.key(profile.getVehicle())) + ? encodingManager.getDecimalEncodedValue(TurnCost.key(profile.getVehicle())) + : null; + if (profile.isTurnCosts() && turnCostEnc == null) { throw new IllegalArgumentException("The profile '" + profile.getName() + "' was configured with " + "'turn_costs=true', but the corresponding vehicle '" + profile.getVehicle() + "' does not support turn costs." + "\nYou need to add `|turn_costs=true` to the vehicle in `graph.flag_encoders`"); diff --git a/core/src/main/java/com/graphhopper/routing/ProfileResolver.java b/core/src/main/java/com/graphhopper/routing/ProfileResolver.java index 3785eef7e00..86878a769d6 100644 --- a/core/src/main/java/com/graphhopper/routing/ProfileResolver.java +++ b/core/src/main/java/com/graphhopper/routing/ProfileResolver.java @@ -29,6 +29,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import static com.graphhopper.routing.weighting.Weighting.INFINITE_U_TURN_COSTS; @@ -79,11 +80,22 @@ public Profile resolveProfile(PMap hints) { boolean disableLM = hints.getBool(Parameters.Landmark.DISABLE, false); String vehicle = hints.getString("vehicle", "").toLowerCase(); - if (!vehicle.isEmpty() && !encodingManager.hasEncoder(vehicle)) - throw new IllegalArgumentException("Vehicle not supported: `" + vehicle + "`. Supported are: `" + encodingManager.toString() + - "`\nYou should consider using the `profile` parameter instead of specifying a vehicle." + - "\nAvailable profiles: " + getProfileNames() + - "\nTo learn more about profiles, see: docs/core/profiles.md"); + if (!vehicle.isEmpty()) { + String accessEncName = EncodingManager.getKey(vehicle, "access"); + String speedEncName = EncodingManager.getKey(vehicle, "average_speed"); + if (!encodingManager.hasEncodedValue(accessEncName) || !encodingManager.hasEncodedValue(speedEncName)) { + // we define the supported vehicles as all vehicle names for which there is an access and speed EV + List vehicles = encodingManager.getEncodedValues().stream() + .filter(ev -> ev.getName().endsWith("_access")) + .map(ev -> ev.getName().replaceAll("_access", "")) + .filter(v -> encodingManager.hasEncodedValue(EncodingManager.getKey(v, "average_speed"))) + .collect(Collectors.toList()); + throw new IllegalArgumentException("Vehicle not supported: `" + vehicle + "`. Supported are: `" + vehicles + + "`\nYou should consider using the `profile` parameter instead of specifying a vehicle." + + "\nAvailable profiles: " + getProfileNames() + + "\nTo learn more about profiles, see: docs/core/profiles.md"); + } + } // we select the profile based on the given request hints and the available profiles if (!chProfiles.isEmpty() && !disableCH) { diff --git a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java index eed89a64d38..e779248092c 100644 --- a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java +++ b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java @@ -95,7 +95,7 @@ public static class Builder { public Builder add(FlagEncoder encoder) { checkNotBuiltAlready(); - if (em.hasEncoder(encoder.getName())) + if (em.flagEncoders.containsKey(encoder.getName())) throw new IllegalArgumentException("FlagEncoder already exists: " + encoder.getName()); VehicleEncodedValues v = (VehicleEncodedValues) encoder; v.setEncodedValueLookup(em); @@ -201,21 +201,6 @@ public boolean hasEncodedValue(String key) { return encodedValueMap.get(key) != null; } - public boolean hasEncoder(String encoder) { - return flagEncoders.containsKey(encoder); - } - - public FlagEncoder getEncoder(String name) { - return getEncoder(name, true); - } - - private FlagEncoder getEncoder(String name, boolean throwExc) { - VehicleEncodedValues flagEncoder = flagEncoders.get(name); - if (flagEncoder == null && throwExc) - throw new IllegalArgumentException("FlagEncoder " + name + " not found. Existing: " + flagEncoders.keySet()); - return flagEncoder; - } - public String toFlagEncodersAsString() { return flagEncoders.values().stream().map(VehicleEncodedValues::toSerializationString).collect(Collectors.joining(",")); } diff --git a/core/src/test/java/com/graphhopper/routing/ProfileResolverTest.java b/core/src/test/java/com/graphhopper/routing/ProfileResolverTest.java index 0ede45cc403..3d859d5d4e5 100644 --- a/core/src/test/java/com/graphhopper/routing/ProfileResolverTest.java +++ b/core/src/test/java/com/graphhopper/routing/ProfileResolverTest.java @@ -30,6 +30,7 @@ import java.util.Arrays; import java.util.Collections; +import java.util.List; import static org.junit.jupiter.api.Assertions.*; @@ -92,6 +93,8 @@ public void missingProfiles() { // if we set the weighting as well it works assertEquals("fast_bike", profileResolver.resolveProfile(new PMap().putObject("vehicle", "bike").putObject("weighting", "fastest")).getName()); assertEquals("short_bike", profileResolver.resolveProfile(new PMap().putObject("vehicle", "bike").putObject("weighting", "shortest")).getName()); + + assertUnsupportedVehicle(profileResolver, "unknown", Arrays.asList("car", "bike")); } @Test @@ -165,4 +168,9 @@ private void assertProfileNotFound(ProfileResolver profileResolver, PMap hints) } } + private void assertUnsupportedVehicle(ProfileResolver profileResolver, String vehicle, List supported) { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> profileResolver.resolveProfile(new PMap().putObject("vehicle", vehicle))); + assertTrue(e.getMessage().contains("Vehicle not supported: `" + vehicle + "`. Supported are: `" + supported + "`"), e.getMessage()); + } + } \ No newline at end of file diff --git a/core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java b/core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java index 541a1b4ddfe..7e6660e5859 100644 --- a/core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java @@ -35,13 +35,6 @@ public void duplicateNamesNotAllowed() { assertThrows(IllegalArgumentException.class, () -> EncodingManager.create("car,car")); } - @Test - public void testEncoderAcceptNoException() { - EncodingManager manager = EncodingManager.create("car"); - assertTrue(manager.hasEncoder("car")); - assertFalse(manager.hasEncoder("foot")); - } - @Test public void testSupportFords() { String flagEncoderStrings = "car,bike,foot"; From 3bc3cc91d0f3dedba6ec91bbf2655b51057c41d3 Mon Sep 17 00:00:00 2001 From: easbar Date: Sat, 25 Jun 2022 14:59:50 +0200 Subject: [PATCH 13/28] 28, FlagEncoders no longer 'stored' in EncodingManager --- benchmark/benchmark.sh | 15 ++++--- .../java/com/graphhopper/GraphHopper.java | 18 +------- .../graphhopper/routing/ProfileResolver.java | 15 ++----- .../util/DefaultFlagEncoderFactory.java | 24 ---------- .../routing/util/EncodingManager.java | 44 ++++++------------- .../routing/util/FlagEncoderFactory.java | 4 -- .../java/com/graphhopper/GraphHopperTest.java | 5 +-- .../reader/osm/GraphHopperOSMTest.java | 3 -- .../graphhopper/reader/osm/OSMReaderTest.java | 6 --- .../routing/ch/CHProfileSelectorTest.java | 17 ++++--- .../routing/lm/LMProfileSelectorTest.java | 17 ++++--- .../util/AbstractBikeTagParserTester.java | 2 - .../example/LocationIndexExample.java | 5 +-- .../com/graphhopper/tools/Measurement.java | 22 ++++------ .../java/com/graphhopper/ui/MiniGraphUI.java | 11 +++-- .../graphhopper/resources/InfoResource.java | 8 ++-- 16 files changed, 74 insertions(+), 142 deletions(-) diff --git a/benchmark/benchmark.sh b/benchmark/benchmark.sh index 05904f5952d..2ceabed5a71 100755 --- a/benchmark/benchmark.sh +++ b/benchmark/benchmark.sh @@ -53,7 +53,8 @@ measurement.weighting=fastest \ measurement.ch.node=true \ measurement.ch.edge=true \ measurement.lm=false \ -"graph.flag_encoders=car|turn_costs=true" \ +measurement.vehicle=car \ +measurement.turn_costs=true \ graph.location=${TMP_DIR}measurement-small-gh \ prepare.min_network_size=10000 \ measurement.json=true \ @@ -80,7 +81,8 @@ measurement.ch.edge=false \ measurement.lm=true \ "measurement.lm.active_counts=[4,8,12]" \ measurement.lm.edge_based=true \ -"graph.flag_encoders=car|turn_costs=true" \ +measurement.vehicle=car \ +measurement.turn_costs=true \ graph.location=${TMP_DIR}measurement-big-gh \ prepare.min_network_size=10000 \ measurement.json=true \ @@ -110,7 +112,8 @@ measurement.ch.edge=false \ measurement.lm=true \ "measurement.lm.active_counts=[8]" \ measurement.lm.edge_based=false \ -"graph.flag_encoders=car|turn_costs=true" \ +measurement.vehicle=car \ +measurement.turn_costs=true \ graph.location=${TMP_DIR}measurement-big-little-custom-gh \ prepare.min_network_size=10000 \ measurement.json=true \ @@ -140,7 +143,8 @@ measurement.ch.edge=false \ measurement.lm=true \ "measurement.lm.active_counts=[8]" \ measurement.lm.edge_based=false \ -"graph.flag_encoders=car|turn_costs=true" \ +measurement.vehicle=car \ +measurement.turn_costs=true \ graph.location=${TMP_DIR}measurement-big-very-custom-gh \ prepare.min_network_size=10000 \ measurement.json=true \ @@ -167,7 +171,8 @@ measurement.ch.edge=false \ measurement.lm=true \ "measurement.lm.active_counts=[4,8,12]" \ measurement.lm.edge_based=false \ -"graph.flag_encoders=foot" \ +measurement.vehicle=foot \ +measurement.turn_costs=false \ graph.location=${TMP_DIR}measurement-big-outdoor-gh \ prepare.min_network_size=10000 \ measurement.json=true \ diff --git a/core/src/main/java/com/graphhopper/GraphHopper.java b/core/src/main/java/com/graphhopper/GraphHopper.java index 69423861e91..38f0232ea24 100644 --- a/core/src/main/java/com/graphhopper/GraphHopper.java +++ b/core/src/main/java/com/graphhopper/GraphHopper.java @@ -66,7 +66,6 @@ import java.text.DateFormat; import java.util.*; import java.util.stream.Collectors; -import java.util.stream.Stream; import static com.graphhopper.util.GHUtility.readCountries; import static com.graphhopper.util.Helper.*; @@ -822,7 +821,6 @@ protected void createBaseGraphAndProperties() { properties.put("graph.em.edge_config", encodingManager.toEdgeConfigAsString()); properties.put("graph.em.turn_cost_config", encodingManager.toTurnCostConfigAsString()); properties.put("graph.encoded_values", encodingManager.toEncodedValuesAsString()); - properties.put("graph.flag_encoders", encodingManager.toFlagEncodersAsString()); } private List readCustomAreas() { @@ -947,23 +945,9 @@ private void loadEncodingManagerFromProperties(StorableProperties properties) { throw new IllegalStateException("Duplicate encoded value name: " + encodedValue.getName() + " in: graph.encoded_values=" + encodedValueStr); }); - String flagEncodersStr = properties.get("graph.flag_encoders"); - LinkedHashMap flagEncoders = Stream.of(flagEncodersStr.split(",")) - .map(str -> flagEncoderFactory.deserializeFlagEncoder(str, name -> { - EncodedValue ev = encodedValues.get(name); - if (ev == null) - throw new IllegalStateException("FlagEncoder " + str + " uses unknown encoded value: " + name); - return ev; - })) - .collect(Collectors.toMap(FlagEncoder::getName, f -> (VehicleEncodedValues) f, - (f1, f2) -> { - throw new IllegalStateException("Duplicate flag encoder: " + f1.getName() + " in: " + flagEncodersStr); - }, - LinkedHashMap::new)); - EncodedValue.InitializerConfig edgeConfig = EncodedValueSerializer.deserializeInitializerConfig(properties.get("graph.em.edge_config")); EncodedValue.InitializerConfig turnCostConfig = EncodedValueSerializer.deserializeInitializerConfig(properties.get("graph.em.turn_cost_config")); - encodingManager = new EncodingManager(encodedValues, flagEncoders, edgeConfig, turnCostConfig); + encodingManager = new EncodingManager(encodedValues, edgeConfig, turnCostConfig); } private ArrayNode deserializeEncodedValueList(String encodedValueStr) { diff --git a/core/src/main/java/com/graphhopper/routing/ProfileResolver.java b/core/src/main/java/com/graphhopper/routing/ProfileResolver.java index 86878a769d6..5083b06052b 100644 --- a/core/src/main/java/com/graphhopper/routing/ProfileResolver.java +++ b/core/src/main/java/com/graphhopper/routing/ProfileResolver.java @@ -29,7 +29,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import static com.graphhopper.routing.weighting.Weighting.INFINITE_U_TURN_COSTS; @@ -81,20 +80,12 @@ public Profile resolveProfile(PMap hints) { String vehicle = hints.getString("vehicle", "").toLowerCase(); if (!vehicle.isEmpty()) { - String accessEncName = EncodingManager.getKey(vehicle, "access"); - String speedEncName = EncodingManager.getKey(vehicle, "average_speed"); - if (!encodingManager.hasEncodedValue(accessEncName) || !encodingManager.hasEncodedValue(speedEncName)) { - // we define the supported vehicles as all vehicle names for which there is an access and speed EV - List vehicles = encodingManager.getEncodedValues().stream() - .filter(ev -> ev.getName().endsWith("_access")) - .map(ev -> ev.getName().replaceAll("_access", "")) - .filter(v -> encodingManager.hasEncodedValue(EncodingManager.getKey(v, "average_speed"))) - .collect(Collectors.toList()); - throw new IllegalArgumentException("Vehicle not supported: `" + vehicle + "`. Supported are: `" + vehicles + + List availableVehicles = encodingManager.getVehicles(); + if (!availableVehicles.contains(vehicle)) + throw new IllegalArgumentException("Vehicle not supported: `" + vehicle + "`. Supported are: `" + availableVehicles + "`\nYou should consider using the `profile` parameter instead of specifying a vehicle." + "\nAvailable profiles: " + getProfileNames() + "\nTo learn more about profiles, see: docs/core/profiles.md"); - } } // we select the profile based on the given request hints and the available profiles diff --git a/core/src/main/java/com/graphhopper/routing/util/DefaultFlagEncoderFactory.java b/core/src/main/java/com/graphhopper/routing/util/DefaultFlagEncoderFactory.java index c55c424416a..4bbc8526cdf 100644 --- a/core/src/main/java/com/graphhopper/routing/util/DefaultFlagEncoderFactory.java +++ b/core/src/main/java/com/graphhopper/routing/util/DefaultFlagEncoderFactory.java @@ -17,13 +17,8 @@ */ package com.graphhopper.routing.util; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.EncodedValue; import com.graphhopper.util.PMap; -import java.util.function.Function; - /** * This class creates FlagEncoders that are already included in the GraphHopper distribution. * @@ -67,23 +62,4 @@ public FlagEncoder createFlagEncoder(String name, PMap configuration) { throw new IllegalArgumentException("entry in encoder list not supported: " + name); } - - @Override - public FlagEncoder deserializeFlagEncoder(String serializedFlagEncoder, Function evLookup) { - String[] strings = serializedFlagEncoder.split("\\|"); - if (strings.length != 9) - throw new IllegalStateException("Deserialized FlagEncoders should consist of nine pipe-separated strings"); - String name = strings[0]; - BooleanEncodedValue accessEnc = strings[1].equals("null") ? null : (BooleanEncodedValue) evLookup.apply(strings[1]); - DecimalEncodedValue avgSpeedEnc = strings[2].equals("null") ? null : (DecimalEncodedValue) evLookup.apply(strings[2]); - DecimalEncodedValue priorityEnc = strings[3].equals("null") ? null : (DecimalEncodedValue) evLookup.apply(strings[3]); - DecimalEncodedValue curvatureEnc = strings[4].equals("null") ? null : (DecimalEncodedValue) evLookup.apply(strings[4]); - DecimalEncodedValue turnCostEnc = strings[5].equals("null") ? null : (DecimalEncodedValue) evLookup.apply(strings[5]); - double maxPossibleSpeed = Double.parseDouble(strings[6]); - boolean isMotorVehicle = Boolean.parseBoolean(strings[7]); - boolean isHGV = Boolean.parseBoolean(strings[8]); - return new VehicleEncodedValues( - name, accessEnc, avgSpeedEnc, priorityEnc, curvatureEnc, turnCostEnc, maxPossibleSpeed, isMotorVehicle, isHGV - ); - } } diff --git a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java index e779248092c..1dbb509e387 100644 --- a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java +++ b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java @@ -40,7 +40,6 @@ */ public class EncodingManager implements EncodedValueLookup { private final LinkedHashMap encodedValueMap; - private final LinkedHashMap flagEncoders; private final EncodedValue.InitializerConfig edgeConfig; private final EncodedValue.InitializerConfig turnCostConfig; @@ -57,13 +56,6 @@ public static EncodingManager create(FlagEncoderFactory factory, String flagEnco map(s -> parseEncoderString(factory, s)).collect(Collectors.toList())).build(); } - /** - * Instantiate manager with the given list of encoders. - */ - public static EncodingManager create(FlagEncoder... flagEncoders) { - return EncodingManager.createBuilder(Arrays.asList(flagEncoders)).build(); - } - private static EncodingManager.Builder createBuilder(List flagEncoders) { Builder builder = new Builder(); for (FlagEncoder flagEncoder : flagEncoders) @@ -78,16 +70,14 @@ public static Builder start() { return new Builder(); } - public EncodingManager(LinkedHashMap encodedValueMap, LinkedHashMap flagEncoders, EncodedValue.InitializerConfig edgeConfig, EncodedValue.InitializerConfig turnCostConfig) { - this.flagEncoders = flagEncoders; + public EncodingManager(LinkedHashMap encodedValueMap, EncodedValue.InitializerConfig edgeConfig, EncodedValue.InitializerConfig turnCostConfig) { this.encodedValueMap = encodedValueMap; this.turnCostConfig = turnCostConfig; this.edgeConfig = edgeConfig; - flagEncoders.values().forEach(f -> f.setEncodedValueLookup(this)); } private EncodingManager() { - this(new LinkedHashMap<>(), new LinkedHashMap<>(), new EncodedValue.InitializerConfig(), new EncodedValue.InitializerConfig()); + this(new LinkedHashMap<>(), new EncodedValue.InitializerConfig(), new EncodedValue.InitializerConfig()); } public static class Builder { @@ -95,8 +85,6 @@ public static class Builder { public Builder add(FlagEncoder encoder) { checkNotBuiltAlready(); - if (em.flagEncoders.containsKey(encoder.getName())) - throw new IllegalArgumentException("FlagEncoder already exists: " + encoder.getName()); VehicleEncodedValues v = (VehicleEncodedValues) encoder; v.setEncodedValueLookup(em); List list = new ArrayList<>(); @@ -106,8 +94,6 @@ public Builder add(FlagEncoder encoder) { list = new ArrayList<>(); v.createTurnCostEncodedValues(list); list.forEach(this::addTurnCostEncodedValue); - - em.flagEncoders.put(v.getName(), v); return this; } @@ -160,15 +146,15 @@ private void addDefaultEncodedValues() { if (!em.hasEncodedValue(RoadAccess.KEY)) add(new EnumEncodedValue<>(RoadAccess.KEY, RoadAccess.class)); - for (VehicleEncodedValues encoder : em.flagEncoders.values()) { - if (encoder.getName().contains("bike") || encoder.getName().contains("mtb")) { + for (String vehicle : em.getVehicles()) { + if (vehicle.contains("bike") || vehicle.contains("mtb")) { if (!em.hasEncodedValue(BikeNetwork.KEY)) add(new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class)); if (!em.hasEncodedValue(GetOffBike.KEY)) add(GetOffBike.create()); if (!em.hasEncodedValue(Smoothness.KEY)) add(new EnumEncodedValue<>(Smoothness.KEY, Smoothness.class)); - } else if (encoder.getName().contains("foot") || encoder.getName().contains("hike") || encoder.getName().contains("wheelchair")) { + } else if (vehicle.contains("foot") || vehicle.contains("hike") || vehicle.contains("wheelchair")) { if (!em.hasEncodedValue(FootNetwork.KEY)) add(new EnumEncodedValue<>(FootNetwork.KEY, RouteNetwork.class)); } @@ -201,8 +187,13 @@ public boolean hasEncodedValue(String key) { return encodedValueMap.get(key) != null; } - public String toFlagEncodersAsString() { - return flagEncoders.values().stream().map(VehicleEncodedValues::toSerializationString).collect(Collectors.joining(",")); + public List getVehicles() { + // the supported vehicles are all those prefixes for which there is an access and speed EV + return getEncodedValues().stream() + .filter(ev -> ev.getName().endsWith("_access")) + .map(ev -> ev.getName().replaceAll("_access", "")) + .filter(v -> hasEncodedValue(EncodingManager.getKey(v, "average_speed"))) + .collect(Collectors.toList()); } public String toEncodedValuesAsString() { @@ -224,7 +215,7 @@ public String toTurnCostConfigAsString() { @Override public String toString() { - return flagEncoders.values().stream().map(Object::toString).collect(Collectors.joining(",")); + return String.join(",", getVehicles()); } // TODO hide IntsRef even more in a later version: https://gist.github.com/karussell/f4c2b2b1191be978d7ee9ec8dd2cd48f @@ -237,15 +228,8 @@ public IntsRef createRelationFlags() { return new IntsRef(2); } - public List fetchEdgeEncoders() { - return new ArrayList<>(flagEncoders.values()); - } - public boolean needsTurnCostsSupport() { - for (FlagEncoder encoder : flagEncoders.values()) - if (encoder.supportsTurnCosts()) - return true; - return false; + return turnCostConfig.getRequiredBits() > 0; } @Override diff --git a/core/src/main/java/com/graphhopper/routing/util/FlagEncoderFactory.java b/core/src/main/java/com/graphhopper/routing/util/FlagEncoderFactory.java index 6131a6b1405..48df5dfeec0 100644 --- a/core/src/main/java/com/graphhopper/routing/util/FlagEncoderFactory.java +++ b/core/src/main/java/com/graphhopper/routing/util/FlagEncoderFactory.java @@ -17,11 +17,8 @@ */ package com.graphhopper.routing.util; -import com.graphhopper.routing.ev.EncodedValue; import com.graphhopper.util.PMap; -import java.util.function.Function; - /** * @author Peter Karich */ @@ -40,5 +37,4 @@ public interface FlagEncoderFactory { FlagEncoder createFlagEncoder(String name, PMap configuration); - FlagEncoder deserializeFlagEncoder(String serializedFlagEncoder, Function evLookup); } diff --git a/core/src/test/java/com/graphhopper/GraphHopperTest.java b/core/src/test/java/com/graphhopper/GraphHopperTest.java index 5c2b70a486c..ec902098ff9 100644 --- a/core/src/test/java/com/graphhopper/GraphHopperTest.java +++ b/core/src/test/java/com/graphhopper/GraphHopperTest.java @@ -1337,18 +1337,17 @@ public void testMultipleVehiclesWithCH() { new CHProfile(profile2) ); hopper.importOrLoad(); - String str = hopper.getEncodingManager().toString(); GHResponse rsp = hopper.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826) .setProfile("profile2")); ResponsePath res = rsp.getBest(); - assertFalse(rsp.hasErrors(), "car routing for " + str + " should not have errors:" + rsp.getErrors()); + assertFalse(rsp.hasErrors(), rsp.getErrors().toString()); assertEquals(207, res.getTime() / 1000f, 1); assertEquals(2837, res.getDistance(), 1); rsp = hopper.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826) .setProfile("profile1")); res = rsp.getBest(); - assertFalse(rsp.hasErrors(), "bike routing for " + str + " should not have errors:" + rsp.getErrors()); + assertFalse(rsp.hasErrors(), rsp.getErrors().toString()); assertEquals(511, res.getTime() / 1000f, 1); assertEquals(2481, res.getDistance(), 1); diff --git a/core/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java b/core/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java index 903084643ea..65d8c430015 100644 --- a/core/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java +++ b/core/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java @@ -26,7 +26,6 @@ import com.graphhopper.routing.ev.EncodedValue; import com.graphhopper.routing.lm.LandmarkStorage; import com.graphhopper.routing.util.EdgeFilter; -import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.weighting.custom.CustomProfile; import com.graphhopper.storage.*; import com.graphhopper.storage.index.LocationIndexTree; @@ -515,7 +514,6 @@ public void testNothingHappensWhenFlagEncodersAreChangedForLoad() { setGraphHopperLocation(ghLoc); instance.load(); assertEquals(5, instance.getBaseGraph().getNodes()); - assertEquals("foot,car", instance.getEncodingManager().fetchEdgeEncoders().stream().map(FlagEncoder::getName).collect(Collectors.joining(","))); assertEquals("foot_access,foot_average_speed,foot_priority,car_access,car_average_speed,foot_subnetwork,car_subnetwork,roundabout,road_class,road_class_link,road_environment,max_speed,road_access,foot_network", instance.getEncodingManager().getEncodedValues().stream().map(EncodedValue::getName).collect(Collectors.joining(","))); } @@ -554,7 +552,6 @@ public void testFailsForWrongEVConfig() { setOSMFile(testOsm3); instance.load(); assertEquals(5, instance.getBaseGraph().getNodes()); - assertEquals("foot,car", instance.getEncodingManager().fetchEdgeEncoders().stream().map(FlagEncoder::getName).collect(Collectors.joining(","))); assertEquals("foot_access,foot_average_speed,foot_priority,car_access,car_average_speed,foot_subnetwork,car_subnetwork,roundabout,road_class,road_class_link,road_environment,max_speed,road_access,foot_network", instance.getEncodingManager().getEncodedValues().stream().map(EncodedValue::getName).collect(Collectors.joining(","))); } diff --git a/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java b/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java index 800b580081c..40da5df9a06 100644 --- a/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java +++ b/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java @@ -46,7 +46,6 @@ import java.io.IOException; import java.util.Collections; import java.util.List; -import java.util.function.Function; import static com.graphhopper.util.GHUtility.readCountries; import static org.junit.jupiter.api.Assertions.*; @@ -676,11 +675,6 @@ public FlagEncoder createFlagEncoder(String name, PMap config) { return new DefaultFlagEncoderFactory().createFlagEncoder(name, config); } } - - @Override - public FlagEncoder deserializeFlagEncoder(String serializedFlagEncoder, Function evLookup) { - return null; - } }); hopper.setVehicleTagParserFactory((lookup, name, config) -> { if (name.equals("truck")) { diff --git a/core/src/test/java/com/graphhopper/routing/ch/CHProfileSelectorTest.java b/core/src/test/java/com/graphhopper/routing/ch/CHProfileSelectorTest.java index e32d2d269ef..1c41d66bf56 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/CHProfileSelectorTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/CHProfileSelectorTest.java @@ -22,9 +22,11 @@ import com.graphhopper.config.LMProfile; import com.graphhopper.config.Profile; import com.graphhopper.routing.ProfileResolver; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.util.PMap; import com.graphhopper.util.Parameters; import org.junit.jupiter.api.BeforeEach; @@ -55,9 +57,14 @@ public class CHProfileSelectorTest { @BeforeEach public void setup() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - FlagEncoder bikeEncoder = FlagEncoders.createBike(); - encodingManager = EncodingManager.create(carEncoder, bikeEncoder); + BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); + DecimalEncodedValue bikeSpeedEnc = new DecimalEncodedValueImpl("bike_average_speed", 4, 2, false); + encodingManager = EncodingManager.start() + .add(carAccessEnc).add(carSpeedEnc) + .add(bikeAccessEnc).add(bikeSpeedEnc) + .build(); fastCar = new Profile("fast_car").setWeighting("fastest").setVehicle("car").setTurnCosts(false); fastCarEdge = new Profile("fast_car_edge").setWeighting("fastest").setVehicle("car").setTurnCosts(true); fastCarEdge10 = new Profile("fast_car_edge10").setWeighting("fastest").setVehicle("car").setTurnCosts(true).putHint(Parameters.Routing.U_TURN_COSTS, 10); diff --git a/core/src/test/java/com/graphhopper/routing/lm/LMProfileSelectorTest.java b/core/src/test/java/com/graphhopper/routing/lm/LMProfileSelectorTest.java index 903500f4047..5dc9736e4cf 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/LMProfileSelectorTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/LMProfileSelectorTest.java @@ -22,9 +22,11 @@ import com.graphhopper.config.LMProfile; import com.graphhopper.config.Profile; import com.graphhopper.routing.ProfileResolver; +import com.graphhopper.routing.ev.BooleanEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.DecimalEncodedValueImpl; +import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.util.PMap; import com.graphhopper.util.Parameters; import org.junit.jupiter.api.BeforeEach; @@ -50,9 +52,14 @@ public class LMProfileSelectorTest { @BeforeEach public void setup() { - FlagEncoder carEncoder = FlagEncoders.createCar(); - FlagEncoder bikeEncoder = FlagEncoders.createBike(); - encodingManager = EncodingManager.create(carEncoder, bikeEncoder); + BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); + DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); + DecimalEncodedValue bikeSpeedEnc = new DecimalEncodedValueImpl("bike_average_speed", 4, 2, false); + encodingManager = EncodingManager.start() + .add(carAccessEnc).add(carSpeedEnc) + .add(bikeAccessEnc).add(bikeSpeedEnc) + .build(); fastCar = new Profile("fast_car").setVehicle("car").setWeighting("fastest").setTurnCosts(false); fastCarEdge = new Profile("fast_car_edge").setVehicle("car").setWeighting("fastest").setTurnCosts(true); fastBike = new Profile("fast_bike").setVehicle("bike").setWeighting("fastest").setTurnCosts(false); diff --git a/core/src/test/java/com/graphhopper/routing/util/AbstractBikeTagParserTester.java b/core/src/test/java/com/graphhopper/routing/util/AbstractBikeTagParserTester.java index 35a6d243c68..347a8773a1a 100644 --- a/core/src/test/java/com/graphhopper/routing/util/AbstractBikeTagParserTester.java +++ b/core/src/test/java/com/graphhopper/routing/util/AbstractBikeTagParserTester.java @@ -48,8 +48,6 @@ public abstract class AbstractBikeTagParserTester { @BeforeEach public void setUp() { encodingManager = createEncodingManager(); - if (encodingManager.fetchEdgeEncoders().size() > 1) - fail("currently we assume there is only one encoder per test"); parser = createBikeTagParser(encodingManager, new PMap("block_fords=true")); osmParsers = createOSMParsers(parser, encodingManager); roundaboutEnc = encodingManager.getBooleanEncodedValue(Roundabout.KEY); diff --git a/example/src/main/java/com/graphhopper/example/LocationIndexExample.java b/example/src/main/java/com/graphhopper/example/LocationIndexExample.java index e7a2d9f1a6c..9de0a2ac8a1 100644 --- a/example/src/main/java/com/graphhopper/example/LocationIndexExample.java +++ b/example/src/main/java/com/graphhopper/example/LocationIndexExample.java @@ -3,8 +3,6 @@ import com.graphhopper.GraphHopper; import com.graphhopper.config.Profile; import com.graphhopper.routing.util.EdgeFilter; -import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoders; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.storage.index.LocationIndexTree; @@ -35,7 +33,8 @@ public static void graphhopperLocationIndex(String relDir) { public static void lowLevelLocationIndex() { // If you don't use the GraphHopper class you have to use the low level API: - BaseGraph graph = new BaseGraph.Builder(EncodingManager.create(FlagEncoders.createCar())).create(); + + BaseGraph graph = new BaseGraph.Builder(1).create(); graph.edge(0, 1).setName("test edge"); graph.getNodeAccess().setNode(0, 12, 42); graph.getNodeAccess().setNode(1, 12.01, 42.01); diff --git a/tools/src/main/java/com/graphhopper/tools/Measurement.java b/tools/src/main/java/com/graphhopper/tools/Measurement.java index 5333a9e15ec..9afd72e8eba 100644 --- a/tools/src/main/java/com/graphhopper/tools/Measurement.java +++ b/tools/src/main/java/com/graphhopper/tools/Measurement.java @@ -30,6 +30,7 @@ import com.graphhopper.routing.ch.PrepareContractionHierarchies; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.Subnetwork; +import com.graphhopper.routing.ev.TurnCost; import com.graphhopper.routing.lm.LMConfig; import com.graphhopper.routing.lm.PrepareLandmarks; import com.graphhopper.routing.util.*; @@ -178,10 +179,8 @@ protected void importOSM() { BaseGraph g = hopper.getBaseGraph(); EncodingManager encodingManager = hopper.getEncodingManager(); - if (encodingManager.fetchEdgeEncoders().size() != 1) { - throw new IllegalArgumentException("There has to be exactly one encoder for each measurement"); - } - FlagEncoder encoder = encodingManager.fetchEdgeEncoders().get(0); + BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(EncodingManager.getKey(vehicle, "access")); + boolean withTurnCosts = encodingManager.hasEncodedValue(TurnCost.key(vehicle)); StopWatch sw = new StopWatch().start(); try { @@ -189,7 +188,7 @@ protected void importOSM() { final boolean runSlow = args.getBool("measurement.run_slow_routing", true); printGraphDetails(g, vehicle); - measureGraphTraversal(g, encoder.getAccessEnc(), count * 100); + measureGraphTraversal(g, accessEnc, count * 100); measureLocationIndex(g, hopper.getLocationIndex(), count); if (runSlow) { @@ -199,7 +198,7 @@ protected void importOSM() { withInstructions()); measureRouting(hopper, new QuerySettings("routing_alt", count / 500, isCH, isLM). alternative()); - if (encoder.supportsTurnCosts()) { + if (withTurnCosts) { measureRouting(hopper, new QuerySettings("routing_edge", count / 20, isCH, isLM). withInstructions().edgeBased()); // unfortunately alt routes are so slow that we cannot really afford many iterations @@ -222,7 +221,7 @@ protected void importOSM() { withInstructions().activeLandmarks(activeLMCount)); measureRouting(hopper, new QuerySettings("routingLM" + activeLMCount + "_alt", count / 500, isCH, isLM). activeLandmarks(activeLMCount).alternative()); - if (args.getBool("measurement.lm.edge_based", encoder.supportsTurnCosts())) { + if (args.getBool("measurement.lm.edge_based", withTurnCosts)) { measureRouting(hopper, new QuerySettings("routingLM" + activeLMCount + "_edge", count / 20, isCH, isLM). withInstructions().activeLandmarks(activeLMCount).edgeBased()); measureRouting(hopper, new QuerySettings("routingLM" + activeLMCount + "_alt_edge", count / 500, isCH, isLM). @@ -308,13 +307,8 @@ protected void importOSM() { private GraphHopperConfig createConfigFromArgs(PMap args) { GraphHopperConfig ghConfig = new GraphHopperConfig(args); - String encodingManagerString = args.getString("graph.flag_encoders", "car"); - List tmpEncoders = EncodingManager.create(encodingManagerString).fetchEdgeEncoders(); - if (tmpEncoders.size() != 1) { - logger.warn("You configured multiple encoders, only the first one is used for the measurements"); - } - vehicle = tmpEncoders.get(0).toString(); - boolean turnCosts = tmpEncoders.get(0).supportsTurnCosts(); + vehicle = args.getString("measurement.vehicle", "car"); + boolean turnCosts = args.getBool("measurement.turn_costs", false); int uTurnCosts = args.getInt("measurement.u_turn_costs", 40); String weighting = args.getString("measurement.weighting", "fastest"); boolean useCHEdge = args.getBool("measurement.ch.edge", true); diff --git a/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java b/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java index 3da5adb1e4b..4fc678efab8 100644 --- a/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java +++ b/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java @@ -34,7 +34,7 @@ import com.graphhopper.routing.querygraph.QueryRoutingCHGraph; import com.graphhopper.routing.util.AllEdgesIterator; import com.graphhopper.routing.util.EdgeFilter; -import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.storage.BaseGraph; @@ -75,7 +75,6 @@ public class MiniGraphUI { private final BaseGraph graph; private final NodeAccess na; private final MapLayer pathLayer; - private final FlagEncoder encoder; private final DecimalEncodedValue avSpeedEnc; private final BooleanEncodedValue accessEnc; private final boolean useCH; @@ -120,9 +119,9 @@ public static void main(String[] strs) { public MiniGraphUI(GraphHopper hopper, boolean debug, boolean useCH) { this.graph = hopper.getBaseGraph(); this.na = graph.getNodeAccess(); - encoder = hopper.getEncodingManager().fetchEdgeEncoders().get(0); - avSpeedEnc = encoder.getAverageSpeedEnc(); - accessEnc = encoder.getAccessEnc(); + String vehicle = hopper.getProfiles().get(0).getVehicle(); + avSpeedEnc = hopper.getEncodingManager().getDecimalEncodedValue(EncodingManager.getKey(vehicle, "average_speed")); + accessEnc = hopper.getEncodingManager().getBooleanEncodedValue(EncodingManager.getKey(vehicle, "access")); this.useCH = useCH; logger.info("locations:" + graph.getNodes() + ", debug:" + debug); @@ -333,7 +332,7 @@ private RoutingAlgorithm createAlgo(GraphHopper hopper, QueryGraph qGraph) { }; AlgorithmOptions algoOpts = new AlgorithmOptions().setAlgorithm(Algorithms.ASTAR_BI). setTraversalMode(TraversalMode.EDGE_BASED); - return algoFactory.createAlgo(qGraph, new FastestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc()), algoOpts); + return algoFactory.createAlgo(qGraph, new FastestWeighting(accessEnc, avSpeedEnc), algoOpts); } } diff --git a/web-bundle/src/main/java/com/graphhopper/resources/InfoResource.java b/web-bundle/src/main/java/com/graphhopper/resources/InfoResource.java index 58564c0151a..448881366f5 100644 --- a/web-bundle/src/main/java/com/graphhopper/resources/InfoResource.java +++ b/web-bundle/src/main/java/com/graphhopper/resources/InfoResource.java @@ -33,7 +33,10 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; -import java.util.*; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; /** * @author Peter Karich @@ -93,8 +96,7 @@ public Info getInfo() { info.profiles.add(new Info.ProfileData("pt", "pt")); info.elevation = hasElevation; - List encoderNames = Arrays.asList(encodingManager.toString().split(",")); - info.supported_vehicles = new ArrayList<>(encoderNames); + info.supported_vehicles = encodingManager.getVehicles(); if (config.has("gtfs.file")) { info.supported_vehicles.add("pt"); } From d11fdbd80a71bd095f4b72e7c64ed6693bc556b6 Mon Sep 17 00:00:00 2001 From: easbar Date: Sat, 25 Jun 2022 15:30:26 +0200 Subject: [PATCH 14/28] 0, we still use VehicleEncodedValues to collectively add EVs to the EM, but FlagEncoder is gone --- .../java/com/graphhopper/GraphHopper.java | 8 +- ...> DefaultVehicleEncodedValuesFactory.java} | 30 ++--- .../util/DefaultVehicleTagParserFactory.java | 2 +- .../routing/util/EncodingManager.java | 32 ++--- .../graphhopper/routing/util/FlagEncoder.java | 66 ---------- .../routing/util/FlagEncoders.java | 85 ------------ .../routing/util/VehicleEncodedValues.java | 121 ++---------------- ....java => VehicleEncodedValuesFactory.java} | 4 +- .../java/com/graphhopper/GraphHopperTest.java | 4 +- .../graphhopper/reader/osm/OSMReaderTest.java | 15 +-- .../graphhopper/example/RoutingExampleTC.java | 5 +- 11 files changed, 52 insertions(+), 320 deletions(-) rename core/src/main/java/com/graphhopper/routing/util/{DefaultFlagEncoderFactory.java => DefaultVehicleEncodedValuesFactory.java} (56%) delete mode 100644 core/src/main/java/com/graphhopper/routing/util/FlagEncoder.java delete mode 100644 core/src/main/java/com/graphhopper/routing/util/FlagEncoders.java rename core/src/main/java/com/graphhopper/routing/util/{FlagEncoderFactory.java => VehicleEncodedValuesFactory.java} (90%) diff --git a/core/src/main/java/com/graphhopper/GraphHopper.java b/core/src/main/java/com/graphhopper/GraphHopper.java index 38f0232ea24..2a5ea27f037 100644 --- a/core/src/main/java/com/graphhopper/GraphHopper.java +++ b/core/src/main/java/com/graphhopper/GraphHopper.java @@ -120,7 +120,7 @@ public class GraphHopper { // for data reader private String osmFile; private ElevationProvider eleProvider = ElevationProvider.NOOP; - private FlagEncoderFactory flagEncoderFactory = new DefaultFlagEncoderFactory(); + private VehicleEncodedValuesFactory vehicleEncodedValuesFactory = new DefaultVehicleEncodedValuesFactory(); private VehicleTagParserFactory vehicleTagParserFactory = new DefaultVehicleTagParserFactory(); private EncodedValueFactory encodedValueFactory = new DefaultEncodedValueFactory(); private TagParserFactory tagParserFactory = new DefaultTagParserFactory(); @@ -389,8 +389,8 @@ public TranslationMap getTranslationMap() { return trMap; } - public GraphHopper setFlagEncoderFactory(FlagEncoderFactory factory) { - this.flagEncoderFactory = factory; + public GraphHopper setVehicleEncodedValuesFactory(VehicleEncodedValuesFactory factory) { + this.vehicleEncodedValuesFactory = factory; return this; } @@ -579,7 +579,7 @@ private void buildEncodingManagerAndOSMParsers(String flagEncodersStr, String en .collect(Collectors.toList()); EncodingManager.Builder emBuilder = new EncodingManager.Builder(); - flagEncodersMap.forEach((name, encoderStr) -> emBuilder.add(flagEncoderFactory.createFlagEncoder(name, new PMap(encoderStr)))); + flagEncodersMap.forEach((name, encoderStr) -> emBuilder.add(vehicleEncodedValuesFactory.createVehicleEncodedValues(name, new PMap(encoderStr)))); profiles.forEach(profile -> emBuilder.add(Subnetwork.create(profile.getName()))); encodedValueStrings.forEach(s -> emBuilder.add(encodedValueFactory.create(s))); encodingManager = emBuilder.build(); diff --git a/core/src/main/java/com/graphhopper/routing/util/DefaultFlagEncoderFactory.java b/core/src/main/java/com/graphhopper/routing/util/DefaultVehicleEncodedValuesFactory.java similarity index 56% rename from core/src/main/java/com/graphhopper/routing/util/DefaultFlagEncoderFactory.java rename to core/src/main/java/com/graphhopper/routing/util/DefaultVehicleEncodedValuesFactory.java index 4bbc8526cdf..394d38fb64c 100644 --- a/core/src/main/java/com/graphhopper/routing/util/DefaultFlagEncoderFactory.java +++ b/core/src/main/java/com/graphhopper/routing/util/DefaultVehicleEncodedValuesFactory.java @@ -20,46 +20,46 @@ import com.graphhopper.util.PMap; /** - * This class creates FlagEncoders that are already included in the GraphHopper distribution. + * This class creates vehicle encoded values that are already included in the GraphHopper distribution. * * @author Peter Karich */ -public class DefaultFlagEncoderFactory implements FlagEncoderFactory { +public class DefaultVehicleEncodedValuesFactory implements VehicleEncodedValuesFactory { @Override - public FlagEncoder createFlagEncoder(String name, PMap configuration) { + public VehicleEncodedValues createVehicleEncodedValues(String name, PMap configuration) { if (name.equals(ROADS)) - return FlagEncoders.createRoads(configuration); + return VehicleEncodedValues.roads(); if (name.equals(CAR)) - return FlagEncoders.createCar(configuration); + return VehicleEncodedValues.car(configuration); if (name.equals(CAR4WD)) - return FlagEncoders.createCar4wd(configuration); + return VehicleEncodedValues.car4wd(configuration); if (name.equals(BIKE)) - return FlagEncoders.createBike(configuration); + return VehicleEncodedValues.bike(configuration); if (name.equals(BIKE2)) - return FlagEncoders.createBike2(configuration); + return VehicleEncodedValues.bike2(configuration); if (name.equals(RACINGBIKE)) - return FlagEncoders.createRacingBike(configuration); + return VehicleEncodedValues.racingbike(configuration); if (name.equals(MOUNTAINBIKE)) - return FlagEncoders.createMountainBike(configuration); + return VehicleEncodedValues.mountainbike(configuration); if (name.equals(FOOT)) - return FlagEncoders.createFoot(configuration); + return VehicleEncodedValues.foot(configuration); if (name.equals(HIKE)) - return FlagEncoders.createHike(configuration); + return VehicleEncodedValues.hike(configuration); if (name.equals(MOTORCYCLE)) - return FlagEncoders.createMotorcycle(configuration); + return VehicleEncodedValues.motorcycle(configuration); if (name.equals(WHEELCHAIR)) - return FlagEncoders.createWheelchair(configuration); + return VehicleEncodedValues.wheelchair(configuration); - throw new IllegalArgumentException("entry in encoder list not supported: " + name); + throw new IllegalArgumentException("entry in vehicle list not supported: " + name); } } diff --git a/core/src/main/java/com/graphhopper/routing/util/DefaultVehicleTagParserFactory.java b/core/src/main/java/com/graphhopper/routing/util/DefaultVehicleTagParserFactory.java index 07a2da84ced..4a395fbd09c 100644 --- a/core/src/main/java/com/graphhopper/routing/util/DefaultVehicleTagParserFactory.java +++ b/core/src/main/java/com/graphhopper/routing/util/DefaultVehicleTagParserFactory.java @@ -21,7 +21,7 @@ import com.graphhopper.routing.ev.EncodedValueLookup; import com.graphhopper.util.PMap; -import static com.graphhopper.routing.util.FlagEncoderFactory.*; +import static com.graphhopper.routing.util.VehicleEncodedValuesFactory.*; public class DefaultVehicleTagParserFactory implements VehicleTagParserFactory { public VehicleTagParser createParser(EncodedValueLookup lookup, String name, PMap configuration) { diff --git a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java index 1dbb509e387..b80a5c41fa5 100644 --- a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java +++ b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java @@ -45,21 +45,21 @@ public class EncodingManager implements EncodedValueLookup { /** * Instantiate manager with the given list of encoders. The manager knows several default - * encoders using DefaultFlagEncoderFactory. + * encoders using DefaultVehicleEncodedValuesFactory. */ public static EncodingManager create(String flagEncodersStr) { - return create(new DefaultFlagEncoderFactory(), flagEncodersStr); + return create(new DefaultVehicleEncodedValuesFactory(), flagEncodersStr); } - public static EncodingManager create(FlagEncoderFactory factory, String flagEncodersStr) { + public static EncodingManager create(VehicleEncodedValuesFactory factory, String flagEncodersStr) { return createBuilder(Arrays.stream(flagEncodersStr.split(",")).filter(s -> !s.trim().isEmpty()). map(s -> parseEncoderString(factory, s)).collect(Collectors.toList())).build(); } - private static EncodingManager.Builder createBuilder(List flagEncoders) { + private static EncodingManager.Builder createBuilder(List vehicleEncodedValues) { Builder builder = new Builder(); - for (FlagEncoder flagEncoder : flagEncoders) - builder.add(flagEncoder); + for (VehicleEncodedValues v : vehicleEncodedValues) + builder.add(v); return builder; } @@ -83,16 +83,14 @@ private EncodingManager() { public static class Builder { private EncodingManager em = new EncodingManager(); - public Builder add(FlagEncoder encoder) { + public Builder add(VehicleEncodedValues vehicleEncodedValues) { checkNotBuiltAlready(); - VehicleEncodedValues v = (VehicleEncodedValues) encoder; - v.setEncodedValueLookup(em); List list = new ArrayList<>(); - v.createEncodedValues(list); + vehicleEncodedValues.createEncodedValues(list); list.forEach(this::add); list = new ArrayList<>(); - v.createTurnCostEncodedValues(list); + vehicleEncodedValues.createTurnCostEncodedValues(list); list.forEach(this::addTurnCostEncodedValue); return this; } @@ -162,13 +160,13 @@ private void addDefaultEncodedValues() { } } - static FlagEncoder parseEncoderString(FlagEncoderFactory factory, String encoderString) { + static VehicleEncodedValues parseEncoderString(VehicleEncodedValuesFactory factory, String encoderString) { if (!encoderString.equals(toLowerCase(encoderString))) - throw new IllegalArgumentException("An upper case name for the FlagEncoder is not allowed: " + encoderString); + throw new IllegalArgumentException("An upper case name for the vehicle is not allowed: " + encoderString); encoderString = encoderString.trim(); if (encoderString.isEmpty()) - throw new IllegalArgumentException("FlagEncoder cannot be empty. " + encoderString); + throw new IllegalArgumentException("vehicle cannot be empty. " + encoderString); String entryVal = ""; if (encoderString.contains("|")) { @@ -176,7 +174,7 @@ static FlagEncoder parseEncoderString(FlagEncoderFactory factory, String encoder encoderString = encoderString.split("\\|")[0]; } PMap configuration = new PMap(entryVal); - return factory.createFlagEncoder(encoderString, configuration); + return factory.createVehicleEncodedValues(encoderString, configuration); } public int getIntsForFlags() { @@ -272,10 +270,6 @@ public T getEncodedValue(String key, Class encodedVa return (T) ev; } - /** - * All EncodedValue names that are created from a FlagEncoder should use this method to mark them as - * "none-shared" across the other FlagEncoders. - */ public static String getKey(String prefix, String str) { return prefix + "_" + str; } diff --git a/core/src/main/java/com/graphhopper/routing/util/FlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/FlagEncoder.java deleted file mode 100644 index 4d1de1f65b2..00000000000 --- a/core/src/main/java/com/graphhopper/routing/util/FlagEncoder.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to GraphHopper GmbH under one or more contributor - * license agreements. See the NOTICE file distributed with this work for - * additional information regarding copyright ownership. - * - * GraphHopper GmbH licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.graphhopper.routing.util; - -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.EncodedValueLookup; - -/** - * This class provides methods to define how a value (like speed or direction) converts to a flag - * (currently an integer value), which is stored in an edge. - * - * @author Peter Karich - */ -public interface FlagEncoder extends EncodedValueLookup { - - String getName(); - - boolean isMotorVehicle(); - - boolean isHGV(); - - /** - * @return the maximum speed in km/h - */ - double getMaxSpeed(); - - /** - * This method returns the EncodedValue used for the direction-dependent access properties of this encoder. - */ - BooleanEncodedValue getAccessEnc(); - - /** - * This method returns the EncodedValue used for the average speed of this encoder. - */ - DecimalEncodedValue getAverageSpeedEnc(); - - DecimalEncodedValue getPriorityEnc(); - - DecimalEncodedValue getCurvatureEnc(); - - DecimalEncodedValue getTurnCostEnc(); - - boolean supportsTurnCosts(); - - - /** - * @return true if already registered in an EncodingManager - */ - boolean isRegistered(); -} diff --git a/core/src/main/java/com/graphhopper/routing/util/FlagEncoders.java b/core/src/main/java/com/graphhopper/routing/util/FlagEncoders.java deleted file mode 100644 index 4437e9f1056..00000000000 --- a/core/src/main/java/com/graphhopper/routing/util/FlagEncoders.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.graphhopper.routing.util; - -import com.graphhopper.util.PMap; - -public class FlagEncoders { - public static FlagEncoder createFoot() { - return createFoot(new PMap()); - } - - public static FlagEncoder createFoot(PMap properties) { - return VehicleEncodedValues.foot(properties); - } - - public static FlagEncoder createHike() { - return createHike(new PMap()); - } - - public static FlagEncoder createHike(PMap properties) { - return VehicleEncodedValues.hike(properties); - } - - public static FlagEncoder createWheelchair() { - return createWheelchair(new PMap()); - } - - public static FlagEncoder createWheelchair(PMap properties) { - return VehicleEncodedValues.wheelchair(properties); - } - - public static FlagEncoder createCar() { - return createCar(new PMap()); - } - - public static FlagEncoder createCar(PMap properties) { - return VehicleEncodedValues.car(properties); - } - - public static FlagEncoder createMotorcycle() { - return createMotorcycle(new PMap()); - } - - public static FlagEncoder createMotorcycle(PMap properties) { - return VehicleEncodedValues.motorcycle(properties); - } - - public static FlagEncoder createCar4wd(PMap properties) { - return VehicleEncodedValues.car4wd(properties); - } - - public static FlagEncoder createRacingBike() { - return createRacingBike(new PMap()); - } - - public static FlagEncoder createRacingBike(PMap properties) { - return VehicleEncodedValues.racingbike(properties); - } - - public static FlagEncoder createBike() { - return createBike(new PMap()); - } - - public static FlagEncoder createBike(PMap properties) { - return VehicleEncodedValues.bike(properties); - } - - public static FlagEncoder createBike2() { - return createBike2(new PMap()); - } - - public static FlagEncoder createBike2(PMap properties) { - return VehicleEncodedValues.bike2(properties); - } - - public static FlagEncoder createMountainBike() { - return createMountainBike(new PMap()); - } - - public static FlagEncoder createMountainBike(PMap properties) { - return VehicleEncodedValues.mountainbike(properties); - } - - public static FlagEncoder createRoads(PMap properties) { - return VehicleEncodedValues.roads(properties); - } -} diff --git a/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValues.java b/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValues.java index a8e264289c9..15483c3674e 100644 --- a/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValues.java +++ b/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValues.java @@ -22,27 +22,16 @@ import com.graphhopper.util.PMap; import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import static com.graphhopper.routing.util.BikeCommonTagParser.BIKE_MAX_SPEED; -import static com.graphhopper.routing.util.CarTagParser.CAR_MAX_SPEED; import static com.graphhopper.routing.util.EncodingManager.getKey; -import static com.graphhopper.routing.util.FootTagParser.FERRY_SPEED; -import static com.graphhopper.routing.util.MotorcycleTagParser.MOTOR_CYCLE_MAX_SPEED; -import static com.graphhopper.routing.util.RoadsTagParser.ROADS_MAX_SPEED; -public class VehicleEncodedValues implements FlagEncoder { +public class VehicleEncodedValues { private final String name; - private final boolean isMotorVehicle; - private final boolean isHGV; - private final double maxPossibleSpeed; private final BooleanEncodedValue accessEnc; private final DecimalEncodedValue avgSpeedEnc; private final DecimalEncodedValue priorityEnc; private final DecimalEncodedValue curvatureEnc; private final DecimalEncodedValue turnCostEnc; - private EncodedValueLookup encodedValueLookup; public static VehicleEncodedValues foot(PMap properties) { String name = properties.getString("name", "foot"); @@ -50,12 +39,11 @@ public static VehicleEncodedValues foot(PMap properties) { double speedFactor = properties.getDouble("speed_factor", 1); boolean speedTwoDirections = properties.getBool("speed_two_directions", false); int maxTurnCosts = properties.getInt("max_turn_costs", properties.getBool("turn_costs", false) ? 1 : 0); - double maxSpeed = properties.getDouble("max_speed", FERRY_SPEED); BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue(getKey(name, "access"), true); DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl(getKey(name, "average_speed"), speedBits, speedFactor, speedTwoDirections); DecimalEncodedValue priorityEnc = new DecimalEncodedValueImpl(getKey(name, "priority"), 4, PriorityCode.getFactor(1), false); DecimalEncodedValue turnCostEnc = maxTurnCosts > 0 ? TurnCost.create(name, maxTurnCosts) : null; - return new VehicleEncodedValues(name, accessEnc, speedEnc, priorityEnc, null, turnCostEnc, speedEnc.getNextStorableValue(maxSpeed), false, false); + return new VehicleEncodedValues(name, accessEnc, speedEnc, priorityEnc, null, turnCostEnc); } public static VehicleEncodedValues hike(PMap properties) { @@ -77,12 +65,11 @@ public static VehicleEncodedValues bike(PMap properties) { double speedFactor = properties.getDouble("speed_factor", 2); boolean speedTwoDirections = properties.getBool("speed_two_directions", false); int maxTurnCosts = properties.getInt("max_turn_costs", properties.getBool("turn_costs", false) ? 1 : 0); - double maxSpeed = properties.getDouble("max_speed", BIKE_MAX_SPEED); BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue(getKey(name, "access"), true); DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl(getKey(name, "average_speed"), speedBits, speedFactor, speedTwoDirections); DecimalEncodedValue priorityEnc = new DecimalEncodedValueImpl(getKey(name, "priority"), 4, PriorityCode.getFactor(1), false); DecimalEncodedValue turnCostEnc = maxTurnCosts > 0 ? TurnCost.create(name, maxTurnCosts) : null; - return new VehicleEncodedValues(name, accessEnc, speedEnc, priorityEnc, null, turnCostEnc, speedEnc.getNextStorableValue(maxSpeed), false, false); + return new VehicleEncodedValues(name, accessEnc, speedEnc, priorityEnc, null, turnCostEnc); } public static VehicleEncodedValues bike2(PMap properties) { @@ -108,11 +95,10 @@ public static VehicleEncodedValues car(PMap properties) { double speedFactor = properties.getDouble("speed_factor", 5); boolean speedTwoDirections = properties.getBool("speed_two_directions", false); int maxTurnCosts = properties.getInt("max_turn_costs", properties.getBool("turn_costs", false) ? 1 : 0); - double maxSpeed = properties.getDouble("max_speed", CAR_MAX_SPEED); BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue(getKey(name, "access"), true); DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl(getKey(name, "average_speed"), speedBits, speedFactor, speedTwoDirections); DecimalEncodedValue turnCostEnc = maxTurnCosts > 0 ? TurnCost.create(name, maxTurnCosts) : null; - return new VehicleEncodedValues(name, accessEnc, speedEnc, null, null, turnCostEnc, speedEnc.getNextStorableValue(maxSpeed), true, false); + return new VehicleEncodedValues(name, accessEnc, speedEnc, null, null, turnCostEnc); } public static VehicleEncodedValues car4wd(PMap properties) { @@ -125,16 +111,15 @@ public static VehicleEncodedValues motorcycle(PMap properties) { double speedFactor = properties.getDouble("speed_factor", 5); boolean speedTwoDirections = properties.getBool("speed_two_directions", true); int maxTurnCosts = properties.getInt("max_turn_costs", properties.getBool("turn_costs", false) ? 1 : 0); - double maxSpeed = properties.getDouble("max_speed", MOTOR_CYCLE_MAX_SPEED); BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue(getKey(name, "access"), true); DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl(getKey(name, "average_speed"), speedBits, speedFactor, speedTwoDirections); DecimalEncodedValue priorityEnc = new DecimalEncodedValueImpl(getKey(name, "priority"), 4, PriorityCode.getFactor(1), false); DecimalEncodedValue curvatureEnc = new DecimalEncodedValueImpl(getKey(name, "curvature"), 4, 0.1, false); DecimalEncodedValue turnCostEnc = maxTurnCosts > 0 ? TurnCost.create(name, maxTurnCosts) : null; - return new VehicleEncodedValues(name, accessEnc, speedEnc, priorityEnc, curvatureEnc, turnCostEnc, speedEnc.getNextStorableValue(maxSpeed), true, false); + return new VehicleEncodedValues(name, accessEnc, speedEnc, priorityEnc, curvatureEnc, turnCostEnc); } - public static VehicleEncodedValues roads(PMap properties) { + public static VehicleEncodedValues roads() { String name = "roads"; int speedBits = 7; double speedFactor = 2; @@ -143,32 +128,18 @@ public static VehicleEncodedValues roads(PMap properties) { BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue(getKey(name, "access"), true); DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl(getKey(name, "average_speed"), speedBits, speedFactor, speedTwoDirections); DecimalEncodedValue turnCostEnc = maxTurnCosts > 0 ? TurnCost.create(name, maxTurnCosts) : null; - boolean isMotorVehicle = properties.getBool("is_motor_vehicle", true); - boolean isHGV = properties.getBool("is_hgv", false); - return new VehicleEncodedValues(name, accessEnc, speedEnc, null, null, turnCostEnc, speedEnc.getNextStorableValue(ROADS_MAX_SPEED), isMotorVehicle, isHGV); + return new VehicleEncodedValues(name, accessEnc, speedEnc, null, null, turnCostEnc); } public VehicleEncodedValues(String name, BooleanEncodedValue accessEnc, DecimalEncodedValue avgSpeedEnc, DecimalEncodedValue priorityEnc, DecimalEncodedValue curvatureEnc, - DecimalEncodedValue turnCostEnc, double maxPossibleSpeed, boolean isMotorVehicle, boolean isHGV) { + DecimalEncodedValue turnCostEnc) { this.name = name; this.accessEnc = accessEnc; this.avgSpeedEnc = avgSpeedEnc; this.priorityEnc = priorityEnc; this.curvatureEnc = curvatureEnc; this.turnCostEnc = turnCostEnc; - this.maxPossibleSpeed = maxPossibleSpeed; - this.isMotorVehicle = isMotorVehicle; - this.isHGV = isHGV; - } - - public void setEncodedValueLookup(EncodedValueLookup encodedValueLookup) { - this.encodedValueLookup = encodedValueLookup; - } - - @Override - public boolean isRegistered() { - return encodedValueLookup != null; } public void createEncodedValues(List registerNewEncodedValue) { @@ -187,102 +158,26 @@ public void createTurnCostEncodedValues(List registerNewTurnCostEn registerNewTurnCostEncodedValues.add(turnCostEnc); } - @Override - public double getMaxSpeed() { - return maxPossibleSpeed; - } - - @Override public BooleanEncodedValue getAccessEnc() { return accessEnc; } - @Override public DecimalEncodedValue getAverageSpeedEnc() { return avgSpeedEnc; } - @Override public DecimalEncodedValue getPriorityEnc() { return priorityEnc; } - @Override public DecimalEncodedValue getCurvatureEnc() { return curvatureEnc; } - @Override public DecimalEncodedValue getTurnCostEnc() { return turnCostEnc; } - public String toSerializationString() { - return - String.join("|", - name, - Stream.of(accessEnc, avgSpeedEnc, priorityEnc, curvatureEnc, turnCostEnc) - .map(ev -> ev == null ? "null" : ev.getName()) - .collect(Collectors.joining("|")), - String.valueOf(maxPossibleSpeed), String.valueOf(isMotorVehicle), String.valueOf(isHGV)); - } - - @Override - public List getEncodedValues() { - return encodedValueLookup.getEncodedValues(); - } - - @Override - public T getEncodedValue(String key, Class encodedValueType) { - return encodedValueLookup.getEncodedValue(key, encodedValueType); - } - - @Override - public BooleanEncodedValue getBooleanEncodedValue(String key) { - return encodedValueLookup.getBooleanEncodedValue(key); - } - - @Override - public IntEncodedValue getIntEncodedValue(String key) { - return encodedValueLookup.getIntEncodedValue(key); - } - - @Override - public DecimalEncodedValue getDecimalEncodedValue(String key) { - return encodedValueLookup.getDecimalEncodedValue(key); - } - - @Override - public > EnumEncodedValue getEnumEncodedValue(String key, Class enumType) { - return encodedValueLookup.getEnumEncodedValue(key, enumType); - } - - @Override - public StringEncodedValue getStringEncodedValue(String key) { - return encodedValueLookup.getStringEncodedValue(key); - } - - @Override - public boolean isMotorVehicle() { - return isMotorVehicle; - } - - @Override - public boolean isHGV() { - return isHGV; - } - - @Override - public boolean supportsTurnCosts() { - return turnCostEnc != null; - } - - @Override - public boolean hasEncodedValue(String key) { - return encodedValueLookup.hasEncodedValue(key); - } - - @Override public String getName() { return name; } diff --git a/core/src/main/java/com/graphhopper/routing/util/FlagEncoderFactory.java b/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValuesFactory.java similarity index 90% rename from core/src/main/java/com/graphhopper/routing/util/FlagEncoderFactory.java rename to core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValuesFactory.java index 48df5dfeec0..57402acfba4 100644 --- a/core/src/main/java/com/graphhopper/routing/util/FlagEncoderFactory.java +++ b/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValuesFactory.java @@ -22,7 +22,7 @@ /** * @author Peter Karich */ -public interface FlagEncoderFactory { +public interface VehicleEncodedValuesFactory { String ROADS = "roads"; String CAR = "car"; String CAR4WD = "car4wd"; @@ -35,6 +35,6 @@ public interface FlagEncoderFactory { String MOTORCYCLE = "motorcycle"; String WHEELCHAIR = "wheelchair"; - FlagEncoder createFlagEncoder(String name, PMap configuration); + VehicleEncodedValues createVehicleEncodedValues(String name, PMap configuration); } diff --git a/core/src/test/java/com/graphhopper/GraphHopperTest.java b/core/src/test/java/com/graphhopper/GraphHopperTest.java index ec902098ff9..956dd8f0ee8 100644 --- a/core/src/test/java/com/graphhopper/GraphHopperTest.java +++ b/core/src/test/java/com/graphhopper/GraphHopperTest.java @@ -2545,7 +2545,7 @@ public void testLoadGraph_implicitEncodedValues_issue1862() { int nodes = hopper.getBaseGraph().getNodes(); hopper.close(); - // load without configured FlagEncoders + // load without configured graph.flag_encoders hopper = new GraphHopper(); hopper.setProfiles(Arrays.asList( new Profile("p_car").setVehicle("car").setWeighting("fastest"), @@ -2557,7 +2557,7 @@ public void testLoadGraph_implicitEncodedValues_issue1862() { assertEquals(nodes, hopper.getBaseGraph().getNodes()); hopper.close(); - // load via explicitly configured FlagEncoders + // load via explicitly configured graph.flag_encoders hopper = new GraphHopper(); hopper.setFlagEncodersString("car,bike"); hopper.setProfiles(Arrays.asList( diff --git a/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java b/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java index 40da5df9a06..d097ef4deca 100644 --- a/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java +++ b/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java @@ -666,14 +666,11 @@ public void testReadEleFromDataProvider() { @Test public void testTurnFlagCombination() { GraphHopper hopper = new GraphHopper(); - hopper.setFlagEncoderFactory(new FlagEncoderFactory() { - @Override - public FlagEncoder createFlagEncoder(String name, PMap config) { - if (name.equals("truck")) { - return FlagEncoders.createCar(new PMap(config).putObject("name", "truck")); - } else { - return new DefaultFlagEncoderFactory().createFlagEncoder(name, config); - } + hopper.setVehicleEncodedValuesFactory((name, config) -> { + if (name.equals("truck")) { + return VehicleEncodedValues.car(new PMap(config).putObject("name", "truck")); + } else { + return new DefaultVehicleEncodedValuesFactory().createVehicleEncodedValues(name, config); } }); hopper.setVehicleTagParserFactory((lookup, name, config) -> { @@ -959,7 +956,7 @@ public void testCurvedWayAlongBorder() throws IOException { // see https://discuss.graphhopper.com/t/country-of-way-is-wrong-on-road-near-border-with-curvature/6908/2 EnumEncodedValue countryEnc = new EnumEncodedValue<>(Country.KEY, Country.class); EncodingManager em = EncodingManager.start() - .add(FlagEncoders.createCar()) + .add(VehicleEncodedValues.car(new PMap())) .add(countryEnc) .build(); CarTagParser carParser = new CarTagParser(em, new PMap()); diff --git a/example/src/main/java/com/graphhopper/example/RoutingExampleTC.java b/example/src/main/java/com/graphhopper/example/RoutingExampleTC.java index c356f2c61b0..9f61e591c1e 100644 --- a/example/src/main/java/com/graphhopper/example/RoutingExampleTC.java +++ b/example/src/main/java/com/graphhopper/example/RoutingExampleTC.java @@ -7,7 +7,6 @@ import com.graphhopper.ResponsePath; import com.graphhopper.config.CHProfile; import com.graphhopper.config.Profile; -import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.util.Parameters; import java.util.Arrays; @@ -68,10 +67,8 @@ static GraphHopper createGraphHopperInstance(String ghLoc) { GraphHopper hopper = new GraphHopper(); hopper.setOSMFile(ghLoc); hopper.setGraphHopperLocation("target/routing-tc-graph-cache"); - // by enabling turn costs for the FlagEncoder, turn restriction constraints like 'no_left_turn' will be taken - // from OSM Profile profile = new Profile("car").setVehicle("car").setWeighting("fastest") - // to actually use the turn restrictions when routing we have to enable turn costs for our routing profile + // enabling turn costs means OSM turn restriction constraints like 'no_left_turn' will be taken into account .setTurnCosts(true) // we can also set u_turn_costs (in seconds). by default no u-turns are allowed, but with this setting // we will consider u-turns at all junctions with a 40s time penalty From 67e5c0c22a2e8ef44ce372f9f6ba70c0f41cafe8 Mon Sep 17 00:00:00 2001 From: easbar Date: Sat, 25 Jun 2022 17:22:53 +0200 Subject: [PATCH 15/28] Move private/destination factors to weighting factory --- .../routing/DefaultWeightingFactory.java | 11 ++++++++++ .../routing/weighting/FastestWeighting.java | 21 ++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java index f072d094e0a..c46fb3ee2c5 100644 --- a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java +++ b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java @@ -30,6 +30,8 @@ import com.graphhopper.util.PMap; import com.graphhopper.util.Parameters; +import static com.graphhopper.routing.weighting.FastestWeighting.DESTINATION_FACTOR; +import static com.graphhopper.routing.weighting.FastestWeighting.PRIVATE_FACTOR; import static com.graphhopper.routing.weighting.TurnCostProvider.NO_TURN_COST_PROVIDER; import static com.graphhopper.routing.weighting.Weighting.INFINITE_U_TURN_COSTS; import static com.graphhopper.util.Helper.toLowerCase; @@ -55,6 +57,15 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis hints.putAll(requestHints); final String vehicle = profile.getVehicle(); + // todonow: we used to take this information from the 'encoder', but now it is gone. We probably need to take + // it from the profile now somehow. + final boolean isMotorVehicle = true; + if (isMotorVehicle) { + hints.putObject(DESTINATION_FACTOR, hints.getDouble(DESTINATION_FACTOR, 10)); + hints.putObject(PRIVATE_FACTOR, hints.getDouble(PRIVATE_FACTOR, 10)); + } else { + hints.putObject(PRIVATE_FACTOR, hints.getDouble(PRIVATE_FACTOR, 1.2)); + } TurnCostProvider turnCostProvider; if (profile.isTurnCosts() && !disableTurnCosts) { DecimalEncodedValue turnCostEnc = encodingManager.getDecimalEncodedValue(TurnCost.key(vehicle)); diff --git a/core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java index 73e614c67b1..e7b1c0b63df 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java @@ -35,6 +35,8 @@ * @author Peter Karich */ public class FastestWeighting extends AbstractWeighting { + public static String DESTINATION_FACTOR = "road_access_destination_factor"; + public static String PRIVATE_FACTOR = "road_access_private_factor"; /** * Converting to seconds is not necessary but makes adding other penalties easier (e.g. turn * costs or traffic light costs etc) @@ -63,19 +65,14 @@ public FastestWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speed // maxSpeed = encoder.getMaxSpeed() / SPEED_CONV; maxSpeed = 140 / SPEED_CONV; - // ensure that we do not need to change getMinWeight, i.e. road_access_factor >= 1 - // todonow: maybe setup these factors in weighting factory? -// double defaultDestinationFactor = encoder.isMotorVehicle() ? 10 : 1; - double defaultDestinationFactor = 10; - destinationPenalty = checkBounds("road_access_destination_factor", map.getDouble("road_access_destination_factor", defaultDestinationFactor), 1, 10); - // todonow: maybe setup these factors in weighting factory? -// double defaultPrivateFactor = encoder.isMotorVehicle() ? 10 : 1.2; - double defaultPrivateFactor = 10; - privatePenalty = checkBounds("road_access_private_factor", map.getDouble("road_access_private_factor", defaultPrivateFactor), 1, 10); + destinationPenalty = map.getDouble(DESTINATION_FACTOR, 1); + privatePenalty = map.getDouble(PRIVATE_FACTOR, 1); + // ensure that we do not need to change getMinWeight, i.e. both factors need to be >= 1 + checkBounds(DESTINATION_FACTOR, destinationPenalty, 1, 10); + checkBounds(PRIVATE_FACTOR, privatePenalty, 1, 10); if (destinationPenalty > 1 || privatePenalty > 1) { - // todonow: maybe only error when factors are given *explicitly* -// if (roadAccessEnc == null) -// throw new IllegalArgumentException("road_access must not be null when destination or private penalties are > 1"); + if (roadAccessEnc == null) + throw new IllegalArgumentException("road_access must not be null when destination or private penalties are > 1"); this.roadAccessEnc = roadAccessEnc; } else this.roadAccessEnc = null; From 51f8f62e6b9d9a4c899cc6fab493c9ff66dcea9f Mon Sep 17 00:00:00 2001 From: easbar Date: Sun, 26 Jun 2022 12:34:17 +0200 Subject: [PATCH 16/28] Fix a test --- .../src/test/java/com/graphhopper/GraphHopperProfileTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/com/graphhopper/GraphHopperProfileTest.java b/core/src/test/java/com/graphhopper/GraphHopperProfileTest.java index 71d95c9f3d4..6fcd9fb7a62 100644 --- a/core/src/test/java/com/graphhopper/GraphHopperProfileTest.java +++ b/core/src/test/java/com/graphhopper/GraphHopperProfileTest.java @@ -63,14 +63,14 @@ public void vehicleDoesNotExist_error() { final GraphHopper hopper = new GraphHopper(); hopper.setGraphHopperLocation(GH_LOCATION).setStoreOnFlush(false). setProfiles(new Profile("profile").setVehicle("your_car")); - assertIllegalArgument(hopper::importOrLoad, "entry in encoder list not supported: your_car"); + assertIllegalArgument(hopper::importOrLoad, "entry in vehicle list not supported: your_car"); } @Test public void vehicleDoesNotExist_error2() { final GraphHopper hopper = new GraphHopper().setGraphHopperLocation(GH_LOCATION).setStoreOnFlush(false). setProfiles(new Profile("profile").setVehicle("your_car")); - assertIllegalArgument(hopper::importOrLoad, "entry in encoder list not supported: your_car"); + assertIllegalArgument(hopper::importOrLoad, "entry in vehicle list not supported: your_car"); } @Test From 6524be09a2315022fb3fce94d5343aec82e2f207 Mon Sep 17 00:00:00 2001 From: easbar Date: Thu, 30 Jun 2022 13:38:22 +0200 Subject: [PATCH 17/28] fix --- .../java/com/graphhopper/routing/DefaultWeightingFactory.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java index 1eeef50c2a6..a8c2738a87f 100644 --- a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java +++ b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java @@ -25,7 +25,6 @@ import com.graphhopper.routing.weighting.custom.CustomModelParser; import com.graphhopper.routing.weighting.custom.CustomProfile; import com.graphhopper.routing.weighting.custom.CustomWeighting; -import com.graphhopper.routing.weighting.custom.FindMinMax; import com.graphhopper.storage.BaseGraph; import com.graphhopper.util.CustomModel; import com.graphhopper.util.PMap; @@ -95,8 +94,7 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis CustomProfile customProfile = (CustomProfile) profile; queryCustomModel = CustomModel.merge(customProfile.getCustomModel(), queryCustomModel); - // todonow: this used to be encoder.getMaxSpeed()! - double maxSpeed = speedEnc.getMaxDecimal(); + double maxSpeed = speedEnc.getMaxOrMaxStorableDecimal(); weighting = CustomModelParser.createWeighting(accessEnc, speedEnc, priorityEnc, maxSpeed, encodingManager, turnCostProvider, queryCustomModel); } else if ("shortest".equalsIgnoreCase(weightingStr)) { From 2807baca8bc6fbce2c9d2d8e6996192546f04697 Mon Sep 17 00:00:00 2001 From: easbar Date: Thu, 30 Jun 2022 13:48:23 +0200 Subject: [PATCH 18/28] Fix a few more tests --- .../routing/weighting/FastestWeightingTest.java | 16 +++++++++++----- .../weighting/custom/CustomWeightingTest.java | 7 ++++--- .../application/resources/RouteResourceTest.java | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java index e1d8fd21ba8..808d8868cc5 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java @@ -28,6 +28,8 @@ import com.graphhopper.util.Parameters.Routing; import org.junit.jupiter.api.Test; +import static com.graphhopper.routing.weighting.FastestWeighting.DESTINATION_FACTOR; +import static com.graphhopper.routing.weighting.FastestWeighting.PRIVATE_FACTOR; import static com.graphhopper.util.GHUtility.createMockedEdgeIteratorState; import static com.graphhopper.util.GHUtility.getEdge; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -146,14 +148,16 @@ public void testDestinationTag() { edge.set(bikeSpeedEnc, 18); EnumEncodedValue roadAccessEnc = em.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class); - FastestWeighting weighting = new FastestWeighting(carAccessEnc, carSpeedEnc); - FastestWeighting bikeWeighting = new FastestWeighting(bikeAccessEnc, bikeSpeedEnc); + FastestWeighting weighting = new FastestWeighting(carAccessEnc, carSpeedEnc, roadAccessEnc, + new PMap().putObject(DESTINATION_FACTOR, 10), TurnCostProvider.NO_TURN_COST_PROVIDER); + FastestWeighting bikeWeighting = new FastestWeighting(bikeAccessEnc, bikeSpeedEnc, roadAccessEnc, + new PMap().putObject(DESTINATION_FACTOR, 1), TurnCostProvider.NO_TURN_COST_PROVIDER); edge.set(roadAccessEnc, RoadAccess.YES); assertEquals(60, weighting.calcEdgeWeight(edge, false), 1.e-6); assertEquals(200, bikeWeighting.calcEdgeWeight(edge, false), 1.e-6); - // the destination tag does not change the weight for bikes! + // the destination tag does not change the weight for the bike weighting edge.set(roadAccessEnc, RoadAccess.DESTINATION); assertEquals(600, weighting.calcEdgeWeight(edge, false), 0.1); assertEquals(200, bikeWeighting.calcEdgeWeight(edge, false), 0.1); @@ -174,8 +178,10 @@ public void testPrivateTag() { edge.set(bikeSpeedEnc, 18); EnumEncodedValue roadAccessEnc = em.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class); - FastestWeighting weighting = new FastestWeighting(carAccessEnc, carSpeedEnc); - FastestWeighting bikeWeighting = new FastestWeighting(bikeAccessEnc, bikeSpeedEnc); + FastestWeighting weighting = new FastestWeighting(carAccessEnc, carSpeedEnc, roadAccessEnc, + new PMap().putObject(PRIVATE_FACTOR, 10), TurnCostProvider.NO_TURN_COST_PROVIDER); + FastestWeighting bikeWeighting = new FastestWeighting(bikeAccessEnc, bikeSpeedEnc, roadAccessEnc, + new PMap().putObject(PRIVATE_FACTOR, 1.2), TurnCostProvider.NO_TURN_COST_PROVIDER); ReaderWay way = new ReaderWay(1); way.setTag("highway", "secondary"); diff --git a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java index b339cd90124..f834cbf9409 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java @@ -250,8 +250,8 @@ public void testMaxSpeed() { assertEquals(1000.0 / 72 * 3.6, createWeighting(new CustomModel(). addToSpeed(If("true", LIMIT, "72")).setDistanceInfluence(0)).getMinWeight(1000)); - // ignore too big limit to let custom model compatibility not break when max speed of encoder later decreases - assertEquals(1000.0 / 155 * 3.6, createWeighting(new CustomModel(). + // ignore too big limit to let custom model compatibility not break when max speed of encoded value later decreases + assertEquals(1000.0 / maxSpeed * 3.6, createWeighting(new CustomModel(). addToSpeed(If("true", LIMIT, "180")).setDistanceInfluence(0)).getMinWeight(1000)); // a speed bigger than the allowed stored speed is fine, see discussion in #2335 @@ -262,7 +262,8 @@ public void testMaxSpeed() { @Test public void testMaxPriority() { - double maxSpeed = 155; + assertEquals(140, maxSpeed); + assertEquals(1000.0 / maxSpeed / 0.5 * 3.6, createWeighting(new CustomModel(). addToPriority(If("true", MULTIPLY, "0.5")).setDistanceInfluence(0)).getMinWeight(1000), 1.e-6); diff --git a/web/src/test/java/com/graphhopper/application/resources/RouteResourceTest.java b/web/src/test/java/com/graphhopper/application/resources/RouteResourceTest.java index 82b52cc222e..44b93c1a97d 100644 --- a/web/src/test/java/com/graphhopper/application/resources/RouteResourceTest.java +++ b/web/src/test/java/com/graphhopper/application/resources/RouteResourceTest.java @@ -492,7 +492,7 @@ public void testGraphHopperWebRealExceptions(boolean usePost) { ex = rsp.getErrors().get(0); assertTrue(ex instanceof IllegalArgumentException, "Wrong exception found: " + ex.getClass().getName() + ", IllegalArgumentException expected."); - assertTrue(ex.getMessage().contains("Vehicle not supported: `space-shuttle`. Supported are: `car`" + + assertTrue(ex.getMessage().contains("Vehicle not supported: `space-shuttle`. Supported are: `[car]`" + "\nYou should consider using the `profile` parameter instead of specifying a vehicle." + "\nAvailable profiles: [my_car]"), ex.getMessage()); From 97ca23156a9a08d936e7454f5ec22e0b34454af7 Mon Sep 17 00:00:00 2001 From: easbar Date: Thu, 30 Jun 2022 15:13:53 +0200 Subject: [PATCH 19/28] default weighting factory determines which vehicles get private/destination penalty --- .../routing/DefaultWeightingFactory.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java index a8c2738a87f..a09202ea3fb 100644 --- a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java +++ b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java @@ -30,6 +30,10 @@ import com.graphhopper.util.PMap; import com.graphhopper.util.Parameters; +import java.util.Arrays; +import java.util.List; + +import static com.graphhopper.routing.util.VehicleEncodedValuesFactory.*; import static com.graphhopper.routing.weighting.FastestWeighting.DESTINATION_FACTOR; import static com.graphhopper.routing.weighting.FastestWeighting.PRIVATE_FACTOR; import static com.graphhopper.routing.weighting.TurnCostProvider.NO_TURN_COST_PROVIDER; @@ -37,6 +41,8 @@ import static com.graphhopper.util.Helper.toLowerCase; public class DefaultWeightingFactory implements WeightingFactory { + private static final List OUTDOOR_VEHICLES = Arrays.asList(BIKE, BIKE2, RACINGBIKE, MOUNTAINBIKE, FOOT, HIKE, WHEELCHAIR); + private final BaseGraph graph; private final EncodingManager encodingManager; @@ -57,14 +63,11 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis hints.putAll(requestHints); final String vehicle = profile.getVehicle(); - // todonow: we used to take this information from the 'encoder', but now it is gone. We probably need to take - // it from the profile now somehow. - final boolean isMotorVehicle = true; - if (isMotorVehicle) { + if (isOutdoorVehicle(vehicle)) { + hints.putObject(PRIVATE_FACTOR, hints.getDouble(PRIVATE_FACTOR, 1.2)); + } else { hints.putObject(DESTINATION_FACTOR, hints.getDouble(DESTINATION_FACTOR, 10)); hints.putObject(PRIVATE_FACTOR, hints.getDouble(PRIVATE_FACTOR, 10)); - } else { - hints.putObject(PRIVATE_FACTOR, hints.getDouble(PRIVATE_FACTOR, 1.2)); } TurnCostProvider turnCostProvider; if (profile.isTurnCosts() && !disableTurnCosts) { @@ -129,4 +132,8 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis return weighting; } + + public boolean isOutdoorVehicle(String name) { + return OUTDOOR_VEHICLES.contains(name); + } } \ No newline at end of file From 0dfec458b326ce6a542b839e8b3aa813d15dcb9e Mon Sep 17 00:00:00 2001 From: easbar Date: Thu, 30 Jun 2022 16:05:04 +0200 Subject: [PATCH 20/28] remove max speed from custom weighting create weighting --- .../routing/DefaultWeightingFactory.java | 3 +-- .../weighting/custom/CustomModelParser.java | 4 ++-- .../routing/PriorityRoutingTest.java | 22 +++++++++---------- .../weighting/custom/CustomWeightingTest.java | 18 +++++++-------- .../graphhopper/util/InstructionListTest.java | 4 ++-- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java index a09202ea3fb..c13c1ee7f93 100644 --- a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java +++ b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java @@ -97,9 +97,8 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis CustomProfile customProfile = (CustomProfile) profile; queryCustomModel = CustomModel.merge(customProfile.getCustomModel(), queryCustomModel); - double maxSpeed = speedEnc.getMaxOrMaxStorableDecimal(); weighting = CustomModelParser.createWeighting(accessEnc, speedEnc, - priorityEnc, maxSpeed, encodingManager, turnCostProvider, queryCustomModel); + priorityEnc, encodingManager, turnCostProvider, queryCustomModel); } else if ("shortest".equalsIgnoreCase(weightingStr)) { weighting = new ShortestWeighting(accessEnc, speedEnc, turnCostProvider); } else if ("fastest".equalsIgnoreCase(weightingStr)) { diff --git a/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomModelParser.java b/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomModelParser.java index 7c0fa77dc9b..c610e4aa7e3 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomModelParser.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomModelParser.java @@ -68,10 +68,10 @@ private CustomModelParser() { } public static CustomWeighting createWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, DecimalEncodedValue priorityEnc, - double maxSpeed, EncodedValueLookup lookup, - TurnCostProvider turnCostProvider, CustomModel customModel) { + EncodedValueLookup lookup, TurnCostProvider turnCostProvider, CustomModel customModel) { if (customModel == null) throw new IllegalStateException("CustomModel cannot be null"); + double maxSpeed = speedEnc.getMaxOrMaxStorableDecimal(); CustomWeighting.Parameters parameters = createWeightingParameters(customModel, lookup, speedEnc, maxSpeed, priorityEnc); return new CustomWeighting(accessEnc, speedEnc, turnCostProvider, parameters); } diff --git a/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java b/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java index 8d4bf63d77f..15c1bb9873e 100644 --- a/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/PriorityRoutingTest.java @@ -56,17 +56,17 @@ void testMaxPriority() { na.setNode(5, 48.2, 11.1); // 0 - 1 - 2 - 3 // \- 4 - 5 -/ - final double maxSpeed = speedEnc.getNextStorableValue(30); + double speed = speedEnc.getNextStorableValue(30); double dist1 = 0; - dist1 += addEdge(em, graph, 0, 1, 1.0, accessEnc, speedEnc, priorityEnc, maxSpeed).getDistance(); - dist1 += addEdge(em, graph, 1, 2, 1.0, accessEnc, speedEnc, priorityEnc, maxSpeed).getDistance(); - dist1 += addEdge(em, graph, 2, 3, 1.0, accessEnc, speedEnc, priorityEnc, maxSpeed).getDistance(); + dist1 += addEdge(em, graph, 0, 1, 1.0, accessEnc, speedEnc, priorityEnc, speed).getDistance(); + dist1 += addEdge(em, graph, 1, 2, 1.0, accessEnc, speedEnc, priorityEnc, speed).getDistance(); + dist1 += addEdge(em, graph, 2, 3, 1.0, accessEnc, speedEnc, priorityEnc, speed).getDistance(); final double maxPrio = PriorityCode.getFactor(PriorityCode.BEST.getValue()); double dist2 = 0; - dist2 += addEdge(em, graph, 0, 4, maxPrio, accessEnc, speedEnc, priorityEnc, maxSpeed).getDistance(); - dist2 += addEdge(em, graph, 4, 5, maxPrio, accessEnc, speedEnc, priorityEnc, maxSpeed).getDistance(); - dist2 += addEdge(em, graph, 5, 3, maxPrio, accessEnc, speedEnc, priorityEnc, maxSpeed).getDistance(); + dist2 += addEdge(em, graph, 0, 4, maxPrio, accessEnc, speedEnc, priorityEnc, speed).getDistance(); + dist2 += addEdge(em, graph, 4, 5, maxPrio, accessEnc, speedEnc, priorityEnc, speed).getDistance(); + dist2 += addEdge(em, graph, 5, 3, maxPrio, accessEnc, speedEnc, priorityEnc, speed).getDistance(); // the routes 0-1-2-3 and 0-4-5-3 have similar distances (and use max speed everywhere) // ... but the shorter route 0-1-2-3 has smaller priority @@ -85,7 +85,7 @@ void testMaxPriority() { { CustomModel customModel = new CustomModel(); CustomWeighting weighting = CustomModelParser.createWeighting(accessEnc, - speedEnc, priorityEnc, maxSpeed, em, + speedEnc, priorityEnc, em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); @@ -98,7 +98,7 @@ void testMaxPriority() { // now we even increase the priority in the custom model, which also needs to be accounted for in weighting.getMinWeight customModel.addToPriority(Statement.If("road_class == MOTORWAY", Statement.Op.MULTIPLY, "3")); CustomWeighting weighting = CustomModelParser.createWeighting(accessEnc, speedEnc, - priorityEnc, maxSpeed, em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); + priorityEnc, em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3); assertEquals(pathDijkstra.calcNodes(), pathAStar.calcNodes()); @@ -106,11 +106,11 @@ void testMaxPriority() { } } - private EdgeIteratorState addEdge(EncodingManager em, BaseGraph graph, int p, int q, double prio, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, DecimalEncodedValue priorityEnc, double maxSpeed) { + private EdgeIteratorState addEdge(EncodingManager em, BaseGraph graph, int p, int q, double prio, BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, DecimalEncodedValue priorityEnc, double speed) { EnumEncodedValue roadClassEnc = em.getEnumEncodedValue(RoadClass.KEY, RoadClass.class); return graph.edge(p, q) .set(accessEnc, true) - .set(speedEnc, maxSpeed) + .set(speedEnc, speed) .set(priorityEnc, prio) .set(roadClassEnc, RoadClass.MOTORWAY) .setDistance(calcDist(graph, p, q)); diff --git a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java index f834cbf9409..4b3c7579d2e 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java @@ -29,7 +29,6 @@ class CustomWeightingTest { DecimalEncodedValue maxSpeedEnc; EnumEncodedValue roadClassEnc; EncodingManager encodingManager; - double maxSpeed; @BeforeEach public void setup() { @@ -43,7 +42,6 @@ public void setup() { maxSpeedEnc = encodingManager.getDecimalEncodedValue(MaxSpeed.KEY); roadClassEnc = encodingManager.getEnumEncodedValue(KEY, RoadClass.class); graph = new BaseGraph.Builder(encodingManager).create(); - maxSpeed = 140; } @Test @@ -128,11 +126,11 @@ public void testBoolean() { set(avSpeedEnc, 15).set(specialEnc, false).setReverse(specialEnc, true).setDistance(10); CustomModel vehicleModel = new CustomModel(); - Weighting weighting = CustomModelParser.createWeighting(accessEnc, avSpeedEnc, null, maxSpeed, encodingManager, NO_TURN_COST_PROVIDER, vehicleModel); + Weighting weighting = CustomModelParser.createWeighting(accessEnc, avSpeedEnc, null, encodingManager, NO_TURN_COST_PROVIDER, vehicleModel); assertEquals(3.1, weighting.calcEdgeWeight(edge, false), 0.01); vehicleModel.addToPriority(If("special == true", MULTIPLY, "0.8")); vehicleModel.addToPriority(If("special == false", MULTIPLY, "0.4")); - weighting = CustomModelParser.createWeighting(accessEnc, avSpeedEnc, null, maxSpeed, encodingManager, NO_TURN_COST_PROVIDER, vehicleModel); + weighting = CustomModelParser.createWeighting(accessEnc, avSpeedEnc, null, encodingManager, NO_TURN_COST_PROVIDER, vehicleModel); assertEquals(6.7, weighting.calcEdgeWeight(edge, false), 0.01); assertEquals(3.7, weighting.calcEdgeWeight(edge, true), 0.01); } @@ -245,16 +243,16 @@ public void testArea() throws Exception { @Test public void testMaxSpeed() { - assertEquals(140, maxSpeed, 0.1); + assertEquals(155, avSpeedEnc.getMaxOrMaxStorableDecimal(), 0.1); assertEquals(1000.0 / 72 * 3.6, createWeighting(new CustomModel(). addToSpeed(If("true", LIMIT, "72")).setDistanceInfluence(0)).getMinWeight(1000)); // ignore too big limit to let custom model compatibility not break when max speed of encoded value later decreases - assertEquals(1000.0 / maxSpeed * 3.6, createWeighting(new CustomModel(). + assertEquals(1000.0 / 155 * 3.6, createWeighting(new CustomModel(). addToSpeed(If("true", LIMIT, "180")).setDistanceInfluence(0)).getMinWeight(1000)); - // a speed bigger than the allowed stored speed is fine, see discussion in #2335 + // reduce speed only a bit assertEquals(1000.0 / 150 * 3.6, createWeighting(new CustomModel(). addToSpeed(If("road_class == SERVICE", MULTIPLY, "1.5")). addToSpeed(If("true", LIMIT, "150")).setDistanceInfluence(0)).getMinWeight(1000)); @@ -262,8 +260,8 @@ public void testMaxSpeed() { @Test public void testMaxPriority() { - assertEquals(140, maxSpeed); - + assertEquals(155, avSpeedEnc.getMaxOrMaxStorableDecimal(), 0.1); + double maxSpeed = 155; assertEquals(1000.0 / maxSpeed / 0.5 * 3.6, createWeighting(new CustomModel(). addToPriority(If("true", MULTIPLY, "0.5")).setDistanceInfluence(0)).getMinWeight(1000), 1.e-6); @@ -310,6 +308,6 @@ public void maxSpeedViolated_bug_2307() { } private Weighting createWeighting(CustomModel vehicleModel) { - return CustomModelParser.createWeighting(accessEnc, avSpeedEnc, null, maxSpeed, encodingManager, NO_TURN_COST_PROVIDER, vehicleModel); + return CustomModelParser.createWeighting(accessEnc, avSpeedEnc, null, encodingManager, NO_TURN_COST_PROVIDER, vehicleModel); } } \ No newline at end of file diff --git a/core/src/test/java/com/graphhopper/util/InstructionListTest.java b/core/src/test/java/com/graphhopper/util/InstructionListTest.java index 4c26fb28ae3..f8b7870c51a 100644 --- a/core/src/test/java/com/graphhopper/util/InstructionListTest.java +++ b/core/src/test/java/com/graphhopper/util/InstructionListTest.java @@ -409,7 +409,7 @@ public void testInstructionIfSlightTurnForCustomProfile() { GHUtility.setSpeed(5, true, true, accessEnc, speedEnc, g.edge(2, 4).setDistance(20).setName("myroad").set(priorityEnc, 1).setWayGeometry(pointList)); Weighting weighting = CustomModelParser.createWeighting(accessEnc, speedEnc, - priorityEnc, 15, tmpEM, DefaultTurnCostProvider.NO_TURN_COST_PROVIDER, + priorityEnc, tmpEM, DefaultTurnCostProvider.NO_TURN_COST_PROVIDER, new CustomModel().setDistanceInfluence(0)); Path p = new Dijkstra(g, weighting, tMode).calcPath(4, 3); assertTrue(p.isFound()); @@ -457,7 +457,7 @@ public void testInstructionWithHighlyCustomProfileWithRoadsBase() { CustomModel customModel = new CustomModel(); customModel.addToPriority(Statement.If("road_class == PEDESTRIAN", Statement.Op.MULTIPLY, "0")); - Weighting weighting = CustomModelParser.createWeighting(roadsAccessEnc, roadsSpeedEnc, null, 255, tmpEM, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); + Weighting weighting = CustomModelParser.createWeighting(roadsAccessEnc, roadsSpeedEnc, null, tmpEM, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel); Path p = new Dijkstra(g, weighting, tMode).calcPath(3, 4); InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, tmpEM, usTR); List tmpList = getTurnDescriptions(wayList); From 036645ae94672acdeb31bc95187596d3d8e05d1f Mon Sep 17 00:00:00 2001 From: easbar Date: Thu, 30 Jun 2022 17:25:38 +0200 Subject: [PATCH 21/28] vehicle access/speed/priority --- .../java/com/graphhopper/GraphHopper.java | 4 +- .../routing/DefaultWeightingFactory.java | 8 ++-- .../ev/DefaultEncodedValueFactory.java | 4 +- .../graphhopper/routing/ev/VehicleAccess.java | 32 ++++++++++++++ .../routing/ev/VehiclePriority.java | 32 ++++++++++++++ .../graphhopper/routing/ev/VehicleSpeed.java | 32 ++++++++++++++ .../routing/util/VehicleEncodedValues.java | 26 +++++------ .../routing/HeadingRoutingTest.java | 32 +++++++------- ...fficChangeWithNodeOrderingReusingTest.java | 8 ++-- .../routing/ch/CHProfileSelectorTest.java | 12 ++--- .../routing/lm/LMProfileSelectorTest.java | 12 ++--- .../routing/util/CarTagParserTest.java | 10 ++--- .../routing/util/FootTagParserTest.java | 14 +++--- .../routing/util/MotorcycleTagParserTest.java | 15 +++---- .../routing/util/RacingBikeTagParserTest.java | 6 +-- .../routing/util/TagParsingTest.java | 44 +++++++++---------- .../routing/util/WheelchairTagParserTest.java | 10 ++--- .../custom/CustomModelParserTest.java | 4 +- .../weighting/custom/CustomWeightingTest.java | 8 ++-- .../graphhopper/example/IsochroneExample.java | 8 ++-- .../example/LowLevelAPIExample.java | 16 +++---- .../java/com/graphhopper/gtfs/GtfsReader.java | 8 ++-- .../gtfs/PtRouterFreeWalkImpl.java | 14 +++--- .../com/graphhopper/gtfs/PtRouterImpl.java | 10 +++-- .../com/graphhopper/tools/Measurement.java | 3 +- .../java/com/graphhopper/ui/MiniGraphUI.java | 7 +-- .../resources/PtIsochroneResource.java | 8 ++-- 27 files changed, 240 insertions(+), 147 deletions(-) create mode 100644 core/src/main/java/com/graphhopper/routing/ev/VehicleAccess.java create mode 100644 core/src/main/java/com/graphhopper/routing/ev/VehiclePriority.java create mode 100644 core/src/main/java/com/graphhopper/routing/ev/VehicleSpeed.java diff --git a/core/src/main/java/com/graphhopper/GraphHopper.java b/core/src/main/java/com/graphhopper/GraphHopper.java index 8a5ec56e38f..5159f766cce 100644 --- a/core/src/main/java/com/graphhopper/GraphHopper.java +++ b/core/src/main/java/com/graphhopper/GraphHopper.java @@ -976,8 +976,8 @@ private void checkProfilesConsistency() { throw new IllegalArgumentException("There has to be at least one profile"); EncodingManager encodingManager = getEncodingManager(); for (Profile profile : profilesByName.values()) { - String accessEncName = EncodingManager.getKey(profile.getVehicle(), "access"); - String speedEncName = EncodingManager.getKey(profile.getVehicle(), "average_speed"); + String accessEncName = VehicleAccess.key(profile.getVehicle()); + String speedEncName = VehicleSpeed.key(profile.getVehicle()); if (!encodingManager.hasEncodedValue(accessEncName) || !encodingManager.hasEncodedValue(speedEncName)) throw new IllegalArgumentException("Unknown vehicle '" + profile.getVehicle() + "' in profile: " + profile + ". " + "Encoded values " + accessEncName + " and " + speedEncName + " are required"); diff --git a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java index c13c1ee7f93..b7f3d51ad2e 100644 --- a/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java +++ b/core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java @@ -85,10 +85,10 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis throw new IllegalArgumentException("You have to specify a weighting"); Weighting weighting = null; - BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(EncodingManager.getKey(vehicle, "access")); - DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey(vehicle, "average_speed")); - DecimalEncodedValue priorityEnc = encodingManager.hasEncodedValue(EncodingManager.getKey(vehicle, "priority")) - ? encodingManager.getDecimalEncodedValue(EncodingManager.getKey(vehicle, "priority")) + BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(VehicleAccess.key(vehicle)); + DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(VehicleSpeed.key(vehicle)); + DecimalEncodedValue priorityEnc = encodingManager.hasEncodedValue(VehiclePriority.key(vehicle)) + ? encodingManager.getDecimalEncodedValue(VehiclePriority.key(vehicle)) : null; if (CustomWeighting.NAME.equalsIgnoreCase(weightingStr)) { if (!(profile instanceof CustomProfile)) diff --git a/core/src/main/java/com/graphhopper/routing/ev/DefaultEncodedValueFactory.java b/core/src/main/java/com/graphhopper/routing/ev/DefaultEncodedValueFactory.java index c9c53e17595..3d91cf6357d 100644 --- a/core/src/main/java/com/graphhopper/routing/ev/DefaultEncodedValueFactory.java +++ b/core/src/main/java/com/graphhopper/routing/ev/DefaultEncodedValueFactory.java @@ -33,9 +33,9 @@ public EncodedValue create(String string) { if (Roundabout.KEY.equals(name)) { enc = Roundabout.create(); } else if ("car_access".equals(name)) { - enc = new SimpleBooleanEncodedValue("car_access", true); + enc = VehicleAccess.create("car"); } else if ("bike_access".equals(name)) { - enc = new SimpleBooleanEncodedValue("bike_access", true); + enc = VehicleAccess.create("bike"); } else if (GetOffBike.KEY.equals(name)) { enc = GetOffBike.create(); } else if (RoadClass.KEY.equals(name)) { diff --git a/core/src/main/java/com/graphhopper/routing/ev/VehicleAccess.java b/core/src/main/java/com/graphhopper/routing/ev/VehicleAccess.java new file mode 100644 index 00000000000..a7f76d9cbab --- /dev/null +++ b/core/src/main/java/com/graphhopper/routing/ev/VehicleAccess.java @@ -0,0 +1,32 @@ +/* + * Licensed to GraphHopper GmbH under one or more contributor + * license agreements. See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + * + * GraphHopper GmbH licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.graphhopper.routing.ev; + +import com.graphhopper.routing.util.EncodingManager; + +public class VehicleAccess { + + public static String key(String name) { + return EncodingManager.getKey(name, "access"); + } + + public static BooleanEncodedValue create(String name) { + return new SimpleBooleanEncodedValue(key(name), true); + } +} diff --git a/core/src/main/java/com/graphhopper/routing/ev/VehiclePriority.java b/core/src/main/java/com/graphhopper/routing/ev/VehiclePriority.java new file mode 100644 index 00000000000..c727997f6f3 --- /dev/null +++ b/core/src/main/java/com/graphhopper/routing/ev/VehiclePriority.java @@ -0,0 +1,32 @@ +/* + * Licensed to GraphHopper GmbH under one or more contributor + * license agreements. See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + * + * GraphHopper GmbH licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.graphhopper.routing.ev; + +import com.graphhopper.routing.util.EncodingManager; + +public class VehiclePriority { + + public static String key(String name) { + return EncodingManager.getKey(name, "priority"); + } + + public static DecimalEncodedValue create(String name, int speedBits, double speedFactor, boolean storeTwoDirections) { + return new DecimalEncodedValueImpl(key(name), speedBits, speedFactor, storeTwoDirections); + } +} diff --git a/core/src/main/java/com/graphhopper/routing/ev/VehicleSpeed.java b/core/src/main/java/com/graphhopper/routing/ev/VehicleSpeed.java new file mode 100644 index 00000000000..a95f09c8754 --- /dev/null +++ b/core/src/main/java/com/graphhopper/routing/ev/VehicleSpeed.java @@ -0,0 +1,32 @@ +/* + * Licensed to GraphHopper GmbH under one or more contributor + * license agreements. See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + * + * GraphHopper GmbH licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.graphhopper.routing.ev; + +import com.graphhopper.routing.util.EncodingManager; + +public class VehicleSpeed { + + public static String key(String name) { + return EncodingManager.getKey(name, "average_speed"); + } + + public static DecimalEncodedValue create(String name, int speedBits, double speedFactor, boolean storeTwoDirections) { + return new DecimalEncodedValueImpl(key(name), speedBits, speedFactor, storeTwoDirections); + } +} diff --git a/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValues.java b/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValues.java index 15483c3674e..f7e83945bb4 100644 --- a/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValues.java +++ b/core/src/main/java/com/graphhopper/routing/util/VehicleEncodedValues.java @@ -39,9 +39,9 @@ public static VehicleEncodedValues foot(PMap properties) { double speedFactor = properties.getDouble("speed_factor", 1); boolean speedTwoDirections = properties.getBool("speed_two_directions", false); int maxTurnCosts = properties.getInt("max_turn_costs", properties.getBool("turn_costs", false) ? 1 : 0); - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue(getKey(name, "access"), true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl(getKey(name, "average_speed"), speedBits, speedFactor, speedTwoDirections); - DecimalEncodedValue priorityEnc = new DecimalEncodedValueImpl(getKey(name, "priority"), 4, PriorityCode.getFactor(1), false); + BooleanEncodedValue accessEnc = VehicleAccess.create(name); + DecimalEncodedValue speedEnc = VehicleSpeed.create(name, speedBits, speedFactor, speedTwoDirections); + DecimalEncodedValue priorityEnc = VehiclePriority.create(name, 4, PriorityCode.getFactor(1), false); DecimalEncodedValue turnCostEnc = maxTurnCosts > 0 ? TurnCost.create(name, maxTurnCosts) : null; return new VehicleEncodedValues(name, accessEnc, speedEnc, priorityEnc, null, turnCostEnc); } @@ -65,9 +65,9 @@ public static VehicleEncodedValues bike(PMap properties) { double speedFactor = properties.getDouble("speed_factor", 2); boolean speedTwoDirections = properties.getBool("speed_two_directions", false); int maxTurnCosts = properties.getInt("max_turn_costs", properties.getBool("turn_costs", false) ? 1 : 0); - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue(getKey(name, "access"), true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl(getKey(name, "average_speed"), speedBits, speedFactor, speedTwoDirections); - DecimalEncodedValue priorityEnc = new DecimalEncodedValueImpl(getKey(name, "priority"), 4, PriorityCode.getFactor(1), false); + BooleanEncodedValue accessEnc = VehicleAccess.create(name); + DecimalEncodedValue speedEnc = VehicleSpeed.create(name, speedBits, speedFactor, speedTwoDirections); + DecimalEncodedValue priorityEnc = VehiclePriority.create(name, 4, PriorityCode.getFactor(1), false); DecimalEncodedValue turnCostEnc = maxTurnCosts > 0 ? TurnCost.create(name, maxTurnCosts) : null; return new VehicleEncodedValues(name, accessEnc, speedEnc, priorityEnc, null, turnCostEnc); } @@ -95,8 +95,8 @@ public static VehicleEncodedValues car(PMap properties) { double speedFactor = properties.getDouble("speed_factor", 5); boolean speedTwoDirections = properties.getBool("speed_two_directions", false); int maxTurnCosts = properties.getInt("max_turn_costs", properties.getBool("turn_costs", false) ? 1 : 0); - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue(getKey(name, "access"), true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl(getKey(name, "average_speed"), speedBits, speedFactor, speedTwoDirections); + BooleanEncodedValue accessEnc = VehicleAccess.create(name); + DecimalEncodedValue speedEnc = VehicleSpeed.create(name, speedBits, speedFactor, speedTwoDirections); DecimalEncodedValue turnCostEnc = maxTurnCosts > 0 ? TurnCost.create(name, maxTurnCosts) : null; return new VehicleEncodedValues(name, accessEnc, speedEnc, null, null, turnCostEnc); } @@ -111,9 +111,9 @@ public static VehicleEncodedValues motorcycle(PMap properties) { double speedFactor = properties.getDouble("speed_factor", 5); boolean speedTwoDirections = properties.getBool("speed_two_directions", true); int maxTurnCosts = properties.getInt("max_turn_costs", properties.getBool("turn_costs", false) ? 1 : 0); - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue(getKey(name, "access"), true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl(getKey(name, "average_speed"), speedBits, speedFactor, speedTwoDirections); - DecimalEncodedValue priorityEnc = new DecimalEncodedValueImpl(getKey(name, "priority"), 4, PriorityCode.getFactor(1), false); + BooleanEncodedValue accessEnc = VehicleAccess.create(name); + DecimalEncodedValue speedEnc = VehicleSpeed.create(name, speedBits, speedFactor, speedTwoDirections); + DecimalEncodedValue priorityEnc = VehiclePriority.create(name, 4, PriorityCode.getFactor(1), false); DecimalEncodedValue curvatureEnc = new DecimalEncodedValueImpl(getKey(name, "curvature"), 4, 0.1, false); DecimalEncodedValue turnCostEnc = maxTurnCosts > 0 ? TurnCost.create(name, maxTurnCosts) : null; return new VehicleEncodedValues(name, accessEnc, speedEnc, priorityEnc, curvatureEnc, turnCostEnc); @@ -125,8 +125,8 @@ public static VehicleEncodedValues roads() { double speedFactor = 2; boolean speedTwoDirections = true; int maxTurnCosts = 3; - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue(getKey(name, "access"), true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl(getKey(name, "average_speed"), speedBits, speedFactor, speedTwoDirections); + BooleanEncodedValue accessEnc = VehicleAccess.create(name); + DecimalEncodedValue speedEnc = VehicleSpeed.create(name, speedBits, speedFactor, speedTwoDirections); DecimalEncodedValue turnCostEnc = maxTurnCosts > 0 ? TurnCost.create(name, maxTurnCosts) : null; return new VehicleEncodedValues(name, accessEnc, speedEnc, null, null, turnCostEnc); } diff --git a/core/src/test/java/com/graphhopper/routing/HeadingRoutingTest.java b/core/src/test/java/com/graphhopper/routing/HeadingRoutingTest.java index a681939e525..53036f2bf65 100644 --- a/core/src/test/java/com/graphhopper/routing/HeadingRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/HeadingRoutingTest.java @@ -54,8 +54,8 @@ class HeadingRoutingTest { @Test public void headingTest1() { // Test enforce start direction - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("car", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -78,8 +78,8 @@ public void headingTest1() { @Test public void headingTest2() { // Test enforce south start direction and east end direction - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("car", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -106,8 +106,8 @@ public void headingTest2() { @Test public void headingTest3() { - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("car", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -132,8 +132,8 @@ public void headingTest3() { @Test public void headingTest4() { // Test straight via routing - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("car", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -158,8 +158,8 @@ public void headingTest4() { @Test public void headingTest5() { // Test independence of previous enforcement for subsequent paths - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("car", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -183,8 +183,8 @@ public void headingTest5() { @Test public void testHeadingWithSnapFilter() { - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("car", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraphWithTunnel(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -246,8 +246,8 @@ public void testHeadingWithSnapFilter() { @Test public void testHeadingWithSnapFilter2() { - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("car", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraphWithTunnel(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); @@ -280,8 +280,8 @@ public void testHeadingWithSnapFilter2() { @Test public void headingTest6() { // Test if snaps at tower nodes are ignored - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("car", 5, 5, false); EncodingManager encodingManager = new EncodingManager.Builder().add(accessEnc).add(speedEnc).add(Subnetwork.create("profile")).build(); BaseGraph graph = createSquareGraph(encodingManager, accessEnc, speedEnc); Router router = createRouter(graph, encodingManager); diff --git a/core/src/test/java/com/graphhopper/routing/TrafficChangeWithNodeOrderingReusingTest.java b/core/src/test/java/com/graphhopper/routing/TrafficChangeWithNodeOrderingReusingTest.java index e677b9d9149..1a66c9db238 100644 --- a/core/src/test/java/com/graphhopper/routing/TrafficChangeWithNodeOrderingReusingTest.java +++ b/core/src/test/java/com/graphhopper/routing/TrafficChangeWithNodeOrderingReusingTest.java @@ -6,8 +6,8 @@ import com.graphhopper.routing.ch.PrepareContractionHierarchies; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValueImpl; -import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; +import com.graphhopper.routing.ev.VehicleAccess; +import com.graphhopper.routing.ev.VehicleSpeed; import com.graphhopper.routing.util.CarTagParser; import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.routing.util.OSMParsers; @@ -58,8 +58,8 @@ private static class Fixture { public Fixture(int maxDeviationPercentage) { this.maxDeviationPercentage = maxDeviationPercentage; - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("car", 5, 5, false); em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); CarTagParser carParser = new CarTagParser(em, new PMap()); carParser.init(new DateRangeParser()); diff --git a/core/src/test/java/com/graphhopper/routing/ch/CHProfileSelectorTest.java b/core/src/test/java/com/graphhopper/routing/ch/CHProfileSelectorTest.java index 1c41d66bf56..9191ed0555f 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/CHProfileSelectorTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/CHProfileSelectorTest.java @@ -24,8 +24,8 @@ import com.graphhopper.routing.ProfileResolver; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValueImpl; -import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; +import com.graphhopper.routing.ev.VehicleAccess; +import com.graphhopper.routing.ev.VehicleSpeed; import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.util.PMap; import com.graphhopper.util.Parameters; @@ -57,10 +57,10 @@ public class CHProfileSelectorTest { @BeforeEach public void setup() { - BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); - BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); - DecimalEncodedValue bikeSpeedEnc = new DecimalEncodedValueImpl("bike_average_speed", 4, 2, false); + BooleanEncodedValue carAccessEnc = VehicleAccess.create("car"); + DecimalEncodedValue carSpeedEnc = VehicleSpeed.create("car", 5, 5, false); + BooleanEncodedValue bikeAccessEnc = VehicleAccess.create("bike"); + DecimalEncodedValue bikeSpeedEnc = VehicleSpeed.create("bike", 4, 2, false); encodingManager = EncodingManager.start() .add(carAccessEnc).add(carSpeedEnc) .add(bikeAccessEnc).add(bikeSpeedEnc) diff --git a/core/src/test/java/com/graphhopper/routing/lm/LMProfileSelectorTest.java b/core/src/test/java/com/graphhopper/routing/lm/LMProfileSelectorTest.java index 5dc9736e4cf..98ffb3f9e1c 100644 --- a/core/src/test/java/com/graphhopper/routing/lm/LMProfileSelectorTest.java +++ b/core/src/test/java/com/graphhopper/routing/lm/LMProfileSelectorTest.java @@ -24,8 +24,8 @@ import com.graphhopper.routing.ProfileResolver; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValueImpl; -import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; +import com.graphhopper.routing.ev.VehicleAccess; +import com.graphhopper.routing.ev.VehicleSpeed; import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.util.PMap; import com.graphhopper.util.Parameters; @@ -52,10 +52,10 @@ public class LMProfileSelectorTest { @BeforeEach public void setup() { - BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue carSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); - BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); - DecimalEncodedValue bikeSpeedEnc = new DecimalEncodedValueImpl("bike_average_speed", 4, 2, false); + BooleanEncodedValue carAccessEnc = VehicleAccess.create("car"); + DecimalEncodedValue carSpeedEnc = VehicleSpeed.create("car", 5, 5, false); + BooleanEncodedValue bikeAccessEnc = VehicleAccess.create("bike"); + DecimalEncodedValue bikeSpeedEnc = VehicleSpeed.create("bike", 4, 2, false); encodingManager = EncodingManager.start() .add(carAccessEnc).add(carSpeedEnc) .add(bikeAccessEnc).add(bikeSpeedEnc) diff --git a/core/src/test/java/com/graphhopper/routing/util/CarTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/CarTagParserTest.java index 20689d698a1..f0b1b937a4c 100644 --- a/core/src/test/java/com/graphhopper/routing/util/CarTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/CarTagParserTest.java @@ -47,12 +47,12 @@ protected String getCarName() { private EncodingManager createEncodingManager(String carName) { return new EncodingManager.Builder() - .add(new SimpleBooleanEncodedValue(carName + "_access", true)) - .add(new DecimalEncodedValueImpl(carName + "_average_speed", 5, 5, true)) + .add(VehicleAccess.create(carName)) + .add(VehicleSpeed.create(carName, 5, 5, true)) .addTurnCostEncodedValue(TurnCost.create(carName, 1)) - .add(new SimpleBooleanEncodedValue("bike_access", true)) - .add(new DecimalEncodedValueImpl("bike_average_speed", 4, 2, false)) - .add(new DecimalEncodedValueImpl("bike_priority", 4, PriorityCode.getFactor(1), false)) + .add(VehicleAccess.create("bike")) + .add(VehicleSpeed.create("bike", 4, 2, false)) + .add(VehiclePriority.create("bike", 4, PriorityCode.getFactor(1), false)) .add(new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class)) .add(new EnumEncodedValue<>(Smoothness.KEY, Smoothness.class)) .build(); diff --git a/core/src/test/java/com/graphhopper/routing/util/FootTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/FootTagParserTest.java index 8421bd6955d..1f743d54cdd 100644 --- a/core/src/test/java/com/graphhopper/routing/util/FootTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/FootTagParserTest.java @@ -35,13 +35,13 @@ * @author Peter Karich */ public class FootTagParserTest { - private final BooleanEncodedValue footAccessEnc = new SimpleBooleanEncodedValue("foot_access", true); - private final DecimalEncodedValue footAvgSpeedEnc = new DecimalEncodedValueImpl("foot_average_speed", 4, 1, false); - private final DecimalEncodedValue footPriorityEnc = new DecimalEncodedValueImpl("foot_priority", 4, PriorityCode.getFactor(1), false); - private final BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); - private final DecimalEncodedValue bikeAvgSpeedEnc = new DecimalEncodedValueImpl("bike_average_speed", 4, 2, false); - private final BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); - private final DecimalEncodedValue carAvSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + private final BooleanEncodedValue footAccessEnc = VehicleAccess.create("foot"); + private final DecimalEncodedValue footAvgSpeedEnc = VehicleSpeed.create("foot", 4, 1, false); + private final DecimalEncodedValue footPriorityEnc = VehiclePriority.create("foot", 4, PriorityCode.getFactor(1), false); + private final BooleanEncodedValue bikeAccessEnc = VehicleAccess.create("bike"); + private final DecimalEncodedValue bikeAvgSpeedEnc = VehicleSpeed.create("bike", 4, 2, false); + private final BooleanEncodedValue carAccessEnc = VehicleAccess.create("car"); + private final DecimalEncodedValue carAvSpeedEnc = VehicleSpeed.create("car", 5, 5, false); private final EncodingManager encodingManager = EncodingManager.start() .add(footAccessEnc).add(footAvgSpeedEnc).add(footPriorityEnc).add(new EnumEncodedValue<>(FootNetwork.KEY, RouteNetwork.class)) .add(bikeAccessEnc).add(bikeAvgSpeedEnc).add(new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class)) diff --git a/core/src/test/java/com/graphhopper/routing/util/MotorcycleTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/MotorcycleTagParserTest.java index 6bab8c518cb..1f67e1c3e12 100644 --- a/core/src/test/java/com/graphhopper/routing/util/MotorcycleTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/MotorcycleTagParserTest.java @@ -19,10 +19,7 @@ import com.graphhopper.reader.ReaderWay; import com.graphhopper.reader.osm.conditional.DateRangeParser; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValueImpl; -import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; +import com.graphhopper.routing.ev.*; import com.graphhopper.storage.BaseGraph; import com.graphhopper.storage.Graph; import com.graphhopper.storage.IntsRef; @@ -40,12 +37,12 @@ * @author Peter Karich */ public class MotorcycleTagParserTest { - private final BooleanEncodedValue motorcycleAccessEnc = new SimpleBooleanEncodedValue("motorcycle_access", true); - private final DecimalEncodedValue motorcycleSpeedEnc = new DecimalEncodedValueImpl("motorcycle_average_speed", 5, 5, true); - private final DecimalEncodedValue motorcyclePriorityEnc = new DecimalEncodedValueImpl("motorcycle_priority", 4, PriorityCode.getFactor(1), false); + private final BooleanEncodedValue motorcycleAccessEnc = VehicleAccess.create("motorcycle"); + private final DecimalEncodedValue motorcycleSpeedEnc = VehicleSpeed.create("motorcycle", 5, 5, true); + private final DecimalEncodedValue motorcyclePriorityEnc = VehiclePriority.create("motorcycle", 4, PriorityCode.getFactor(1), false); private final DecimalEncodedValue motorcycleCurvatureEnc = new DecimalEncodedValueImpl("motorcycle_curvature", 4, 0.1, false); - private final BooleanEncodedValue footAccessEnc = new SimpleBooleanEncodedValue("foot_access", true); - private final DecimalEncodedValue footSpeedEnc = new DecimalEncodedValueImpl("foot_speed", 4, 1, false); + private final BooleanEncodedValue footAccessEnc = VehicleAccess.create("foot"); + private final DecimalEncodedValue footSpeedEnc = VehicleSpeed.create("foot", 4, 1, false); private final EncodingManager em = EncodingManager.start() .add(motorcycleAccessEnc).add(motorcycleSpeedEnc).add(motorcyclePriorityEnc).add(motorcycleCurvatureEnc) .add(footAccessEnc).add(footSpeedEnc) diff --git a/core/src/test/java/com/graphhopper/routing/util/RacingBikeTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/RacingBikeTagParserTest.java index 347e2d6337c..71c321f7838 100644 --- a/core/src/test/java/com/graphhopper/routing/util/RacingBikeTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/RacingBikeTagParserTest.java @@ -220,9 +220,9 @@ public void testHandleWayTagsInfluencedByRelation() { public void testPriority_avoidanceOfHighMaxSpeed() { // here we test the priority that would be calculated if the way was accessible (even when it is not) // therefore we need a modified parser that always yields access=WAY - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("racingbike_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("racingbike_average_speed", 4, 2, false); - DecimalEncodedValue priorityEnc = new DecimalEncodedValueImpl("racingbike_priority", 4, PriorityCode.getValue(1), false); + BooleanEncodedValue accessEnc = VehicleAccess.create("racingbike"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("racingbike", 4, 2, false); + DecimalEncodedValue priorityEnc = VehiclePriority.create("racingbike", 4, PriorityCode.getValue(1), false); EncodingManager encodingManager = EncodingManager.start() .add(accessEnc).add(speedEnc).add(priorityEnc) .add(new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class)) diff --git a/core/src/test/java/com/graphhopper/routing/util/TagParsingTest.java b/core/src/test/java/com/graphhopper/routing/util/TagParsingTest.java index 43dfaebfa3f..47d9d8032dc 100644 --- a/core/src/test/java/com/graphhopper/routing/util/TagParsingTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/TagParsingTest.java @@ -44,12 +44,12 @@ public void testCombineRelations() { osmWay.setTag("highway", "track"); ReaderRelation osmRel = new ReaderRelation(1); - BooleanEncodedValue bike1AccessEnc = new SimpleBooleanEncodedValue("bike1_access", true); - DecimalEncodedValue bike1SpeedEnc = new DecimalEncodedValueImpl("bike1_average_speed", 4, 2, false); - DecimalEncodedValue bike1PriorityEnc = new DecimalEncodedValueImpl("bike1_priority", 4, PriorityCode.getFactor(1), false); - BooleanEncodedValue bike2AccessEnc = new SimpleBooleanEncodedValue("bike2_access", true); - DecimalEncodedValue bike2SpeedEnc = new DecimalEncodedValueImpl("bike2_average_speed", 4, 2, false); - DecimalEncodedValue bike2PriorityEnc = new DecimalEncodedValueImpl("bike2_priority", 4, PriorityCode.getFactor(1), false); + BooleanEncodedValue bike1AccessEnc = VehicleAccess.create("bike1"); + DecimalEncodedValue bike1SpeedEnc = VehicleSpeed.create("bike1", 4, 2, false); + DecimalEncodedValue bike1PriorityEnc = VehiclePriority.create("bike1", 4, PriorityCode.getFactor(1), false); + BooleanEncodedValue bike2AccessEnc = VehicleAccess.create("bike2"); + DecimalEncodedValue bike2SpeedEnc = VehicleSpeed.create("bike2", 4, 2, false); + DecimalEncodedValue bike2PriorityEnc = VehiclePriority.create("bike2", 4, PriorityCode.getFactor(1), false); EnumEncodedValue bikeNetworkEnc = new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class); EncodingManager em = EncodingManager.start() .add(bike1AccessEnc).add(bike1SpeedEnc).add(bike1PriorityEnc) @@ -94,12 +94,12 @@ public void testMixBikeTypesAndRelationCombination() { ReaderRelation osmRel = new ReaderRelation(1); - BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); - DecimalEncodedValue bikeSpeedEnc = new DecimalEncodedValueImpl("bike_average_speed", 4, 2, false); - DecimalEncodedValue bikePriorityEnc = new DecimalEncodedValueImpl("bike_priority", 4, PriorityCode.getFactor(1), false); - BooleanEncodedValue mtbAccessEnc = new SimpleBooleanEncodedValue("mtb_access", true); - DecimalEncodedValue mtbSpeedEnc = new DecimalEncodedValueImpl("mtb_average_speed", 4, 2, false); - DecimalEncodedValue mtbPriorityEnc = new DecimalEncodedValueImpl("mtb_priority", 4, PriorityCode.getFactor(1), false); + BooleanEncodedValue bikeAccessEnc = VehicleAccess.create("bike"); + DecimalEncodedValue bikeSpeedEnc = VehicleSpeed.create("bike", 4, 2, false); + DecimalEncodedValue bikePriorityEnc = VehiclePriority.create("bike", 4, PriorityCode.getFactor(1), false); + BooleanEncodedValue mtbAccessEnc = VehicleAccess.create("mtb"); + DecimalEncodedValue mtbSpeedEnc = VehicleSpeed.create("mtb", 4, 2, false); + DecimalEncodedValue mtbPriorityEnc = VehiclePriority.create("mtb", 4, PriorityCode.getFactor(1), false); EnumEncodedValue bikeNetworkEnc = new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class); EncodingManager em = EncodingManager.start() .add(bikeAccessEnc).add(bikeSpeedEnc).add(bikePriorityEnc) @@ -162,18 +162,18 @@ public void testCompatibilityBug() { @Test public void testSharedEncodedValues() { - BooleanEncodedValue carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); - BooleanEncodedValue footAccessEnc = new SimpleBooleanEncodedValue("foot_access", true); - BooleanEncodedValue bikeAccessEnc = new SimpleBooleanEncodedValue("bike_access", true); - BooleanEncodedValue motorcycleAccessEnc = new SimpleBooleanEncodedValue("motorcycle_access", true); - BooleanEncodedValue mtbAccessEnc = new SimpleBooleanEncodedValue("mtb_access", true); + BooleanEncodedValue carAccessEnc = VehicleAccess.create("car"); + BooleanEncodedValue footAccessEnc = VehicleAccess.create("foot"); + BooleanEncodedValue bikeAccessEnc = VehicleAccess.create("bike"); + BooleanEncodedValue motorcycleAccessEnc = VehicleAccess.create("motorcycle"); + BooleanEncodedValue mtbAccessEnc = VehicleAccess.create("mtb"); List accessEncs = Arrays.asList(carAccessEnc, footAccessEnc, bikeAccessEnc, motorcycleAccessEnc, mtbAccessEnc); EncodingManager manager = EncodingManager.start() - .add(carAccessEnc).add(new DecimalEncodedValueImpl("car_average_speed", 5, 5, false)) - .add(footAccessEnc).add(new DecimalEncodedValueImpl("foot_average_speed", 4, 1, true)).add(new DecimalEncodedValueImpl("foot_priority", 4, PriorityCode.getFactor(1), false)) - .add(bikeAccessEnc).add(new DecimalEncodedValueImpl("bike_average_speed", 4, 2, false)).add(new DecimalEncodedValueImpl("bike_priority", 4, PriorityCode.getFactor(1), false)) - .add(motorcycleAccessEnc).add(new DecimalEncodedValueImpl("motorcycle_average_speed", 5, 5, true)).add(new DecimalEncodedValueImpl("motorcycle_priority", 4, PriorityCode.getFactor(1), false)).add(new DecimalEncodedValueImpl("motorcycle_curvature", 5, 5, true)) - .add(mtbAccessEnc).add(new DecimalEncodedValueImpl("mtb_average_speed", 4, 2, false)).add(new DecimalEncodedValueImpl("mtb_priority", 4, PriorityCode.getFactor(1), false)) + .add(carAccessEnc).add(VehicleSpeed.create("car", 5, 5, false)) + .add(footAccessEnc).add(VehicleSpeed.create("foot", 4, 1, true)).add(VehiclePriority.create("foot", 4, PriorityCode.getFactor(1), false)) + .add(bikeAccessEnc).add(VehicleSpeed.create("bike", 4, 2, false)).add(VehiclePriority.create("bike", 4, PriorityCode.getFactor(1), false)) + .add(motorcycleAccessEnc).add(VehicleSpeed.create("motorcycle", 5, 5, true)).add(VehiclePriority.create("motorcycle", 4, PriorityCode.getFactor(1), false)).add(new DecimalEncodedValueImpl("motorcycle_curvature", 5, 5, true)) + .add(mtbAccessEnc).add(VehicleSpeed.create("mtb", 4, 2, false)).add(VehiclePriority.create("mtb", 4, PriorityCode.getFactor(1), false)) .add(new EnumEncodedValue<>(FootNetwork.KEY, RouteNetwork.class)) .add(new EnumEncodedValue<>(BikeNetwork.KEY, RouteNetwork.class)) .add(new EnumEncodedValue<>(Smoothness.KEY, Smoothness.class)) diff --git a/core/src/test/java/com/graphhopper/routing/util/WheelchairTagParserTest.java b/core/src/test/java/com/graphhopper/routing/util/WheelchairTagParserTest.java index 84fe7f94f22..9160b98ae93 100644 --- a/core/src/test/java/com/graphhopper/routing/util/WheelchairTagParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/WheelchairTagParserTest.java @@ -45,11 +45,11 @@ public class WheelchairTagParserTest { private final WheelchairTagParser wheelchairParser; public WheelchairTagParserTest() { - wheelchairAccessEnc = new SimpleBooleanEncodedValue("wheelchair_access", true); - wheelchairAvSpeedEnc = new DecimalEncodedValueImpl("wheelchair_average_speed", 4, 1, true); - wheelchairPriorityEnc = new DecimalEncodedValueImpl("wheelchair_priority", 4, PriorityCode.getFactor(1), false); - carAccessEnc = new SimpleBooleanEncodedValue("car_access", true); - carAvSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + wheelchairAccessEnc = VehicleAccess.create("wheelchair"); + wheelchairAvSpeedEnc = VehicleSpeed.create("wheelchair", 4, 1, true); + wheelchairPriorityEnc = VehiclePriority.create("wheelchair", 4, PriorityCode.getFactor(1), false); + carAccessEnc = VehicleAccess.create("car"); + carAvSpeedEnc = VehicleSpeed.create("car", 5, 5, false); encodingManager = EncodingManager.start() .add(wheelchairAccessEnc).add(wheelchairAvSpeedEnc).add(wheelchairPriorityEnc).add(new EnumEncodedValue<>(FootNetwork.KEY, RouteNetwork.class)) .add(carAccessEnc).add(carAvSpeedEnc) diff --git a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomModelParserTest.java b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomModelParserTest.java index f4437affb9c..c92a70759a4 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomModelParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomModelParserTest.java @@ -52,8 +52,8 @@ class CustomModelParserTest { @BeforeEach void setup() { - accessEnc = new SimpleBooleanEncodedValue("car_access", true); - avgSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + accessEnc = VehicleAccess.create("car"); + avgSpeedEnc = VehicleSpeed.create("car", 5, 5, false); countryEnc = new StringEncodedValue("country", 10); encodingManager = new EncodingManager.Builder().add(accessEnc).add(avgSpeedEnc) .add(countryEnc).add(MaxSpeed.create()).add(new EnumEncodedValue<>(Surface.KEY, Surface.class)).build(); diff --git a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java index 4b3c7579d2e..ccc987fa140 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/custom/CustomWeightingTest.java @@ -32,8 +32,8 @@ class CustomWeightingTest { @BeforeEach public void setup() { - accessEnc = new SimpleBooleanEncodedValue("car_access", true); - avSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, true); + accessEnc = VehicleAccess.create("car"); + avSpeedEnc = VehicleSpeed.create("car", 5, 5, true); encodingManager = new EncodingManager.Builder().add(accessEnc).add(avSpeedEnc) .add(new EnumEncodedValue<>(Toll.KEY, Toll.class)) .add(new EnumEncodedValue<>(Hazmat.KEY, Hazmat.class)) @@ -116,8 +116,8 @@ public void testSpeedFactorBooleanEV() { @Test public void testBoolean() { - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue avSpeedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue avSpeedEnc = VehicleSpeed.create("car", 5, 5, false); BooleanEncodedValue specialEnc = new SimpleBooleanEncodedValue("special", true); encodingManager = new EncodingManager.Builder().add(accessEnc).add(avSpeedEnc).add(specialEnc).build(); graph = new BaseGraph.Builder(encodingManager).create(); diff --git a/example/src/main/java/com/graphhopper/example/IsochroneExample.java b/example/src/main/java/com/graphhopper/example/IsochroneExample.java index 26677d5001d..ff339b7d839 100644 --- a/example/src/main/java/com/graphhopper/example/IsochroneExample.java +++ b/example/src/main/java/com/graphhopper/example/IsochroneExample.java @@ -3,9 +3,7 @@ import com.graphhopper.GraphHopper; import com.graphhopper.config.Profile; import com.graphhopper.isochrone.algorithm.ShortestPathTree; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.Subnetwork; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.util.DefaultSnapFilter; import com.graphhopper.routing.util.EncodingManager; @@ -21,8 +19,8 @@ public static void main(String[] args) { GraphHopper hopper = createGraphHopperInstance(relDir + "core/files/andorra.osm.pbf"); // get encoder from GraphHopper instance EncodingManager encodingManager = hopper.getEncodingManager(); - BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(EncodingManager.getKey("car", "access")); - DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey("car", "average_speed")); + BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(VehicleAccess.key("car")); + DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(VehicleSpeed.key("car")); // snap some GPS coordinates to the routing graph and build a query graph FastestWeighting weighting = new FastestWeighting(accessEnc, speedEnc); diff --git a/example/src/main/java/com/graphhopper/example/LowLevelAPIExample.java b/example/src/main/java/com/graphhopper/example/LowLevelAPIExample.java index 9e2495d1dc3..221ea352037 100644 --- a/example/src/main/java/com/graphhopper/example/LowLevelAPIExample.java +++ b/example/src/main/java/com/graphhopper/example/LowLevelAPIExample.java @@ -7,8 +7,8 @@ import com.graphhopper.routing.ch.PrepareContractionHierarchies; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValueImpl; -import com.graphhopper.routing.ev.SimpleBooleanEncodedValue; +import com.graphhopper.routing.ev.VehicleAccess; +import com.graphhopper.routing.ev.VehicleSpeed; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.routing.util.EncodingManager; @@ -37,8 +37,8 @@ public static void main(String[] args) { public static void createAndSaveGraph() { { - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("car", 5, 5, false); EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).setDir(new RAMDirectory(graphLocation, true)).create(); // Make a weighted edge between two nodes and set average speed to 50km/h @@ -62,8 +62,8 @@ public static void createAndSaveGraph() { { // Load the graph ... can be also in a different code location // note that the EncodingManager must be the same - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("car", 5, 5, false); EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); BaseGraph graph = new BaseGraph.Builder(em).setDir(new RAMDirectory(graphLocation, true)).build(); graph.loadExisting(); @@ -89,8 +89,8 @@ public static void createAndSaveGraph() { public static void useContractionHierarchiesToMakeQueriesFaster() { // Creating and saving the graph - BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue("car_access", true); - DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl("car_average_speed", 5, 5, false); + BooleanEncodedValue accessEnc = VehicleAccess.create("car"); + DecimalEncodedValue speedEnc = VehicleSpeed.create("car", 5, 5, false); EncodingManager em = EncodingManager.start().add(accessEnc).add(speedEnc).build(); Weighting weighting = new FastestWeighting(accessEnc, speedEnc); CHConfig chConfig = CHConfig.nodeBased("my_profile", weighting); diff --git a/reader-gtfs/src/main/java/com/graphhopper/gtfs/GtfsReader.java b/reader-gtfs/src/main/java/com/graphhopper/gtfs/GtfsReader.java index bc47dac7147..cca17ef1f23 100644 --- a/reader-gtfs/src/main/java/com/graphhopper/gtfs/GtfsReader.java +++ b/reader-gtfs/src/main/java/com/graphhopper/gtfs/GtfsReader.java @@ -22,9 +22,7 @@ import com.conveyal.gtfs.model.*; import com.google.common.collect.HashMultimap; import com.google.transit.realtime.GtfsRealtime; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.Subnetwork; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.DefaultSnapFilter; import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.routing.util.EncodingManager; @@ -105,8 +103,8 @@ static class TripWithStopTimes { } void connectStopsToStreetNetwork() { - BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(EncodingManager.getKey("foot", "access")); - DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey("foot", "average_speed")); + BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(VehicleAccess.key("foot")); + DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(VehicleSpeed.key("foot")); final EdgeFilter filter = new DefaultSnapFilter(new FastestWeighting(accessEnc, speedEnc), encodingManager.getBooleanEncodedValue(Subnetwork.key("foot"))); for (Stop stop : feed.stops.values()) { if (stop.location_type == 0) { // Only stops. Not interested in parent stations for now. diff --git a/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterFreeWalkImpl.java b/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterFreeWalkImpl.java index d8790875909..8ca431599e3 100644 --- a/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterFreeWalkImpl.java +++ b/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterFreeWalkImpl.java @@ -27,6 +27,8 @@ import com.graphhopper.routing.DefaultWeightingFactory; import com.graphhopper.routing.WeightingFactory; import com.graphhopper.routing.ev.Subnetwork; +import com.graphhopper.routing.ev.VehicleAccess; +import com.graphhopper.routing.ev.VehicleSpeed; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.util.DefaultSnapFilter; import com.graphhopper.routing.util.EdgeFilter; @@ -65,8 +67,8 @@ public PtRouterFreeWalkImpl(GraphHopperConfig config, TranslationMap translation this.config = config; this.weightingFactory = new DefaultWeightingFactory(baseGraph.getBaseGraph(), encodingManager); this.accessEgressWeighting = new FastestWeighting( - encodingManager.getBooleanEncodedValue(EncodingManager.getKey("foot", "access")), - encodingManager.getDecimalEncodedValue(EncodingManager.getKey("foot", "average_speed")) + encodingManager.getBooleanEncodedValue(VehicleAccess.key("foot")), + encodingManager.getDecimalEncodedValue(VehicleSpeed.key("foot")) ); this.translationMap = translationMap; this.baseGraph = baseGraph; @@ -170,14 +172,14 @@ private class RequestHandler { accessProfile = config.getProfiles().stream().filter(p -> p.getName().equals(request.getAccessProfile())).findFirst().get(); accessWeighting = weightingFactory.createWeighting(accessProfile, new PMap(), false); accessSnapFilter = new DefaultSnapFilter(new FastestWeighting( - encodingManager.getBooleanEncodedValue(EncodingManager.getKey(accessProfile.getVehicle(), "access")), - encodingManager.getDecimalEncodedValue(EncodingManager.getKey(accessProfile.getVehicle(), "average_speed")) + encodingManager.getBooleanEncodedValue(VehicleAccess.key(accessProfile.getVehicle())), + encodingManager.getDecimalEncodedValue(VehicleSpeed.key(accessProfile.getVehicle())) ), encodingManager.getBooleanEncodedValue(Subnetwork.key(accessProfile.getVehicle()))); egressProfile = config.getProfiles().stream().filter(p -> p.getName().equals(request.getEgressProfile())).findFirst().get(); egressWeighting = weightingFactory.createWeighting(egressProfile, new PMap(), false); egressSnapFilter = new DefaultSnapFilter(new FastestWeighting( - encodingManager.getBooleanEncodedValue(EncodingManager.getKey(egressProfile.getVehicle(), "access")), - encodingManager.getDecimalEncodedValue(EncodingManager.getKey(egressProfile.getVehicle(), "average_speed")) + encodingManager.getBooleanEncodedValue(VehicleAccess.key(egressProfile.getVehicle())), + encodingManager.getDecimalEncodedValue(VehicleSpeed.key(egressProfile.getVehicle())) ), encodingManager.getBooleanEncodedValue(Subnetwork.key(egressProfile.getVehicle()))); } diff --git a/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterImpl.java b/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterImpl.java index 4ea6123b9f5..be9c6094b74 100644 --- a/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterImpl.java +++ b/reader-gtfs/src/main/java/com/graphhopper/gtfs/PtRouterImpl.java @@ -27,6 +27,8 @@ import com.graphhopper.routing.DefaultWeightingFactory; import com.graphhopper.routing.WeightingFactory; import com.graphhopper.routing.ev.Subnetwork; +import com.graphhopper.routing.ev.VehicleAccess; +import com.graphhopper.routing.ev.VehicleSpeed; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.util.DefaultSnapFilter; import com.graphhopper.routing.util.EdgeFilter; @@ -167,14 +169,14 @@ private class RequestHandler { accessProfile = config.getProfiles().stream().filter(p -> p.getName().equals(request.getAccessProfile())).findFirst().get(); accessWeighting = weightingFactory.createWeighting(accessProfile, new PMap(), false); accessSnapFilter = new DefaultSnapFilter(new FastestWeighting( - encodingManager.getBooleanEncodedValue(EncodingManager.getKey(accessProfile.getVehicle(), "access")), - encodingManager.getDecimalEncodedValue(EncodingManager.getKey(accessProfile.getVehicle(), "average_speed")) + encodingManager.getBooleanEncodedValue(VehicleAccess.key(accessProfile.getVehicle())), + encodingManager.getDecimalEncodedValue(VehicleSpeed.key(accessProfile.getVehicle())) ), encodingManager.getBooleanEncodedValue(Subnetwork.key(accessProfile.getVehicle()))); egressProfile = config.getProfiles().stream().filter(p -> p.getName().equals(request.getEgressProfile())).findFirst().get(); egressWeighting = weightingFactory.createWeighting(egressProfile, new PMap(), false); egressSnapFilter = new DefaultSnapFilter(new FastestWeighting( - encodingManager.getBooleanEncodedValue(EncodingManager.getKey(egressProfile.getVehicle(), "access")), - encodingManager.getDecimalEncodedValue(EncodingManager.getKey(egressProfile.getVehicle(), "average_speed")) + encodingManager.getBooleanEncodedValue(VehicleAccess.key(egressProfile.getVehicle())), + encodingManager.getDecimalEncodedValue(VehicleSpeed.key(egressProfile.getVehicle())) ), encodingManager.getBooleanEncodedValue(Subnetwork.key(egressProfile.getVehicle()))); } diff --git a/tools/src/main/java/com/graphhopper/tools/Measurement.java b/tools/src/main/java/com/graphhopper/tools/Measurement.java index 9afd72e8eba..01caf87902f 100644 --- a/tools/src/main/java/com/graphhopper/tools/Measurement.java +++ b/tools/src/main/java/com/graphhopper/tools/Measurement.java @@ -31,6 +31,7 @@ import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.Subnetwork; import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.VehicleAccess; import com.graphhopper.routing.lm.LMConfig; import com.graphhopper.routing.lm.PrepareLandmarks; import com.graphhopper.routing.util.*; @@ -179,7 +180,7 @@ protected void importOSM() { BaseGraph g = hopper.getBaseGraph(); EncodingManager encodingManager = hopper.getEncodingManager(); - BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(EncodingManager.getKey(vehicle, "access")); + BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(VehicleAccess.key(vehicle)); boolean withTurnCosts = encodingManager.hasEncodedValue(TurnCost.key(vehicle)); StopWatch sw = new StopWatch().start(); diff --git a/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java b/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java index 4fc678efab8..06fb256a847 100644 --- a/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java +++ b/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java @@ -28,13 +28,14 @@ import com.graphhopper.routing.*; import com.graphhopper.routing.ev.BooleanEncodedValue; import com.graphhopper.routing.ev.DecimalEncodedValue; +import com.graphhopper.routing.ev.VehicleAccess; +import com.graphhopper.routing.ev.VehicleSpeed; import com.graphhopper.routing.lm.LMRoutingAlgorithmFactory; import com.graphhopper.routing.lm.LandmarkStorage; import com.graphhopper.routing.querygraph.QueryGraph; import com.graphhopper.routing.querygraph.QueryRoutingCHGraph; import com.graphhopper.routing.util.AllEdgesIterator; import com.graphhopper.routing.util.EdgeFilter; -import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.storage.BaseGraph; @@ -120,8 +121,8 @@ public MiniGraphUI(GraphHopper hopper, boolean debug, boolean useCH) { this.graph = hopper.getBaseGraph(); this.na = graph.getNodeAccess(); String vehicle = hopper.getProfiles().get(0).getVehicle(); - avSpeedEnc = hopper.getEncodingManager().getDecimalEncodedValue(EncodingManager.getKey(vehicle, "average_speed")); - accessEnc = hopper.getEncodingManager().getBooleanEncodedValue(EncodingManager.getKey(vehicle, "access")); + accessEnc = hopper.getEncodingManager().getBooleanEncodedValue(VehicleAccess.key(vehicle)); + avSpeedEnc = hopper.getEncodingManager().getDecimalEncodedValue(VehicleSpeed.key(vehicle)); this.useCH = useCH; logger.info("locations:" + graph.getNodes() + ", debug:" + debug); diff --git a/web-bundle/src/main/java/com/graphhopper/resources/PtIsochroneResource.java b/web-bundle/src/main/java/com/graphhopper/resources/PtIsochroneResource.java index 28d228bf628..ec079d52ec0 100644 --- a/web-bundle/src/main/java/com/graphhopper/resources/PtIsochroneResource.java +++ b/web-bundle/src/main/java/com/graphhopper/resources/PtIsochroneResource.java @@ -25,9 +25,7 @@ import com.graphhopper.isochrone.algorithm.ContourBuilder; import com.graphhopper.isochrone.algorithm.ReadableTriangulation; import com.graphhopper.jackson.ResponsePathSerializer; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.Subnetwork; +import com.graphhopper.routing.ev.*; import com.graphhopper.routing.util.DefaultSnapFilter; import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.routing.weighting.FastestWeighting; @@ -95,8 +93,8 @@ public Response doGet( double targetZ = seconds * 1000; GeometryFactory geometryFactory = new GeometryFactory(); - BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(EncodingManager.getKey("foot", "access")); - DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey("foot", "average_speed")); + BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(VehicleAccess.key("foot")); + DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(VehicleSpeed.key("foot")); final Weighting weighting = new FastestWeighting(accessEnc, speedEnc); DefaultSnapFilter snapFilter = new DefaultSnapFilter(weighting, encodingManager.getBooleanEncodedValue(Subnetwork.key("foot"))); From e6238da664e257a42baf1d47dc3c0862e60d0fc2 Mon Sep 17 00:00:00 2001 From: easbar Date: Thu, 30 Jun 2022 17:33:25 +0200 Subject: [PATCH 22/28] tmp --- .../com/graphhopper/routing/util/Bike2WeightTagParser.java | 7 +++---- .../java/com/graphhopper/routing/util/BikeTagParser.java | 7 +++---- .../java/com/graphhopper/routing/util/Car4WDTagParser.java | 5 ++--- .../java/com/graphhopper/routing/util/CarTagParser.java | 7 +++---- .../java/com/graphhopper/routing/util/EncodingManager.java | 2 +- .../java/com/graphhopper/routing/util/FootTagParser.java | 7 +++---- .../java/com/graphhopper/routing/util/HikeTagParser.java | 7 +++---- 7 files changed, 18 insertions(+), 24 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/util/Bike2WeightTagParser.java b/core/src/main/java/com/graphhopper/routing/util/Bike2WeightTagParser.java index 3fd0b4311d7..6bc34b1f49e 100644 --- a/core/src/main/java/com/graphhopper/routing/util/Bike2WeightTagParser.java +++ b/core/src/main/java/com/graphhopper/routing/util/Bike2WeightTagParser.java @@ -25,7 +25,6 @@ import com.graphhopper.util.PMap; import com.graphhopper.util.PointList; -import static com.graphhopper.routing.util.EncodingManager.getKey; import static com.graphhopper.util.Helper.keepIn; /** @@ -37,9 +36,9 @@ public class Bike2WeightTagParser extends BikeTagParser { public Bike2WeightTagParser(EncodedValueLookup lookup, PMap properties) { this( - lookup.getBooleanEncodedValue(getKey(properties.getString("name", "bike2"), "access")), - lookup.getDecimalEncodedValue(getKey(properties.getString("name", "bike2"), "average_speed")), - lookup.getDecimalEncodedValue(getKey(properties.getString("name", "bike2"), "priority")), + lookup.getBooleanEncodedValue(VehicleAccess.key(properties.getString("name", "bike2"))), + lookup.getDecimalEncodedValue(VehicleSpeed.key(properties.getString("name", "bike2"))), + lookup.getDecimalEncodedValue(VehiclePriority.key(properties.getString("name", "bike2"))), lookup.getEnumEncodedValue(BikeNetwork.KEY, RouteNetwork.class), lookup.getBooleanEncodedValue(Roundabout.KEY), lookup.getEnumEncodedValue(Smoothness.KEY, Smoothness.class), diff --git a/core/src/main/java/com/graphhopper/routing/util/BikeTagParser.java b/core/src/main/java/com/graphhopper/routing/util/BikeTagParser.java index 082b1a3bef3..accd01b3c85 100644 --- a/core/src/main/java/com/graphhopper/routing/util/BikeTagParser.java +++ b/core/src/main/java/com/graphhopper/routing/util/BikeTagParser.java @@ -21,7 +21,6 @@ import com.graphhopper.util.PMap; import static com.graphhopper.routing.ev.Smoothness.*; -import static com.graphhopper.routing.util.EncodingManager.getKey; /** * Specifies the settings for cycletouring/trekking @@ -33,9 +32,9 @@ public class BikeTagParser extends BikeCommonTagParser { public BikeTagParser(EncodedValueLookup lookup, PMap properties) { this( - lookup.getBooleanEncodedValue(getKey(properties.getString("name", "bike"), "access")), - lookup.getDecimalEncodedValue(getKey(properties.getString("name", "bike"), "average_speed")), - lookup.getDecimalEncodedValue(getKey(properties.getString("name", "bike"), "priority")), + lookup.getBooleanEncodedValue(VehicleAccess.key(properties.getString("name", "bike"))), + lookup.getDecimalEncodedValue(VehicleSpeed.key(properties.getString("name", "bike"))), + lookup.getDecimalEncodedValue(VehiclePriority.key(properties.getString("name", "bike"))), lookup.getEnumEncodedValue(BikeNetwork.KEY, RouteNetwork.class), lookup.getEnumEncodedValue(Smoothness.KEY, Smoothness.class), properties.getString("name", "bike"), diff --git a/core/src/main/java/com/graphhopper/routing/util/Car4WDTagParser.java b/core/src/main/java/com/graphhopper/routing/util/Car4WDTagParser.java index fcf81b82915..79ed0dd4b19 100644 --- a/core/src/main/java/com/graphhopper/routing/util/Car4WDTagParser.java +++ b/core/src/main/java/com/graphhopper/routing/util/Car4WDTagParser.java @@ -20,7 +20,6 @@ import com.graphhopper.routing.ev.*; import com.graphhopper.util.PMap; -import static com.graphhopper.routing.util.EncodingManager.getKey; /** * Defines bit layout for cars with four wheel drive @@ -31,8 +30,8 @@ public class Car4WDTagParser extends CarTagParser { public Car4WDTagParser(EncodedValueLookup lookup, PMap properties) { this( - lookup.getBooleanEncodedValue(getKey(properties.getString("name", "car4wd"), "access")), - lookup.getDecimalEncodedValue(getKey(properties.getString("name", "car4wd"), "average_speed")), + lookup.getBooleanEncodedValue(VehicleAccess.key(properties.getString("name", "car4wd"))), + lookup.getDecimalEncodedValue(VehicleSpeed.key(properties.getString("name", "car4wd"))), lookup.hasEncodedValue(TurnCost.key(properties.getString("name", "car4wd"))) ? lookup.getDecimalEncodedValue(TurnCost.key(properties.getString("name", "car4wd"))) : null, lookup.getBooleanEncodedValue(Roundabout.KEY), new PMap(properties).putObject("name", "car4wd"), diff --git a/core/src/main/java/com/graphhopper/routing/util/CarTagParser.java b/core/src/main/java/com/graphhopper/routing/util/CarTagParser.java index 4f3ea5654cf..77babb47be6 100644 --- a/core/src/main/java/com/graphhopper/routing/util/CarTagParser.java +++ b/core/src/main/java/com/graphhopper/routing/util/CarTagParser.java @@ -28,7 +28,6 @@ import java.util.Map; import java.util.Set; -import static com.graphhopper.routing.util.EncodingManager.getKey; /** * Defines bit layout for cars. (speed, access, ferries, ...) @@ -52,13 +51,13 @@ public class CarTagParser extends VehicleTagParser { public CarTagParser(EncodedValueLookup lookup, PMap properties) { this( - lookup.getBooleanEncodedValue(getKey(properties.getString("name", "car"), "access")), - lookup.getDecimalEncodedValue(getKey(properties.getString("name", "car"), "average_speed")), + lookup.getBooleanEncodedValue(VehicleAccess.key(properties.getString("name", "car"))), + lookup.getDecimalEncodedValue(VehicleSpeed.key(properties.getString("name", "car"))), lookup.hasEncodedValue(TurnCost.key(properties.getString("name", "car"))) ? lookup.getDecimalEncodedValue(TurnCost.key(properties.getString("name", "car"))) : null, lookup.getBooleanEncodedValue(Roundabout.KEY), properties, TransportationMode.CAR, - lookup.getDecimalEncodedValue(getKey(properties.getString("name", "car"), "average_speed")).getNextStorableValue(CAR_MAX_SPEED) + lookup.getDecimalEncodedValue(VehicleSpeed.key(properties.getString("name", "car"))).getNextStorableValue(CAR_MAX_SPEED) ); } diff --git a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java index b80a5c41fa5..73eb3292b45 100644 --- a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java +++ b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java @@ -190,7 +190,7 @@ public List getVehicles() { return getEncodedValues().stream() .filter(ev -> ev.getName().endsWith("_access")) .map(ev -> ev.getName().replaceAll("_access", "")) - .filter(v -> hasEncodedValue(EncodingManager.getKey(v, "average_speed"))) + .filter(v -> hasEncodedValue(VehicleSpeed.key(v))) .collect(Collectors.toList()); } diff --git a/core/src/main/java/com/graphhopper/routing/util/FootTagParser.java b/core/src/main/java/com/graphhopper/routing/util/FootTagParser.java index 5aee1625660..d3fb6218be3 100644 --- a/core/src/main/java/com/graphhopper/routing/util/FootTagParser.java +++ b/core/src/main/java/com/graphhopper/routing/util/FootTagParser.java @@ -25,7 +25,6 @@ import java.util.*; import static com.graphhopper.routing.ev.RouteNetwork.*; -import static com.graphhopper.routing.util.EncodingManager.getKey; import static com.graphhopper.routing.util.PriorityCode.*; /** @@ -55,9 +54,9 @@ public class FootTagParser extends VehicleTagParser { public FootTagParser(EncodedValueLookup lookup, PMap properties) { this( - lookup.getBooleanEncodedValue(getKey(properties.getString("name", "foot"), "access")), - lookup.getDecimalEncodedValue(getKey(properties.getString("name", "foot"), "average_speed")), - lookup.getDecimalEncodedValue(getKey(properties.getString("name", "foot"), "priority")), + lookup.getBooleanEncodedValue(VehicleAccess.key(properties.getString("name", "foot"))), + lookup.getDecimalEncodedValue(VehicleSpeed.key(properties.getString("name", "foot"))), + lookup.getDecimalEncodedValue(VehiclePriority.key(properties.getString("name", "foot"))), lookup.getEnumEncodedValue(FootNetwork.KEY, RouteNetwork.class), "foot" ); diff --git a/core/src/main/java/com/graphhopper/routing/util/HikeTagParser.java b/core/src/main/java/com/graphhopper/routing/util/HikeTagParser.java index 75c0ff9817b..87eb5285fd1 100644 --- a/core/src/main/java/com/graphhopper/routing/util/HikeTagParser.java +++ b/core/src/main/java/com/graphhopper/routing/util/HikeTagParser.java @@ -25,7 +25,6 @@ import java.util.TreeMap; import static com.graphhopper.routing.ev.RouteNetwork.*; -import static com.graphhopper.routing.util.EncodingManager.getKey; import static com.graphhopper.routing.util.PriorityCode.*; /** @@ -37,9 +36,9 @@ public class HikeTagParser extends FootTagParser { public HikeTagParser(EncodedValueLookup lookup, PMap properties) { this( - lookup.getBooleanEncodedValue(getKey(properties.getString("name", "hike"), "access")), - lookup.getDecimalEncodedValue(getKey(properties.getString("name", "hike"), "average_speed")), - lookup.getDecimalEncodedValue(getKey(properties.getString("name", "hike"), "priority")), + lookup.getBooleanEncodedValue(VehicleAccess.key(properties.getString("name", "hike"))), + lookup.getDecimalEncodedValue(VehicleSpeed.key(properties.getString("name", "hike"))), + lookup.getDecimalEncodedValue(VehiclePriority.key(properties.getString("name", "hike"))), lookup.getEnumEncodedValue(FootNetwork.KEY, RouteNetwork.class), properties.getString("name", "hike") ); From c78a1b047cf32294d40ed1c29841df2db1f084c2 Mon Sep 17 00:00:00 2001 From: easbar Date: Thu, 30 Jun 2022 17:38:52 +0200 Subject: [PATCH 23/28] use keys --- .../graphhopper/routing/util/MotorcycleTagParser.java | 6 +++--- .../routing/util/MountainBikeTagParser.java | 7 +++---- .../graphhopper/routing/util/RacingBikeTagParser.java | 7 +++---- .../com/graphhopper/routing/util/RoadsTagParser.java | 10 +++------- .../graphhopper/routing/util/WheelchairTagParser.java | 7 +++---- .../java/com/graphhopper/reader/osm/OSMReaderTest.java | 4 ++-- .../routing/util/AbstractBikeTagParserTester.java | 6 +++--- .../com/graphhopper/routing/util/TagParsingTest.java | 5 ++--- 8 files changed, 22 insertions(+), 30 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/util/MotorcycleTagParser.java b/core/src/main/java/com/graphhopper/routing/util/MotorcycleTagParser.java index a154da15b30..9c3e872883f 100644 --- a/core/src/main/java/com/graphhopper/routing/util/MotorcycleTagParser.java +++ b/core/src/main/java/com/graphhopper/routing/util/MotorcycleTagParser.java @@ -46,11 +46,11 @@ public class MotorcycleTagParser extends CarTagParser { public MotorcycleTagParser(EncodedValueLookup lookup, PMap properties) { this( - lookup.getBooleanEncodedValue(getKey("motorcycle", "access")), - lookup.getDecimalEncodedValue(getKey("motorcycle", "average_speed")), + lookup.getBooleanEncodedValue(VehicleAccess.key("motorcycle")), + lookup.getDecimalEncodedValue(VehicleSpeed.key("motorcycle")), lookup.hasEncodedValue(TurnCost.key("motorcycle")) ? lookup.getDecimalEncodedValue(TurnCost.key("motorcycle")) : null, lookup.getBooleanEncodedValue(Roundabout.KEY), - lookup.getDecimalEncodedValue(getKey("motorcycle", "priority")), + lookup.getDecimalEncodedValue(VehiclePriority.key("motorcycle")), lookup.getDecimalEncodedValue(getKey("motorcycle", "curvature")), new PMap(properties).putObject("name", "motorcycle"), TransportationMode.MOTORCYCLE diff --git a/core/src/main/java/com/graphhopper/routing/util/MountainBikeTagParser.java b/core/src/main/java/com/graphhopper/routing/util/MountainBikeTagParser.java index b02fc0e1f65..467d64a658d 100644 --- a/core/src/main/java/com/graphhopper/routing/util/MountainBikeTagParser.java +++ b/core/src/main/java/com/graphhopper/routing/util/MountainBikeTagParser.java @@ -24,7 +24,6 @@ import java.util.TreeMap; import static com.graphhopper.routing.ev.RouteNetwork.*; -import static com.graphhopper.routing.util.EncodingManager.getKey; import static com.graphhopper.routing.util.PriorityCode.*; /** @@ -38,9 +37,9 @@ public class MountainBikeTagParser extends BikeCommonTagParser { public MountainBikeTagParser(EncodedValueLookup lookup, PMap properties) { this( - lookup.getBooleanEncodedValue(getKey("mtb", "access")), - lookup.getDecimalEncodedValue(getKey("mtb", "average_speed")), - lookup.getDecimalEncodedValue(getKey("mtb", "priority")), + lookup.getBooleanEncodedValue(VehicleAccess.key("mtb")), + lookup.getDecimalEncodedValue(VehicleSpeed.key("mtb")), + lookup.getDecimalEncodedValue(VehiclePriority.key("mtb")), lookup.getEnumEncodedValue(BikeNetwork.KEY, RouteNetwork.class), lookup.getEnumEncodedValue(Smoothness.KEY, Smoothness.class), lookup.getBooleanEncodedValue(Roundabout.KEY), diff --git a/core/src/main/java/com/graphhopper/routing/util/RacingBikeTagParser.java b/core/src/main/java/com/graphhopper/routing/util/RacingBikeTagParser.java index c94a578f260..374650b0182 100644 --- a/core/src/main/java/com/graphhopper/routing/util/RacingBikeTagParser.java +++ b/core/src/main/java/com/graphhopper/routing/util/RacingBikeTagParser.java @@ -24,7 +24,6 @@ import java.util.TreeMap; import static com.graphhopper.routing.ev.RouteNetwork.*; -import static com.graphhopper.routing.util.EncodingManager.getKey; import static com.graphhopper.routing.util.PriorityCode.*; /** @@ -37,9 +36,9 @@ public class RacingBikeTagParser extends BikeCommonTagParser { public RacingBikeTagParser(EncodedValueLookup lookup, PMap properties) { this( - lookup.getBooleanEncodedValue(getKey("racingbike", "access")), - lookup.getDecimalEncodedValue(getKey("racingbike", "average_speed")), - lookup.getDecimalEncodedValue(getKey("racingbike", "priority")), + lookup.getBooleanEncodedValue(VehicleAccess.key("racingbike")), + lookup.getDecimalEncodedValue(VehicleSpeed.key("racingbike")), + lookup.getDecimalEncodedValue(VehiclePriority.key("racingbike")), lookup.getEnumEncodedValue(BikeNetwork.KEY, RouteNetwork.class), lookup.getEnumEncodedValue(Smoothness.KEY, Smoothness.class), lookup.getBooleanEncodedValue(Roundabout.KEY), diff --git a/core/src/main/java/com/graphhopper/routing/util/RoadsTagParser.java b/core/src/main/java/com/graphhopper/routing/util/RoadsTagParser.java index f556c0264ba..94db9124c11 100644 --- a/core/src/main/java/com/graphhopper/routing/util/RoadsTagParser.java +++ b/core/src/main/java/com/graphhopper/routing/util/RoadsTagParser.java @@ -1,21 +1,17 @@ package com.graphhopper.routing.util; import com.graphhopper.reader.ReaderWay; -import com.graphhopper.routing.ev.BooleanEncodedValue; -import com.graphhopper.routing.ev.DecimalEncodedValue; -import com.graphhopper.routing.ev.EncodedValueLookup; -import com.graphhopper.routing.ev.TurnCost; +import com.graphhopper.routing.ev.*; import com.graphhopper.storage.IntsRef; -import static com.graphhopper.routing.util.EncodingManager.getKey; public class RoadsTagParser extends VehicleTagParser { public static final double ROADS_MAX_SPEED = 254; public RoadsTagParser(EncodedValueLookup lookup) { this( - lookup.getBooleanEncodedValue(getKey("roads", "access")), - lookup.getDecimalEncodedValue(getKey("roads", "average_speed")), + lookup.getBooleanEncodedValue(VehicleAccess.key("roads")), + lookup.getDecimalEncodedValue(VehicleSpeed.key("roads")), lookup.getDecimalEncodedValue(TurnCost.key("roads")) ); } diff --git a/core/src/main/java/com/graphhopper/routing/util/WheelchairTagParser.java b/core/src/main/java/com/graphhopper/routing/util/WheelchairTagParser.java index 1cc04a9b63d..808f6565b73 100644 --- a/core/src/main/java/com/graphhopper/routing/util/WheelchairTagParser.java +++ b/core/src/main/java/com/graphhopper/routing/util/WheelchairTagParser.java @@ -29,7 +29,6 @@ import java.util.Set; import java.util.TreeMap; -import static com.graphhopper.routing.util.EncodingManager.getKey; import static com.graphhopper.routing.util.PriorityCode.AVOID; import static com.graphhopper.routing.util.PriorityCode.VERY_NICE; @@ -45,9 +44,9 @@ public class WheelchairTagParser extends FootTagParser { public WheelchairTagParser(EncodedValueLookup lookup, PMap properties) { this( - lookup.getBooleanEncodedValue(getKey("wheelchair", "access")), - lookup.getDecimalEncodedValue(getKey("wheelchair", "average_speed")), - lookup.getDecimalEncodedValue(getKey("wheelchair", "priority")), + lookup.getBooleanEncodedValue(VehicleAccess.key("wheelchair")), + lookup.getDecimalEncodedValue(VehicleSpeed.key("wheelchair")), + lookup.getDecimalEncodedValue(VehiclePriority.key("wheelchair")), lookup.getEnumEncodedValue(FootNetwork.KEY, RouteNetwork.class) ); blockPrivate(properties.getBool("block_private", true)); diff --git a/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java b/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java index d097ef4deca..2fc56ae86dd 100644 --- a/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java +++ b/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java @@ -676,8 +676,8 @@ public void testTurnFlagCombination() { hopper.setVehicleTagParserFactory((lookup, name, config) -> { if (name.equals("truck")) { return new CarTagParser( - lookup.getBooleanEncodedValue(EncodingManager.getKey("truck", "access")), - lookup.getDecimalEncodedValue(EncodingManager.getKey("truck", "average_speed")), + lookup.getBooleanEncodedValue(VehicleAccess.key("truck")), + lookup.getDecimalEncodedValue(VehicleSpeed.key("truck")), lookup.hasEncodedValue(TurnCost.key("truck")) ? lookup.getDecimalEncodedValue(TurnCost.key("truck")) : null, lookup.getBooleanEncodedValue(Roundabout.KEY), config, diff --git a/core/src/test/java/com/graphhopper/routing/util/AbstractBikeTagParserTester.java b/core/src/test/java/com/graphhopper/routing/util/AbstractBikeTagParserTester.java index 347a8773a1a..6a7b12f7261 100644 --- a/core/src/test/java/com/graphhopper/routing/util/AbstractBikeTagParserTester.java +++ b/core/src/test/java/com/graphhopper/routing/util/AbstractBikeTagParserTester.java @@ -51,7 +51,7 @@ public void setUp() { parser = createBikeTagParser(encodingManager, new PMap("block_fords=true")); osmParsers = createOSMParsers(parser, encodingManager); roundaboutEnc = encodingManager.getBooleanEncodedValue(Roundabout.KEY); - priorityEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey(parser.getName(), "priority")); + priorityEnc = encodingManager.getDecimalEncodedValue(VehiclePriority.key(parser.getName())); avgSpeedEnc = parser.getAverageSpeedEnc(); } @@ -76,7 +76,7 @@ protected void assertPriorityAndSpeed(int expectedPrio, double expectedSpeed, Re IntsRef relFlags = osmParsers.handleRelationTags(rel, osmParsers.createRelationFlags()); IntsRef edgeFlags = encodingManager.createEdgeFlags(); edgeFlags = osmParsers.handleWayTags(edgeFlags, way, relFlags); - DecimalEncodedValue enc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey(parser.toString(), "priority")); + DecimalEncodedValue enc = encodingManager.getDecimalEncodedValue(VehiclePriority.key(parser.toString())); assertEquals(PriorityCode.getValue(expectedPrio), enc.getDecimal(false, edgeFlags), 0.01); assertEquals(expectedSpeed, parser.getAverageSpeedEnc().getDecimal(false, edgeFlags), 0.1); assertEquals(expectedSpeed, parser.getAverageSpeedEnc().getDecimal(true, edgeFlags), 0.1); @@ -365,7 +365,7 @@ public void testHandleWayTagsCallsHandlePriority() { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "cycleway"); IntsRef edgeFlags = parser.handleWayTags(encodingManager.createEdgeFlags(), osmWay); - DecimalEncodedValue priorityEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey(parser.getName(), "priority")); + DecimalEncodedValue priorityEnc = encodingManager.getDecimalEncodedValue(VehiclePriority.key(parser.getName())); assertEquals(PriorityCode.getValue(VERY_NICE.getValue()), priorityEnc.getDecimal(false, edgeFlags), 1e-3); } diff --git a/core/src/test/java/com/graphhopper/routing/util/TagParsingTest.java b/core/src/test/java/com/graphhopper/routing/util/TagParsingTest.java index 47d9d8032dc..78636f71f89 100644 --- a/core/src/test/java/com/graphhopper/routing/util/TagParsingTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/TagParsingTest.java @@ -33,7 +33,6 @@ import java.util.Arrays; import java.util.List; -import static com.graphhopper.routing.util.EncodingManager.getKey; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -151,11 +150,11 @@ public void testCompatibilityBug() { flags = footParser.handleWayTags(manager.createEdgeFlags(), osmWay); flags = bikeParser.handleWayTags(flags, osmWay); - DecimalEncodedValue bikeSpeedEnc = manager.getDecimalEncodedValue(getKey("bike2", "average_speed")); + DecimalEncodedValue bikeSpeedEnc = manager.getDecimalEncodedValue(VehicleSpeed.key("bike2")); assertEquals(singleSpeed, bikeSpeedEnc.getDecimal(false, flags), 1e-2); assertEquals(singleSpeed, bikeSpeedEnc.getDecimal(true, flags), 1e-2); - DecimalEncodedValue footSpeedEnc = manager.getDecimalEncodedValue(getKey("foot", "average_speed")); + DecimalEncodedValue footSpeedEnc = manager.getDecimalEncodedValue(VehicleSpeed.key("foot")); assertEquals(5, footSpeedEnc.getDecimal(false, flags), 1e-2); assertEquals(5, footSpeedEnc.getDecimal(true, flags), 1e-2); } From b42f9921390f341beac98f10a5161d460f123542 Mon Sep 17 00:00:00 2001 From: easbar Date: Fri, 1 Jul 2022 09:03:07 +0200 Subject: [PATCH 24/28] Remove car/bike_access evs from DefaultEncodedValueFactory --- .../graphhopper/routing/ev/DefaultEncodedValueFactory.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/ev/DefaultEncodedValueFactory.java b/core/src/main/java/com/graphhopper/routing/ev/DefaultEncodedValueFactory.java index 3d91cf6357d..5cb66e4c713 100644 --- a/core/src/main/java/com/graphhopper/routing/ev/DefaultEncodedValueFactory.java +++ b/core/src/main/java/com/graphhopper/routing/ev/DefaultEncodedValueFactory.java @@ -32,10 +32,6 @@ public EncodedValue create(String string) { if (Roundabout.KEY.equals(name)) { enc = Roundabout.create(); - } else if ("car_access".equals(name)) { - enc = VehicleAccess.create("car"); - } else if ("bike_access".equals(name)) { - enc = VehicleAccess.create("bike"); } else if (GetOffBike.KEY.equals(name)) { enc = GetOffBike.create(); } else if (RoadClass.KEY.equals(name)) { From 4bad6b2c97a530755082f9381073cfaeb0f312f7 Mon Sep 17 00:00:00 2001 From: easbar Date: Sun, 3 Jul 2022 17:56:37 +0200 Subject: [PATCH 25/28] fix --- .../test/java/com/graphhopper/routing/PathTest.java | 4 ++-- .../java/com/graphhopper/gpx/GpxConversionsTest.java | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/test/java/com/graphhopper/routing/PathTest.java b/core/src/test/java/com/graphhopper/routing/PathTest.java index 38c2d1ec4b0..e6b0af10645 100644 --- a/core/src/test/java/com/graphhopper/routing/PathTest.java +++ b/core/src/test/java/com/graphhopper/routing/PathTest.java @@ -890,12 +890,12 @@ public void testUTurnRight() { na.setNode(6, -33.885692, 151.181445); na.setNode(7, -33.885692, 151.181445); - GHUtility.setSpeed(60, 0, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 0, carAccessEnc, carAvSpeedEnc, g.edge(1, 2).setDistance(5).setKeyValues(singletonMap("name", "Parramatta Road")), g.edge(2, 3).setDistance(5).setKeyValues(singletonMap("name", "Parramatta Road")), g.edge(4, 5).setDistance(5).setKeyValues(singletonMap("name", "Parramatta Road")), g.edge(5, 6).setDistance(5).setKeyValues(singletonMap("name", "Parramatta Road"))); - GHUtility.setSpeed(60, 60, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), + GHUtility.setSpeed(60, 60, carAccessEnc, carAvSpeedEnc, g.edge(2, 5).setDistance(5).setKeyValues(singletonMap("name", "Larkin Street")), g.edge(5, 7).setDistance(5).setKeyValues(singletonMap("name", "Larkin Street"))); diff --git a/web-bundle/src/test/java/com/graphhopper/gpx/GpxConversionsTest.java b/web-bundle/src/test/java/com/graphhopper/gpx/GpxConversionsTest.java index 6a931aa58f6..4289aa42d26 100644 --- a/web-bundle/src/test/java/com/graphhopper/gpx/GpxConversionsTest.java +++ b/web-bundle/src/test/java/com/graphhopper/gpx/GpxConversionsTest.java @@ -81,12 +81,12 @@ public void testInstructionsWithTimeAndPlace() { na.setNode(6, 15.1, 10.1); na.setNode(7, 15.1, 9.8); - GHUtility.setSpeed(63, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(7000).setKeyValues(singletonMap("name", "1-2")); - GHUtility.setSpeed(72, true, true, accessEnc, speedEnc, g.edge(2, 3).setDistance(8000).setKeyValues(singletonMap("name", "2-3")); - GHUtility.setSpeed(9, true, true, accessEnc, speedEnc, g.edge(2, 6).setDistance(10000).setKeyValues(singletonMap("name", "2-6")); - GHUtility.setSpeed(81, true, true, accessEnc, speedEnc, g.edge(3, 4).setDistance(9000).setKeyValues(singletonMap("name", "3-4")); - GHUtility.setSpeed(9, true, true, accessEnc, speedEnc, g.edge(3, 7).setDistance(10000).setKeyValues(singletonMap("name", "3-7")); - GHUtility.setSpeed(90, true, true, accessEnc, speedEnc, g.edge(4, 5).setDistance(10000).setKeyValues(singletonMap("name", "4-5")); + GHUtility.setSpeed(63, true, true, accessEnc, speedEnc, g.edge(1, 2).setDistance(7000).setKeyValues(singletonMap("name", "1-2"))); + GHUtility.setSpeed(72, true, true, accessEnc, speedEnc, g.edge(2, 3).setDistance(8000).setKeyValues(singletonMap("name", "2-3"))); + GHUtility.setSpeed(9, true, true, accessEnc, speedEnc, g.edge(2, 6).setDistance(10000).setKeyValues(singletonMap("name", "2-6"))); + GHUtility.setSpeed(81, true, true, accessEnc, speedEnc, g.edge(3, 4).setDistance(9000).setKeyValues(singletonMap("name", "3-4"))); + GHUtility.setSpeed(9, true, true, accessEnc, speedEnc, g.edge(3, 7).setDistance(10000).setKeyValues(singletonMap("name", "3-7"))); + GHUtility.setSpeed(90, true, true, accessEnc, speedEnc, g.edge(4, 5).setDistance(10000).setKeyValues(singletonMap("name", "4-5"))); ShortestWeighting weighting = new ShortestWeighting(accessEnc, speedEnc); Path p = new Dijkstra(g, weighting, TraversalMode.NODE_BASED).calcPath(1, 5); From a0e4e952c578f1c1d658cda3a7181d54ace94ecb Mon Sep 17 00:00:00 2001 From: easbar Date: Mon, 4 Jul 2022 08:41:05 +0200 Subject: [PATCH 26/28] minor --- .../com/graphhopper/routing/util/EncodingManager.java | 6 +++--- .../java/com/graphhopper/reader/osm/OSMReaderTest.java | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java index 73eb3292b45..d958780ba18 100644 --- a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java +++ b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java @@ -83,14 +83,14 @@ private EncodingManager() { public static class Builder { private EncodingManager em = new EncodingManager(); - public Builder add(VehicleEncodedValues vehicleEncodedValues) { + public Builder add(VehicleEncodedValues v) { checkNotBuiltAlready(); List list = new ArrayList<>(); - vehicleEncodedValues.createEncodedValues(list); + v.createEncodedValues(list); list.forEach(this::add); list = new ArrayList<>(); - vehicleEncodedValues.createTurnCostEncodedValues(list); + v.createTurnCostEncodedValues(list); list.forEach(this::addTurnCostEncodedValue); return this; } diff --git a/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java b/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java index 2fc56ae86dd..670a7145fcc 100644 --- a/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java +++ b/core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java @@ -413,7 +413,7 @@ public void testFords() { Graph graph = hopper.getBaseGraph(); // our way is split into five edges, because there are two ford nodes assertEquals(5, graph.getEdges()); - BooleanEncodedValue accessEnc = hopper.getEncodingManager().getBooleanEncodedValue("car_access"); + BooleanEncodedValue accessEnc = hopper.getEncodingManager().getBooleanEncodedValue(VehicleAccess.key("car")); int blocked = 0; int notBlocked = 0; AllEdgesIterator edge = graph.getAllEdges(); @@ -1008,11 +1008,11 @@ protected void importOSM() { BaseGraph baseGraph = new BaseGraph.Builder(getEncodingManager()).set3D(hasElevation()).withTurnCosts(getEncodingManager().needsTurnCostsSupport()).build(); setBaseGraph(baseGraph); super.importOSM(); - carAccessEnc = getEncodingManager().getBooleanEncodedValue("car_access"); - carSpeedEnc = getEncodingManager().getDecimalEncodedValue("car_average_speed"); + carAccessEnc = getEncodingManager().getBooleanEncodedValue(VehicleAccess.key("car")); + carSpeedEnc = getEncodingManager().getDecimalEncodedValue(VehicleSpeed.key("car")); carOutExplorer = getBaseGraph().createEdgeExplorer(AccessFilter.outEdges(carAccessEnc)); carAllExplorer = getBaseGraph().createEdgeExplorer(AccessFilter.allEdges(carAccessEnc)); - footAccessEnc = getEncodingManager().getBooleanEncodedValue("foot_access"); + footAccessEnc = getEncodingManager().getBooleanEncodedValue(VehicleAccess.key("foot")); } @Override From f68bd4b338e4d5731b09de89c8e743b37692266c Mon Sep 17 00:00:00 2001 From: easbar Date: Mon, 4 Jul 2022 13:00:06 +0200 Subject: [PATCH 27/28] use access enc from super class (was the same) --- .../graphhopper/routing/weighting/custom/CustomWeighting.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomWeighting.java index a945061d5b6..c7759f7c6e5 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomWeighting.java @@ -77,7 +77,6 @@ public final class CustomWeighting extends AbstractWeighting { * costs or traffic light costs etc) */ private final static double SPEED_CONV = 3.6; - private final BooleanEncodedValue baseVehicleAccessEnc; private final double maxSpeed; private final double maxPriority; private final double distanceInfluence; @@ -89,7 +88,6 @@ public CustomWeighting(BooleanEncodedValue baseAccessEnc, DecimalEncodedValue ba super(baseAccessEnc, baseSpeedEnc, turnCostProvider); this.edgeToSpeedMapping = parameters.getEdgeToSpeedMapping(); this.edgeToPriorityMapping = parameters.getEdgeToPriorityMapping(); - this.baseVehicleAccessEnc = baseAccessEnc; this.headingPenaltySeconds = parameters.getHeadingPenaltySeconds(); this.maxSpeed = parameters.getMaxSpeed() / SPEED_CONV; this.maxPriority = parameters.getMaxPriority(); @@ -124,7 +122,7 @@ public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) { reverse = false; // TODO see #1835 - if (reverse ? !edgeState.getReverse(baseVehicleAccessEnc) : !edgeState.get(baseVehicleAccessEnc)) + if (reverse ? !edgeState.getReverse(accessEnc) : !edgeState.get(accessEnc)) return Double.POSITIVE_INFINITY; double speed = edgeToSpeedMapping.get(edgeState, reverse); From 421b56f1274591d5eb55c986d844f8a151f50a34 Mon Sep 17 00:00:00 2001 From: easbar Date: Mon, 4 Jul 2022 20:38:41 +0200 Subject: [PATCH 28/28] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7b070615e5..1e3a5ff216d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ ### 6.0 [not yet released] +- removed the FlagEncoder interface. for example encoder.getAccessEnc() is now encodingManager.getBooleanEncodedValue( + VehicleAccess.key("car")), #2611 - backward incompatible change as instructions and the street_name path detail do no longer contain the ref #2598 - StringIndex is now EdgeKVStorage and can store e.g. byte arrays. String values needs to be limited to 255 bytes before storing them. See #2597