Search in sources :

Example 1 with Entity

use of org.neo4j.graphdb.Entity in project neo4j by neo4j.

the class DijkstraMultiplePathsTest method test7.

@Test
void test7() {
    try (Transaction transaction = graphDb.beginTx()) {
        Relationship edgeAB = graph.makeEdge(transaction, "a", "b");
        Relationship edgeBC = graph.makeEdge(transaction, "b", "c");
        Relationship edgeCD = graph.makeEdge(transaction, "c", "d");
        Relationship edgeDE = graph.makeEdge(transaction, "d", "e");
        Relationship edgeAB2 = graph.makeEdge(transaction, "a", "b2");
        Relationship edgeB2C = graph.makeEdge(transaction, "b2", "c");
        Relationship edgeCD2 = graph.makeEdge(transaction, "c", "d2");
        Relationship edgeD2E = graph.makeEdge(transaction, "d2", "e");
        Dijkstra<Double> dijkstra = new Dijkstra<>(0.0, graph.getNode(transaction, "a"), graph.getNode(transaction, "e"), (relationship, direction) -> 1.0, new DoubleAdder(), Double::compareTo, Direction.OUTGOING, MyRelTypes.R1);
        // path discovery flags
        boolean pathBD = false;
        boolean pathB2D = false;
        boolean pathBD2 = false;
        boolean pathB2D2 = false;
        List<List<Entity>> paths = dijkstra.getPaths();
        assertEquals(4, paths.size());
        for (List<Entity> path : paths) {
            assertEquals(9, path.size());
            assertEquals(path.get(0), graph.getNode(transaction, "a"));
            assertEquals(path.get(4), graph.getNode(transaction, "c"));
            assertEquals(path.get(8), graph.getNode(transaction, "e"));
            // first choice
            if (path.get(2).equals(graph.getNode(transaction, "b"))) {
                assertEquals(path.get(1), edgeAB);
                assertEquals(path.get(3), edgeBC);
            } else {
                assertEquals(path.get(1), edgeAB2);
                assertEquals(path.get(2), graph.getNode(transaction, "b2"));
                assertEquals(path.get(3), edgeB2C);
            }
            // second choice
            if (path.get(6).equals(graph.getNode(transaction, "d"))) {
                assertEquals(path.get(5), edgeCD);
                assertEquals(path.get(7), edgeDE);
            } else {
                assertEquals(path.get(5), edgeCD2);
                assertEquals(path.get(6), graph.getNode(transaction, "d2"));
                assertEquals(path.get(7), edgeD2E);
            }
            // combinations
            if (path.get(2).equals(graph.getNode(transaction, "b"))) {
                if (path.get(6).equals(graph.getNode(transaction, "d"))) {
                    pathBD = true;
                } else if (path.get(6).equals(graph.getNode(transaction, "d2"))) {
                    pathBD2 = true;
                }
            } else {
                if (path.get(6).equals(graph.getNode(transaction, "d"))) {
                    pathB2D = true;
                } else if (path.get(6).equals(graph.getNode(transaction, "d2"))) {
                    pathB2D2 = true;
                }
            }
        }
        assertTrue(pathBD);
        assertTrue(pathB2D);
        assertTrue(pathBD2);
        assertTrue(pathB2D2);
        transaction.commit();
    }
}
Also used : Entity(org.neo4j.graphdb.Entity) DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) List(java.util.List) Dijkstra(org.neo4j.graphalgo.impl.shortestpath.Dijkstra) Test(org.junit.jupiter.api.Test)

Example 2 with Entity

use of org.neo4j.graphdb.Entity in project neo4j by neo4j.

the class FulltextProceduresTestSupport method assertQueryFindsIds.

static void assertQueryFindsIds(GraphDatabaseService db, boolean queryNodes, String index, String query, LongHashSet ids) {
    // Create a defensive copy, because we're going to modify this instance.
    ids = new LongHashSet(ids);
    String queryCall = queryNodes ? QUERY_NODES : QUERY_RELS;
    long[] expectedIds = ids.toArray();
    MutableLongSet actualIds = new LongHashSet();
    try (Transaction tx = db.beginTx()) {
        LongFunction<Entity> getEntity = queryNodes ? tx::getNodeById : tx::getRelationshipById;
        Result result = tx.execute(format(queryCall, index, query));
        Double score = Double.MAX_VALUE;
        while (result.hasNext()) {
            Map<String, Object> entry = result.next();
            long nextId = ((Entity) entry.get(queryNodes ? NODE : RELATIONSHIP)).getId();
            Double nextScore = (Double) entry.get(SCORE);
            assertThat(nextScore).isLessThanOrEqualTo(score);
            score = nextScore;
            actualIds.add(nextId);
            if (!ids.remove(nextId)) {
                String msg = "This id was not expected: " + nextId;
                failQuery(getEntity, index, query, ids, expectedIds, actualIds, msg);
            }
        }
        if (!ids.isEmpty()) {
            String msg = "Not all expected ids were found: " + ids;
            failQuery(getEntity, index, query, ids, expectedIds, actualIds, msg);
        }
        tx.commit();
    }
}
Also used : Entity(org.neo4j.graphdb.Entity) Result(org.neo4j.graphdb.Result) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) Transaction(org.neo4j.graphdb.Transaction)

Example 3 with Entity

use of org.neo4j.graphdb.Entity in project neo4j by neo4j.

the class FulltextIndexTransactionStateTest method queryResultsMustIncludeOldPropertyValuesWhenRemovalsAreUndone.

@MethodSource("entityTypeProvider")
@ParameterizedTest
void queryResultsMustIncludeOldPropertyValuesWhenRemovalsAreUndone(EntityUtil entityUtil) {
    createIndexAndWait(entityUtil);
    long entityId;
    try (Transaction tx = db.beginTx()) {
        entityId = entityUtil.createEntityWithProperty(tx, "primo");
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        entityUtil.assertQueryFindsIdsInOrder(tx, "primo", entityId);
        Entity entity = entityUtil.getEntity(tx, entityId);
        entity.removeProperty(PROP);
        entityUtil.assertQueryFindsIdsInOrder(tx, "primo");
        entity.setProperty(PROP, "primo");
        entityUtil.assertQueryFindsIdsInOrder(tx, "primo", entityId);
        tx.commit();
    }
}
Also used : Entity(org.neo4j.graphdb.Entity) Transaction(org.neo4j.graphdb.Transaction) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with Entity

use of org.neo4j.graphdb.Entity in project neo4j by neo4j.

the class Neo4jJsonCodecTest method testIteratorWriting.

@Test
void testIteratorWriting() throws IOException {
    // Given
    Entity entity = mock(Entity.class);
    when(entity.getAllProperties()).thenThrow(RuntimeException.class);
    // When
    assertThrows(Exception.class, () -> jsonCodec.writeValue(jsonGenerator, Collections.singletonList(entity)));
    // Then
    verify(jsonGenerator).writeEndArray();
}
Also used : Entity(org.neo4j.graphdb.Entity) Test(org.junit.jupiter.api.Test)

Example 5 with Entity

use of org.neo4j.graphdb.Entity in project neo4j by neo4j.

the class Neo4jJsonCodecTest method testPathWriting.

@Test
void testPathWriting() throws IOException {
    // Given
    Path path = mock(Path.class);
    Entity entity = mock(Entity.class);
    when(entity.getAllProperties()).thenThrow(RuntimeException.class);
    when(path.iterator()).thenReturn(Arrays.asList(entity).listIterator());
    // When
    assertThrows(Exception.class, () -> jsonCodec.writeValue(jsonGenerator, path));
    // Then
    verify(jsonGenerator).writeEndArray();
}
Also used : Path(org.neo4j.graphdb.Path) 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