Search in sources :

Example 46 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 47 with Direction

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

the class TestTraversal method testSanityChecks2.

@Test
public void testSanityChecks2() throws Exception {
    // ------------- with traverser direction -------------
    // Valid data
    Node root = getGraphDb().createNode();
    RelationshipType[] traversableRels = new RelationshipType[] { MyRelTypes.TEST };
    Direction[] traversableDirs = new Direction[] { Direction.OUTGOING };
    // Null traversable relationships
    this.sanityCheckTraverser("Sanity check failed: null traversable " + "rels should throw an " + "IllegalArgumentException", BREADTH_FIRST, root, null, traversableDirs[0], StopEvaluator.END_OF_GRAPH, ReturnableEvaluator.ALL);
    // Null traversable directions
    this.sanityCheckTraverser("Sanity check failed: null traversable " + "rels should throw an " + "IllegalArgumentException", BREADTH_FIRST, root, traversableRels[0], null, StopEvaluator.END_OF_GRAPH, ReturnableEvaluator.ALL);
    // Null stop evaluator
    this.sanityCheckTraverser("Sanity check failed: null stop eval " + "should throw an IllegalArgumentException", BREADTH_FIRST, root, traversableRels[0], traversableDirs[0], null, ReturnableEvaluator.ALL);
    // Null returnable evaluator
    this.sanityCheckTraverser("Sanity check failed: null returnable " + "evaluator should throw an " + "IllegalArgumentException", BREADTH_FIRST, root, traversableRels[0], traversableDirs[0], StopEvaluator.END_OF_GRAPH, null);
    // traversable relationships length not equal to traversable directions
    // length
    this.sanityCheckTraverser("Sanity check failed: null returnable " + "evaluator should throw an " + "IllegalArgumentException", BREADTH_FIRST, root, traversableRels[0], null, StopEvaluator.END_OF_GRAPH, null);
    this.sanityCheckTraverser("Sanity check failed: null returnable " + "evaluator should throw an " + "IllegalArgumentException", BREADTH_FIRST, root, null, traversableDirs[0], StopEvaluator.END_OF_GRAPH, null);
    root.delete();
}
Also used : Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test)

Example 48 with Direction

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

the class TestLoopRelationships method verifyRelationships.

private void verifyRelationships(String message, Node node, boolean[] loop, Relationship... relationships) {
    try (Transaction transaction = getGraphDb().beginTx()) {
        var root = transaction.getNodeById(node.getId());
        for (Direction dir : Direction.values()) {
            Set<Relationship> expected = new HashSet<>();
            for (int i = 0; i < relationships.length; i++) {
                if (relationships[i] != null && (dir != Direction.INCOMING || loop[i])) {
                    expected.add(relationships[i]);
                }
            }
            for (Relationship rel : root.getRelationships(dir)) {
                assertTrue(expected.remove(rel), message + ": unexpected relationship: " + rel);
            }
            assertTrue(expected.isEmpty(), message + ": expected relationships not seen " + expected);
        }
        transaction.commit();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) Direction(org.neo4j.graphdb.Direction) HashSet(java.util.HashSet)

Example 49 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 = tx.createNode();
    for (int i = 0; i < initialSize; i++) {
        me.createRelationshipTo(tx.createNode(), RelType.INITIAL);
    }
    newTransaction();
    me = tx.getNodeById(me.getId());
    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 = tx.createNode();
                me.createRelationshipTo(otherNode, spec.type);
            } else if (spec.dir == Direction.INCOMING) {
                otherNode = tx.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();
    me = tx.getNodeById(me.getId());
    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();
    me = tx.getNodeById(me.getId());
    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)

Example 50 with Direction

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

the class DijkstraDirectionTest method testDijkstraDirection2.

@Test
void testDijkstraDirection2() {
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdge(transaction, "a", "b");
        graph.makeEdge(transaction, "b", "c");
        graph.makeEdge(transaction, "c", "d");
        graph.makeEdge(transaction, "d", "a");
        graph.makeEdge(transaction, "s", "a");
        graph.makeEdge(transaction, "b", "s");
        graph.makeEdge(transaction, "e", "c");
        graph.makeEdge(transaction, "d", "e");
        Dijkstra<Double> dijkstra = new Dijkstra<>((double) 0, graph.getNode(transaction, "s"), graph.getNode(transaction, "e"), (relationship, direction) -> {
            assertEquals(Direction.OUTGOING, direction);
            return 1.0;
        }, new DoubleAdder(), Double::compareTo, Direction.OUTGOING, MyRelTypes.R1);
        dijkstra.getCost();
        dijkstra = new Dijkstra<>((double) 0, graph.getNode(transaction, "s"), graph.getNode(transaction, "e"), (relationship, direction) -> {
            assertEquals(Direction.INCOMING, direction);
            return 1.0;
        }, new DoubleAdder(), Double::compareTo, Direction.INCOMING, MyRelTypes.R1);
        dijkstra.getCost();
        transaction.commit();
    }
}
Also used : Test(org.junit.jupiter.api.Test) Dijkstra(org.neo4j.graphalgo.impl.shortestpath.Dijkstra) Relationship(org.neo4j.graphdb.Relationship) Map(java.util.Map) Direction(org.neo4j.graphdb.Direction) HashMap(java.util.HashMap) Neo4jAlgoTestCase(common.Neo4jAlgoTestCase) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CostEvaluator(org.neo4j.graphalgo.CostEvaluator) Transaction(org.neo4j.graphdb.Transaction) DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) Transaction(org.neo4j.graphdb.Transaction) Dijkstra(org.neo4j.graphalgo.impl.shortestpath.Dijkstra) Test(org.junit.jupiter.api.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