Skip to content

Commit

Permalink
#368 migrate structure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarc committed Jul 12, 2024
1 parent 285d5fd commit f19bef1
Show file tree
Hide file tree
Showing 79 changed files with 1,183 additions and 1,076 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package kpn.server.analyzer.engine.analysis.route
case class RouteNodeAnalysis(
reversed: Boolean = false, // TODO redesign - obsolete?
freeNodes: Seq[RouteNodeData] = Seq.empty, // TODO redesign - obsolete?
startNodes: Seq[RouteNodeData] = Seq.empty,
endNodes: Seq[RouteNodeData] = Seq.empty,
startNode: Option[RouteNodeData] = None,
endNode: Option[RouteNodeData] = None,
startTentacleFromNodes: Seq[RouteNodeData] = Seq.empty,
endTentacleToNodes: Seq[RouteNodeData] = Seq.empty,
redundantNodes: Seq[RouteNodeData] = Seq.empty
) {
def nodes: Seq[RouteNodeData] = freeNodes ++ startNodes ++ endNodes ++ redundantNodes
def nodes: Seq[RouteNodeData] = startNode.toSeq ++ endNode.toSeq ++ startTentacleFromNodes ++ endTentacleToNodes

def nodeIds: Seq[Long] = (startNodes ++ endNodes).map(_.node.id)
def nodeIds: Seq[Long] = nodes.map(_.node.id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ class RouteNodeAnalysisFormatter(analysis: RouteNodeAnalysis) {
def nodeStrings: Seq[String] = {
List(
nodeStrings("Free", analysis.freeNodes),
nodeStrings("Start", analysis.startNodes),
nodeStrings("End", analysis.endNodes),
nodeStrings("Start", analysis.startNode.toSeq),
nodeStrings("End", analysis.endNode.toSeq),
nodeStrings("Start tentacle from", analysis.startTentacleFromNodes),
nodeStrings("End tentacle to", analysis.endTentacleToNodes),
nodeStrings("Redundant", analysis.redundantNodes),
if (analysis.reversed) Seq("(reversed)") else Seq.empty
).flatten
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ class RouteNodeAnalyzer(context: RouteDetailAnalysisContext) {
val routeNodeAnalysis = RouteNodeAnalysis(
reversed = false,
freeNodes = Seq.empty,
startNodes = startNodes,
endNodes = endNodes,
startNode = startNodes.lastOption,
endNode = endNodes.headOption,
startTentacleFromNodes = startNodes.dropRight(1),
endTentacleToNodes = endNodes.drop(1),
redundantNodes = redundantNodes
)

if (routeNodeAnalysis.startNodes.nonEmpty && !routeNodeAnalysis.startNodes.exists(_.isInWay)) {
if (routeNodeAnalysis.startNode.nonEmpty && !routeNodeAnalysis.startNode.exists(_.isInWay)) {
facts += RouteNodeMissingInWays
}
else if (routeNodeAnalysis.endNodes.nonEmpty && !routeNodeAnalysis.endNodes.exists(_.isInWay)) {
else if (routeNodeAnalysis.endNode.nonEmpty && !routeNodeAnalysis.endNode.exists(_.isInWay)) {
facts += RouteNodeMissingInWays
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ class RouteLinksReport(context: RouteDetailAnalysisContext) {

private def networkNodeString(nodeIds: Seq[Long]): String = {
Seq(
networkNodeStrings(nodeIds, "start", context.routeNodeAnalysis.startNodes),
networkNodeStrings(nodeIds, "end", context.routeNodeAnalysis.endNodes),
networkNodeStrings(nodeIds, "start", context.routeNodeAnalysis.startNode.toSeq),
networkNodeStrings(nodeIds, "end", context.routeNodeAnalysis.endNode.toSeq),
networkNodeStrings(nodeIds, "start tentacle", context.routeNodeAnalysis.startTentacleFromNodes),
networkNodeStrings(nodeIds, "end tentacle", context.routeNodeAnalysis.endTentacleToNodes),
networkNodeStrings(nodeIds, "free", context.routeNodeAnalysis.freeNodes),
networkNodeStrings(nodeIds, "redundant", context.routeNodeAnalysis.redundantNodes)
).flatten.mkString(", ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object RouteNodeAnalysisReport {

def report(context: RouteDetailAnalysisContext): String = {
val routeNodeAnalysis = context.routeNodeAnalysis
if (routeNodeAnalysis.startNodes.nonEmpty || routeNodeAnalysis.endNodes.nonEmpty || routeNodeAnalysis.freeNodes.nonEmpty || routeNodeAnalysis.redundantNodes.nonEmpty) {
if (routeNodeAnalysis.startNode.nonEmpty || routeNodeAnalysis.endNode.nonEmpty || routeNodeAnalysis.freeNodes.nonEmpty || routeNodeAnalysis.redundantNodes.nonEmpty) {
s"""
|<table>
| <tr class="header">
Expand All @@ -19,8 +19,8 @@ object RouteNodeAnalysisReport {
| <td>name</td>
| <td>isInWay</td>
| </tr>
| ${routeNodeAnalysis.startNodes.map(n => routeNodeReport("startNode", n)).mkString}
| ${routeNodeAnalysis.endNodes.map(n => routeNodeReport("endNode", n)).mkString}
| ${routeNodeAnalysis.startNode.map(n => routeNodeReport("startNode", n)).mkString}
| ${routeNodeAnalysis.endNode.map(n => routeNodeReport("endNode", n)).mkString}
| ${routeNodeAnalysis.freeNodes.map(n => routeNodeReport("freeNode", n)).mkString}
| ${routeNodeAnalysis.redundantNodes.map(n => routeNodeReport("redundantNode", n)).mkString}
| <tr><td colspan="4">reversed: ${routeNodeAnalysis.reversed}</td></tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ class RouteSegmentReport(context: RouteDetailAnalysisContext) {

private def networkNodeString(nodeIds: Seq[Long]): String = {
Seq(
networkNodeStrings(nodeIds, "start", context.routeNodeAnalysis.startNodes),
networkNodeStrings(nodeIds, "end", context.routeNodeAnalysis.endNodes),
networkNodeStrings(nodeIds, "start", context.routeNodeAnalysis.startNode.toSeq),
networkNodeStrings(nodeIds, "end", context.routeNodeAnalysis.endNode.toSeq),
networkNodeStrings(nodeIds, "start tentacle", context.routeNodeAnalysis.startTentacleFromNodes),
networkNodeStrings(nodeIds, "end tentacle", context.routeNodeAnalysis.endTentacleToNodes),
networkNodeStrings(nodeIds, "free", context.routeNodeAnalysis.freeNodes),
networkNodeStrings(nodeIds, "redundant", context.routeNodeAnalysis.redundantNodes)
).flatten.mkString(", ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ class StructureElementGroupsReport(context: RouteDetailAnalysisContext) {

private def networkNodes(fragment: StructureFragment): String = {
Seq(
fragmentNodes(fragment, "start", context.routeNodeAnalysis.startNodes),
fragmentNodes(fragment, "end", context.routeNodeAnalysis.endNodes),
fragmentNodes(fragment, "start", context.routeNodeAnalysis.startNode.toSeq),
fragmentNodes(fragment, "end", context.routeNodeAnalysis.endNode.toSeq),
fragmentNodes(fragment, "start tentacle", context.routeNodeAnalysis.startTentacleFromNodes),
fragmentNodes(fragment, "end tentacle", context.routeNodeAnalysis.endTentacleToNodes),
fragmentNodes(fragment, "free", context.routeNodeAnalysis.freeNodes),
fragmentNodes(fragment, "redundant", context.routeNodeAnalysis.redundantNodes)
).flatten.mkString(", ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class OldStructureAnalyzer(traceEnabled: Boolean = false) {

def analyze(routeNodeAnalysis: RouteNodeAnalysis, elementGroups: Seq[StructureElementGroup]): OldStructure = {

val mainStartNode = routeNodeAnalysis.startNodes.lastOption
val mainEndNode = routeNodeAnalysis.endNodes.headOption
val mainStartNode = routeNodeAnalysis.startNode.lastOption
val mainEndNode = routeNodeAnalysis.endNode.headOption

if (elementGroups.size != 1) {
val otherPaths: Seq[OldStructurePath] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,34 @@ class RouteSegmentAnalyzer(context: RouteDetailAnalysisContext) {
}

if (context.routeNodeAnalysis.nodeIds.contains(linkFragments.head.fromNodeId)) {
if (fragments.nonEmpty) {
elements += buildSegmentElement(fragments.toSeq)
fragments.clear()
}
finalizeSegmentElement()
}

linkFragments.foreach { fragment =>
if (context.routeNodeAnalysis.nodeIds.contains(fragment.nodeIds.last)) {
fragments += fragment
elements += buildSegmentElement(fragments.toSeq)
fragments.clear()
finalizeSegmentElement()
}
else {
fragments += fragment
}
}

if (!currentRouteLinkWay.link.hasNext) {
if (fragments.nonEmpty) {
elements += buildSegmentElement(fragments.toSeq)
fragments.clear()
}
finalizeSegmentElement()
segments += buildSegment(segments.size + 1, elements.toSeq)
elements.clear()
}
else {
val change = isDirectionChange(currentRouteLinkWay, nextRouteLinkWayOption)
if (change) {
elements += buildSegmentElement(fragments.toSeq)
fragments.clear()
finalizeSegmentElement()
}
}
}
}

if (fragments.nonEmpty) { // TODO redesign - do we ever get here?
elements += buildSegmentElement(fragments.toSeq)
fragments.clear()
}
finalizeSegmentElement()

if (elements.nonEmpty) { // TODO redesign - do we ever get here?
segments += buildSegment(segments.size + 1, elements.toSeq)
Expand All @@ -85,11 +74,7 @@ class RouteSegmentAnalyzer(context: RouteDetailAnalysisContext) {
}

private def handleRoundabout(previousRouteLinkWayOption: Option[RouteLinkWay], currentRouteLinkWay: RouteLinkWay, nextRouteLinkWayOption: Option[RouteLinkWay]) = {
// finalize current element, if any
if (fragments.nonEmpty) {
elements += buildSegmentElement(fragments.toSeq)
fragments.clear()
}
finalizeSegmentElement()

nextRouteLinkWayOption match {
case None =>
Expand Down Expand Up @@ -239,7 +224,7 @@ class RouteSegmentAnalyzer(context: RouteDetailAnalysisContext) {
}
}

def buildFragmentElement(routeLinkWay: RouteLinkWay, direction: RoutePathDirection, nodeIds: Seq[Long]): NewRouteSegmentElement = {
private def buildFragmentElement(routeLinkWay: RouteLinkWay, direction: RoutePathDirection, nodeIds: Seq[Long]): NewRouteSegmentElement = {
// this is a closed loop at the end of the route
val fragment = NewRouteSegmentElementFragment(
fragmentIds.next(),
Expand Down Expand Up @@ -274,4 +259,11 @@ class RouteSegmentAnalyzer(context: RouteDetailAnalysisContext) {
Seq.empty // filled in later during path analysis
)
}

private def finalizeSegmentElement(): Unit = {
if (fragments.nonEmpty) {
elements += buildSegmentElement(fragments.toSeq)
fragments.clear()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,18 @@ class StructureAnalyzer(traceEnabled: Boolean = false) {
)
}
else {
val mainStartNodeOption = routeNodeAnalysis.startNodes.lastOption
val mainEndNodeOption = routeNodeAnalysis.endNodes.headOption

val structureOption = mainStartNodeOption match {
val structureOption = routeNodeAnalysis.startNode match {
case None => None
case Some(mainStartNode) =>
mainEndNodeOption match {
routeNodeAnalysis.endNode match {
case None => None
case Some(mainEndNode) =>
doAnalyzeNodeNetworkRoute(
routeNodeAnalysis: RouteNodeAnalysis,
mainStartNode: RouteNodeData,
mainEndNode: RouteNodeData,
segments: Seq[NewRouteSegment],
paths: Seq[RoutePath]
routeNodeAnalysis,
mainStartNode,
mainEndNode,
segments,
paths
)
}
}
Expand Down Expand Up @@ -92,7 +89,7 @@ class StructureAnalyzer(traceEnabled: Boolean = false) {
}

val backwardPath: Option[StructurePath] = {
paths.reverse.find(_.toNodeId == mainEndNode.node.id) match {
paths.find(_.toNodeId == mainEndNode.node.id) match {
case None => None
case Some(lastBackwardPath) =>
Some(
Expand All @@ -110,32 +107,40 @@ class StructureAnalyzer(traceEnabled: Boolean = false) {
}
}

val usedPathIds = forwardPath.toSeq.flatMap(_.pathIds) ++ backwardPath.toSeq.flatMap(_.pathIds)
val remainingPaths = paths.filterNot(path => usedPathIds.contains(path.id))

val startTentacleFromNodes = routeNodeAnalysis.startNodes.dropRight(1)
val startTentaclePaths = startTentacleFromNodes.flatMap { fromNode =>
remainingPaths.find(_.fromNodeId == fromNode.node.id) match {
case None => None
case Some(firstPath) =>
Some(
StructurePath(
firstPath.fromNodeId,
firstPath.toNodeId,
Seq(
StructurePathElement(
firstPath,
reversed = false
val startTentaclePaths = {
val usedPathIds = forwardPath.toSeq.flatMap(_.pathIds) ++ backwardPath.toSeq.flatMap(_.pathIds)
val remainingPaths = paths.filterNot(path => usedPathIds.contains(path.id))
routeNodeAnalysis.startTentacleFromNodes.flatMap { fromNode =>
remainingPaths.find(_.fromNodeId == fromNode.node.id) match {
case None => None
case Some(firstPath) =>
Some(
StructurePath(
firstPath.fromNodeId,
firstPath.toNodeId,
Seq(
StructurePathElement(
firstPath,
reversed = false
)
)
)
)
)
}
}
}

val endTentacleToNodes = routeNodeAnalysis.endNodes.drop(1)
val endTentaclePaths = endTentacleToNodes.flatMap { toNode =>
remainingPaths.find(_.toNodeId == toNode.node.id) match {
val endTentaclePaths = routeNodeAnalysis.endTentacleToNodes.flatMap { toNode =>
val usedPathIds = forwardPath.toSeq.flatMap(_.pathIds) ++ backwardPath.toSeq.flatMap(_.pathIds) ++ startTentaclePaths.flatMap(_.pathIds)
val remainingPaths = paths.filterNot(path => usedPathIds.contains(path.id))
remainingPaths.find { path =>
if (path.direction == RoutePathDirection.Backward) {
path.fromNodeId == toNode.node.id
}
else {
path.toNodeId == toNode.node.id
}
} match {
case None => None
case Some(firstPath) =>
Some(
Expand Down
Loading

0 comments on commit f19bef1

Please sign in to comment.