Skip to content

Commit

Permalink
adding paths
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Dec 11, 2010
1 parent 6b29ca8 commit 56fdc6e
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 1 deletion.
26 changes: 26 additions & 0 deletions examples/facebook_v2.rb
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
23 changes: 23 additions & 0 deletions examples/linkedin_v2.rb
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
3 changes: 2 additions & 1 deletion lib/neography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ def find_and_require_user_defined_code
require 'neography/property_container'
require 'neography/property'
require 'neography/node_relationship'
require 'neography/node_path'
require 'neography/relationship_traverser'
require 'neography/node_traverser'
require 'neography/path_traverser'
require 'neography/equal'


require 'neography/node'
require 'neography/relationship'

Expand Down
1 change: 1 addition & 0 deletions lib/neography/node.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Neography
class Node < PropertyContainer
include Neography::NodeRelationship
include Neography::NodePath
include Neography::Equal
include Neography::Property

Expand Down
29 changes: 29 additions & 0 deletions lib/neography/node_path.rb
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
10 changes: 10 additions & 0 deletions lib/neography/node_traverser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def incoming(type)
self
end

def uniqueness(u)
@uniqueness = u
self
end

def order(o)
@order = o
self
end

def filter(body)
@filter = Hash.new
@filter["language"] = "javascript"
Expand Down
93 changes: 93 additions & 0 deletions lib/neography/path_traverser.rb
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
Loading

0 comments on commit 56fdc6e

Please sign in to comment.