Skip to content

Commit

Permalink
#19 building report from graph
Browse files Browse the repository at this point in the history
  • Loading branch information
ThetaSinner committed Oct 27, 2018
1 parent cc4e857 commit 2836ca7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@ class Graph {
fun findNode(label: String): Node? {
return nodes.find { node -> node.label == label }
}

// Only valid for di-graphs, otherwise much allow edges if incoming edges have a corresponding outgoing edge.
fun findSourceNodes(): HashSet<Node> {
val tempNodes = HashSet<Node>()
tempNodes.addAll(nodes)

edges.forEach { edge ->
val node = edge.sink
tempNodes.remove(node)
}

return tempNodes
}

fun firstNode(): Node {
return nodes.first()
}

fun findNodesConnectedFrom(node: Node): HashSet<Node> {
val tempNodes = HashSet<Node>()
edges.forEach { edge ->
if (edge.source == node) {
tempNodes.add(edge.sink)
}
}

return tempNodes
}
}

class Node(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
package org.ephyra.acropolis.report.impl

import org.ephyra.acropolis.report.api.IReportRunner
import org.ephyra.acropolis.report.api.model.Graph
import org.ephyra.acropolis.report.api.model.GraphContainer
import org.ephyra.acropolis.report.api.model.Node
import org.springframework.stereotype.Component

@Component
private class ReportRunner : IReportRunner {
override fun run(graphContainer: GraphContainer) {
println("Running report")
buildNodeDepth(graphContainer.graph)


}

fun buildNodeDepth(graph: Graph): HashMap<Node, Int> {
val startNode = pickStartNode(graph)

val depths = HashMap<Node, Int>()
depths[startNode] = 0

nodeDepth(startNode, graph, depths, 1)

return depths
}

fun nodeDepth(currentNode: Node, graph: Graph, depths: HashMap<Node, Int>, depth: Int) {
val connected = graph.findNodesConnectedFrom(currentNode)

// Remove all the nodes which are already numbered.
connected.removeAll(depths.keys)

// Number each of the connected nodes.
connected.forEach { con ->
depths[con] = depth
}

// Now apply the same process to each of the nodes we just numbered.
connected.forEach { con ->
nodeDepth(con, graph, depths, depth + 1)
}
}

fun pickStartNode(graph: Graph): Node {
val sourceNodes = graph.findSourceNodes()
return if (sourceNodes.isEmpty()) {
graph.firstNode()
}
else {
sourceNodes.first()
}
}
}

0 comments on commit 2836ca7

Please sign in to comment.