Skip to content

Commit

Permalink
Make cluster subgraphs as nodes for edges 2 (#24)
Browse files Browse the repository at this point in the history
* Make cluster subgraphs as nodes for edges

* Add test for cluster subgraphs

* update grammar to handle subgraphs without statements
  • Loading branch information
awalterschulze authored Mar 23, 2017
1 parent 057e6a5 commit 9d8c978
Show file tree
Hide file tree
Showing 8 changed files with 525 additions and 270 deletions.
2 changes: 2 additions & 0 deletions dot.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ SubGraphStmt
: "{" StmtList "}" << ast.NewSubGraph(nil, $1) >>
| subgraph "{" StmtList "}" << ast.NewSubGraph(nil, $2) >>
| subgraph Id "{" StmtList "}" << ast.NewSubGraph($1, $3) >>
| subgraph "{" "}" << ast.NewSubGraph(nil, nil) >>
| subgraph Id "{" "}" << ast.NewSubGraph($1, nil) >>
;

//An edgeop is -> in directed graphs and -- in undirected graphs.
Expand Down
40 changes: 40 additions & 0 deletions escape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,43 @@ func TestEscape(t *testing.T) {
t.Fatalf("should be a node")
}
}

func TestClusterSubgraphs(t *testing.T) {
g := NewEscape()
g.SetName("G")
g.SetDir(false)
g.AddSubGraph("G", "cluster0", nil)
g.AddSubGraph("cluster0", "cluster_1", nil)
g.AddSubGraph("cluster0", "cluster_2", nil)
g.AddNode("G", "Code deployment", nil)
g.AddPortEdge("cluster_2", "", "cluster_1", "", false, nil)
s := g.String()
graphStr := `graph G {
cluster_2--cluster_1;
subgraph cluster0 {
subgraph cluster_1 {
}
;
subgraph cluster_2 {
}
;
}
;
"Code deployment";
}`
if !strings.HasPrefix(s, graphStr) {
t.Fatalf("%s", s)
}
g2, err := Parse([]byte(s))
if err != nil {
t.Fatal(err)
}
s2 := g2.String()
if !strings.HasPrefix(s2, graphStr) {
t.Fatalf("%s", s2)
}
}
8 changes: 8 additions & 0 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package gographviz

import "strings"

//The analysed representation of the Graph parsed from the DOT format.
type Graph struct {
Attrs Attrs
Expand Down Expand Up @@ -112,3 +114,9 @@ func (this *Graph) IsSubGraph(name string) bool {
_, ok := this.SubGraphs.SubGraphs[name]
return ok
}

func (this *Graph) IsClusterSubGraph(name string) bool {
isSubGraph := this.IsSubGraph(name)
isCluster := strings.HasPrefix(name, "cluster")
return isSubGraph && isCluster
}
Loading

0 comments on commit 9d8c978

Please sign in to comment.