Search in sources :

Example 6 with Direction

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

the class DijkstraMultiplePathsTest method test8.

@Test
public void test8() {
    Relationship edgeAB = graph.makeEdge("a", "b");
    Relationship edgeBC = graph.makeEdge("b", "c");
    Relationship edgeCD = graph.makeEdge("c", "d");
    Relationship edgeDE = graph.makeEdge("d", "e");
    Relationship edgeAB2 = graph.makeEdge("a", "b2");
    Relationship edgeB2C = graph.makeEdge("b2", "c");
    Relationship edgeCD2 = graph.makeEdge("c", "d2");
    Relationship edgeD2E = graph.makeEdge("d2", "e");
    Dijkstra<Double> dijkstra = new Dijkstra<Double>(0.0, graph.getNode("a"), graph.getNode("e"), new CostEvaluator<Double>() {

        public Double getCost(Relationship relationship, Direction direction) {
            return 1.0;
        }
    }, new DoubleAdder(), new DoubleComparator(), Direction.OUTGOING, MyRelTypes.R1);
    // path discovery flags
    boolean pathBD = false;
    boolean pathB2D = false;
    boolean pathBD2 = false;
    boolean pathB2D2 = false;
    List<List<Relationship>> paths = dijkstra.getPathsAsRelationships();
    assertTrue(paths.size() == 4);
    for (List<Relationship> path : paths) {
        assertTrue(path.size() == 4);
        // first choice
        if (path.get(0).equals(edgeAB)) {
            assertTrue(path.get(1).equals(edgeBC));
        } else {
            assertTrue(path.get(0).equals(edgeAB2));
            assertTrue(path.get(1).equals(edgeB2C));
        }
        // second choice
        if (path.get(2).equals(edgeCD)) {
            assertTrue(path.get(3).equals(edgeDE));
        } else {
            assertTrue(path.get(2).equals(edgeCD2));
            assertTrue(path.get(3).equals(edgeD2E));
        }
        // combinations
        if (path.get(0).equals(edgeAB)) {
            if (path.get(2).equals(edgeCD)) {
                pathBD = true;
            } else if (path.get(2).equals(edgeCD2)) {
                pathBD2 = true;
            }
        } else {
            if (path.get(2).equals(edgeCD)) {
                pathB2D = true;
            } else if (path.get(2).equals(edgeCD2)) {
                pathB2D2 = true;
            }
        }
    }
    assertTrue(pathBD);
    assertTrue(pathB2D);
    assertTrue(pathBD2);
    assertTrue(pathB2D2);
}
Also used : DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) Relationship(org.neo4j.graphdb.Relationship) DoubleComparator(org.neo4j.graphalgo.impl.util.DoubleComparator) List(java.util.List) Direction(org.neo4j.graphdb.Direction) Dijkstra(org.neo4j.graphalgo.impl.shortestpath.Dijkstra) Test(org.junit.Test)

Example 7 with Direction

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

the class DijkstraMultiplePathsTest method test7.

@Test
public void test7() {
    Relationship edgeAB = graph.makeEdge("a", "b");
    Relationship edgeBC = graph.makeEdge("b", "c");
    Relationship edgeCD = graph.makeEdge("c", "d");
    Relationship edgeDE = graph.makeEdge("d", "e");
    Relationship edgeAB2 = graph.makeEdge("a", "b2");
    Relationship edgeB2C = graph.makeEdge("b2", "c");
    Relationship edgeCD2 = graph.makeEdge("c", "d2");
    Relationship edgeD2E = graph.makeEdge("d2", "e");
    Dijkstra<Double> dijkstra = new Dijkstra<Double>(0.0, graph.getNode("a"), graph.getNode("e"), new CostEvaluator<Double>() {

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

Example 8 with Direction

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

the class OrderedByTypeExpander method doExpand.

@Override
Iterator<Relationship> doExpand(final Path path, BranchState state) {
    final Node node = path.endNode();
    return new NestingIterator<Relationship, Pair<RelationshipType, Direction>>(orderedTypes.iterator()) {

        @Override
        protected Iterator<Relationship> createNestedIterator(Pair<RelationshipType, Direction> entry) {
            RelationshipType type = entry.first();
            Direction dir = entry.other();
            return ((dir == Direction.BOTH) ? node.getRelationships(type) : node.getRelationships(type, dir)).iterator();
        }
    };
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) NestingIterator(org.neo4j.helpers.collection.NestingIterator) RelationshipType(org.neo4j.graphdb.RelationshipType) Direction(org.neo4j.graphdb.Direction) Pair(org.neo4j.helpers.collection.Pair)

Example 9 with Direction

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

the class TestLoopRelationships method canAddLoopRelationship.

@Test
public void canAddLoopRelationship() {
    Node node = getGraphDb().createNode();
    node.createRelationshipTo(node, TEST);
    newTransaction();
    for (Direction dir : Direction.values()) {
        int count = 0;
        for (Relationship rel : node.getRelationships(dir)) {
            count++;
            assertEquals("start node", node, rel.getStartNode());
            assertEquals("end node", node, rel.getEndNode());
            assertEquals("other node", node, rel.getOtherNode(node));
        }
        assertEquals(dir.name() + " relationship count", 1, count);
    }
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test)

Example 10 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)

Aggregations

Direction (org.neo4j.graphdb.Direction)41 Relationship (org.neo4j.graphdb.Relationship)24 Test (org.junit.Test)21 RelationshipType (org.neo4j.graphdb.RelationshipType)13 EigenvectorCentrality (org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality)10 Node (org.neo4j.graphdb.Node)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