Skip to content

Commit

Permalink
#278 monitor route analysis - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarc committed Jan 12, 2024
1 parent 0800146 commit fb3c7de
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package kpn.server.analyzer.engine.monitor

object ElementDirection extends Enumeration {
val Up, Down = Value
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package kpn.server.analyzer.engine.monitor

object MonitorRouteElement {
def from(fragments: Seq[MonitorRouteFragment], direction: Option[String]): MonitorRouteElement = {
def from(fragments: Seq[MonitorRouteFragment], direction: Option[ElementDirection.Value]): MonitorRouteElement = {
MonitorRouteElement(0, fragments, direction)
}
}

case class MonitorRouteElement(id: Long, fragments: Seq[MonitorRouteFragment], direction: Option[String]) {
case class MonitorRouteElement(
id: Long,
fragments: Seq[MonitorRouteFragment],
direction: Option[ElementDirection.Value]
) {

def startNodeId: Long = {
fragments.head.startNode.id
Expand All @@ -19,7 +23,7 @@ case class MonitorRouteElement(id: Long, fragments: Seq[MonitorRouteFragment], d
def isLoop: Boolean = {
startNodeId == endNodeId
}

def string: String = {
val endNodeIds = fragments.map(_.endNode.id)
val nodeString = startNodeId.toString + endNodeIds.mkString(">", ">", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object MonitorRouteElementAnalyzer {

class MonitorRouteElementAnalyzer(wayMembers: Seq[WayMember]) {

private var oneWayDirection: Option[String] = None // "up" or "down"
private var elementDirection: Option[ElementDirection.Value] = None

private val elementGroups = mutable.Buffer[MonitorRouteElementGroup]()
private val elements = mutable.Buffer[MonitorRouteElement]()
Expand All @@ -46,19 +46,19 @@ class MonitorRouteElementAnalyzer(wayMembers: Seq[WayMember]) {
private def processWayMember(): Unit = {
debug(s"way ${contextCurrentWayMember.way.id} role=${contextCurrentWayMember.role}")
if (contextCurrentWayMember.isUnidirectional) {
if (oneWayDirection.isEmpty) {
if (elementDirection.isEmpty) {
processFirstUnidirectionalWay()
}
else {
processNextUnidirectionalWay()
}
}
else {
if (oneWayDirection.nonEmpty) {
if (elementDirection.nonEmpty) {
debug(s" first way after unidirectional element")

oneWayDirection match {
case Some("up") => reverseFragments()
elementDirection match {
case Some(ElementDirection.Up) => reverseFragments()
case _ =>
}

Expand Down Expand Up @@ -86,12 +86,12 @@ class MonitorRouteElementAnalyzer(wayMembers: Seq[WayMember]) {
val fragments1 = element.fragments.takeWhile(fragment => fragment.startNode.id != splitNodeId)
val fragments2 = element.fragments.drop(fragments1.length)

val oppositeDirection = oneWayDirection match {
case Some("down") => Some("up")
case Some("up") => Some("down")
val oppositeDirection = elementDirection match {
case Some(ElementDirection.Down) => Some(ElementDirection.Up)
case Some(ElementDirection.Up) => Some(ElementDirection.Down)
case _ => None
}
val element1 = MonitorRouteElement.from(fragments1, oneWayDirection)
val element1 = MonitorRouteElement.from(fragments1, elementDirection)
val element2 = MonitorRouteElement.from(fragments2, oppositeDirection)

debug(s" split element1: direction=${element1.direction}, fragments: ${element1.fragments.map(_.string).mkString(", ")}")
Expand All @@ -105,7 +105,7 @@ class MonitorRouteElementAnalyzer(wayMembers: Seq[WayMember]) {
}
}

oneWayDirection = None
elementDirection = None
addBidirectionalFragmentByLookingAheadAtNextWayMember()
}
else {
Expand Down Expand Up @@ -203,21 +203,21 @@ class MonitorRouteElementAnalyzer(wayMembers: Seq[WayMember]) {
case None =>
// the first way in the route has a role
if (contextCurrentWayMember.hasRoleForward) {
oneWayDirection = Some("down")
elementDirection = Some(ElementDirection.Down)
} else {
oneWayDirection = Some("up")
elementDirection = Some(ElementDirection.Up)
}

case Some(previousElement) =>
if (previousElement.endNodeId == contextCurrentWayMember.startNode.id) {
oneWayDirection = Some("down")
elementDirection = Some(ElementDirection.Down)
}
else if (previousElement.endNodeId == contextCurrentWayMember.startNode.id) {
oneWayDirection = Some("up")
elementDirection = Some(ElementDirection.Up)
}
else {
finalizeCurrentGroup()
oneWayDirection = Some("down")
elementDirection = Some(ElementDirection.Down)
}
}

Expand All @@ -230,7 +230,7 @@ class MonitorRouteElementAnalyzer(wayMembers: Seq[WayMember]) {
}

private def processNextUnidirectionalWay(): Unit = {
if (oneWayDirection.contains("down")) {
if (elementDirection.contains(ElementDirection.Down)) {
findPreviousFragment() match {
case None =>
throw new Exception("illegal state??")
Expand All @@ -246,7 +246,7 @@ class MonitorRouteElementAnalyzer(wayMembers: Seq[WayMember]) {
if (contextCurrentWayMember.endNode.id == firstFragmentInElement.startNode.id) {
// switch
finalizeCurrentElement()
oneWayDirection = Some("up")
elementDirection = Some(ElementDirection.Up)
addFragment(contextCurrentWayMember)
}
else {
Expand All @@ -273,7 +273,7 @@ class MonitorRouteElementAnalyzer(wayMembers: Seq[WayMember]) {
if (contextCurrentWayMember.endNode.id == firstFragmentInElement.startNode.id) {
// switch
finalizeCurrentElement()
oneWayDirection = Some("down")
elementDirection = Some(ElementDirection.Down)
addFragment(contextCurrentWayMember)
}
else {
Expand Down Expand Up @@ -307,8 +307,8 @@ class MonitorRouteElementAnalyzer(wayMembers: Seq[WayMember]) {
}

private def addElement(fragments: Seq[MonitorRouteFragment]): MonitorRouteElement = {
debug(s" add element: direction=$oneWayDirection, fragments: ${fragments.map(_.string).mkString(", ")}")
val element = MonitorRouteElement.from(fragments, oneWayDirection)
debug(s" add element: direction=$elementDirection, fragments: ${fragments.map(_.string).mkString(", ")}")
val element = MonitorRouteElement.from(fragments, elementDirection)
elements.addOne(element)
element
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class MonitorRouteElementAnalyzerTest extends UnitTest {
).shouldMatchTo(
Seq(
Seq(
"1>2 (down)",
"1>2 (Down)",
"2>3"
)
)
Expand All @@ -101,7 +101,7 @@ class MonitorRouteElementAnalyzerTest extends UnitTest {
).shouldMatchTo(
Seq(
Seq(
"2>1 (down)",
"2>1 (Down)",
),
Seq(
"3>2"
Expand All @@ -124,8 +124,8 @@ class MonitorRouteElementAnalyzerTest extends UnitTest {
Seq(
Seq(
"1>2",
"2>3>8 (down)",
"8>7>2 (up)",
"2>3>8 (Down)",
"8>7>2 (Up)",
"8>9",
)
)
Expand All @@ -148,8 +148,8 @@ class MonitorRouteElementAnalyzerTest extends UnitTest {
Seq(
Seq(
"1>2",
"2>3>8 (down)",
"8>7>2 (up)",
"2>3>8 (Down)",
"8>7>2 (Up)",
"8>9",
),
Seq(
Expand Down

0 comments on commit fb3c7de

Please sign in to comment.