SparqlTraversalSource extends GraphTraversalSource { public <S> GraphTraversal<S,?> sparql(final String query) { final SparqlTraversalSource clone = this.withStrategies(SparqlStrategy.instance()).clone(); clone.bytecode.addStep(GraphTraversal.Symbols.inject, query); final GraphTraversal.Admin<S, S> traversal = new DefaultGraphTraversal(clone); return traversal.addStep(new InjectStep<>(traversal, query)); } SparqlTraversalSource(final Graph graph, final TraversalStrategies traversalStrategies);  SparqlTraversalSource(final Graph graph);  SparqlTraversalSource(final RemoteConnection connection); @SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException") SparqlTraversalSource clone(); @Override SparqlTraversalSource with(final String key); @Override SparqlTraversalSource with(final String key, final Object value); @Override SparqlTraversalSource withComputer(final Computer computer); @Override SparqlTraversalSource withComputer(final Class<? extends GraphComputer> graphComputerClass); @Override SparqlTraversalSource withComputer(); @Override SparqlTraversalSource withSideEffect(final String key, final Supplier<A> initialValue, final BinaryOperator<A> reducer); @Override SparqlTraversalSource withSideEffect(final String key, final A initialValue, final BinaryOperator<A> reducer); @Override SparqlTraversalSource withSideEffect(final String key, final A initialValue); @Override SparqlTraversalSource withSideEffect(final String key, final Supplier<A> initialValue); @Override SparqlTraversalSource withSack(final Supplier<A> initialValue, final UnaryOperator<A> splitOperator, final BinaryOperator<A> mergeOperator); @Override SparqlTraversalSource withSack(final A initialValue, final UnaryOperator<A> splitOperator, final BinaryOperator<A> mergeOperator); @Override SparqlTraversalSource withSack(final A initialValue); @Override SparqlTraversalSource withSack(final Supplier<A> initialValue); @Override SparqlTraversalSource withSack(final Supplier<A> initialValue, final UnaryOperator<A> splitOperator); @Override SparqlTraversalSource withSack(final A initialValue, final UnaryOperator<A> splitOperator); @Override SparqlTraversalSource withSack(final Supplier<A> initialValue, final BinaryOperator<A> mergeOperator); @Override SparqlTraversalSource withSack(final A initialValue, final BinaryOperator<A> mergeOperator); @Override SparqlTraversalSource withBulk(final boolean useBulk); @Override SparqlTraversalSource withPath(); @Override SparqlTraversalSource withStrategies(final TraversalStrategy... traversalStrategies); @Override @SuppressWarnings({"unchecked"}) SparqlTraversalSource withoutStrategies(final Class<? extends TraversalStrategy>... traversalStrategyClasses); GraphTraversal<S,?> sparql(final String query);  }
@Test public void shouldStartWithSparqlReturningMapAndEndWithGremlin() { final List<?> x = g.sparql("SELECT ?name ?age WHERE { ?person v:name ?name . ?person v:age ?age }").select("name").toList(); assertThat(x, containsInAnyOrder("marko", "vadas", "josh", "peter")); }
@Test public void shouldStartWithSparqlReturningVertexAndEndWithGremlin() { final List<?> x = g.sparql("SELECT * WHERE { }").out("knows").values("name").toList(); assertThat(x, containsInAnyOrder("vadas", "josh")); }
@Test public void shouldFindAllPersonsNamesAndAges() { final List<?> x = g.sparql("SELECT ?name ?age WHERE { ?person v:name ?name . ?person v:age ?age }").toList(); assertThat(x, containsInAnyOrder( new HashMap<String,Object>(){{ put("name", "marko"); put("age", 29); }}, new HashMap<String,Object>(){{ put("name", "vadas"); put("age", 27); }}, new HashMap<String,Object>(){{ put("name", "josh"); put("age", 32); }}, new HashMap<String,Object>(){{ put("name", "peter"); put("age", 35); }} )); }
@Test public void shouldFindAllPersonsNamesAndAgesOrdered() { final List<?> x = g.sparql("SELECT ?name ?age WHERE { ?person v:name ?name . ?person v:age ?age } ORDER BY ASC(?age)").toList(); assertThat(x, contains( new HashMap<String,Object>(){{ put("name", "vadas"); put("age", 27); }}, new HashMap<String,Object>(){{ put("name", "marko"); put("age", 29); }}, new HashMap<String,Object>(){{ put("name", "josh"); put("age", 32); }}, new HashMap<String,Object>(){{ put("name", "peter"); put("age", 35); }} )); }
@Test public void shouldFindAllNamesOrdered() { final List<?> x = g.sparql("SELECT ?name WHERE { ?person v:name ?name } ORDER BY DESC(?name)").toList(); assertThat(x, contains("vadas", "ripple", "peter", "marko", "lop", "josh")); }
@Test public void shouldFilter() { final Map<String,Vertex> x = (Map) g.sparql( "SELECT ?a ?b ?c\n" + "WHERE {\n" + " ?a v:label \"person\" .\n" + " ?a e:knows ?b .\n" + " ?a e:created ?c .\n" + " ?b e:created ?c .\n" + " ?a v:age ?d .\n" + " FILTER (?d < 30)\n" + "}").next(); assertEquals(x.get("a"), _g.V(1).next()); assertEquals(x.get("b"), _g.V(4).next()); assertEquals(x.get("c"), _g.V(3).next()); assertEquals(3, x.size()); }
@Test public void shouldFilterMulti() { final List<String> names = (List<String>) g.sparql( "SELECT DISTINCT ?name\n" + "WHERE {\n" + "?person v:label \"person\" .\n" + "?person v:age ?age .\n" + "?person e:created ?project .\n" + "?project v:name ?name .\n" + "?project v:lang ?lang .\n" + "FILTER (?age > 30 && ?lang = \"java\") }").toList(); assertThat(names, containsInAnyOrder("ripple", "lop")); }
@Test public void shouldDistinct() { final List<?> x = g.sparql( "SELECT DISTINCT ?name\n" + "WHERE {\n" + " ?a e:created ?b .\n" + " ?a v:name ?name .\n" + "}").toList(); assertThat(x, containsInAnyOrder("marko", "josh", "peter")); }
@Test public void shouldDistinctAndOrder() { final List<?> x = g.sparql( "SELECT DISTINCT ?name\n" + "WHERE {\n" + " ?a e:created ?b .\n" + " ?a v:name ?name .\n" + "}" + "ORDER BY ?name").toList(); assertThat(x, contains("josh", "marko", "peter")); }
@Test public void shouldGroup() { final Map<String,Long> x = (Map) g.sparql( "SELECT ?name (COUNT(?name) AS ?name_count)\n" + "WHERE {\n" + " ?a e:created ?b .\n" + " ?a v:name ?name .\n" + "}" + "GROUP BY ?name").next(); assertEquals(new Long(2), x.get("josh")); assertEquals(new Long(1), x.get("peter")); assertEquals(new Long(1), x.get("marko")); assertEquals(3, x.size()); }