red_grape 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ext/array_ext.rb CHANGED
@@ -3,8 +3,7 @@ class Array
3
3
  loops = context.loops
4
4
  map! do |e|
5
5
  context.loops = loops
6
- #pipe.pass e._, context # TODO
7
- pipe.pass e, context # TODO
6
+ pipe.pass e, context
8
7
  end
9
8
  context.loops = loops
10
9
  normalize_for_graph
@@ -23,7 +23,7 @@ module RedGrape
23
23
  end
24
24
 
25
25
  class Base
26
- attr_accessor :opts, :prev, :next, :value
26
+ attr_accessor :opts, :prev, :next, :value, :it
27
27
 
28
28
  def initialize(prev, *opts, &block)
29
29
  @prev = prev
@@ -56,7 +56,6 @@ module RedGrape
56
56
 
57
57
  def take(context=Context.new)
58
58
  if first?
59
- #context = Context.new
60
59
  val = @prev.pass_through self, context
61
60
  if context.aggregating?
62
61
  context.resume_from_aggregating
@@ -87,6 +86,11 @@ module RedGrape
87
86
  end
88
87
  end
89
88
 
89
+ def pipe_eval(params={}, &block)
90
+ params.each {|k, v| self.send "#{k}=", v}
91
+ instance_eval(&block)
92
+ end
93
+
90
94
  def to_s
91
95
  Pipe.auto_take ? take.to_s : super
92
96
  end
@@ -76,7 +76,7 @@ module RedGrape
76
76
  end
77
77
 
78
78
  def resume_from_grouping
79
- obj, pipe = *@grouping_items.first # TODO: is '.first' ok?
79
+ obj, pipe = *@grouping_items.first # TODO: '.first' is used to get next_pipe.
80
80
  if pipe
81
81
  push_history obj do |ctx|
82
82
  obj.pass_through(pipe, ctx)
@@ -9,7 +9,7 @@ module RedGrape
9
9
  end
10
10
 
11
11
  def pass(obj, context)
12
- #pipelines = self.opts
12
+ #pipelines = self.opts.map do |pipeline|
13
13
  pipelines = @opts2.map do |pipeline| # TODO: opts2??
14
14
  pipe = pipeline.first_pipe
15
15
  pipe.prev = obj
@@ -5,7 +5,11 @@ module RedGrape
5
5
  class GroupCountPipe < Pipe::Base
6
6
  def pass(obj, context)
7
7
  if self.opts.empty?
8
+ #context.group({obj => 1}, self.next)
8
9
  context.group obj, self.next
10
+ elsif self.opts.first.is_a? Proc
11
+ #context.group({pipe_eval(it:obj, &self.opts.first) => 1}, self.next)
12
+ context.group pipe_eval(it:obj, &self.opts.first), self.next
9
13
  else
10
14
  self.opts.first[obj] ||= 0
11
15
  self.opts.first[obj] += 1
@@ -55,7 +55,7 @@ Subcommands:
55
55
  def start(port=nil, &block)
56
56
  Kernel.module_eval %Q{
57
57
  def redgrape?(key=nil)
58
- RedGrape::IRG::Help.redgrape? key
58
+ RedGrape::Tools::IRG::Help.redgrape? key
59
59
  end
60
60
  alias rg? redgrape?
61
61
  }
data/lib/red_grape.rb CHANGED
@@ -11,8 +11,10 @@ require 'red_grape/pipe/except_pipe'
11
11
  require 'red_grape/pipe/exhaust_merge_pipe'
12
12
  require 'red_grape/pipe/fill_pipe'
13
13
  require 'red_grape/pipe/filter_pipe'
14
+ require 'red_grape/pipe/group_by_pipe'
14
15
  require 'red_grape/pipe/group_count_pipe'
15
16
  require 'red_grape/pipe/has_pipe'
17
+ require 'red_grape/pipe/has_not_pipe'
16
18
  require 'red_grape/pipe/if_then_else_pipe'
17
19
  require 'red_grape/pipe/in_pipe'
18
20
  require 'red_grape/pipe/in_e_pipe'
@@ -31,6 +33,8 @@ require 'red_grape/pipe/e_pipe'
31
33
  require 'red_grape/graph'
32
34
 
33
35
  module RedGrape
36
+ VERSION = "0.0.9"
37
+
34
38
  module_function
