-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
136 additions
and
109 deletions.
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
119 changes: 87 additions & 32 deletions
119
shared/src/commonMain/kotlin/com/mbta/tid/mbta_app/model/AlertAssociatedStop.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 |
---|---|---|
@@ -1,48 +1,103 @@ | ||
package com.mbta.tid.mbta_app.model | ||
|
||
private val disruptableStopTypes: List<LocationType> = | ||
listOf(LocationType.STOP, LocationType.STATION) | ||
|
||
class AlertAssociatedStop( | ||
val stop: Stop, | ||
val relevantAlerts: Set<Alert>, | ||
val routePatterns: List<RoutePattern>, | ||
val childStops: Map<String, Stop>, | ||
val relevantAlerts: List<Alert>, | ||
val serviceAlerts: List<Alert>, | ||
val childAlerts: Map<String, AlertAssociatedStop>, | ||
val serviceStatus: StopServiceStatus | ||
) { | ||
val serviceAlerts = relevantAlerts.filter { Alert.serviceDisruptionEffects.contains(it.effect) } | ||
constructor( | ||
stop: Stop, | ||
relevantAlerts: List<Alert>, | ||
routePatterns: List<RoutePattern>, | ||
childStops: Map<String, Stop>, | ||
childAlerts: Map<String, AlertAssociatedStop> | ||
) : this( | ||
stop, | ||
relevantAlerts, | ||
getServiceAlerts(relevantAlerts), | ||
childAlerts, | ||
getServiceStatus( | ||
stop, | ||
getServiceAlerts(relevantAlerts), | ||
routePatterns, | ||
childStops, | ||
childAlerts | ||
) | ||
) | ||
} | ||
|
||
enum class StopServiceStatus { | ||
NORMAL, | ||
NO_SERVICE, | ||
PARTIAL_SERVICE | ||
} | ||
|
||
private fun entityMatcher( | ||
entity: Alert.InformedEntity, | ||
stop: Stop, | ||
pattern: RoutePattern | ||
): Boolean { | ||
return entity.appliesTo( | ||
stopId = stop.id, | ||
routeId = pattern.routeId, | ||
directionId = pattern.directionId | ||
) | ||
} | ||
|
||
private fun getDisruptableChildren(childStops: Map<String, Stop>): List<Stop> { | ||
return childStops.values.filter { | ||
listOf(LocationType.STOP, LocationType.STATION).contains(it.locationType) | ||
} | ||
} | ||
|
||
var hasNoService: Boolean = | ||
private fun getServiceAlerts(alerts: List<Alert>): List<Alert> { | ||
return alerts.filter { Alert.serviceDisruptionEffects.contains(it.effect) } | ||
} | ||
|
||
private fun getServiceStatus( | ||
stop: Stop, | ||
serviceAlerts: List<Alert>, | ||
routePatterns: List<RoutePattern>, | ||
childStops: Map<String, Stop>, | ||
childAlerts: Map<String, AlertAssociatedStop> | ||
): StopServiceStatus { | ||
val children = getDisruptableChildren(childStops) | ||
|
||
val hasNoService = | ||
if (routePatterns.isEmpty()) { | ||
// No route patterns and every child station/stop has no service | ||
childStops.isNotEmpty() && | ||
childStops.values | ||
.filter { disruptableStopTypes.contains(it.locationType) } | ||
.all { childAlerts[it.id]?.hasNoService == true } | ||
childStops.isNotEmpty() && children.all { hasNoService(it, childAlerts) } | ||
} else { | ||
// All route patterns and child stations/stops have no service | ||
routePatterns.all { pattern -> | ||
serviceAlerts.any { alert -> | ||
alert.anyInformedEntity { entityMatcher(it, stop, pattern) } | ||
} | ||
} && | ||
childStops.values | ||
.filter { disruptableStopTypes.contains(it.locationType) } | ||
.all { childAlerts[it.id]?.hasNoService == true } | ||
routePatterns.all { isDisruptedPattern(it, stop, serviceAlerts) } && | ||
children.all { hasNoService(it, childAlerts) } | ||
} | ||
if (hasNoService) { | ||
return StopServiceStatus.NO_SERVICE | ||
} | ||
|
||
val hasSomeDisruptedService: Boolean = | ||
routePatterns.any { isDisruptedPattern(it, stop, serviceAlerts) } || | ||
children.any { hasNoService(it, childAlerts) } | ||
if (hasSomeDisruptedService) { | ||
return StopServiceStatus.PARTIAL_SERVICE | ||
} | ||
|
||
var hasSomeDisruptedService: Boolean = | ||
routePatterns.any { pattern -> | ||
serviceAlerts.any { alert -> | ||
alert.anyInformedEntity { entityMatcher(it, stop, pattern) } | ||
} | ||
} || stop.childStopIds?.any { childAlerts[it]?.hasSomeDisruptedService == true } == true | ||
return StopServiceStatus.NORMAL | ||
} | ||
|
||
private fun entityMatcher( | ||
entity: Alert.InformedEntity, | ||
stop: Stop, | ||
pattern: RoutePattern | ||
): Boolean { | ||
return entity.appliesTo(stopId = stop.id, routeId = pattern.routeId) | ||
private fun hasNoService(stop: Stop, alerts: Map<String, AlertAssociatedStop>): Boolean { | ||
return alerts[stop.id]?.serviceStatus == StopServiceStatus.NO_SERVICE | ||
} | ||
|
||
private fun isDisruptedPattern( | ||
pattern: RoutePattern, | ||
stop: Stop, | ||
serviceAlerts: List<Alert> | ||
): Boolean { | ||
return serviceAlerts.any { alert -> | ||
alert.anyInformedEntity { entityMatcher(it, stop, pattern) } | ||
} | ||
} |
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
Oops, something went wrong.