class Neo4j::Relationship

A relationship between two nodes in the graph. A relationship has a start node, an end node and a type. You can attach properties to relationships with the API specified in Neo4j::JavaPropertyMixin.

Relationship are created by invoking the << operator on the rels method on the node as follow:

node.outgoing(:friends) << other_node << yet_another_node

or using the Neo4j::Relationship#new method (which does the same thing):

rel = Neo4j::Relationship.new(:friends, node, other_node)

The fact that the relationship API gives meaning to start and end nodes implicitly means that all relationships have a direction. In the example above, rel would be directed from node to otherNode. A relationship’s start node and end node and their relation to outgoing and incoming are defined so that the assertions in the following code are true:

a = Neo4j::Node.new
b = Neo4j::Node.new
rel = Neo4j::Relationship.new(:some_type, a, b)
# Now we have: (a) --- REL_TYPE ---> (b)

 rel.start_node # => a
 rel.end_node   # => b

Furthermore, Neo4j guarantees that a relationship is never “hanging freely,” i.e. #start_node, #end_node and #other_node are guaranteed to always return valid, non-null nodes.

Wrapping

Notice that the ::new does not create a Ruby object. Instead, it returns a Java org.neo4j.graphdb.Relationship object which has been modified to feel more rubyish (like Neo4j::Node).

See also

Included Mixins

(Those mixin are actually not included in the Neo4j::Relationship but instead directly included in the java class org.neo4j.kernel.impl.core.RelationshipProxy)

Public Class Methods

_load(rel_id, db = Neo4j.started_db) click to toggle source

Same as load but does not return the node as a wrapped Ruby object.

# File lib/neo4j/relationship.rb, line 216
def _load(rel_id, db = Neo4j.started_db)
  return nil if rel_id.nil?
  rel = db.graph.get_relationship_by_id(rel_id.to_i)
  rel.hasProperty('_classname')  # since we want a IllegalStateException which is otherwise not triggered
  rel
rescue java.lang.IllegalStateException
  nil # the node has been deleted
rescue org.neo4j.graphdb.NotFoundException
  nil
end
create(type, start_node, end_node, props=nil) click to toggle source

create is the same as new

Alias for: new
load(rel_id, db = Neo4j.started_db) click to toggle source

Loads a relationship or wrapped relationship given a native java relationship or an id. If there is a Ruby wrapper for the node then it will create a Ruby object that will wrap the java node (see Neo4j::RelationshipMixin).

If the relationship does not exist it will return nil

# File lib/neo4j/relationship.rb, line 208
def load(rel_id, db = Neo4j.started_db)
  rel = _load(rel_id, db)
  return nil if rel.nil?
  rel.wrapper
end
new(type, start_node, end_node, props=nil) click to toggle source

Returns a org.neo4j.graphdb.Relationship java object (!) Will trigger a event that the relationship was created.

Parameters

type

the type of relationship

from_node

the start node of this relationship

#end_node

the end node of this relationship

props

optional properties for the created relationship

Returns

org.neo4j.graphdb.Relationship java object

Examples

Neo4j::Relationship.new :friend, node1, node2, :since => '2001-01-02', :status => 'okey'
# File lib/neo4j/relationship.rb, line 192
def new(type, start_node, end_node, props=nil)
  java_type = type_to_java(type)
  rel = start_node._java_node.create_relationship_to(end_node._java_node, java_type)
  props.each_pair {|k,v| rel[k] = v} if props
  rel
end
Also aliased as: create

Public Instance Methods

del() click to toggle source

Deletes this relationship. Invoking any methods on this relationship after delete() has returned is invalid and will lead t

# File lib/neo4j/relationship.rb, line 167
      
end_node() click to toggle source

Returns the start node of this relationship

# File lib/neo4j/relationship.rb, line 145
      
other_node() click to toggle source

A convenience operation that, given a node that is attached to this relationship, returns the other node. For example if node is a start node, the end node will be returned, and vice versa. This is a very convenient operation when you’re manually traversing the node space by invoking one of the rels method on a node. For example, to get the node “at the other end” of a relationship, use the following:

end_node = node.rels.first.other_node(node)

This operation will throw a runtime exception if node is neither this relationship’s start node nor its end node.

Parameters

node

the node that we don’t want to return

# File lib/neo4j/relationship.rb, line 162
      
start_node() click to toggle source

Returns the start node of this relationship

# File lib/neo4j/relationship.rb, line 140