35
39
  def set_auto_take(val=true)
36
40
  Pipe.set_auto_take val
data/red_grape.gemspec CHANGED
@@ -1,6 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "red_grape/version"
3
+ #require "red_grape/version"
4
+ require "red_grape"
4
5
 
5
6
  Gem::Specification.new do |s|
6
7
  s.name = "red_grape"
@@ -3,28 +3,28 @@ require 'red_grape'
3
3
 
4
4
  class TraversalPatternsTest < Test::Unit::TestCase
5
5
  def setup
6
- @graph = RedGrape.load_graph 'data/graph-example-1.xml'
6
+ @g1 = RedGrape.load_graph 'data/graph-example-1.xml'
7
7
  end
8
8
 
9
9
  # https://github.com/tinkerpop/gremlin/wiki/Backtrack-Pattern
10
10
  def test_backtrack_pattern
11
- assert_equal [29], @graph.V.out('knows').has('age', :gt, 30).back(2).age.take
12
- assert_equal [29], @graph.V.as('x').outE('knows').inV.has('age', :gt, 30).back('x').age.take
11
+ assert_equal [29], @g1.V.out('knows').has('age', :gt, 30).back(2).age.take
12
+ assert_equal [29], @g1.V.as('x').outE('knows').inV.has('age', :gt, 30).back('x').age.take
13
13
  end
14
14
 
15
15
  # https://github.com/tinkerpop/gremlin/wiki/Except-Retain-Pattern
16
16
  def test_except_retain_pattern
17
- assert_equal %w(2 3 4), @graph.v(1).out.take.map(&:id).sort
18
- assert_equal %w(3 5), @graph.v(1).out.out.take.map(&:id).sort
19
- assert_equal %w(5), @graph.v(1).out.aggregate(:x).out.except(:x).take.map(&:id).sort
20
- assert_equal %w(3), @graph.v(1).out.aggregate(:x).out.retain(:x).take.map(&:id).sort
17
+ assert_equal %w(2 3 4), @g1.v(1).out.take.map(&:id).sort
18
+ assert_equal %w(3 5), @g1.v(1).out.out.take.map(&:id).sort
19
+ assert_equal %w(5), @g1.v(1).out.aggregate(:x).out.except(:x).take.map(&:id).sort
20
+ assert_equal %w(3), @g1.v(1).out.aggregate(:x).out.retain(:x).take.map(&:id).sort
21
21
  end
22
22
 
23
23
  # https://github.com/tinkerpop/gremlin/wiki/Flow-Rank-Pattern
24
24
  def test_flow_rank_pattern
25
- assert_equal %w(3 5), @graph.V('lang', 'java').take.map(&:id).sort
25
+ assert_equal %w(3 5), @g1.V('lang', 'java').take.map(&:id).sort
26
26
  software = []
27
- @graph.V('lang', 'java').fill(software).take
27
+ @g1.V('lang', 'java').fill(software).take
28
28
  assert_equal %w(3 5), software.map(&:id).sort
29
29
  assert_equal %w(marko josh peter josh), software._.in('created').name.take
