dot

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2020 License: MIT Imports: 9 Imported by: 0

README

dot

a lightweight, pure golang graphviz-compatible dot language implementation

Status GitHub tag (latest SemVer) GitHub Issues GitHub Pull Requests License

Go Report Card GoDoc

Quality Gate Status Reliability Rating Bugs Security Rating Vulnerabilities

Maintainability Rating Coverage Code Smells Technical Debt

📝 Table of Contents

🧐 About

WARNING: this package is a WIP and will introduce breaking changes while on major version zero.

Dot provides interfaces and ready-to-use concrete types to create graphviz-compatible graphs using its dot language.

This package was inspired/initially forked from emicklei/dot, but has too many breaking changes compared to the original - namely interface usage and other distinct design decisions - that I decided to maintain it separately. If you need a simpler, no-brainy option, use emicklei's dot package.

🏁 Getting Started

Clone the repository and then run make to build, test, generate coverage and lint the code.

Prerequisites

Golang 1.12+ and ideally a modules-enabled application. Dot has no dependencies.

🎈 Usage

Add it to your modules with

go get -u github.com/wwmoraes/dot

And then:

package main

import (
  "os"
  "github.com/wwmoraes/dot"
)

func main() {
  graph := dot.New()
  graph.SetAttributeString("label", "an amazing graph")
  clusterA := graph.Subgraph(WithID("Cluster A"), WithCluster())
  clusterA.SetAttributeString("label", "Cluster A")
  clusterB := graph.Subgraph(WithID("Cluster B"), WithCluster())
  clusterB.SetAttributeString("label", "Cluster B")

  clusterA.
    Node("one").
    Edge(clusterA.Node("two")).
    Edge(clusterB.Node("three")).
    Edge(graph.Node("Outside")).
    Edge(clusterB.Node("four")).
    Edge(clusterA.Node("one"))

  fd, _ := os.Create("sample.dot")
  graph.WriteTo(fd)
}

The constants sub-package has all supported keys defined as variables, and can be used instead of plain strings to avoid both duplication and errors:

package main

import (
  "os"
  "github.com/wwmoraes/dot"
  "github.com/wwmoraes/dot/constants"
)

func main() {
  graph := dot.New()
  graph.SetAttributeString(constants.KeyLabel, "a graph")
  node := graph.Node("n1")
  node.SetAttributeString(constants.KeyLabel, "a node")
  edge := graph.Edge(graph.Node("n2"), graph.Node("n3"))
  edge.SetAttributeString(constants.KeyLabel, "a edge")

  fd, _ := os.Create("sample.dot")
  graph.WriteTo(fd)
}

You can also set literals and HTML values using the helper functions:

package main

import (
  "os"
  "github.com/wwmoraes/dot"
  "github.com/wwmoraes/dot/constants"
)

func main() {
  graph := dot.New()
  graph.Node("n1").SetAttributeLiteral(constants.KeyLabel, `a left label\l`)
  graph.Node("n2").SetAttributeHTML(constants.KeyLabel, `<b>a bold label</b>`)
  fd, _ := os.Create("sample.dot")
  graph.WriteTo(fd)
}

✍️ Authors

🎉 Acknowledgements

Documentation

Overview

Package dot provides interfaces and ready-to-use concrete types to create graphviz-compatible graphs using its dot language.

This package was inspired/initially forked from github.com/emicklei/dot, but has too many breaking changes compared to the original - namely interface usage and other distinct design decisions - that it was decided to maintain separately. If you need a simpler, no-brainy option, use the original one.

Copyright (c) William Artero. MIT License

Index

Constants

This section is empty.

Variables

View Source
var ErrGraphWithoutGenerator = errors.New("cannot create a graph without an ID generator")

ErrGraphWithoutGenerator means a graph cannot be created without a generator

View Source
var ErrNilParent = errors.New("cannot create subgraph without a parent")

ErrNilParent means a nil parent was provided

View Source
var ErrRootAsCluster = errors.New("cannot create a root cluster graph")

ErrRootAsCluster means a cluster graph cannot be created at root level

View Source
var ErrRootWithParent = errors.New("cannot create [di]graph with parent")

ErrRootWithParent means a non-subgraph cannot be created with a parent

View Source
var ErrSubgraphWithoutParent = errors.New("cannot create subgraph without parent")

ErrSubgraphWithoutParent means a subgraph cannot be created without a parent

Functions

This section is empty.

Types

type Edge

type Edge interface {
	attributes.Styleable
	attributes.Serializable

	// From returns the tail node this Edge is connected from
	From() Node
	// From returns the head node this Edge is connected to
	To() Node
	// Edge creates an Edge to a Node using the head node of this Edge as tail
	Edge(to Node) Edge
	// Edge creates an Edge with the provided attributes to a Node using the head
	// node of this Edge as tail
	EdgeWithAttributes(to Node, attributes attributes.Reader) Edge
	// EdgesTo returns all edges between the head Node of this Edge and the target
	// Node
	EdgesTo(to Node) []Edge
}

Edge is implemented by dot-compatible edge values

type EdgeInitializerFn added in v0.4.0

type EdgeInitializerFn func(StyledEdge)

EdgeInitializerFn mutates Edges during their creation time

type Graph

type Graph interface {
	attributes.Identity
	attributes.Styleable
	attributes.Serializable

	// Root returns the root graph (i.e. the topmost, without a parent graph)
	Root() Graph
	// Type returns the graph type: directed, undirected or sub
	Type() GraphType
	// FindSubgraph returns the subgraph of this graph or from one of its parents
	FindSubgraph(id string) (Graph, bool)
	// Subgraph creates a subgraph of this graph
	Subgraph(optionsFn ...GraphOptionFn) (Graph, error)
	// Node gets a node by id, or creates a new one if it doesn't exist
	Node(id string) Node
	// Edge creates a new edge between the two provided nodes
	Edge(n1, n2 Node) StyledEdge
	// Edge creates a new edge between the two provided nodes, and also set the
	// given attributes
	EdgeWithAttributes(n1, n2 Node, attributes attributes.Reader) StyledEdge
	// FindEdges gets all edges in the graph between the two provided nodes
	FindEdges(fromNode, toNode Node) (found []Edge)
	// FindNode gets a node by id
	FindNode(id string) (Node, bool)
	// VisitNodes runs the provided function on all nodes recursively
	VisitNodes(callback func(node Node) (done bool))
	// AddToSameRank adds the given nodes to the specified rank group, forcing
	// them to be rendered in the same row
	AddToSameRank(group string, nodes ...Node)
	// FindNodeByID return node by id
	FindNodeByID(id string) (foundNode Node, found bool)
	// FindNodes returns all nodes recursively
	FindNodes() (nodes []Node)
	// HasSubgraphs returns true if the graph has any subgraphs
	HasSubgraphs() bool
	// HasNodes returns true if the graph has any nodes
	HasNodes() bool
	// HasEdges returns true if the graph has any nodes
	HasEdges() bool
	// HasSameRankNodes returns true if the graph has nodes grouped as same rank
	HasSameRankNodes() bool
	// IsStrict return true if the graph is set as strict
	IsStrict() bool
}

Graph is implemented by dot-compatible graph values

func New added in v0.4.1

func New(optionsFn ...GraphOptionFn) (Graph, error)

New return a Graph after all option functions are processed

func NewWithOptions added in v0.4.1

func NewWithOptions(options GraphOptions) (Graph, error)

NewWithOptions returns a Graph using the options values

type GraphOptionFn added in v0.4.0

type GraphOptionFn func(GraphOptions) error

GraphOptionFn is a functor that mutates graph options

func WithCluster added in v0.4.0

func WithCluster() GraphOptionFn

WithCluster sets the graph as a cluster

func WithEdgeInitializer added in v0.4.0

func WithEdgeInitializer(edgeInitializerFn EdgeInitializerFn) GraphOptionFn

WithEdgeInitializer sets the graph edge initializer function

func WithGenerator added in v0.4.0

func WithGenerator(generator generators.IDGenerator) GraphOptionFn

WithGenerator sets the graph id generator

func WithID added in v0.4.0

func WithID(id string) GraphOptionFn

WithID sets the graph id, or generates one if id is "-"

func WithNodeInitializer added in v0.4.0

func WithNodeInitializer(nodeInitializerFn NodeInitializerFn) GraphOptionFn

WithNodeInitializer sets the graph node initializer function

func WithParent added in v0.4.0

func WithParent(graph Graph) GraphOptionFn

WithParent sets the graph parent

func WithStrict added in v0.4.0

func WithStrict() GraphOptionFn

WithStrict sets the graph as strict

func WithType added in v0.4.0

func WithType(graphType GraphType) GraphOptionFn

WithType sets the graph type

type GraphOptions

type GraphOptions interface {
	// SetID changes the id
	SetID(string)
	// ID returns the id
	ID() string
	// SetParent changes the parent graph
	SetParent(Graph)
	// Parent return the parent graph
	Parent() Graph
	// SetType changes the graph type
	SetType(GraphType)
	// Type returns the graph type
	Type() GraphType
	// SetStrict changes the strict flag
	SetStrict(bool)
	// Strict returns the strict flag
	Strict() bool
	// SetGenerator changes the ID generator
	SetGenerator(generators.IDGenerator)
	// Generator return the ID generator
	Generator() generators.IDGenerator
	// SetNodeInitializer changes the node initializer function
	SetNodeInitializer(NodeInitializerFn)
	// NodeInitializer returns the node initializer function
	NodeInitializer() NodeInitializerFn
	// SetEdgeInitializer changes the node initializer function
	SetEdgeInitializer(EdgeInitializerFn)
	// EdgeInitializer returns the node initializer function
	EdgeInitializer() EdgeInitializerFn
	// SetCluster changes the cluster flag
	SetCluster(bool)
	// Cluster returns if the graph is a cluster
	Cluster() bool
}

GraphOptions is implemented by values used to initialize graphs

func NewGraphOptions added in v0.4.0

func NewGraphOptions(optionsFn ...GraphOptionFn) (options GraphOptions, err error)

NewGraphOptions creates a new GraphOptions value that has all values initialized properly

type GraphType added in v0.3.0

type GraphType string

GraphType graphviz graph types

const (
	// GraphTypeUndirected node edges will have no direction, i.e. a layout engine
	// can freely organize the graph and draw glyphless edges between the nodes
	GraphTypeUndirected GraphType = "graph"
	// GraphTypeDirected node edges will have direction, i.e. a layout engine will
	// organize the graph and draw glyph edges denoting its orientation
	GraphTypeDirected GraphType = "digraph"
	// GraphTypeSub can be used to group objects, serve as a context for attributes
	// or draw its contents as a independent cluster, on supported layout engines
	GraphTypeSub GraphType = "subgraph"
)

type Node

type Node interface {
	attributes.Identity
	attributes.Styleable
	attributes.Serializable

	// Edge creates an Edge to a Node
	Edge(to Node) Edge
	// EdgeWithAttributes creates an Edge with the provided attributes to a Node
	EdgeWithAttributes(to Node, attributes attributes.Reader) Edge
	// EdgesTo returns all edges between this Node and the target Node
	EdgesTo(to Node) []Edge
}

Node is implemented by dot-compatible node values

type NodeInitializerFn added in v0.4.0

type NodeInitializerFn func(Node)

NodeInitializerFn mutates Nodes during their creation time

type StyledEdge

type StyledEdge interface {
	Edge
	// Solid sets the edge style to solid
	Solid() Edge
	// Solid sets the edge style to bold
	Bold() Edge
	// Solid sets the edge style to dashed
	Dashed() Edge
	// Solid sets the edge style to dotted
	Dotted() Edge
}

StyledEdge is implemented by dot-compatible edge values which have convenience styling methods

type StyledNode

type StyledNode interface {
	Node
	// Box sets the node style to box
	Box() Node
}

StyledNode is implemented by dot-compatible node values which have convenience styling methods

Directories

Path Synopsis
Package dottest provides helper values to create tests in a controlled and replicable environment, such as write errors
Package dottest provides helper values to create tests in a controlled and replicable environment, such as write errors

Jump to

Keyboard shortcuts

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