*/
@SuppressWarnings("boxing")
public static void main(String[] args) throws Exception {
/* Create or load the database content from a filesystem storage */
final GraphDatabaseService graphDatabase = new EmbeddedGraphDatabase(System.getProperty("user.dir") + "/neo4j-graphdb");
/* Add a JVM shutdown hook in order to stop cleanly the database */
Runtime.getRuntime().addShutdownHook(new Thread() {
/**
* Stop cleanly the database <br>
*
* {@inheritDoc}
*
* @see java.lang.Thread#run()
*/
@Override
public void run() {
System.out.print("\n==> Shutdown cleanly the database...");
graphDatabase.shutdown();
System.out.println("OK");
}
});
/* Add nodes (members in our sample) and relationships between them */
// Create graph database nodes using the database reference object
Node memberA = createNode(graphDatabase, "Dominique");
Node memberB = createNode(graphDatabase, "Tony");
Node memberC = createNode(graphDatabase, "Guillaume");
Node memberD = createNode(graphDatabase, "Sebastien");
// Link nodes between them by adding relationships
// ---Relationship owned by the MemberA node
Relationship r01 = createRelationship(graphDatabase, memberA, memberB, SocialNetworkRelationship.FRIEND);
Relationship r02 = createRelationship(graphDatabase, memberA, memberC, SocialNetworkRelationship.TEAMMATE);
// ---Relationship owned by the MemberB node
Relationship r03 = createRelationship(graphDatabase, memberB, memberC, SocialNetworkRelationship.TEAMMATE);
// ---Relationship owned by the MemberC node
Relationship r04 = createRelationship(graphDatabase, memberC, memberB, SocialNetworkRelationship.FRIEND);
// ---Relationship owned by the MemberD node
Relationship r05 = createRelationship(graphDatabase, memberD, memberC, SocialNetworkRelationship.FRIEND);
// Display relationship created above
System.out.println("*** MEMBERS RELATIONSHIPS CREATED ***");
displayRelationship(r01);
displayRelationship(r02);
displayRelationship(r03);
displayRelationship(r04);
displayRelationship(r05);
/* Perform selections on the graph database */
System.out.println("\n*** MEMBERS RELATIONSHIPS SELECTION ***");
// Use a transaction for selections because it's required by Neo4J...
Transaction transaction = graphDatabase.beginTx();
// Find a node according to a property value (search for Tony member)
System.out.println("** Find a node according to a property value (search for Tony member)");
Iterable<Node> members = graphDatabase.getAllNodes();
long tonyId = -1;
for (Node member : members) {
if (member.hasProperty("MemberName") && "Tony".equals(member.getProperty("MemberName"))) {
System.out.printf("Tony found, is node ID is %s\n", member.getId());
tonyId = member.getId();
}
}
// Get all relationships of a specific node (of Tony member in this
// case)
System.out.println("** Get all relationships of a specific node (of Tony member in this case)");
// --Get directory access to the node using is unique ID
Node tonyNode = graphDatabase.getNodeById(tonyId);
// --Get is relationships in both directions (from this node to another
// node (OUTGOING direction name) and another node to this node
// (INCOMING direction name))
Iterator<Relationship> relationships = tonyNode.getRelationships(Direction.BOTH).iterator();
while (relationships.hasNext()) {
displayRelationship(relationships.next());
}
// Use a traverser to traverse all specific relationships that ending to
// a specific node (our goal here is to find all teamate member of
// Guillaume)
System.out.println("** Use a traverser to traverse all specific relationships that ending to a specific node (our goal here is to find all teamate member of Guillaume)");
Node guillaumeNode = graphDatabase.getNodeById(3);
Traverser teammates = guillaumeNode.traverse(Order.BREADTH_FIRST, StopEvaluator.END_OF_GRAPH, ReturnableEvaluator.ALL_BUT_START_NODE, SocialNetworkRelationship.TEAMMATE, Direction.INCOMING);
for (Node teammateMember : teammates) {
System.out.printf("%s (NodeID=%s)\n", teammateMember.getProperty("MemberName"), teammateMember.getId());
}