scalameta / ascii-graphs   0.1.2

MIT License GitHub

Parse ASCII art diagrams of graphs (nodes and edges)

Scala versions: 2.13 2.12

NOTE: This is a fork of https://github.com/mdr/ascii-graphs that has been updated to support Scala 2.12 and 2.13.

An ASCII-art diagram library for graphs. It supports both parsing existing diagrams and rendering graphs out as an ASCII diagram.

You can use it via sbt:

// Scala 2.13 and Scala 2.12 are supported.
libraryDependencies += "org.scalameta" %% "ascii-graphs" % "0.1.2"

Graph layout

import org.scalameta.ascii.layout._

val graph = Graph(
  vertices = List(
    "V1", "V2", "V3", "V4", "V5", "V6", "V7"),
  edges = List(
    "V1" -> "V2",
    "V7" -> "V1",
    "V1" -> "V3",
    "V1" -> "V4",
    "V2" -> "V5",
    "V2" -> "V6"))

val ascii = Layouter.renderGraph(graph)

println(ascii)

This would produce:

         +---+         
         |V7 |         
         +---+         
           |           
           v           
       +-------+       
       |  V1   |       
       +-------+       
         |  ||         
     -----  |--------  
     |      ---     |  
     v        |     |  
  +-----+     |     |  
  | V2  |     |     |  
  +-----+     |     |  
    | |       |     |  
  --- ---     |     |  
  |     |     |     |  
  v     v     v     v  
+---+ +---+ +---+ +---+
|V5 | |V6 | |V4 | |V3 |
+---+ +---+ +---+ +---+

Layout is Sugiyama-style layered graph drawing, and supports multi-edges, cycles, and vertex labels, but not self-loops or edge labels.

Other ASCII layout libraries

Graph parser

The graph parser is intended for constructing test DSLs, particularly for data which would be much more comprehensible in ASCII art than constructed through regular programming language expressions. For example, directed graphs, trees, 2D games, object graphs, and so on.

Typical usage is to parse the diagram into Diagram/Box/Edge objects, and then convert those objects into whatever your specific test data happens to be.

Syntax

Boxes

A Box is drawn as follows:

+------+
|      |
|      |
|      |
+------+

It can contain text:

+---------------+
|The quick brown|
|fox jumps over |
|the lazy dog.  |
+---------------+

Or other boxes:

+-----+
|+---+|
||+-+||
||| |||
||+-+||
|+---+|
+-----+

Edges

An Edge connects two boxes:

+-----+
|     |
|     |---------
|     |        |
+-----+        |
   |           |
   |           |
+-----+     +-----+
|     |     |     |
|     |-----|     |
|     |     |     |
+-----+     +-----+

Edges can have an arrow at either or both ends:

+-----+
|     |
|     |---------
|     |        |
+-----+        |
   ^           |
   |           v
+-----+     +-----+
|     |     |     |
|     |<--->|     |
|     |     |     |
+-----+     +-----+

You can connect a child box to its parent:

+--------------+
|              |
|   +-----+    |
|   |     |    |
|   |     |----|
|   +-----+    |
|              |
+--------------+   

Edges can cross using a +:

         +-----+
         |     |
         |     |
         |     |
         +-----+
            |
+-----+     |     +-----+
|     |     |     |     |
|     |-----+---->|     |
|     |     |     |     |
+-----+     |     +-----+
            v
         +-----+
         |     |
         |     |
         |     |
         +-----+    

Labels

Edges can have an associated label:

+-----+
|     |
|     |
|     |
+-----+
   |
   |[label]
   |
+-----+         +-----+
|     | [label] |     |
|     |---------|     |
|     |         |     |
+-----+         +-----+

The label's [ or ] bracket must be adjacent (horizontally or vertically) to part of the edge.

Usage

import org.scalameta.ascii._

val diagram = Diagram("""
             +-+             
    ---------|E|----------   
    |        +-+         |   
 [9]|                 [6]|   
    |                    |   
   +-+  [2]  +-+  [11]  +-+  
   |F|-------|C|--------|D|  
   +-+       +-+        +-+  
    |         |          |   
[14]|     [10]|      [15]|   
    |         |          |   
   +-+       +-+         |   
   |A|-------|B|----------   
   +-+  [7]  +-+      """)

// Print all the vertices neighbouring A along with the edge weight:
for {
  box ← diagram.allBoxes.find(_.text == "A")
  (edge, otherBox) ← box.connections()
  label ← edge.label
} println(box + " ==> " + label + " ==> " + otherBox)