30
30
  assert_equal(
@@ -39,7 +39,7 @@ class TraversalPatternsTest < Test::Unit::TestCase
39
39
 
40
40
  # https://github.com/tinkerpop/gremlin/wiki/Path-Pattern
41
41
  def test_path_pattern
42
- v1 = @graph.vertex(1)
42
+ v1 = @g1.vertex(1)
43
43
 
44
44
  assert_equal %w(josh lop vadas), v1.out.name.take.sort
45
45
 
@@ -58,9 +58,9 @@ class TraversalPatternsTest < Test::Unit::TestCase
58
58
  end
59
59
 
60
60
  def test_loop_pattern
61
- g = RedGrape.load_graph 'data/graph-example-2.xml'
62
- v89 = g.vertex 89
63
- assert_equal 36, g.v(89).outE.inV.path.take.size
61
+ @g2 ||= RedGrape.load_graph 'data/graph-example-2.xml'
62
+ v89 = @g2.vertex 89
63
+ assert_equal 36, v89.outE.inV.path.take.size
64
64
 
65
65
  path = v89.outE.inV.loop(2){it.loops < 3}.path.take.first
66
66
  assert_equal '[v[89], e[7006][89-followed_by->127], v[127], e[7786][127-sung_by->340], v[340]]', path.to_s
@@ -86,15 +86,48 @@ class TraversalPatternsTest < Test::Unit::TestCase
86
86
  assert_equal RedGrape::Edge, path[3].class
87
87
  assert_equal RedGrape::Vertex, path[4].class
88
88
 
89
- #assert_equal 70307, v89.out.loop(1, proc{it.loops < 4}).take.size #=> OK
90
- #assert_equal 71972, v89.out.loop(1, proc{it.loops < 4}, proc{true}).take.size #=> 70307
91
- #assert_equal 582, v89.out.loop(1, proc{it.loops < 4}, proc{it.object.id == '89'}).take.size #=> 564
89
+ #assert_equal(
90
+ # 70307,
91
+ # v89.out.loop(1, proc{it.loops < 4}).take.size
92
+ #) #=> OK
93
+
94
+ #assert_equal(
95
+ # 71972,
96
+ # v89.out.loop(1, proc{it.loops < 4}, proc{true}).take.size
97
+ #) #=> 70307
98
+
99
+ #assert_equal(
100
+ # 582,
101
+ # v89.out.loop(1, proc{it.loops < 4}, proc{it.object.id == '89'}).take.size
102
+ #) #=> 564
92
103
  end
93
104
 
94
105
  # https://github.com/tinkerpop/gremlin/wiki/Split-Merge-Pattern
95
- def test_split_marge
96
- v1 = @graph.vertex 1
97
- #assert_equal ['ripple', 27, 'lop', 32], v1.out('knows').copy_split(_.out('created').name, _.age).fair_merge.take # TODO: not yet
98
- assert_equal [27, 'ripple', 'lop', 32], v1.out('knows').copy_split(_.out('created').name, _.age).exhaust_merge.take
106
+ def test_split_marge_pattern
107
+ v1 = @g1.vertex 1
108
+
109
+ #assert_equal(
110
+ # ['ripple', 27, 'lop', 32],
111
+ # v1.out('knows').copy_split(_.out('created').name, _.age).fair_merge.take
112
+ #) # TODO: not yet
113
+
114
+ assert_equal(
115
+ [27, 'ripple', 'lop', 32],
116
+ v1.out('knows').copy_split(_.out('created').name, _.age).exhaust_merge.take
117
+ )
118
+ end
119
+
120
+ def test_map_reduce_pattern
121
+ @g2 ||= RedGrape.load_graph 'data/graph-example-2.xml'
122
+ assert_equal(
123
+ {'cover'=>313, 'original'=>184},
124
+ @g2.V.has_not('song_type', nil).group_count(proc{it.song_type}).cap.take
125
+ )
126
+ =begin
127
+ assert_equal(
128
+ {},
129
+ @g2.V.has_not('song_type', nil).group_by(proc{it.song_type}, proc{it}).cap.take
130
+ )
131
+ =end
99
132
  end
100
133
  end
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.8
4
+ version: 0.0.9
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-14 00:00:00.000000000 Z
12
+ date: 2012-06-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &70196102597160 !ruby/object:Gem::Requirement
16
+ requirement: &70205221723480 !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: *70196102597160
24
+ version_requirements: *70205221723480
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.
@@ -33,7 +33,6 @@ executables:
33
33
  extensions: []
34
34
  extra_rdoc_files: []
35
35
  files:
36
- - .autotest
37
36
  - .gitignore
38
37
  - Gemfile
39
38
  - README.md
data/.autotest DELETED
@@ -1,23 +0,0 @@
1
- # -*- ruby -*-
2
-
3
- require 'autotest/restart'
4
-
5
- # Autotest.add_hook :initialize do |at|
6
- # at.extra_files << "../some/external/dependency.rb"
7
- #
8
- # at.libs << ":../some/external"
9
- #
10
- # at.add_exception 'vendor'
11
- #
12
- # at.add_mapping(/dependency.rb/) do |f, _|
13
- # at.files_matching(/test_.*rb$/)
14
- # end
15
- #
16
- # %w(TestA TestB).each do |klass|
17
- # at.extra_class_map[klass] = "test/test_misc.rb"
18
- # end
19
- # end
20
-
21
- # Autotest.add_hook :run_command do |at|
22
- # system "rake build"
23
- # end