Skip to content

Commit e2686bd

Browse files
authored
Use LMProfile instead of just Weighting for LMPreparationHandler (graphhopper#1899)
1 parent ca78601 commit e2686bd

16 files changed

+176
-118
lines changed

core/src/main/java/com/graphhopper/GraphHopper.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.graphhopper.routing.ch.CHPreparationHandler;
2929
import com.graphhopper.routing.ch.CHRoutingAlgorithmFactory;
3030
import com.graphhopper.routing.lm.LMPreparationHandler;
31+
import com.graphhopper.routing.lm.LMProfile;
3132
import com.graphhopper.routing.profiles.DefaultEncodedValueFactory;
3233
import com.graphhopper.routing.profiles.EncodedValueFactory;
3334
import com.graphhopper.routing.profiles.EnumEncodedValue;
@@ -774,7 +775,7 @@ public boolean load(String graphHopperFolder) {
774775

775776
GHLock lock = null;
776777
try {
777-
// create locks only if writes are allowed, if they are not allowed a lock cannot be created
778+
// create locks only if writes are allowed, if they are not allowed a lock cannot be created
778779
// (e.g. on a read only filesystem locks would fail)
779780
if (ghStorage.getDirectory().getDefaultType().isStoring() && isAllowWrites()) {
780781
lockFactory.setLockDir(new File(ghLocation));
@@ -821,6 +822,9 @@ public final CHPreparationHandler getCHPreparationHandler() {
821822

822823
private void initCHPreparationHandler() {
823824
if (!chPreparationHandler.hasCHProfiles()) {
825+
if (chPreparationHandler.getCHProfileStrings().isEmpty())
826+
throw new IllegalStateException("Potential bug: chProfileStrings is empty");
827+
824828
for (FlagEncoder encoder : encodingManager.fetchEdgeEncoders()) {
825829
for (String chWeightingStr : chPreparationHandler.getCHProfileStrings()) {
826830
// ghStorage is null at this point
@@ -851,14 +855,17 @@ public final LMPreparationHandler getLMPreparationHandler() {
851855
}
852856

853857
private void initLMPreparationHandler() {
854-
if (lmPreparationHandler.hasWeightings())
858+
if (lmPreparationHandler.hasLMProfiles())
855859
return;
856860

861+
if (lmPreparationHandler.getLMProfileStrings().isEmpty()) {
862+
throw new IllegalStateException("Potential bug: lmProfileStrings is empty");
863+
}
857864
for (FlagEncoder encoder : encodingManager.fetchEdgeEncoders()) {
858-
for (String lmWeightingStr : lmPreparationHandler.getWeightingsAsStrings()) {
865+
for (String lmWeightingStr : lmPreparationHandler.getLMProfileStrings()) {
859866
// note that we do not consider turn costs during LM preparation?
860867
Weighting weighting = createWeighting(new HintsMap(lmWeightingStr), encoder, NO_TURN_COST_PROVIDER);
861-
lmPreparationHandler.addWeighting(weighting);
868+
lmPreparationHandler.addLMProfile(new LMProfile(weighting));
862869
}
863870
}
864871
}

core/src/main/java/com/graphhopper/routing/ch/CHPreparationHandler.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ public void init(GraphHopperConfig ghConfig) {
7979

8080
if ("no".equals(chWeightingsStr) || "false".equals(chWeightingsStr)) {
8181
// default is fastest and we need to clear this explicitly
82-
chProfileStrings.clear();
82+
setCHProfilesAsStrings(Collections.<String>emptyList());
8383
} else if (!chWeightingsStr.isEmpty()) {
8484
setCHProfilesAsStrings(Arrays.asList(chWeightingsStr.split(",")));
8585
}
8686

87-
boolean enableThis = !chProfileStrings.isEmpty();
87+
boolean enableThis = !getCHProfileStrings().isEmpty();
8888
setEnabled(enableThis);
8989
if (enableThis)
9090
setDisablingAllowed(ghConfig.getBool(CH.INIT_DISABLING_ALLOWED, isDisablingAllowed()));
9191

9292
String edgeBasedCHStr = ghConfig.get(CH.PREPARE + "edge_based", "off").trim();
9393
edgeBasedCHStr = edgeBasedCHStr.equals("false") ? "off" : edgeBasedCHStr;
94-
edgeBasedCHMode = EdgeBasedCHMode.valueOf(edgeBasedCHStr.toUpperCase(Locale.ROOT));
94+
setEdgeBasedCHMode(EdgeBasedCHMode.valueOf(edgeBasedCHStr.toUpperCase(Locale.ROOT)));
9595

9696
pMap = ghConfig.asPMap();
9797
}
@@ -185,9 +185,6 @@ public EdgeBasedCHMode getEdgeBasedCHMode() {
185185
}
186186

187187
public List<String> getCHProfileStrings() {
188-
if (chProfileStrings.isEmpty())
189-
throw new IllegalStateException("Potential bug: chProfileStrings is empty");
190-
191188
return new ArrayList<>(chProfileStrings);
192189
}
193190

@@ -200,9 +197,6 @@ public CHPreparationHandler setCHProfileStrings(String... profileStrings) {
200197
* @see #addCHProfileAsString(String)
201198
*/
202199
public CHPreparationHandler setCHProfilesAsStrings(List<String> profileStrings) {
203-
if (profileStrings.isEmpty())
204-
throw new IllegalArgumentException("It is not allowed to pass an empty list of CH profile strings");
205-
206200
chProfileStrings.clear();
207201
for (String profileString : profileStrings) {
208202
profileString = toLowerCase(profileString);

core/src/main/java/com/graphhopper/routing/lm/LMPreparationHandler.java

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import com.graphhopper.routing.RoutingAlgorithmFactorySimple;
2727
import com.graphhopper.routing.ch.CHPreparationHandler;
2828
import com.graphhopper.routing.util.HintsMap;
29-
import com.graphhopper.routing.weighting.AbstractWeighting;
30-
import com.graphhopper.routing.weighting.Weighting;
3129
import com.graphhopper.storage.Graph;
3230
import com.graphhopper.storage.GraphHopperStorage;
3331
import com.graphhopper.storage.StorableProperties;
@@ -59,8 +57,8 @@ public class LMPreparationHandler {
5957
private final List<PrepareLandmarks> preparations = new ArrayList<>();
6058
// input weighting list from configuration file
6159
// one such entry can result into multiple Weighting objects e.g. fastest & car,foot => fastest|car and fastest|foot
62-
private final List<String> weightingsAsStrings = new ArrayList<>();
63-
private final List<Weighting> weightings = new ArrayList<>();
60+
private final List<String> lmProfileStrings = new ArrayList<>();
61+
private final List<LMProfile> lmProfiles = new ArrayList<>();
6462
private final Map<String, Double> maximumWeights = new HashMap<>();
6563
private boolean enabled = false;
6664
private int minNodes = -1;
@@ -89,10 +87,10 @@ public void init(GraphHopperConfig ghConfig) {
8987
String lmWeightingsStr = ghConfig.get(Landmark.PREPARE + "weightings", "");
9088
if (!lmWeightingsStr.isEmpty() && !lmWeightingsStr.equalsIgnoreCase("no") && !lmWeightingsStr.equalsIgnoreCase("false")) {
9189
List<String> tmpLMWeightingList = Arrays.asList(lmWeightingsStr.split(","));
92-
setWeightingsAsStrings(tmpLMWeightingList);
90+
setLMProfileStrings(tmpLMWeightingList);
9391
}
9492

95-
boolean enableThis = !weightingsAsStrings.isEmpty();
93+
boolean enableThis = !getLMProfileStrings().isEmpty();
9694
setEnabled(enableThis);
9795
if (enableThis)
9896
setDisablingAllowed(ghConfig.getBool(Landmark.INIT_DISABLING_ALLOWED, isDisablingAllowed()));
@@ -137,40 +135,34 @@ public void setPreparationThreads(int preparationThreads) {
137135
}
138136

139137
/**
140-
* Enables the use of contraction hierarchies to reduce query times. Enabled by default.
138+
* Enables the use of landmarks to reduce query times.
141139
*
142-
* @param weightingList A list containing multiple weightings like: "fastest", "shortest" or
143-
* your own weight-calculation type.
140+
* @param lmProfilesStrings A list containing multiple lm profiles like: "fastest", "shortest" or
141+
* your own weight-calculation type.
144142
*/
145-
public LMPreparationHandler setWeightingsAsStrings(List<String> weightingList) {
146-
if (weightingList.isEmpty())
147-
throw new IllegalArgumentException("It is not allowed to pass an emtpy weightingList");
148-
149-
weightingsAsStrings.clear();
150-
for (String strWeighting : weightingList) {
151-
strWeighting = toLowerCase(strWeighting);
152-
strWeighting = strWeighting.trim();
153-
addWeighting(strWeighting);
143+
public LMPreparationHandler setLMProfileStrings(List<String> lmProfilesStrings) {
144+
lmProfileStrings.clear();
145+
for (String profileStr : lmProfilesStrings) {
146+
profileStr = toLowerCase(profileStr);
147+
profileStr = profileStr.trim();
148+
addLMProfileAsString(profileStr);
154149
}
155150
return this;
156151
}
157152

158-
public List<String> getWeightingsAsStrings() {
159-
if (this.weightingsAsStrings.isEmpty())
160-
throw new IllegalStateException("Potential bug: weightingsAsStrings is empty");
161-
162-
return this.weightingsAsStrings;
153+
public List<String> getLMProfileStrings() {
154+
return lmProfileStrings;
163155
}
164156

165-
public LMPreparationHandler addWeighting(String weighting) {
166-
String[] str = weighting.split("\\|");
157+
public LMPreparationHandler addLMProfileAsString(String profile) {
158+
String[] str = profile.split("\\|");
167159
double value = -1;
168160
if (str.length > 1) {
169-
PMap map = new PMap(weighting);
161+
PMap map = new PMap(profile);
170162
value = map.getDouble("maximum", -1);
171163
}
172164

173-
weightingsAsStrings.add(str[0]);
165+
lmProfileStrings.add(str[0]);
174166
maximumWeights.put(str[0], value);
175167
return this;
176168
}
@@ -179,26 +171,26 @@ public LMPreparationHandler addWeighting(String weighting) {
179171
* Decouple weightings from PrepareLandmarks as we need weightings for the graphstorage and the
180172
* graphstorage for the preparation.
181173
*/
182-
public LMPreparationHandler addWeighting(Weighting weighting) {
183-
weightings.add(weighting);
174+
public LMPreparationHandler addLMProfile(LMProfile lmProfile) {
175+
lmProfiles.add(lmProfile);
184176
return this;
185177
}
186178

187179
public LMPreparationHandler addPreparation(PrepareLandmarks plm) {
188180
preparations.add(plm);
189181
int lastIndex = preparations.size() - 1;
190-
if (lastIndex >= weightings.size())
191-
throw new IllegalStateException("Cannot access weighting for PrepareLandmarks with " + plm.getWeighting()
192-
+ ". Call add(Weighting) before");
182+
if (lastIndex >= lmProfiles.size())
183+
throw new IllegalStateException("Cannot access profile for PrepareLandmarks with " + plm.getLMProfile()
184+
+ ". Call add(LMProfile) before");
193185

194-
if (preparations.get(lastIndex).getWeighting() != weightings.get(lastIndex))
195-
throw new IllegalArgumentException("Weighting of PrepareContractionHierarchies " + preparations.get(lastIndex).getWeighting()
196-
+ " needs to be identical to previously added " + weightings.get(lastIndex));
186+
if (preparations.get(lastIndex).getLMProfile() != lmProfiles.get(lastIndex))
187+
throw new IllegalArgumentException("LMProfile of PrepareLandmarks " + preparations.get(lastIndex).getLMProfile()
188+
+ " needs to be identical to previously added " + lmProfiles.get(lastIndex));
197189
return this;
198190
}
199191

200-
public boolean hasWeightings() {
201-
return !weightings.isEmpty();
192+
public boolean hasLMProfiles() {
193+
return !lmProfiles.isEmpty();
202194
}
203195

204196
public boolean hasPreparations() {
@@ -209,10 +201,6 @@ public int size() {
209201
return preparations.size();
210202
}
211203

212-
public List<Weighting> getWeightings() {
213-
return weightings;
214-
}
215-
216204
public List<PrepareLandmarks> getPreparations() {
217205
return preparations;
218206
}
@@ -230,10 +218,10 @@ public RoutingAlgorithmFactory getAlgorithmFactory(HintsMap map) {
230218
return new LMRoutingAlgorithmFactory(preparations.get(0), new RoutingAlgorithmFactorySimple());
231219
}
232220

233-
List<Weighting> lmWeightings = new ArrayList<>(preparations.size());
221+
List<String> lmProfiles = new ArrayList<>(preparations.size());
234222
for (final PrepareLandmarks p : preparations) {
235-
lmWeightings.add(p.getWeighting());
236-
if (p.getWeighting().matches(map))
223+
lmProfiles.add(p.getLMProfile().getName());
224+
if (p.getLMProfile().getWeighting().matches(map))
237225
return new LMRoutingAlgorithmFactory(p, new RoutingAlgorithmFactorySimple());
238226
}
239227

@@ -244,7 +232,7 @@ public RoutingAlgorithmFactory getAlgorithmFactory(HintsMap map) {
244232
String requestedString = (map.getWeighting().isEmpty() ? "*" : map.getWeighting()) + "|" +
245233
(map.getVehicle().isEmpty() ? "*" : map.getVehicle());
246234
throw new IllegalArgumentException("Cannot find matching LM profile for your request." +
247-
"\nrequested: " + requestedString + "\navailable: " + lmWeightings);
235+
"\nrequested: " + requestedString + "\navailable: " + lmProfiles);
248236
}
249237

250238
/**
@@ -271,9 +259,9 @@ public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) {
271259
}
272260

273261
/**
274-
* This method calculates the landmark data for all weightings (optionally in parallel) or if already existent loads it.
262+
* This method calculates the landmark data for all profiles (optionally in parallel) or if already existent loads it.
275263
*
276-
* @return true if the preparation data for at least one weighting was calculated.
264+
* @return true if the preparation data for at least one profile was calculated.
277265
* @see CHPreparationHandler#prepare(StorableProperties, boolean) for a very similar method
278266
*/
279267
public boolean loadOrDoWork(final StorableProperties properties, final boolean closeEarly) {
@@ -283,14 +271,14 @@ public boolean loadOrDoWork(final StorableProperties properties, final boolean c
283271
for (final PrepareLandmarks plm : preparations) {
284272
counter++;
285273
final int tmpCounter = counter;
286-
final String name = AbstractWeighting.weightingToFileName(plm.getWeighting());
274+
final String name = plm.getLMProfile().getName();
287275
completionService.submit(new Runnable() {
288276
@Override
289277
public void run() {
290278
if (plm.loadExisting())
291279
return;
292280

293-
LOGGER.info(tmpCounter + "/" + getPreparations().size() + " calling LM prepare.doWork for " + plm.getWeighting() + " ... (" + getMemInfo() + ")");
281+
LOGGER.info(tmpCounter + "/" + getPreparations().size() + " calling LM prepare.doWork for " + plm.getLMProfile().getWeighting() + " ... (" + getMemInfo() + ")");
294282
prepared.set(true);
295283
Thread.currentThread().setName(name);
296284
plm.doWork();
@@ -321,7 +309,7 @@ public void run() {
321309
public void createPreparations(GraphHopperStorage ghStorage, LocationIndex locationIndex) {
322310
if (!isEnabled() || !preparations.isEmpty())
323311
return;
324-
if (weightings.isEmpty())
312+
if (lmProfiles.isEmpty())
325313
throw new IllegalStateException("No landmark weightings found");
326314

327315
List<LandmarkSuggestion> lmSuggestions = new ArrayList<>(lmSuggestionsLocations.size());
@@ -335,14 +323,14 @@ public void createPreparations(GraphHopperStorage ghStorage, LocationIndex locat
335323
}
336324
}
337325

338-
for (Weighting weighting : getWeightings()) {
339-
Double maximumWeight = maximumWeights.get(weighting.getName());
326+
for (LMProfile lmProfile : lmProfiles) {
327+
Double maximumWeight = maximumWeights.get(lmProfile.getWeighting().getName());
340328
if (maximumWeight == null)
341329
throw new IllegalStateException("maximumWeight cannot be null. Default should be just negative. " +
342-
"Couldn't find " + weighting.getName() + " in " + maximumWeights);
330+
"Couldn't find " + lmProfile.getName() + " in " + maximumWeights);
343331

344332
PrepareLandmarks tmpPrepareLM = new PrepareLandmarks(ghStorage.getDirectory(), ghStorage,
345-
weighting, landmarkCount, activeLandmarkCount).
333+
lmProfile, landmarkCount, activeLandmarkCount).
346334
setLandmarkSuggestions(lmSuggestions).
347335
setMaximumWeight(maximumWeight).
348336
setLogDetails(logDetails);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to GraphHopper GmbH under one or more contributor
3+
* license agreements. See the NOTICE file distributed with this work for
4+
* additional information regarding copyright ownership.
5+
*
6+
* GraphHopper GmbH licenses this file to you under the Apache License,
7+
* Version 2.0 (the "License"); you may not use this file except in
8+
* compliance with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.graphhopper.routing.lm;
20+
21+
import com.graphhopper.routing.weighting.AbstractWeighting;
22+
import com.graphhopper.routing.weighting.Weighting;
23+
24+
import java.util.Objects;
25+
26+
public class LMProfile {
27+
private final String profileName;
28+
private final Weighting weighting;
29+
30+
public LMProfile(Weighting weighting) {
31+
this(AbstractWeighting.weightingToFileName(weighting), weighting);
32+
}
33+
34+
public LMProfile(String profileName, Weighting weighting) {
35+
this.profileName = profileName;
36+
this.weighting = weighting;
37+
}
38+
39+
public String getName() {
40+
return profileName;
41+
}
42+
43+
public Weighting getWeighting() {
44+
return weighting;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return profileName;
50+
}
51+
52+
@Override
53+
public boolean equals(Object o) {
54+
if (this == o) return true;
55+
if (o == null || getClass() != o.getClass()) return false;
56+
LMProfile lmProfile = (LMProfile) o;
57+
return Objects.equals(profileName, lmProfile.profileName);
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return profileName.hashCode();
63+
}
64+
}

0 commit comments

Comments
 (0)