dot

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2024 License: MIT Imports: 11 Imported by: 30

README

dot

dot is a Go package that provides support for working with the DOT language, which is used for describing graphs in Graphviz.

Features

  • Create and manipulate graph structures
  • Add nodes, edges, and subgraphs
  • Set attributes for graphs, nodes, and edges
  • Generate DOT language output
  • Import and export graphs in DOT and JSON formats
  • Graph analysis and algorithms (e.g., topological sort, shortest path)
  • Graph visualization helpers (PNG and SVG output)

Installation

To install the dot package, use the following command:

go install github.com/tmc/dot@latest

Usage

Here's a simple example of how to use the dot package:

package main

import (
    "fmt"
    "github.com/tmc/dot"
)

func main() {
    g := dot.NewGraph("Example")
    g.Set("label", "This is an example graph")

    n1 := dot.NewNode("Node1")
    n1.Set("shape", "box")
    n2 := dot.NewNode("Node2")
    n2.Set("color", "red")

    g.AddNode(n1)
    g.AddNode(n2)

    e := dot.NewEdge(n1, n2)
    e.Set("label", "connects to")
    g.AddEdge(e)

    fmt.Println(g.String())
}

This will output the DOT language representation of the graph.

Documentation

For detailed documentation, please refer to the GoDoc page.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func QuoteIfNecessary

func QuoteIfNecessary(s string) string

Types

type AttributeError

type AttributeError struct {
	AttributeName string
	ObjectType    string
}

Error type for invalid attributes

func (AttributeError) Error

func (e AttributeError) Error() string

type Edge

type Edge struct {
	// contains filtered or unexported fields
}

func NewEdge

func NewEdge(src, dst *Node) *Edge

func (*Edge) Destination

func (e *Edge) Destination() *Node

func (*Edge) Equals

func (e *Edge) Equals(other *Edge) bool

Edge comparison

func (*Edge) Get

func (e *Edge) Get(key string) string

func (*Edge) Name

func (e *Edge) Name() string

func (*Edge) Sequence

func (e *Edge) Sequence() int

func (*Edge) Set

func (e *Edge) Set(key, value string) error

func (*Edge) SetAttribute

func (e *Edge) SetAttribute(key, value string) *Edge

func (*Edge) SetSequence

func (e *Edge) SetSequence(seq int)

func (*Edge) Source

func (e *Edge) Source() *Node

func (*Edge) String

func (e *Edge) String() string

func (*Edge) Type

func (e *Edge) Type() string

type Graph

type Graph struct {
	sync.RWMutex
	Name string
	// contains filtered or unexported fields
}

func ImportDOT

func ImportDOT(r io.Reader) (*Graph, error)

func ImportJSON

func ImportJSON(r io.Reader) (*Graph, error)

func NewGraph

func NewGraph(name string) *Graph
Example
package main

import (
	"fmt"

	"github.com/tmc/dot"
)

func main() {
	g := dot.NewGraph("G")
	g.Set("label", "Example graph")
	n1, n2 := dot.NewNode("Node1"), dot.NewNode("Node2")

	n1.Set("color", "sienna")

	g.AddNode(n1)
	g.AddNode(n2)

	e := dot.NewEdge(n1, n2)
	e.Set("dir", "both")
	g.AddEdge(e)

	fmt.Println(g)
}
Output:

digraph G {
graph [
  label="Example graph";
];
Node1 [color=sienna];
Node2;
Node1 -> Node2 [ dir=both ]
}

func (*Graph) AddEdge

func (g *Graph) AddEdge(e *Edge)

func (*Graph) AddEdgeConcurrent

func (g *Graph) AddEdgeConcurrent(e *Edge)

func (*Graph) AddEdgeWithContext

func (g *Graph) AddEdgeWithContext(ctx context.Context, e *Edge) error

func (*Graph) AddNode

func (g *Graph) AddNode(n *Node)

func (*Graph) AddNodeConcurrent

func (g *Graph) AddNodeConcurrent(n *Node)

func (*Graph) AddNodeWithContext

func (g *Graph) AddNodeWithContext(ctx context.Context, n *Node) error

func (*Graph) AddSubgraph

func (g *Graph) AddSubgraph(sg *SubGraph)

func (*Graph) AddSubgraphConcurrent

func (g *Graph) AddSubgraphConcurrent(sg *SubGraph)

func (*Graph) AddSubgraphWithContext

func (g *Graph) AddSubgraphWithContext(ctx context.Context, sg *SubGraph) error

func (*Graph) ApplyLayout

func (g *Graph) ApplyLayout(layout string) error

func (*Graph) BFS

func (g *Graph) BFS(startNode string, visitor func(*Node))

func (*Graph) Clone

func (g *Graph) Clone() *Graph

func (*Graph) DFS

func (g *Graph) DFS(startNode string, visitor func(*Node))

func (*Graph) Diff

func (g *Graph) Diff(other *Graph) (*Graph, error)

func (*Graph) Equals

func (g *Graph) Equals(other *Graph) bool

func (*Graph) ExportDOT

func (g *Graph) ExportDOT(w io.Writer) error

func (*Graph) ExportJSON

func (g *Graph) ExportJSON(w io.Writer) error

func (*Graph) GetAverageDegree

func (g *Graph) GetAverageDegree() float64

func (*Graph) GetDegree

func (g *Graph) GetDegree(nodeName string) int

func (*Graph) GetDensity

func (g *Graph) GetDensity() float64

func (*Graph) GetDiameter

func (g *Graph) GetDiameter() int

func (*Graph) GetEdgeCount

func (g *Graph) GetEdgeCount() int

func (*Graph) GetInDegree

func (g *Graph) GetInDegree(nodeName string) int

func (*Graph) GetNodeCount

func (g *Graph) GetNodeCount() int

func (*Graph) GetOutDegree

func (g *Graph) GetOutDegree(nodeName string) int

func (*Graph) GetSubgraphCount

func (g *Graph) GetSubgraphCount() int

func (*Graph) GetSubgraphs

func (g *Graph) GetSubgraphs() []*SubGraph

func (*Graph) MarshalJSON

func (g *Graph) MarshalJSON() ([]byte, error)

func (*Graph) Merge

func (g *Graph) Merge(other *Graph) error

func (*Graph) Optimize

func (g *Graph) Optimize()

func (*Graph) RemoveEdge

func (g *Graph) RemoveEdge(src, dst string)

func (*Graph) RemoveNode

func (g *Graph) RemoveNode(nodeName string)

func (*Graph) RemoveSubgraph

func (g *Graph) RemoveSubgraph(name string)

func (*Graph) SameRank

func (g *Graph) SameRank(nodes []string)

func (*Graph) Set

func (g *Graph) Set(key, value string) error

func (*Graph) SetAttribute

func (g *Graph) SetAttribute(key, value string) *Graph

func (*Graph) SetGlobalEdgeAttr

func (g *Graph) SetGlobalEdgeAttr(key, value string) error

func (*Graph) SetGlobalEdgeAttribute

func (g *Graph) SetGlobalEdgeAttribute(key, value string) *Graph

func (*Graph) SetGlobalNodeAttr

func (g *Graph) SetGlobalNodeAttr(key, value string) error

func (*Graph) SetGlobalNodeAttribute

func (g *Graph) SetGlobalNodeAttribute(key, value string) *Graph

func (*Graph) SetStrict

func (g *Graph) SetStrict(strict bool) *Graph

func (*Graph) SetType

func (g *Graph) SetType(t GraphType) *Graph

func (*Graph) ShortestPath

func (g *Graph) ShortestPath(start, end *Node) ([]*Node, error)

func (*Graph) String

func (g *Graph) String() string

func (*Graph) ToPNG

func (g *Graph) ToPNG(filename string) error

func (*Graph) ToSVG

func (g *Graph) ToSVG(filename string) error

func (*Graph) TopologicalSort

func (g *Graph) TopologicalSort() ([]*Node, error)

func (*Graph) UnmarshalJSON

func (g *Graph) UnmarshalJSON(data []byte) error

func (*Graph) Validate

func (g *Graph) Validate() error

type GraphObject

type GraphObject interface {
	fmt.Stringer
	Type() string
	Name() string
	Get(string) string
	Set(string, string) error
}

type GraphType

type GraphType int
const (
	DIGRAPH GraphType = iota
	GRAPH
	SUBGRAPH
)

func GraphTypeFromString

func GraphTypeFromString(s string) GraphType

func (GraphType) String

func (gt GraphType) String() string

type Item

type Item struct {
	// contains filtered or unexported fields
}

type Node

type Node struct {
	// contains filtered or unexported fields
}

func NewNode

func NewNode(name string) *Node

func (*Node) Equals

func (n *Node) Equals(other *Node) bool

Node comparison

func (*Node) Get

func (n *Node) Get(key string) string

func (*Node) Name

func (n *Node) Name() string

func (*Node) Sequence

func (n *Node) Sequence() int

func (*Node) Set

func (n *Node) Set(key, value string) error

func (*Node) SetAttribute

func (n *Node) SetAttribute(key, value string) *Node

func (*Node) SetSequence

func (n *Node) SetSequence(seq int)

func (*Node) String

func (n *Node) String() string

func (*Node) Type

func (n *Node) Type() string

type PriorityQueue

type PriorityQueue []*Item

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(x interface{})

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

func (*PriorityQueue) Update

func (pq *PriorityQueue) Update(item *Item)

type Sequenceable

type Sequenceable interface {
	Sequence() int
	SetSequence(int)
}

type SubGraph

type SubGraph struct {
	Graph
	// contains filtered or unexported fields
}

func NewSubgraph

func NewSubgraph(name string) *SubGraph

func (*SubGraph) Equals

func (sg *SubGraph) Equals(other *SubGraph) bool

SubGraph comparison

func (*SubGraph) Sequence

func (sg *SubGraph) Sequence() int

func (*SubGraph) SetSequence

func (sg *SubGraph) SetSequence(seq int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL