Skip to content

Replace EncodingManager with TagParserManager where it is needed - #2541

Merged
easbar merged 7 commits into
masterfrom
tag_parser_manager
Mar 18, 2022
Merged

easbar merged 7 commits into
masterfrom
tag_parser_manager

Conversation

@easbar

@easbar easbar commented Mar 18, 2022 •

Copy link
Copy Markdown
Member

This is based on #2540.

EncodingManager does (too) many things. One thing it does is dealing with the different parsers used for the OSM import. However, we use it in very many places where we are not dealing with parsing OSM at all. Here I have created a 1:1 copy of EncodingManager, called TagParserManager and replaced usages of EncodingManager with this new TagParserManager class wherever we actually use the parsers. In turn, we can remove the parsing stuff from EncodingManager, because it is not needed in all the other places. Now there are around 120 usages of TagParserManager und 540 usages of EncodingManager which means that we can effectively remove parsing related code in many places.

The TagParserManager is often created via the GraphHopper class, just like we created the EncodingManager there before. To be able to transition from this higher level code to the lower level API that uses only the EncodingManager there is TagParserManager#getEncodingManager.

From here on (not this PR, but soon after) we still need to keep cleaning up EncodingManager. Eventually, it should just be a combination of the encodedValuesMap and edge/turnCostConfig, so we will have to focus on removing the edgeEncoders list and the turnCostParsers map. One very common usage pattern is this:

CarFlagEncoder encoder = new CarFlagEncoder();
EncodingManager em = EncodingManager.create(encoder);
BaseGraph graph = new BaseGraph.Builder(em.getIntsForFlags()).create(); 
graph.edge(0, 1)
    .set(encoder.getAccessEnc(), true)
    .set(encoder.getAverageSpeedEnc(), 50);
    
// we already have a reference to the car encoder so it looks like we do not need this, 
// but this is what we do at request time: We use the vehicle string to get a certain encoder (see DefaultWeightingFactory)
encoder = em.getEncoder("car"); 
Weighting weighting = new FastestWeighting(encoder);
Dijkstra algo = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED);

I think at some point this should (at least in principle) be turned into something like this to get rid of the flag encoders list for example:

String vehicle = "car";
// maybe add some helper code to create these common encoded values, but they are just this: some encoded values
DecimalEncodedValue speedEnc = new DecimalEncodedValueImpl(EncodingManager.getKey(vehicle, "average_speed"), speedBits, speedFactor, speedTwoDirections);
BooleanEncodedValue accessEnc = new SimpleBooleanEncodedValue(EncodingManager.getKey(prefix, "access"), true);
EnumEncodedValue<RoadAccess> roadAccessEnc = new EnumEncodedValue<>(RoadAccess.KEY, RoadAccess.class);
EncodingManager em = new EncodingManager(); // basically this is just an empty encodedValueMap
em.add(speedEnc); // calls ev.init(edgeConfig) internally
em.add(accessEnc);
em.add(roadClassEnc);
graph.edge(0, 1)
     .set(accessEnc, true)
     .set(speedEnc, 50);
     
// at request time we need to get the encoded values from the encoding manager
speedEnc = em.getDecimalEncodedValue(EncodingManager.getKey(vehicle, "average_speed");
accessEnc = em.getBooleanEncodedValue(EncodingManager.getKey(vehicle, "access");     
roadClassEnc = em.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class);
// we use an empty PMap in this example but it could be used to configure heading/private/destination penalties for example
// by default we could take the max speed from the max possible value for speedEnc here, and set it to a smaller value for production code
Weighting weighting = new FastestWeighting(accessEnc, speedEnc, roadClassEnc, new PMap());
Dijkstra algo = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED);     

We will also need to get rid of Weighting#getFlagEncoder() for this to work.

One possible shortcut I see that we could use to keep focus on removing FlagEncoders in our import code first, without having to deal with changes to all the FastestWeighting usages etc. at the same time, would be something like this:

// this is just a class wrapping the encoded values we saw above. It's an implementation of the FlagEncoder, 
// but it does not extend AbstractFlagEncoder so it has nothing to do with OSM parsing.
RoutingFlagEncoder encoder = RoutingFlagEncoder.forVehicle("car");
EncodingManager em = new EncodingManager(); 
EncodingManager.add(encoder.getEncodedValues());
graph.edge(0, 1)
     .set(encoder.getAccessEnc(), true)
     .set(encoder.getAvgSpeedEnc(), 50);
     
encoder = em.getEncoder("car");     
Weighting weighting = new FastestWeighting(encoder);
Dijkstra algo = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED);     

For this the next step would be turning the List<AbstractFlagEncoder> in EncodingManager into List<FlagEncoder>.

@easbar
easbar changed the base branch from master to gitbug March 18, 2022 16:09
@easbar
easbar changed the base branch from gitbug to master March 18, 2022 16:10
@easbar
easbar merged commit fcba4c2 into master Mar 18, 2022
@easbar
easbar deleted the tag_parser_manager branch March 18, 2022 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant