Search in sources :

Example 46 with GraphTraversal

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

Example 47 with GraphTraversal

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

the class JanusGraphIoRegistryTest method serializationTest.

private void serializationTest(GraphTraversal[] traversals) throws SerializationException {
    Builder mapper = GryoMapper.build().addRegistry(JanusGraphIoRegistry.instance());
    MessageSerializer binarySerializer = new GryoMessageSerializerV1d0(mapper);
    for (GraphTraversal traversal : traversals) {
        Bytecode expectedBytecode = traversal.asAdmin().getBytecode();
        RequestMessage requestMessage = RequestMessage.build(Tokens.OPS_BYTECODE).processor("traversal").addArg(Tokens.ARGS_GREMLIN, expectedBytecode).create();
        ByteBuf bb = binarySerializer.serializeRequestAsBinary(requestMessage, allocator);
        final int mimeLen = bb.readByte();
        bb.readBytes(new byte[mimeLen]);
        RequestMessage deser = binarySerializer.deserializeRequest(bb);
        Bytecode result = (Bytecode) deser.getArgs().get(Tokens.ARGS_GREMLIN);
        assertEquals(expectedBytecode, result);
    }
}
Also used : MessageSerializer(org.apache.tinkerpop.gremlin.driver.MessageSerializer) Builder(org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper.Builder) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) RequestMessage(org.apache.tinkerpop.gremlin.driver.message.RequestMessage) Bytecode(org.apache.tinkerpop.gremlin.process.traversal.Bytecode) ByteBuf(io.netty.buffer.ByteBuf) GryoMessageSerializerV1d0(org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0)

Example 48 with GraphTraversal

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

the class JanusGraphIoRegistryTest method testJanusGraphPredicatesAsGryo.

@Test
public void testJanusGraphPredicatesAsGryo() throws SerializationException {
    Graph graph = EmptyGraph.instance();
    GraphTraversalSource g = graph.traversal();
    // Janus Graph Geo, Text Predicates
    GraphTraversal[] traversals = { g.E().has("place", Geo.geoIntersect(Geoshape.circle(37.97, 23.72, 50))), g.E().has("place", Geo.geoWithin(Geoshape.circle(37.97, 23.72, 50))), g.E().has("place", Geo.geoDisjoint(Geoshape.circle(37.97, 23.72, 50))), g.V().has("place", Geo.geoContains(Geoshape.point(37.97, 23.72))), g.V().has("name", Text.textContains("neptune")), g.V().has("name", Text.textContainsPrefix("nep")), g.V().has("name", Text.textContainsRegex("nep.*")), g.V().has("name", Text.textPrefix("n")), g.V().has("name", Text.textRegex(".*n.*")), g.V().has("name", Text.textContainsFuzzy("neptun")), g.V().has("name", Text.textFuzzy("nepitne")) };
    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.jupiter.api.Test)

Example 49 with GraphTraversal

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

the class JanusGraphSONModuleTest method testJanusGraphPredicatesAsGraphSON.

@ParameterizedTest(name = "GraphSON Version: {0}")
@EnumSource(value = GraphSONVersion.class, mode = EnumSource.Mode.EXCLUDE, names = { "V1_0" })
public void testJanusGraphPredicatesAsGraphSON(GraphSONVersion graphSONVersion) throws Exception {
    Graph graph = EmptyGraph.instance();
    GraphTraversalSource g = graph.traversal();
    GraphTraversal[] traversals = { g.E().has("place", Geo.geoIntersect(Geoshape.circle(37.97, 23.72, 50))), g.E().has("place", Geo.geoWithin(Geoshape.circle(37.97, 23.72, 50))), g.E().has("place", Geo.geoDisjoint(Geoshape.circle(37.97, 23.72, 50))), g.V().has("place", Geo.geoContains(Geoshape.point(37.97, 23.72))), g.V().has("name", Text.textContains("neptune")), g.V().has("name", Text.textContainsPrefix("nep")), g.V().has("name", Text.textContainsRegex("nep.*")), g.V().has("name", Text.textPrefix("n")), g.V().has("name", Text.textRegex(".*n.*")), g.V().has("name", Text.textContainsFuzzy("neptun")), g.V().has("name", Text.textFuzzy("nepitne")) };
    graphsonSerializationTest(traversals, graphSONVersion);
}
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) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 50 with GraphTraversal

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

the class JanusGraphIoRegistryTest method testTinkerPopPredicatesAsGryo.

/**
 * This is necessary since we replace the default TinkerPop PSerializer
 *
 * @throws Exception
 */
@Test
public void testTinkerPopPredicatesAsGryo() throws SerializationException {
    // Don't change this trivially. At the time of this writing (TinkerPop
    // 3.2.3), this is how many P predicate methods were defined. If this
    // fails, then JanusGraphPSerializer needs to be updated to add/remove
    // any TinkerPop predicates!
    assertEquals(15, Stream.of(P.class.getDeclaredMethods()).filter(m -> Modifier.isStatic(m.getModifiers())).filter(p -> {
        log.debug("Predicate: {}", p);
        return !p.isSynthetic();
    }).count());
    Graph graph = EmptyGraph.instance();
    GraphTraversalSource g = graph.traversal();
    // TinkerPop Predicates
    GraphTraversal[] traversals = { g.V().has("age", within(5000)), g.V().has("age", without(5000)), g.V().has("age", within(5000, 45)), g.V().has("age", inside(45, 5000)), g.V().and(has("age", between(45, 5000)), has("name", within("pluto"))), g.V().or(has("age", between(45, 5000)), has("name", within("pluto", "neptune"))) };
    serializationTest(traversals);
}
Also used : P(org.apache.tinkerpop.gremlin.process.traversal.P) UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) Bytecode(org.apache.tinkerpop.gremlin.process.traversal.Bytecode) Output(org.apache.tinkerpop.shaded.kryo.io.Output) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Graph(org.apache.tinkerpop.gremlin.structure.Graph) Tokens(org.apache.tinkerpop.gremlin.driver.Tokens) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) P.between(org.apache.tinkerpop.gremlin.process.traversal.P.between) Builder(org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper.Builder) Geoshape(org.janusgraph.core.attribute.Geoshape) RequestMessage(org.apache.tinkerpop.gremlin.driver.message.RequestMessage) Kryo(org.apache.tinkerpop.shaded.kryo.Kryo) MessageSerializer(org.apache.tinkerpop.gremlin.driver.MessageSerializer) SerializationException(org.apache.tinkerpop.gremlin.driver.ser.SerializationException) ByteBuf(io.netty.buffer.ByteBuf) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) P.without(org.apache.tinkerpop.gremlin.process.traversal.P.without) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) P(org.apache.tinkerpop.gremlin.process.traversal.P) Geo(org.janusgraph.core.attribute.Geo) Logger(org.slf4j.Logger) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has) GryoMapper(org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper) P.inside(org.apache.tinkerpop.gremlin.process.traversal.P.inside) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Test(org.junit.jupiter.api.Test) Input(org.apache.tinkerpop.shaded.kryo.io.Input) EmptyGraph(org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph) Stream(java.util.stream.Stream) Text(org.janusgraph.core.attribute.Text) Modifier(java.lang.reflect.Modifier) GryoMessageSerializerV1d0(org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0) GraphSONMessageSerializerV3d0(org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV3d0) TOKEN_IO_REGISTRIES(org.apache.tinkerpop.gremlin.driver.ser.AbstractMessageSerializer.TOKEN_IO_REGISTRIES) P.within(org.apache.tinkerpop.gremlin.process.traversal.P.within) Collections(java.util.Collections) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) 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.jupiter.api.Test)

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