yagraphlib 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/lib/yagraphlib.rb +113 -0
  4. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0106303fc6aabcd29143cea8477a736bd6260d28
4
+ data.tar.gz: 24bb1426c89edbea9cd861dcd91a24f54f3783c8
5
+ SHA512:
6
+ metadata.gz: 9b86a0d50ce724ac51a9dfbba400d3eaa74e708ed8b11c102b4c2156139aa948f84830acb1c23fc1e99e1c90027291868db9b8143f913ec558c11dfa5ce73df5
7
+ data.tar.gz: '0909427006581726b22a2dca9869910cb7555bb3b1735d47d48152df99b1c144f576e3cd2c2d0e79b2324c5951e2e867270b0c482322defdfb8047396c0317d0'
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org' do
2
+ gemspec
3
+ end
data/lib/yagraphlib.rb ADDED
@@ -0,0 +1,113 @@
1
+
2
+ """
3
+ Extend the Node and Edge classes to control rendering.
4
+
5
+ Add nodes by calling graph#nodes passing in the new node. Note that this function de-duplicates based upon the uid of the node; returning the existing node if possible.
6
+
7
+ Add edges in the same style; calling graph#edges. Again, this de-duplicates based on the uid of the edge.
8
+
9
+ Extension points for Node:
10
+
11
+ uid: determines if a node is the same as another node in the graph. Default: random 16 word characters.
12
+ id: graphviz node id; must return an identifier.
13
+ label: display name for the node; any graphviz label is valid. Default: id.
14
+ shape: graphviz shape. Default: 'oval'.
15
+ colour: line colour for the node. Default: 'black'
16
+
17
+ Extension points for Edge:
18
+
19
+ uid: determines if an edge is the same as another edge. Useful for de-duplicating edges. Default: random 16 word characters.
20
+ label: display name for the node; any graphviz label is valid. Default: '' [empty string].
21
+ colour: line colour for the node. Default: 'black'
22
+
23
+ """
24
+ module YaGraph
25
+ class Element
26
+ def to_graphviz(out) "" end
27
+
28
+ """
29
+ an id that is unique in the context of the element type. Subtypes should override this.
30
+ """
31
+ def uid()
32
+ @uid ||= random_id()
33
+ end
34
+
35
+ def random_id()
36
+ (0...16).map { (65 + rand(26)).chr }.join
37
+ end
38
+ end
39
+
40
+ class Graph < Element
41
+ RANKDIR_LR = "LR"
42
+
43
+ def initialize()
44
+ @nodes = {}
45
+ @edges = {}
46
+ end
47
+
48
+ def edges(edge)
49
+ @edges[edge.uid] ||= edge
50
+ end
51
+
52
+ def edge?(s, e)
53
+ @edges.any? do |k, v|
54
+ v.start == s and v.finish == e
55
+ end
56
+ end
57
+
58
+ def nodes(node)
59
+ @nodes[node.uid] ||= node
60
+ end
61
+
62
+ def rankdir(mode)
63
+ @rankdir = mode
64
+ end
65
+
66
+ def initial_nodes()
67
+ initial = {}
68
+ @nodes.each {|k, n| initial[k] = n}
69
+ @edges.each do |k, e|
70
+ initial.delete(e.finish.uid)
71
+ end
72
+ initial.values
73
+ end
74
+
75
+ def terminal_nodes()
76
+ terminal = {}
77
+ @nodes.each {|k, n| terminal[k] = n}
78
+ @edges.each do |k, e|
79
+ terminal.delete(e.start.uid)
80
+ end
81
+ terminal.values
82
+ end
83
+
84
+ def to_graphviz(out)
85
+ out.puts("digraph main {\n")
86
+ out.puts(" rankdir=#{@rankdir}") if @rankdir
87
+ @nodes.each {|k, n| n.to_graphviz(out) }
88
+ @edges.each {|k, e| e.to_graphviz(out) }
89
+ out.puts("}")
90
+ end
91
+ end
92
+
93
+ class Node < Element
94
+ def id() "" end
95
+ def to_graphviz(out)
96
+ out.puts(" #{id} [ color=\"#{colour}\" label=\"#{label}\" shape=\"#{shape}\" ]")
97
+ end
98
+ def label() id end
99
+ def shape() "oval" end
100
+ def colour() "black" end
101
+ end
102
+
103
+ class Edge < Element
104
+ attr_reader :start, :finish
105
+ def to_graphviz(out)
106
+ out.puts(" #{start.id} -> #{finish.id} [ color=\"#{colour}\" label=\"#{label}\"]")
107
+ end
108
+ def label() "" end
109
+ def colour() "black" end
110
+ end
111
+ end
112
+
113
+ # vi: sw=2 ts=2 sts=2 et
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yagraphlib
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - candle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.5.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.5.0
27
+ description: " Yet Another Graph Library\n"
28
+ email: candle@candle.me.uk
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - Gemfile
34
+ - lib/yagraphlib.rb
35
+ homepage:
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.6.13
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Yet Another Graph Library
59
+ test_files: []