Skip to content

Why do we need EncodedValueFactory? #2438

Closed

Description

@easbar

We currently create EncodedValue objects in two places: The TagParsers and in DefaultEncodedValueFactory. The reason is probably that we have two scenarios:

  1. We generate new graph data from an OSM file. For this we define TagParsers that handle different OSM tags and write to according encoded values. The TagParsers also create these EncodedValues, e.g. OSMMaxSpeedParser creates the MaxSpeed encoded value that it writes to.

  2. We load existing graph data. In this case we don't need TagParsers, because we are not parsing any tags. We still have to create the EncodedValue objects though. This is where EncodedValueFactory comes in. We use it to create the EncodedValue objects given the strings that were written to the properties file during the import here:

    ? EncodingManager.create(emBuilder, encodedValueFactory, flagEncoderFactory, properties)

The obvious downside of EncodedValues being created in not one, but two places is that they have to be kept in sync. And, to no surprise, they currently aren't.

This test fails:

    @Test
    public void lanesEV() {
        {
            GraphHopperConfig config = new GraphHopperConfig();
            config.putObject("graph.location", GH_LOCATION);
            config.putObject("datareader.file", BAYREUTH);
            config.putObject("graph.encoded_values", "lanes");
            config.setProfiles(Collections.singletonList(
                    new Profile("car").setVehicle("car").setWeighting("fastest")
            ));
            GraphHopper hopper = new GraphHopper();
            hopper.init(config);
            hopper.importOrLoad();
        }
        {
            GraphHopper hopper = new GraphHopper()
                    .setGraphHopperLocation(GH_LOCATION)
                    .setProfiles(new Profile("car").setVehicle("car").setWeighting("fastest"));
            hopper.importOrLoad();
            // error: java.lang.IllegalArgumentException: DefaultEncodedValueFactory cannot find EncodedValue lanes
        }
    }

It fails, because we have an OSMLanesParser that is listed in DefaultTagParserFactory, but the Lanes EV is not listed in DefaultEncodedValueFactory. But here is the catch: The test only fails, because we use the GraphHopper's 'setter' initialization when we load the graph data. If we used init instead it would work, because in this case, even though there is nothing to import anymore, we do create the TagParsers, which then create the encoded values (and not the EncodedValueFactory). Only when we skip the init() call encodingManager will be null in the load method and we try to create the encoded values from the graph.encoded_values string that was written to the properties file.

So we only ever use EncodedValueFactory when we load existing graph data and initialize the GraphHopper class using the setters (do not call init()).

We have a few options:

  1. Just remember that we have to keep the EncodedValueFactory in sync with the TagParserFactory in case we load GraphHopper without calling init(). This is what we do currently. Really ugly (see above).

  2. Remove EncodedValueFactory and only create encoded values via TagParsers. Maybe a bit better, because EncodedValueFactory is already only used in one specific case and it is already broken/out of sync. Still ugly though, because this means we need to create tag parser objects also when loading an existing graph. But 'parsing tags' is something that has nothing to do with 'loading existing graph data'. The fact that GraphHopper (as well as GraphHopperStorage, DataAccess, FlagEncoder, EncodingManager etc.) to a large part use the same code to 'load' as they use to 'import' is a huge technical debt if you ask me. The root of many evils, so to say. However, since we already use TagParsers to create the encoded values when we use load() after init() anyway, it actually might be worth to remove EncodedValueFactory first and only introduce something similar when we remove TagParser#createEncodedValues.

  3. Always create encoded values via EncodedValueFactory (sounds right at least) and stop creating encoded values in TagParsers. For example I wonder what if we created all encoded values first and then passed them to each tag parser. Maybe this would also allow some new use cases, like modifying an encoded value from different tag parsers, or setting encoded values based on others ('aggregated encoded values').

Activity

  1. easbar commented on Oct 21, 2021

    @easbar
    MemberAuthor

    I also did a quick comparison of DefaultEncodedValueFactory and DefaultTagParserFactory:

    • The encoded value factory is missing the following EVs for which the tag parser factory provides tag parsers: lanes,mtb_rating,hike_rating,horse_rating,country
    • The tag parser factory does not provide tag parsers for the following EVs that can be created via the encoded value factory (this is probably fine(?)): get_off_bike, bike/foot network and the subnetwork EV
  2. easbar commented on Apr 5, 2022

    @easbar
    MemberAuthor

    I went with option 2) in #2551.

    For example I wonder what if we created all encoded values first and then passed them to each tag parser.

    This is next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions