Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource#withStrategies()

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource#withStrategies() . 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: TinkerGraphGryoTranslatorProvider.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
public GraphTraversalSource traversal(final Graph graph) {
    if ((Boolean) graph.configuration().getProperty("skipTest"))
        return graph.traversal();
        //throw new VerificationException("This test current does not work with Gremlin-Python", EmptyTraversal.instance());
    else {
        final GraphTraversalSource g = graph.traversal();
        return g.withStrategies(new TranslationStrategy(g, new GryoTranslator<>(JavaTranslator.of(g))));
    }
}
 
Example 2
Source File: AbstractTinkerGraphGraphSONTranslatorProvider.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
public GraphTraversalSource traversal(final Graph graph) {
    if ((Boolean) graph.configuration().getProperty("skipTest"))
        return graph.traversal();
    else {
        final GraphTraversalSource g = graph.traversal();
        return g.withStrategies(new TranslationStrategy(g, new GraphSONTranslator<>(JavaTranslator.of(g), version)));
    }
}
 
Example 3
Source File: GryoRemoteGraphGroovyTranslatorProvider.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public GraphTraversalSource traversal(final Graph graph) {
    final GraphTraversalSource g = super.traversal(graph);
    return g.withStrategies(new TranslationStrategy(g, GroovyTranslator.of("g"), true));
}
 
Example 4
Source File: TinkerGraphGryoTranslatorProvider.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public GraphTraversalSource traversal(final Graph graph) {
    final GraphTraversalSource g = graph.traversal();
    return g.withStrategies(new TranslationStrategy(g, new GryoTranslator<>(JavaTranslator.of(g)), true));
}
 
Example 5
Source File: AbstractTinkerGraphGraphSONTranslatorProvider.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public GraphTraversalSource traversal(final Graph graph) {
    final GraphTraversalSource g = graph.traversal();
    return g.withStrategies(new TranslationStrategy(g, new GraphSONTranslator<>(JavaTranslator.of(g), version), true));
}
 
Example 6
Source File: ParameterizedGroovyTranslatorTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldSupportStringSupplierLambdas() {
    final TinkerGraph graph = TinkerFactory.createModern();
    GraphTraversalSource g = graph.traversal();
    g = g.withStrategies(new TranslationStrategy(g, GroovyTranslator.of("g", true), false));
    final GraphTraversal.Admin<Vertex, Integer> t = g.withSideEffect("lengthSum", 0).withSack(1)
            .V()
            .filter(Lambda.predicate("it.get().label().equals('person')"))
            .flatMap(Lambda.function("it.get().vertices(Direction.OUT)"))
            .map(Lambda.<Traverser<Object>, Integer>function("it.get().value('name').length()"))
            .sideEffect(Lambda.consumer("{ x -> x.sideEffects(\"lengthSum\", x.<Integer>sideEffects('lengthSum') + x.get()) }"))
            .order().by(Lambda.comparator("a,b -> a <=> b"))
            .sack(Lambda.biFunction("{ a,b -> a + b }"))
            .asAdmin();

    final List<Integer> sacks = new ArrayList<>();
    final List<Integer> lengths = new ArrayList<>();
    while (t.hasNext()) {
        final Traverser.Admin<Integer> traverser = t.nextTraverser();
        sacks.add(traverser.sack());
        lengths.add(traverser.get());
    }
    assertFalse(t.hasNext());
    //
    assertEquals(6, lengths.size());
    assertEquals(3, lengths.get(0).intValue());
    assertEquals(3, lengths.get(1).intValue());
    assertEquals(3, lengths.get(2).intValue());
    assertEquals(4, lengths.get(3).intValue());
    assertEquals(5, lengths.get(4).intValue());
    assertEquals(6, lengths.get(5).intValue());
    ///
    assertEquals(6, sacks.size());
    assertEquals(4, sacks.get(0).intValue());
    assertEquals(4, sacks.get(1).intValue());
    assertEquals(4, sacks.get(2).intValue());
    assertEquals(5, sacks.get(3).intValue());
    assertEquals(6, sacks.get(4).intValue());
    assertEquals(7, sacks.get(5).intValue());
    //
    assertEquals(24, t.getSideEffects().<Number>get("lengthSum").intValue());

    final Script script = GroovyTranslator.of("g", true).translate(t.getBytecode());
    Bindings bindings = new SimpleBindings();
    script.getParameters().ifPresent(bindings::putAll);
    assertEquals(9, bindings.size());
    assertEquals("lengthSum", bindings.get("_args_0"));
    assertEquals(Integer.valueOf(0), bindings.get("_args_1"));
    assertEquals(Integer.valueOf(1), bindings.get("_args_2"));
    assertEquals(Lambda.predicate("it.get().label().equals('person')"), bindings.get("_args_3"));
    assertEquals(Lambda.function("it.get().vertices(Direction.OUT)"), bindings.get("_args_4"));
    assertEquals(Lambda.<Traverser<Object>, Integer>function("it.get().value('name').length()"), bindings.get("_args_5"));
    assertEquals(Lambda.consumer("{ x -> x.sideEffects(\"lengthSum\", x.<Integer>sideEffects('lengthSum') + x.get()) }"), bindings.get("_args_6"));
    assertEquals(Lambda.comparator("a,b -> a <=> b"), bindings.get("_args_7"));
    assertEquals(Lambda.biFunction("{ a,b -> a + b }"), bindings.get("_args_8"));
    assertEquals("g.withStrategies(org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.TranslationStrategy.instance())" +
                    ".withSideEffect(_args_0,_args_1).withSack(_args_2)" +
                    ".V()" +
                    ".filter(_args_3)" +
                    ".flatMap(_args_4)" +
                    ".map(_args_5)" +
                    ".sideEffect(_args_6)" +
                    ".order().by(_args_7)" +
                    ".sack(_args_8)",
            script.getScript());
}
 
Example 7
Source File: GroovyTranslatorProvider.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public GraphTraversalSource traversal(final Graph graph) {
    final GraphTraversalSource g = graph.traversal();
    return g.withStrategies(new TranslationStrategy(g, GroovyTranslator.of("g"), true));
}
 
Example 8
Source File: GroovyTranslatorTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldSupportStringSupplierLambdas() {
    final TinkerGraph graph = TinkerFactory.createModern();
    GraphTraversalSource g = graph.traversal();
    g = g.withStrategies(new TranslationStrategy(g, GroovyTranslator.of("g"), false));
    final GraphTraversal.Admin<Vertex, Integer> t = g.withSideEffect("lengthSum", 0).withSack(1)
            .V()
            .filter(Lambda.predicate("it.get().label().equals('person')"))
            .flatMap(Lambda.function("it.get().vertices(Direction.OUT)"))
            .map(Lambda.<Traverser<Object>, Integer>function("it.get().value('name').length()"))
            .sideEffect(Lambda.consumer("{ x -> x.sideEffects(\"lengthSum\", x.<Integer>sideEffects('lengthSum') + x.get()) }"))
            .order().by(Lambda.comparator("a,b -> a <=> b"))
            .sack(Lambda.biFunction("{ a,b -> a + b }"))
            .asAdmin();
    final List<Integer> sacks = new ArrayList<>();
    final List<Integer> lengths = new ArrayList<>();
    while (t.hasNext()) {
        final Traverser.Admin<Integer> traverser = t.nextTraverser();
        sacks.add(traverser.sack());
        lengths.add(traverser.get());
    }
    assertFalse(t.hasNext());
    //
    assertEquals(6, lengths.size());
    assertEquals(3, lengths.get(0).intValue());
    assertEquals(3, lengths.get(1).intValue());
    assertEquals(3, lengths.get(2).intValue());
    assertEquals(4, lengths.get(3).intValue());
    assertEquals(5, lengths.get(4).intValue());
    assertEquals(6, lengths.get(5).intValue());
    ///
    assertEquals(6, sacks.size());
    assertEquals(4, sacks.get(0).intValue());
    assertEquals(4, sacks.get(1).intValue());
    assertEquals(4, sacks.get(2).intValue());
    assertEquals(5, sacks.get(3).intValue());
    assertEquals(6, sacks.get(4).intValue());
    assertEquals(7, sacks.get(5).intValue());
    //
    assertEquals(24, t.getSideEffects().<Number>get("lengthSum").intValue());

    final String script = GroovyTranslator.of("g").translate(t.getBytecode()).getScript();
    assertEquals("g.withStrategies(org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.TranslationStrategy.instance())" +
                    ".withSideEffect(\"lengthSum\",(int) 0).withSack((int) 1)" +
                    ".V()" +
                    ".filter({it.get().label().equals('person')})" +
                    ".flatMap({it.get().vertices(Direction.OUT)})" +
                    ".map({it.get().value('name').length()})" +
                    ".sideEffect({ x -> x.sideEffects(\"lengthSum\", x.<Integer>sideEffects('lengthSum') + x.get()) })" +
                    ".order().by({a,b -> a <=> b})" +
                    ".sack({ a,b -> a + b })",
            script);
}
 
Example 9
Source File: ParameterizedGroovyTranslatorProvider.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public GraphTraversalSource traversal(final Graph graph) {
    final GraphTraversalSource g = graph.traversal();
    return g.withStrategies(new TranslationStrategy(g, GroovyTranslator.of("g", true), true));
}
 
Example 10
Source File: TestAndStepBarrier.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void shouldFilterEdgeCriterion() {
    loadModern();
    final Traversal<Edge, ?> edgeCriterion = __.or(
            __.has("weight", 1.0d).hasLabel("knows"), // 8
            __.has("weight", 0.4d).hasLabel("created").outV().has("name", "marko"), // 9
            __.has("weight", 1.0d).hasLabel("created") // 10
    );

    GraphTraversalSource g = this.sqlgGraph.traversal();
    final SubgraphStrategy strategy = SubgraphStrategy.build().edges(edgeCriterion).create();
    final GraphTraversalSource sg = g.withStrategies(strategy);

    // all vertices are here
    Assert.assertEquals(6, g.V().count().next().longValue());
    final Traversal t = sg.V();
    t.hasNext();
    printTraversalForm(t);


    Assert.assertEquals(6, sg.V().count().next().longValue());

    // only the given edges are included
    Assert.assertEquals(6, g.E().count().next().longValue());
    Assert.assertEquals(3, sg.E().count().next().longValue());

    Assert.assertEquals(2, g.V(convertToVertexId("marko")).outE("knows").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("marko")).outE("knows").count().next().longValue());

    // wrapped Traversal<Vertex, Vertex> takes into account the edges it must pass through
    Assert.assertEquals(2, g.V(convertToVertexId("marko")).out("knows").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("marko")).out("knows").count().next().longValue());
    Assert.assertEquals(2, g.V(convertToVertexId("josh")).out("created").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).out("created").count().next().longValue());

    // from vertex

    Assert.assertEquals(2, g.V(convertToVertexId("josh")).outE().count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).outE().count().next().longValue());
    Assert.assertEquals(2, g.V(convertToVertexId("josh")).out().count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).out().count().next().longValue());

    Assert.assertEquals(1, g.V(convertToVertexId("josh")).inE().count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).inE().count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).in().count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).in().count().next().longValue());

    Assert.assertEquals(3, g.V(convertToVertexId("josh")).bothE().count().next().longValue());
    Assert.assertEquals(2, sg.V(convertToVertexId("josh")).bothE().count().next().longValue());
    Assert.assertEquals(3, g.V(convertToVertexId("josh")).both().count().next().longValue());
    Assert.assertEquals(2, sg.V(convertToVertexId("josh")).both().count().next().longValue());

    // with label

    Assert.assertEquals(2, g.V(convertToVertexId("josh")).outE("created").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).outE("created").count().next().longValue());
    Assert.assertEquals(2, g.V(convertToVertexId("josh")).out("created").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).out("created").count().next().longValue());
    Assert.assertEquals(2, g.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
    Assert.assertEquals(2, g.V(convertToVertexId("josh")).both("created").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).both("created").count().next().longValue());

    Assert.assertEquals(1, g.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).in("knows").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).in("knows").count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).both("knows").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).both("knows").count().next().longValue());

    // with branch factor

    Assert.assertEquals(1, g.V(convertToVertexId("josh")).limit(1).local(__.bothE().limit(1)).count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).limit(1).local(__.bothE().limit(1)).count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).limit(1).local(__.bothE().limit(1)).inV().count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).limit(1).local(__.bothE().limit(1)).inV().count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).local(__.bothE("knows", "created").limit(1)).count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).local(__.bothE("knows", "created").limit(1)).count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).local(__.bothE("knows", "created").limit(1)).inV().count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).local(__.bothE("knows", "created").limit(1)).inV().count().next().longValue());

    // from edge

    Assert.assertEquals(2, g.E(convertToEdgeId(this.sqlgGraph, "marko", "knows", "josh")).bothV().count().next().longValue());
    Assert.assertEquals(2, sg.E(convertToEdgeId(this.sqlgGraph, "marko", "knows", "josh")).bothV().count().next().longValue());

    Assert.assertEquals(3, g.E(convertToEdgeId(this.sqlgGraph, "marko", "knows", "josh")).outV().outE().count().next().longValue());
    Assert.assertEquals(2, sg.E(convertToEdgeId(this.sqlgGraph, "marko", "knows", "josh")).outV().outE().count().next().longValue());
}