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();
}
};
}
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));
}
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;
}
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);
}
}
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));
}
Aggregations