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.
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).
Neo4j::RelationshipMixin if you want to wrap a relationship with your own Ruby class.
(Those mixin are actually not included in the Neo4j::Relationship but instead directly included in the java class org.neo4j.kernel.impl.core.RelationshipProxy)
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 is the same as new
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
Returns a org.neo4j.graphdb.Relationship java object (!) Will trigger a event that the relationship was created.
the type of relationship
the start node of this relationship
the end node of this relationship
optional properties for the created relationship
org.neo4j.graphdb.Relationship java object
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
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
Returns the start node of this relationship
# File lib/neo4j/relationship.rb, line 145
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.
the node that we don’t want to return
# File lib/neo4j/relationship.rb, line 162
Returns the start node of this relationship
# File lib/neo4j/relationship.rb, line 140