Skip to content

Instantly share code, notes, and snippets.

@sdb
Created July 16, 2011 16:54
Show Gist options
  • Save sdb/1086537 to your computer and use it in GitHub Desktop.
Save sdb/1086537 to your computer and use it in GitHub Desktop.
Render class diagram with Graphviz

This example models a class diagram with Scuml and then uses Graphviz to render the class diagram.

import scuml._
// helper object for using the Graphviz command line utility
object Graphviz {
import java.io._
import actors.Actor
import actors.Actor._
def apply(dot: String): File = {
val p = Runtime.getRuntime.exec("dot -Tpng")
val writer = actor {
receive {
case 'Write =>
val w = new BufferedWriter(new OutputStreamWriter(p.getOutputStream))
w write dot
w.newLine
w.close
exit
}
}
val reader = actor {
receive {
case 'Read =>
val f = File.createTempFile("scuml", "")
val out = new FileOutputStream(f)
val buffer = new Array[Byte](1024)
Stream.continually(p.getInputStream.read(buffer)).takeWhile(_ != -1).foreach(out.write(buffer, 0, _))
p.waitFor
reply { f }
exit
}
}
writer ! 'Write
val f = reader !? 'Read
f.asInstanceOf[File]
}
}
import scuml._
// create a Diagram with the DSL
val element = "Element" % "abstract" % "sealed"
val node = "Node" % "case"
val edge = "Edge" % "case"
val group = "Group" % "case"
val anchor = "Anchor" % "case"
val diagram = Diagram(
"Diagram" % "case" :>> "+" === ">" <<: "0..*" <*: element,
node === "^" <<: element,
edge === "^" <<: element,
group === "^" <<: element,
edge :*> "source" === ">" <<: anchor,
edge :*> "destination" === ">" <<: anchor,
anchor === ">" <<: node,
group :>> "+" === ">" <<: "0..*" <*: node)
// render in DOT format and create an image with the help of Graphviz
val dot = diagram.toDot
val f = Graphviz(dot)
java.awt.Desktop.getDesktop.open(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment