Skip to content

Commit 44da33a

Browse files
committed
fix a bug
1 parent dd6ead2 commit 44da33a

File tree

7 files changed

+71
-26
lines changed

7 files changed

+71
-26
lines changed

lib/red_grape.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
module RedGrape
2+
VERSION = '0.0.10'
3+
end
4+
15
require 'ext/nil_class_ext'
26
require 'ext/object_ext'
37
require 'ext/array_ext'
4-
require 'red_grape/version'
58
require 'red_grape/pipe/aggregate_pipe'
69
require 'red_grape/pipe/as_pipe'
710
require 'red_grape/pipe/back_pipe'
@@ -11,8 +14,10 @@
1114
require 'red_grape/pipe/exhaust_merge_pipe'
1215
require 'red_grape/pipe/fill_pipe'
1316
require 'red_grape/pipe/filter_pipe'
17+
require 'red_grape/pipe/group_by_pipe'
1418
require 'red_grape/pipe/group_count_pipe'
1519
require 'red_grape/pipe/has_pipe'
20+
require 'red_grape/pipe/has_not_pipe'
1621
require 'red_grape/pipe/if_then_else_pipe'
1722
require 'red_grape/pipe/in_pipe'
1823
require 'red_grape/pipe/in_e_pipe'

lib/red_grape/pipe/base.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def self.set_auto_take(val=true)
2323
end
2424

2525
class Base
26-
attr_accessor :opts, :prev, :next, :value
26+
attr_accessor :opts, :prev, :next, :value, :it
2727

2828
def initialize(prev, *opts, &block)
2929
@prev = prev
@@ -56,7 +56,6 @@ def done?
5656

5757
def take(context=Context.new)
5858
if first?
59-
#context = Context.new
6059
val = @prev.pass_through self, context
6160
if context.aggregating?
6261
context.resume_from_aggregating
@@ -87,6 +86,11 @@ def pass_next(context, pushed_obj, next_obj=nil, &block)
8786
end
8887
end
8988

89+
def pipe_eval(params={}, &block)
90+
params.each {|k, v| self.send "#{k}=", v}
91+
instance_eval(&block)
92+
end
93+
9094
def to_s
9195
Pipe.auto_take ? take.to_s : super
9296
end

lib/red_grape/pipe/context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def grouping?
7676
end
7777

7878
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.
8080
if pipe
8181
push_history obj do |ctx|
8282
obj.pass_through(pipe, ctx)

lib/red_grape/pipe/group_count_pipe.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ module Pipe
55
class GroupCountPipe < Pipe::Base
66
def pass(obj, context)
77
if self.opts.empty?
8+
#context.group({obj => 1}, self.next)
89
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
913
else
1014
self.opts.first[obj] ||= 0
1115
self.opts.first[obj] += 1

lib/red_grape/tools/irg.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def start(port=nil, &block)
5555
def start(port=nil, &block)
5656
Kernel.module_eval %Q{
5757
def redgrape?(key=nil)
58-
RedGrape::IRG::Help.redgrape? key
58+
RedGrape::Tools::IRG::Help.redgrape? key
5959
end
6060
alias rg? redgrape?
6161
}

red_grape.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- encoding: utf-8 -*-
22
$:.push File.expand_path("../lib", __FILE__)
3-
require "red_grape/version"
3+
#require "red_grape/version"
4+
require "red_grape"
45

56
Gem::Specification.new do |s|
67
s.name = "red_grape"

test/test_traversal_patterns.rb

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33

44
class TraversalPatternsTest < Test::Unit::TestCase
55
def setup
6-
@graph = RedGrape.load_graph 'data/graph-example-1.xml'
6+
@g1 = RedGrape.load_graph 'data/graph-example-1.xml'
77
end
88

99
# https://github.com/tinkerpop/gremlin/wiki/Backtrack-Pattern
1010
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
1313
end
1414

1515
# https://github.com/tinkerpop/gremlin/wiki/Except-Retain-Pattern
1616
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
2121
end
2222

2323
# https://github.com/tinkerpop/gremlin/wiki/Flow-Rank-Pattern
2424
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
2626
software = []
27-
@graph.V('lang', 'java').fill(software).take
27+
@g1.V('lang', 'java').fill(software).take
2828
assert_equal %w(3 5), software.map(&:id).sort
2929
assert_equal %w(marko josh peter josh), software._.in('created').name.take
3030
assert_equal(
@@ -39,7 +39,7 @@ def test_flow_rank_pattern
3939

4040
# https://github.com/tinkerpop/gremlin/wiki/Path-Pattern
4141
def test_path_pattern
42-
v1 = @graph.vertex(1)
42+
v1 = @g1.vertex(1)
4343

4444
assert_equal %w(josh lop vadas), v1.out.name.take.sort
4545

@@ -58,9 +58,9 @@ def test_path_pattern
5858
end
5959

6060
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
6464

6565
path = v89.outE.inV.loop(2){it.loops < 3}.path.take.first
6666
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,46 @@ def test_loop_pattern
8686
assert_equal RedGrape::Edge, path[3].class
8787
assert_equal RedGrape::Vertex, path[4].class
8888

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
92103
end
93104

94105
# 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+
assert_equal(
127+
{},
128+
@g2.V.has_not('song_type', nil).group_by(proc{it.song_type}, proc{it}).cap.take
129+
)
99130
end
100131
end

0 commit comments

Comments
 (0)