@Test public void shouldCalculateStartLabelCorrectly() { Traversal.Admin<?, ?> traversal = match( where(and(as("a").out("created").as("b"), as("b").in("created").count().is(eq(3)))), as("a").both().as("b"), where(as("b").in())) .asAdmin(); assertEquals( "a", MatchStep.Helper.computeStartLabel( ((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren())); ///// traversal = match( where("a", P.neq("c")), as("a").out("created").as("b"), or( as("a").out("knows").has("name", "vadas"), as("a").in("knows").and().as("a").has(T.label, "person")), as("b").in("created").as("c"), as("b").in("created").count().is(P.gt(1))) .asAdmin(); assertEquals( "a", MatchStep.Helper.computeStartLabel( ((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren())); }
public static void reIdSteps( final StepPosition stepPosition, final Traversal.Admin<?, ?> traversal) { stepPosition.x = 0; stepPosition.y = -1; stepPosition.z = -1; stepPosition.parentId = null; Traversal.Admin<?, ?> current = traversal; while (!(current instanceof EmptyTraversal)) { stepPosition.y++; final TraversalParent parent = current.getParent(); if (null == stepPosition.parentId && !(parent instanceof EmptyStep)) stepPosition.parentId = parent.asStep().getId(); if (-1 == stepPosition.z) { final int globalChildrenSize = parent.getGlobalChildren().size(); for (int i = 0; i < globalChildrenSize; i++) { if (parent.getGlobalChildren().get(i) == current) { stepPosition.z = i; } } for (int i = 0; i < parent.getLocalChildren().size(); i++) { if (parent.getLocalChildren().get(i) == current) { stepPosition.z = i + globalChildrenSize; } } } current = parent.asStep().getTraversal(); } if (-1 == stepPosition.z) stepPosition.z = 0; if (null == stepPosition.parentId) stepPosition.parentId = ""; for (final Step<?, ?> step : traversal.getSteps()) { step.setId(stepPosition.nextXId()); } }
private Predicates getPredicates(Step step, Traversal.Admin traversal) { Predicates predicates = new Predicates(); Step<?, ?> nextStep = step.getNextStep(); while (true) { if (nextStep instanceof HasContainerHolder) { HasContainerHolder hasContainerHolder = (HasContainerHolder) nextStep; boolean skip = false; for (HasContainer has : hasContainerHolder.getHasContainers()) if (has.getPredicate().getTraversals().size() > 0) skip = true; if (!skip) { hasContainerHolder.getHasContainers().forEach((has) -> predicates.hasContainers.add(has)); nextStep.getLabels().forEach(label -> predicates.labels.add(label.toString())); traversal.removeStep(nextStep); } } else if (nextStep instanceof RangeGlobalStep) { RangeGlobalStep rangeGlobalStep = (RangeGlobalStep) nextStep; predicates.limitLow = rangeGlobalStep.getLowRange(); predicates.limitHigh = rangeGlobalStep.getHighRange(); traversal.removeStep(nextStep); } else return predicates; nextStep = nextStep.getNextStep(); } }
/** * Replace a step with a new step. * * @param removeStep the step to remove * @param insertStep the step to insert * @param traversal the traversal on which the action will occur */ public static <S, E> void replaceStep( final Step<S, E> removeStep, final Step<S, E> insertStep, final Traversal.Admin<?, ?> traversal) { traversal.addStep(stepIndex(removeStep, traversal), insertStep); traversal.removeStep(removeStep); }
@Override public void apply(Traversal.Admin<?, ?> traversal) { if (traversal.getEngine().isComputer()) return; Graph graph = traversal.getGraph().get(); if (!(graph instanceof ElasticGraph)) return; ElasticGraph elasticGraph = (ElasticGraph) graph; TraversalHelper.getStepsOfClass(GraphStep.class, traversal) .forEach( graphStep -> { if (graphStep.getIds().length == 0) { Predicates predicates = getPredicates(graphStep, traversal); final ElasticGraphStep<?> elasticGraphStep = new ElasticGraphStep<>(graphStep, predicates, elasticGraph.elasticService); TraversalHelper.replaceStep(graphStep, (Step) elasticGraphStep, traversal); } }); TraversalHelper.getStepsOfClass(VertexStep.class, traversal) .forEach( vertexStep -> { boolean returnVertex = vertexStep.getReturnClass().equals(Vertex.class); Predicates predicates = returnVertex ? new Predicates() : getPredicates(vertexStep, traversal); ElasticVertexStep elasticVertexStep = new ElasticVertexStep(vertexStep, predicates, elasticGraph.elasticService); TraversalHelper.replaceStep(vertexStep, elasticVertexStep, traversal); }); }
@Override public int hashCode() { int result = this.getClass().hashCode(), i = 0; for (final Traversal.Admin<A, B> traversal : this.traversals) { result ^= Integer.rotateLeft(traversal.hashCode(), i++); } return result; }
@Override public Traversal.Admin<S, E> apply(final Graph graph) { final Traversal.Admin<S, E> clone = this.traversal.clone(); if (!clone.isLocked()) { clone.setGraph(graph); clone.applyStrategies(); } return clone; }
public static void setMemorySideEffects( final Traversal.Admin<?, ?> traversal, final Memory memory, final ProgramPhase phase) { final TraversalSideEffects sideEffects = traversal.getSideEffects(); if (!(sideEffects instanceof MemoryTraversalSideEffects)) { traversal.setSideEffects(new MemoryTraversalSideEffects(sideEffects)); } final MemoryTraversalSideEffects memoryTraversalSideEffects = ((MemoryTraversalSideEffects) traversal.getSideEffects()); memoryTraversalSideEffects.memory = memory; memoryTraversalSideEffects.phase = phase; }
@Override public TraversalRing<A, B> clone() { try { final TraversalRing<A, B> clone = (TraversalRing<A, B>) super.clone(); clone.traversals = new ArrayList<>(); for (final Traversal.Admin<A, B> traversal : this.traversals) { clone.addTraversal(traversal.clone()); } return clone; } catch (final CloneNotSupportedException e) { throw new IllegalStateException(e.getMessage(), e); } }
public static <S, E> void removeToTraversal( final Step<S, ?> startStep, final Step<?, E> endStep, final Traversal.Admin<S, E> newTraversal) { final Traversal.Admin<?, ?> originalTraversal = startStep.getTraversal(); Step<?, ?> currentStep = startStep; while (currentStep != endStep && !(currentStep instanceof EmptyStep)) { final Step<?, ?> temp = currentStep.getNextStep(); originalTraversal.removeStep(currentStep); newTraversal.addStep(currentStep); currentStep = temp; } }
public static Set<MemoryComputeKey> getMemoryComputeKeys(final Traversal.Admin<?, ?> traversal) { final Set<MemoryComputeKey> keys = new HashSet<>(); final TraversalSideEffects sideEffects = traversal.getSideEffects() instanceof MemoryTraversalSideEffects ? ((MemoryTraversalSideEffects) traversal.getSideEffects()).getSideEffects() : traversal.getSideEffects(); sideEffects .keys() .stream() .forEach( key -> keys.add(MemoryComputeKey.of(key, sideEffects.getReducer(key), true, false))); return keys; }
public Traversal.Admin<S, E> apply(final Graph graph) { try { final ScriptEngine engine = SingleGremlinScriptEngineManager.get(this.scriptEngineName); final Bindings engineBindings = engine.createBindings(); engineBindings.put("g", this.traversalSourceFactory.createTraversalSource(graph)); for (int i = 0; i < this.bindings.length; i = i + 2) { engineBindings.put((String) this.bindings[i], this.bindings[i + 1]); } final Traversal.Admin<S, E> traversal = (Traversal.Admin<S, E>) engine.eval(this.traversalScript, engineBindings); if (!traversal.isLocked()) traversal.applyStrategies(); return traversal; } catch (final ScriptException e) { throw new IllegalStateException(e.getMessage(), e); } }
@Test public void testPreCompilationOfStartAndEnds() { final Traversal.Admin<?, ?> traversal = __.match(as("a").out().as("b"), as("c").path().as("d")).asAdmin(); final MatchStep<?, ?> matchStep = (MatchStep<?, ?>) traversal.getStartStep(); assertEquals(MatchStep.class, traversal.getStartStep().getClass()); assertEquals(2, matchStep.getGlobalChildren().size()); Traversal.Admin<Object, Object> pattern = matchStep.getGlobalChildren().get(0); assertEquals("a", ((MatchStep.MatchStartStep) pattern.getStartStep()).getSelectKey().get()); assertEquals(VertexStep.class, pattern.getStartStep().getNextStep().getClass()); assertEquals("b", ((MatchStep.MatchEndStep) pattern.getEndStep()).getMatchKey().get()); // pattern = matchStep.getGlobalChildren().get(1); assertEquals("c", ((MatchStep.MatchStartStep) pattern.getStartStep()).getSelectKey().get()); assertEquals(PathStep.class, pattern.getStartStep().getNextStep().getClass()); assertEquals("d", ((MatchStep.MatchEndStep) pattern.getEndStep()).getMatchKey().get()); }
/** * Gets the index of a particular step in the {@link Traversal}. * * @param step the step to retrieve the index for * @param traversal the traversal to perform the action on * @return the index of the step or -1 if the step is not present */ public static <S, E> int stepIndex(final Step<S, E> step, final Traversal.Admin<?, ?> traversal) { int i = 0; for (final Step s : traversal.getSteps()) { if (s.equals(step, true)) return i; i++; } return -1; }
/** * Determine if the traversal has a step of an assignable class. * * @param superClass the step super class to look for * @param traversal the traversal to perform the action on * @return {@code true} if the class is found and {@code false} otherwise */ public static boolean hasStepOfAssignableClass( final Class superClass, final Traversal.Admin<?, ?> traversal) { for (final Step<?, ?> step : traversal.getSteps()) { if (superClass.isAssignableFrom(step.getClass())) { return true; } } return false; }
/** * Determine if the traversal has a step of a particular class. * * @param stepClass the step class to look for * @param traversal the traversal to perform the action on * @return {@code true} if the class is found and {@code false} otherwise */ public static boolean hasStepOfClass( final Class stepClass, final Traversal.Admin<?, ?> traversal) { for (final Step<?, ?> step : traversal.getSteps()) { if (step.getClass().equals(stepClass)) { return true; } } return false; }
@Override public void sendMessage(final MessageScope messageScope, final M message) { if (messageScope instanceof MessageScope.Local) { final MessageScope.Local<M> localMessageScope = (MessageScope.Local) messageScope; final Traversal.Admin<Vertex, Edge> incidentTraversal = SparkMessenger.setVertexStart( localMessageScope.getIncidentTraversal().get(), this.vertex); final Direction direction = SparkMessenger.getOppositeDirection(incidentTraversal); incidentTraversal.forEachRemaining( edge -> this.outgoingMessages.add( new Tuple2<>(edge.vertices(direction).next().id(), message))); } else { ((MessageScope.Global) messageScope) .vertices() .forEach(v -> this.outgoingMessages.add(new Tuple2<>(v.id(), message))); } }
public static <S> Optional<S> getFirstStepOfAssignableClass( final Class<S> stepClass, final Traversal.Admin<?, ?> traversal) { return (Optional<S>) traversal .getSteps() .stream() .filter(step -> stepClass.isAssignableFrom(step.getClass())) .findFirst(); }
public static <S> List<S> getStepsOfAssignableClass( final Class<S> stepClass, final Traversal.Admin<?, ?> traversal) { return (List) traversal .getSteps() .stream() .filter(step -> stepClass.isAssignableFrom(step.getClass())) .collect(Collectors.toList()); }
public static <S> List<S> getStepsOfClass( final Class<S> stepClass, final Traversal.Admin<?, ?> traversal) { List<S> steps = new ArrayList<>(); for (Step step : traversal.getSteps()) { if (step.getClass().equals(stepClass)) { steps.add((S) step); } } return steps; }
private static Set<String> getLabels( final Set<String> labels, final Traversal.Admin<?, ?> traversal) { for (final Step<?, ?> step : traversal.getSteps()) { labels.addAll(step.getLabels()); if (step instanceof TraversalParent) { ((TraversalParent) step).getLocalChildren().forEach(child -> getLabels(labels, child)); ((TraversalParent) step).getGlobalChildren().forEach(child -> getLabels(labels, child)); } } return labels; }
public static <S, E> Step<?, E> insertTraversal( final int insertIndex, final Traversal.Admin<S, E> insertTraversal, final Traversal.Admin<?, ?> traversal) { if (0 == traversal.getSteps().size()) { Step currentStep = EmptyStep.instance(); for (final Step insertStep : insertTraversal.getSteps()) { currentStep = insertStep; traversal.addStep(insertStep); } return currentStep; } else { Step currentStep = traversal.getSteps().get(insertIndex); for (final Step insertStep : insertTraversal.getSteps()) { TraversalHelper.insertAfterStep(insertStep, currentStep, traversal); currentStep = insertStep; } return currentStep; } }
@Test public void testPreCompilationOfWherePredicate() { final List<Traversal.Admin<?, ?>> traversals = Arrays.asList( __.match(as("a").out().as("b"), as("c").where(P.neq("d"))).asAdmin(), __.match(as("a").out().as("b"), where("c", P.neq("d"))).asAdmin()); assertEquals( 1, new HashSet<>(traversals) .size()); // the two patterns should pre-compile to the same traversal traversals.forEach( traversal -> { MatchStep<?, ?> matchStep = (MatchStep<?, ?>) traversal.getStartStep(); // assertFalse(matchStep.getStartLabel().isPresent()); assertEquals(2, matchStep.getGlobalChildren().size()); Traversal.Admin<Object, Object> pattern = matchStep.getGlobalChildren().get(0); assertEquals( "a", ((MatchStep.MatchStartStep) pattern.getStartStep()).getSelectKey().get()); assertEquals(VertexStep.class, pattern.getStartStep().getNextStep().getClass()); assertEquals("b", ((MatchStep.MatchEndStep) pattern.getEndStep()).getMatchKey().get()); // pattern = matchStep.getGlobalChildren().get(1); assertEquals(MatchStep.MatchStartStep.class, pattern.getStartStep().getClass()); assertEquals( "c", ((MatchStep.MatchStartStep) pattern.getStartStep()).getSelectKey().get()); assertEquals(WherePredicateStep.class, pattern.getStartStep().getNextStep().getClass()); assertEquals( MatchStep.MatchEndStep.class, pattern.getStartStep().getNextStep().getNextStep().getClass()); assertFalse( ((WherePredicateStep<?>) pattern.getStartStep().getNextStep()) .getStartKey() .isPresent()); assertEquals( "d", ((WherePredicateStep<?>) pattern.getStartStep().getNextStep()) .getPredicate() .get() .getOriginalValue()); }); }
private static Set<Scoping.Variable> getVariableLocations( final Set<Scoping.Variable> variables, final Traversal.Admin<?, ?> traversal) { if (variables.size() == 2) return variables; // has both START and END so no need to compute further final Step<?, ?> startStep = traversal.getStartStep(); if (StartStep.isVariableStartStep(startStep)) variables.add(Scoping.Variable.START); else if (startStep instanceof WherePredicateStep) { if (((WherePredicateStep) startStep).getStartKey().isPresent()) variables.add(Scoping.Variable.START); } else if (startStep instanceof WhereTraversalStep.WhereStartStep) { if (!((WhereTraversalStep.WhereStartStep) startStep).getScopeKeys().isEmpty()) variables.add(Scoping.Variable.START); } else if (startStep instanceof MatchStep.MatchStartStep) { if (((MatchStep.MatchStartStep) startStep).getSelectKey().isPresent()) variables.add(Scoping.Variable.START); } else if (startStep instanceof MatchStep) { ((MatchStep<?, ?>) startStep) .getGlobalChildren() .forEach(child -> TraversalHelper.getVariableLocations(variables, child)); } else if (startStep instanceof ConnectiveStep || startStep instanceof NotStep || startStep instanceof WhereTraversalStep) ((TraversalParent) startStep) .getLocalChildren() .forEach(child -> TraversalHelper.getVariableLocations(variables, child)); /// final Step<?, ?> endStep = traversal.getEndStep(); if (endStep instanceof WherePredicateStep) { if (((WherePredicateStep) endStep).getStartKey().isPresent()) variables.add(Scoping.Variable.END); } else if (endStep instanceof WhereTraversalStep.WhereEndStep) { if (!((WhereTraversalStep.WhereEndStep) endStep).getScopeKeys().isEmpty()) variables.add(Scoping.Variable.END); } else if (endStep instanceof MatchStep.MatchEndStep) { if (((MatchStep.MatchEndStep) endStep).getMatchKey().isPresent()) variables.add(Scoping.Variable.END); } else if (!endStep.getLabels().isEmpty()) variables.add(Scoping.Variable.END); /// return variables; }
public static <S> List<S> getStepsOfAssignableClassRecursively( final Class<S> stepClass, final Traversal.Admin<?, ?> traversal) { final List<S> list = new ArrayList<>(); for (final Step<?, ?> step : traversal.getSteps()) { if (stepClass.isAssignableFrom(step.getClass())) list.add((S) step); if (step instanceof TraversalParent) { for (final Traversal.Admin<?, ?> globalChild : ((TraversalParent) step).getGlobalChildren()) { list.addAll(TraversalHelper.getStepsOfAssignableClassRecursively(stepClass, globalChild)); } } } return list; }
/** * Determine if any step in {@link Traversal} or its children match the step given the provided * {@link Predicate}. * * @param predicate the match function * @param traversal th traversal to perform the action on * @return {@code true} if there is a match and {@code false} otherwise */ public static boolean anyStepRecursively( final Predicate<Step> predicate, final Traversal.Admin<?, ?> traversal) { for (final Step<?, ?> step : traversal.getSteps()) { if (predicate.test(step)) { return true; } if (step instanceof TraversalParent) { for (final Traversal.Admin<?, ?> globalChild : ((TraversalParent) step).getGlobalChildren()) { if (anyStepRecursively(predicate, globalChild)) return true; } } } return false; }
/** * Determine if the traversal has a step of an assignable class in the current {@link Traversal} * and its child traversals. * * @param stepClass the step class to look for * @param traversal the traversal in which to look for the given step class * @return <code>true</code> if any step in the given traversal (and its child traversals) is an * instance of the given <code>stepClass</code>, otherwise <code>false</code>. */ public static boolean hasStepOfAssignableClassRecursively( final Class stepClass, final Traversal.Admin<?, ?> traversal) { for (final Step<?, ?> step : traversal.getSteps()) { if (stepClass.isAssignableFrom(step.getClass())) { return true; } if (step instanceof TraversalParent) { for (final Traversal.Admin<?, ?> globalChild : ((TraversalParent) step).getGlobalChildren()) { if (hasStepOfAssignableClassRecursively(stepClass, globalChild)) return true; } } } return false; }
/** * Determine if the traversal has any of the supplied steps of an assignable class in the current * {@link Traversal} and its child traversals. * * @param stepClasses the step classes to look for * @param traversal the traversal in which to look for the given step classes * @return <code>true</code> if any step in the given traversal (and its child traversals) is an * instance of a class provided in <code>stepClasses</code>, otherwise <code>false</code>. */ public static boolean hasStepOfAssignableClassRecursively( final Collection<Class> stepClasses, final Traversal.Admin<?, ?> traversal) { if (stepClasses.size() == 1) return hasStepOfAssignableClassRecursively(stepClasses.iterator().next(), traversal); for (final Step<?, ?> step : traversal.getSteps()) { if (IteratorUtils.anyMatch( stepClasses.iterator(), stepClass -> stepClass.isAssignableFrom(step.getClass()))) { return true; } if (step instanceof TraversalParent) { for (final Traversal.Admin<?, ?> globalChild : ((TraversalParent) step).getGlobalChildren()) { if (hasStepOfAssignableClassRecursively(stepClasses, globalChild)) return true; } } } return false; }
private static boolean isLocalStarGraph(final Traversal.Admin<?, ?> traversal, char state) { for (final Step step : traversal.getSteps()) { if (step instanceof RepeatStep && ((RepeatStep<?>) step) .getGlobalChildren() .stream() .flatMap(t -> t.getSteps().stream()) .filter(temp -> temp instanceof VertexStep) .findAny() .isPresent()) // TODO: is this sufficient? return false; if (step instanceof PropertiesStep && state == 'u') return false; else if (step instanceof VertexStep) { if (((VertexStep) step).returnsVertex()) { if (state == 'u') return false; if (state == 'v') state = 'u'; } else { state = 'e'; } } else if (step instanceof EdgeVertexStep) { if (state == 'e') state = 'u'; } else if (step instanceof HasContainerHolder && state == 'u') { if (((HasContainerHolder) step) .getHasContainers() .stream() .filter(c -> !c.getKey().equals(T.id.getAccessor())) // TODO: are labels available? .findAny() .isPresent()) return false; } else if (step instanceof TraversalParent) { final char currState = state; if (((TraversalParent) step) .getLocalChildren() .stream() .filter(t -> !isLocalStarGraph(t.asAdmin(), currState)) .findAny() .isPresent()) return false; } } return true; }
public static TitanTransaction getTx(Traversal.Admin<?, ?> traversal) { TitanTransaction tx = null; Optional<Graph> optGraph = TraversalHelper.getRootTraversal(traversal.asAdmin()).getGraph(); if (traversal instanceof FulgoraElementTraversal) { tx = (TitanTransaction) optGraph.get(); } else { if (!optGraph.isPresent()) throw new IllegalArgumentException("Traversal is not bound to a graph: " + traversal); Graph graph = optGraph.get(); if (graph instanceof TitanTransaction) tx = (TitanTransaction) graph; else if (graph instanceof TitanBlueprintsGraph) tx = ((TitanBlueprintsGraph) graph).getCurrentThreadTx(); else throw new IllegalArgumentException( "Traversal is not bound to a Titan Graph, but: " + graph); } if (tx == null) throw new IllegalArgumentException( "Not a valid start step for a Titan traversal: " + traversal); if (tx.isOpen()) return tx; else return ((StandardTitanTx) tx).getNextTx(); }