org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversal Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversal. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestLocalVertexStepLimit.java    From sqlg with MIT License 6 votes vote down vote up
@Test
    public void testCount() {
        loadModern();
        @SuppressWarnings("unchecked") DefaultTraversal<Vertex, Long> traversal = (DefaultTraversal<Vertex, Long>) this.sqlgGraph.traversal()
                .V()
                .local(
                        __.out().count()
                );
        List<Long> counts = traversal.toList();
//        for (Long count : counts) {
//            System.out.println(count);
//        }
        Assert.assertEquals(6, counts.size());
        Assert.assertTrue(counts.remove(3L));
        Assert.assertTrue(counts.remove(2L));
        Assert.assertTrue(counts.remove(1L));
        Assert.assertTrue(counts.remove(0L));
        Assert.assertTrue(counts.remove(0L));
        Assert.assertTrue(counts.remove(0L));
        Assert.assertTrue(counts.isEmpty());
    }
 
Example #2
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldIdentifyLocalChildren() {
    final Traversal.Admin<?, ?> localChild = __.as("x").select("a", "b").by("name").asAdmin();
    new LocalStep<>(new DefaultTraversal(), localChild);
    assertFalse(TraversalHelper.isGlobalChild(localChild));
    ///
    new WhereTraversalStep<>(new DefaultTraversal(), localChild);
    assertFalse(TraversalHelper.isGlobalChild(localChild));
    ///
    new TraversalFilterStep<>(new DefaultTraversal(), localChild);
    assertFalse(TraversalHelper.isGlobalChild(localChild));
    ///
    new TraversalMapStep<>(new DefaultTraversal(), localChild);
    assertFalse(TraversalHelper.isGlobalChild(localChild));
    ///
    new TraversalFlatMapStep<>(new DefaultTraversal(), localChild);
    assertFalse(TraversalHelper.isGlobalChild(localChild));
    ///
    final Traversal.Admin<?, ?> remoteLocalChild = __.repeat(localChild).asAdmin();
    new LocalStep<>(new DefaultTraversal<>(), remoteLocalChild);
    assertFalse(TraversalHelper.isGlobalChild(localChild));
}
 
Example #3
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldIdentifyGlobalChildren() {
    final Traversal.Admin<?, ?> globalChild = __.select("a", "b").by("name").asAdmin();
    TraversalParent parent = new RepeatStep<>(new DefaultTraversal());
    ((RepeatStep) parent).setRepeatTraversal(globalChild);
    assertTrue(TraversalHelper.isGlobalChild(globalChild));
    ///
    new UnionStep<>(new DefaultTraversal(), globalChild);
    assertTrue(TraversalHelper.isGlobalChild(globalChild));
    ///
    new TraversalVertexProgramStep(new DefaultTraversal<>(), globalChild);
    assertTrue(TraversalHelper.isGlobalChild(globalChild));
    ///
    final Traversal.Admin<?, ?> remoteRemoteChild = __.repeat(globalChild).asAdmin();
    new UnionStep<>(new DefaultTraversal(), remoteRemoteChild);
    assertTrue(TraversalHelper.isGlobalChild(globalChild));
}
 
Example #4
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRemoveStepsCorrectly() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(new IdentityStep(traversal));
    traversal.asAdmin().addStep(new HasStep(traversal));
    traversal.asAdmin().addStep(new LambdaFilterStep(traversal, traverser -> true));

    traversal.asAdmin().addStep(new PropertiesStep(traversal, PropertyType.VALUE, "marko"));
    traversal.asAdmin().removeStep(3);
    validateToyTraversal(traversal);

    traversal.asAdmin().addStep(0, new PropertiesStep(traversal, PropertyType.PROPERTY, "marko"));
    traversal.asAdmin().removeStep(0);
    validateToyTraversal(traversal);

    traversal.asAdmin().removeStep(1);
    traversal.asAdmin().addStep(1, new HasStep(traversal));
    validateToyTraversal(traversal);
}
 
Example #5
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetTheStepIndex() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    final HasStep hasStep = new HasStep(traversal);
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, hasStep);
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    assertEquals(1, TraversalHelper.stepIndex(hasStep, traversal));
}
 
