graphshaper 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -20,9 +20,17 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- The commandline tool expects one argument: The number of vertices you want your generated graph to have. This will result in two CSV files containing your vertices and edges. For example:
23
+ The commandline tool expects one argument: The number of vertices you want your generated graph to have. This is how to use it:
24
24
 
25
- graphshaper 50
25
+ graphshaper [options] size
26
+
27
+ You can use the following options:
28
+
29
+ * `-a`, `--avocado`: Store the graph in a local AvocadoDB instance
30
+ * `-l`, `--log`: Store the graph in two CSV files for nodes and edges
31
+ * `-d`, `--dot`: Store the graph in the dot format
32
+ * `-p`, `--png`: Export the graph as a PNG (you need to install graphviz for that – circo is used for the layout)
33
+ * `--version`: Show version
26
34
 
27
35
  You can also use the library in your Ruby Code. You can find the documentation [here](http://rubydoc.info/github/moonglum/graphshaper).
28
36
 
data/bin/graphshaper CHANGED
@@ -1,40 +1,95 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'graphshaper'
4
+ require 'optparse'
4
5
 
5
- if ARGV.length == 0 || ARGV[0] == "--help"
6
- puts " Usage: graphshaper SIZE"
7
- elsif ARGV[0].to_i < 21
8
- puts "Please choose a size of at least 21"
9
- else
10
- number_of_vertices = ARGV[0].to_i
11
- inner_vertices = 20
12
-
13
- vertex_output_file = File.new "vertices.csv", "w"
14
- edge_output_file = File.new "edges.csv", "w"
15
- logger = Graphshaper::LoggingAdapter.new vertex_output_file, edge_output_file
6
+ options = {}
7
+
8
+ optparse = OptionParser.new do |opts|
9
+ opts.banner = "Usage: graphshaper [options] size"
16
10
 
17
- avocado_db = Graphshaper::AvocadoDbAdapter.new "vertices", "edges"
11
+ opts.on("-a", "--avocado", "Store the graph in a local AvocadoDB instance") do |avocado|
12
+ options[:avocado] = avocado
13
+ end
18
14
 
19
- start_time = Time.now
20
- graph = Graphshaper::UndirectedGraph.without_orphans_with_order_of inner_vertices, adapters: [logger, avocado_db]
21
- (number_of_vertices - inner_vertices).times do
22
- graph.add_vertex do |preferential_attachment|
23
- preferential_attachment > rand
24
- end
15
+ opts.on("-l", "--log", "Store the graph in two CSV files for nodes and edges") do |log|
16
+ options[:log] = log
25
17
  end
26
- end_time = Time.now
27
18
 
28
- ellapsed_time = end_time - start_time
19
+ opts.on("-d", "--dot", "Store the graph in the dot format") do |dot|
20
+ options[:dot] = dot
21
+ end
29
22
 
30
- puts "#{graph.order} vertices (saved to vertices.csv)"
31
- puts "#{graph.size} edges (saved to edges.csv)"
23
+ opts.on("-p", "--png", "Export the graph as a PNG (graphviz must be installed)") do |png|
24
+ options[:dot] = png
25
+ options[:png] = png
26
+ end
32
27
 
33
- if ellapsed_time < 2
34
- puts "Generated and saved in about one second"
35
- elsif ellapsed_time < 60
36
- puts "Generated and saved in about #{ellapsed_time.round} seconds"
37
- else
38
- puts "Generated and saved in about #{ellapsed_time.round / 60} minutes and #{ellapsed_time.round % 60} seconds"
28
+ opts.on_tail("--version", "Show version") do
29
+ puts Graphshaper::VERSION
30
+ exit
39
31
  end
32
+
33
+ opts.parse!
34
+ end
35
+
36
+ if ARGV.length == 0
37
+ puts "Please enter a size for your graph."
38
+ exit
39
+ end
40
+
41
+ number_of_vertices = ARGV.first.to_i
42
+ inner_vertices = 20
43
+
44
+ adapters = []
45
+ open_files = []
46
+
47
+ if options[:log]
48
+ vertex_output_file = File.new "vertices.csv", "w"
49
+ open_files << vertex_output_file
50
+ edge_output_file = File.new "edges.csv", "w"
51
+ open_files << edge_output_file
52
+ adapters << Graphshaper::LoggingAdapter.new(vertex_output_file, edge_output_file)
40
53
  end
54
+
55
+ if options[:avocado]
56
+ adapters << Graphshaper::AvocadoDbAdapter.new("vertices", "edges")
57
+ end
58
+
59
+ if options[:dot]
60
+ dot_output_file = File.new "generated_graph.dot", "w"
61
+ open_files << dot_output_file
62
+ dot_adapter = Graphshaper::DotAdapter.new(dot_output_file)
63
+ adapters << dot_adapter
64
+ end
65
+
66
+ start_time = Time.now
67
+
68
+ graph = Graphshaper::UndirectedGraph.without_orphans_with_order_of inner_vertices, adapters: adapters
69
+ (number_of_vertices - inner_vertices).times do
70
+ graph.add_vertex { |preferential_attachment| preferential_attachment > rand }
71
+ end
72
+
73
+ dot_adapter.close if options[:dot]
74
+
75
+ ellapsed_time = Time.now - start_time
76
+
77
+ print "#{graph.order} vertices and #{graph.size} edges generated"
78
+
79
+ print " and saved into AvocadoDB" if options[:avocado]
80
+ print " and logged" if options[:log]
81
+ print " and generated as a dot" if options[:dot]
82
+
83
+ open_files.each { |file| file.close }
84
+
85
+ if options[:png]
86
+ system('circo -Tpng generated_graph.dot -o generated_graph.png')
87
+ end
88
+
89
+ if ellapsed_time < 2
90
+ puts " in about one second"
91
+ elsif ellapsed_time < 60
92
+ puts " in about #{ellapsed_time.round} seconds"
93
+ else
94
+ puts " in about #{ellapsed_time.round / 60} minutes and #{ellapsed_time.round % 60} seconds"
95
+ end
@@ -0,0 +1,20 @@
1
+ module Graphshaper
2
+ class DotAdapter
3
+ def initialize(output_file)
4
+ @output_file = output_file
5
+ @output_file << "digraph genereated_graph { \n rankdir=LR;\n node [shape = circle];\n edge [dir=none];\n"
6
+ end
7
+
8
+ def close
9
+ @output_file << "}"
10
+ end
11
+
12
+ def add_edge(edge_id, in_id, out_id)
13
+ @output_file << " #{in_id} -> #{out_id} [ label = \"#{edge_id}\" ];\n"
14
+ end
15
+
16
+ def add_vertex(vertex_id)
17
+ @output_file << " #{vertex_id};\n"
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Graphshaper
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/graphshaper.rb CHANGED
@@ -2,6 +2,7 @@ require "graphshaper/version"
2
2
  require "graphshaper/undirected_graph"
3
3
  require "graphshaper/adapters/avocadodb_adapter"
4
4
  require "graphshaper/adapters/logging_adapter"
5
+ require "graphshaper/adapters/dot_adapter"
5
6
 
6
7
  module Graphshaper
7
8
  end
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ describe Graphshaper::DotAdapter do
4
+ before :each do
5
+ @output_file = double()
6
+ @output_file.stub :<<
7
+ end
8
+
9
+ it "should write a header into the file" do
10
+ @output_file.should_receive(:<<).with("digraph genereated_graph { \n rankdir=LR;\n node [shape = circle];\n edge [dir=none];\n")
11
+ Graphshaper::DotAdapter.new @output_file
12
+ end
13
+
14
+ describe "initialized Dot Adapter" do
15
+ before :each do
16
+ @dot_adapter = Graphshaper::DotAdapter.new @output_file
17
+ end
18
+
19
+ it "should be closeable" do
20
+ @output_file.should_receive(:<<).with("}")
21
+ @dot_adapter.close
22
+ end
23
+
24
+ it "should write an edge in the correct format" do
25
+ @output_file.should_receive(:<<).with(" 1 -> 2 [ label = \"0\" ];\n")
26
+ @dot_adapter.add_edge(0,1,2)
27
+ end
28
+
29
+ it "should write a vertex in the correct format" do
30
+ @output_file.should_receive(:<<).with(" 15;\n")
31
+ @dot_adapter.add_vertex(15)
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphshaper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
16
- requirement: &70277557670180 !ruby/object:Gem::Requirement
16
+ requirement: &70255428363860 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.8.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70277557670180
24
+ version_requirements: *70255428363860
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70277557669680 !ruby/object:Gem::Requirement
27
+ requirement: &70255428363360 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.9.2.2
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70277557669680
35
+ version_requirements: *70255428363360
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70277557669220 !ruby/object:Gem::Requirement
38
+ requirement: &70255428362900 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.9.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70277557669220
46
+ version_requirements: *70255428362900
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: yard
49
- requirement: &70277557668760 !ruby/object:Gem::Requirement
49
+ requirement: &70255428362440 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 0.7.5
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70277557668760
57
+ version_requirements: *70255428362440
58
58
  description: Generate realistic graphs
59
59
  email:
60
60
  - me@moonglum.net
@@ -73,9 +73,11 @@ files:
73
73
  - graphshaper.gemspec
74
74
  - lib/graphshaper.rb
75
75
  - lib/graphshaper/adapters/avocadodb_adapter.rb
76
+ - lib/graphshaper/adapters/dot_adapter.rb
76
77
  - lib/graphshaper/adapters/logging_adapter.rb
77
78
  - lib/graphshaper/undirected_graph.rb
78
79
  - lib/graphshaper/version.rb
80
+ - spec/adapters/dot_adapter_spec.rb
79
81
  - spec/adapters/logging_adapter_spec.rb
80
82
  - spec/spec_helper.rb
81
83
  - spec/undirected_graph_spec.rb
@@ -104,6 +106,7 @@ signing_key:
104
106
  specification_version: 3
105
107
  summary: Graphshaper can generate realistic, scale-free graphs of any size.
106
108
  test_files:
109
+ - spec/adapters/dot_adapter_spec.rb
107
110
  - spec/adapters/logging_adapter_spec.rb
108
111
  - spec/spec_helper.rb
109
112
  - spec/undirected_graph_spec.rb