graphshaper 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/graphshaper CHANGED
@@ -10,11 +10,12 @@ else
10
10
  number_of_vertices = ARGV[0].to_i
11
11
  inner_vertices = 20
12
12
 
13
- edge_output_file = File.new "edges.csv", "w"
14
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
15
16
 
16
17
  start_time = Time.now
17
- graph = Graphshaper::UndirectedGraph.without_orphans_with_order_of inner_vertices, edge_creation_logger: edge_output_file, vertex_creation_logger: vertex_output_file
18
+ graph = Graphshaper::UndirectedGraph.without_orphans_with_order_of inner_vertices, adapters: [logger]
18
19
  (number_of_vertices - inner_vertices).times do
19
20
  graph.add_vertex do |preferential_attachment|
20
21
  preferential_attachment > rand
@@ -34,4 +35,4 @@ else
34
35
  else
35
36
  puts "Generated in about #{ellapsed_time.round / 60} minutes and #{ellapsed_time.round % 60} seconds"
36
37
  end
37
- end
38
+ end
data/graphshaper.gemspec CHANGED
@@ -15,6 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Graphshaper::VERSION
17
17
 
18
+ gem.add_dependency "httparty", "~> 0.8.1"
18
19
  gem.add_development_dependency "rake", "~> 0.9.2.2"
19
20
  gem.add_development_dependency "rspec", "~> 2.9.0"
20
21
  gem.add_development_dependency "yard", "~> 0.7.5"
@@ -0,0 +1,20 @@
1
+ module Graphshaper
2
+
3
+ # A simple adapter for logging the created graph
4
+ class LoggingAdapter
5
+ def initialize(vertex_logger_file, edge_logger_file)
6
+ @vertex_logger_file = vertex_logger_file
7
+ @edge_logger_file = edge_logger_file
8
+ @vertex_logger_file << "vertex_id\n"
9
+ @edge_logger_file << "edge_id,from_id,to_id\n"
10
+ end
11
+
12
+ def add_edge(edge_id, from_id, to_id)
13
+ @edge_logger_file << "#{edge_id},#{from_id},#{to_id}\n"
14
+ end
15
+
16
+ def add_vertex(vertex_id)
17
+ @vertex_logger_file << "#{vertex_id}\n"
18
+ end
19
+ end
20
+ end
@@ -9,12 +9,10 @@ module Graphshaper
9
9
  #
10
10
  # @param [Integer] number_of_vertices The number of vertices that the generated graph should have
11
11
  # @param [Hash] options_hash The options to create an undirected graph
12
- # @option options_hash [IO] :edge_creation_logger An IO object that should log every edge creation in the graph (default: no logging)
13
- # @option options_hash [IO] ::vertex_creation_logger An IO object that should log every vertex creation in the graph (default: no logging)
12
+ # @option options_hash [Array<Object>] :adapters An array of adapters you want to use
14
13
  def initialize(number_of_vertices, options_hash = {})
15
- @edge_creation_logger = options_hash[:edge_creation_logger] if options_hash.has_key? :edge_creation_logger
16
- @vertex_creation_logger = options_hash[:vertex_creation_logger] if options_hash.has_key? :vertex_creation_logger
17
-
14
+ @adapters = options_hash[:adapters] if options_hash.has_key? :adapters
15
+
18
16
  @vertex_degrees = []
19
17
  @unconnected_vertices = Set.new
20
18
 
@@ -26,8 +24,7 @@ module Graphshaper
26
24
  #
27
25
  # @param [Integer] number_of_vertices The number of vertices that the generated graph should have
28
26
  # @param [Hash] options_hash The options to create an undirected graph
29
- # @option options_hash [IO] :edge_creation_logger An IO object that should log every edge creation in the graph (default: no logging)
30
- # @option options_hash [IO] ::vertex_creation_logger An IO object that should log every vertex creation in the graph (default: no logging)
27
+ # @option options_hash [Array<Object>] :adapters An array of adapters you want to use
31
28
  # @return [UndirectedGraph] The generated graph.
32
29
  def UndirectedGraph.without_orphans_with_order_of(number_of_vertices, options_hash = {})
33
30
  graph = self.new number_of_vertices, options_hash
@@ -70,7 +67,11 @@ module Graphshaper
70
67
  @vertex_degrees << 0
71
68
  @unconnected_vertices << new_vertex_id
72
69
 
73
- @vertex_creation_logger << "#{new_vertex_id}\n" if @vertex_creation_logger
70
+ unless @adapters.nil?
71
+ @adapters.each do |adapter|
72
+ adapter.add_vertex new_vertex_id
73
+ end
74
+ end
74
75
 
75
76
  if block_given?
76
77
  each_vertex_with_preferential_attachment do |vertex_id, preferential_attachment|
@@ -98,7 +99,11 @@ module Graphshaper
98
99
  @vertex_degrees[second_vertex_id] += 1
99
100
  @edges << [first_vertex_id, second_vertex_id].sort
100
101
 
101
- @edge_creation_logger << "#{first_vertex_id},#{second_vertex_id}\n" if @edge_creation_logger
102
+ unless @adapters.nil?
103
+ @adapters.each do |adapter|
104
+ adapter.add_edge @edges.length - 1, first_vertex_id, second_vertex_id
105
+ end
106
+ end
102
107
  end
103
108
  end
104
109
 
@@ -1,3 +1,3 @@
1
1
  module Graphshaper
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
data/lib/graphshaper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "graphshaper/version"
2
2
  require "graphshaper/undirected_graph"
3
+ require "graphshaper/adapters/logging_adapter"
3
4
 
4
5
  module Graphshaper
5
6
  end
@@ -0,0 +1,32 @@
1
+ require "spec_helper"
2
+
3
+ describe Graphshaper::LoggingAdapter do
4
+ before :each do
5
+ @vertex_logger = double()
6
+ @vertex_logger.stub :<<
7
+ @edge_logger = double()
8
+ @edge_logger.stub :<<
9
+ end
10
+
11
+ it "should write a header into the files" do
12
+ @vertex_logger.should_receive(:<<).with("vertex_id\n")
13
+ @edge_logger.should_receive(:<<).with("edge_id,from_id,to_id\n")
14
+ Graphshaper::LoggingAdapter.new @vertex_logger, @edge_logger
15
+ end
16
+
17
+ describe "Initialized Logger" do
18
+ before :each do
19
+ @adapter = Graphshaper::LoggingAdapter.new @vertex_logger, @edge_logger
20
+ end
21
+
22
+ it "should write edges to the logger at edge creation" do
23
+ @edge_logger.should_receive(:<<).with("0,1,3\n")
24
+ @adapter.add_edge 0,1,3
25
+ end
26
+
27
+ it "should write vertices to the logger at vertex creation" do
28
+ @vertex_logger.should_receive(:<<).with("5\n")
29
+ @adapter.add_vertex 5
30
+ end
31
+ end
32
+ end
@@ -1,5 +1,4 @@
1
1
  require "spec_helper"
2
- require "stringio" # for testing the logger
3
2
 
4
3
  describe Graphshaper::UndirectedGraph do
5
4
  it "should create a graph with a given number of vertices and no edges" do
@@ -8,23 +7,6 @@ describe Graphshaper::UndirectedGraph do
8
7
  graph.size.should ==(0)
9
8
  end
10
9
 
