From 17e54c4d6292be146a2577c7537076ae1a7d7c08 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Sun, 15 May 2016 20:05:17 -0300 Subject: [PATCH] Navitia: Support Ambiguous QueryTripsResult --- .../pte/AbstractNavitiaProvider.java | 30 +++++++++++++++++-- .../live/AbstractNavitiaProviderLiveTest.java | 26 ++++++++++++++++ .../pte/live/ParisProviderLiveTest.java | 14 +++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/enabler/src/de/schildbach/pte/AbstractNavitiaProvider.java b/enabler/src/de/schildbach/pte/AbstractNavitiaProvider.java index 63fec412..e89f2fd3 100644 --- a/enabler/src/de/schildbach/pte/AbstractNavitiaProvider.java +++ b/enabler/src/de/schildbach/pte/AbstractNavitiaProvider.java @@ -1191,10 +1191,36 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider throw new ParserException(jsonExc); } } - else + else if (from != null && to != null) { - return new QueryTripsResult(resultHeader, QueryTripsResult.Status.NO_TRIPS); + List ambiguousFrom = null, ambiguousTo = null; + Location newFrom = null, newTo = null; + + if (!from.isIdentified() && from.hasName()) + { + ambiguousFrom = suggestLocations(from.name).getLocations(); + if (ambiguousFrom.isEmpty()) + return new QueryTripsResult(resultHeader, QueryTripsResult.Status.UNKNOWN_FROM); + if (ambiguousFrom.size() == 1 && ambiguousFrom.get(0).isIdentified()) + newFrom = ambiguousFrom.get(0); + } + + if (!to.isIdentified() && to.hasName()) + { + ambiguousTo = suggestLocations(to.name).getLocations(); + if (ambiguousTo.isEmpty()) + return new QueryTripsResult(resultHeader, QueryTripsResult.Status.UNKNOWN_TO); + if (ambiguousTo.size() == 1 && ambiguousTo.get(0).isIdentified()) + newTo = ambiguousTo.get(0); + } + + if (newTo != null && newFrom != null) + return queryTrips(newFrom, via, newTo, date, dep, products, optimize, walkSpeed, accessibility, options); + + if (ambiguousFrom != null || ambiguousTo != null) + return new QueryTripsResult(resultHeader, ambiguousFrom, null, ambiguousTo); } + return new QueryTripsResult(resultHeader, QueryTripsResult.Status.NO_TRIPS); } catch (final NotFoundException fnfExc) { diff --git a/enabler/test/de/schildbach/pte/live/AbstractNavitiaProviderLiveTest.java b/enabler/test/de/schildbach/pte/live/AbstractNavitiaProviderLiveTest.java index 93fc681f..b69aa5b1 100644 --- a/enabler/test/de/schildbach/pte/live/AbstractNavitiaProviderLiveTest.java +++ b/enabler/test/de/schildbach/pte/live/AbstractNavitiaProviderLiveTest.java @@ -224,6 +224,32 @@ public abstract class AbstractNavitiaProviderLiveTest extends AbstractProviderLi print(result); } + protected final void queryTripAmbiguousFrom(final Location from, final CharSequence to) throws IOException + { + final SuggestLocationsResult toResult = suggestLocations(to); + assertTrue(toResult.getLocations().size() > 0); + + final QueryTripsResult result = queryTrips(from, null, toResult.getLocations().get(0), new Date(), true, + Product.ALL, WalkSpeed.NORMAL, Accessibility.NEUTRAL); + assertEquals(QueryTripsResult.Status.AMBIGUOUS, result.status); + assertTrue(result.ambiguousFrom != null); + assertTrue(result.ambiguousFrom.size() > 0); + print(result); + } + + protected final void queryTripAmbiguousTo(final CharSequence from, final Location to) throws IOException + { + final SuggestLocationsResult fromResult = suggestLocations(from); + assertTrue(fromResult.getLocations().size() > 0); + + final QueryTripsResult result = queryTrips(fromResult.getLocations().get(0), null, to, new Date(), true, + Product.ALL, WalkSpeed.NORMAL, Accessibility.NEUTRAL); + assertEquals(QueryTripsResult.Status.AMBIGUOUS, result.status); + assertTrue(result.ambiguousTo != null); + assertTrue(result.ambiguousTo.size() > 0); + print(result); + } + protected final void queryTripSlowWalk(final CharSequence from, final CharSequence to) throws IOException { final SuggestLocationsResult fromResult = suggestLocations(from); diff --git a/enabler/test/de/schildbach/pte/live/ParisProviderLiveTest.java b/enabler/test/de/schildbach/pte/live/ParisProviderLiveTest.java index 775822c8..fac8b266 100644 --- a/enabler/test/de/schildbach/pte/live/ParisProviderLiveTest.java +++ b/enabler/test/de/schildbach/pte/live/ParisProviderLiveTest.java @@ -22,6 +22,8 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; import de.schildbach.pte.ParisProvider; +import de.schildbach.pte.dto.Location; +import de.schildbach.pte.dto.LocationType; import de.schildbach.pte.dto.Point; /** @@ -178,6 +180,18 @@ public class ParisProviderLiveTest extends AbstractNavitiaProviderLiveTest queryTripUnknownTo("secretan buttes chaumont paris"); } + @Test + public void queryTripAmbiguousFrom() throws Exception + { + queryTripAmbiguousFrom(new Location(LocationType.ANY, "ambiguous", null, "Eiffel"), "Gare St-Lazare"); + } + + @Test + public void queryTripAmbiguousTo() throws Exception + { + queryTripAmbiguousTo("Gare St-Lazare", new Location(LocationType.ANY, "ambiguous", null, "Eiffel")); + } + @Test public void queryTripSlowWalk() throws Exception {