Commits

Sidharth Kuruvila committed d1965b8

- Added composite ids which are ids containing a :
- Added support for sequences of statements to be embeded as statements.

Comments (0)

Files changed (2)

src/main/scala/graphviz/Graphviz.scala

 package graphviz
 
 import net.kuruvila.graphviz.DotLanguage._
+import java.io._
 
 object Graphviz{
   def main(args: Array[String]) = {
       subgraph(
         style("filled"),
         color("#eeeeee"),
-        node(style("filled"), color("#ffffff")),
+        node(style("filled"), color("#ffffff"),shape.Record),
         "a0" --> "a1" --> "a2" --> "a3",
+        "a0".attr(label("a|{b|c}|d")),
         label("process #1")
       ),
       subgraph(
       "start".attr(shape.Mdiamond),
       "end".attr(shape.Msquare)
       )
-    println(g.repr)
-    println(net.kuruvila.graphviz.Graphviz.dot(g))
+    val pr = new PrintWriter(new FileWriter("test.svg"))
+    pr.println(net.kuruvila.graphviz.Graphviz.dot(g))
+    pr.close()
   }
 }

src/main/scala/net/kuruvila/graphviz/Dot.scala

 
 trait DotLanguage{
   var _id: Int = 0
-  def id:Id = {_id+=1;Id("id_"+ _id)}
-  def id(label:String):Id = ids.getOrElseUpdate(label, Id(label))
+  def id:SimpleId = {_id+=1;Id("id_"+ _id)}
+  def id(label:String):SimpleId = ids.getOrElseUpdate(label, Id(label))
 
-  val ids = MMap[String, Id]();
+  val ids = MMap[String, SimpleId]();
 
 
   def digraph(statements:Statement*): Digraph = Digraph(id, statements)
   def style(v:String) = StringAttribute("style", v)
   def color(name:String) = EscapedStringAttribute("color", name)
   def label(label:String) = EscapedStringAttribute("label", label)
+  def rankDir(dir:String) = StringAttribute("rankdir", dir)
 
   case class Digraph(id:Id, statements:Seq[Statement]){
     def repr: String = "digraph %s {%s}".format(id.repr, statements map (_.repr) mkString (";"))
   
   trait Attribute extends Statement
 
+  implicit def seqToStatement(seq: Seq[Statement]):StatementSeq = StatementSeq(seq)
   implicit def toEdgeStatement(label:Id):EdgeStatement = EdgeLhs(label)
   implicit def stringToId(label: String): Id = id(label)
 
 
-  case class Id(id:String){
-    def repr:String = id
+  case class StatementSeq(seq: Seq[Statement]) extends Statement{
+    def repr: String = seq map (_.repr) mkString(";")
+  }
 
+
+  object Id{ def apply(id: String) = SimpleId(id) }
+  trait Id{
+    def repr:String
     def attr(attributes:Attribute*): NodeAttributesStatement = NodeAttributesStatement(this, attributes)
     def -->(rhs:Id):EdgeStatement = EdgeRhs(EdgeLhs(this), rhs)
+    def createChild = CompositeId(this, id)
+  }
+
+  case class SimpleId(repr:String) extends Id
+
+  case class CompositeId(parent:Id, simple:SimpleId) extends Id{
+    def repr:String = parent.repr + ":" + simple.repr
   }
     
 
     case object Folder extends Shape{def repr:String = "shape = folder"}
     case object Box3d extends Shape{def repr:String = "shape = box3d"}
     case object Component extends Shape{def repr:String = "shape = component"}
+    case object Record extends Shape{def repr:String = "shape = record"}
   }
   
   sealed trait SmoothType extends AttributeValue