algebraic-graphs-0.6.1: A library for algebraic graph construction and transformation
Copyright (c) Andrey Mokhov 2016-2022
License MIT (see the file LICENSE)
Maintainer andrey.mokhov@gmail.com
Stability experimental
Safe Haskell None
Language Haskell2010

Algebra.Graph.Export.Dot

Description

Alga is a library for algebraic construction and manipulation of graphs in Haskell. See this paper for the motivation behind the library, the underlying theory, and implementation details.

This module defines functions for exporting graphs in the DOT file format.

Synopsis

Graph attributes and style

data Attribute s Source #

An attribute is just a key-value pair, for example "shape" := "box" . Attributes are used to specify the style of graph elements during export.

Constructors

(:=) s s

data Quoting Source #

The style of quoting used when exporting attributes; DoubleQuotes is the default.

data Style a s Source #

The record Style a s specifies the style to use when exporting a graph in the DOT format. Here a is the type of the graph vertices, and s is the type of string to represent the resulting DOT document (e.g. String, Text, etc.). The only field that has no obvious default value is vertexName , which holds a function of type a -> s to compute vertex names. See the function export for an example.

Constructors

Style

Fields

defaultStyle :: Monoid s => (a -> s) -> Style a s Source #

Default style for exporting graphs. The vertexName field is provided as the only argument; the other fields are set to trivial defaults.

defaultStyleViaShow :: ( Show a, IsString s, Monoid s) => Style a s Source #

Default style for exporting graphs with Show -able vertices. The vertexName field is computed using show ; the other fields are set to trivial defaults.

defaultStyleViaShow = defaultStyle (fromString . show)

Export functions

export :: ( IsString s, Monoid s, Ord a, ToGraph g, ToVertex g ~ a) => Style a s -> g -> s Source #

Export a graph with a given style.

For example:

style :: Style Int String
style = Style
    { graphName               = "Example"
    , preamble                = ["  // This is an example", ""]
    , graphAttributes         = ["label" := "Example", "labelloc" := "top"]
    , defaultVertexAttributes = ["shape" := "circle"]
    , defaultEdgeAttributes   = mempty
    , vertexName              = \x   -> "v" ++ show x
    , vertexAttributes        = \x   -> ["color" := "blue"   | odd x      ]
    , edgeAttributes          = \x y -> ["style" := "dashed" | odd (x * y)]
    , attributeQuoting        = DoubleQuotes }

> putStrLn $ export style (1 * 2 + 3 * 4 * 5 :: Graph Int)

digraph Example
{
  // This is an example

  graph [label="Example" labelloc="top"]
  node [shape="circle"]
  "v1" [color="blue"]
  "v2"
  "v3" [color="blue"]
  "v4"
  "v5" [color="blue"]
  "v1" -> "v2"
  "v3" -> "v4"
  "v3" -> "v5" [style="dashed"]
  "v4" -> "v5"
}

exportAsIs :: ( IsString s, Monoid s, Ord ( ToVertex g), ToGraph g, ToVertex g ~ s) => g -> s Source #

Export a graph whose vertices are represented simply by their names.

For example:

> Text.putStrLn $ exportAsIs (circuit ["a", "b", "c"] :: AdjacencyMap Text)

digraph
{
  "a"
  "b"
  "c"
  "a" -> "b"
  "b" -> "c"
  "c" -> "a"
}

exportViaShow :: ( IsString s, Monoid s, Ord ( ToVertex g), Show ( ToVertex g), ToGraph g) => g -> s Source #

Export a graph using the defaultStyleViaShow .

For example:

> putStrLn $ exportViaShow (1 + 2 * (3 + 4) :: Graph Int)

digraph
{
  "1"
  "2"
  "3"
  "4"
  "2" -> "3"
  "2" -> "4"
}