Micha Kops avatar Micha Kops committed bff0a10

Node filtering/traversal added.

Comments (0)

Files changed (3)

 	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
 	<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
 	<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
 	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
 	<classpathentry kind="output" path="target/classes"/>
 </classpath>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-  <modelVersion>4.0.0</modelVersion>
-  <groupId>com.hascode.tutorial</groupId>
-  <artifactId>neo4j-tutorial</artifactId>
-  <version>0.0.1-SNAPSHOT</version>
-  <dependencies>
-  	<dependency>
-  		<groupId>org.neo4j</groupId>
-  		<artifactId>neo4j</artifactId>
-  		<version>1.5</version>
-  	</dependency>
-  </dependencies>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>com.hascode.tutorial</groupId>
+	<artifactId>neo4j-tutorial</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+	
+	<dependencies>
+		<dependency>
+			<groupId>org.neo4j</groupId>
+			<artifactId>neo4j</artifactId>
+			<version>1.5</version>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<configuration>
+					<source>1.6</source>
+					<target>1.6</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
 </project>

src/main/java/com/hascode/tutorial/FilteredNodeTraversalExample.java

+package com.hascode.tutorial;
+
+import org.neo4j.graphdb.Direction;
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.graphdb.Node;
+import org.neo4j.graphdb.Relationship;
+import org.neo4j.graphdb.ReturnableEvaluator;
+import org.neo4j.graphdb.StopEvaluator;
+import org.neo4j.graphdb.Transaction;
+import org.neo4j.graphdb.TraversalPosition;
+import org.neo4j.graphdb.Traverser;
+import org.neo4j.graphdb.Traverser.Order;
+import org.neo4j.kernel.EmbeddedGraphDatabase;
+
+public class FilteredNodeTraversalExample {
+	private static String DB_PATH = "/tmp/neo4j";
+
+	public static void main(final String[] args) {
+		GraphDatabaseService graphDb = new EmbeddedGraphDatabase(DB_PATH);
+		registerShutdownHook(graphDb);
+
+		Transaction tx = graphDb.beginTx();
+		try {
+			Node peterNode = graphDb.createNode();
+			peterNode.setProperty("id", 1);
+			peterNode.setProperty("name", "Peter");
+
+			Node rayNode = graphDb.createNode();
+			rayNode.setProperty("id", 2);
+			rayNode.setProperty("name", "Ray");
+
+			Node egonNode = graphDb.createNode();
+			egonNode.setProperty("id", 3);
+			egonNode.setProperty("name", "Egon");
+
+			Node winstonNode = graphDb.createNode();
+			winstonNode.setProperty("id", 4);
+			winstonNode.setProperty("name", "Winston");
+
+			Node slimerNode = graphDb.createNode();
+			slimerNode.setProperty("id", 5);
+			slimerNode.setProperty("name", "Slimer");
+
+			Relationship rel1 = peterNode.createRelationshipTo(rayNode,
+					RelTypes.KNOWS);
+			rel1.setProperty("visibility", "public");
+
+			Relationship rel2 = rayNode.createRelationshipTo(egonNode,
+					RelTypes.KNOWS);
+			rel2.setProperty("visibility", "hidden");
+
+			Relationship rel3 = rayNode.createRelationshipTo(winstonNode,
+					RelTypes.KNOWS);
+			rel3.setProperty("visibility", "public");
+
+			Relationship rel4 = winstonNode.createRelationshipTo(slimerNode,
+					RelTypes.KNOWS);
+			rel4.setProperty("visibility", "public");
+
+			tx.success();
+
+			System.out
+					.println("traversing nodes for Peter's public acquaintances..");
+			Traverser acquaintanceTraverser = getAcquaintances(peterNode);
+			for (Node acquaintanceNode : acquaintanceTraverser.getAllNodes()) {
+				System.out.println("Peter knows "
+						+ acquaintanceNode.getProperty("name") + " (id: "
+						+ acquaintanceNode.getProperty("id") + ") at depth: "
+						+ acquaintanceTraverser.currentPosition().depth());
+			}
+		} finally {
+			tx.finish();
+		}
+
+		graphDb.shutdown();
+	}
+
+	private static Traverser getAcquaintances(final Node personNode) {
+		return personNode.traverse(Order.BREADTH_FIRST,
+				StopEvaluator.END_OF_GRAPH, new ReturnableEvaluator() {
+					@Override
+					public boolean isReturnableNode(
+							final TraversalPosition currentPos) {
+						return !currentPos.isStartNode()
+								&& currentPos.lastRelationshipTraversed()
+										.hasProperty("visibility")
+								&& "public".equals(currentPos
+										.lastRelationshipTraversed()
+										.getProperty("visibility"));
+					}
+				}, RelTypes.KNOWS, Direction.OUTGOING);
+	}
+
+	private static void registerShutdownHook(final GraphDatabaseService graphDb) {
+		// Registers a shutdown hook for the Neo4j instance so that it
+		// shuts down nicely when the VM exits (even if you "Ctrl-C" the
+		// running example before it's completed)
+		Runtime.getRuntime().addShutdownHook(new Thread() {
+			@Override
+			public void run() {
+				graphDb.shutdown();
+			}
+		});
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.