Repository navigation
Weighting support via yaml configuration #1841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
202 commits
Select commit
Hold shift + click to select a range
919f38c
initial version with a basic Weighting support via yaml configuration
karussell 63638b0
implement DelayFlexConfig
karussell 40c2bbe
use flex model for CH preparation - yeah. Now questions like #1708 arise
karussell 9589d63
reverse priority is logical
karussell aa6f23e
add more tests regarding common use cases like max_height and avoid t…
karussell 674de1a
avoid special case of config after graphhopper.init
karussell 1aa1e51
merge FlexResource into RouteResource
karussell 93fe39b
avoid additional test classes
karussell f0e9723
Merge branch 'master' into issue_1776
karussell f63d3ca
add minor java docs
karussell 1fd862e
Merge branch 'master' into issue_1776
karussell 5624342
Merge branch 'master' into issue_1776
karussell 6cb6eb2
minor fixes
karussell 48c0561
added initial UI
karussell 47b0938
replace min with max priority for proper A* heuristic. Range check al…
karussell 07ce143
throw exception if encoded values like max_height does not exists
karussell c597c64
added todo to PR
karussell 56d4a79
improve warning
karussell 247cf2d
avoid calling edge.getDistance in weight twice -> we need this for al…
karussell 4fc14bd
Merge branch 'master' into issue_1776
easbar 1771bf8
Merge branch 'master' into issue_1776
easbar f54c726
Merge branch 'master' into issue_1776
easbar 5c65d8b
Remove unused import
easbar f9f5ec9
Add EnumToValue#toString
easbar d68c04b
Merge branch 'master' into issue_1776
easbar 1a8a4e9
rename FlexModelWeighting to CustomWeighting etc to avoid confusion w…
karussell 577ca13
minor doc in config-example.yml
karussell f6ae26c
merged master
karussell 14caa36
ensure that we currently just ignore URL parameters for POST /route r…
karussell 2e4242e
merged master
karussell 21e70fe
better description and examples
karussell 253b9df
remove DelayCustomConfig for now as virtual edges would artifically d…
karussell 1b0d20f
reduce default max prio should increase speed; introduce max speed an…
karussell 223d9d1
properly pick max priority
karussell 46a9f03
set default distance_factor to same like in ShortFastestWeighting ins…
karussell a9f8c1e
remove delay from CustomWeighting description
karussell 1e440cb
change naming custom_models -> custom_profiles #493
karussell c9f7c56
fix tests
karussell a894593
merged master
karussell 2fe3ad4
for now reduce time for routingLM8_custom
karussell f071db2
replaced the often not well working average_speed with max_speed map …
karussell 75adc4e
merged master
karussell c6e207f
fix refactoring
karussell ca2dece
normalize priority and speed_factor so it always can be used for LM e…
karussell fd16781
merged master
karussell 454fd70
apply max_speed after speed_factor; change order and document this in…
karussell 9d8cfbe
merged master
karussell 757bb8a
finally made custom models working with LM, also in Measurement
karussell 8b329df
move custom again to separate /custom endpoint; also move 'model' to …
karussell 28188c5
introduce speed_factor map
karussell 80f966b
merged master
karussell f6b321d
make distance_factor map similar to the other factor maps (i.e. a hig…
karussell 7fda98a
add missing distance factor in weight formula
karussell 1db93e5
sum instead product for distance_term
karussell 31cfa05
remove distance term for now (can be added later)
karussell 6551385
fix flex UI to new /custom endpoint and level up the model
karussell 339fad7
config-example.yml: usage of camel case is required due to https://gi…
karussell 20ef817
enable usage of BooleanEncodedValue
karussell 2fe47d7
implement avoid area
karussell a346a8a
avoid using block_area property for /custom endpoint
karussell dfba6a7
avoid subclassing and reusing CustomWeighting stuff
karussell 3672378
demo: load yml from URL
karussell 298d4f0
refactoring
karussell 27e94aa
merged master
karussell 2f2567b
avoid global snake case due to config.yml backward compatibility
karussell d622462
avoid problem due to recently more strict LM fallback and use our tes…
karussell bfb1d6a
normalizing the speed_factor makes no sense. Just use 1 as maximum.
karussell e57ca9d
AbstractFlagEncoder: removed newly introduced but unnecessary name pa…
karussell 157e4f8
merged master
karussell dae2540
fix typo
karussell 237f1b8
Merge branch 'master' into issue_1776
karussell a58e5d7
merged master
karussell 59f6a14
changed formula and make distance_constant_term independent of priority
karussell 50e78af
rename distance_term_constant to shorter distance_influence
karussell e7d507e
change unit of distance_influence to sec/km
karussell aa47605
merged master
karussell 51e2dea
removed 'name' parameter from AbstractWeighting; introduced GHRequest…
karussell 6d0a55c
merged master
karussell ee4a5e9
merged master
karussell 38d1bf6
merged master
karussell f42e4fa
move CustomModel into ProfileConfig and auto-create profiles per Cust…
karussell 83b6e3d
CustomModel is tightly coupled to ProfileConfig but should not be par…
karussell 37c40f6
Merge branch 'master' into issue_1776
karussell 943fac5
make GeoToValue more efficient; introduces quite a bit complexity
karussell 2398a08
fix test
karussell 6553e33
merged master, made area things much easier
karussell f1434b1
clean up, move TODO NOWs to later
karussell 2dd2ad3
renamed ConfigMapEntry to EdgeToValueEntry
karussell 515ac41
do kind of autocompletion and more documentation
karussell 2aab9b3
merged master
karussell 3fe90a2
help with root entries too
karussell 5170770
make custom model usage more explicit (force profile creation) and li…
karussell 8d10fdd
make things even more clear and introduce CustomProfileConfig to remo…
karussell 0fa5a0d
fixed Measurement
karussell 875ccf3
merged master
karussell 1848008
test fix
karussell 175b9a6
fix Measurement
karussell e1854dc
minor comment change
karussell bc934dd
Merge branch 'master' into issue_1776
karussell bae3fb9
merged master
karussell 0562bcd
no need for CustomProfileConfig.customModel instead store in hints
karussell e5d961a
merged master
karussell cff8fa8
minor test fixes
karussell b289d3f
fix measurement
karussell ec76054
add preparation for truck and do timing for custom profile separate
karussell ad5b95e
introduce custom light to better compare speed and preparation time t…
karussell 65f3fed
minor fix
karussell 6c02d0a
make 'pure' custommodel with car faster
karussell a175abc
merged master
karussell 2877e5e
add CH profile for custom weighting
karussell bef6632
try to make CustomWeighting identical to FastestWeighting
karussell a0c598e
make CustomModel.merge compatible or throw illegalarg
karussell af182ed
revert change in FastestWeighting
karussell aeff09c
fix hints usage
karussell 402b3b2
Try to make curl error visible
easbar 7d9ec58
fix bug: decouple queryCustomModel from merge too
karussell 2aef61a
Try to print error response *and* fail the build
easbar 1bcec45
fix again: fastest weighting has no default distance influence
karussell 42ab0fc
measurement: allow multiple CH profile preps
karussell 8b3a449
merged master
karussell 7d15e45
no need to change ProfileResolver anymore
karussell 115b337
truck profile should use distance influence as we have now car fastes…
karussell c07b888
Merge branch 'master' into issue_1776
karussell e8b0871
Merge branch 'master' into issue_1776
karussell b3ca9fb
fixed something that we cannot really test as parsing the config is r…
karussell 3585d6b
fixed config-example.yml, related to #1987
karussell cb405c5
increase Xmx a bit due to DW 2.0.x seems to need 10MB more
karussell 38b251c
Merge branch 'master' into issue_1776
easbar 5caa3cd
Replace DropwizardAppRule
easbar 7a95654
Fix import
easbar a9f37e6
Merge branch 'master' into issue_1776
easbar a62839b
Minor clean up
easbar 71c6876
Merge branch 'master' into issue_1776
easbar dd778a8
revert changes in ProfileResolver
karussell 7ba6396
Minor
easbar e85ef62
Merge branch 'master' into issue_1776
easbar fd300fd
Merge branch 'master' into issue_1776
easbar 3bb51a9
Improve (hopefully) javadocs of EdgeToValueEntry
easbar 679a798
Remove EncodedValueFactory#findValues
easbar 5c7146a
Remove ugly ClassCastException catching
easbar 29d0fbc
Minor rename
easbar 112e86c
Merge branch 'master' into issue_1776
easbar af3d674
Remove gpx stuff from custom route endpoint
easbar 977ff2b
better exception if boolean is not specified correctly
karussell 25a49a5
Fix misleading javadocs, minor readability fix
easbar 4e04cbb
no negative values will happen, makes it easier to understand intend
karussell a4ff4e7
hide CustomWeighting-specific helper methods
karussell f4b5c83
Setup measurements for custom profiles
easbar 0fed492
Merge branch 'master' into issue_1776
easbar 7a7f53c
Measurement failed, add debug output and try again
easbar 775a119
Fix benchmark
easbar 253bb52
Update/extend profiles.md
easbar 87a91de
Some more profiles.md
easbar b8f01ab
Fix typo
easbar c512ef0
Continue with profiles.md
easbar 841ec4f
More profiles.md: areas also for speed and allowed value ranges for s…
easbar b9ec45a
Fix typo
easbar f9c75f3
Fix explanation of distance_influence in the presence of priorities
easbar db38504
Fix measurement name
easbar fea0c74
Lets see how slow slow routing is
easbar 94d95e8
Merge branch 'master' into issue_1776
easbar 2970ba9
Rename Speed/PriorityCustomConfig to Speed/PriorityCalculator
easbar 395a233
Enforce custom_model_file=empty for empty custom model
easbar 73f20e6
Use AREA_PREFIX constant instead of key() function
easbar 52b3ba7
add getAllShared to lookup
karussell cb16499
minor: avoid public constant. not sure if required
karussell 0d894db
throw exception for geo and boolean too if too big speed
karussell 33d1f6f
Merge branch 'master' into issue_1776
karussell 71fa1a0
Keep slow routing, but for custom only use LM8 node-based
easbar ac271ea
a bit dirty fix regarding CustomModel usage via Java API
karussell 8d82c58
Fix benchmark.sh
easbar a7d3fff
Move max_weight into priority and more refactorings (#2015)
karussell 6241b61
Merge branch 'master' into issue_1776
easbar 4c8d5ad
Update CustomWeighting java docs (even though it does not yet match t…
easbar 49a8b36
Minor
easbar f60d304
Another fix for benchmark.sh...
easbar 7c4136f
Fix model for measurement for new syntax
easbar a106107
... and another fix
easbar 90870f8
... and another fix 2
easbar e548057
benchmark: include dijkstra on big map
easbar 3231fd6
Merge branch 'master' into issue_1776
easbar 6b5105b
Add a few tests for priority/speed calculator
easbar f173818
Update profiles.md
easbar c12100c
Some more cleanup, docs and testing
easbar 9511a04
Remove todo
easbar cf96566
removed yaml endpoint and renamed to /route-custom
karussell db12bbd
accept JSON as custom profile on server side
karussell 90e4898
Fix some of the remaining todos
easbar d9a54dc
Minor cleanup
easbar 187897e
merge 'comparison keys' properly; added tests and docs for this
karussell b83df9e
Remove more todos
easbar 1fc1152
Try removing local distance variable, is it slower?
easbar f6d9b2f
minor Xmx increase
karussell d6bf1dd
Minor fix in docs
easbar d20175a
Merge branch 'master' into issue_1776
easbar ebac0d4
Remove server side custom model inheritance from docs
easbar 1d19ab0
revert changes in FlagEncoder constructor
karussell 4b48883
add nice use case as test; add missing IntToValueEntry; remove furthe…
karussell 79ab4e5
Trry storying distance in local variable is it faster?
easbar f662906
Re-enable all measurements, disable custom/astar measurements for now
easbar 2bc997b
Add link to profiles.md
easbar 1839b2a
Merge branch 'master' into issue_1776
easbar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
269 changes: 269 additions & 0 deletions
269
api/src/main/java/com/graphhopper/routing/util/CustomModel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,269 @@ | ||
| /* | ||
| * 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.json.geo.JsonFeature; | ||
| import com.graphhopper.util.Parameters; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * This class is used in combination with CustomProfileConfig. | ||
| */ | ||
| public class CustomModel { | ||
|
|
||
| public static final String KEY = "custom_model"; | ||
|
|
||
| static double DEFAULT_D_I = 70; | ||
| // optional: | ||
| private Double maxSpeedFallback; | ||
| private Double headingPenalty = Parameters.Routing.DEFAULT_HEADING_PENALTY; | ||
| // default value derived from the cost for time e.g. 25€/hour and for distance 0.5€/km, for trucks this is usually larger | ||
| private double distanceInfluence = DEFAULT_D_I; | ||
| private Map<String, Object> speedFactorMap = new HashMap<>(); | ||
| private Map<String, Object> maxSpeedMap = new HashMap<>(); | ||
| private Map<String, Object> priorityMap = new HashMap<>(); | ||
| private Map<String, JsonFeature> areas = new HashMap<>(); | ||
|
|
||
| public CustomModel() { | ||
| } | ||
|
|
||
| public CustomModel(CustomModel toCopy) { | ||
| this.maxSpeedFallback = toCopy.maxSpeedFallback; | ||
| this.headingPenalty = toCopy.headingPenalty; | ||
| this.distanceInfluence = toCopy.distanceInfluence; | ||
|
|
||
| speedFactorMap = deepCopy(toCopy.getSpeedFactor()); | ||
| maxSpeedMap = deepCopy(toCopy.getMaxSpeed()); | ||
| priorityMap = deepCopy(toCopy.getPriority()); | ||
|
|
||
| areas.putAll(toCopy.getAreas()); | ||
| } | ||
|
|
||
| private <T> T deepCopy(T originalObject) { | ||
| if (originalObject instanceof List) { | ||
| List<Object> newList = new ArrayList<>(((List) originalObject).size()); | ||
| for (Object item : (List) originalObject) { | ||
| newList.add(deepCopy(item)); | ||
| } | ||
| return (T) newList; | ||
| } else if (originalObject instanceof Map) { | ||
| Map copy = new HashMap<>(((Map) originalObject).size()); | ||
| for (Object o : ((Map) originalObject).entrySet()) { | ||
| Map.Entry entry = (Map.Entry) o; | ||
| copy.put(entry.getKey(), deepCopy(entry.getValue())); | ||
| } | ||
| return (T) copy; | ||
| } else { | ||
| return originalObject; | ||
| } | ||
| } | ||
|
|
||
| public Map<String, Object> getSpeedFactor() { | ||
| return speedFactorMap; | ||
| } | ||
|
|
||
| public Map<String, Object> getMaxSpeed() { | ||
| return maxSpeedMap; | ||
| } | ||
|
|
||
| public CustomModel setMaxSpeedFallback(Double maxSpeedFallback) { | ||
| this.maxSpeedFallback = maxSpeedFallback; | ||
| return this; | ||
| } | ||
|
|
||
| public Double getMaxSpeedFallback() { | ||
| return maxSpeedFallback; | ||
| } | ||
|
|
||
| public Map<String, Object> getPriority() { | ||
| return priorityMap; | ||
| } | ||
|
|
||
| public CustomModel setAreas(Map<String, JsonFeature> areas) { | ||
| this.areas = areas; | ||
| return this; | ||
| } | ||
|
|
||
| public Map<String, JsonFeature> getAreas() { | ||
| return areas; | ||
| } | ||
|
|
||
| public CustomModel setDistanceInfluence(double distanceFactor) { | ||
| this.distanceInfluence = distanceFactor; | ||
| return this; | ||
| } | ||
|
|
||
| public double getDistanceInfluence() { | ||
| return distanceInfluence; | ||
| } | ||
|
|
||
| public void setHeadingPenalty(double headingPenalty) { | ||
| this.headingPenalty = headingPenalty; | ||
| } | ||
|
|
||
| public double getHeadingPenalty() { | ||
| return headingPenalty; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "CustomModel{" + | ||
| "distanceInfluence=" + distanceInfluence + | ||
| ", speedFactor=" + speedFactorMap + | ||
| ", maxSpeed=" + maxSpeedMap + | ||
| ", maxSpeedFallback=" + maxSpeedFallback + | ||
| ", priorityMap=" + priorityMap + | ||
| ", #areas=" + areas.size() + | ||
| '}'; | ||
| } | ||
|
|
||
| /** | ||
| * A new CustomModel is created from the baseModel merged with the specified queryModel. | ||
| */ | ||
| public static CustomModel merge(CustomModel baseModel, CustomModel queryModel) { | ||
| // avoid changing the specified CustomModel via deep copy otherwise the server-side CustomModel would be modified (same problem if queryModel would be used as target) | ||
| CustomModel mergedCM = new CustomModel(baseModel); | ||
| if (queryModel.maxSpeedFallback != null) { | ||
| if (mergedCM.maxSpeedFallback != null && mergedCM.maxSpeedFallback > queryModel.maxSpeedFallback) | ||
| throw new IllegalArgumentException("CustomModel in query can only use max_speed_fallback bigger or equal to " + mergedCM.maxSpeedFallback); | ||
| mergedCM.maxSpeedFallback = queryModel.maxSpeedFallback; | ||
| } | ||
| if (Math.abs(queryModel.distanceInfluence - CustomModel.DEFAULT_D_I) > 0.01) { | ||
| if (mergedCM.distanceInfluence > queryModel.distanceInfluence) | ||
| throw new IllegalArgumentException("CustomModel in query can only use distance_influence bigger or equal to " + mergedCM.distanceInfluence); | ||
| mergedCM.distanceInfluence = queryModel.distanceInfluence; | ||
| } | ||
|
|
||
| // example | ||
| // max_speed: { road_class: { secondary : 0.4 } } | ||
| // or | ||
| // priority: { max_weight: { "<3.501": 0.7 } } | ||
| for (Map.Entry<String, Object> queryEntry : queryModel.getMaxSpeed().entrySet()) { | ||
| Object value = mergedCM.maxSpeedMap.get(queryEntry.getKey()); | ||
| applyChange(mergedCM.maxSpeedMap, value, queryEntry); | ||
| } | ||
| for (Map.Entry<String, Object> queryEntry : queryModel.getSpeedFactor().entrySet()) { | ||
| Object value = mergedCM.speedFactorMap.get(queryEntry.getKey()); | ||
| applyChange(mergedCM.speedFactorMap, value, queryEntry); | ||
| } | ||
| for (Map.Entry<String, Object> queryEntry : queryModel.getPriority().entrySet()) { | ||
| Object value = mergedCM.priorityMap.get(queryEntry.getKey()); | ||
| applyChange(mergedCM.priorityMap, value, queryEntry); | ||
| } | ||
| for (Map.Entry<String, JsonFeature> entry : queryModel.getAreas().entrySet()) { | ||
| if (mergedCM.areas.containsKey(entry.getKey())) | ||
| throw new IllegalArgumentException("area " + entry.getKey() + " already exists"); | ||
| mergedCM.areas.put(entry.getKey(), entry.getValue()); | ||
| } | ||
|
|
||
| return mergedCM; | ||
| } | ||
|
|
||
| private static void applyChange(Map<String, Object> mergedSuperMap, | ||
| Object mergedObj, Map.Entry<String, Object> querySuperEntry) { | ||
| if (mergedObj == null) { | ||
| // no need for a merge | ||
| mergedSuperMap.put(querySuperEntry.getKey(), querySuperEntry.getValue()); | ||
| return; | ||
| } | ||
| if (!(mergedObj instanceof Map)) | ||
| throw new IllegalArgumentException(querySuperEntry.getKey() + ": entry is not a map: " + mergedObj); | ||
| Object queryObj = querySuperEntry.getValue(); | ||
| if (!(queryObj instanceof Map)) | ||
| throw new IllegalArgumentException(querySuperEntry.getKey() + ": query entry is not a map: " + queryObj); | ||
|
|
||
| Map<Object, Object> mergedMap = (Map) mergedObj; | ||
| Map<Object, Object> queryMap = (Map) queryObj; | ||
| for (Map.Entry queryEntry : queryMap.entrySet()) { | ||
| if (queryEntry.getKey() == null || queryEntry.getKey().toString().isEmpty()) | ||
| throw new IllegalArgumentException(querySuperEntry.getKey() + ": key cannot be null or empty"); | ||
| String key = queryEntry.getKey().toString(); | ||
| if (isComparison(key)) | ||
| continue; | ||
|
|
||
| Object mergedValue = mergedMap.get(key); | ||
| if (mergedValue == null) { | ||
| mergedMap.put(key, queryEntry.getValue()); | ||
| } else if (multiply(queryEntry.getValue(), mergedValue) != null) { | ||
| // existing value needs to be multiplied | ||
| mergedMap.put(key, multiply(queryEntry.getValue(), mergedValue)); | ||
| } else { | ||
| throw new IllegalArgumentException(querySuperEntry.getKey() + ": cannot merge value " + queryEntry.getValue() + " for key " + key + ", merged value: " + mergedValue); | ||
| } | ||
| } | ||
|
|
||
| // now special handling for comparison keys start e.g. <2 or >3.0, see testMergeComparisonKeys | ||
| // this could be simplified if CustomModel would be already an abstract syntax tree :) | ||
| List<String> queryComparisonKeys = getComparisonKeys(queryMap); | ||
| if (queryComparisonKeys.isEmpty()) | ||
| return; | ||
| if (queryComparisonKeys.size() > 1) | ||
| throw new IllegalArgumentException(querySuperEntry.getKey() + ": entry in " + querySuperEntry.getValue() + " must not contain more than one key comparison but contained " + queryComparisonKeys); | ||
| char opChar = queryComparisonKeys.get(0).charAt(0); | ||
| List<String> mergedComparisonKeys = getComparisonKeys(mergedMap); | ||
| if (mergedComparisonKeys.isEmpty()) { | ||
| mergedMap.put(queryComparisonKeys.get(0), queryMap.get(queryComparisonKeys.get(0))); | ||
| } else if (mergedComparisonKeys.get(0).charAt(0) == opChar) { | ||
| if (multiply(queryMap.get(queryComparisonKeys.get(0)), mergedMap.get(mergedComparisonKeys.get(0))) != 0) | ||
| throw new IllegalArgumentException(querySuperEntry.getKey() + ": currently only blocking comparisons are allowed, but query was " + queryMap.get(queryComparisonKeys.get(0)) + " and server side: " + mergedMap.get(mergedComparisonKeys.get(0))); | ||
|
|
||
| try { | ||
| double comparisonMergedValue = Double.parseDouble(mergedComparisonKeys.get(0).substring(1)); | ||
| double comparisonQueryValue = Double.parseDouble(queryComparisonKeys.get(0).substring(1)); | ||
| if (opChar == '<') { | ||
| if (comparisonMergedValue > comparisonQueryValue) | ||
| throw new IllegalArgumentException(querySuperEntry.getKey() + ": only use a comparison key with a bigger value than " + comparisonMergedValue + " but was " + comparisonQueryValue); | ||
| } else if (opChar == '>') { | ||
| if (comparisonMergedValue < comparisonQueryValue) | ||
| throw new IllegalArgumentException(querySuperEntry.getKey() + ": only use a comparison key with a smaller value than " + comparisonMergedValue + " but was " + comparisonQueryValue); | ||
| } else { | ||
| throw new IllegalArgumentException(querySuperEntry.getKey() + ": only use a comparison key with < or > as operator but was " + opChar); | ||
| } | ||
| mergedMap.remove(mergedComparisonKeys.get(0)); | ||
| mergedMap.put(queryComparisonKeys.get(0), queryMap.get(queryComparisonKeys.get(0))); | ||
| } catch (NumberFormatException ex) { | ||
| throw new IllegalArgumentException(querySuperEntry.getKey() + ": number in one of the 'comparison' keys for " + querySuperEntry.getKey() + " wasn't parsable: " + queryComparisonKeys + " (" + mergedComparisonKeys + ")"); | ||
| } | ||
| } else { | ||
| throw new IllegalArgumentException(querySuperEntry.getKey() + ": comparison keys must match but did not: " + queryComparisonKeys.get(0) + " vs " + mergedComparisonKeys.get(0)); | ||
| } | ||
| } | ||
|
|
||
| static Double multiply(Object queryValue, Object mergedValue) { | ||
| if (queryValue instanceof Number && mergedValue instanceof Number) | ||
| return ((Number) queryValue).doubleValue() * ((Number) mergedValue).doubleValue(); | ||
| return null; | ||
| } | ||
|
|
||
| static boolean isComparison(String key) { | ||
| return key.startsWith("<") || key.startsWith(">"); | ||
| } | ||
|
|
||
| private static List<String> getComparisonKeys(Map<Object, Object> map) { | ||
| List<String> list = new ArrayList<>(); | ||
| for (Map.Entry queryEntry : map.entrySet()) { | ||
| String key = queryEntry.getKey().toString(); | ||
| if (isComparison(key)) list.add(key); | ||
| } | ||
| return list; | ||
| } | ||
| } | ||
109 changes: 109 additions & 0 deletions
109
api/src/test/java/com/graphhopper/routing/util/CustomModelTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package com.graphhopper.routing.util; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class CustomModelTest { | ||
|
|
||
| static CustomModel setValue(CustomModel model, String op, String encodedValue, double value) { | ||
| Map<String, Object> map = new HashMap<>(); | ||
| map.put(op + value, 0); | ||
| model.getPriority().put(encodedValue, map); | ||
| return model; | ||
| } | ||
|
|
||
| Object getValue(CustomModel model, String encodedValue) { | ||
| Map map = (Map) model.getPriority().get(encodedValue); | ||
| if (map == null || map.isEmpty()) | ||
| return null; | ||
| return map.keySet().iterator().next(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMergeComparisonKeys() { | ||
| CustomModel truck = setValue(new CustomModel(), "<", "max_width", 3); | ||
| CustomModel car = setValue(new CustomModel(), "<", "max_width", 2); | ||
| CustomModel bike = setValue(new CustomModel(), "<", "max_weight", 0.02); | ||
|
|
||
| assertEquals("<2.0", getValue(CustomModel.merge(bike, car), "max_width")); | ||
| assertNull(getValue(bike, "max_width")); | ||
| assertNull(getValue(car, "max_weight")); | ||
|
|
||
| assertEquals("<3.0", getValue(CustomModel.merge(car, truck), "max_width")); | ||
| try { | ||
| CustomModel.merge(truck, car); | ||
| fail("car is incompatible to truck (base)"); | ||
| } catch (Exception ex) { | ||
| assertTrue(ex.getMessage().contains("max_width: only use a comparison key with a bigger value than 3.0 but was 2.0"), ex.getMessage()); | ||
| } | ||
|
|
||
| CustomModel car2 = setValue(new CustomModel(), ">", "max_width", 2); | ||
| try { | ||
| CustomModel.merge(truck, car2); | ||
| fail("car is incompatible to car2 (base)"); | ||
| } catch (Exception ex) { | ||
| assertTrue(ex.getMessage().contains("max_width: comparison keys must match but did not: "), ex.getMessage()); | ||
| } | ||
|
|
||
| Map<String, Object> map = new HashMap<>(); | ||
| map.put("<2.0", 0.5); | ||
| CustomModel customModel = new CustomModel(); | ||
| customModel.getPriority().put("max_width", map); | ||
| try { | ||
| CustomModel.merge(customModel, customModel); | ||
| fail("models are incompatible"); | ||
| } catch (Exception ex) { | ||
| assertTrue(ex.getMessage().contains("only blocking comparisons are allowed, but query was 0.5 and server side: 0.5"), ex.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testMergeEmptyModel() { | ||
| CustomModel emptyCar = new CustomModel(); | ||
| CustomModel car = new CustomModel(); | ||
| car.getPriority().put("road_class", createMap("primary", 2.0, "tertiary", 1.2)); | ||
| // empty entries means "extend base" profile, i.e. primary=1 | ||
| Map map = (Map) CustomModel.merge(emptyCar, car).getPriority().get("road_class"); | ||
| assertEquals(2.0, map.get("primary")); | ||
| assertEquals(1.2, map.get("tertiary")); | ||
|
|
||
| map = (Map) CustomModel.merge(car, emptyCar).getPriority().get("road_class"); | ||
| assertEquals(2.0, map.get("primary")); | ||
| assertEquals(1.2, map.get("tertiary")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMergeMap() { | ||
| CustomModel truck = new CustomModel(); | ||
| truck.getPriority().put("road_class", createMap("primary", 1.5, "secondary", 2.0)); | ||
| CustomModel car = new CustomModel(); | ||
| car.getPriority().put("road_class", createMap("primary", 2.0, "tertiary", 1.2)); | ||
|
|
||
| Map map = (Map) CustomModel.merge(truck, car).getPriority().get("road_class"); | ||
| assertEquals(1.5 * 2.0, map.get("primary")); | ||
| assertEquals(2.0, map.get("secondary")); | ||
| assertEquals(1.2, map.get("tertiary")); | ||
|
|
||
| // do not change argument of merge method | ||
| assertEquals(1.5, (Double) ((Map) truck.getPriority().get("road_class")).get("primary"), .1); | ||
|
|
||
| truck.getPriority().put("road_class", createMap("primary", "incompatible")); | ||
| try { | ||
| CustomModel.merge(car, truck); | ||
| fail("we cannot merge this"); | ||
| } catch (Exception ex) { | ||
| } | ||
| } | ||
|
|
||
| Map createMap(Object... objects) { | ||
| Map map = new HashMap(); | ||
| for (int i = 0; i < objects.length; i += 2) { | ||
| map.put(objects[i], objects[i + 1]); | ||
| } | ||
| return map; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.