All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.autonomousapps.graph.GraphWriter.kt Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package com.autonomousapps.graph

internal object GraphWriter {

  fun toDot(graph: DependencyGraph) = buildString {
    append("strict digraph DependencyGraph {\n")
    graph.edges().forEach { edge ->
      val (from, to) = edge.nodeIds()
      append("  \"$from\" -> \"$to\";")
      append("\n")
    }
    append("}")
  }

  fun toDot(path: Iterable) = buildString {
    append("strict digraph DependencyGraph {\n")
    path.forEach { edge ->
      val (from, to) = edge.nodeIds()
      append("  \"$from\" -> \"$to\";")
      append("\n")
    }
    append("}")
  }

  fun toDot(graph: DependencyGraph, path: Iterable) = buildString {
    val importantNodes = path.flatMap {
      it.nodeIds().toList()
    }

    append("strict digraph DependencyGraph {\n")
    importantNodes.forEach {
      append("\n  \"$it\" [style=filled fillcolor=\"#008080\"];\n")
    }
    append("\n")

    graph.edges().forEach { edge ->
      val style =
        if (path.contains(edge)) " [style=bold color=\"#FF6347\" weight=8]"
        else ""

      val (from, to) = edge.nodeIds()
      append("  \"$from\" -> \"$to\"$style;")
      append("\n")
    }
    append("}")
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy