neo4j 2.0.0.alpha.5-java → 2.0.0.alpha.6-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. data/CHANGELOG +12 -0
  2. data/Gemfile +0 -4
  3. data/README.rdoc +106 -62
  4. data/lib/neo4j.rb +7 -33
  5. data/lib/neo4j/performance.rb +43 -0
  6. data/lib/neo4j/rails/accept_id.rb +19 -18
  7. data/lib/neo4j/rails/attributes.rb +366 -120
  8. data/lib/neo4j/rails/finders.rb +41 -15
  9. data/lib/neo4j/rails/has_n.rb +203 -0
  10. data/lib/neo4j/rails/identity.rb +25 -0
  11. data/lib/neo4j/rails/model.rb +65 -242
  12. data/lib/neo4j/rails/nested_attributes.rb +108 -0
  13. data/lib/neo4j/rails/node_persistance.rb +56 -0
  14. data/lib/neo4j/rails/observer.rb +0 -2
  15. data/lib/neo4j/rails/persistence.rb +32 -154
  16. data/lib/neo4j/rails/rack_middleware.rb +26 -2
  17. data/lib/neo4j/rails/rails.rb +9 -6
  18. data/lib/neo4j/rails/railtie.rb +1 -2
  19. data/lib/neo4j/rails/relationship.rb +18 -125
  20. data/lib/neo4j/rails/relationship_persistence.rb +107 -0
  21. data/lib/neo4j/rails/relationships/node_dsl.rb +72 -44
  22. data/lib/neo4j/rails/relationships/relationships.rb +187 -59
  23. data/lib/neo4j/rails/relationships/rels_dsl.rb +18 -17
  24. data/lib/neo4j/rails/relationships/storage.rb +19 -17
  25. data/lib/neo4j/rails/timestamps.rb +53 -51
  26. data/lib/neo4j/rails/transaction.rb +9 -1
  27. data/lib/neo4j/rails/validations/uniqueness.rb +1 -1
  28. data/lib/neo4j/rails/versioning/versioning.rb +2 -2
  29. data/lib/neo4j/version.rb +1 -1
  30. data/lib/orm_adapter/adapters/neo4j.rb +47 -46
  31. data/neo4j.gemspec +1 -1
  32. metadata +10 -69
  33. data/lib/neo4j/algo/algo.rb +0 -294
  34. data/lib/neo4j/batch/batch.rb +0 -4
  35. data/lib/neo4j/batch/indexer.rb +0 -109
  36. data/lib/neo4j/batch/inserter.rb +0 -179
  37. data/lib/neo4j/batch/rule_inserter.rb +0 -24
  38. data/lib/neo4j/batch/rule_node.rb +0 -72
  39. data/lib/neo4j/config.rb +0 -177
  40. data/lib/neo4j/core_ext/class/inheritable_attributes.rb +0 -12
  41. data/lib/neo4j/core_ext/class/rewrite_inheritable_attributes.rb +0 -170
  42. data/lib/neo4j/database.rb +0 -158
  43. data/lib/neo4j/equal.rb +0 -21
  44. data/lib/neo4j/event_handler.rb +0 -263
  45. data/lib/neo4j/has_list/class_methods.rb +0 -11
  46. data/lib/neo4j/has_list/has_list.rb +0 -3
  47. data/lib/neo4j/has_list/mapping.rb +0 -133
  48. data/lib/neo4j/has_n/class_methods.rb +0 -119
  49. data/lib/neo4j/has_n/decl_relationship_dsl.rb +0 -246
  50. data/lib/neo4j/has_n/has_n.rb +0 -3
  51. data/lib/neo4j/has_n/mapping.rb +0 -98
  52. data/lib/neo4j/identity_map.rb +0 -140
  53. data/lib/neo4j/index/class_methods.rb +0 -108
  54. data/lib/neo4j/index/index.rb +0 -39
  55. data/lib/neo4j/index/indexer.rb +0 -341
  56. data/lib/neo4j/index/indexer_registry.rb +0 -68
  57. data/lib/neo4j/index/lucene_query.rb +0 -256
  58. data/lib/neo4j/load.rb +0 -25
  59. data/lib/neo4j/migrations/class_methods.rb +0 -110
  60. data/lib/neo4j/migrations/extensions.rb +0 -58
  61. data/lib/neo4j/migrations/lazy_node_mixin.rb +0 -41
  62. data/lib/neo4j/migrations/migration.rb +0 -112
  63. data/lib/neo4j/migrations/migrations.rb +0 -6
  64. data/lib/neo4j/migrations/node_mixin.rb +0 -80
  65. data/lib/neo4j/migrations/ref_node_wrapper.rb +0 -32
  66. data/lib/neo4j/model.rb +0 -4
  67. data/lib/neo4j/neo4j.rb +0 -216
  68. data/lib/neo4j/node.rb +0 -270
  69. data/lib/neo4j/node_mixin/class_methods.rb +0 -51
  70. data/lib/neo4j/node_mixin/node_mixin.rb +0 -141
  71. data/lib/neo4j/paginated.rb +0 -23
  72. data/lib/neo4j/property/class_methods.rb +0 -79
  73. data/lib/neo4j/property/property.rb +0 -111
  74. data/lib/neo4j/rails/mapping/property.rb +0 -183
  75. data/lib/neo4j/rails/rel_persistence.rb +0 -237
  76. data/lib/neo4j/relationship.rb +0 -239
  77. data/lib/neo4j/relationship_mixin/class_methods.rb +0 -36
  78. data/lib/neo4j/relationship_mixin/relationship_mixin.rb +0 -142
  79. data/lib/neo4j/relationship_set.rb +0 -58
  80. data/lib/neo4j/rels/rels.rb +0 -110
  81. data/lib/neo4j/rels/traverser.rb +0 -102
  82. data/lib/neo4j/rule/class_methods.rb +0 -201
  83. data/lib/neo4j/rule/event_listener.rb +0 -66
  84. data/lib/neo4j/rule/functions/count.rb +0 -43
  85. data/lib/neo4j/rule/functions/function.rb +0 -74
  86. data/lib/neo4j/rule/functions/functions.rb +0 -3
  87. data/lib/neo4j/rule/functions/sum.rb +0 -29
  88. data/lib/neo4j/rule/rule.rb +0 -150
  89. data/lib/neo4j/rule/rule_node.rb +0 -217
  90. data/lib/neo4j/to_java.rb +0 -31
  91. data/lib/neo4j/transaction.rb +0 -73
  92. data/lib/neo4j/traversal/filter_predicate.rb +0 -25
  93. data/lib/neo4j/traversal/prune_evaluator.rb +0 -14
  94. data/lib/neo4j/traversal/rel_expander.rb +0 -31
  95. data/lib/neo4j/traversal/traversal.rb +0 -141
  96. data/lib/neo4j/traversal/traverser.rb +0 -284
  97. data/lib/neo4j/type_converters/type_converters.rb +0 -288
