red_grape 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,11 +26,10 @@ RedGrape is an in-memory graph database written in ruby. I made this in order to
26
26
  ## REPL:
27
27
 
28
28
  $ bin/redgrape
29
- T
30
- ooooo
31
- ----- ooo -----
32
- RED o GRAPE
33
- -----------------
29
+ T
30
+ oOOOo
31
+ oOo
32
+ -------- O --------
34
33
  ruby :001 > g = RedGrape.create_tinker_graph
35
34
  => redgrape[vertices:6 edges:6]
36
35
  ruby :002 > g.class
@@ -69,6 +68,11 @@ In REPL, the `take' method which invokes all pipes is automatically called.
69
68
  ### SERVER:
70
69
 
71
70
  $ ./bin/trellis
71
+ +=================+
72
+ | + T + |
73
+ | oOo oOOOo oOo |
74
+ | 8 oOo 8 |
75
+ | O |
72
76
  Start server: druby://localhost:28282
73
77
  [Ctrl+C to stop]
74
78
 
@@ -76,10 +80,9 @@ In REPL, the `take' method which invokes all pipes is automatically called.
76
80
 
77
81
  $ ./bin/redgrape
78
82
  T
79
- ooooo
80
- ------ ooo ------
81
- RED o GRAPE
82
- -------------------
83
+ oOOOo
84
+ oOo
85
+ -------- O --------
83
86
  ruby-1.9.3-head :001 > store = RedGrape::GraphStore.open
84
87
  => #<RedGrape::GraphStore:0x007fb615137a90>
85
88
  ruby-1.9.3-head :002 > store.graphs
data/bin/redgrape CHANGED
@@ -1,80 +1,30 @@
1
1
  #!/usr/bin/env ruby
2
2
  $: << File.expand_path('../../lib', __FILE__)
3
3
 
4
- require 'drb/drb'
5
- require 'irb'
6
- require 'irb/completion'
7
4
  require 'optparse'
8
- require 'red_grape'
9
5
  require 'red_grape/graph_store'
6
+ require 'red_grape/irg'
7
+ require 'red_grape/version'
10
8
 
11
- RedGrape.set_auto_take
12
- module RedGrape
13
- module Help
14
- module_function
15
-
16
- def redgrape?(key=nil)
17
- case key
18
- when NilClass
19
- puts help_message
20
- :OK
21
- when 'pipe', 'pipes', :pipe, :pieps
22
- puts pipe_help_message
23
- :OK
24
- else
25
- puts '?'
26
- :NG
27
- end
28
- end
29
-
30
- def help_message
31
- <<-EOS
32
- Subcommands:
33
- redgrape? :pipe : list all pipes.
34
- EOS
35
- #redgrape? '[pipe name]' : describe the given pipe.
36
- end
37
-
38
- def pipe_help_message
39
- pipes = RedGrape::Pipe.constants.map(&:to_s).select{|p|
40
- p =~ /.*Pipe$/}.map{|p| underscore p.sub(/Pipe$/, '')}
41
- "Available pipes:\n #{pipes.sort.join ', '}"
42
- end
43
-
44
- def underscore(str)
45
- str.sub(/^[A-Z]/){|p| p.downcase}.gsub(/([a-z])([A-Z])/){"#{$1}_#{$2.downcase}"}
46
- end
47
-
48
- def camelcase(str)
49
- str.sub(/^[a-z]/){|p| p.upcase}.gsub(/_([a-z])/){"#{$1.upcase}"}
50
- end
51
- end
52
- end
53
-
54
- def redgrape?(key=nil)
55
- RedGrape::Help.redgrape? key
56
- end
57
-
58
- alias rg? redgrape?
59
-
9
+ # interprete options
10
+ ::Version = RedGrape::VERSION
60
11
  COMMAND_LINE = "#{$0} #{ARGV.join(' ')}"
61
- OPT = {}
12
+ OPT = {
13
+ port:RedGrape::GraphStore::DEFAULT_PORT
14
+ }
62
15
  opts = OptionParser.new
63
- #opts.on('-a <abc>'){|v| OPT[:abc] = v}
64
- #opts.on('-x', '--xyz <xyz>'){|v| OPT[:xyz] = v}
16
+ opts.on('-p', '--port <port number>',
17
+ "Set the port number for the connected datastore (default: #{OPT[:port]})"){|v| OPT[:port] = v.to_i}
65
18
  opts.on_tail('-v', '--version', 'Show version.'){puts(opts.ver); exit}
66
19
  opts.on_tail('-h', '--help', 'Show this message.'){puts(opts.help); exit}
67
-
68
- ::Version = RedGrape::VERSION
69
20
  opts.order! ARGV
70
- CMD = ARGV.shift
71
21
 
72
- $store = RedGrape::GraphStore.open
73
-
74
- puts %Q{ T
75
- o000o
76
- o0o
77
- -------- 0 --oo-o--
78
- }
79
-
80
- IRB.start
22
+ # start
23
+ RedGrape::IRG.start(OPT[:port]) do
24
+ puts <<-EOS
25
+ T
26
+ oOOOo
27
+ oOo
28
+ -------- O --------
29
+ EOS
30
+ end
data/bin/trellis CHANGED
@@ -5,23 +5,31 @@ require 'optparse'
5
5
  require 'red_grape/graph_store'
6
6
  require 'red_grape/version'
7
7
 
8
- DEFAULT_DBFILE = 'dbfile.bin'
9
-
8
+ # interprete options
10
9
  ::Version = RedGrape::VERSION
11
10
  COMMAND_LINE = "#{$0} #{ARGV.join(' ')}"
12
- OPT = {}
11
+ OPT = {
12
+ port:RedGrape::GraphStore::DEFAULT_PORT,
13
+ dbfile:'dbfile.bin'
14
+ }
13
15
  opts = OptionParser.new
14
16
  opts.on('-p', '--port <port number>',
15
- "Set the port number (default: #{RedGrape::GraphStore::DEFAULT_PORT})"){|v| OPT[:port] = v}
17
+ "Set the port number (default: #{OPT[:port]})"){|v| OPT[:port] = v.to_i}
16
18
  opts.on('-d', '--file <datafile>',
17
- "Set the name of a file to store data (default: #{DEFAULT_DBFILE})"){|v| OPT[:dbfile] = v}
19
+ "Set the filename to store data (default: #{OPT[:dbfile]})"){|v| OPT[:dbfile] = v}
18
20
  opts.on_tail('-v', '--version', 'Show version'){puts(opts.ver); exit}
19
21
  opts.on_tail('-h', '--help', 'Show this message'){puts(opts.help); exit}
20
22
  opts.order! ARGV
21
23
 
24
+ # start
22
25
  Signal.trap :INT, 'EXIT'
23
- RedGrape::GraphStore.start(:default, DEFAULT_DBFILE) do
26
+ RedGrape::GraphStore.start(OPT[:port], OPT[:dbfile]) do
24
27
  puts <<-EOS
28
+ +=================+
29
+ | + T + |
30
+ | oOo oOOOo oOo |
31
+ | 8 oOo 8 |
32
+ | O |
25
33
  Start server: #{DRb.uri}
26
34
  [Ctrl+C to stop]
27
35
  EOS
@@ -27,16 +27,18 @@ module RedGrape
27
27
  end
28
28
  end
29
29
 
30
- # set property value with type checking
30
+ # set property value with type checking if its definition exists
31
31
  def set_property(kid, v)
32
- # TODO: should be refactored
33
- desc = @property_description[kid]
34
- if desc.accessible? v
35
- self[desc.name] = v
36
- elsif desc.convertable? v
37
- self[desc.name] = desc.convert v
32
+ if @property_description && desc = @property_description[kid]
33
+ if desc.accessible? v
34
+ self[desc.name] = v
35
+ elsif desc.convertable? v
36
+ self[desc.name] = desc.convert v
37
+ else
38
+ raise ArgumentError.new "#{kid} should be #{desc.type}."
39
+ end
38
40
  else
39
- raise ArgumentError.new "#{kid} should be #{desc.type}."
41
+ self[kid] = v
40
42
  end
41
43
  end
42
44
 
@@ -120,6 +120,7 @@ module RedGrape
120
120
  def add_vertex(id, v=nil)
121
121
  if v
122
122
  if v.is_a? Hash
123
+ id = id.to_s
123
124
  v = Vertex.new self, id, v
124
125
  end
125
126
  else
@@ -128,14 +129,15 @@ module RedGrape
128
129
  id = (v[:id] || v['id']).to_s
129
130
  elsif id.respond_to?(:id)
130
131
  v = id
131
- id = v.id
132
+ id = v.id.to_s
132
133
  else
134
+ id = id.to_s
133
135
  v = {}
134
136
  end
135
137
  v = Vertex.new self, id, v
136
138
  end
137
- id = id.to_s
138
139
  raise ArgumentError.new "invalid id" unless id == v.id
140
+ raise ArgumentError.new "#{id} already exists." if @vertices[id]
139
141
 
140
142
  @vertices[id] = v
141
143
  end
@@ -155,17 +157,19 @@ module RedGrape
155
157
  end
156
158
 
157
159
  def add_edge(id, from, to, label, opts={})
158
- edge = if id.is_a? Edge
159
- id
160
- else
161
- id = id.to_s
162
- from = self.vertex(from.to_s) unless from.is_a? Vertex
163
- to = self.vertex(to.to_s) unless to.is_a? Vertex
164
- add_vertex from unless self.vertex(from.id)
165
- add_vertex to unless self.vertex(to.id)
166
- Edge.new self, id, from, to, label, opts
167
- end
168
- @edges[edge.id] = edge
160
+ if id.is_a? Edge
161
+ raise ArgumentError.new "#{id.id} already exists." if @edges[id.id]
162
+ @edges[id.id] = id
163
+ else
164
+ id = id.to_s
165
+ raise ArgumentError.new "#{id} already exists." if @edges[id]
166
+
167
+ from = self.vertex(from.to_s) unless from.is_a? Vertex
168
+ to = self.vertex(to.to_s) unless to.is_a? Vertex
169
+ add_vertex from unless self.vertex(from.id)
170
+ add_vertex to unless self.vertex(to.id)
171
+ @edges[id] = Edge.new self, id, from, to, label, opts
172
+ end
169
173
  end
170
174
 
171
175
  def remove_edge(id)
@@ -4,15 +4,18 @@ require 'red_grape'
4
4
  module RedGrape
5
5
  class GraphStore
6
6
  DEFAULT_PORT = 28282
7
- DEFAULT_URI = "druby://localhost:#{DEFAULT_PORT}"
8
7
 
9
8
  class << self
10
- def start(uri=nil, filename=nil, &block)
11
- self.new(filename).start uri, &block
9
+ def start(port=nil, filename=nil, &block)
10
+ self.new(filename).start port, &block
12
11
  end
13
12
 
14
- def open(uri=nil)
15
- DRbObject.new_with_uri(uri || DEFAULT_URI)
13
+ def uri(port=nil)
14
+ "druby://localhost:#{port || DEFAULT_PORT}"
15
+ end
16
+
17
+ def open(port=nil)
18
+ DRbObject.new_with_uri(uri port)
16
19
  end
17
20
  end
18
21
 
@@ -48,22 +51,14 @@ module RedGrape
48
51
  @graphs[key.to_sym] = graph
49
52
  end
50
53
 
51
- def start(uri=nil, &block)
54
+ def start(port=nil, &block)
52
55
  at_exit do
53
56
  File.open @filename, 'w' do |file|
54
57
  Marshal.dump @graphs, file
55
58
  end if @filename
56
59
  end
57
60
 
58
- uri = case uri
59
- when NilClass, :default
60
- DEFAULT_URI
61
- when Integer
62
- "druby://localhost:#{uri}"
63
- else
64
- uri
65
- end
66
- DRb.start_service uri, self
61
+ DRb.start_service self.class.uri(port), self
67
62
  block.call if block
68
63
  sleep
69
64
  end
@@ -0,0 +1,68 @@
1
+ require 'drb/drb'
2
+ require 'irb'
3
+ require 'irb/completion'
4
+ require 'red_grape'
5
+ require 'red_grape/graph_store'
6
+
7
+ module RedGrape
8
+ class IRG
9
+ module Help
10
+ module_function
11
+ def redgrape?(key=nil)
12
+ case key
13
+ when NilClass
14
+ puts help_message
15
+ :OK
16
+ when 'pipe', 'pipes', :pipe, :pieps
17
+ puts pipe_help_message
18
+ :OK
19
+ else
20
+ puts '?'
21
+ :NG
22
+ end
23
+ end
24
+
25
+ def help_message
26
+ <<-EOS
27
+ Subcommands:
28
+ redgrape? :pipe : list all pipes.
29
+ EOS
30
+ #redgrape? '[pipe name]' : describe the given pipe.
31
+ end
32
+
33
+ def pipe_help_message
34
+ pipes = RedGrape::Pipe.constants.map(&:to_s).select{|p|
35
+ p =~ /.*Pipe$/}.map{|p| underscore p.sub(/Pipe$/, '')}
36
+ "Available pipes:\n #{pipes.sort.join ', '}"
37
+ end
38
+
39
+ def underscore(str)
40
+ str.sub(/^[A-Z]/){|p| p.downcase}.gsub(/([a-z])([A-Z])/){"#{$1}_#{$2.downcase}"}
41
+ end
42
+
43
+ def camelcase(str)
44
+ str.sub(/^[a-z]/){|p| p.upcase}.gsub(/_([a-z])/){"#{$1.upcase}"}
45
+ end
46
+ end
47
+
48
+ class << self
49
+ def start(port=nil, &block)
50
+ self.new.start port, &block
51
+ end
52
+ end
53
+
54
+ def start(port=nil, &block)
55
+ Kernel.module_eval %Q{
56
+ def redgrape?(key=nil)
57
+ RedGrape::IRG::Help.redgrape? key
58
+ end
59
+ alias rg? redgrape?
60
+ }
61
+
62
+ RedGrape.set_auto_take
63
+ $store = RedGrape::GraphStore.open port if port
64
+ block.call if block
65
+ IRB.start
66
+ end
67
+ end
68
+ end
@@ -1,3 +1,3 @@
1
1
  module RedGrape
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/test/all.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'test/unit'
2
+ Dir["#{File.dirname(__FILE__)}/test_*.rb"].each do |name|
3
+ require name
4
+ end
data/test/test_graph.rb CHANGED
@@ -7,6 +7,9 @@ class GraphTest < Test::Unit::TestCase
7
7
  graph.add_vertex id:1, val:'a'
8
8
  graph.add_vertex id:2, val:'b'
9
9
  graph.add_vertex id:3, val:'c'
10
+ assert_raise(ArgumentError) do
11
+ graph.add_vertex id:3, val:'d'
12
+ end
10
13
 
11
14
  assert_equal 3, graph.vertex.size
12
15
  assert_equal '1', graph.vertex(1).id
@@ -104,7 +107,7 @@ class GraphTest < Test::Unit::TestCase
104
107
  v2 = g.add_vertex 2
105
108
  v3 = g.add_vertex 3
106
109
  e12 = g.add_edge 1, 1, 2, :connect
107
- e23 = g.add_edge 1, 2, 3, :connect
110
+ e23 = g.add_edge 2, 2, 3, :connect
108
111
  assert v1.out_edges.map(&:id).include?(e12.id)
109
112
  assert v2.in_edges.map(&:id).include?(e12.id)
110
113
  assert v2.out_edges.map(&:id).include?(e23.id)
@@ -128,7 +131,7 @@ class GraphTest < Test::Unit::TestCase
128
131
  v2 = g.add_vertex 2
129
132
  v3 = g.add_vertex 3
130
133
  e12 = g.add_edge 1, 1, 2, :connect
131
- e23 = g.add_edge 1, 2, 3, :connect
134
+ e23 = g.add_edge 2, 2, 3, :connect
132
135
  assert v1.out_edges.map(&:id).include?(e12.id)
133
136
  assert v2.in_edges.map(&:id).include?(e12.id)
134
137
  assert v2.out_edges.map(&:id).include?(e23.id)
@@ -141,7 +144,7 @@ class GraphTest < Test::Unit::TestCase
141
144
  v2 = g.add_vertex 2
142
145
  v3 = g.add_vertex 3
143
146
  e12 = g.add_edge 1, 1, 2, :connect
144
- e23 = g.add_edge 1, 2, 3, :connect
147
+ e23 = g.add_edge 2, 2, 3, :connect
145
148
  assert v1.out_edges.map(&:id).include?(e12.id)
146
149
  assert v2.in_edges.map(&:id).include?(e12.id)
147
150
  assert v2.out_edges.map(&:id).include?(e23.id)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: red_grape
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-07 00:00:00.000000000 Z
12
+ date: 2012-06-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &70245490551240 !ruby/object:Gem::Requirement
16
+ requirement: &70107699251780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70245490551240
24
+ version_requirements: *70107699251780
25
25
  description: RedGrape is an in-memory graph database written in ruby. I made this
26
26
  in order to learn how graph databases work so that please do not use this for any
27
27
  serious purpose.
@@ -36,10 +36,8 @@ files:
36
36
  - .autotest
37
37
  - .gitignore
38
38
  - Gemfile
39
- - History.txt
40
39
  - README.md
41
40
  - Rakefile
42
- - TODO.txt
43
41
  - bin/redgrape
44
42
  - bin/trellis
45
43
  - data/graph-example-1.xml
@@ -52,6 +50,7 @@ files:
52
50
  - lib/red_grape/element.rb
53
51
  - lib/red_grape/graph.rb
54
52
  - lib/red_grape/graph_store.rb
53
+ - lib/red_grape/irg.rb
55
54
  - lib/red_grape/path_group.rb
56
55
  - lib/red_grape/pipe/aggregate_pipe.rb
57
56
  - lib/red_grape/pipe/as_pipe.rb
@@ -80,6 +79,7 @@ files:
80
79
  - lib/red_grape/version.rb
81
80
  - lib/red_grape/vertex.rb
82
81
  - red_grape.gemspec
82
+ - test/all.rb
83
83
  - test/test_element.rb
84
84
  - test/test_graph.rb
85
85
  - test/test_on_the_nature_of_pipes.rb
@@ -112,6 +112,7 @@ signing_key:
112
112
  specification_version: 3
113
113
  summary: RedGrape - Extremely Simple GraphDB
114
114
  test_files:
115
+ - test/all.rb
115
116
  - test/test_element.rb
116
117
  - test/test_graph.rb
117
118
  - test/test_on_the_nature_of_pipes.rb
data/History.txt DELETED
@@ -1,6 +0,0 @@
1
- === 1.0.0 / 2012-05-16
2
-
3
- * 1 major enhancement
4
-
5
- * Birthday!
6
-
data/TODO.txt DELETED
@@ -1,23 +0,0 @@
1
- TODO
2
- * serialize a graph
3
- * implement tinkerpop methods
4
- * use symbols for keys
5
- * customize irb prompt
6
-
7
- DONE
8
- * show features
9
- * marshal
10
- * readonly graph
11
- * use dRuby for persistence
12
- * cut a GraphML reader function from Graph class
13
-
14
- MEMO
15
- #IRB.init_config 'redgrape'
16
- #IRB.conf[:PROMPT][:RED_GRAPE] = {
17
- # :PROMPT_I => "abc%N(%m):%03n:%i> ",
18
- # :PROMPT_S => "abc%N(%m):%03n:%i%l ",
19
- # :PROMPT_C => "abc%N(%m):%03n:%i* ",
20
- # :RETURN => "%s\n"
21
- #}
22
- #IRB.conf[:PROMPT_MODE] = :RED_GRAPE
23
-