Search in sources :

Example 1 with CypherAstWrapper

use of org.opencypher.gremlin.translation.CypherAstWrapper 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));
}
Also used : GroovyPredicate(org.opencypher.gremlin.translation.groovy.GroovyPredicate) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) P(org.apache.tinkerpop.gremlin.process.traversal.P) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) ReturnNormalizer(org.opencypher.gremlin.traversal.ReturnNormalizer) CypherAstWrapper(org.opencypher.gremlin.translation.CypherAstWrapper) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with CypherAstWrapper

use of org.opencypher.gremlin.translation.CypherAstWrapper in project cypher-for-gremlin by opencypher.

the class TranslationOnlyClient method run.

@Override
public void run(String cypher) {
    CypherAstWrapper ast = CypherAstWrapper.parse(cypher);
    DefaultGraphTraversal g = new DefaultGraphTraversal();
    Translator<GraphTraversal, P> translator = Translator.builder().traversal(g).build();
    blackhole.consume(ast.buildTranslation(translator));
}
Also used : P(org.apache.tinkerpop.gremlin.process.traversal.P) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) CypherAstWrapper(org.opencypher.gremlin.translation.CypherAstWrapper)

Example 3 with CypherAstWrapper

use of org.opencypher.gremlin.translation.CypherAstWrapper 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));
}
Also used : GroovyPredicate(org.opencypher.gremlin.translation.groovy.GroovyPredicate) ResultSet(org.apache.tinkerpop.gremlin.driver.ResultSet) ReturnNormalizer(org.opencypher.gremlin.traversal.ReturnNormalizer) CypherAstWrapper(org.opencypher.gremlin.translation.CypherAstWrapper)

Example 4 with CypherAstWrapper

use of org.opencypher.gremlin.translation.CypherAstWrapper in project cypher-for-gremlin by opencypher.

the class BytecodeCypherGremlinClient 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<Bytecode, P> translator = Translator.builder().bytecode().build(flavor);
    Bytecode bytecode = ast.buildTranslation(translator);
    CompletableFuture<ResultSet> resultSetFuture = client.submitAsync(bytecode);
    ReturnNormalizer returnNormalizer = ReturnNormalizer.create(ast.getReturnTypes());
    return resultSetFuture.thenApply(ResultSet::iterator).thenApply(resultIterator -> new CypherResultSet(new TraverserIterator(resultIterator), returnNormalizer::normalize));
}
Also used : P(org.apache.tinkerpop.gremlin.process.traversal.P) ResultSet(org.apache.tinkerpop.gremlin.driver.ResultSet) Bytecode(org.apache.tinkerpop.gremlin.process.traversal.Bytecode) ReturnNormalizer(org.opencypher.gremlin.traversal.ReturnNormalizer) CypherAstWrapper(org.opencypher.gremlin.translation.CypherAstWrapper)

Example 5 with CypherAstWrapper

use of org.opencypher.gremlin.translation.CypherAstWrapper in project cypher-for-gremlin by opencypher.

the class InMemoryCypherGremlinClient 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));
    }
    DefaultGraphTraversal g = new DefaultGraphTraversal(gts.clone());
    Translator<GraphTraversal, P> translator = Translator.builder().traversal(g).build();
    GraphTraversal<?, ?> traversal = ast.buildTranslation(translator);
    ReturnNormalizer returnNormalizer = ReturnNormalizer.create(ast.getReturnTypes());
    List<Result> results = traversal.toStream().map(returnNormalizer::normalize).map(Result::new).collect(toList());
    return completedFuture(new CypherResultSet(results.iterator()));
}
Also used : P(org.apache.tinkerpop.gremlin.process.traversal.P) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) ReturnNormalizer(org.opencypher.gremlin.traversal.ReturnNormalizer) CypherAstWrapper(org.opencypher.gremlin.translation.CypherAstWrapper) Result(org.apache.tinkerpop.gremlin.driver.Result)

Aggregations

CypherAstWrapper (org.opencypher.gremlin.translation.CypherAstWrapper)7 P (org.apache.tinkerpop.gremlin.process.traversal.P)4 GroovyPredicate (org.opencypher.gremlin.translation.groovy.GroovyPredicate)4 ReturnNormalizer (org.opencypher.gremlin.traversal.ReturnNormalizer)4 DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)3 GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)3 ResultSet (org.apache.tinkerpop.gremlin.driver.ResultSet)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Result (org.apache.tinkerpop.gremlin.driver.Result)1 Bytecode (org.apache.tinkerpop.gremlin.process.traversal.Bytecode)1 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)1