From 5bd1325a878dd88ceb943222a7e1835477cd1045 Mon Sep 17 00:00:00 2001 From: Pablo Fernandez Date: Thu, 22 Mar 2012 00:51:06 -0300 Subject: [PATCH] Support for shortest weighted path --- lib/neography/rest.rb | 7 +++++++ spec/integration/rest_path_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 7cfa5f4..755215f 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -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'} } @@ -482,6 +487,8 @@ def get_algorithm(algorithm) "shortestPath" when :allSimplePaths, "allSimplePaths", :simple, "simple" "allSimplePaths" + when :dijkstra, "dijkstra" + "dijkstra" else "allPaths" end diff --git a/spec/integration/rest_path_spec.rb b/spec/integration/rest_path_spec.rb index 6c83323..30721a5 100644 --- a/spec/integration/rest_path_spec.rb +++ b/spec/integration/rest_path_spec.rb @@ -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