Created
July 19, 2016 19:05
-
-
Save Daiver/0893a772d581dbf3dddda9d646a1cefb to your computer and use it in GitHub Desktop.
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
package object graphviz { | |
case class Attribute(key: String, value: String) { | |
def render(): String = s"${key} = ${value}" | |
} | |
case class AttributePlaceholder(str: String){ | |
def := (other: AttributePlaceholder): Attribute = Attribute(str, other.str) | |
} | |
implicit def strToAttrPlaceholder(str: String):AttributePlaceholder = AttributePlaceholder(str) | |
abstract class Statement | |
case class EdgeStatement( | |
startNode: String, | |
finishNode: String, | |
attributes: Seq[Attribute] | |
) extends Statement { | |
def :| (newAttr: Attribute): EdgeStatement = EdgeStatement(startNode, finishNode, attributes :+ newAttr) | |
def render(): String = { | |
val attrsRendered = attributes map {_.render} mkString "," | |
s"${startNode} -- ${finishNode}[${attrsRendered}]" | |
} | |
} | |
case class EdgeStatementPlaceholder(name: String) { | |
def --- (other: EdgeStatementPlaceholder) = EdgeStatement(name, other.name, Seq()) | |
} | |
implicit def stringToEsPlaceholder(str: String) = EdgeStatementPlaceholder(str) | |
case class Graph(id: String, edges: Seq[EdgeStatement]){ | |
def render() = { | |
val tmp = (edges map {x => s"\t${x.render};\n"} mkString "") | |
s"graph ${id} {\n${tmp}}\n" | |
} | |
} | |
def createGraph(id: String, edges: EdgeStatement*) = { | |
Graph(id, edges) | |
} | |
} |
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
import graphviz._ | |
import sys.process._ | |
import java.io._ | |
package graphviz { | |
package object io { | |
def saveGraph(fname: String, pngFName: String, graph: Graph) = { | |
val rendered = graph.render | |
new PrintWriter(fname) { write(rendered); close } | |
s"dot -Tpng ${fname} -o${pngFName}" ! | |
//println(s"echo \' ${rendered} \' | dot -Tpng -o${fname}") | |
//(s"echo ${rendered}") #| (s"dot -Tpng -o${fname}") ! | |
//println("ls -al" #| "grep Foo" !) | |
//println(Seq("echo", "1") #| "dot -Tpng -otmp.png" !!) | |
} | |
} | |
} |
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
import graphviz._ | |
import graphviz.io._ | |
object App { | |
case class MyClass(x: Int){ | |
def ---(b: MyClass):(Int, Int) = (x, b.x) | |
} | |
implicit def intToMyTrait(x:Int): MyClass = MyClass(x) | |
def main(args: Array[String]) { | |
println("Hi") | |
println(1 --- 2) | |
//val graph = createGraph("tmp", ("1", "2"), ("2", "3")) | |
val graph = createGraph("tmp", "1" --- "2" :| ("color" := "red")) | |
println(graph.render) | |
saveGraph("tmp2.dot", "tmp2.png", graph) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment