Advertisement
Guest User

Untitled

a guest
Sep 13th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.68 KB | None | 0 0
  1.  
  2. sealed class GraphItem {
  3.  
  4.     data class Node(val x: Int, val y: Int) : GraphItem(), Comparable<Node> {
  5.  
  6.         companion object Factory {
  7.  
  8.             fun fromGraphObject(node: GraphObject): Node {
  9.  
  10.                 return Node(
  11.                         node.attribute.getValueAt("tileX").toString().toInt(),
  12.                         node.attribute.getValueAt("tileY").toString().toInt()
  13.                 )
  14.             }
  15.         }
  16.  
  17.         override fun compareTo(other: Node) = compareValuesBy(this, other, { it.x }, { it.y })
  18.  
  19.     }
  20.  
  21.     data class Edge(val type: String, val source: Node, val target: Node) :  GraphItem(), Comparable<Edge> {
  22.  
  23.         companion object Factory {
  24.  
  25.             fun fromGraphObject(node: GraphObject): Edge {
  26.                 val edge = node as Arc
  27.                 return Edge(node.type.name, Node.fromGraphObject(edge.source), Node.fromGraphObject(edge.target))
  28.             }
  29.         }
  30.  
  31.         override fun compareTo(other: Edge) = compareValuesBy(this, other, { it.type }, { it.source }, { it.target })
  32.     }
  33. }
  34.  
  35. fun Match.fingerprint(): String {
  36.  
  37.     var edges = mutableListOf<GraphItem.Edge>()
  38.     var nodes = mutableListOf<GraphItem.Node>()
  39.  
  40.     codomain.asSequence().forEach {
  41.         when {
  42.             it.isArc -> edges.add(GraphItem.Edge.fromGraphObject(it))
  43.             it.isNode -> nodes.add(GraphItem.Node.fromGraphObject(it))
  44.         }
  45.     }
  46.  
  47.     nodes = nodes.sorted() as MutableList<GraphItem.Node>
  48.     edges = edges.sorted() as MutableList<GraphItem.Edge>
  49.  
  50.     var fingerprint = mutableListOf<GraphItem>()
  51.  
  52.  
  53.     fingerprint.addAll(nodes)
  54.     fingerprint.addAll(edges)
  55.  
  56.     return "$name ${fingerprint.joinToString()}}"
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement