/
CustomModelParserTest.java
330 lines (284 loc) · 17.1 KB
/
CustomModelParserTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* 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.weighting.custom;
import com.graphhopper.routing.ev.*;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.storage.BaseGraph;
import com.graphhopper.util.CustomModel;
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.util.JsonFeature;
import com.graphhopper.util.JsonFeatureCollection;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import static com.graphhopper.json.Statement.*;
import static com.graphhopper.json.Statement.Op.LIMIT;
import static com.graphhopper.json.Statement.Op.MULTIPLY;
import static com.graphhopper.routing.ev.RoadClass.*;
import static com.graphhopper.routing.weighting.custom.CustomModelParser.parseExpressions;
import static org.junit.jupiter.api.Assertions.*;
class CustomModelParserTest {
BaseGraph graph;
EncodingManager encodingManager;
EnumEncodedValue<RoadClass> roadClassEnc;
BooleanEncodedValue accessEnc;
DecimalEncodedValue avgSpeedEnc;
EnumEncodedValue<Country> countryEnc;
EnumEncodedValue<State> stateEnc;
double maxSpeed;
@BeforeEach
void setup() {
accessEnc = VehicleAccess.create("car");
avgSpeedEnc = VehicleSpeed.create("car", 5, 5, false);
countryEnc = Country.create();
stateEnc = State.create();
encodingManager = new EncodingManager.Builder().add(accessEnc).add(avgSpeedEnc)
.add(stateEnc).add(countryEnc).add(MaxSpeed.create()).add(Surface.create()).build();
graph = new BaseGraph.Builder(encodingManager).create();
roadClassEnc = encodingManager.getEnumEncodedValue(RoadClass.KEY, RoadClass.class);
maxSpeed = 140;
}
@Test
void setPriorityForRoadClass() {
CustomModel customModel = new CustomModel();
customModel.addToPriority(If("road_class == PRIMARY", MULTIPLY, "0.5"));
CustomWeighting.EdgeToDoubleMapping priorityMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager,
avgSpeedEnc, maxSpeed, null).getEdgeToPriorityMapping();
BaseGraph graph = new BaseGraph.Builder(encodingManager).create();
EdgeIteratorState edge1 = graph.edge(0, 1).setDistance(100).set(roadClassEnc, RoadClass.PRIMARY);
EdgeIteratorState edge2 = graph.edge(1, 2).setDistance(100).set(roadClassEnc, RoadClass.SECONDARY);
assertEquals(0.5, priorityMapping.get(edge1, false), 1.e-6);
assertEquals(1.0, priorityMapping.get(edge2, false), 1.e-6);
}
@Test
void testPriority() {
EdgeIteratorState primary = graph.edge(0, 1).setDistance(10).
set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80).set(accessEnc, true, true);
EdgeIteratorState secondary = graph.edge(1, 2).setDistance(10).
set(roadClassEnc, SECONDARY).set(avgSpeedEnc, 70).set(accessEnc, true, true);
EdgeIteratorState tertiary = graph.edge(1, 2).setDistance(10).
set(roadClassEnc, TERTIARY).set(avgSpeedEnc, 70).set(accessEnc, true, true);
CustomModel customModel = new CustomModel();
customModel.addToPriority(If("road_class == PRIMARY", MULTIPLY, "0.5"));
customModel.addToPriority(ElseIf("road_class == SECONDARY", MULTIPLY, "0.7"));
customModel.addToPriority(Else(MULTIPLY, "0.9"));
customModel.addToPriority(If("road_environment != FERRY", MULTIPLY, "0.8"));
CustomWeighting.EdgeToDoubleMapping priorityMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager,
avgSpeedEnc, maxSpeed, null).getEdgeToPriorityMapping();
assertEquals(0.5 * 0.8, priorityMapping.get(primary, false), 0.01);
assertEquals(0.7 * 0.8, priorityMapping.get(secondary, false), 0.01);
assertEquals(0.9 * 0.8, priorityMapping.get(tertiary, false), 0.01);
// force integer value
customModel = new CustomModel();
customModel.addToPriority(If("road_class == PRIMARY", MULTIPLY, "1"));
customModel.addToPriority(If("road_class == SECONDARY", MULTIPLY, "0.9"));
priorityMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager,
avgSpeedEnc, maxSpeed, null).getEdgeToPriorityMapping();
assertEquals(1, priorityMapping.get(primary, false), 0.01);
assertEquals(0.9, priorityMapping.get(secondary, false), 0.01);
}
@Test
public void testCountry() {
EdgeIteratorState usRoad = graph.edge(0, 1).setDistance(10).
set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80).set(accessEnc, true, true).
set(countryEnc, Country.USA).set(stateEnc, State.US_AK);
EdgeIteratorState us2Road = graph.edge(0, 1).setDistance(10).
set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80).set(accessEnc, true, true).
set(countryEnc, Country.USA);
EdgeIteratorState deRoad = graph.edge(1, 2).setDistance(10).
set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 70).set(accessEnc, true, true).
set(countryEnc, Country.DEU);
CustomModel customModel = new CustomModel();
customModel.addToPriority(If("country == USA", MULTIPLY, "0.5"));
customModel.addToPriority(If("country == USA && state == US_AK", MULTIPLY, "0.6"));
customModel.addToPriority(If("country == DEU", MULTIPLY, "0.8"));
CustomWeighting.EdgeToDoubleMapping priorityMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager,
avgSpeedEnc, maxSpeed, null).getEdgeToPriorityMapping();
assertEquals(0.6 * 0.5, priorityMapping.get(usRoad, false), 0.01);
assertEquals(0.5, priorityMapping.get(us2Road, false), 0.01);
assertEquals(0.8, priorityMapping.get(deRoad, false), 0.01);
}
@Test
public void testBrackets() {
EdgeIteratorState primary = graph.edge(0, 1).setDistance(10).set(accessEnc, true, true).
set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80);
EdgeIteratorState secondary = graph.edge(0, 1).setDistance(10).set(accessEnc, true, true).
set(roadClassEnc, SECONDARY).set(avgSpeedEnc, 40);
CustomModel customModel = new CustomModel();
customModel.addToPriority(If("(road_class == PRIMARY || car_access == true) && car_average_speed > 50", MULTIPLY, "0.9"));
CustomWeighting.Parameters parameters = CustomModelParser.createWeightingParameters(customModel, encodingManager,
avgSpeedEnc, maxSpeed, null);
assertEquals(0.9, parameters.getEdgeToPriorityMapping().get(primary, false), 0.01);
assertEquals(1, parameters.getEdgeToPriorityMapping().get(secondary, false), 0.01);
}
@Test
public void testSpeedFactorAndPriorityAndMaxSpeed() {
EdgeIteratorState primary = graph.edge(0, 1).setDistance(10).
set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80).set(accessEnc, true, true);
EdgeIteratorState secondary = graph.edge(1, 2).setDistance(10).
set(roadClassEnc, SECONDARY).set(avgSpeedEnc, 70).set(accessEnc, true, true);
CustomModel customModel = new CustomModel();
customModel.addToPriority(If("road_class == PRIMARY", MULTIPLY, "0.9"));
customModel.addToSpeed(If("road_class == PRIMARY", MULTIPLY, "0.8"));
CustomWeighting.Parameters parameters = CustomModelParser.createWeightingParameters(customModel, encodingManager,
avgSpeedEnc, maxSpeed, null);
assertEquals(0.9, parameters.getEdgeToPriorityMapping().get(primary, false), 0.01);
assertEquals(64, parameters.getEdgeToSpeedMapping().get(primary, false), 0.01);
assertEquals(1, parameters.getEdgeToPriorityMapping().get(secondary, false), 0.01);
assertEquals(70, parameters.getEdgeToSpeedMapping().get(secondary, false), 0.01);
customModel.addToSpeed(If("road_class != PRIMARY", LIMIT, "50"));
CustomWeighting.EdgeToDoubleMapping speedMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager,
avgSpeedEnc, maxSpeed, null).getEdgeToSpeedMapping();
assertEquals(64, speedMapping.get(primary, false), 0.01);
assertEquals(50, speedMapping.get(secondary, false), 0.01);
}
@Test
void testIllegalOrder() {
CustomModel customModel = new CustomModel();
customModel.addToPriority(Else(MULTIPLY, "0.9"));
customModel.addToPriority(If("road_environment != FERRY", MULTIPLY, "0.8"));
assertThrows(IllegalArgumentException.class, () -> CustomModelParser.createWeightingParameters(customModel, encodingManager,
avgSpeedEnc, maxSpeed, null));
CustomModel customModel2 = new CustomModel();
customModel2.addToPriority(ElseIf("road_environment != FERRY", MULTIPLY, "0.9"));
customModel2.addToPriority(If("road_class != PRIMARY", MULTIPLY, "0.8"));
assertThrows(IllegalArgumentException.class, () -> CustomModelParser.createWeightingParameters(customModel2, encodingManager,
avgSpeedEnc, maxSpeed, null));
}
@Test
public void multipleAreas() {
CustomModel customModel = new CustomModel();
JsonFeatureCollection areas = new JsonFeatureCollection();
Coordinate[] area_1_coordinates = new Coordinate[]{
new Coordinate(48.019324184801185, 11.28021240234375),
new Coordinate(48.019324184801185, 11.53564453125),
new Coordinate(48.11843396091691, 11.53564453125),
new Coordinate(48.11843396091691, 11.28021240234375),
new Coordinate(48.019324184801185, 11.28021240234375),
};
Coordinate[] area_2_coordinates = new Coordinate[]{
new Coordinate(48.15509285476017, 11.53289794921875),
new Coordinate(48.15509285476017, 11.8212890625),
new Coordinate(48.281365151571755, 11.8212890625),
new Coordinate(48.281365151571755, 11.53289794921875),
new Coordinate(48.15509285476017, 11.53289794921875),
};
areas.getFeatures().add(new JsonFeature("area_1",
"Feature",
null,
new GeometryFactory().createPolygon(area_1_coordinates),
new HashMap<>()));
areas.getFeatures().add(new JsonFeature("area_2",
"Feature",
null,
new GeometryFactory().createPolygon(area_2_coordinates),
new HashMap<>()));
customModel.setAreas(areas);
customModel.addToSpeed(If("in_area_1", LIMIT, "100"));
customModel.addToSpeed(If("!in_area_2", LIMIT, "25"));
customModel.addToSpeed(Else(LIMIT, "15"));
// No exception is thrown during createWeightingParameters
assertAll(() ->
CustomModelParser.createWeightingParameters(customModel, encodingManager,
avgSpeedEnc, maxSpeed, null));
CustomModel customModel2 = new CustomModel();
customModel2.setAreas(areas);
customModel2.addToSpeed(If("in_area_1", LIMIT, "100"));
customModel2.addToSpeed(If("in_area_2", LIMIT, "25"));
customModel2.addToSpeed(If("in_area_3", LIMIT, "150"));
customModel2.addToSpeed(Else(LIMIT, "15"));
assertThrows(IllegalArgumentException.class, () ->
CustomModelParser.createWeightingParameters(customModel2, encodingManager,
avgSpeedEnc, maxSpeed, null));
}
@Test
public void parseValue() {
DecimalEncodedValue maxSpeedEnc = encodingManager.getDecimalEncodedValue(MaxSpeed.KEY);
EdgeIteratorState maxLower = graph.edge(0, 1).setDistance(10).
set(maxSpeedEnc, 60).set(avgSpeedEnc, 70).set(accessEnc, true, true);
EdgeIteratorState maxSame = graph.edge(1, 2).setDistance(10).
set(maxSpeedEnc, 70).set(avgSpeedEnc, 70).set(accessEnc, true, true);
CustomModel customModel = new CustomModel();
customModel.addToSpeed(If("true", LIMIT, "max_speed * 1.1"));
CustomWeighting.EdgeToDoubleMapping speedMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager,
avgSpeedEnc, maxSpeed, null).getEdgeToSpeedMapping();
assertEquals(70.0, speedMapping.get(maxSame, false), 0.01);
assertEquals(66.0, speedMapping.get(maxLower, false), 0.01);
}
@Test
public void parseValueWithError() {
CustomModel customModel1 = new CustomModel();
customModel1.addToSpeed(If("true", LIMIT, "unknown"));
IllegalArgumentException ret = assertThrows(IllegalArgumentException.class,
() -> CustomModelParser.createWeightingParameters(customModel1, encodingManager,
avgSpeedEnc, maxSpeed, null));
assertTrue(ret.getMessage().startsWith("Cannot compile expression: 'unknown' not available"), ret.getMessage());
CustomModel customModel2 = new CustomModel();
customModel2.addToSpeed(If("road_class == PRIMARY", MULTIPLY, "0.5"));
customModel2.addToSpeed(Else(MULTIPLY, "-0.5"));
ret = assertThrows(IllegalArgumentException.class,
() -> CustomModelParser.createWeightingParameters(customModel2, encodingManager,
avgSpeedEnc, maxSpeed, null));
assertTrue(ret.getMessage().startsWith("Cannot compile expression: speed has to be >=0 but can be negative (-0.5)"), ret.getMessage());
CustomModel customModel3 = new CustomModel();
customModel3.addToSpeed(If("road_class == PRIMARY", MULTIPLY, "0.5"));
customModel3.addToSpeed(Else(MULTIPLY, "road_class"));
ret = assertThrows(IllegalArgumentException.class,
() -> CustomModelParser.createWeightingParameters(customModel3, encodingManager,
avgSpeedEnc, maxSpeed, null));
assertTrue(ret.getMessage().contains("Binary numeric promotion not possible on types \"double\" and \"com.graphhopper.routing.ev.RoadClass\""), ret.getMessage());
}
@Test
public void parseConditionWithError() {
NameValidator validVariable = s -> encodingManager.hasEncodedValue(s);
// existing encoded value but not added
IllegalArgumentException ret = assertThrows(IllegalArgumentException.class,
() -> parseExpressions(new StringBuilder(),
validVariable, "[HERE]", new HashSet<>(),
Arrays.asList(If("max_weight > 10", MULTIPLY, "0"))));
assertTrue(ret.getMessage().startsWith("[HERE] invalid condition \"max_weight > 10\": 'max_weight' not available"), ret.getMessage());
// invalid variable or constant (NameValidator returns false)
ret = assertThrows(IllegalArgumentException.class,
() -> parseExpressions(new StringBuilder(),
validVariable, "[HERE]", new HashSet<>(),
Arrays.asList(If("country == GERMANY", MULTIPLY, "0"))));
assertTrue(ret.getMessage().startsWith("[HERE] invalid condition \"country == GERMANY\": 'GERMANY' not available"), ret.getMessage());
// not whitelisted method
ret = assertThrows(IllegalArgumentException.class,
() -> parseExpressions(new StringBuilder(),
validVariable, "[HERE]", new HashSet<>(),
Arrays.asList(If("edge.fetchWayGeometry().size() > 2", MULTIPLY, "0"))));
assertTrue(ret.getMessage().startsWith("[HERE] invalid condition \"edge.fetchWayGeometry().size() > 2\": size is an illegal method"), ret.getMessage());
}
@Test
void testBackwardFunction() {
CustomModel customModel = new CustomModel();
customModel.addToPriority(If("backward_car_access != car_access", MULTIPLY, "0.5"));
CustomWeighting.EdgeToDoubleMapping priorityMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager,
avgSpeedEnc, maxSpeed, null).getEdgeToPriorityMapping();
BaseGraph graph = new BaseGraph.Builder(encodingManager).create();
EdgeIteratorState edge1 = graph.edge(0, 1).setDistance(100).set(accessEnc, true, false);
EdgeIteratorState edge2 = graph.edge(1, 2).setDistance(100).set(accessEnc, true, true);
assertEquals(0.5, priorityMapping.get(edge1, false), 1.e-6);
assertEquals(1.0, priorityMapping.get(edge2, false), 1.e-6);
}
}