-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b29ca8
commit 56fdc6e
Showing
8 changed files
with
406 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'rubygems' | ||
require 'neography' | ||
|
||
Neography::Config.server = 'neography.org' | ||
@neo = Neography::Rest.new | ||
|
||
def suggestions_for(node) | ||
node.incoming(:friends).order("breadth first").uniqueness("node global").filter("position.length() == 2;").depth(2) | ||
end | ||
|
||
johnathan = Neography::Node.create("name" =>'Johnathan') | ||
mark = Neography::Node.create("name" =>'Mark') | ||
phill = Neography::Node.create("name" =>'Phill') | ||
mary = Neography::Node.create("name" =>'Mary') | ||
luke = Neography::Node.create("name" =>'Luke') | ||
|
||
johnathan.both(:friends) << mark | ||
mark.both(:friends) << mary | ||
mark.both(:friends) << phill | ||
phill.both(:friends) << mary | ||
phill.both(:friends) << luke | ||
|
||
puts "Johnathan should become friends with #{suggestions_for(johnathan).map{|n| n.name }.join(', ')}" | ||
|
||
# RESULT | ||
# Johnathan should become friends with Mary, Phill |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'rubygems' | ||
require 'neography' | ||
|
||
Neography::Config.server = 'neography.org' | ||
@neo = Neography::Rest.new | ||
|
||
johnathan = Neography::Node.create("name" =>'Johnathan') | ||
mark = Neography::Node.create("name" =>'Mark') | ||
phill = Neography::Node.create("name" =>'Phill') | ||
mary = Neography::Node.create("name" =>'Mary') | ||
|
||
johnathan.both(:friends) << mark | ||
mark.both(:friends) << phill | ||
phill.both(:friends) << mary | ||
mark.both(:friends) << mary | ||
|
||
johnathan.all_simple_paths_to(mary).incoming(:friends).depth(4).nodes.each do |node| | ||
puts node.map{|n| n.name }.join(' => friends => ') | ||
end | ||
|
||
# RESULT | ||
# Johnathan => friends => Mark => friends => Phill => friends => Mary | ||
# Johnathan => friends => Mark => friends => Mary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Neography | ||
module NodePath | ||
|
||
def all_paths_to(to) | ||
PathTraverser.new(self, to, "allPaths", true) | ||
end | ||
|
||
def all_simple_paths_to(to) | ||
PathTraverser.new(self, to, "allSimplePaths", true) | ||
end | ||
|
||
def all_shortest_paths_to(to) | ||
PathTraverser.new(self, to, "shortestPath", true) | ||
end | ||
|
||
def path_to(to) | ||
PathTraverser.new(self, to, "allPaths", false) | ||
end | ||
|
||
def simple_path_to(to) | ||
PathTraverser.new(self, to, "allSimplePaths", false) | ||
end | ||
|
||
def shortest_path_to(to) | ||
PathTraverser.new(self, to, "shortestPath", false) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
module Neography | ||
class PathTraverser | ||
include Enumerable | ||
|
||
attr_accessor :depth, :algorithm, :relationships, :get | ||
|
||
def initialize(from, to, algorithm, all=false, types = nil, dir = "all" ) | ||
@from = from | ||
@to = to | ||
@all = all | ||
@relationships = Array.new | ||
types.each do |type| | ||
@relationships << {"type" => type.to_s, "direction" => dir.to_s } | ||
end unless types.nil? | ||
@get = ["node","rel"] | ||
@loaded = Array.new | ||
end | ||
|
||
def nodes | ||
@get = ["node"] | ||
self | ||
end | ||
|
||
def relationships | ||
@get = ["rel"] | ||
self | ||
end | ||
|
||
alias_method :rels, :relationships | ||
|
||
def both(type) | ||
@relationships << {"type" => type.to_s, "direction" => "all"} | ||
self | ||
end | ||
|
||
def outgoing(type) | ||
@relationships << {"type" => type.to_s, "direction" => "out"} | ||
self | ||
end | ||
|
||
def incoming(type) | ||
@relationships << {"type" => type.to_s, "direction" => "in"} | ||
self | ||
end | ||
|
||
def depth(d) | ||
d = 2147483647 if d == :all | ||
@depth = d | ||
self | ||
end | ||
|
||
def size | ||
[*self].size | ||
end | ||
|
||
alias_method :length, :size | ||
|
||
def each | ||
iterator.each do |path| | ||
paths = Array.new | ||
|
||
if @get.include?("node") | ||
path["nodes"].each_with_index do |n, i| | ||
@loaded[n.split('/').last.to_i] = Neography::Node.load(n) if @loaded.at(n.split('/').last.to_i).nil? | ||
paths[i * 2] = @loaded[n.split('/').last.to_i] | ||
end | ||
end | ||
|
||
if @get.include?("rel") | ||
path["relationships"].each_with_index do |r, i| | ||
@loaded[r.split('/').last.to_i] = Neography::Relationship.load(r) if @loaded.at(r.split('/').last.to_i).nil? | ||
paths[i * 2 + 1] = @loaded[r.split('/').last.to_i] | ||
end | ||
end | ||
|
||
yield paths.compact | ||
end | ||
end | ||
|
||
def empty? | ||
first == nil | ||
end | ||
|
||
def iterator | ||
if @all.nil? | ||
@from.neo_server.get_path(@from, @to, @relationships, @depth, @algorithm) | ||
else | ||
@from.neo_server.get_paths(@from, @to, @relationships, @depth, @algorithm) | ||
end | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.