-
Notifications
You must be signed in to change notification settings - Fork 6
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
15 changed files
with
209 additions
and
50 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
69 changes: 69 additions & 0 deletions
69
server/src/main/scala/kpn/core/tools/next/support/RouteAnalysisCompareTool.scala
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,69 @@ | ||
package kpn.core.tools.next.support | ||
|
||
import kpn.api.custom.Relation | ||
import kpn.core.doc.OldRouteDoc | ||
import kpn.core.doc.RouteDetailDoc | ||
import kpn.core.tools.analysis.AnalysisStartConfiguration | ||
import kpn.core.tools.analysis.AnalysisStartToolOptions | ||
import kpn.core.tools.next.domain.RouteRelation | ||
import kpn.core.tools.next.support.compare.CompareEdges | ||
import kpn.core.util.Log | ||
import kpn.server.analyzer.engine.analysis.route.RouteDetailDocBuilder | ||
|
||
object RouteAnalysisCompareTool { | ||
def main(args: Array[String]): Unit = { | ||
val configuration = new AnalysisStartConfiguration(AnalysisStartToolOptions("kpn-next")) | ||
new RouteAnalysisCompareTool(configuration).analyze() | ||
} | ||
} | ||
|
||
class RouteAnalysisCompareTool(config: AnalysisStartConfiguration) { | ||
|
||
private val log = Log(classOf[RouteAnalysisTool]) | ||
|
||
def analyze(): Unit = { | ||
log.info("Collecting routeIds") | ||
val routeIds = config.oldDatabase.oldRoutes.ids() | ||
log.info(s"Comparing ${routeIds.size} routes") | ||
routeIds.zipWithIndex.foreach { case (routeId, index) => | ||
if (index % 50 == 0) { | ||
log.info(s"${index + 1}/${routeIds.size}") | ||
} | ||
Log.context(s"${index + 1}/${routeIds.size} route=$routeId") { | ||
try { | ||
analyzeAndCompare(routeId) | ||
} catch { | ||
case e: Throwable => | ||
log.error("Error processing route", e) | ||
} | ||
} | ||
} | ||
log.info(s"Done") | ||
} | ||
|
||
private def analyzeAndCompare(routeId: Long): Unit = { | ||
config.nextRepository.nextRouteRelation(routeId) match { | ||
case None => log.error(s"route not found in route-relations") | ||
case Some(nextRouteRelation) => | ||
analyzeRoute(nextRouteRelation.relation, nextRouteRelation.structure) match { | ||
case None => log.error(s"could not analyze route") | ||
case Some(newRouteDetailDoc) => | ||
config.oldDatabase.oldRoutes.findById(routeId) match { | ||
case None => | ||
case Some(oldRouteDoc) => | ||
compare(oldRouteDoc, newRouteDetailDoc) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private def analyzeRoute(relation: Relation, hierarchy: Option[RouteRelation]): Option[RouteDetailDoc] = { | ||
config.routeDetailMainAnalyzer.analyze(relation, hierarchy).map { context => | ||
new RouteDetailDocBuilder(context).build() | ||
} | ||
} | ||
|
||
private def compare(oldRouteDoc: OldRouteDoc, newRouteDoc: RouteDetailDoc): Unit = { | ||
new CompareEdges(oldRouteDoc, newRouteDoc, log).compare() | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
server/src/main/scala/kpn/core/tools/next/support/compare/CompareEdges.scala
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,85 @@ | ||
package kpn.core.tools.next.support.compare | ||
|
||
import kpn.api.common.route.RouteEdge | ||
import kpn.core.doc.OldRouteDoc | ||
import kpn.core.doc.RouteDetailDoc | ||
import kpn.core.util.Log | ||
|
||
import scala.math.abs | ||
|
||
case class CompareEdge( | ||
sourceNodeId: Long, | ||
sinkNodeId: Long, | ||
meters: Long | ||
) | ||
|
||
class CompareEdges(oldRouteDoc: OldRouteDoc, newRouteDoc: RouteDetailDoc, log: Log) { | ||
def compare(): Unit = { | ||
if (newRouteDoc.segments.size == 1) { // cannot compare edges | ||
val oldEdges = oldCompareEdges() | ||
val newEdges = newCompareEdges() | ||
if (!edgesEqual(oldEdges, newEdges)) { | ||
val detail = Seq( | ||
oldRouteDoc.edges.map(edge => s"old-edge $edge"), | ||
newRouteDoc.edges.map(edge => s"new-edge $edge"), | ||
oldEdges.map(edge => s"old-edge $edge"), | ||
newEdges.map(edge => s"new-edge $edge"), | ||
).flatten.mkString("\n") | ||
log.info(s"edge mismatch\n$detail") | ||
} | ||
} | ||
} | ||
|
||
private def newCompareEdges(): Seq[CompareEdge] = { | ||
sort(newRouteDoc.edges.map(toCompareEdge)) | ||
} | ||
|
||
private def oldCompareEdges(): Seq[CompareEdge] = { | ||
sort(distinctOldEdges(Seq.empty, oldRouteDoc.edges)) | ||
} | ||
|
||
private def distinctOldEdges(edges: Seq[CompareEdge], remainingEdges: Seq[RouteEdge]): Seq[CompareEdge] = { | ||
if (remainingEdges.isEmpty) { | ||
edges | ||
} | ||
else { | ||
val edge = toCompareEdge(remainingEdges.head) | ||
if (edges.contains(edge)) { | ||
distinctOldEdges(edges, remainingEdges.tail) | ||
} | ||
else { | ||
distinctOldEdges(edges :+ edge, remainingEdges.tail) | ||
} | ||
} | ||
} | ||
|
||
private def toCompareEdge(edge: RouteEdge): CompareEdge = { | ||
CompareEdge( | ||
edge.sourceNodeId, | ||
edge.sinkNodeId, | ||
edge.meters | ||
) | ||
} | ||
|
||
private def sort(edges: Seq[CompareEdge]): Seq[CompareEdge] = { | ||
edges.sortBy(edge => edge.sourceNodeId -> edge.sinkNodeId) | ||
} | ||
|
||
private def edgesEqual(oldEdges: Seq[CompareEdge], newEdges: Seq[CompareEdge]): Boolean = { | ||
if (oldEdges.size == newEdges.size) { | ||
oldEdges.zip(newEdges).forall { case (oldEdge, newEdge) => | ||
edgeEqual(oldEdge, newEdge) | ||
} | ||
} | ||
else { | ||
false | ||
} | ||
} | ||
|
||
private def edgeEqual(oldEdge: CompareEdge, newEdge: CompareEdge): Boolean = { | ||
val metersEqual = abs(oldEdge.meters - newEdge.meters) < (newEdge.meters / 20) // 20% | ||
oldEdge.sourceNodeId == newEdge.sourceNodeId && | ||
oldEdge.sinkNodeId == newEdge.sinkNodeId && | ||
metersEqual | ||
} | ||
} |
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
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
11 changes: 11 additions & 0 deletions
11
...main/scala/kpn/server/analyzer/engine/analysis/route/structure/StructurePathElement.scala
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