Skip to content

Commit

Permalink
Support for shortest weighted path
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Mar 22, 2012
1 parent 9e4540e commit 5bd1325
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ def get_paths(from, to, relationships, depth=1, algorithm="allPaths")
options = { :body => {"to" => self.configuration + "/node/#{get_id(to)}", "relationships" => relationships, "max_depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} }
paths = post("/node/#{get_id(from)}/paths", options) || Array.new
end

def get_shortest_weighted_path(from, to, relationships, weight_attr='weight', depth=1, algorithm="dijkstra")
options = { :body => {"to" => self.configuration + "/node/#{get_id(to)}", "relationships" => relationships, "cost_property" => weight_attr, "max_depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} }
paths = post("/node/#{get_id(from)}/paths", options) || Hash.new
end

def execute_query(query, params = {})
options = { :body => {:query => query, :params => params}.to_json, :headers => {'Content-Type' => 'application/json'} }
Expand Down Expand Up @@ -482,6 +487,8 @@ def get_algorithm(algorithm)
"shortestPath"
when :allSimplePaths, "allSimplePaths", :simple, "simple"
"allSimplePaths"
when :dijkstra, "dijkstra"
"dijkstra"
else
"allPaths"
end
Expand Down
22 changes: 22 additions & 0 deletions spec/integration/rest_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@
path["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"], new_node5["self"]]
end

it "can get the shortest weighted path between two nodes" do
new_node1 = @neo.create_node
new_node2 = @neo.create_node
new_node3 = @neo.create_node
new_node4 = @neo.create_node
new_node5 = @neo.create_node
rel1_2 = @neo.create_relationship("friends", new_node1, new_node2)
rel2_3 = @neo.create_relationship("friends", new_node2, new_node3)
rel3_4 = @neo.create_relationship("friends", new_node3, new_node4)
rel4_5 = @neo.create_relationship("friends", new_node4, new_node5)
rel3_5 = @neo.create_relationship("friends", new_node3, new_node5)
@neo.set_relationship_properties(rel1_2, {weight: 1})
@neo.set_relationship_properties(rel2_3, {weight: 1})
@neo.set_relationship_properties(rel3_4, {weight: 1})
@neo.set_relationship_properties(rel4_5, {weight: 1})
@neo.set_relationship_properties(rel3_5, {weight: 2})
path = @neo.get_shortest_weighted_path(new_node1, new_node5, {"type"=> "friends", "direction" => "out"})
path["start"].should == new_node1["self"]
path["end"].should == new_node5["self"]
path["nodes"].should == [new_node1["self"], new_node2["self"], new_node3["self"], new_node5["self"]]
end

it "can get a simple path between two nodes" do
new_node1 = @neo.create_node
new_node2 = @neo.create_node
Expand Down

0 comments on commit 5bd1325

Please sign in to comment.