Skip to content

Commit

Permalink
Introduce primary and secondary elements for stop clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed May 22, 2024
1 parent e2868c6 commit 6899268
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import java.time.LocalDate;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -213,21 +215,21 @@ class StopClusters {
)
void stopClustersWithTypos(String searchTerm) {
var results = index.queryStopClusters(searchTerm).toList();
var ids = results.stream().map(StopCluster::id).toList();
var ids = results.stream().map(primaryId()).toList();
assertEquals(List.of(ALEXANDERPLATZ_STATION.getId()), ids);
}

@Test
void fuzzyStopClusters() {
var result1 = index.queryStopClusters("arts").map(StopCluster::id).toList();
var result1 = index.queryStopClusters("arts").map(primaryId()).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());
assertEquals(LICHTERFELDE_OST_1.getName().toString(), result.getFirst().primary().name());
}

@ParameterizedTest
Expand Down Expand Up @@ -259,7 +261,7 @@ void deduplicatedStopClusters() {
}
)
void stopClustersWithSpace(String query) {
var result = index.queryStopClusters(query).map(StopCluster::id).toList();
var result = index.queryStopClusters(query).map(primaryId()).toList();
assertEquals(List.of(FIVE_POINTS_STATION.getId()), result);
}

Expand All @@ -268,24 +270,28 @@ void stopClustersWithSpace(String query) {
void fuzzyStopCode(String query) {
var result = index.queryStopClusters(query).toList();
assertEquals(1, result.size());
assertEquals(ARTS_CENTER.getName().toString(), result.getFirst().name());
assertEquals(ARTS_CENTER.getName().toString(), result.getFirst().primary().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());
assertEquals(WESTHAFEN.getName().toString(), stop.primary().name());
assertEquals(List.of(FERRY.name(), BUS.name()), stop.primary().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());
assertEquals(ALEXANDERPLATZ_STATION.getName().toString(), result.primary().name());
assertEquals(List.of(StopClusterMapper.toAgency(BVG)), result.primary().agencies());
assertEquals("A Publisher", result.primary().feedPublisher().name());
}
}

private static @Nonnull Function<StopCluster, FeedScopedId> primaryId() {
return c -> c.primary().id();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -198,7 +199,7 @@ private StopCluster toStopCluster(Document document) {
var feedPublisher = StopClusterMapper.toFeedPublisher(
transitService.getFeedInfo(clusterId.getFeedId())
);
return new StopCluster(
var primary = new StopCluster.Location(
clusterId,
code,
name,
Expand All @@ -207,6 +208,8 @@ private StopCluster toStopCluster(Document document) {
agencies,
feedPublisher
);

return new StopCluster(primary, List.of());
}

static IndexWriterConfig iwcWithSuggestField(Analyzer analyzer, final Set<String> suggestFields) {
Expand Down
28 changes: 21 additions & 7 deletions src/ext/java/org/opentripplanner/ext/geocoder/StopCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import org.opentripplanner.transit.model.framework.FeedScopedId;

Expand All @@ -15,13 +16,8 @@
* - if stops are closer than 10 meters to each and have an identical name, only one is returned
*/
record StopCluster(
FeedScopedId id,
@Nullable String code,
String name,
Coordinate coordinate,
Collection<String> modes,
List<Agency> agencies,
@Nullable FeedPublisher feedPublisher
Location primary,
Collection<Location> secondaries
) {
/**
* Easily serializable version of a coordinate
Expand All @@ -37,4 +33,22 @@ public record Agency(FeedScopedId id, String name) {}
* Easily serializable version of a feed publisher
*/
public record FeedPublisher(String name) {}

public record Location(
FeedScopedId id,
@Nullable String code,
String name,
Coordinate coordinate,
Collection<String> modes,
List<Agency> agencies,
@Nullable FeedPublisher feedPublisher
){
public Location{
Objects.requireNonNull(id);
Objects.requireNonNull(name);
Objects.requireNonNull(coordinate);
Objects.requireNonNull(modes);
Objects.requireNonNull(agencies);
}
}
}

0 comments on commit 6899268

Please sign in to comment.