Search in sources :

Example 1 with Direction

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

the class EigenvectorCentralityTest method shouldHandleTargetNodeBeingOrphan.

@Test
public void shouldHandleTargetNodeBeingOrphan() {
    graph.makeNode("o");
    EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.BOTH, new CostEvaluator<Double>() {

        @Override
        public Double getCost(Relationship relationship, Direction direction) {
            return 1d;
        }
    }, graph.getAllNodes(), graph.getAllEdges(), 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "o", 0d, 0.02);
}
Also used : Relationship(org.neo4j.graphdb.Relationship) EigenvectorCentrality(org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test)

Example 2 with Direction

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

the class EigenvectorCentralityTest method testWeightAndDirection.

/**
     * Same network as above, but with direction BOTH and weights in different
     * directions are given by a map.
     */
@Test
public void testWeightAndDirection() {
    graph.makeEdgeChain("a,b");
    graph.makeEdgeChain("b,c");
    graph.makeEdgeChain("c,d");
    graph.makeEdgeChain("c,a");
    final Map<String, Double> costs = new HashMap<String, Double>();
    costs.put("a,b", 1.0);
    costs.put("b,c", 1.0);
    costs.put("c,d", 1.0);
    costs.put("c,b", 0.1);
    costs.put("c,a", 0.1);
    EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.BOTH, new CostEvaluator<Double>() {

        public Double getCost(Relationship relationship, Direction direction) {
            String start = graph.getNodeId(relationship.getStartNode());
            String end = graph.getNodeId(relationship.getEndNode());
            if (direction == Direction.INCOMING) {
                // swap
                String tmp = end;
                end = start;
                start = tmp;
            }
            Double value = costs.get(start + "," + end);
            if (value == null) {
                return 0.0;
            }
            return value;
        }
    }, graph.getAllNodes(), graph.getAllEdges(), 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "a", 0.0851, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "b", 0.244, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "c", 0.456, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "d", 0.852, 0.02);
}
Also used : HashMap(java.util.HashMap) Relationship(org.neo4j.graphdb.Relationship) EigenvectorCentrality(org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test)

Example 3 with Direction

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

the class EigenvectorCentralityTest method testDirection.

/**
     * Same as above, but inverted direction.
     */
@Test
public void testDirection() {
    graph.makeEdgeChain("d,c,b,a");
    graph.makeEdges("a,b,a,c");
    EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.INCOMING, new CostEvaluator<Double>() {

        public Double getCost(Relationship relationship, Direction direction) {
            return 1.0;
        }
    }, graph.getAllNodes(), graph.getAllEdges(), 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "a", 0.693, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "b", 0.523, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "c", 0.395, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "d", 0.298, 0.02);
}
Also used : Relationship(org.neo4j.graphdb.Relationship) EigenvectorCentrality(org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test)

Example 4 with Direction

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

the class NodeRelationshipCacheTest method shouldPutRandomStuff.

@Test
public void shouldPutRandomStuff() throws Exception {
    // GIVEN
    int nodes = 10_000;
    PrimitiveLongObjectMap<long[]> key = Primitive.longObjectMap(nodes);
    cache = new NodeRelationshipCache(NumberArrayFactory.HEAP, 1, 1000, base);
    // mark random nodes as dense (dense node threshold is 1 so enough with one increment
    cache.setHighNodeId(nodes);
    for (long nodeId = 0; nodeId < nodes; nodeId++) {
        if (random.nextBoolean()) {
            cache.incrementCount(nodeId);
        }
    }
    // WHEN
    for (int i = 0; i < 100_000; i++) {
        long nodeId = random.nextLong(nodes);
        boolean dense = cache.isDense(nodeId);
        Direction direction = random.among(Direction.values());
        long relationshipId = random.nextLong(1_000_000);
        long previousHead = cache.getAndPutRelationship(nodeId, direction, relationshipId, false);
        long[] keyIds = key.get(nodeId);
        int keyIndex = dense ? direction.ordinal() : 0;
        if (keyIds == null) {
            key.put(nodeId, keyIds = minusOneLongs(Direction.values().length));
        }
        assertEquals(keyIds[keyIndex], previousHead);
        keyIds[keyIndex] = relationshipId;
    }
}
Also used : Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test)

Example 5 with Direction

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

the class NodeRelationshipCacheTest method shouldObserveFirstRelationshipAsEmptyInEachDirection.

@Test
public void shouldObserveFirstRelationshipAsEmptyInEachDirection() throws Exception {
    // GIVEN
    cache = new NodeRelationshipCache(NumberArrayFactory.AUTO, 1, 100, base);
    int nodes = 100;
    Direction[] directions = Direction.values();
    GroupVisitor groupVisitor = mock(GroupVisitor.class);
    cache.setForwardScan(true);
    cache.setHighNodeId(nodes + 1);
    for (int i = 0; i < nodes; i++) {
        assertEquals(-1L, cache.getFirstRel(nodes, groupVisitor));
        cache.incrementCount(i);
        long previous = cache.getAndPutRelationship(i, directions[i % directions.length], random.nextInt(1_000_000), true);
        assertEquals(-1L, previous);
    }
    // WHEN
    cache.setForwardScan(false);
    for (int i = 0; i < nodes; i++) {
        long previous = cache.getAndPutRelationship(i, directions[i % directions.length], random.nextInt(1_000_000), false);
        assertEquals(-1L, previous);
    }
    // THEN
    cache.setForwardScan(true);
    for (int i = 0; i < nodes; i++) {
        assertEquals(-1L, cache.getFirstRel(nodes, groupVisitor));
    }
}
Also used : GroupVisitor(org.neo4j.unsafe.impl.batchimport.cache.NodeRelationshipCache.GroupVisitor) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test)

Aggregations

Direction (org.neo4j.graphdb.Direction)56 Relationship (org.neo4j.graphdb.Relationship)28 Test (org.junit.Test)20 RelationshipType (org.neo4j.graphdb.RelationshipType)19 Node (org.neo4j.graphdb.Node)14 HashMap (java.util.HashMap)11 EigenvectorCentrality (org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality)10 Test (org.junit.jupiter.api.Test)8 Map (java.util.Map)7 Transaction (org.neo4j.graphdb.Transaction)7 ArrayList (java.util.ArrayList)6 Dijkstra (org.neo4j.graphalgo.impl.shortestpath.Dijkstra)6 Collection (java.util.Collection)5 HashSet (java.util.HashSet)5 DoubleAdder (org.neo4j.graphalgo.impl.util.DoubleAdder)5 List (java.util.List)4 Neo4jAlgoTestCase (common.Neo4jAlgoTestCase)3 EnumMap (java.util.EnumMap)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Stack (java.util.Stack)2