Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal#Admin

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal#Admin . 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: CoreTraversalTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldThrowFastNoSuchElementExceptionInNestedTraversals() {
    //The nested traversal should throw a regular FastNoSuchElementException

    final GraphTraversal<Object, Object> nestedTraversal = __.has("name", "foo");
    final GraphTraversal<Vertex, Object> traversal = g.V().has("name", "marko").branch(nestedTraversal);

    final GraphTraversal.Admin<Object, Object> nestedTraversalAdmin = nestedTraversal.asAdmin();
    nestedTraversalAdmin.reset();
    nestedTraversalAdmin.addStart(nestedTraversalAdmin.getTraverserGenerator().generate(g.V().has("name", "marko").next(), (Step) traversal.asAdmin().getStartStep(), 1l));

    try {
        nestedTraversal.next();
    } catch (NoSuchElementException e) {
        assertEquals(FastNoSuchElementException.class, e.getClass());
    }
}
 
Example 2
Source File: InlineFilterStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static GraphTraversal.Admin<?, ?> addHas(final GraphTraversal<?, ?> traversal, final Object... hasKeyValues) {
    final HasStep<?> hasStep = new HasStep<>((Traversal.Admin) traversal);
    for (int i = 0; i < hasKeyValues.length; i = i + 2) {
        hasStep.addHasContainer(new HasContainer((String) hasKeyValues[i], (P) hasKeyValues[i + 1]));
    }
    return traversal.asAdmin().addStep(hasStep);
}
 
Example 3
Source File: JavaTranslatorBenchmark.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Benchmark
public GraphTraversal.Admin<Vertex,Vertex> testTranslationLong() {
    return translator.translate(g.V().match(
            as("a").has("song", "name", "HERE COMES SUNSHINE"),
            as("a").map(inE("followedBy").values("weight").mean()).as("b"),
            as("a").inE("followedBy").as("c"),
            as("c").filter(values("weight").where(P.gte("b"))).outV().as("d")).
            <String>select("d").by("name").asAdmin().getBytecode());
}
 
Example 4
Source File: IncidentToAdjacentStrategyProcessTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldGenerateCorrectTraversers() throws Exception {

    final GraphTraversalSource itag = g.withStrategies(IncidentToAdjacentStrategy.instance());
    
    GraphTraversal.Admin traversal;

    traversal = itag.V().outE().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);
    traversal = itag.V().outE().inV().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);
    traversal = itag.V().outE().otherV().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);
    traversal = itag.V().out().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);

    traversal = itag.V().bothE().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);
    traversal = itag.V().bothE().otherV().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);
    traversal = itag.V().both().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);

    traversal = itag.V().flatMap(bothE()).iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);
    traversal = itag.V().flatMap(bothE().otherV()).iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);
    traversal = itag.V().flatMap(both()).iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);
}
 
Example 5
Source File: SubgraphStrategyProcessTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldGenerateCorrectTraversers() throws Exception {

    assumeThat(graph, Matchers.not(Matchers.instanceOf(RemoteGraph.class)));

    GraphTraversalSource sg;
    GraphTraversal.Admin traversal;
    SubgraphStrategy strategy;

    strategy = SubgraphStrategy.build().vertices(has("name", P.within("josh", "lop", "ripple"))).create();
    sg = g.withStrategies(strategy);

    traversal = sg.V().outE().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);
    traversal = sg.V().outE().inV().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);
    traversal = sg.V().out().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);

    traversal = sg.V().bothE().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);
    traversal = sg.V().bothE().otherV().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_LP_O_P_S_SE_SL_TraverserGenerator);
    traversal = sg.V().both().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_LP_O_P_S_SE_SL_TraverserGenerator);

    traversal = sg.V().flatMap(bothE()).iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_O_TraverserGenerator);
    traversal = sg.V().flatMap(bothE().otherV()).iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_LP_O_P_S_SE_SL_TraverserGenerator);
    traversal = sg.V().flatMap(both()).iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_LP_O_P_S_SE_SL_TraverserGenerator);

    strategy = SubgraphStrategy.build().vertices(__.filter(__.simplePath())).create();
    sg = g.withStrategies(strategy);

    traversal = sg.V().out().iterate().asAdmin();
    assertTrue(traversal.getTraverserGenerator() instanceof B_LP_O_P_S_SE_SL_TraverserGenerator);
}
 
Example 6
Source File: TinkerGraphStepStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static GraphTraversal.Admin<?, ?> g_V(final Object... hasKeyValues) {
    final GraphTraversal.Admin<?, ?> traversal = new DefaultGraphTraversal<>();
    final TinkerGraphStep<Vertex, Vertex> graphStep = new TinkerGraphStep<>(new GraphStep<>(traversal, Vertex.class, true));
    for (int i = 0; i < hasKeyValues.length; i = i + 2) {
        graphStep.addHasContainer(new HasContainer((String) hasKeyValues[i], (P) hasKeyValues[i + 1]));
    }
    return traversal.addStep(graphStep);
}
 
Example 7
Source File: TinkerGraphStepStrategyTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
private static GraphTraversal.Admin<?, ?> g_V(final Object... hasKeyValues) {
    final GraphTraversal.Admin<?, ?> traversal = new DefaultGraphTraversal<>();
    final TinkerGraphStep<Vertex, Vertex> graphStep = new TinkerGraphStep<>(new GraphStep<>(traversal, Vertex.class, true));
    for (int i = 0; i < hasKeyValues.length; i = i + 2) {
        graphStep.addHasContainer(new HasContainer((String) hasKeyValues[i], (P) hasKeyValues[i + 1]));
    }
    return traversal.addStep(graphStep);
}
 
Example 8
Source File: SparqlTraversalSource.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * The start step for a SPARQL based traversal that accepts a string representation of the query to execute.
 */
public <S> GraphTraversal<S,?> sparql(final String query) {
    final SparqlTraversalSource clone = this.withStrategies(SparqlStrategy.instance()).clone();

    // the inject() holds the sparql which the SparqlStrategy then detects and converts to a traversal
    clone.bytecode.addStep(GraphTraversal.Symbols.inject, query);
    final GraphTraversal.Admin<S, S> traversal = new DefaultGraphTraversal(clone);
    return traversal.addStep(new InjectStep<>(traversal, query));
}
 
Example 9
Source File: Neo4jGraphStepStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static GraphTraversal.Admin<?, ?> g_V(final Object... hasKeyValues) {
    final GraphTraversal.Admin<?, ?> traversal = new DefaultGraphTraversal<>();
    final Neo4jGraphStep<Vertex, Vertex> graphStep = new Neo4jGraphStep<>(new GraphStep<>(traversal, Vertex.class, true));
    for (int i = 0; i < hasKeyValues.length; i = i + 2) {
        graphStep.addHasContainer(new HasContainer((String) hasKeyValues[i], (P) hasKeyValues[i + 1]));
    }
    return traversal.addStep(graphStep);
}
 
Example 10
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 11
Source File: JavaTranslatorBenchmark.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Benchmark
public GraphTraversal.Admin<Vertex,Vertex> testTranslationWithStrategy() {
    return translator.translate(g.withStrategies(ReadOnlyStrategy.instance())
            .withStrategies(SubgraphStrategy.build().vertices(hasLabel("person")).create())
            .V().out().in("link").out().in("link").asAdmin().getBytecode());
}
 
Example 12
Source File: JavaTranslatorBenchmark.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Benchmark
public GraphTraversal.Admin<Vertex,Vertex> testTranslationMedium() {
    return translator.translate(g.V().out().in("link").out().in("link").asAdmin().getBytecode());
}
 
Example 13
Source File: JavaTranslatorBenchmark.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Benchmark
public GraphTraversal.Admin<Vertex,Vertex> testTranslationShort() {
    return translator.translate(g.V().asAdmin().getBytecode());
}
 
