Search in sources :

Example 16 with Direction

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

the class OldTraverserWrapper method toExpander.

private static RelationshipExpander toExpander(Object[] relationshipTypesAndDirections) {
    Stack<Object[]> entries = new Stack<Object[]>();
    for (int i = 0; i < relationshipTypesAndDirections.length; i += 2) {
        Object relType = relationshipTypesAndDirections[i];
        if (relType == null) {
            throw new IllegalArgumentException("Null relationship type at " + i);
        }
        if (!(relType instanceof RelationshipType)) {
            throw new IllegalArgumentException("Expected RelationshipType at var args pos " + i + ", found " + relType);
        }
        Object direction = relationshipTypesAndDirections[i + 1];
        if (direction == null) {
            throw new IllegalArgumentException("Null direction at " + (i + 1));
        }
        if (!(direction instanceof Direction)) {
            throw new IllegalArgumentException("Expected Direction at var args pos " + (i + 1) + ", found " + direction);
        }
        entries.push(new Object[] { relType, direction });
    }
    OrderedByTypeExpander expander = new OrderedByTypeExpander();
    while (!entries.isEmpty()) {
        Object[] entry = entries.pop();
        expander = (OrderedByTypeExpander) expander.add((RelationshipType) entry[0], (Direction) entry[1]);
    }
    return expander;
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) OrderedByTypeExpander(org.neo4j.kernel.OrderedByTypeExpander) Direction(org.neo4j.graphdb.Direction) Stack(java.util.Stack)

Example 17 with Direction

use of org.neo4j.graphdb.Direction in project neo4j-mobile-android by neo4j-contrib.

the class OldTraverserWrapper method toExpander.

private static RelationshipExpander toExpander(Object[] relationshipTypesAndDirections) {
    Stack<Object[]> entries = new Stack<Object[]>();
    for (int i = 0; i < relationshipTypesAndDirections.length; i += 2) {
        Object relType = relationshipTypesAndDirections[i];
        if (relType == null) {
            throw new IllegalArgumentException("Null relationship type at " + i);
        }
        if (!(relType instanceof RelationshipType)) {
            throw new IllegalArgumentException("Expected RelationshipType at var args pos " + i + ", found " + relType);
        }
        Object direction = relationshipTypesAndDirections[i + 1];
        if (direction == null) {
            throw new IllegalArgumentException("Null direction at " + (i + 1));
        }
        if (!(direction instanceof Direction)) {
            throw new IllegalArgumentException("Expected Direction at var args pos " + (i + 1) + ", found " + direction);
        }
        entries.push(new Object[] { relType, direction });
    }
    OrderedByTypeExpander expander = new OrderedByTypeExpander();
    while (!entries.isEmpty()) {
        Object[] entry = entries.pop();
        expander = (OrderedByTypeExpander) expander.add((RelationshipType) entry[0], (Direction) entry[1]);
    }
    return expander;
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) OrderedByTypeExpander(org.neo4j.kernel.OrderedByTypeExpander) Direction(org.neo4j.graphdb.Direction) Stack(java.util.Stack)

Example 18 with Direction

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

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 19 with Direction

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

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 20 with Direction

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

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);
    // 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)

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