Skip to content

Commit

Permalink
hello graph
Browse files Browse the repository at this point in the history
  • Loading branch information
awalterschulze committed Jan 11, 2013
0 parents commit 32bf72e
Show file tree
Hide file tree
Showing 47 changed files with 17,923 additions and 0 deletions.
46 changes: 46 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Copyright 2013 Vastech SA (PTY) LTD

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http:https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-------------------------------------------------------------------------------
Portions of gocc's source code has been derived from Go, and are covered by the
following license:
-------------------------------------------------------------------------------

Copyright (c) 2009 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
163 changes: 163 additions & 0 deletions src/gographviz/analyse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//Copyright 2013 Vastech SA (PTY) LTD
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

package dot

import (
"gographviz/ast"
"fmt"
)

func AnalyseGraph(graph *ast.Graph) *Graph {
g := NewGraph()
Read(graph, g)
return g
}

func Read(graph *ast.Graph, g Interface) {
graph.Walk(&graphVisitor{g})
}

type nilVisitor struct {
}

func (this *nilVisitor) Visit(v ast.Elem) ast.Visitor {
return this
}

type graphVisitor struct {
g Interface
}

func (this *graphVisitor) Visit(v ast.Elem) ast.Visitor {
graph, ok := v.(*ast.Graph)
if !ok {
return this
}
this.g.SetStrict(graph.Strict)
this.g.SetDir(graph.Type == ast.DIGRAPH)
graphName := graph.Id.String()
this.g.SetName(graphName)
return newStmtVisitor(this.g, graphName)
}

func newStmtVisitor(g Interface, graphName string) *stmtVisitor {
return &stmtVisitor{g, graphName, make(Attrs), make(Attrs), make(Attrs)}
}

type stmtVisitor struct {
g Interface
graphName string
currentNodeAttrs Attrs
currentEdgeAttrs Attrs
currentGraphAttrs Attrs
}

func (this *stmtVisitor) Visit(v ast.Elem) ast.Visitor {
switch s := v.(type) {
case ast.NodeStmt:
fmt.Printf("NodeStmt %#v\n", s)
return this.nodeStmt(s)
case ast.EdgeStmt:
return this.edgeStmt(s)
case ast.NodeAttrs:
return this.nodeAttrs(s)
case ast.EdgeAttrs:
return this.edgeAttrs(s)
case ast.GraphAttrs:
return this.graphAttrs(s)
case *ast.SubGraph:
return this.subGraph(s)
case *ast.Attr:
return this.attr(s)
case ast.AttrList:
return &nilVisitor{}
default:
//fmt.Printf("unknown stmt %T\n", v)
}
return this
}

func ammend(attrs Attrs, add Attrs) Attrs {
for key, value := range add {
if _, ok := attrs[key]; !ok {
attrs[key] = value
}
}
return attrs
}

func overwrite(attrs Attrs, overwrite Attrs) Attrs {
for key, value := range overwrite {
attrs[key] = value
}
return attrs
}

func (this *stmtVisitor) nodeStmt(stmt ast.NodeStmt) ast.Visitor {
attrs := Attrs(stmt.Attrs.GetMap())
attrs = ammend(attrs, this.currentNodeAttrs)
this.g.AddNode(this.graphName, stmt.NodeId.String(), attrs)
return &nilVisitor{}
}

func (this *stmtVisitor) edgeStmt(stmt ast.EdgeStmt) ast.Visitor {
attrs := stmt.Attrs.GetMap()
attrs = ammend(attrs, this.currentEdgeAttrs)
src := stmt.Source.GetId()
this.g.AddNode(this.graphName, src.String(), this.currentNodeAttrs)
srcPort := stmt.Source.GetPort()
for i := range stmt.EdgeRHS {
directed := bool(stmt.EdgeRHS[i].Op)
dst := stmt.EdgeRHS[i].Destination.GetId()
this.g.AddNode(this.graphName, dst.String(), this.currentNodeAttrs)
dstPort := stmt.EdgeRHS[i].Destination.GetPort()
this.g.AddEdge(src.String(), srcPort.String(), dst.String(), dstPort.String(), directed, attrs)
src = dst
srcPort = dstPort
}
return this
}

func (this *stmtVisitor) nodeAttrs(stmt ast.NodeAttrs) ast.Visitor {
this.currentNodeAttrs = overwrite(this.currentNodeAttrs, ast.AttrList(stmt).GetMap())
return &nilVisitor{}
}

func (this *stmtVisitor) edgeAttrs(stmt ast.EdgeAttrs) ast.Visitor {
this.currentEdgeAttrs = overwrite(this.currentEdgeAttrs, ast.AttrList(stmt).GetMap())
return &nilVisitor{}
}

func (this *stmtVisitor) graphAttrs(stmt ast.GraphAttrs) ast.Visitor {
attrs := ast.AttrList(stmt).GetMap()
for key, value := range attrs {
this.g.AddAttr(this.graphName, key, value)
}
this.currentGraphAttrs = overwrite(this.currentGraphAttrs, attrs)
return &nilVisitor{}
}

func (this *stmtVisitor) subGraph(stmt *ast.SubGraph) ast.Visitor {
fmt.Printf("reading %#v\n", stmt)
subGraphName := stmt.Id.String()
this.g.AddSubGraph(this.graphName, subGraphName, this.currentGraphAttrs)
return newStmtVisitor(this.g, subGraphName)
}

func (this *stmtVisitor) attr(stmt *ast.Attr) ast.Visitor {
this.g.AddAttr(this.graphName, stmt.Field.String(), stmt.Value.String())
return this
}

Loading

0 comments on commit 32bf72e

Please sign in to comment.