Repository navigation
Conversation
* for any usage of GraphHopper there should either be a setCHEnabled(false) call or some CH profile strings should be set * -> now we can remove it again :)
* remove enabled flag in CH/LMPreparationHandlers
# Conflicts: # reader-gtfs/README.md
# Conflicts: # core/files/changelog.txt # core/src/main/java/com/graphhopper/GraphHopper.java # core/src/main/java/com/graphhopper/routing/ch/CHPreparationHandler.java # core/src/main/java/com/graphhopper/routing/lm/LMPreparationHandler.java # core/src/test/java/com/graphhopper/routing/ch/CHPreparationHandlerTest.java # core/src/test/java/com/graphhopper/routing/lm/LMPreparationHandlerTest.java # core/src/test/java/com/graphhopper/storage/GraphHopperStorageLMTest.java # core/src/test/java/com/graphhopper/storage/GraphHopperStorageTest.java # reader-osm/src/test/java/com/graphhopper/GraphHopperIT.java # reader-osm/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java # reader-osm/src/test/java/com/graphhopper/routing/RoutingAlgorithmWithOSMIT.java
|
For completeness, here is an example for an LM profile (it includes the profiles_lm:
- profile: car
maximum_lm_weight: 10000 |
# Conflicts: # core/files/changelog.txt # graphhopper.sh # reader-osm/src/test/java/com/graphhopper/GraphHopperIT.java
karussell
left a comment
There was a problem hiding this comment.
Thanks, this looks good!
When using the Java API for the profiles it feels a bit too lengthy to create a profile via the setters, but I do not know how to make it shorter :D ... except a constructor approach for most common combinations as the builder pattern we use e.g. for EncodingManager is also not shorter.
When adding a CH profile, why do we need an object and cannot just use a string? (btw: for LM I can imagine that we get rid of the maximum weight parameter via creating a better estimation procedure or put this into the hints of the ProfileConfig?). Because if it is just a string we could see LM or CH preparation as a property of ProfileConfig and avoid parts of checkProfilesConsistency?
|
|
||
| public static void validateProfileName(String profileName) { | ||
| if (!profileName.matches("^[a-z0-9_\\-]*$")) { | ||
| throw new IllegalArgumentException("Profile names may only contain lower case letters, numbers, underscores and dashs, given: " + profileName); |
There was a problem hiding this comment.
Can we remove dashes from the accepted characters or is dash intended to be used for the future query parameter guessing?
There was a problem hiding this comment.
The dash might also be a minus and we have it currently for example as utc-1 in the default naming scheme for CHProfile. There would probably be a way to change this and no longer allow dash/minus, but then again this character makes no problems when used in url parameters or filenames, so I thought its ok to allow it.
There was a problem hiding this comment.
utc-1
Ah, ok. Ugly. Would be nice to change this (likely in another issue & later)
| import static com.graphhopper.config.ProfileConfig.validateProfileName; | ||
|
|
||
| public class CHProfileConfig { | ||
| private String profile = "unset_ch_profile"; |
There was a problem hiding this comment.
Why is the pre-initialization necessary? I.e. why not an empty or null string?
There was a problem hiding this comment.
Its certainly not necessary, but I thought its a bit more explicit like that. I do not like using null, because then it could actually be null after deserialization (not sure if this is good or bad, but for example if we always make sure everything is initialized it would be sufficient to check for empty string (if even) and we do not need null checks. -> I changed it to be initialized with an empty string now.
| # - vehicle (required): refers to the `graph.flag_encoder` used for this profile | ||
| # - weighting (required): the weighting used for this profile, e.g. fastest,shortest or short_fastest | ||
| # - turn_costs (true/false, default: false): whether or not turn restrictions should be applied for this profile. | ||
| # this will only work if the `graph.flag_encoder` for the given `vehicle` is configured with `|turn_costs=true`. |
There was a problem hiding this comment.
Wouldn't it be possible to remove the graph.flag_encoders list now and use the vehicle property of all profiles instead? (btw minor typo of missing 's' in both cases)
There was a problem hiding this comment.
Yes it might be possible, but lets say this would be another/next step (which is not strictly required here). The problem I am having with this is that the flag encoders are not simply something like car, but rather car|turn_costs=true or motorcycle|your_property=here. So right now if we would do any kind of such configurations of the flag encoders like this we would have to repeat it in each profile? Or somehow merge them? Or choose one by some (possibley not so obvious) rule? And even if we think a bit about future development when the flag encoders become collections of encoded values, then we will probably want to configure these collections of encoded values, give them a name and refer to this name in the profile (just like we are doing here).
btw minor typo of missing 's' in both cases
fixed, thanks
There was a problem hiding this comment.
Ok, yeah. Probably for another issue
we would have to repeat it in each profile
Yeah, now I understand the problem. If one profile requests "car with turn instruction" and the other "car without turn instruction" it would be required to create two flag encoders but they would collide due to the same name.
There was a problem hiding this comment.
If we assume there is one encoder per profile, thats the problem. Otherwise it would be unclear whether the (one) car encoder would have turn instructions or not (and probably similar problems for other encoder properties), there would no longer be a single place to define the encoder.
| Map<String, ProfileConfig> profilesByName = getProfilesByName(); | ||
| for (CHProfileConfig chConfig : chPreparationHandler.getCHProfileConfigs()) { | ||
| ProfileConfig profile = profilesByName.get(chConfig.getProfile()); | ||
| assert profile != null : "there is no profile " + chConfig.getProfile(); |
There was a problem hiding this comment.
I would prefer to throw an IllegalStateException for everything that is not called very often instead of an assert, because assert is only active for our tests. Or why / when do you use assert?
There was a problem hiding this comment.
The reason I am using assert here is that this is a private method and the existence of the profile is already checked in checkProfilesConsistency before. So at this point its clear that the profile exists (unless there is some programming error and my assumption that it was checked before is false). So we could either skip this check here entirely, or repeat the check again, or use an assert as a bit of a reminder that (if nothing is broken) we can be sure the profile exists. In general of course you are right and the program logic should not depend on an assert. Here it does not because the same check is done before already. If you think this is confusing somehow I think we should just remove this assert here (and use the null pointer exception to figure out what is going on if this ever pops up). I have to admit the assert message here is not chosen very well it would better be: "There is no profile ... even though this should have been checked before"
There was a problem hiding this comment.
An assert statement always distracts a bit and should have some reason it is no if+throw because even if there is a bug we'll never know with assert, because in production it is disabled and in tests it does not happen. (except we create some randomization tests that randomize the initialization too)
So for higher level code where speed does not matter I would always use an if statement + throw an IllegalStateException OR decide it is not worth the clutter and remove it.
Interestingly even for code where speed matters I observed that "if+throw" can often be removed via JIT and does not hurt. E.g. recently I introduced this in RAMDataAccess.setInt and I should probably replace the remaining assert too or remove it.
There was a problem hiding this comment.
An assert statement always distracts a bit
Thats a bit subjective :) I think asserts can do a great job in communicating intend (even though the present case is not a particularly good example for this).
it is no if+throw because even if there is a bug we'll never know with assert, because in production it is disabled and in tests it does not happen
Yes and like I said I did not add the assert because its supposed to throw anything (we check wether or not the profile exists in checkConsistency() already.
So for higher level code where speed does not matter I would always use an if statement
I did not decide to use assert for performance reasons here. Performing a single if is probably very rarely a performance issue (and especially not here).
What do you prefer?
// option 1:
Profile profile = profiles.get(profileName);
// profile cannot be null because we checked already
profile.doSth();
// option2
Profile profile = profiles.get(profile);
profile.doSth();
// option3
Profile profile = profiles.get(profile);
assert profile != null : "profile does not exist even though this should have been checked already";
profile.doSth();Here, I am happy with any of the options, but I would not agree if we tried to never use assert whatsoever. Like I said this is not a particular good/important use-case here.
There was a problem hiding this comment.
If I'm confident that it is really not null I would probably use option 2.
If not, I would pick option 4: throw an IllegalStateException ;)
I still do not see the benefit of an assert that is never thrown in production. Why should I continue in production when we are in a wrong situation? It feels worse than catching an exception and then just logging it :)
There was a problem hiding this comment.
If I'm confident that it is really not null I would probably use option 2.
Then the next person might ask the same question and maybe he/she is not as confident :)
If not, I would pick option 4: throw an IllegalStateException ;)
Ok that also works for me.
I still do not see the benefit of an assert that is never thrown in production. Why should I continue in production when we are in a wrong situation? It feels worse than catching an exception and then just logging it :)
The idea is that this code never goes to production, because some tests will fail. I think assertions can:
-
help finding the source of an error (you know something goes wrong at some place, but assertions might tell you that the real reason something went wrong was that something else went wrong before this)
-
help communicate under which assumptions some code was written
You are right because both things can be achieved equally well by throwing IllegalStateException (or some shorter helper method that does this) as long as performance is not an issue. My idea here was that the assert tells me that we are checking something that has already been checked anyway, but I see this is not easy to follow for others.
If however assertions are used more extensively (and possibly using some assertions that are more expensive than a simple boolean check) they can certainly be useful because they can be disabled for production. The present case is not a good example for that, but generally I think using more asserts is a good thing to do (especially in code where calculating some precondition only to find out whether or not we are in a sane state is expensive). For example RoutingKit does this a lot and its almost like you do not need tests anymore (or only a few), because when running the code with enabled assertions there is a very high chance some assertions fail when you are making a programming error and it improves readability when you are told what the expectations for the current method call were when the method was written. You are also able to easily test internal things that would never be easy to cover using tests. Ok, this is a whole other use-case than what we are talking about here, but I would just not go as far as saying that asserts are always bad or distracting.
-> I'll remove the assert :)
| if (ghConfig.has("prepare.chWeighting") || ghConfig.has("prepare.chWeightings") || ghConfig.has("prepare.ch.weightings")) | ||
| throw new IllegalStateException("Use profiles_ch instead of prepare.chWeighting, prepare.chWeightings or prepare.ch.weightings"); | ||
| if (ghConfig.has("prepare.ch.edge_based")) | ||
| throw new IllegalStateException("Use profiles_ch instead of prepare.ch.edge_based"); |
There was a problem hiding this comment.
In both exceptions I would link to this issue #1922 to give more context.
| return setProfiles(Arrays.asList(profiles)); | ||
| } | ||
|
|
||
| public GraphHopper setProfiles(List<ProfileConfig> profiles) { |
There was a problem hiding this comment.
Some javadocs would be good here, also how to set the LM/CHProfileConfig. Or link to this PR.
There was a problem hiding this comment.
Yes will do as soon as we have agreed on a final design
| private void checkProfilesConsistency() { | ||
| Set<String> profileSet = new LinkedHashSet<>(profiles.size()); | ||
| for (ProfileConfig profile : profiles) { | ||
| boolean added = profileSet.add(profile.getName()); |
There was a problem hiding this comment.
you can add the profile directly to the set as the hashcode & equals is already implemented for that (?)
Or maybe shouldn't we do the check when calling setProfileConfigs or create a
LinkedHashMap<String, ProfileConfig> profiles in the GraphHopper class on setProfileConfigs? This would also avoid the method getProfilesByName.
There was a problem hiding this comment.
Yes using a map is better, also because later when resolving the profile during a request we have to obtain the profile by name again. I used a map before and then changed it back to list, but now I do not remember why anymore -> its a map now 👍
| return this; | ||
| } | ||
|
|
||
| public PMap hints() { |
There was a problem hiding this comment.
Why is it not named getHints?
There was a problem hiding this comment.
Because of the jackson conventions. If there was getHints() Jackson would allow a hints sub-node, but we want all the other fields on the top level (see the @JsonAnySetter in the corresponding mixin)
There was a problem hiding this comment.
What do you mean with "would allow a hints sub-node" - when would it allow this? As there is no setHints method.
There was a problem hiding this comment.
The getter makes the hints serializable and deserializable: https://www.baeldung.com/jackson-field-serializable-deserializable-or-not#a-getter-makes-a-non-public-field-serializable-and-deserializable. So if we put getHints by convention the deserialized config would look like:
profile:
name: my_name
hints:
foo: bar
baz: foobut (afaik) we want:
profile:
name: my_name
foo: bar
baz: fooThere was a problem hiding this comment.
Yes with @JsonIgnore we can have getHints and Jackson will still not consider hints to be a property. -> I renamed to getHints().
|
|
||
| import com.graphhopper.util.PMap; | ||
|
|
||
| public class ProfileConfig { |
There was a problem hiding this comment.
Here some JavaDoc would be good with a link to LM/CHProfileConfig
There was a problem hiding this comment.
Yes will do as soon as we have agreed on a final design
| @@ -1,4 +1,5 @@ | |||
| 1.0 | |||
| add profiles parameter and replace prepare.ch/lm.weightings and prepare.ch.edge_based with profiles_ch/lm config parameters | |||
# Conflicts: # core/src/main/java/com/graphhopper/routing/lm/LMPreparationHandler.java
I tried reducing it and at least we have the varargs parameter so we do not have to create a list ourselves...
You mean we should use a builder pattern or we should not? We already have kind of a builder pattern using the fluent setters:
Of course you are right and we do not strictly need an object (since its just containing a single string). Initially I had the profiles_lm:
- profile: "my_profile"
#vs
profiles_lm:
- my_profile? In yaml I think its even better with |
Ok
Ok, the difference between query and import parameter is convincing :) and an object is of course not really problematic, would have been just a little bit easier.
In yaml we could avoid a separate |
Yes, but then we could/should(?) do the same in the java classes? I would rather keep this symmetric (the whole point of |
|
I'll try to remove |
Ah, didn't mean that. Your argument sounds good to keep them. |
Are you sure? You got me thinking again... Lets say we do something like this: profiles:
- name: my_car
vehicle: car
weighting: fastest
ch_profiles:
- profile: my_car
update_neighbors: 84At first sight its nice to not clutter the profile with things like profiles:
- name: my_car
vehicle: car
weighting: fastest
prepare_ch: true
prepare_lm: false
update_neighbors: 84but after the import |
|
Regarding the 'what should be loaded?' configuration I think we agreed that in the future the answer should be 'everything from the -gh folder', but maybe we want to be able to only load parts of it? Then we need some kind of include/exclude list for the preparations, but this should really only be a list of profile names (for ch and lm), because this way we cannot do anything besides including/excluding the preparations (no new configuration). So for this use case the |
Yes, I thought this was a good argument :) as having a CH property in a non-CH profile feels incorrect.
Ok.
Yes.
Ah, ok. I see. Makes sense. |
Ok that is a good reason to split it into two/three sections. So I'll leave as is for now, but we might need to change it again when doing further refactorings (graph database etc.). I guess thats just the price we pay for adding the custom weightings before doing the graph storing refactorings. Btw another ugliness is that if we specify the profiles, do CH import and then change some profile parameter (especially with custom) without changing the profile name its inconsistent. This will also be resolved when we only use the profile configuration to do the import and afterwards consider the graph folder as single source of truth... |
| "\nYou need to add `|turn_costs=true` to the vehicle in `graph.flag_encoders`"); | ||
| } | ||
| try { | ||
| createWeighting(new HintsMap(profile.getWeighting()), encoder, NO_TURN_COST_PROVIDER); |
There was a problem hiding this comment.
Sorry for being a bit lat to look at the changes. I was wondering why are we creating an instance of the weighting here? Wouldn't it be enough to check if a weighting with a certain name is "registered"/"available"? Or is this the easiest option to check if the weighting is available?
There was a problem hiding this comment.
Well yes the weighting is only created to check whether or not this is possible. Simply creating it seems like the easiest (and certainly most complete) way to do this. For example with custom weightings in #1776 we also get an error if something is wrong about the custom model YAML configuration this way. Then again, like you observed, there is no real need to create the weighting. Do you see a problem with this?
There was a problem hiding this comment.
For example with custom weightings in #1776 we also get an error if something is wrong about the custom model YAML configuration this way.
Ah I see, that's a strong point to do this and makes sense.
Then again, like you observed, there is no real need to create the weighting. Do you see a problem with this?
This created an exception for me. I use a weighting that requires certain parameters in the HintsMap, otherwise an exception is thrown. It is easy enough to fix, so that's no problem.
This PR replaces the following configuration options:
with three new sections:
More details can be found in
config-example.ymldocumentation in this PR, for completeness I am copying this here:With the new configuration its now possible to e.g. use CH for only one of multiple flag encoders (previously there was a preparation for every vehicle for every given weighting in
prepare.ch/lm.weightings), i.e. this PR fixes #493.Importantly the profiles have a (required) name field, which soon will be used to pick the profile during a routing request (see #1859). However, this PR still uses the old method (the profile is identified by comparing the weighting/vehicle/turn_costs parameters)!
This PR also does not yet allow the new custom weighting from #1776, but the profiles section in the configuration together with the (to be added)
profilerequest parameter should make it easy to integrate this.Important Note: This PR does not only remove the above mentioned configuration options, but there will be an error in case they are still being used, so it forces an upgrade of the config files.