ascii-graphs


License

License

MIT
GroupId

GroupId

com.github.mutcianm
ArtifactId

ArtifactId

ascii-graphs_2.11
Last Version

Last Version

0.0.6
Release Date

Release Date

Type

Type

jar
Description

Description

ascii-graphs
ascii-graphs
Project URL

Project URL

https://github.com/mutcianm/ascii-graphs
Project Organization

Project Organization

com.github.mutcianm
Source Code Management

Source Code Management

https://github.com/mutcianm/ascii-graphs

Download ascii-graphs_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.mutcianm/ascii-graphs_2.11/ -->
<dependency>
    <groupId>com.github.mutcianm</groupId>
    <artifactId>ascii-graphs_2.11</artifactId>
    <version>0.0.6</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.mutcianm/ascii-graphs_2.11/
implementation 'com.github.mutcianm:ascii-graphs_2.11:0.0.6'
// https://jarcasting.com/artifacts/com.github.mutcianm/ascii-graphs_2.11/
implementation ("com.github.mutcianm:ascii-graphs_2.11:0.0.6")
'com.github.mutcianm:ascii-graphs_2.11:jar:0.0.6'
<dependency org="com.github.mutcianm" name="ascii-graphs_2.11" rev="0.0.6">
  <artifact name="ascii-graphs_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.mutcianm', module='ascii-graphs_2.11', version='0.0.6')
)
libraryDependencies += "com.github.mutcianm" % "ascii-graphs_2.11" % "0.0.6"
[com.github.mutcianm/ascii-graphs_2.11 "0.0.6"]

Dependencies

compile (1)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.8

test (2)

Group / Artifact Type Version
org.scalatest : scalatest_2.11 jar 3.0.7
org.scalacheck : scalacheck_2.11 jar 1.14.0

Project Modules

There are no modules declared in this project.

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:

libraryDependencies += "com.github.mdr" %% "ascii-graphs" % "0.0.3"

Graph layout

import com.github.mdr.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 com.github.mdr.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)

Versions

Version
0.0.6