Skip to content

Instantly share code, notes, and snippets.

@andreasronge
Created May 4, 2012 07:07
Show Gist options
  • Save andreasronge/2592759 to your computer and use it in GitHub Desktop.
Save andreasronge/2592759 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'neo4j-core'
def connect(d)
d.each{|i| Neo4j::Relationship.new(:flies_to, i[:from], i[:to])}
end
tx = Neo4j::Transaction.new
@a, @b, @c, @z = %w[A B C D].map{|name| Neo4j::Node.new(:name => name)}
connect([ {:from=>@a, :to=>@b}, {:from=>@a, :to=>@c}, {:from=>@b, :to=>@c},
{:from=>@b, :to=>@z}, {:from=>@c, :to=>@b}, {:from=>@c, :to=>@z}])
tx.success
tx.finish
# Notice the unique parameter (default is :node_global) see https://github.com/andreasronge/neo4j/wiki/Neo4j%3A%3ACore-Traverse
traversal = @a.outgoing(:flies_to).depth(:all).unique(:node_path).eval_paths { |path| :include_and_continue }
# This prints out
#Path A->B
#Path A->C
#Path A->B->C
#Path A->B->D
#Path A->C->B
#Path A->C->D
#Path A->B->C->D
#Path A->C->B->D
traversal.paths.each do |path|
p = path.map{|p| p[:name]}.compact.join('->')
puts "Path #{p.to_s}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment