Search in sources :

Example 21 with Entity

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;
}
Also used : Entity(org.neo4j.graphdb.Entity) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) LinkedList(java.util.LinkedList)

Example 22 with Entity

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;
}
Also used : Entity(org.neo4j.graphdb.Entity) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) LinkedList(java.util.LinkedList)

Example 23 with Entity

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();
}
Also used : Entity(org.neo4j.graphdb.Entity) Test(org.junit.jupiter.api.Test)

Example 24 with Entity

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();
}
Also used : Entity(org.neo4j.graphdb.Entity) Test(org.junit.jupiter.api.Test)

Example 25 with Entity

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();
}
Also used : Entity(org.neo4j.graphdb.Entity) Test(org.junit.jupiter.api.Test)

Aggregations

Entity (org.neo4j.graphdb.Entity)33 Transaction (org.neo4j.graphdb.Transaction)12 Test (org.junit.jupiter.api.Test)10 Node (org.neo4j.graphdb.Node)10 Relationship (org.neo4j.graphdb.Relationship)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 MethodSource (org.junit.jupiter.params.provider.MethodSource)7 Path (org.neo4j.graphdb.Path)7 LinkedList (java.util.LinkedList)6 Map (java.util.Map)4 List (java.util.List)3 TraversalBranch (org.neo4j.graphdb.traversal.TraversalBranch)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Result (org.neo4j.graphdb.Result)2 Geometry (org.neo4j.graphdb.spatial.Geometry)2 Point (org.neo4j.graphdb.spatial.Point)2 Temporal (java.time.temporal.Temporal)1 TemporalAmount (java.time.temporal.TemporalAmount)1 ArrayList (java.util.ArrayList)1