Example 14
Source File: GryoSerializerIntegrateTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldHaveAllRegisteredGryoSerializerClasses() throws Exception {
    // this is a stress test that ensures that when data is spilling to disk, persisted to an RDD, etc. the correct classes are registered with GryoSerializer.
    final TinkerGraph randomGraph = TinkerGraph.open();
    int totalVertices = 200000;
    TestHelper.createRandomGraph(randomGraph, totalVertices, 100);
    final String inputLocation = TestHelper.makeTestDataFile(GryoSerializerIntegrateTest.class,
                                                             UUID.randomUUID().toString(),
                                                             "random-graph.kryo");
    randomGraph.io(IoCore.gryo()).writeGraph(inputLocation);
    randomGraph.clear();
    randomGraph.close();

    final String outputLocation = TestHelper.makeTestDataDirectory(GryoSerializerIntegrateTest.class, UUID.randomUUID().toString());
    Configuration configuration = getBaseConfiguration();
    configuration.clearProperty(Constants.SPARK_SERIALIZER); // ensure proper default to GryoSerializer
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, inputLocation);
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, GryoInputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, GryoOutputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, outputLocation);
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, false);
    Graph graph = GraphFactory.open(configuration);
    final GraphTraversal.Admin<Vertex, Map<Vertex, Collection<Vertex>>> traversal = graph.traversal().withComputer(SparkGraphComputer.class).V().group("m").<Map<Vertex, Collection<Vertex>>>cap("m").asAdmin();
    assertTrue(traversal.hasNext());
    assertEquals(traversal.next(), traversal.getSideEffects().get("m"));
    assertFalse(traversal.hasNext());
    assertTrue(traversal.getSideEffects().exists("m"));
    assertTrue(traversal.getSideEffects().get("m") instanceof Map);
    assertEquals(totalVertices, traversal.getSideEffects().<Map>get("m").size());

    configuration = getBaseConfiguration();
    configuration.clearProperty(Constants.SPARK_SERIALIZER); // ensure proper default to GryoSerializer
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, inputLocation);
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, GryoInputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, PersistedOutputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_STORAGE_LEVEL, "DISK_ONLY");
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, "persisted-rdd");
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, true);
    graph = GraphFactory.open(configuration);
    assertEquals(totalVertices, graph.compute(SparkGraphComputer.class).program(PageRankVertexProgram.build().iterations(2).create(graph)).submit().get().graph().traversal().V().count().next().longValue());

    configuration = getBaseConfiguration();
    configuration.clearProperty(Constants.SPARK_SERIALIZER); // ensure proper default to GryoSerializer
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, "persisted-rdd");
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, PersistedInputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, GryoOutputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, outputLocation);
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, true);
    graph = GraphFactory.open(configuration);
    assertEquals(totalVertices, graph.traversal().withComputer(SparkGraphComputer.class).V().count().next().longValue());

    configuration = getBaseConfiguration();
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, "persisted-rdd");
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, PersistedInputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, PersistedOutputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, outputLocation);
    configuration.setProperty(Constants.GREMLIN_SPARK_GRAPH_STORAGE_LEVEL, "MEMORY_ONLY"); // this should be ignored as you can't change the persistence level once created
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_STORAGE_LEVEL, "MEMORY_AND_DISK");
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, true);
    graph = GraphFactory.open(configuration);
    assertEquals(totalVertices, graph.traversal().withComputer(SparkGraphComputer.class).V().count().next().longValue());
}
 
Example 15
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 16
Source File: VertexProgramStrategyTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
private static GraphTraversal.Admin<?, ?> start() {
    return new DefaultGraphTraversal<>().asAdmin();
}
 
