use of org.neo4j.graphdb.Entity in project neo4j by neo4j.
the class Util method constructSinglePathToNode.
/**
* Constructs a path to a given node, for a given set of predecessors. The
* result is a list of alternating Node/Relationship.
* @param node
* The start node
* @param predecessors
* The predecessors set
* @param includeNode
* Boolean which determines if the start node should be included
* in the paths
* @param backwards
* Boolean, if true the order of the nodes in the paths will be
* reversed
* @return A path as a list of alternating Node/Relationship.
*/
public static List<Entity> constructSinglePathToNode(Node node, Map<Node, List<Relationship>> predecessors, boolean includeNode, boolean backwards) {
LinkedList<Entity> path = new LinkedList<>();
if (includeNode) {
if (backwards) {
path.addLast(node);
} else {
path.addFirst(node);
}
}
Node currentNode = node;
List<Relationship> currentPreds = predecessors.get(currentNode);
// Traverse predecessors until we have added a node without predecessors
while (currentPreds != null && !currentPreds.isEmpty()) {
// Get next node
Relationship currentRelationship = currentPreds.get(0);
currentNode = currentRelationship.getOtherNode(currentNode);
// Add current
if (backwards) {
path.addLast(currentRelationship);
path.addLast(currentNode);
} else {
path.addFirst(currentRelationship);
path.addFirst(currentNode);
}
// Continue with the next node
currentPreds = predecessors.get(currentNode);
}
return path;
}
use of org.neo4j.graphdb.Entity in project neo4j by neo4j.
the class Util method constructAllPathsToNodeAsLinkedLists.
/**
* Same as constructAllPathsToNode, but different return type
*/
protected static List<LinkedList<Entity>> constructAllPathsToNodeAsLinkedLists(Node node, Map<Node, List<Relationship>> predecessors, boolean includeNode, boolean backwards) {
List<LinkedList<Entity>> paths = new LinkedList<>();
List<Relationship> current = predecessors.get(node);
// First build all paths to this node's predecessors
if (current != null) {
for (Relationship r : current) {
Node n = r.getOtherNode(node);
List<LinkedList<Entity>> newPaths = constructAllPathsToNodeAsLinkedLists(n, predecessors, true, backwards);
paths.addAll(newPaths);
// Add the relationship
for (LinkedList<Entity> path : newPaths) {
if (backwards) {
path.addFirst(r);
} else {
path.addLast(r);
}
}
}
}
// have this node added to it)
if (paths.isEmpty()) {
paths.add(new LinkedList<>());
}
// Then add this node to all those paths
if (includeNode) {
for (LinkedList<Entity> path : paths) {
if (backwards) {
path.addFirst(node);
} else {
path.addLast(node);
}
}
}
return paths;
}
use of org.neo4j.graphdb.Entity in project neo4j by neo4j.
the class Neo4jJsonCodecTest method testNodeWriting.
@Test
void testNodeWriting() throws IOException {
// Given
Entity node = mock(Node.class);
when(node.getAllProperties()).thenThrow(RuntimeException.class);
// When
assertThrows(RuntimeException.class, () -> jsonCodec.writeValue(jsonGenerator, node));
// Then
verify(jsonGenerator).writeEndObject();
}
use of org.neo4j.graphdb.Entity in project neo4j by neo4j.
the class Neo4jJsonCodecTest method testRelationshipWriting.
@Test
void testRelationshipWriting() throws IOException {
// Given
Entity relationship = mock(Relationship.class);
when(relationship.getAllProperties()).thenThrow(RuntimeException.class);
// When
assertThrows(RuntimeException.class, () -> jsonCodec.writeValue(jsonGenerator, relationship));
// Then
verify(jsonGenerator).writeEndObject();
}
use of org.neo4j.graphdb.Entity in project neo4j by neo4j.
the class Neo4jJsonCodecTest method testEntityWriting.
@Test
void testEntityWriting() throws IOException {
// Given
Entity entity = mock(Entity.class);
when(entity.getAllProperties()).thenThrow(RuntimeException.class);
var e = assertThrows(IllegalArgumentException.class, () -> jsonCodec.writeValue(jsonGenerator, entity));
verify(jsonGenerator, never()).writeEndObject();
}
Aggregations