Created
May 24, 2010 09:40
-
-
Save peterneubauer/411695 to your computer and use it in GitHub Desktop.
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
require "rubygems" | |
require "neo4j" | |
class Person | |
include Neo4j::NodeMixin | |
# define Neo4j properties | |
property :name | |
# define an one way relationship to any other node | |
has_n :friends | |
# adds a Lucene index on the following properties | |
index :name | |
end | |
Neo4j::Transaction.run do | |
#the players | |
neo = Person.new :name => 'Neo' | |
morpheus = Person.new :name => 'Morpheus' | |
trinity = Person.new :name => 'Trinity' | |
cypher = Person.new :name => 'Cypher' | |
smith = Person.new :name => 'Agent Smith' | |
architect = Person.new :name => 'Architect' | |
#the connections | |
trinity.rels.outgoing(:loves) << neo | |
architect.rels.outgoing(:has_coded) << smith | |
cypher.friends << morpheus | |
cypher.friends << smith | |
neo.friends << morpheus | |
morpheus.friends << trinity | |
#find Neo's friends | |
# neo.friends.each { |n| puts 'Friend: ' + n.name } | |
#Neo's friends of friends | |
# neo.friends.depth(2).each { |n| puts 'FOAF: ' + n.name } | |
#who is in love? | |
# architect.traverse.both(:friends, :has_coded, :loves).depth(:all).filter do | |
# outgoing(:loves).to_a.size > 0 | |
# end.each do |n| | |
# puts 'In love: ' + n.name | |
# end | |
#who loves neo? | |
neo.friends.depth(2).each { |node| | |
node.rels.outgoing(:loves).each { |love| | |
if love.end_node == neo then | |
puts love.start_node.name + ' loves Neo' | |
end | |
} | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment