dot

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2024 License: MIT Imports: 10 Imported by: 29 modules, 31 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
var (
	ErrNilNode               = errors.New("cannot add nil node")
	ErrEmptyNodeName         = errors.New("node name cannot be empty")
	ErrDuplicateNode         = errors.New("node with this name already exists")
	ErrNilEdge               = errors.New("cannot add nil edge")
	ErrInvalidEdge           = errors.New("edge must have both source and destination nodes")
	ErrNodeNotFound          = errors.New("node not found in the graph")
	ErrNilSubgraph           = errors.New("cannot add nil subgraph")
	ErrEmptySubgraphName     = errors.New("subgraph name cannot be empty")
	ErrDuplicateSubgraph     = errors.New("subgraph with this name already exists")
	ErrInvalidGraphType      = errors.New("invalid graph type")
	ErrUnsupportedOperation  = errors.New("operation not supported for this graph type")
	ErrCycleDetected         = errors.New("graph contains a cycle")
	ErrNoPath                = errors.New("no path exists between the given nodes")
	ErrDOTParsingFailed      = errors.New("failed to parse DOT input")
	ErrGraphvizNotFound      = errors.New("Graphviz tools not found in system PATH")
	ErrInvalidNodeAttribute  = errors.New("invalid node attribute")
	ErrInvalidEdgeAttribute  = errors.New("invalid edge attribute")
	ErrInvalidGraphAttribute = errors.New("invalid graph attribute")
)

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) Clone added in v0.2.0

func (e *Edge) Clone() *Edge

func (*Edge) Destination

func (e *Edge) Destination() *Node

func (*Edge) Equals

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

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) 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
}

Graph represents a graph in the DOT language.

func NewGraph

func NewGraph(name string) *Graph

NewGraph creates a new graph with the given name.

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) (*Edge, error)

func (*Graph) AddNode

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

func (*Graph) AddSubgraph

func (g *Graph) AddSubgraph(sg *SubGraph) (*SubGraph, 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) Get added in v0.2.0

func (g *Graph) Get(key string) string

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) 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

Merge merges the contents of another graph into this graph.

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

Set an attribute on the graph.

func (*Graph) SetGlobalEdgeAttr

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

func (*Graph) SetGlobalNodeAttr

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

func (*Graph) SetStrict

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

func (*Graph) SetType

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

SetType sets the type of the 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

GraphType represents the type of a graph.

const (
	DIGRAPH GraphType = iota
	GRAPH
	SUBGRAPH
)

func GraphTypeFromString

func GraphTypeFromString(s string) GraphType

func (GraphType) String

func (gt GraphType) String() string

type Node

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

func NewNode

func NewNode(name string) *Node

func (*Node) Clone added in v0.2.0

func (n *Node) Clone() *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) SetSequence

func (n *Node) SetSequence(seq int)

func (*Node) String

func (n *Node) String() string

func (*Node) Type

func (n *Node) Type() string

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