Created
May 16, 2012 14:13
-
-
Save FaKod/2710656 to your computer and use it in GitHub Desktop.
NullPointerException @ RestRelationship
This file contains hidden or 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
public class RestTraversalMain { | |
/** | |
* running neo4j community 1.5.2 | |
* Matrix Example -> Node 391 is Neo | |
* <p/> | |
* Throws Exception: | |
* <p/> | |
* Exception in thread "main" java.lang.NullPointerException | |
* at org.neo4j.rest.graphdb.entity.RestEntity.<init>(RestEntity.java:59) | |
* at org.neo4j.rest.graphdb.entity.RestRelationship.<init>(RestRelationship.java:44) | |
* at org.neo4j.rest.graphdb.traversal.RestPathParser.parseFullPath(RestPathParser.java:87) | |
* at org.neo4j.rest.graphdb.traversal.RestPathParser.parse(RestPathParser.java:54) | |
* at org.neo4j.rest.graphdb.traversal.RestTraverser.parseToPaths(RestTraverser.java:48) | |
* at org.neo4j.rest.graphdb.traversal.RestTraverser.<init>(RestTraverser.java:41) | |
* at org.neo4j.rest.graphdb.traversal.RestTraversal.traverse(RestTraversal.java:179) | |
* at org.neo4j.rest.graphdb.RestTraversalMain.main(RestTraversalMain.java:66) | |
*/ | |
public static void main(String[] args) { | |
Object[] rels = {DynamicRelationshipType.withName("KNOWS"), Direction.BOTH, | |
DynamicRelationshipType.withName("CODED_BY"), Direction.OUTGOING}; | |
RestGraphDatabase gd = new RestGraphDatabase("http://localhost:7474/db/data/"); | |
Node nodeNeo = gd.getNodeById(391); | |
RestTraversal rt = getRestTraversal(Traverser.Order.BREADTH_FIRST, rels); | |
rt.maxDepth(100); | |
rt.filter(Traversal.returnAll()); | |
TraverserImpl result = new TraverserImpl(); | |
result.iter = rt.traverse(nodeNeo).iterator(); | |
} | |
private static RestTraversal getRestTraversal(Traverser.Order order, Object... rels) { | |
Stack<Object[]> entries = calcRels(rels); | |
RestTraversal rt = new RestTraversal(); | |
switch (order) { | |
case BREADTH_FIRST: | |
rt.breadthFirst(); | |
break; | |
case DEPTH_FIRST: | |
rt.depthFirst(); | |
break; | |
} | |
// writing down relations | |
while (!entries.isEmpty()) { | |
Object[] entry = entries.pop(); | |
rt.relationships((RelationshipType) entry[0], (Direction) entry[1]); | |
} | |
return rt; | |
} | |
private static Stack<Object[]> calcRels(Object... rels) { | |
if (rels.length % 2 != 0 || rels.length == 0) { | |
throw new IllegalArgumentException(); | |
} | |
Stack<Object[]> entries = new Stack<Object[]>(); | |
for (int i = 0; i < rels.length; i += 2) { | |
Object relType = rels[i]; | |
if (relType == null) { | |
throw new IllegalArgumentException( | |
"Null relationship type at " + i); | |
} | |
if (!(relType instanceof RelationshipType)) { | |
throw new IllegalArgumentException( | |
"Expected RelationshipType at var args pos " + i | |
+ ", found " + relType); | |
} | |
Object direction = rels[i + 1]; | |
if (direction == null) { | |
throw new IllegalArgumentException( | |
"Null direction at " + (i + 1)); | |
} | |
if (!(direction instanceof Direction)) { | |
throw new IllegalArgumentException( | |
"Expected Direction at var args pos " + (i + 1) | |
+ ", found " + direction); | |
} | |
entries.push(new Object[]{relType, direction}); | |
} | |
return entries; | |
} | |
private static class Filter implements Predicate<Path> { | |
private final TraverserImpl traverser; | |
private final ReturnableEvaluator evaluator; | |
Filter(TraverserImpl traverser, ReturnableEvaluator returnableEvaluator) { | |
this.traverser = traverser; | |
this.evaluator = returnableEvaluator; | |
} | |
public boolean accept(Path position) { | |
return evaluator.isReturnableNode(new PositionImpl(traverser, | |
position)); | |
} | |
} | |
private static class TraverserImpl implements org.neo4j.graphdb.Traverser, Iterator<Node> { | |
private TraversalPosition currentPos; | |
private Path nextPath; | |
private Iterator<Path> iter; | |
private int count; | |
private Filter filter; | |
public TraversalPosition currentPosition() { | |
return currentPos; | |
} | |
public Collection<Node> getAllNodes() { | |
List<Node> result = new ArrayList<Node>(); | |
for (Node node : this) { | |
result.add(node); | |
} | |
return result; | |
} | |
public Iterator<Node> iterator() { | |
return this; | |
} | |
public boolean hasNext() { | |
while (iter.hasNext()) { | |
Path path = iter.next(); | |
if (filter == null || filter.accept(path)) { | |
nextPath = path; | |
return true; | |
} | |
} | |
nextPath = null; | |
return false; | |
} | |
public Node next() { | |
currentPos = new PositionImpl(this, nextPath); | |
count++; | |
return currentPos.currentNode(); | |
} | |
public void remove() { | |
throw new UnsupportedOperationException(); | |
} | |
} | |
private static class PositionImpl implements TraversalPosition { | |
private final Path position; | |
private final int count; | |
PositionImpl(TraverserImpl traverser, Path position) { | |
this.position = position; | |
this.count = traverser.count; | |
} | |
public Node currentNode() { | |
return position.endNode(); | |
} | |
public int depth() { | |
return position.length(); | |
} | |
public boolean isStartNode() { | |
return position.length() == 0; | |
} | |
public boolean notStartNode() { | |
return !isStartNode(); | |
} | |
public Relationship lastRelationshipTraversed() { | |
return position.lastRelationship(); | |
} | |
public Node previousNode() { | |
return position.lastRelationship().getOtherNode(position.endNode()); | |
} | |
public int returnedNodesCount() { | |
return count; | |
} | |
} | |
} |
That's a zero length path (just one node) so the rels is empty.
just had a look, this is just a bug in RestPathParser, I fix it in snapshot
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Printing out the path of method RestPathParser.parseFullPath(Map path, final RestAPI restApi)
-> relationships is an empty array
Key start {outgoing_relationships=http://localhost:7474/db/data/node/391/relationships/out, data={CLASS=org.neo4j.scala.Test_Matrix, profession=Hacker, name=Neo}, traverse=http://localhost:7474/db/data/node/391/traverse/{returnType}, all_typed_relationships=http://localhost:7474/db/data/node/391/relationships/all/{-list|&|types}, property=http://localhost:7474/db/data/node/391/properties/{key}, self=http://localhost:7474/db/data/node/391, properties=http://localhost:7474/db/data/node/391/properties, outgoing_typed_relationships=http://localhost:7474/db/data/node/391/relationships/out/{-list|&|types}, incoming_relationships=http://localhost:7474/db/data/node/391/relationships/in, extensions={}, create_relationship=http://localhost:7474/db/data/node/391/relationships, paged_traverse=http://localhost:7474/db/data/node/391/paged/traverse/{returnType}{?pageSize,leaseTime}, all_relationships=http://localhost:7474/db/data/node/391/relationships/all, incoming_typed_relationships=http://localhost:7474/db/data/node/391/relationships/in/{-list|&|types}} :
Key nodes [{outgoing_relationships=http://localhost:7474/db/data/node/391/relationships/out, data={CLASS=org.neo4j.scala.Test_Matrix, profession=Hacker, name=Neo}, traverse=http://localhost:7474/db/data/node/391/traverse/{returnType}, all_typed_relationships=http://localhost:7474/db/data/node/391/relationships/all/{-list|&|types}, property=http://localhost:7474/db/data/node/391/properties/{key}, self=http://localhost:7474/db/data/node/391, properties=http://localhost:7474/db/data/node/391/properties, outgoing_typed_relationships=http://localhost:7474/db/data/node/391/relationships/out/{-list|&|types}, incoming_relationships=http://localhost:7474/db/data/node/391/relationships/in, extensions={}, create_relationship=http://localhost:7474/db/data/node/391/relationships, paged_traverse=http://localhost:7474/db/data/node/391/paged/traverse/{returnType}{?pageSize,leaseTime}, all_relationships=http://localhost:7474/db/data/node/391/relationships/all, incoming_typed_relationships=http://localhost:7474/db/data/node/391/relationships/in/{-list|&|types}}] :
Key length 0 :
Key relationships [] :
Key end {outgoing_relationships=http://localhost:7474/db/data/node/391/relationships/out, data={CLASS=org.neo4j.scala.Test_Matrix, profession=Hacker, name=Neo}, traverse=http://localhost:7474/db/data/node/391/traverse/{returnType}, all_typed_relationships=http://localhost:7474/db/data/node/391/relationships/all/{-list|&|types}, property=http://localhost:7474/db/data/node/391/properties/{key}, self=http://localhost:7474/db/data/node/391, properties=http://localhost:7474/db/data/node/391/properties, outgoing_typed_relationships=http://localhost:7474/db/data/node/391/relationships/out/{-list|&|types}, incoming_relationships=http://localhost:7474/db/data/node/391/relationships/in, extensions={}, create_relationship=http://localhost:7474/db/data/node/391/relationships, paged_traverse=http://localhost:7474/db/data/node/391/paged/traverse/{returnType}{?pageSize,leaseTime}, all_relationships=http://localhost:7474/db/data/node/391/relationships/all, incoming_typed_relationships=http://localhost:7474/db/data/node/391/relationships/in/{-list|&|types}} :