Module: Neo4j::Core::Traversal

Includes:
ToJava
Included in:
Node
Defined in:
lib/neo4j-core/traversal/traversal.rb,
lib/neo4j-core/traversal/evaluator.rb,
lib/neo4j-core/traversal/traverser.rb,
lib/neo4j-core/traversal/rel_expander.rb,
lib/neo4j-core/traversal/prune_evaluator.rb,
lib/neo4j-core/traversal/filter_predicate.rb

Overview

Contains methods that are mixin for Neo4j::Node The builder pattern is used to construct traversals (all methods returns Neo4j::Traversal::Traverser)

Defined Under Namespace

Classes: CypherQuery, Evaluator, FilterPredicate, PruneEvaluator, RelExpander, Traverser

Instance Method Summary collapse

Methods included from ToJava

dir_from_java, dir_to_java, type_to_java, types_to_java

Instance Method Details

#both(type = nil) ⇒ Neo4j::Core::Traversal::Traverser

Returns both incoming and outgoing nodes of given types(s)

If a type is not given then it will return all types of relationships.

Returns:

See Also:



83
84
85
# File 'lib/neo4j-core/traversal/traversal.rb', line 83

def both(type=nil)
  Traverser.new(self, :both, type)
end

#eval_paths(&eval_block) ⇒ Neo4j::Core::Traversal::Traverser

Traverse using a block. The block is expected to return one of the following values:

  • :exclude_and_continue

  • :exclude_and_prune

  • :include_and_continue

  • :include_and_prune

This value decides if it should continue to traverse and if it should include the node in the traversal result. The block will receive a path argument.

See also

Examples:

@pet0.eval_paths {|path| path.end_node ==  @principal1 ? :include_and_prune : :exclude_and_continue }.unique(:node_path).depth(:all)

Returns:



106
107
108
# File 'lib/neo4j-core/traversal/traversal.rb', line 106

def eval_paths(&eval_block)
  Traverser.new(self).eval_paths(&eval_block)
end

#expand {|node| ... } ⇒ Neo4j::Core::Traversal::Traverser

A more powerful alternative of #outgoing, #incoming and #both method. You can use this method for example to only traverse nodes based on properties on the relationships

Examples:

traverse all relationships with a property of age > 5

some_node.expand { |n| n._rels.find_all { |r| r[:age] > 5 } }.depth(:all).to_a

Yields:

  • (node)

    Used to find out which relationship should be included in the traversal

Yield Parameters:

  • node (Neo4j::Node)

    the current node from which we can decide which relationship should be traversed

Yield Returns:

Returns:



26
27
28
# File 'lib/neo4j-core/traversal/traversal.rb', line 26

def expand(&expander)
  Traverser.new(self).expander(&expander)
end

#incoming(type) ⇒ Neo4j::Core::Traversal::Traverser

Returns the incoming nodes of given type(s).

Parameters:

  • type (String, Symbol)

    the relationship type

Returns:

See Also:



73
74
75
# File 'lib/neo4j-core/traversal/traversal.rb', line 73

def incoming(type)
  Traverser.new(self, :incoming, type)
end

#outgoing(type) ⇒ Neo4j::Core::Traversal::Traverser

Returns the outgoing nodes for this node.

Of course all the methods outgoing, incoming, both, depth, include_start_node, filter, and prune, eval_paths, unique can be combined.

Examples:

Find all my friends (nodes of depth 1 of type friends)

me.outgoing(:friends).each {|friend| puts friend.name}

A possible faster way, avoid loading wrapper Ruby classes, instead use raw java neo4j node objects

me.outgoing(:friends).raw.each {|friend| puts friend[:name]}

Find all my friends and their friends (nodes of depth 1 of type friends)

me.outgoing(:friends).depth(2).each {|friend| puts friend[:name]}

Find all my friends and include my self in the result

me.outgoing(:friends).depth(4).include_start_node.each {...}

Find all my friends friends friends, etc. at any depth

me.outgoing(:friends).depth(:all).each {...}

Find all my friends friends but do not include my friends (only depth == 2)

me.outgoing(:friends).depth(2).filter{|path| path.length == 2}

Find all my friends but ‘cut off’ some parts of the traversal path

me.outgoing(:friends).depth(42).prune(|path| an_expression_using_path_returning_true_false }

Find all my friends and work colleges

me.outgoing(:friends).outgoing(:work).each {...}

Parameters:

  • type (String, Symbol)

    the relationship type

Returns:

See Also:



62
63
64
# File 'lib/neo4j-core/traversal/traversal.rb', line 62

def outgoing(type)
  Traverser.new(self, :outgoing, type)
end

#unique(u) ⇒ Neo4j::Core::Traversal::Traverser

Sets uniqueness of nodes or relationships to visit during a traversals.

Allowed values

  • :node_global A node cannot be traversed more than once (default)

  • :node_path For each returned node there ‘s a unique path from the start node to it.

  • :node_recent This is like :node_global, but only guarantees uniqueness among the most recent visited nodes, with a configurable count.

  • :none No restriction (the user will have to manage it).

  • :rel_global A relationship cannot be traversed more than once, whereas nodes can.

  • :rel_path No restriction (the user will have to manage it).

  • :rel_recent Same as for :node_recent, but for relationships.

Parameters:

  • u (:node_global, :node_path, :node_recent, :none, :rel_global, :rel_path, :rel_recent)

    the uniqueness option

Returns:

See Also:



125
126
127
# File 'lib/neo4j-core/traversal/traversal.rb', line 125

def unique(u)
  Traverser.new(self).unique(u)
end