Some stuff for yUML-like diagramming including:
- data classes to build a diagram with nodes and edges
- a parser to parse yUML class diagram notation
- a renderer to render a diagram in yUML class diagram notation
- a renderer to render a diagram in DOT format
- Scala sugar to create a diagram in Scala code
git clone https://github.com/sdb/scuml.git cd scuml sbt test package
This will generate a JAR under the
target directory. You can also startup the Scala interpreter (REPL) to experiment with the library:
This section explains how to use the parsers, renderers and DSL. Don't forget to look at the tests for examples.
Importing everything from the package
scuml is the easiest way to get access to all the goodies.
scuml.AST contains the data classes (
Node, ...) for building a diagram. Note that the
scuml package object defines aliasses for these data classes.
The following example creates a diagram with two nodes.
val diagram = Diagram(Node("Foo"), Node("Bar"))
It's possible to parse a yUML-like string into a diagram.
ScumlParser parse "[Foo]-[Bar]"
This returns a
Diagram with only one edge, from node
Because creating a diagram from the case classes in the AST is a bit verbose, there's also some sugar to make it more expressive.
"Foo" === "Bar" | "Bar" =-= "^" <<: "Baz"
is equivalent to
Diagram(Edge(Anchor(Node("Foo")), Anchor(Node("Bar"))), Edge(Anchor(Node("Bar")), Anchor(Node("Baz"), "^"), true))
The following example renders the diagram created in the previous section.
val diagram = "Foo" === "Bar" | "Bar" =-= "^" <<: "Baz" ScumlRenderer render diagram
This renders the string
It's also possible to render the diagram in DOT format.
DotRenderer render diagram
Let's create a diagram with the DSL and render it in DOT format and then use Graphviz to render the image. For this example we'll generate an image that shows the class diagram for the
scuml.AST class hierarchy.
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)
We can create a DOT file from the diagram which can be processed by Graphviz to create an image.
Note that the
Graphviz method is not part of this library. Check out this gist for the full source code of this example.
The image rendered by Graphviz: