forked from opentripplanner/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLuceneIndexTest.java
294 lines (264 loc) · 8.92 KB
/
LuceneIndexTest.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
package org.opentripplanner.ext.geocoder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.opentripplanner.transit.model._data.TransitModelForTest.id;
import static org.opentripplanner.transit.model.basic.TransitMode.BUS;
import static org.opentripplanner.transit.model.basic.TransitMode.FERRY;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.opentripplanner.model.FeedInfo;
import org.opentripplanner.transit.model._data.TransitModelForTest;
import org.opentripplanner.transit.model.basic.TransitMode;
import org.opentripplanner.transit.model.framework.Deduplicator;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.organization.Agency;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.Station;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;
class LuceneIndexTest {
private static final TransitModelForTest TEST_MODEL = TransitModelForTest.of();
static final Agency BVG = Agency
.of(id("bvg"))
.withName("BVG")
.withTimezone("Europe/Berlin")
.build();
// Berlin
static final Station BERLIN_HAUPTBAHNHOF_STATION = TEST_MODEL
.station("Hauptbahnhof")
.withCoordinate(52.52495, 13.36952)
.build();
static final Station ALEXANDERPLATZ_STATION = TEST_MODEL
.station("Alexanderplatz")
.withCoordinate(52.52277, 13.41046)
.build();
static final RegularStop ALEXANDERPLATZ_BUS = TEST_MODEL
.stop("Alexanderplatz Bus")
.withCoordinate(52.52277, 13.41046)
.withVehicleType(BUS)
.withParentStation(ALEXANDERPLATZ_STATION)
.build();
static final RegularStop ALEXANDERPLATZ_RAIL = TEST_MODEL
.stop("Alexanderplatz S-Bahn")
.withCoordinate(52.52157, 13.41123)
.withVehicleType(TransitMode.RAIL)
.withParentStation(ALEXANDERPLATZ_STATION)
.build();
static final RegularStop LICHTERFELDE_OST_1 = TEST_MODEL
.stop("Lichterfelde Ost")
.withId(id("lichterfelde-gleis-1"))
.withCoordinate(52.42986, 13.32808)
.build();
static final RegularStop LICHTERFELDE_OST_2 = TEST_MODEL
.stop("Lichterfelde Ost")
.withId(id("lichterfelde-gleis-2"))
.withCoordinate(52.42985, 13.32807)
.build();
static final RegularStop WESTHAFEN = TEST_MODEL
.stop("Westhafen")
.withVehicleType(null)
.withCoordinate(52.42985, 13.32807)
.build();
// Atlanta
static final Station FIVE_POINTS_STATION = TEST_MODEL
.station("Five Points")
.withCoordinate(33.753899, -84.39156)
.build();
static final RegularStop ARTS_CENTER = TEST_MODEL
.stop("Arts Center")
.withCode("4456")
.withCoordinate(52.52277, 13.41046)
.build();
static final RegularStop ARTHUR = TEST_MODEL
.stop("Arthur Langford Jr Pl SW at 220")
.withCoordinate(52.52277, 13.41046)
.build();
static LuceneIndex index;
static StopClusterMapper mapper;
@BeforeAll
static void setup() {
var stopModel = TEST_MODEL.stopModelBuilder();
List
.of(
ALEXANDERPLATZ_BUS,
ALEXANDERPLATZ_RAIL,
LICHTERFELDE_OST_1,
LICHTERFELDE_OST_2,
WESTHAFEN,
ARTS_CENTER,
ARTHUR
)
.forEach(stopModel::withRegularStop);
List
.of(ALEXANDERPLATZ_STATION, BERLIN_HAUPTBAHNHOF_STATION, FIVE_POINTS_STATION)
.forEach(stopModel::withStation);
var transitModel = new TransitModel(stopModel.build(), new Deduplicator());
transitModel.index();
var transitService = new DefaultTransitService(transitModel) {
private final Multimap<StopLocation, TransitMode> modes = ImmutableMultimap
.<StopLocation, TransitMode>builder()
.putAll(WESTHAFEN, FERRY, BUS)
.build();
@Override
public List<TransitMode> getModesOfStopLocation(StopLocation stop) {
if (stop.getGtfsVehicleType() != null) {
return List.of(stop.getGtfsVehicleType());
} else {
return List.copyOf(modes.get(stop));
}
}
@Override
public List<Agency> getAgenciesForStopLocation(StopLocation stop) {
if (stop.equals(ALEXANDERPLATZ_BUS)) {
return List.of(BVG);
} else {
return List.of();
}
}
@Override
public Agency getAgencyForId(FeedScopedId id) {
if (id.equals(BVG.getId())) {
return BVG;
}
return null;
}
@Override
public FeedInfo getFeedInfo(String feedId) {
return new FeedInfo(
"F",
"A Publisher",
"http://example.com",
"de",
LocalDate.MIN,
LocalDate.MIN,
"1"
);
}
};
index = new LuceneIndex(transitService);
mapper = new StopClusterMapper(transitService);
}
@Test
void stopLocations() {
var result1 = index.queryStopLocations("lich", true).toList();
assertEquals(1, result1.size());
assertEquals(LICHTERFELDE_OST_1.getName().toString(), result1.getFirst().getName().toString());
var result2 = index.queryStopLocations("alexan", true).collect(Collectors.toSet());
assertEquals(Set.of(ALEXANDERPLATZ_BUS, ALEXANDERPLATZ_RAIL), result2);
}
@Test
void stopLocationGroups() {
var result1 = index.queryStopLocationGroups("alex", true).toList();
assertEquals(List.of(ALEXANDERPLATZ_STATION), result1);
var result2 = index.queryStopLocationGroups("haupt", true).toList();
assertEquals(List.of(BERLIN_HAUPTBAHNHOF_STATION), result2);
}
@Test
void stopLocationGroupsWithSpace() {
var result1 = index.queryStopLocationGroups("five points", true).toList();
assertEquals(List.of(FIVE_POINTS_STATION), result1);
}
@Nested
class StopClusters {
@ParameterizedTest
@ValueSource(
strings = {
"Alexanderplatz",
"Alexa",
"alex",
"aleyanderplazt",
"alexnderplazt",
"Alexnderplatz",
"Alexnaderplatz",
"xande",
"xanderpla",
"alexnaderplaz",
"Alexanderplat",
"alexanderplat",
"alexand",
"alexander platz",
"alexander-platz",
"alexander",
}
)
void stopClustersWithTypos(String searchTerm) {
var results = index.queryStopClusters(searchTerm).toList();
var ids = results.stream().map(StopCluster::id).toList();
assertEquals(List.of(ALEXANDERPLATZ_STATION.getId()), ids);
}
@Test
void fuzzyStopClusters() {
var result1 = index.queryStopClusters("arts").map(StopCluster::id).toList();
assertEquals(List.of(ARTS_CENTER.getId()), result1);
}
@Test
void deduplicatedStopClusters() {
var result = index.queryStopClusters("lich").toList();
assertEquals(1, result.size());
assertEquals(LICHTERFELDE_OST_1.getName().toString(), result.getFirst().name());
}
@ParameterizedTest
@ValueSource(
strings = {
"five",
"five ",
"five p",
"five po",
"five poi",
"five poin",
"five point",
"five points",
"fife point",
"five poits",
"fife",
"points",
"ife points",
"the five points",
"five @ points",
"five @ the points",
"five@points",
"five at points",
"five&points",
"five & points",
"five and the points",
"points five",
"points fife",
}
)
void stopClustersWithSpace(String query) {
var result = index.queryStopClusters(query).map(StopCluster::id).toList();
assertEquals(List.of(FIVE_POINTS_STATION.getId()), result);
}
@ParameterizedTest
@ValueSource(strings = { "4456", "445" })
void fuzzyStopCode(String query) {
var result = index.queryStopClusters(query).toList();
assertEquals(1, result.size());
assertEquals(ARTS_CENTER.getName().toString(), result.getFirst().name());
}
@Test
void modes() {
var result = index.queryStopClusters("westh").toList();
assertEquals(1, result.size());
var stop = result.getFirst();
assertEquals(WESTHAFEN.getName().toString(), stop.name());
assertEquals(List.of(FERRY.name(), BUS.name()), stop.modes());
}
@Test
void agenciesAndFeedPublisher() {
var result = index.queryStopClusters("alexanderplatz").toList().getFirst();
assertEquals(ALEXANDERPLATZ_STATION.getName().toString(), result.name());
assertEquals(List.of(StopClusterMapper.toAgency(BVG)), result.agencies());
assertEquals("A Publisher", result.feedPublisher().name());
}
}
}