rubigraph 0.1.0 → 0.2.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.
- data/lib/rubigraph.rb +30 -18
- data/test/test_rubigraph.rb +37 -0
- metadata +2 -2
data/lib/rubigraph.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'xmlrpc/client'
|
2
2
|
require 'pp'
|
3
|
+
require 'thread'
|
3
4
|
|
4
5
|
#
|
5
6
|
# Ruby wrapper for Ubigraph.
|
@@ -7,31 +8,31 @@ require 'pp'
|
|
7
8
|
# Call Rubigraph.init at first.
|
8
9
|
#
|
9
10
|
module Rubigraph
|
10
|
-
VERSION = '0.
|
11
|
+
VERSION = '0.2.0'
|
11
12
|
|
12
13
|
class Vertex
|
13
14
|
attr_reader :id
|
14
15
|
|
15
16
|
def initialize(id = nil)
|
16
17
|
@id = id ?
|
17
|
-
Rubigraph.
|
18
|
-
Rubigraph.
|
18
|
+
Rubigraph.call('ubigraph.new_vertex_w_id', id) :
|
19
|
+
Rubigraph.call('ubigraph.new_vertex')
|
19
20
|
raise 'Rubigraph::Vertex.initialize: cannot create vertex' if @id == -1
|
20
21
|
end
|
21
22
|
|
22
23
|
def remove
|
23
|
-
if -1 == Rubigraph.
|
24
|
+
if -1 == Rubigraph.call('ubigraph.remove_vertex', @id)
|
24
25
|
raise "Rubigraph::Vertex#remove: cannot remove vertex #{@id}"
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
28
29
|
def set_attribute(att, value)
|
29
|
-
Rubigraph.
|
30
|
+
Rubigraph.call('ubigraph.set_vertex_attribute', @id, att, value.to_s)
|
30
31
|
end
|
31
32
|
|
32
33
|
def set_attributes(attrs)
|
33
34
|
attrs.each do |k, v|
|
34
|
-
Rubigraph.
|
35
|
+
Rubigraph.call('ubigraph.set_vertex_attribute', @id, k, v.to_s)
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
@@ -83,27 +84,25 @@ module Rubigraph
|
|
83
84
|
# create an Edge.
|
84
85
|
# from, to should be Vertex.
|
85
86
|
def initialize(from, to, id = nil)
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
@id = Rubigraph.server.call('ubigraph.new_edge', from.id, to.id)
|
90
|
-
end
|
87
|
+
@id = id ?
|
88
|
+
Rubigraph.call('ubigraph.new_edge_w_id', id, from.id, to.id) :
|
89
|
+
Rubigraph.call('ubigraph.new_edge', from.id, to.id)
|
91
90
|
raise 'Rubigraph::Edge.initialize: cannot create edge' if @id == -1
|
92
91
|
end
|
93
92
|
|
94
93
|
def remove
|
95
|
-
if -1 == Rubigraph.
|
94
|
+
if -1 == Rubigraph.call('ubigraph.remove_edge', @id)
|
96
95
|
raise "Rubigraph::Edge#remove: cannot remove edge #{@id}"
|
97
96
|
end
|
98
97
|
end
|
99
98
|
|
100
99
|
def set_attribute(att, value)
|
101
|
-
Rubigraph.
|
100
|
+
Rubigraph.call('ubigraph.set_edge_attribute', @id, att, value.to_s)
|
102
101
|
end
|
103
102
|
|
104
103
|
def set_attributes(attrs)
|
105
104
|
attrs.each do |k, v|
|
106
|
-
Rubigraph.
|
105
|
+
Rubigraph.call('ubigraph.set_edge_attribute', @id, k, v.to_s)
|
107
106
|
end
|
108
107
|
end
|
109
108
|
|
@@ -163,15 +162,28 @@ module Rubigraph
|
|
163
162
|
# initialize XML-RPC client
|
164
163
|
def self.init(host='127.0.0.1', port='20738')
|
165
164
|
@server = XMLRPC::Client.new2("http://#{host}:#{port}/RPC2")
|
165
|
+
@mutex = Mutex.new
|
166
166
|
end
|
167
167
|
|
168
168
|
# clear all vertex, edges
|
169
169
|
def self.clear
|
170
|
-
|
170
|
+
call('ubigraph.clear')
|
171
171
|
end
|
172
172
|
|
173
|
-
|
174
|
-
|
175
|
-
|
173
|
+
def self.call(msg, *args)
|
174
|
+
@mutex.synchronize {
|
175
|
+
case args.size
|
176
|
+
when 0
|
177
|
+
@server.call(msg)
|
178
|
+
when 1
|
179
|
+
@server.call(msg, args[0])
|
180
|
+
when 2
|
181
|
+
@server.call(msg, args[0], args[1])
|
182
|
+
when 3
|
183
|
+
@server.call(msg, args[0], args[1], args[2])
|
184
|
+
else
|
185
|
+
raise
|
186
|
+
end
|
187
|
+
}
|
176
188
|
end
|
177
189
|
end # Rubigraph
|
data/test/test_rubigraph.rb
CHANGED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'lib/rubigraph'
|
3
|
+
|
4
|
+
Rubigraph.init
|
5
|
+
|
6
|
+
class RubigraphTest < Test::Unit::TestCase
|
7
|
+
def test_clear
|
8
|
+
Rubigraph.clear
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_vertex
|
12
|
+
v = Rubigraph::Vertex.new
|
13
|
+
v.set_attribute('size', '2.0')
|
14
|
+
v.remove
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_edge
|
18
|
+
v1 = Rubigraph::Vertex.new
|
19
|
+
v2 = Rubigraph::Vertex.new
|
20
|
+
e = Rubigraph::Edge.new(v1, v2)
|
21
|
+
e.label = 'edge'
|
22
|
+
e.remove
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_many
|
26
|
+
vs = []
|
27
|
+
100.times do |i|
|
28
|
+
v = Rubigraph::Vertex.new
|
29
|
+
v.color = sprintf("#%02d%02d%02d", i, i, i)
|
30
|
+
vs.push v
|
31
|
+
end
|
32
|
+
|
33
|
+
100.times do |i|
|
34
|
+
e = Rubigraph::Edge.new(vs[i], vs[(i+1)%100])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubigraph
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mootoh
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-06-04 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|