use of org.neo4j.graphdb.traversal.TraversalDescription in project graphdb by neo4j-attic.
the class OldTraverserWrapper method traverse.
public static org.neo4j.graphdb.Traverser traverse(Node node, Order traversalOrder, StopEvaluator stopEvaluator, ReturnableEvaluator returnableEvaluator, Object... relationshipTypesAndDirections) {
assertNotNull(traversalOrder, "order");
assertNotNull(stopEvaluator, "stop evaluator");
assertNotNull(returnableEvaluator, "returnable evaluator");
if (relationshipTypesAndDirections.length % 2 != 0 || relationshipTypesAndDirections.length == 0) {
throw new IllegalArgumentException();
}
TraverserImpl result = new TraverserImpl();
TraversalDescription description = traversal(result, traversalOrder, stopEvaluator, returnableEvaluator);
description = description.expand(toExpander(relationshipTypesAndDirections));
result.iter = description.traverse(node).iterator();
return result;
}
use of org.neo4j.graphdb.traversal.TraversalDescription in project neo4j-mobile-android by neo4j-contrib.
the class ExactDepthPathFinder method paths.
private Iterator<Path> paths(final Node start, final Node end) {
TraversalDescription base = Traversal.description().uniqueness(Uniqueness.RELATIONSHIP_PATH).order(new BranchOrderingPolicy() {
public BranchSelector create(TraversalBranch startSource) {
return new LiteDepthFirstSelector(startSource, startThreshold);
}
});
final int firstHalf = onDepth / 2;
Traverser startTraverser = base.prune(Traversal.pruneAfterDepth(firstHalf)).expand(expander).filter(new Predicate<Path>() {
public boolean accept(Path item) {
return item.length() == firstHalf;
}
}).traverse(start);
final int secondHalf = onDepth - firstHalf;
Traverser endTraverser = base.prune(Traversal.pruneAfterDepth(secondHalf)).expand(expander.reversed()).filter(new Predicate<Path>() {
public boolean accept(Path item) {
return item.length() == secondHalf;
}
}).traverse(end);
final Iterator<Path> startIterator = startTraverser.iterator();
final Iterator<Path> endIterator = endTraverser.iterator();
final Map<Node, Visit> visits = new HashMap<Node, Visit>();
return new PrefetchingIterator<Path>() {
@Override
protected Path fetchNextOrNull() {
Path[] found = null;
while (found == null && (startIterator.hasNext() || endIterator.hasNext())) {
found = goOneStep(start, startIterator, visits);
if (found == null) {
found = goOneStep(end, endIterator, visits);
}
}
return found != null ? toPath(found, start) : null;
}
};
}
use of org.neo4j.graphdb.traversal.TraversalDescription in project neo4j-mobile-android by neo4j-contrib.
the class OldTraverserWrapper method traversal.
private static TraversalDescription traversal(TraverserImpl traverser, Order order, StopEvaluator stopEvaluator, ReturnableEvaluator returnableEvaluator) {
TraversalDescription description = BASE_DESCRIPTION;
switch(order) {
case BREADTH_FIRST:
description = description.breadthFirst();
break;
case DEPTH_FIRST:
description = description.depthFirst();
break;
default:
throw new IllegalArgumentException("Onsupported traversal order: " + order);
}
description = description.prune(new Pruner(traverser, stopEvaluator));
description = description.filter(new Filter(traverser, returnableEvaluator));
return description;
}
use of org.neo4j.graphdb.traversal.TraversalDescription in project neo4j by neo4j.
the class Trav method exec.
@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
assertCurrentIsNode(session);
Node node = this.getCurrent(session).asNode();
boolean caseInsensitiveFilters = parser.options().containsKey("i");
boolean looseFilters = parser.options().containsKey("l");
boolean quiet = parser.options().containsKey("q");
// Order
TraversalDescription description = getServer().getDb().traversalDescription();
String order = parser.options().get("o");
if (order != null) {
description = description.order(parseOrder(order));
}
// Relationship types / expander
String relationshipTypes = parser.options().get("r");
if (relationshipTypes != null) {
Map<String, Object> types = parseFilter(relationshipTypes, out);
description = description.expand(toExpander(getServer().getDb(), null, types, caseInsensitiveFilters, looseFilters));
}
// Uniqueness
String uniqueness = parser.options().get("u");
if (uniqueness != null) {
description = description.uniqueness(parseUniqueness(uniqueness));
}
// Depth limit
String depthLimit = parser.options().get("d");
if (depthLimit != null) {
description = description.evaluator(toDepth(parseInt(depthLimit)));
}
String filterString = parser.options().get("f");
Map<String, Object> filterMap = filterString != null ? parseFilter(filterString, out) : null;
String commandToRun = parser.options().get("c");
Collection<String> commandsToRun = new ArrayList<>();
if (commandToRun != null) {
commandsToRun.addAll(Arrays.asList(commandToRun.split(Pattern.quote("&&"))));
}
for (Path path : description.traverse(node)) {
boolean hit = false;
if (filterMap == null) {
hit = true;
} else {
Node endNode = path.endNode();
Map<String, Boolean> matchPerFilterKey = new HashMap<>();
for (String key : endNode.getPropertyKeys()) {
for (Map.Entry<String, Object> filterEntry : filterMap.entrySet()) {
String filterKey = filterEntry.getKey();
if (matchPerFilterKey.containsKey(filterKey)) {
continue;
}
if (matches(newPattern(filterKey, caseInsensitiveFilters), key, caseInsensitiveFilters, looseFilters)) {
Object value = endNode.getProperty(key);
String filterPattern = filterEntry.getValue() != null ? filterEntry.getValue().toString() : null;
if (matches(newPattern(filterPattern, caseInsensitiveFilters), value.toString(), caseInsensitiveFilters, looseFilters)) {
matchPerFilterKey.put(filterKey, true);
}
}
}
}
if (matchPerFilterKey.size() == filterMap.size()) {
hit = true;
}
}
if (hit) {
if (commandsToRun.isEmpty()) {
printPath(path, quiet, session, out);
} else {
printAndInterpretTemplateLines(commandsToRun, false, true, NodeOrRelationship.wrap(path.endNode()), getServer(), session, out);
}
}
}
return Continuation.INPUT_COMPLETE;
}
use of org.neo4j.graphdb.traversal.TraversalDescription in project graphdb by neo4j-attic.
the class ExactDepthPathFinder method paths.
private Iterator<Path> paths(final Node start, final Node end) {
TraversalDescription base = Traversal.description().uniqueness(Uniqueness.RELATIONSHIP_PATH).order(new BranchOrderingPolicy() {
public BranchSelector create(TraversalBranch startSource) {
return new LiteDepthFirstSelector(startSource, startThreshold);
}
});
final int firstHalf = onDepth / 2;
Traverser startTraverser = base.prune(Traversal.pruneAfterDepth(firstHalf)).expand(expander).filter(new Predicate<Path>() {
public boolean accept(Path item) {
return item.length() == firstHalf;
}
}).traverse(start);
final int secondHalf = onDepth - firstHalf;
Traverser endTraverser = base.prune(Traversal.pruneAfterDepth(secondHalf)).expand(expander.reversed()).filter(new Predicate<Path>() {
public boolean accept(Path item) {
return item.length() == secondHalf;
}
}).traverse(end);
final Iterator<Path> startIterator = startTraverser.iterator();
final Iterator<Path> endIterator = endTraverser.iterator();
final Map<Node, Visit> visits = new HashMap<Node, Visit>();
return new PrefetchingIterator<Path>() {
@Override
protected Path fetchNextOrNull() {
Path[] found = null;
while (found == null && (startIterator.hasNext() || endIterator.hasNext())) {
found = goOneStep(start, startIterator, visits);
if (found == null) {
found = goOneStep(end, endIterator, visits);
}
}
return found != null ? toPath(found, start) : null;
}
};
}
Aggregations