Search in sources :

Example 51 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<>() {

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

        private String representation(Entity 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 : Entity(org.neo4j.graphdb.Entity) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Direction(org.neo4j.graphdb.Direction)

Example 52 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 53 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.class);
    for (Direction direction : Direction.values()) {
        List<RelationshipType> types = new ArrayList<>();
        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) Direction(org.neo4j.graphdb.Direction) EnumMap(java.util.EnumMap)

Example 54 with Direction

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

the class CachingExpandInto method connectingRelationships.

/**
 * Creates a cursor for all connecting relationships given a first and a second node.
 *
 * @param nodeCursor Node cursor used in traversal
 * @param traversalCursor Traversal cursor used in traversal
 * @param firstNode The first node
 * @param types The relationship types to traverse
 * @param secondNode The second node
 * @return The interconnecting relationships in the given direction with any of the given types.
 */
public RelationshipTraversalCursor connectingRelationships(NodeCursor nodeCursor, RelationshipTraversalCursor traversalCursor, long firstNode, int[] types, long secondNode) {
    Iterator<Relationship> connections = relationshipCache.get(firstNode, secondNode, direction);
    if (connections != null) {
        return new FromCachedSelectionCursor(connections, read, firstNode, secondNode);
    }
    Direction reverseDirection = direction.reverse();
    // Check secondNode, will position nodeCursor at secondNode
    int secondDegree = degreeCache.getIfAbsentPut(secondNode, () -> calculateTotalDegreeIfCheap(read, secondNode, nodeCursor, reverseDirection, types));
    if (secondDegree == 0) {
        return Cursors.emptyTraversalCursor(read);
    }
    boolean secondNodeHasCheapDegrees = secondDegree != EXPENSIVE_DEGREE;
    // Check firstNode, note that nodeCursor is now pointing at firstNode
    if (!singleNode(read, nodeCursor, firstNode)) {
        return Cursors.emptyTraversalCursor(read);
    }
    boolean firstNodeHasCheapDegrees = nodeCursor.supportsFastDegreeLookup();
    // Both can determine degree cheaply, start with the one with the lesser degree
    if (firstNodeHasCheapDegrees && secondNodeHasCheapDegrees) {
        // Note that we have already position the cursor at firstNode
        int firstDegree = degreeCache.getIfAbsentPut(firstNode, () -> calculateTotalDegree(nodeCursor, direction, types));
        long toNode;
        Direction relDirection;
        if (firstDegree < secondDegree) {
            // Everything is correctly positioned
            toNode = secondNode;
            relDirection = direction;
        } else {
            // cursor is already pointing at firstNode
            singleNode(read, nodeCursor, secondNode);
            toNode = firstNode;
            relDirection = reverseDirection;
        }
        return connectingRelationshipsCursor(relationshipsCursor(traversalCursor, nodeCursor, types, relDirection), toNode, firstNode, secondNode);
    } else if (secondNodeHasCheapDegrees) {
        long toNode = secondNode;
        return connectingRelationshipsCursor(relationshipsCursor(traversalCursor, nodeCursor, types, direction), toNode, firstNode, secondNode);
    } else if (firstNodeHasCheapDegrees) {
        // must move to secondNode
        singleNode(read, nodeCursor, secondNode);
        long toNode = firstNode;
        return connectingRelationshipsCursor(relationshipsCursor(traversalCursor, nodeCursor, types, reverseDirection), toNode, firstNode, secondNode);
    } else {
        // Both are sparse
        long toNode = secondNode;
        return connectingRelationshipsCursor(relationshipsCursor(traversalCursor, nodeCursor, types, direction), toNode, firstNode, secondNode);
    }
}
Also used : Direction(org.neo4j.graphdb.Direction)

Example 55 with Direction

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

the class StandardExpander method create.

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)

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