Search in sources :

Example 31 with Direction

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

the class EigenvectorCentralityTest method simpleTest.

@Test
public void simpleTest() {
    /*
         * Layout
         *     ___________
         *   v             \
         *  (a) -> (b) -> (c) -> (d)
         *   ^     /
         *    ----
         */
    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.02);
    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 32 with Direction

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

the class Paths method descriptorForIdAndProperties.

/**
     * Create a new {@link Paths.PathDescriptor} that prints values of listed property keys
     * and id of nodes and relationships if configured so.
     * @param nodeId            true if node id should be included.
     * @param relId             true if relationship id should be included.
     * @param propertyKeys      all property keys that should be included.
     * @param <T>               the type of the {@link Path}
     * @return                  a new {@link Paths.PathDescriptor}
     */
public static <T extends Path> PathDescriptor<T> descriptorForIdAndProperties(final boolean nodeId, final boolean relId, final String... propertyKeys) {
    return new Paths.PathDescriptor<T>() {

        @Override
        public String nodeRepresentation(T path, Node node) {
            String representation = representation(node);
            return "(" + (nodeId ? node.getId() : "") + (nodeId && !representation.equals("") ? "," : "") + representation + ")";
        }

        private String representation(PropertyContainer entity) {
            StringBuilder builder = new StringBuilder();
            for (String key : propertyKeys) {
                Object value = entity.getProperty(key, null);
                if (value != null) {
                    if (builder.length() > 0) {
                        builder.append(",");
                    }
                    builder.append(value);
                }
            }
            return builder.toString();
        }

        @Override
        public String relationshipRepresentation(T path, Node from, Relationship relationship) {
            Direction direction = relationship.getEndNode().equals(from) ? Direction.INCOMING : Direction.OUTGOING;
            StringBuilder builder = new StringBuilder();
            if (direction.equals(Direction.INCOMING)) {
                builder.append("<");
            }
            builder.append("--[" + (relId ? relationship.getId() : ""));
            String representation = representation(relationship);
            if (relId && !representation.equals("")) {
                builder.append(",");
            }
            builder.append(representation);
            builder.append("]--");
            if (direction.equals(Direction.OUTGOING)) {
                builder.append(">");
            }
            return builder.toString();
        }
    };
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Direction(org.neo4j.graphdb.Direction)

Example 33 with Direction

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

the class StandardExpander method temporaryTypeMapFrom.

private static Map<Direction, Collection<RelationshipType>> temporaryTypeMapFrom(Map<Direction, RelationshipType[]> typeMap) {
    Map<Direction, Collection<RelationshipType>> map = new EnumMap<Direction, Collection<RelationshipType>>(Direction.class);
    for (Direction direction : Direction.values()) {
        ArrayList<RelationshipType> types = new ArrayList<RelationshipType>();
        map.put(direction, types);
        RelationshipType[] existing = typeMap.get(direction);
        if (existing != null) {
            types.addAll(asList(existing));
        }
    }
    return map;
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) ArrayList(java.util.ArrayList) Collection(java.util.Collection) EnumMap(java.util.EnumMap) Direction(org.neo4j.graphdb.Direction)

Example 34 with Direction

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

the class StandardExpander method create.

public static StandardExpander create(RelationshipType type1, Direction dir1, RelationshipType type2, Direction dir2, Object... more) {
    Map<Direction, Collection<RelationshipType>> tempMap = temporaryTypeMap();
    tempMap.get(dir1).add(type1);
    tempMap.get(dir2).add(type2);
    for (int i = 0; i < more.length; i++) {
        RelationshipType type = (RelationshipType) more[i++];
        Direction direction = (Direction) more[i];
        tempMap.get(direction).add(type);
    }
    return new RegularExpander(toTypeMap(tempMap));
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) Collection(java.util.Collection) Direction(org.neo4j.graphdb.Direction)

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

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