use of org.opencypher.gremlin.translation.groovy.GroovyPredicate in project cypher-for-gremlin by opencypher.
the class CypherOpProcessor method evalCypher.
private void evalCypher(Context context) throws OpProcessorException {
Map<String, Object> args = context.getRequestMessage().getArgs();
String cypher = (String) args.get(Tokens.ARGS_GREMLIN);
logger.info("Cypher: {}", cypher.replaceAll("\n", " "));
GraphTraversalSource gts = traversal(context);
DefaultGraphTraversal g = new DefaultGraphTraversal(gts.clone());
Map<String, Object> parameters = getParameters(args);
CypherAstWrapper ast = CypherAstWrapper.parse(cypher, parameters);
Translator<String, GroovyPredicate> stringTranslator = Translator.builder().gremlinGroovy().inlineParameters().build();
String gremlin = ast.buildTranslation(stringTranslator);
logger.info("Gremlin: {}", gremlin);
if (ast.getOptions().contains(EXPLAIN)) {
explainQuery(context, ast, gremlin);
return;
}
Translator<GraphTraversal, P> traversalTranslator = Translator.builder().traversal(g).build();
GraphTraversal<?, ?> traversal = ast.buildTranslation(traversalTranslator);
ReturnNormalizer returnNormalizer = ReturnNormalizer.create(ast.getReturnTypes());
Traversal<?, Map<String, Object>> normalizedTraversal = traversal.map(returnNormalizer::normalize);
inTransaction(gts, () -> handleIterator(context, normalizedTraversal));
}
use of org.opencypher.gremlin.translation.groovy.GroovyPredicate in project cypher-for-gremlin by opencypher.
the class GroovyCypherGremlinClient method submitAsync.
@Override
public CompletableFuture<CypherResultSet> submitAsync(String cypher, Map<String, ?> parameters) {
CypherAstWrapper ast;
try {
ast = CypherAstWrapper.parse(cypher, parameters);
} catch (Exception e) {
return completedFuture(exceptional(e));
}
if (ast.getOptions().contains(EXPLAIN)) {
return completedFuture(explain(ast));
}
Translator<String, GroovyPredicate> translator = Translator.builder().gremlinGroovy().build(flavor);
String gremlin = ast.buildTranslation(translator);
Map<String, Object> extractedParameters = ast.getExtractedParameters();
CompletableFuture<ResultSet> resultSetFuture = client.submitAsync(gremlin, extractedParameters);
ReturnNormalizer returnNormalizer = ReturnNormalizer.create(ast.getReturnTypes());
return resultSetFuture.thenApply(ResultSet::iterator).thenApply(resultIterator -> new CypherResultSet(resultIterator, returnNormalizer::normalize));
}
use of org.opencypher.gremlin.translation.groovy.GroovyPredicate in project cypher-for-gremlin by opencypher.
the class CommonResultSets method explain.
static CypherResultSet explain(CypherAstWrapper ast) {
Map<String, Object> explanation = new LinkedHashMap<>();
Translator<String, GroovyPredicate> translator = Translator.builder().gremlinGroovy().inlineParameters().build();
explanation.put("translation", ast.buildTranslation(translator));
explanation.put("options", ast.getOptions().toString());
Iterator<Result> iterator = singletonIterator(() -> new Result(explanation));
return new CypherResultSet(iterator);
}
use of org.opencypher.gremlin.translation.groovy.GroovyPredicate in project cypher-for-gremlin by opencypher.
the class Translation method translateVerbose.
@Test
public void translateVerbose() throws Exception {
// freshReadmeSnippet: verbose
String cypher = "MATCH (p:Person) WHERE p.age > 25 RETURN p.name";
CypherAstWrapper ast = CypherAstWrapper.parse(cypher);
Translator<String, GroovyPredicate> translator = Translator.builder().gremlinGroovy().build();
String gremlin = ast.buildTranslation(translator);
// freshReadmeSnippet: verbose
assertThat(gremlin).startsWith("g.V()");
}
use of org.opencypher.gremlin.translation.groovy.GroovyPredicate in project cypher-for-gremlin by opencypher.
the class NoCustomFunctionsTest method functionsAndPredicates.
@Test
public void functionsAndPredicates() {
String cypher = "MATCH (n:N) " + "WITH n.p AS s " + "WHERE s STARTS WITH 'x' " + "AND s ENDS WITH 'x' " + "AND s CONTAINS 'x' " + "RETURN length(s), toString(s)";
CypherAstWrapper ast = CypherAstWrapper.parse(cypher);
Translator<String, GroovyPredicate> translator = Translator.builder().gremlinGroovy().build(flavor);
assertThatThrownBy(() -> ast.buildTranslation(translator)).hasMessageContaining("contains, convertToString, endsWith, length, starsWith");
}
Aggregations