Created
September 16, 2011 16:43
-
-
Save ryanswanstrom/1222515 to your computer and use it in GitHub Desktop.
Find a common ancestor with Neo4j, this is not highly optimized.
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
package neotest; | |
import hackerunderground.NanoTimer; | |
import java.util.Iterator; | |
import org.neo4j.graphdb.Direction; | |
import org.neo4j.graphdb.GraphDatabaseService; | |
import org.neo4j.graphdb.Node; | |
import org.neo4j.graphdb.Relationship; | |
import org.neo4j.graphdb.RelationshipType; | |
import org.neo4j.graphdb.Transaction; | |
import org.neo4j.graphdb.index.Index; | |
import org.neo4j.graphdb.index.IndexHits; | |
import org.neo4j.graphdb.index.IndexManager; | |
import org.neo4j.kernel.EmbeddedGraphDatabase; | |
/** | |
* This code creates a tree of Integers | |
* in a Neo4j graph database. Then it | |
* finds the common ancestor of 2 nodes. | |
* | |
*/ | |
public class Main { | |
private enum MyRelationshipTypes implements RelationshipType { | |
KNOWS, RULES | |
} | |
private GraphDatabaseService theGraph; | |
private static final String val = "value"; | |
private IndexManager index; | |
private void loadGraph() { | |
if (theGraph == null) { | |
theGraph = new EmbeddedGraphDatabase("C:/ryan/graphdb"); | |
this.index = theGraph.index(); | |
Index<Node> values = index.forNodes("values"); | |
Transaction tx = theGraph.beginTx(); | |
try { | |
Node node0 = theGraph.createNode(); | |
node0.setProperty(val, Integer.valueOf(30)); | |
Node node1 = theGraph.createNode(); | |
node1.setProperty(val, Integer.valueOf(8)); | |
Node node2 = theGraph.createNode(); | |
node2.setProperty(val, Integer.valueOf(52)); | |
node0.createRelationshipTo(node1, MyRelationshipTypes.RULES); | |
node0.createRelationshipTo(node2, MyRelationshipTypes.RULES); | |
Node node3 = theGraph.createNode(); | |
node3.setProperty(val, Integer.valueOf(3)); | |
Node node4 = theGraph.createNode(); | |
node4.setProperty(val, Integer.valueOf(20)); | |
node1.createRelationshipTo(node3, MyRelationshipTypes.RULES); | |
node1.createRelationshipTo(node4, MyRelationshipTypes.RULES); | |
Node node5 = theGraph.createNode(); | |
node5.setProperty(val, Integer.valueOf(10)); | |
Node node6 = theGraph.createNode(); | |
node6.setProperty(val, Integer.valueOf(29)); | |
node4.createRelationshipTo(node5, MyRelationshipTypes.RULES); | |
node4.createRelationshipTo(node6, MyRelationshipTypes.RULES); | |
// setup indexes for searching | |
values.add(node0, val, node0.getProperty(val)); | |
values.add(node1, val, node1.getProperty(val)); | |
values.add(node2, val, node2.getProperty(val)); | |
values.add(node3, val, node3.getProperty(val)); | |
values.add(node4, val, node4.getProperty(val)); | |
values.add(node5, val, node5.getProperty(val)); | |
values.add(node6, val, node6.getProperty(val)); | |
// find common ancestor of two nodes | |
int num1 = 3; | |
int num2 = 29; | |
IndexHits<Node> hits = values.get(val, Integer.valueOf(num1)); | |
Node nodeResult1 = hits.getSingle(); | |
hits = values.get(val, Integer.valueOf(num2)); | |
Node nodeResult2 = hits.getSingle(); | |
boolean found = false; | |
Iterable<Relationship> relations = nodeResult1.getRelationships(Direction.INCOMING, MyRelationshipTypes.RULES); | |
Iterator<Relationship> iter = relations.iterator(); | |
while (iter.hasNext() && !found) { | |
Relationship r = iter.next(); | |
System.out.println("r is " + r.getStartNode().getProperty(val)); | |
Iterable<Relationship> relations2 = nodeResult2.getRelationships(Direction.INCOMING, MyRelationshipTypes.RULES); | |
Iterator<Relationship> iter2 = relations2.iterator(); | |
while (iter2.hasNext() && !found) { | |
if (r.getStartNode().getProperty(val).equals(iter2.next().getStartNode().getProperty(val))) { | |
found = true; | |
System.out.println("ancestor is: " + r.getStartNode().getProperty(val)); | |
} | |
} | |
} | |
tx.success(); | |
} finally { | |
values.delete(); | |
tx.finish(); | |
theGraph.shutdown(); | |
} | |
} | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
NanoTimer timer = new NanoTimer(); | |
timer.start(); | |
Main main = new Main(); | |
main.loadGraph(); | |
timer.end(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment