directed_graph 0.6.0 → 0.6.1
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.
- checksums.yaml +4 -4
- data/README.md +22 -0
- data/example/example.rb +58 -0
- data/lib/directed_graph.rb +2 -0
- data/lib/directed_graph/graphml.rb +78 -0
- data/lib/directed_graph/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 072f04bf6d9b527c8b6754ddc0ba6bcc7ce78be8
|
4
|
+
data.tar.gz: 525dcb15965f21d4021f8e231a153bebbe318cfa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae7c9473a9d90281a79c1fa9edba73922b17dc8c3a1bcc8d9d31981bae2d02bf586a1bb3ba38aaf30b87c8a5cf8e865aecda94c492d8a1f10fc965b90a4169a8
|
7
|
+
data.tar.gz: 2bd210fc46ecf1865d0176de90abcba0775a5352830662b5e0596beb275aaf9718b50817c3f64fffef773bb01c060e16787e3a29c7582a2f38ebdabfb881b8b7
|
data/README.md
CHANGED
@@ -53,6 +53,28 @@ The `@graph` object can be used to calculate the longest path between two vertic
|
|
53
53
|
@graph.longest_path(@d, @a) # returns [] when the path doesn't exist
|
54
54
|
```
|
55
55
|
|
56
|
+
The `@graph` object can be used to generate a graphml file
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
require 'color-generator'
|
60
|
+
|
61
|
+
generator = ColorGenerator.new saturation: 0.3, lightness: 0.75
|
62
|
+
|
63
|
+
# Set the color and label of each vertex
|
64
|
+
#
|
65
|
+
@graph.vertices.each do |vertex|
|
66
|
+
color = generator.create_hex
|
67
|
+
label = vertex.name
|
68
|
+
|
69
|
+
vertex.data[:graphics] = {:fill=>["##{color}"], :label=>[label], :group=>color}
|
70
|
+
end
|
71
|
+
|
72
|
+
# Generate a graphml file
|
73
|
+
# Import this file into program like yEd to size nodes and layout graph
|
74
|
+
File.open("output.graphml", "w+") { |f| f.write(@graph.to_graphml())}
|
75
|
+
```
|
76
|
+
|
77
|
+
|
56
78
|
## Installation
|
57
79
|
|
58
80
|
Add this line to your application's Gemfile:
|
data/example/example.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'color-generator'
|
3
|
+
require 'directed_graph' #This is an external gem
|
4
|
+
|
5
|
+
include DirectedGraph
|
6
|
+
|
7
|
+
class BugTest < Minitest::Test
|
8
|
+
def test_two()
|
9
|
+
|
10
|
+
# Create the Vertices
|
11
|
+
#
|
12
|
+
@root = Vertex.new(name: "root")
|
13
|
+
@a = Vertex.new(name: "a")
|
14
|
+
@b = Vertex.new(name: "b")
|
15
|
+
@c = Vertex.new(name: "c")
|
16
|
+
@d = Vertex.new(name: "d")
|
17
|
+
@e = Vertex.new(name: "e")
|
18
|
+
|
19
|
+
# Create the Edges
|
20
|
+
#
|
21
|
+
# The first argument is the origin_vertex
|
22
|
+
# and the second is the destination_vertex
|
23
|
+
@ra = Edge.new(origin_vertex: @root, destination_vertex: @a)
|
24
|
+
@ab = Edge.new(origin_vertex: @a, destination_vertex: @b)
|
25
|
+
@bc = Edge.new(origin_vertex: @b, destination_vertex: @c)
|
26
|
+
@bd = Edge.new(origin_vertex: @b, destination_vertex: @d)
|
27
|
+
@ae = Edge.new(origin_vertex: @a, destination_vertex: @e)
|
28
|
+
@de = Edge.new(origin_vertex: @d, destination_vertex: @e)
|
29
|
+
|
30
|
+
# Create the Graph
|
31
|
+
#
|
32
|
+
@edges = [@ra, @ab, @bc, @bd, @ae, @de]
|
33
|
+
@graph = Graph.new(@edges)
|
34
|
+
|
35
|
+
# Set the color of the vertexes
|
36
|
+
#
|
37
|
+
@generator = ColorGenerator.new saturation: 0.3, lightness: 0.75
|
38
|
+
|
39
|
+
@graph.vertices.each do |vertex|
|
40
|
+
label = vertex.name
|
41
|
+
color = @generator.create_hex
|
42
|
+
|
43
|
+
vertex.data[:graphics] = {:fill=>["##{color}"], :label=>[label], :group=>color}
|
44
|
+
end
|
45
|
+
|
46
|
+
# Generate a graphml file
|
47
|
+
# Import this file into program like yEd to size nodes and layout graph
|
48
|
+
#
|
49
|
+
filename = "example_graph"
|
50
|
+
|
51
|
+
txt = @graph.to_graphml()
|
52
|
+
|
53
|
+
File.open("#{filename}.graphml", "w+") { |f| f.write(txt)}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
|
data/lib/directed_graph.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
require 'simple-graph'
|
2
|
+
require 'builder'
|
2
3
|
|
3
4
|
require "directed_graph/version"
|
4
5
|
require_relative "./directed_graph/job_runner.rb"
|
5
6
|
require_relative "./directed_graph/vertex.rb"
|
6
7
|
require_relative "./directed_graph/edge.rb"
|
7
8
|
require_relative "./directed_graph/graph.rb"
|
9
|
+
require_relative "./directed_graph/graphml.rb"
|
8
10
|
|
9
11
|
module DirectedGraph
|
10
12
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
#require 'builder'
|
2
|
+
|
3
|
+
module DirectedGraph
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
# Generate a string key from an array of identifiers
|
7
|
+
def compute_key(external_identifier)
|
8
|
+
x = [external_identifier].flatten
|
9
|
+
x.map! {|xx| xx.to_s}
|
10
|
+
x.join("_")
|
11
|
+
end
|
12
|
+
|
13
|
+
# Return graph as graphml text
|
14
|
+
def to_graphml()
|
15
|
+
builder = Builder::XmlMarkup.new(:indent => 1)
|
16
|
+
builder.instruct! :xml, :version => "1.0"
|
17
|
+
|
18
|
+
graphml_attribs = {
|
19
|
+
"xmlns" => "http://graphml.graphdrawing.org/xmlns",
|
20
|
+
"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
|
21
|
+
"xmlns:y" => "http://www.yworks.com/xml/graphml",
|
22
|
+
"xmlns:yed" => "http://www.yworks.com/xml/yed/3",
|
23
|
+
"xsi:schemaLocation" => "http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd",
|
24
|
+
:directed => "1",
|
25
|
+
:label => "test"
|
26
|
+
}
|
27
|
+
|
28
|
+
builder.graphml(graphml_attribs) do
|
29
|
+
|
30
|
+
# Define key id's at top of graphml file
|
31
|
+
builder.key({:for=>"node", :id=>"d3", "yfiles.type"=>"nodegraphics"})
|
32
|
+
|
33
|
+
# Build Graph
|
34
|
+
#
|
35
|
+
builder.graph({:id=>"G"}) do
|
36
|
+
|
37
|
+
vertices.each do |vertex|
|
38
|
+
|
39
|
+
builder.node(:id => compute_key([vertex.name, vertex.object_id])) do
|
40
|
+
|
41
|
+
builder.data({:key=>"d3"}) do
|
42
|
+
builder.tag!("y:ShapeNode") do
|
43
|
+
|
44
|
+
graphics = vertex.data.fetch(:graphics, {})
|
45
|
+
graphics.fetch(:fill, []).each {|f| builder.tag! "y:Fill", {:color=>f, :transparent=>"false"}}
|
46
|
+
graphics.fetch(:shape,[]).each {|s| builder.tag! "y:Shape", {:type=>s}}
|
47
|
+
graphics.fetch(:geometry,[]).each {|s| builder.tag! "y:Geometry", s}
|
48
|
+
graphics.fetch(:label,[]).each {|l| builder.tag! "y:NodeLabel", l}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
edges.each do |edge|
|
55
|
+
source = edge.origin_vertex
|
56
|
+
target = edge.destination_vertex
|
57
|
+
|
58
|
+
options = edge.data[:options]
|
59
|
+
label = ""
|
60
|
+
|
61
|
+
builder.edge(
|
62
|
+
:source => s = compute_key([source.name, source.object_id]),
|
63
|
+
:target => t = compute_key([target.name, target.object_id]),
|
64
|
+
:id => compute_key([source.name, target.name, edge.object_id]),
|
65
|
+
:label => "#{label}"
|
66
|
+
) do
|
67
|
+
#edge[:attributes].each_pair { |k, v|
|
68
|
+
# id_str = compute_key([k,:edge_attr])
|
69
|
+
# builder.data(v.to_s, {:key=>@guid[id_str]})
|
70
|
+
#}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
builder.target!
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: directed_graph
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MrPowers
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -98,10 +98,12 @@ files:
|
|
98
98
|
- bin/console
|
99
99
|
- bin/setup
|
100
100
|
- directed_graph.gemspec
|
101
|
+
- example/example.rb
|
101
102
|
- example/simple_directed_graph.png
|
102
103
|
- lib/directed_graph.rb
|
103
104
|
- lib/directed_graph/edge.rb
|
104
105
|
- lib/directed_graph/graph.rb
|
106
|
+
- lib/directed_graph/graphml.rb
|
105
107
|
- lib/directed_graph/job_runner.rb
|
106
108
|
- lib/directed_graph/version.rb
|
107
109
|
- lib/directed_graph/vertex.rb
|