Created
April 3, 2010 16:36
-
-
Save mbbx6spp/354659 to your computer and use it in GitHub Desktop.
Code sample from 'Why proponents of marriage equality should love graph databases, Part 2' http://bit.ly/dccCPj
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
// Creative Commons Attribution-Share Alike 3.0 United States License | |
// http://creativecommons.org/licenses/by-sa/3.0/us/ | |
// From blog post: http://bit.ly/dccCPj | |
package net.susanpotter.inclusivemarriage; | |
import java.text.SimpleDateFormat; | |
import java.text.ParseException; | |
import java.util.Date; | |
import org.neo4j.graphdb.*; | |
import org.neo4j.kernel.EmbeddedGraphDatabase; | |
public class NeoBase { | |
public enum IMRelationshipTypes implements RelationshipType { | |
MARRIED, KNOWS, THINKS | |
} | |
public static void main(String[] args) { | |
GraphDatabaseService graphDb = new EmbeddedGraphDatabase("var/base"); | |
Transaction tx = graphDb.beginTx(); | |
try { | |
Node harry = toPersonNode(graphDb, "Harry"); | |
Node mary = toPersonNode(graphDb, "Mary"); | |
Node teresa = toPersonNode(graphDb, "Teresa"); | |
Node tony = toPersonNode(graphDb, "Tony"); | |
Node fredPhelps = toPersonNode(graphDb, "Fred Phelps"); | |
Node bigotedOpinion = toOpinionNode(graphDb, "Homosexuals are going to hell!"); | |
Relationship t2mMarriage = toMarriage(graphDb, teresa, mary, "2008-08-08", null); | |
Relationship m2hMarriage = toMarriage(graphDb, mary, harry, "2005-05-03", "2010-01-23"); | |
Relationship hate = fredPhelps.createRelationshipTo(bigotedOpinion, IMRelationshipTypes.THINKS); | |
Relationship t2hFriendship = toFriendship(graphDb, tony, harry, "1999-12-31", null); | |
// now we can do some queries to demonstrate how graph databases can help with modeling gay and polyamorous relationships | |
Traverser marysPartners = mary.traverse( | |
Traverser.Order.BREADTH_FIRST, | |
StopEvaluator.DEPTH_ONE, | |
ReturnableEvaluator.ALL_BUT_START_NODE, | |
IMRelationshipTypes.MARRIED, | |
Direction.BOTH | |
); | |
for(Node partner : marysPartners) { | |
System.out.println("Mary is married to " + partner.getProperty("name")); | |
} | |
Traverser tonysFriends = tony.traverse( | |
Traverser.Order.BREADTH_FIRST, | |
StopEvaluator.DEPTH_ONE, | |
ReturnableEvaluator.ALL_BUT_START_NODE, | |
IMRelationshipTypes.KNOWS, | |
Direction.BOTH | |
); | |
for (Node friend : tonysFriends) { | |
System.out.println("Tony is friends with " + friend.getProperty("name")); | |
} | |
Traverser fredPhelpsOpinions = fredPhelps.traverse( | |
Traverser.Order.BREADTH_FIRST, | |
StopEvaluator.DEPTH_ONE, | |
ReturnableEvaluator.ALL_BUT_START_NODE, | |
IMRelationshipTypes.THINKS, | |
Direction.BOTH | |
); | |
for (Node opinion : fredPhelpsOpinions) { | |
System.out.println("Fred Phelps thinks, \"" + opinion.getProperty("content") + "\""); | |
} | |
tx.success(); | |
} finally { | |
tx.finish(); | |
graphDb.shutdown(); | |
} | |
} | |
private static Relationship toMarriage(GraphDatabaseService gdb, Node firstPartner, Node secondPartner, String createdAt, String expiredAt) { | |
Relationship rel = firstPartner.createRelationshipTo(secondPartner, IMRelationshipTypes.MARRIED); | |
if (null != createdAt) { rel.setProperty("createdAt", createdAt); } | |
if (null != expiredAt) { rel.setProperty("expiredAt", expiredAt); } | |
return rel; | |
} | |
private static Relationship toFriendship(GraphDatabaseService gdb, Node firstFriend, Node secondFriend, String createdAt, String expiredAt) { | |
Relationship rel = firstFriend.createRelationshipTo(secondFriend, IMRelationshipTypes.KNOWS); | |
if (null != createdAt) { rel.setProperty("createdAt", createdAt); } | |
if (null != expiredAt) { rel.setProperty("expiredAt", expiredAt); } | |
return rel; | |
} | |
private static Node toOpinionNode(GraphDatabaseService gdb, String content) { | |
Node opinion = gdb.createNode(); | |
opinion.setProperty("content", content); | |
return opinion; | |
} | |
private static Node toPersonNode(GraphDatabaseService gdb, String name) { | |
Node person = gdb.createNode(); | |
person.setProperty("name", name); | |
return person; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment