Skip to content

Commit

Permalink
adding traverser
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Nov 21, 2010
1 parent 77b9011 commit 90cb6ed
Show file tree
Hide file tree
Showing 3 changed files with 311 additions and 4 deletions.
31 changes: 27 additions & 4 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
== Welcome to Neography

Neography is a thin ruby wrapper to the Neo4j Rest API, for more information:
Neography is a thin Ruby wrapper to the Neo4j Rest API, for more information:
* {Getting Started with Neo4j Server}[http://wiki.neo4j.org/content/Getting_Started_with_Neo4j_Server]
* {Neo4j Rest API Reference}[http://components.neo4j.org/neo4j-rest/]

If you want to the full power of Neo4j, you will want to use JRuby and the excellent {Neo4j.rb} [https://github.com/andreasronge/neo4j]

=== Installation

Expand Down Expand Up @@ -78,17 +79,39 @@ To Use:
@neo.get_path(from, to, relationships, depth=4, algorithm="shortestPath") # finds the shortest path between two nodes
@neo.get_paths(from, to, relationships, depth=3, algorithm="allPaths") # finds all paths between two nodes

nodes = @neo.traverse(id, # the id of the node where the traversal starts
"nodes", # the return type (can be "nodes", "relationships" or "paths"
{"order" => "breadth first", # "breadth first" or "depth first" traversal order
"uniqueness" => "node global", # See Uniqueness in API documentation for options.
"relationships" => [{"type"=> "roommates", # A hash containg a description of the traversal
"direction" => "all"}, # two relationships.
{"type"=> "friends", #
"direction" => "out"}], #
"prune evaluator" => {"language" => "javascript", # A prune evaluator (when to stop traversing)
"body" => "position.endNode().getProperty('age') < 21;"
"return filter" => {"language" => "builtin", # "all" or "all but start node"
"name" => "all"},
"depth" => 4})

# "depth" is a short-hand way of specifying a prune evaluator which prunes after a certain depth.
# If not specified a depth of 1 is used and if a "prune evaluator" is specified instead of a depth, no depth limit is set.

{Order}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/graphdb/Traverser.Order.html]
{Uniqueness}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/kernel/Uniqueness.html]
{Prune Evaluator}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/graphdb/StopEvaluator.html]
{Return Filter}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/graphdb/ReturnableEvaluator.html]

=== To Do

* Traverse tests
* @neo.traverse()
* More tests
* examples
* batch import with typhoeus ?
* create proper objects for Node and Relationship

=== License

* Neography - MIT, see the LICENSE file http://github.com/maxdemarzi/neography/tree/master/LICENSE.
* Lucene - Apache, see http://lucene.apache.org/java/docs/features.html
* Neo4j - Dual free software/commercial license, see http://neo4j.org/
* Neo4j - Dual free software/commercial license, see http://neo4j.org


54 changes: 54 additions & 0 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ def get_index(key, value)
index
end

def traverse(id, return_type, description)
options = { :body => {"order" => get_order(description["order"]),
"uniqueness" => get_uniqueness(description["uniqueness"]),
"relationships" => description["relationships"],
"prune evaluator" => description["prune evaluator"],
"return filter" => description["return filter"],
"max depth" => get_depth(description["depth"]), }.to_json, :headers => {'Content-Type' => 'application/json'} }
traversal = post("/node/#{id}/traverse/#{get_type(return_type)}", options) || Array.new
end

def get_path(from, to, relationships, depth=1, algorithm="shortestPath")
options = { :body => {"to" => self.configuration + "/node/#{to}", "relationships" => relationships, "max depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} }
path = post("/node/#{from}/path", options) || Hash.new
Expand Down Expand Up @@ -237,5 +247,49 @@ def get_algorithm(algorithm)
end
end

def get_order(order)
case order
when :breadth, "breadth", "breadth first", "breadthFirst", :wide, "wide"
"breadth first"
else
"depth first"
end
end

def get_type(type)
case type
when :node, "nodes", :nodes, "nodes"
"node"
when :relationship, "relationship", :relationships, "relationships"
"relationship"
else
"path"
end
end

def get_uniqueness(uniqueness)
case uniqueness
when :nodeglobal, "node global", "nodeglobal", "node_global"
"node global"
when :nodepath, "node path", "nodepath", "node_path"
"node path"
when :noderecent, "node recent", "noderecent", "node_recent"
"node recent"
when :relationshipglobal, "relationship global", "relationshipglobal", "relationship_global"
"relationship global"
when :relationshippath, "relationship path", "relationshippath", "relationship_path"
"relationship path"
when :relationshiprecent, "relationship recent", "relationshiprecent", "relationship_recent"
"relationship recent"
else
"none"
end
end

def get_depth(depth)
return 1 if depth.to_i == 0
depth.to_i
end

end
end
230 changes: 230 additions & 0 deletions spec/integration/rest_traverse_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe Neography::Rest do
before(:each) do
@neo = Neography::Rest.new
end

describe "traverse" do
it "can traverse the graph and return nodes" do
new_node1 = @neo.create_node
new_node1[:id] = new_node1["self"].split('/').last
new_node2 = @neo.create_node
new_node2[:id] = new_node2["self"].split('/').last
new_node3 = @neo.create_node
new_node3[:id] = new_node3["self"].split('/').last
new_node4 = @neo.create_node
new_node4[:id] = new_node4["self"].split('/').last
new_node5 = @neo.create_node
new_node5[:id] = new_node5["self"].split('/').last
@neo.create_relationship("friends", new_node1[:id], new_node2[:id])
@neo.create_relationship("friends", new_node2[:id], new_node3[:id])
@neo.create_relationship("friends", new_node3[:id], new_node4[:id])
@neo.create_relationship("friends", new_node4[:id], new_node5[:id])
@neo.create_relationship("friends", new_node3[:id], new_node5[:id])
nodes = @neo.traverse(new_node1[:id], "nodes", {"relationships" => {"type"=> "friends", "direction" => "out"}, "depth" => 4} )
nodes.should_not be_nil
nodes[0]["self"].should == new_node2["self"]
nodes[1]["self"].should == new_node3["self"]
nodes[2]["self"].should == new_node4["self"]
nodes[3]["self"].should == new_node5["self"]
end

it "can traverse the graph and return relationships" do
new_node1 = @neo.create_node
new_node1[:id] = new_node1["self"].split('/').last
new_node2 = @neo.create_node
new_node2[:id] = new_node2["self"].split('/').last
new_node3 = @neo.create_node
new_node3[:id] = new_node3["self"].split('/').last
new_node4 = @neo.create_node
new_node4[:id] = new_node4["self"].split('/').last
new_node5 = @neo.create_node
new_node5[:id] = new_node5["self"].split('/').last

new_relationship1= @neo.create_relationship("friends", new_node1[:id], new_node2[:id])
new_relationship2= @neo.create_relationship("friends", new_node2[:id], new_node3[:id])
new_relationship3= @neo.create_relationship("friends", new_node3[:id], new_node4[:id])
new_relationship4= @neo.create_relationship("friends", new_node4[:id], new_node5[:id])
new_relationship5= @neo.create_relationship("friends", new_node3[:id], new_node5[:id])

relationships = @neo.traverse(new_node1[:id], "relationships", {"relationships" => {"type"=> "friends", "direction" => "out"}, "depth" => 4} )
relationships.should_not be_nil

relationships[0]["self"].should == new_relationship1["self"]
relationships[1]["self"].should == new_relationship2["self"]
relationships[2]["self"].should == new_relationship3["self"]
relationships[3]["self"].should == new_relationship4["self"]
end

it "can traverse the graph and return paths" do
new_node1 = @neo.create_node
new_node1[:id] = new_node1["self"].split('/').last
new_node2 = @neo.create_node
new_node2[:id] = new_node2["self"].split('/').last
new_node3 = @neo.create_node
new_node3[:id] = new_node3["self"].split('/').last
new_node4 = @neo.create_node
new_node4[:id] = new_node4["self"].split('/').last
new_node5 = @neo.create_node
new_node5[:id] = new_node5["self"].split('/').last

new_relationship1= @neo.create_relationship("friends", new_node1[:id], new_node2[:id])
new_relationship2= @neo.create_relationship("friends", new_node2[:id], new_node3[:id])
new_relationship3= @neo.create_relationship("friends", new_node3[:id], new_node4[:id])
new_relationship4= @neo.create_relationship("friends", new_node4[:id], new_node5[:id])
new_relationship5= @neo.create_relationship("friends", new_node3[:id], new_node5[:id])

paths = @neo.traverse(new_node1[:id], "paths", {"relationships" => {"type"=> "friends", "direction" => "out"}, "depth" => 4} )
paths.should_not be_nil

paths[0]["nodes"].should == [new_node1["self"], new_node2["self"]]
paths[1]["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"]]
paths[2]["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"], new_node4["self"]]
paths[3]["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"], new_node4["self"], new_node5["self"]]
end

it "can traverse the graph up to a certain depth" do
new_node1 = @neo.create_node
new_node1[:id] = new_node1["self"].split('/').last
new_node2 = @neo.create_node
new_node2[:id] = new_node2["self"].split('/').last
new_node3 = @neo.create_node
new_node3[:id] = new_node3["self"].split('/').last
new_node4 = @neo.create_node
new_node4[:id] = new_node4["self"].split('/').last
new_node5 = @neo.create_node
new_node5[:id] = new_node5["self"].split('/').last

new_relationship1= @neo.create_relationship("friends", new_node1[:id], new_node2[:id])
new_relationship2= @neo.create_relationship("friends", new_node2[:id], new_node3[:id])
new_relationship3= @neo.create_relationship("friends", new_node3[:id], new_node4[:id])
new_relationship4= @neo.create_relationship("friends", new_node4[:id], new_node5[:id])
new_relationship5= @neo.create_relationship("friends", new_node3[:id], new_node5[:id])

paths = @neo.traverse(new_node1[:id], "paths", {"relationships" => {"type"=> "friends", "direction" => "out"}, "depth" => 3} )
paths.should_not be_nil

paths[0]["nodes"].should == [new_node1["self"], new_node2["self"]]
paths[1]["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"]]
paths[2]["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"], new_node4["self"]]
paths[3]["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"], new_node5["self"]]
end

it "can traverse the graph in a certain order" do
new_node1 = @neo.create_node
new_node1[:id] = new_node1["self"].split('/').last
new_node2 = @neo.create_node
new_node2[:id] = new_node2["self"].split('/').last
new_node3 = @neo.create_node
new_node3[:id] = new_node3["self"].split('/').last
new_node4 = @neo.create_node
new_node4[:id] = new_node4["self"].split('/').last
new_node5 = @neo.create_node
new_node5[:id] = new_node5["self"].split('/').last

new_relationship1= @neo.create_relationship("friends", new_node1[:id], new_node2[:id])
new_relationship2= @neo.create_relationship("friends", new_node2[:id], new_node3[:id])
new_relationship3= @neo.create_relationship("friends", new_node3[:id], new_node4[:id])
new_relationship4= @neo.create_relationship("friends", new_node4[:id], new_node5[:id])
new_relationship5= @neo.create_relationship("friends", new_node3[:id], new_node5[:id])

paths = @neo.traverse(new_node1[:id], "paths", {"order" => "breadth first", "relationships" => {"type"=> "friends", "direction" => "out"}, "depth" => 4} )
paths.should_not be_nil

paths[0]["nodes"].should == [new_node1["self"], new_node2["self"]]
paths[1]["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"]]
paths[2]["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"], new_node4["self"]]
paths[3]["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"], new_node5["self"]]
end

it "can traverse the graph with a specific uniqueness" do
new_node1 = @neo.create_node
new_node1[:id] = new_node1["self"].split('/').last
new_node2 = @neo.create_node
new_node2[:id] = new_node2["self"].split('/').last
new_node3 = @neo.create_node
new_node3[:id] = new_node3["self"].split('/').last
new_node4 = @neo.create_node
new_node4[:id] = new_node4["self"].split('/').last
new_node5 = @neo.create_node
new_node5[:id] = new_node5["self"].split('/').last

new_relationship1= @neo.create_relationship("roommates", new_node1[:id], new_node2[:id])
new_relationship2= @neo.create_relationship("roommates", new_node2[:id], new_node3[:id])
new_relationship1= @neo.create_relationship("friends", new_node3[:id], new_node2[:id])
new_relationship2= @neo.create_relationship("friends", new_node2[:id], new_node5[:id])
new_relationship3= @neo.create_relationship("friends", new_node3[:id], new_node4[:id])
new_relationship4= @neo.create_relationship("friends", new_node4[:id], new_node5[:id])
new_relationship5= @neo.create_relationship("friends", new_node3[:id], new_node5[:id])

paths = @neo.traverse(new_node1[:id], "paths", {"order" => "breadth first", "uniqueness" => "node global", "relationships" => [{"type"=> "roommates", "direction" => "all"},{"type"=> "friends", "direction" => "out"}], "depth" => 4} )
paths.should_not be_nil

paths[0]["nodes"].should == [new_node1["self"], new_node2["self"]]
paths[1]["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"]]
paths[2]["nodes"].should == [new_node1["self"], new_node2["self"], new_node5["self"]]
paths[3]["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"], new_node4["self"]]
end

it "can traverse the graph with a prune evaluator" do
new_node1 = @neo.create_node("age" => 31, "name" => "Max")
new_node1[:id] = new_node1["self"].split('/').last
new_node2 = @neo.create_node("age" => 30, "name" => "Helene")
new_node2[:id] = new_node2["self"].split('/').last
new_node3 = @neo.create_node("age" => 17, "name" => "Alex")
new_node3[:id] = new_node3["self"].split('/').last
new_node4 = @neo.create_node("age" => 24, "name" => "Eric")
new_node4[:id] = new_node4["self"].split('/').last
new_node5 = @neo.create_node("age" => 32, "name" => "Leslie")
new_node5[:id] = new_node5["self"].split('/').last

new_relationship1= @neo.create_relationship("friends", new_node1[:id], new_node2[:id])
new_relationship2= @neo.create_relationship("friends", new_node2[:id], new_node3[:id])
new_relationship3= @neo.create_relationship("friends", new_node3[:id], new_node4[:id])
new_relationship4= @neo.create_relationship("friends", new_node4[:id], new_node5[:id])
new_relationship5= @neo.create_relationship("friends", new_node3[:id], new_node5[:id])

paths = @neo.traverse(new_node1[:id],
"paths",
{"relationships" => {"type"=> "friends", "direction" => "out"},
"depth" => 3,
"prune evaluator" => {"language" => "javascript", "body" => "position.endNode().getProperty('age') < 21;"
}} )
paths.should_not be_nil
paths[0]["nodes"].should == [new_node1["self"], new_node2["self"]]
paths[1]["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"]]
paths[2].should be_nil
end

it "can traverse the graph with a return filter" do
new_node1 = @neo.create_node
new_node1[:id] = new_node1["self"].split('/').last
new_node2 = @neo.create_node
new_node2[:id] = new_node2["self"].split('/').last
new_node3 = @neo.create_node
new_node3[:id] = new_node3["self"].split('/').last
new_node4 = @neo.create_node
new_node4[:id] = new_node4["self"].split('/').last
new_node5 = @neo.create_node
new_node5[:id] = new_node5["self"].split('/').last
@neo.create_relationship("friends", new_node1[:id], new_node2[:id])
@neo.create_relationship("friends", new_node2[:id], new_node3[:id])
@neo.create_relationship("friends", new_node3[:id], new_node4[:id])
@neo.create_relationship("friends", new_node4[:id], new_node5[:id])
@neo.create_relationship("friends", new_node3[:id], new_node5[:id])
nodes = @neo.traverse(new_node1[:id], "nodes", {"relationships" => {"type"=> "friends", "direction" => "out"},
"return filter" => {"language" => "builtin", "name" => "all"},
"depth" => 4} )
nodes.should_not be_nil
nodes[0]["self"].should == new_node1["self"]
nodes[1]["self"].should == new_node2["self"]
nodes[2]["self"].should == new_node3["self"]
nodes[3]["self"].should == new_node4["self"]
nodes[4]["self"].should == new_node5["self"]
end


end

end

0 comments on commit 90cb6ed

Please sign in to comment.