Search in sources :

Example 1 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project grakn by graknlabs.

the class GraqlTraversal method getGraphTraversal.

/**
 * Get the {@code GraphTraversal} that this {@code GraqlTraversal} represents.
 */
// Because 'union' accepts an array, we can't use generics
@SuppressWarnings("unchecked")
public GraphTraversal<Vertex, Map<String, Element>> getGraphTraversal(EmbeddedGraknTx<?> tx, Set<Var> vars) {
    if (fragments().size() == 1) {
        // If there are no disjunctions, we don't need to union them and get a performance boost
        ImmutableList<Fragment> list = Iterables.getOnlyElement(fragments());
        return getConjunctionTraversal(tx, tx.getTinkerTraversal().V(), vars, list);
    } else {
        Traversal[] traversals = fragments().stream().map(list -> getConjunctionTraversal(tx, __.V(), vars, list)).toArray(Traversal[]::new);
        // This is a sneaky trick - we want to do a union but tinkerpop requires all traversals to start from
        // somewhere, so we start from a single arbitrary vertex.
        GraphTraversal traversal = tx.getTinkerTraversal().V().limit(1).union(traversals);
        return selectVars(traversal, vars);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) SelectStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectStep) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) ConceptId(ai.grakn.concept.ConceptId) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Collectors.joining(java.util.stream.Collectors.joining) Match(ai.grakn.graql.Match) List(java.util.List) Element(org.apache.tinkerpop.gremlin.structure.Element) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) Schema(ai.grakn.util.Schema) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment)

Example 2 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project grakn by graknlabs.

the class JanusPreviousPropertyStepTest method optimisedTraversal.

private GraphTraversal<Vertex, Vertex> optimisedTraversal(GraphTraversalSource g) {
    GraphTraversal expected = g.V().outE().values("e prop").as("x");
    GraphTraversal.Admin<Vertex, Object> admin = expected.asAdmin();
    JanusPreviousPropertyStep<?> graphStep = new JanusPreviousPropertyStep<>(admin, "v prop", "x");
    admin.addStep(graphStep);
    admin.applyStrategies();
    return expected;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)

Example 3 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project janusgraph by JanusGraph.

the class JanusGraphIoRegistryTest method testGeoshapAsGryo.

@Test
public void testGeoshapAsGryo() throws SerializationException {
    Graph graph = EmptyGraph.instance();
    GraphTraversalSource g = graph.traversal();
    GraphTraversal[] traversals = { g.addV().property("loc", Geoshape.box(0.1d, 0.2d, 0.3d, 0.4d)), g.addV().property("loc", Geoshape.box(0.1f, 0.3f, 0.5f, 0.6f)), g.addV().property("loc", Geoshape.circle(0.1d, 0.3d, 0.3d)), g.addV().property("loc", Geoshape.circle(0.2f, 0.4f, 0.5f)), g.addV().property("loc", Geoshape.point(1.0d, 4.0d)), g.addV().property("loc", Geoshape.point(1.0f, 1.0f)) };
    serializationTest(traversals);
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Graph(org.apache.tinkerpop.gremlin.structure.Graph) EmptyGraph(org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Test(org.junit.Test)

Example 4 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal 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 5 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal 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)

Aggregations

GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)93 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)67 Test (org.junit.Test)57 List (java.util.List)51 TestGraphBuilder.newGraph (nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph)50 VertexMatcher.likeVertex (nl.knaw.huygens.timbuctoo.util.VertexMatcher.likeVertex)44 Before (org.junit.Before)42 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)40 Matchers.contains (org.hamcrest.Matchers.contains)39 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)35 Lists (com.google.common.collect.Lists)34 FacetValue (nl.knaw.huygens.timbuctoo.search.FacetValue)31 Optional (java.util.Optional)24 P (org.apache.tinkerpop.gremlin.process.traversal.P)21 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__)20 ListFacetValue (nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.ListFacetValue)19 Matchers.is (org.hamcrest.Matchers.is)17 Mockito.mock (org.mockito.Mockito.mock)17 Matchers.empty (org.hamcrest.Matchers.empty)15 Edge (org.apache.tinkerpop.gremlin.structure.Edge)14