data/lib/neo4j/to_java.rb DELETED
@@ -1,31 +0,0 @@
1
- module Neo4j
2
- module ToJava
3
-
4
- def type_to_java(type)
5
- org.neo4j.graphdb.DynamicRelationshipType.withName(type.to_s)
6
- end
7
-
8
- def dir_from_java(dir)
9
- case dir
10
- when org.neo4j.graphdb.Direction::OUTGOING then :outgoing
11
- when org.neo4j.graphdb.Direction::BOTH then :both
12
- when org.neo4j.graphdb.Direction::INCOMING then :incoming
13
- else raise "unknown direction '#{dir} / #{dir.class}'"
14
- end
15
- end
16
-
17
- def dir_to_java(dir)
18
- case dir
19
- when :outgoing then org.neo4j.graphdb.Direction::OUTGOING
20
- when :both then org.neo4j.graphdb.Direction::BOTH
21
- when :incoming then org.neo4j.graphdb.Direction::INCOMING
22
- else raise "unknown direction '#{dir}', expects :outgoing, :incoming or :both"
23
- end
24
- end
25
-
26
- def ensure_valid_props(hash)
27
- hash ||= {}
28
- Hash[ hash.each_pair.map{|k,v| [k.to_s, v]} ]
29
- end
30
- end
31
- end
@@ -1,73 +0,0 @@
1
- module Neo4j
2
- #
3
- # All modifying operations that work with the node space must be wrapped in a transaction. Transactions are thread confined.
4
- # Neo4j does not implement true nested transaction, instead it uses flat nested transactions
5
- # see http://wiki.neo4j.org/content/Flat_nested_transactions
6
- #
7
- class Transaction
8
-
9
- # Starts a new Neo4j Transaction
10
- # Returns a Java Neo4j Transaction object
11
- # See http://api.neo4j.org/current/org/neo4j/graphdb/Transaction.html
12
- #
13
- # Example:
14
- # tx = Neo4j::Transaction.new
15
- # # modify something
16
- # tx.success
17
- # tx.finish
18
- def self.new(instance = Neo4j.started_db)
19
- instance.begin_tx
20
- end
21
-
22
- # Runs a block in a Neo4j transaction
23
- #
24
- # Many operations on neo requires an transaction. You will get much better performance if
25
- # one transaction is wrapped around several neo operation instead of running one transaction per
26
- # neo operation.
27
- # If one transaction is already running then a 'placebo' transaction will be created.
28
- # Performing a finish on a placebo transaction will not finish the 'real' transaction.
29
- #
30
- # ==== Params
31
- # ::yield:: the block to be performed in one transaction
32
- #
33
- # ==== Examples
34
- # include 'neo4j'
35
- #
36
- # Neo4j::Transaction.run {
37
- # node = PersonNode.new
38
- # }
39
- #
40
- # You have also access to transaction object
41
- #
42
- # Neo4j::Transaction.run { |t|
43
- # # something failed
44
- # t.failure # will cause a rollback
45
- # }
46
-
47
- # If an exception occurs inside the block the transaction will rollback automatically
48
- #
49
- # ==== Returns
50
- # The value of the evaluated provided block
51
- #
52
- def self.run # :yield: block that will be executed in a transaction
53
- raise ArgumentError.new("Expected a block to run in Transaction.run") unless block_given?
54
-
55
- begin
56
- tx = Neo4j::Transaction.new
57
- ret = yield tx
58
- tx.success
59
- rescue Exception => e
60
- if Neo4j::Config[:debug_java] && e.respond_to?(:cause)
61
- puts "Java Exception in a transaction, cause: #{e.cause}"
62
- e.cause.print_stack_trace
63
- end
64
-
65
- tx.failure unless tx.nil?
66
- raise
67
- ensure
68
- tx.finish unless tx.nil?
69
- end
70
- ret
71
- end
72
- end
73
- end
@@ -1,25 +0,0 @@
1
- module Neo4j
2
- module Traversal
3
- class FilterPredicate # :nodoc:
4
- include org.neo4j.helpers.Predicate
5
- def initialize
6
- @procs = []
7
- end
8
-
9
- def add(proc)
10
- @procs << proc
11
- end
12
-
13
- def include_start_node
14
- @include_start_node = true
15
- end
16
-
17
- def accept(path)
18
- return false if @include_start_node && path.length == 0
19
- # find the first filter which returns false
20
- # if not found then we will accept this path
21
- @procs.find {|p| !p.call(path)}.nil?
22
- end
23
- end
24
- end
25
- end
@@ -1,14 +0,0 @@
1
- module Neo4j
2
- module Traversal
3
- class PruneEvaluator # :nodoc:
4
- include org.neo4j.graphdb.traversal.PruneEvaluator
5
- def initialize(proc)
6
- @proc = proc
7
- end
8
-
9
- def prune_after(path)
10
- @proc.call(path)
11
- end
12
- end
13
- end
14
- end
@@ -1,31 +0,0 @@
1
- module Neo4j
2
- module Traversal
3
- class RelExpander
4
- include org.neo4j.graphdb.RelationshipExpander
5
-
6
- attr_accessor :reversed
7
-
8
- def initialize(&block)
9
- @block = block
10
- @reverse = false
11
- end
12
-
13
- def self.create_pair(&block)
14
- normal = RelExpander.new(&block)
15
- reversed = RelExpander.new(&block)
16
- normal.reversed = reversed
17
- reversed.reversed = normal
18
- reversed.reverse!
19
- normal
20
- end
21
-
22
- def expand(node)
23
- @block.arity == 1 ? @block.call(node) : @block.call(node, @reverse)
24
- end
25
-
26
- def reverse!
27
- @reverse = true
28
- end
29
- end
30
- end
31
- end
@@ -1,141 +0,0 @@
1
- require 'neo4j/traversal/filter_predicate'
2
- require 'neo4j/traversal/prune_evaluator'
3
- require 'neo4j/traversal/rel_expander'
4
- require 'neo4j/traversal/traverser'
5
-
6
- module Neo4j
7
-
8
- # Contains methods that are mixin for Neo4j::Node
9
- # They all return Neo4j::Traversal::Traverser
10
- # See the {Neo4j.rb Guide: Traversing Relationships and Nodes}[http://neo4j.rubyforge.org/guides/traverser.html]
11
- #
12
- module Traversal
13
- include ToJava
14
-
15
- # A more powerful alternative of #outgoing, #incoming and #both method.
16
- # You can use this method for example to only traverse nodes based on properties on the relationships
17
- #
18
- # ==== Example
19
- #
20
- # some_node.expand { |n| n._rels.find_all { |r| r[:age] > 5 } }.depth(:all).to_a
21
- #
22
- # The above traverse all relationships with a property of age > 5
23
- #
24
- # See http://neo4j.rubyforge.org/guides/traverser.html
25
- #
26
- def expand(&expander)
27
- Traverser.new(self).expander(&expander)
28
- end
29
-
30
-
31
- # Returns the outgoing nodes for this node.
32
- #
33
- # ==== Returns
34
- # a Neo4j::NodeTraverser which can be used to further specify which nodes should be included
35
- # in traversal by using the <tt>depth</tt>, <tt>filter</tt> and <tt>prune</tt> methods.
36
- #
37
- # ==== Examples
38
- # # Find all my friends (nodes of depth 1 of type <tt>friends</tt>)
39
- # me.outgoing(:friends).each {|friend| puts friend.name}
40
- #
41
- # # A possible faster way, avoid loading wrapper Ruby classes, instead use raw java neo4j node objects
42
- # me.outgoing(:friends).raw.each {|friend| puts friend[:name]}
43
- #
44
- # # Find all my friends and their friends (nodes of depth 1 of type <tt>friends</tt>)
45
- # # me.outgoing(:friends).depth(2).each {|friend| puts friend[:name]}
46
- #
47
- # # Find all my friends and include my self in the result
48
- # me.outgoing(:friends).depth(4).include_start_node.each {...}
49
- #
50
- # # Find all my friends friends friends, etc. at any depth
51
- # me.outgoing(:friends).depth(:all).each {...}
52
- #
53
- # # Find all my friends friends but do not include my friends (only depth == 2)
54
- # me.outgoing(:friends).depth(2).filter{|path| path.length == 2}
55
- #
56
- # # Find all my friends but 'cut off' some parts of the traversal path
57
- # me.outgoing(:friends).depth(42).prune(|path| an_expression_using_path_returning_true_false }
58
- #
59
- # # Find all my friends and work colleges
60
- # me.outgoing(:friends).outgoing(:work).each {...}
61
- #
62
- # Of course all the methods <tt>outgoing</tt>, <tt>incoming</tt>, <tt>both</tt>, <tt>depth</tt>, <tt>include_start_node</tt>, <tt>filter</tt>, and <tt>prune</tt>, <tt>eval_paths</tt>, <tt>unique</tt> can be combined.
63
- #
64
- # See the {Neo4j.rb Guides}[http://neo4j.rubyforge.org/guides/traverser.html]
65
- #
66
- def outgoing(type)
67
- if type
68
- Traverser.new(self).outgoing(type)
69
- else
70
- raise "Not implemented getting all types of outgoing relationship. Specify a relationship type"
71
- end
72
- end
73
-
74
-
75
- # Returns the incoming nodes of given type(s).
76
- #
77
- # See #outgoing and http://neo4j.rubyforge.org/guides/traverser.html
78
- #
79
- def incoming(type)
80
- if type
81
- Traverser.new(self).incoming(type)
82
- else
83
- raise "Not implemented getting all types of incoming relationship. Specify a relationship type"
84
- end
85
- end
86
-
87
- # Returns both incoming and outgoing nodes of given types(s)
88
- #
89
- # If a type is not given then it will return all types of relationships.
90
- #
91
- # See #outgoing and http://neo4j.rubyforge.org/guides/traverser.html
92
- #
93
- def both(type=nil)
94
- if type
95
- Traverser.new(self).both(type)
96
- else
97
- Traverser.new(self) # default is both
98
- end
99
- end
100
-
101
-
102
- # Traverse using a block. The block is expected to return one of the following values:
103
- # * <tt>:exclude_and_continue</tt>
104
- # * <tt>:exclude_and_prune</tt>
105
- # * <tt>:include_and_continue</tt>
106
- # * <tt>:include_and_prune</tt>
107
- # This value decides if it should continue to traverse and if it should include the node in the traversal result.
108
- # The block will receive a path argument.
109
- #
110
- # ==== Example
111
- #
112
- # @pet0.eval_paths {|path| path.end_node == @principal1 ? :include_and_prune : :exclude_and_continue }.unique(:node_path).depth(:all)
113
- #
114
- # ==== See also
115
- #
116
- # * How to use - http://neo4j.rubyforge.org/guides/traverser.html
117
- # * the path parameter - http://api.neo4j.org/1.4/org/neo4j/graphdb/Path.html
118
- # * the #unique method - if paths should be visit more the once, etc...
119
- #
120
- def eval_paths(&eval_block)
121
- Traverser.new(self).eval_paths(&eval_block)
122
- end
123
-
124
- # Sets uniqueness of nodes or relationships to visit during a traversals.
125
- #
126
- # Allowed values
127
- # * <tt>:node_global</tt> A node cannot be traversed more than once (default)
128
- # * <tt>:node_path</tt> For each returned node there 's a unique path from the start node to it.
129
- # * <tt>:node_recent</tt> This is like :node_global, but only guarantees uniqueness among the most recent visited nodes, with a configurable count.
130
- # * <tt>:none</tt> No restriction (the user will have to manage it).
131
- # * <tt>:rel_global</tt> A relationship cannot be traversed more than once, whereas nodes can.
132
- # * <tt>:rel_path</tt> :: No restriction (the user will have to manage it).
133
- # * <tt>:rel_recent</tt> Same as for :node_recent, but for relationships.
134
- #
135
- # See example in #eval_paths
136
- # See http://api.neo4j.org/1.4/org/neo4j/kernel/Uniqueness.html and http://neo4j.rubyforge.org/guides/traverser.html
137
- def unique(u)
138
- Traverser.new(self).unique(u)
139
- end
140
- end
141
- end
@@ -1,284 +0,0 @@
1
- module Neo4j
2
-
3
- module Traversal
4
-
5
- class Evaluator #:nodoc:
6
- include org.neo4j.graphdb.traversal.Evaluator
7
- def initialize(&eval_block)
8
- @eval_block = eval_block
9
- end
10
-
11
- # Implements the Java Interface:
12
- # evaluate(Path path)
13
- # Evaluates a Path and returns an Evaluation containing information about whether or not to include it in the traversal result, i.e return it from the Traverser.
14
- def evaluate(path)
15
- ret = @eval_block.call(path)
16
- case ret
17
- when :exclude_and_continue then
18
- org.neo4j.graphdb.traversal.Evaluation::EXCLUDE_AND_CONTINUE
19
- when :exclude_and_prune then
20
- org.neo4j.graphdb.traversal.Evaluation::EXCLUDE_AND_PRUNE
21
- when :include_and_continue then
22
- org.neo4j.graphdb.traversal.Evaluation::INCLUDE_AND_CONTINUE
23
- when :include_and_prune then
24
- org.neo4j.graphdb.traversal.Evaluation::INCLUDE_AND_PRUNE
25
- else
26
- raise "Got #{ret}, only accept :exclude_and_continue,:exclude_and_prune,:include_and_continue and :include_and_prune"
27
- end
28
- end
29
- end
30
-
31
- class Traverser
32
- include Enumerable
33
- include ToJava
34
-
35
-
36
- def initialize(from, type = nil, dir=nil)
37
- @from = from
38
- @depth = 1
39
- if type.nil? || dir.nil?
40
- @td = org.neo4j.kernel.impl.traversal.TraversalDescriptionImpl.new.breadth_first()
41
- else
42
- @type = type_to_java(type)
43
- @dir = dir_to_java(dir)
44
- @td = org.neo4j.kernel.impl.traversal.TraversalDescriptionImpl.new.breadth_first().relationships(@type, @dir)
45
- end
46
- end
47
-
48
-
49
- # Sets traversing depth first.
50
- #
51
- # The <tt>pre_or_post</tt> parameter parameter can have two values: :pre or :post
52
- # * :pre - Traversing depth first, visiting each node before visiting its child nodes (default)
53
- # * :post - Traversing depth first, visiting each node after visiting its child nodes.
54
- #
55
- def depth_first(pre_or_post = :pre)
56
- case pre_or_post
57
- when :pre then @td = @td.order(org.neo4j.kernel.Traversal.preorderDepthFirst())
58
- when :post then @td = @td.order(org.neo4j.kernel.Traversal.postorderDepthFirst())
59
- else raise "Unknown type #{pre_or_post}, should be :pre or :post"
60
- end
61
- self
62
- end
63
-
64
-
65
- # Sets traversing breadth first (default).
66
- #
67
- # This is the default ordering if none is defined.
68
- # The <tt>pre_or_post</tt> parameter parameter can have two values: :pre or :post
69
- # * :pre - Traversing breadth first, visiting each node before visiting its child nodes (default)
70
- # * :post - Traversing breadth first, visiting each node after visiting its child nodes.
71
- #
72
- # ==== Note
73
- # Please note that breadth first traversals have a higher memory overhead than depth first traversals.
74
- # BranchSelectors carries state and hence needs to be uniquely instantiated for each traversal. Therefore it is supplied to the TraversalDescription through a BranchOrderingPolicy interface, which is a factory of BranchSelector instances.
75
- #
76
- def breadth_first(pre_or_post = :pre)
77
- case pre_or_post
78
- when :pre then @td = @td.order(org.neo4j.kernel.Traversal.preorderBreadthFirst())
79
- when :post then @td = @td.order(org.neo4j.kernel.Traversal.postorderBreadthFirst())
80
- else raise "Unknown type #{pre_or_post}, should be :pre or :post"
81
- end
82
- self
83
- end
84
-
85
-
86
- def eval_paths(&eval_path_block)
87
- @td = @td.evaluator(Evaluator.new(&eval_path_block))
88
- self
89
- end
90
-
91
- def unique(u = :node_global)
92
- case u
93
- when :node_global then
94
- # A node cannot be traversed more than once.
95
- @td = @td.uniqueness(org.neo4j.kernel.Uniqueness::NODE_GLOBAL)
96
- when :node_path then
97
- # For each returned node there 's a unique path from the start node to it.
98
- @td = @td.uniqueness(org.neo4j.kernel.Uniqueness::NODE_PATH)
99
- when :node_recent then
100
- # This is like NODE_GLOBAL, but only guarantees uniqueness among the most recent visited nodes, with a configurable count.
101
- @td = @td.uniqueness(org.neo4j.kernel.Uniqueness::NODE_RECENT)
102
- when :none then
103
- # No restriction (the user will have to manage it).
104
- @td = @td.uniqueness(org.neo4j.kernel.Uniqueness::NONE)
105
- when :rel_global then
106
- # A relationship cannot be traversed more than once, whereas nodes can.
107
- @td = @td.uniqueness(org.neo4j.kernel.Uniqueness::RELATIONSHIP_GLOBAL)
108
- when :rel_path then
109
- # No restriction (the user will have to manage it).
110
- @td = @td.uniqueness(org.neo4j.kernel.Uniqueness::RELATIONSHIP_PATH)
111
- when :rel_recent then
112
- # Same as for NODE_RECENT, but for relationships.
113
- @td = @td.uniqueness(org.neo4j.kernel.Uniqueness::RELATIONSHIP_RECENT)
114
- else
115
- raise "Got option for unique '#{u}' allowed: :node_global, :node_path, :node_recent, :none, :rel_global, :rel_path, :rel_recent"
116
- end
117
- self
118
- end
119
-
120
- def to_s
121
- "NodeTraverser [from: #{@from.neo_id} depth: #{@depth} type: #{@type} dir:#{@dir}"
122
- end
123
-
124
-
125
- def <<(other_node)
126
- new(other_node)
127
- self
128
- end
129
-
130
- # Returns an real ruby array.
131
- def to_ary
132
- self.to_a
133
- end
134
-
135
- def new(other_node)
136
- case @dir
137
- when org.neo4j.graphdb.Direction::OUTGOING
138
- @from.create_relationship_to(other_node, @type)
139
- when org.neo4j.graphdb.Direction::INCOMING
140
- other_node.create_relationship_to(@from, @type)
141
- else
142
- raise "Only allowed to create outgoing or incoming relationships (not #@dir)"
143
- end
144
- end
145
-
146
- def both(type)
147
- @type = type_to_java(type) if type
148
- @dir = dir_to_java(:both)
149
- @td = @td.relationships(type_to_java(type), @dir)
150
- self
151
- end
152
-
153
- def expander(&expander)
154
- @td = @td.expand(RelExpander.create_pair(&expander))
155
- self
156
- end
157
-
158
- def outgoing(type)
159
- @type = type_to_java(type) if type
160
- @dir = dir_to_java(:outgoing)
161
- @td = @td.relationships(type_to_java(type), @dir)
162
- self
163
- end
164
-
165
- def incoming(type)
166
- @type = type_to_java(type) if type
167
- @dir = dir_to_java(:incoming)
168
- @td = @td.relationships(type_to_java(type), @dir)
169
- self
170
- end
171
-
172
- def filter_method(name, &proc)
173
- # add method name
174
- singelton = class << self;
175
- self;
176
- end
177
- singelton.send(:define_method, name) { filter &proc }
178
- self
179
- end
180
-
181
- def functions_method(func, rule_node, rule_name)
182
- singelton = class << self;
183
- self;
184
- end
185
- singelton.send(:define_method, func.class.function_name) do |*args|
186
- function_id = args.empty? ? "_classname" : args[0]
187
- function = rule_node.find_function(rule_name, func.class.function_name, function_id)
188
- function.value(rule_node.rule_node, rule_name)
189
- end
190
- self
191
- end
192
-
193
- def prune(&block)
194
- @td = @td.prune(PruneEvaluator.new(block))
195
- self
196
- end
197
-
198
- def filter(&block)
199
- # we keep a reference to filter predicate since only one filter is allowed and we might want to modify it
200
- @filter_predicate ||= FilterPredicate.new
201
- @filter_predicate.add(block)
202
- @td = @td.filter(@filter_predicate)
203
- self
204
- end
205
-
206
- # Sets depth, if :all then it will traverse any depth
207
- def depth(d)
208
- @depth = d
209
- self
210
- end
211
-
212
- def include_start_node
213
- @include_start_node = true
214
- self
215
- end
216
-
217
- def size
218
- s = 0
219
- iterator.each { |_| s += 1 }
220
- s
221
- end
222
-
223
- alias_method :length, :size
224
-
225
- def [](index)
226
- each_with_index { |node, i| break node if index == i }
227
- end
228
-
229
- def empty?
230
- first == nil
231
- end
232
-
233
- def each
234
- @raw ? iterator.each { |i| yield i } : iterator.each { |i| yield i.wrapper }
235
- end
236
-
237
- # Same as #each but does not wrap each node in a Ruby class, yields the Java Neo4j Node instance instead.
238
- def each_raw
239
- iterator.each { |i| yield i }
240
- end
241
-
242
- # Returns an enumerable of relationships instead of nodes
243
- #
244
- def rels
245
- @traversal_result = :rels
246
- self
247
- end
248
-
249
- # If this is called then it will not wrap the nodes but instead return the raw Java Neo4j::Node objects when traversing
250
- #
251
- def raw
252
- @raw = true
253
- self
254
- end
255
-
256
- # Returns an enumerable of paths instead of nodes
257
- #
258
- def paths
259
- @traversal_result = :paths
260
- @raw = true
261
- self
262
- end
263
-
264
- def iterator
265
- unless @include_start_node
266
- if @filter_predicate
267
- @filter_predicate.include_start_node
268
- else
269
- @td = @td.filter(org.neo4j.kernel.Traversal.return_all_but_start_node)
270
- end
271
- end
272
- @td = @td.prune(org.neo4j.kernel.Traversal.pruneAfterDepth(@depth)) unless @depth == :all
273
- if @traversal_result == :rels
274
- @td.traverse(@from._java_node).relationships
275
- elsif @traversal_result == :paths
276
- @td.traverse(@from._java_node).iterator
277
- else
278
- @td.traverse(@from._java_node).nodes
279
- end
280
-
281
- end
282
- end
283
- end
284
- end