11
- it "should create a graph with a logger for edge creation" do
12
- edge_creation_logger = StringIO.new
13
- graph = Graphshaper::UndirectedGraph.new 5, edge_creation_logger: edge_creation_logger
14
-
15
- graph.add_edge 1,3
16
- graph.add_edge 2,3
17
- edge_creation_logger.string.should ==("1,3\n2,3\n")
18
- end
19
-
20
- it "should create a graph with a logger for vertex creation" do
21
- vertex_creation_logger = StringIO.new
22
- graph = Graphshaper::UndirectedGraph.new 5, vertex_creation_logger: vertex_creation_logger
23
-
24
- graph.add_vertex
25
- vertex_creation_logger.string.should ==("0\n1\n2\n3\n4\n5\n")
26
- end
27
-
28
10
  describe "initialized graph" do
29
11
  before :each do
30
12
  @graph = Graphshaper::UndirectedGraph.new 5
@@ -167,4 +149,31 @@ describe Graphshaper::UndirectedGraph do
167
149
  @graph.size.should ==(3)
168
150
  end
169
151
  end
152
+
153
+ describe "Adapter Support" do
154
+ before :each do
155
+ @adapter = double()
156
+ @adapter.stub :add_vertex
157
+ @adapter.stub :add_edge
158
+ end
159
+
160
+ it "should tell the adapter about the inital vertices on creation" do
161
+ 5.times do |vertex_id|
162
+ @adapter.should_receive(:add_vertex).with(vertex_id)
163
+ end
164
+ graph = Graphshaper::UndirectedGraph.new 5, adapters: [@adapter]
165
+ end
166
+
167
+ it "should tell the adapter about later added vertices" do
168
+ graph = Graphshaper::UndirectedGraph.new 5, adapters: [@adapter]
169
+ @adapter.should_receive(:add_vertex).with(5)
170
+ graph.add_vertex
171
+ end
172
+
173
+ it "should tell the adapter about later added edges" do
174
+ graph = Graphshaper::UndirectedGraph.new 5, adapters: [@adapter]
175
+ @adapter.should_receive(:add_edge).with(0, 1, 2)
176
+ graph.add_edge 1, 2
177
+ end
178
+ end
170
179
  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.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,9 +11,20 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-04-04 00:00:00.000000000Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &70304824409140 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70304824409140
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: rake
16
- requirement: &70350123480940 !ruby/object:Gem::Requirement
27
+ requirement: &70304824408640 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ~>
@@ -21,10 +32,10 @@ dependencies:
21
32
  version: 0.9.2.2
22
33
  type: :development
23
34
  prerelease: false
24
- version_requirements: *70350123480940
35
+ version_requirements: *70304824408640
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: rspec
27
- requirement: &70350123480440 !ruby/object:Gem::Requirement
38
+ requirement: &70304824408180 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ~>
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: 2.9.0
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *70350123480440
46
+ version_requirements: *70304824408180
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: yard
38
- requirement: &70350123479980 !ruby/object:Gem::Requirement
49
+ requirement: &70304824407720 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ~>
@@ -43,7 +54,7 @@ dependencies:
43
54
  version: 0.7.5
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *70350123479980
57
+ version_requirements: *70304824407720
47
58
  description: Generate realistic graphs
48
59
  email:
49
60
  - me@moonglum.net
@@ -61,8 +72,10 @@ files:
61
72
  - bin/graphshaper
62
73
  - graphshaper.gemspec
63
74
  - lib/graphshaper.rb
75
+ - lib/graphshaper/adapters/logging_adapter.rb
64
76
  - lib/graphshaper/undirected_graph.rb
65
77
  - lib/graphshaper/version.rb
78
+ - spec/adapters/logging_adapter_spec.rb
66
79
  - spec/spec_helper.rb
67
80
  - spec/undirected_graph_spec.rb
68
81
  homepage: http://github.com/moonglum/graphshaper
@@ -90,6 +103,7 @@ signing_key:
90
103
  specification_version: 3
91
104
  summary: Graphshaper can generate realistic, scale-free graphs of any size.
92
105
  test_files:
106
+ - spec/adapters/logging_adapter_spec.rb
93
107
  - spec/spec_helper.rb
94
108
  - spec/undirected_graph_spec.rb
95
109
  has_rdoc: