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
You need xsbt to build this project. Read these instructions to install xsbt. When you're done with that execute the following commands:
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:
sbt console
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.
import scuml._
The object scuml.AST
contains the data classes (Diagram
, Edge
, 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 Foo
to Bar
.
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 [Foo]-[Bar],[Bar]-.-^[Baz]
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.
The diagram:
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.
Graphviz(diagram.toDot)
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:
If you want to contribute fixes and/or features then fork this repository, push your changes and send me a pull request.