Search in sources :

Example 36 with Direction

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

the class TransactionProvidingApp method toSortedExpander.

protected static PathExpander toSortedExpander(GraphDatabaseService db, Direction defaultDirection, Map<String, Object> relationshipTypes, boolean caseInsensitiveFilters, boolean looseFilters) throws ShellException {
    defaultDirection = defaultDirection != null ? defaultDirection : Direction.BOTH;
    Map<String, Direction> matches = filterMapToTypes(db, defaultDirection, relationshipTypes, caseInsensitiveFilters, looseFilters);
    PathExpanderBuilder expander = PathExpanderBuilder.emptyOrderedByType();
    for (Map.Entry<String, Direction> entry : matches.entrySet()) {
        expander = expander.add(RelationshipType.withName(entry.getKey()), entry.getValue());
    }
    return expander.build();
}
Also used : ShellException.stackTraceAsString(org.neo4j.shell.ShellException.stackTraceAsString) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) Direction(org.neo4j.graphdb.Direction) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 37 with Direction

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

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);
    // eigenvectorCentrality.setMaxIterations( 100 );
    assertApproximateCentrality(eigenvectorCentrality, "a", 0.0851, 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "b", 0.244, 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "c", 0.456, 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "d", 0.852, 0.01);
}
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 38 with Direction

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

the class EigenvectorCentralityTest method testRun.

@Test
public void testRun() {
    graph.makeEdgeChain("a,b,c,d");
    graph.makeEdges("b,a,c,a");
    EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.OUTGOING, new CostEvaluator<Double>() {

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

Example 39 with Direction

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

the class NodeRelationshipCacheTest method shouldGetAndPutRelationshipAroundChunkEdge.

@Test
public void shouldGetAndPutRelationshipAroundChunkEdge() throws Exception {
    // GIVEN
    cache = new NodeRelationshipCache(NumberArrayFactory.HEAP, 10);
    // WHEN
    long nodeId = 1_000_000 - 1;
    cache.setHighNodeId(nodeId + 1);
    Direction direction = Direction.OUTGOING;
    long relId = 10;
    cache.getAndPutRelationship(nodeId, direction, relId, false);
    // THEN
    assertEquals(relId, cache.getFirstRel(nodeId, mock(GroupVisitor.class)));
}
Also used : Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test)

Example 40 with Direction

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

the class TestRelationshipCount method ensureRightDegree.

private void ensureRightDegree(int initialSize, Collection<RelationshipCreationSpec> cspecs, Collection<RelationshipDeletionSpec> dspecs) {
    Map<RelType, int[]> expectedCounts = new EnumMap<>(RelType.class);
    for (RelType type : RelType.values()) {
        expectedCounts.put(type, new int[3]);
    }
    Node me = getGraphDb().createNode();
    for (int i = 0; i < initialSize; i++) {
        me.createRelationshipTo(getGraphDb().createNode(), RelType.INITIAL);
    }
    newTransaction();
    expectedCounts.get(RelType.INITIAL)[0] = initialSize;
    assertCounts(me, expectedCounts);
    int counter = 0;
    for (RelationshipCreationSpec spec : cspecs) {
        for (int i = 0; i < spec.count; i++) {
            Node otherNode = null;
            if (spec.dir == Direction.OUTGOING) {
                otherNode = getGraphDb().createNode();
                me.createRelationshipTo(otherNode, spec.type);
            } else if (spec.dir == Direction.INCOMING) {
                otherNode = getGraphDb().createNode();
                otherNode.createRelationshipTo(me, spec.type);
            } else {
                me.createRelationshipTo(me, spec.type);
            }
            expectedCounts.get(spec.type)[spec.dir.ordinal()]++;
            if (otherNode != null) {
                assertEquals(1, otherNode.getDegree());
            }
            assertCounts(me, expectedCounts);
            if (counter % 3 == 0 && counter > 0) {
                newTransaction();
                assertCounts(me, expectedCounts);
            }
        }
    }
    assertCounts(me, expectedCounts);
    newTransaction();
    assertCounts(me, expectedCounts);
    // Delete one of each type/direction combination
    counter = 0;
    if (dspecs == null) {
        for (RelType type : RelType.values()) {
            if (!type.measure) {
                continue;
            }
            for (Direction direction : Direction.values()) {
                int[] counts = expectedCounts.get(type);
                if (counts[direction.ordinal()] > 0) {
                    deleteOneRelationship(me, type, direction, 0);
                    counts[direction.ordinal()]--;
                    assertCounts(me, expectedCounts);
                    if (counter % 3 == 0 && counter > 0) {
                        newTransaction();
                        assertCounts(me, expectedCounts);
                    }
                }
            }
        }
    } else {
        for (RelationshipDeletionSpec spec : dspecs) {
            deleteOneRelationship(me, spec.type, spec.dir, spec.which);
            expectedCounts.get(spec.type)[spec.dir.ordinal()]--;
            assertCounts(me, expectedCounts);
            if (counter % 3 == 0 && counter > 0) {
                newTransaction();
                assertCounts(me, expectedCounts);
            }
        }
    }
    assertCounts(me, expectedCounts);
    newTransaction();
    assertCounts(me, expectedCounts);
    // Clean up
    for (Relationship rel : me.getRelationships()) {
        Node otherNode = rel.getOtherNode(me);
        if (!otherNode.equals(me)) {
            otherNode.delete();
        }
        rel.delete();
    }
    me.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) EnumMap(java.util.EnumMap) Direction(org.neo4j.graphdb.Direction)

Aggregations

Direction (org.neo4j.graphdb.Direction)42 Relationship (org.neo4j.graphdb.Relationship)25 Test (org.junit.Test)21 RelationshipType (org.neo4j.graphdb.RelationshipType)14 Node (org.neo4j.graphdb.Node)11 EigenvectorCentrality (org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality)10 HashMap (java.util.HashMap)6 Dijkstra (org.neo4j.graphalgo.impl.shortestpath.Dijkstra)6 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4 List (java.util.List)4 DoubleAdder (org.neo4j.graphalgo.impl.util.DoubleAdder)4 DoubleComparator (org.neo4j.graphalgo.impl.util.DoubleComparator)4 EnumMap (java.util.EnumMap)3 PropertyContainer (org.neo4j.graphdb.PropertyContainer)3 NestingIterator (org.neo4j.helpers.collection.NestingIterator)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Stack (java.util.Stack)2 TreeMap (java.util.TreeMap)2