Example #6
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAddStepsCorrectly() {
    Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(0, new LambdaFilterStep(traversal, traverser -> true));
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new IdentityStep(traversal));
    validateToyTraversal(traversal);

    traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(0, new IdentityStep(traversal));
    traversal.asAdmin().addStep(1, new HasStep(traversal));
    traversal.asAdmin().addStep(2, new LambdaFilterStep(traversal, traverser -> true));
    validateToyTraversal(traversal);
}
 
Example #7
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldChainTogetherStepsWithNextPreviousInALinkedListStructure() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(new IdentityStep(traversal));
    traversal.asAdmin().addStep(new HasStep(traversal));
    traversal.asAdmin().addStep(new LambdaFilterStep(traversal, traverser -> true));
    validateToyTraversal(traversal);
}
 
Example #8
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReplaceStep() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    final HasStep hasStep = new HasStep(traversal);
    final IdentityStep identityStep = new IdentityStep(traversal);
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, hasStep);
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    TraversalHelper.replaceStep(hasStep, identityStep, traversal);

    assertEquals(traversal.asAdmin().getSteps().get(1), identityStep);
    assertEquals(3, traversal.asAdmin().getSteps().size());
}
 
Example #9
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldInsertAfterStep() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    final HasStep hasStep = new HasStep(traversal);
    final IdentityStep identityStep = new IdentityStep(traversal);
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, hasStep);
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    TraversalHelper.insertAfterStep(identityStep, hasStep, traversal);

    assertEquals(traversal.asAdmin().getSteps().get(2), identityStep);
    assertEquals(4, traversal.asAdmin().getSteps().size());
}
 
Example #10
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldInsertBeforeStep() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    final HasStep hasStep = new HasStep(traversal);
    final IdentityStep identityStep = new IdentityStep(traversal);
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, hasStep);
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    TraversalHelper.insertBeforeStep(identityStep, hasStep, traversal);

    assertEquals(traversal.asAdmin().getSteps().get(1), identityStep);
    assertEquals(4, traversal.asAdmin().getSteps().size());
}
 
Example #11
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotFindTheStepIndex() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    final IdentityStep identityStep = new IdentityStep(traversal);
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    assertEquals(-1, TraversalHelper.stepIndex(identityStep, traversal));
}
 
Example #12
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFindStepOfAssignableClassInTraversal() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    assertThat(TraversalHelper.hasStepOfAssignableClass(FilterStep.class, traversal), is(true));
}
 
Example #13
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotFindStepOfAssignableClassInTraversal() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    assertThat(TraversalHelper.hasStepOfAssignableClass(IdentityStep.class, traversal), is(false));
}
 
Example #14
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFindStepOfClassInTraversal() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new IdentityStep<>(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    assertThat(TraversalHelper.hasStepOfClass(IdentityStep.class, traversal), is(true));
}
 
Example #15
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotFindStepOfClassInTraversal() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    assertThat(TraversalHelper.hasStepOfClass(FilterStep.class, traversal), is(false));
}
 
Example #16
Source File: MatchPredicateStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!TraversalHelper.hasStepOfClass(MatchStep.class, traversal))
        return;

    TraversalHelper.getStepsOfClass(MatchStep.class, traversal).forEach(matchStep -> {
        // match().select().where() --> match(where()).select()
        // match().select().dedup() --> match(dedup()).select()
        Step<?, ?> nextStep = matchStep.getNextStep();
        while (nextStep instanceof WherePredicateStep ||
                nextStep instanceof WhereTraversalStep ||
                (nextStep instanceof DedupGlobalStep && !((DedupGlobalStep) nextStep).getScopeKeys().isEmpty() && ((DedupGlobalStep) nextStep).getLocalChildren().isEmpty()) ||
                (nextStep instanceof SelectStep && ((SelectStep) nextStep).getLocalChildren().isEmpty()) ||
                (nextStep instanceof SelectOneStep && ((SelectOneStep) nextStep).getLocalChildren().isEmpty())) {
            if (nextStep instanceof WherePredicateStep || nextStep instanceof WhereTraversalStep) {
                traversal.removeStep(nextStep);
                matchStep.addGlobalChild(traversal instanceof GraphTraversal ?
                        new DefaultGraphTraversal<>().addStep(nextStep) :
                        new DefaultTraversal<>().addStep(nextStep));
                nextStep = matchStep.getNextStep();
            } else if (nextStep instanceof DedupGlobalStep && !((DedupGlobalStep) nextStep).getScopeKeys().isEmpty() && ((DedupGlobalStep) nextStep).getLocalChildren().isEmpty() && !TraversalHelper.onGraphComputer(traversal)) {
                traversal.removeStep(nextStep);
                matchStep.setDedupLabels(((DedupGlobalStep<?>) nextStep).getScopeKeys());
                nextStep = matchStep.getNextStep();
            } else if (nextStep.getLabels().isEmpty()) {
                nextStep = nextStep.getNextStep();
            } else
                break;
        }
    });
}
 
Example #17
Source File: GremlinDslProcessor.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
private void generateDefaultTraversal(final Context ctx) throws IOException {
    final TypeSpec.Builder defaultTraversalClass = TypeSpec.classBuilder(ctx.defaultTraversalClazz)
            .addModifiers(Modifier.PUBLIC)
            .addTypeVariables(Arrays.asList(TypeVariableName.get("S"), TypeVariableName.get("E")))
            .superclass(TypeName.get(elementUtils.getTypeElement(DefaultTraversal.class.getCanonicalName()).asType()))
            .addSuperinterface(ParameterizedTypeName.get(ctx.traversalClassName, TypeVariableName.get("S"), TypeVariableName.get("E")));

    // add the required constructors for instantiation
    defaultTraversalClass.addMethod(MethodSpec.constructorBuilder()
            .addModifiers(Modifier.PUBLIC)
            .addStatement("super()")
            .build());
    defaultTraversalClass.addMethod(MethodSpec.constructorBuilder()
            .addModifiers(Modifier.PUBLIC)
            .addParameter(Graph.class, "graph")
            .addStatement("super($N)", "graph")
            .build());
    defaultTraversalClass.addMethod(MethodSpec.constructorBuilder()
            .addModifiers(Modifier.PUBLIC)
            .addParameter(ctx.traversalSourceClassName, "traversalSource")
            .addStatement("super($N)", "traversalSource")
            .build());
    defaultTraversalClass.addMethod(MethodSpec.constructorBuilder()
            .addModifiers(Modifier.PUBLIC)
            .addParameter(ctx.traversalSourceClassName, "traversalSource")
            .addParameter(ctx.graphTraversalAdminClassName, "traversal")
            .addStatement("super($N, $N.asAdmin())", "traversalSource", "traversal")
            .build());

    // add the override
    defaultTraversalClass.addMethod(MethodSpec.methodBuilder("iterate")
            .addModifiers(Modifier.PUBLIC)
            .addAnnotation(Override.class)
            .addStatement("return ($T) super.iterate()", ctx.traversalClassName)
            .returns(ParameterizedTypeName.get(ctx.traversalClassName, TypeVariableName.get("S"), TypeVariableName.get("E")))
            .build());
    defaultTraversalClass.addMethod(MethodSpec.methodBuilder("asAdmin")
            .addModifiers(Modifier.PUBLIC)
            .addAnnotation(Override.class)
            .addStatement("return ($T) super.asAdmin()", GraphTraversal.Admin.class)
            .returns(ParameterizedTypeName.get(ctx.graphTraversalAdminClassName, TypeVariableName.get("S"), TypeVariableName.get("E")))
            .build());
    defaultTraversalClass.addMethod(MethodSpec.methodBuilder("clone")
            .addModifiers(Modifier.PUBLIC)
            .addAnnotation(Override.class)
            .addStatement("return ($T) super.clone()", ctx.defaultTraversalClassName)
            .returns(ParameterizedTypeName.get(ctx.defaultTraversalClassName, TypeVariableName.get("S"), TypeVariableName.get("E")))
            .build());

    final JavaFile defaultTraversalJavaFile = JavaFile.builder(ctx.packageName, defaultTraversalClass.build()).build();
    defaultTraversalJavaFile.writeTo(filer);
}