Example 17
Source File: TitanGraphStepStrategyTest.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
@Test
@IgnoreEngine(TraversalEngine.Type.COMPUTER)
public void shouldFoldInHasContainers() {
    GraphTraversal.Admin traversal = g.V().has("name", "marko").asAdmin();
    assertEquals(2, traversal.getSteps().size());
    assertEquals(HasStep.class, traversal.getEndStep().getClass());
    traversal.applyStrategies();
    assertEquals(1, traversal.getSteps().size());
    assertEquals(TitanGraphStep.class, traversal.getStartStep().getClass());
    assertEquals(TitanGraphStep.class, traversal.getEndStep().getClass());
    assertEquals(1, ((TitanGraphStep) traversal.getStartStep()).getHasContainers().size());
    assertEquals("name", ((TitanGraphStep<?, ?>) traversal.getStartStep()).getHasContainers().get(0).getKey());
    assertEquals("marko", ((TitanGraphStep<?, ?>) traversal.getStartStep()).getHasContainers().get(0).getValue());
    ////
    traversal = g.V().has("name", "marko").has("age", P.gt(20)).asAdmin();
    traversal.applyStrategies();
    assertEquals(1, traversal.getSteps().size());
    assertEquals(TitanGraphStep.class, traversal.getStartStep().getClass());
    assertEquals(2, ((TitanGraphStep) traversal.getStartStep()).getHasContainers().size());
    ////
    traversal = g.V().has("name", "marko").out().has("name", "daniel").asAdmin();
    traversal.applyStrategies();
    assertEquals(3, traversal.getSteps().size());
    assertEquals(TitanGraphStep.class, traversal.getStartStep().getClass());
    assertEquals(1, ((TitanGraphStep) traversal.getStartStep()).getHasContainers().size());
    assertEquals("name", ((TitanGraphStep<?, ?>) traversal.getStartStep()).getHasContainers().get(0).getKey());
    assertEquals("marko", ((TitanGraphStep<?, ?>) traversal.getStartStep()).getHasContainers().get(0).getValue());
    assertEquals(HasStep.class, traversal.getEndStep().getClass());
    ////
    traversal = g.V().has("name", "marko").out().V().has("name", "daniel").asAdmin();
    traversal.applyStrategies();
    assertEquals(3, traversal.getSteps().size());
    assertEquals(TitanGraphStep.class, traversal.getStartStep().getClass());
    assertEquals(1, ((TitanGraphStep) traversal.getStartStep()).getHasContainers().size());
    assertEquals("name", ((TitanGraphStep<?, ?>) traversal.getStartStep()).getHasContainers().get(0).getKey());
    assertEquals("marko", ((TitanGraphStep<?, ?>) traversal.getStartStep()).getHasContainers().get(0).getValue());
    assertEquals(TitanGraphStep.class, traversal.getSteps().get(2).getClass());
    assertEquals(1, ((TitanGraphStep) traversal.getSteps().get(2)).getHasContainers().size());
    assertEquals("name", ((TitanGraphStep<?, ?>) traversal.getSteps().get(2)).getHasContainers().get(0).getKey());
    assertEquals("daniel", ((TitanGraphStep<?,?>) traversal.getSteps().get(2)).getHasContainers().get(0).getValue());
    assertEquals(TitanGraphStep.class, traversal.getEndStep().getClass());
}
 
Example 18
Source File: Neo4jGraph.java    From tinkerpop with Apache License 2.0 3 votes vote down vote up
/**
 * Execute the Cypher query with provided parameters and get the result set as a {@link GraphTraversal}.
 *
 * @param query      the Cypher query to execute
 * @param parameters the parameters of the Cypher query
 * @return a fluent Gremlin traversal
 */
public <S, E> GraphTraversal<S, E> cypher(final String query, final Map<String, Object> parameters) {
    this.tx().readWrite();
    final GraphTraversal.Admin<S, E> traversal = new DefaultGraphTraversal<>(this);
    traversal.addStep(new CypherStartStep(traversal, query, new Neo4jCypherIterator<>((Iterator) this.baseGraph.execute(query, parameters), this)));
    return traversal;
}