-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compare routes using step names #3301
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
libnavigation-core/src/main/java/com/mapbox/navigation/core/fasterroute/RouteComparator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.mapbox.navigation.core.fasterroute | ||
|
||
import com.mapbox.api.directions.v5.models.DirectionsRoute | ||
import com.mapbox.api.directions.v5.models.LegStep | ||
import com.mapbox.navigation.base.trip.model.RouteProgress | ||
|
||
/** | ||
* Compares if an alternative route is different from the current route progress. | ||
*/ | ||
internal class RouteComparator { | ||
|
||
private val mapLegStepToName: (LegStep) -> String = { it.name() ?: "" } | ||
|
||
/** | ||
* @param routeProgress current route progress | ||
* @param alternativeRoute suggested new route | ||
* | ||
* @return true when the alternative route has different | ||
* geometry from the current route progress | ||
*/ | ||
fun isRouteDescriptionDifferent(routeProgress: RouteProgress, alternativeRoute: DirectionsRoute): Boolean { | ||
val currentDescription = routeDescription(routeProgress) | ||
val alternativeDescription = alternativeDescription(alternativeRoute) | ||
alternativeDescription.ifEmpty { | ||
return false | ||
} | ||
|
||
return currentDescription != alternativeDescription | ||
} | ||
|
||
private fun routeDescription(routeProgress: RouteProgress): String { | ||
val routeLeg = routeProgress.currentLegProgress?.routeLeg | ||
val steps = routeLeg?.steps() | ||
val stepIndex = routeProgress.currentLegProgress?.currentStepProgress?.stepIndex | ||
?: return "" | ||
|
||
return steps?.listIterator(stepIndex)?.asSequence() | ||
?.joinToString(transform = mapLegStepToName) ?: "" | ||
} | ||
|
||
private fun alternativeDescription(directionsRoute: DirectionsRoute): String { | ||
val steps = directionsRoute.legs()?.firstOrNull()?.steps() | ||
return steps?.asSequence() | ||
?.joinToString(transform = mapLegStepToName) ?: "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,22 @@ import com.mapbox.api.directions.v5.models.DirectionsRoute | |
import com.mapbox.navigation.base.trip.model.RouteProgress | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
|
||
class FasterRouteDetectorTest { | ||
|
||
private val fasterRouteDetector = FasterRouteDetector() | ||
private val routeComparator: RouteComparator = mockk { | ||
every { isRouteDescriptionDifferent(any(), any()) } returns true | ||
} | ||
|
||
private val fasterRouteDetector = FasterRouteDetector(routeComparator) | ||
|
||
@Test | ||
fun shouldDetectWhenRouteIsFaster() { | ||
fun shouldDetectWhenRouteIsFaster() = runBlocking { | ||
every { routeComparator.isRouteDescriptionDifferent(any(), any()) } returns true | ||
val newRoute: DirectionsRoute = mockk() | ||
every { newRoute.duration() } returns 402.6 | ||
val routeProgress: RouteProgress = mockk() | ||
|
@@ -25,7 +31,20 @@ class FasterRouteDetectorTest { | |
} | ||
|
||
@Test | ||
fun shouldDetectWhenRouteIsSlower() { | ||
fun shouldDetectWhenRouteIsFasterOnlyIfDifferent() = runBlocking { | ||
every { routeComparator.isRouteDescriptionDifferent(any(), any()) } returns false | ||
val newRoute: DirectionsRoute = mockk() | ||
every { newRoute.duration() } returns 402.6 | ||
val routeProgress: RouteProgress = mockk() | ||
every { routeProgress.durationRemaining } returns 797.447 | ||
|
||
val isFasterRoute = fasterRouteDetector.isRouteFaster(newRoute, routeProgress) | ||
|
||
assertFalse(isFasterRoute) | ||
} | ||
|
||
@Test | ||
fun shouldDetectWhenRouteIsSlower() = runBlocking { | ||
val newRoute: DirectionsRoute = mockk() | ||
every { newRoute.duration() } returns 512.2 | ||
val routeProgress: RouteProgress = mockk() | ||
|
@@ -37,7 +56,7 @@ class FasterRouteDetectorTest { | |
} | ||
|
||
@Test | ||
fun shouldDefaultToFalseWhenDurationIsNull() { | ||
fun shouldDefaultToFalseWhenDurationIsNull() = runBlocking { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The answer is in, runBlocking is better than runBlockingTest |
||
val newRoute: DirectionsRoute = mockk() | ||
every { newRoute.duration() } returns null | ||
val routeProgress: RouteProgress = mockk() | ||
|
@@ -49,7 +68,7 @@ class FasterRouteDetectorTest { | |
} | ||
|
||
@Test | ||
fun shouldNotAllowSlightlyFasterRoutes() { | ||
fun shouldNotAllowSlightlyFasterRoutes() = runBlocking { | ||
val newRoute: DirectionsRoute = mockk() | ||
every { newRoute.duration() } returns 634.7 | ||
val routeProgress: RouteProgress = mockk() | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These test values are covered in FasterRouteDetectorTest