Search in sources :

Example 1 with TinkerGraph

use of org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph in project cypher-for-gremlin by opencypher.

the class InMemoryCypherGremlinClientTest method setUp.

@Before
public void setUp() {
    TinkerGraph graph = TinkerGraph.open();
    client = new InMemoryCypherGremlinClient(graph.traversal());
}
Also used : TinkerGraph(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph) Before(org.junit.Before)

Example 2 with TinkerGraph

use of org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph in project cypher-for-gremlin by opencypher.

the class CypherGremlinServerClient method inMemory.

@Test
public void inMemory() {
    // freshReadmeSnippet: inMemory
    TinkerGraph graph = TinkerFactory.createModern();
    GraphTraversalSource traversal = graph.traversal();
    CypherGremlinClient cypherGremlinClient = CypherGremlinClient.inMemory(traversal);
    String cypher = "MATCH (p:person) WHERE p.age > 25 RETURN p.name";
    List<Map<String, Object>> results = cypherGremlinClient.submit(cypher).all();
    // freshReadmeSnippet: inMemory
    assertThat(results).extracting("p.name").containsExactly("marko", "vadas", "josh", "peter");
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) TinkerGraph(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph) CypherGremlinClient(org.opencypher.gremlin.client.CypherGremlinClient) Map(java.util.Map) Test(org.junit.Test)

Example 3 with TinkerGraph

use of org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph in project janusgraph by JanusGraph.

the class TinkerGraphAppTest method createSchema.

@Test
public void createSchema() throws ConfigurationException, IOException {
    final TinkerGraphApp app = new TinkerGraphApp(CONF_FILE);
    final GraphTraversalSource g = app.openGraph();
    app.createSchema();
    final TinkerGraph tinkerGraph = (TinkerGraph) g.getGraph();
    final Set<String> vertexIndexes = tinkerGraph.getIndexedKeys(TinkerVertex.class);
    assertEquals(1, vertexIndexes.size());
    assertEquals("name", vertexIndexes.toArray()[0]);
    final Set<String> edgeIndexes = tinkerGraph.getIndexedKeys(TinkerEdge.class);
    assertTrue(edgeIndexes.isEmpty());
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) TinkerGraph(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph) Test(org.junit.jupiter.api.Test)

Example 4 with TinkerGraph

use of org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph in project titan by thinkaurelius.

the class TitanVertexDeserializer method readHadoopVertex.

// Read a single row from the edgestore and create a TinkerVertex corresponding to the row
// The neighboring vertices are represented by DetachedVertex instances
public TinkerVertex readHadoopVertex(final StaticBuffer key, Iterable<Entry> entries) {
    // Convert key to a vertex ID
    final long vertexId = idManager.getKeyID(key);
    Preconditions.checkArgument(vertexId > 0);
    // Partitioned vertex handling
    if (idManager.isPartitionedVertex(vertexId)) {
        Preconditions.checkState(setup.getFilterPartitionedVertices(), "Read partitioned vertex (ID=%s), but partitioned vertex filtering is disabled.", vertexId);
        log.debug("Skipping partitioned vertex with ID {}", vertexId);
        return null;
    }
    // Create TinkerVertex
    TinkerGraph tg = TinkerGraph.open();
    boolean foundVertexState = !verifyVertexExistence;
    TinkerVertex tv = null;
    // Iterate over edgestore columns to find the vertex's label relation
    for (final Entry data : entries) {
        RelationReader relationReader = setup.getRelationReader(vertexId);
        final RelationCache relation = relationReader.parseRelation(data, false, typeManager);
        if (systemTypes.isVertexLabelSystemType(relation.typeId)) {
            // Found vertex Label
            long vertexLabelId = relation.getOtherVertexId();
            VertexLabel vl = typeManager.getExistingVertexLabel(vertexLabelId);
            // Create TinkerVertex with this label
            //tv = (TinkerVertex)tg.addVertex(T.label, vl.label(), T.id, vertexId);
            tv = getOrCreateVertex(vertexId, vl.name(), tg);
        }
    }
    // Added this following testing
    if (null == tv) {
        //tv = (TinkerVertex)tg.addVertex(T.id, vertexId);
        tv = getOrCreateVertex(vertexId, null, tg);
    }
    Preconditions.checkState(null != tv, "Unable to determine vertex label for vertex with ID %s", vertexId);
    // Iterate over and decode edgestore columns (relations) on this vertex
    for (final Entry data : entries) {
        try {
            RelationReader relationReader = setup.getRelationReader(vertexId);
            final RelationCache relation = relationReader.parseRelation(data, false, typeManager);
            if (systemTypes.isVertexExistsSystemType(relation.typeId)) {
                foundVertexState = true;
            }
            //Ignore system types
            if (systemTypes.isSystemType(relation.typeId))
                continue;
            final RelationType type = typeManager.getExistingRelationType(relation.typeId);
            //Ignore hidden types
            if (((InternalRelationType) type).isInvisibleType())
                continue;
            // Decode and create the relation (edge or property)
            if (type.isPropertyKey()) {
                // Decode property
                Object value = relation.getValue();
                Preconditions.checkNotNull(value);
                VertexProperty.Cardinality card = getPropertyKeyCardinality(type.name());
                tv.property(card, type.name(), value, T.id, relation.relationId);
            } else {
                assert type.isEdgeLabel();
                // Partitioned vertex handling
                if (idManager.isPartitionedVertex(relation.getOtherVertexId())) {
                    Preconditions.checkState(setup.getFilterPartitionedVertices(), "Read edge incident on a partitioned vertex, but partitioned vertex filtering is disabled.  " + "Relation ID: %s.  This vertex ID: %s.  Other vertex ID: %s.  Edge label: %s.", relation.relationId, vertexId, relation.getOtherVertexId(), type.name());
                    log.debug("Skipping edge with ID {} incident on partitioned vertex with ID {} (and nonpartitioned vertex with ID {})", relation.relationId, relation.getOtherVertexId(), vertexId);
                    continue;
                }
                // Decode edge
                TinkerEdge te;
                if (relation.direction.equals(Direction.IN)) {
                    // We don't know the label of the other vertex, but one must be provided
                    TinkerVertex outV = getOrCreateVertex(relation.getOtherVertexId(), null, tg);
                    te = (TinkerEdge) outV.addEdge(type.name(), tv, T.id, relation.relationId);
                } else if (relation.direction.equals(Direction.OUT)) {
                    // We don't know the label of the other vertex, but one must be provided
                    TinkerVertex inV = getOrCreateVertex(relation.getOtherVertexId(), null, tg);
                    te = (TinkerEdge) tv.addEdge(type.name(), inV, T.id, relation.relationId);
                } else {
                    throw new RuntimeException("Direction.BOTH is not supported");
                }
                if (relation.hasProperties()) {
                    // Load relation properties
                    for (final LongObjectCursor<Object> next : relation) {
                        assert next.value != null;
                        RelationType rt = typeManager.getExistingRelationType(next.key);
                        if (rt.isPropertyKey()) {
                            //                                PropertyKey pkey = (PropertyKey)vertex.getTypeManager().getPropertyKey(rt.name());
                            //                                log.debug("Retrieved key {} for name \"{}\"", pkey, rt.name());
                            //                                frel.property(pkey.label(), next.value);
                            te.property(rt.name(), next.value);
                        } else {
                            throw new RuntimeException("Metaedges are not supported");
                        //                                assert next.value instanceof Long;
                        //                                EdgeLabel el = (EdgeLabel)vertex.getTypeManager().getEdgeLabel(rt.name());
                        //                                log.debug("Retrieved ege label {} for name \"{}\"", el, rt.name());
                        //                                frel.setProperty(el, new FaunusVertex(configuration,(Long)next.value));
                        }
                    }
                }
            }
        //                // Iterate over and copy the relation's metaproperties
        //                if (relation.hasProperties()) {
        //                    // Load relation properties
        //                    for (final LongObjectCursor<Object> next : relation) {
        //                        assert next.value != null;
        //                        RelationType rt = typeManager.getExistingRelationType(next.key);
        //                        if (rt.isPropertyKey()) {
        //                            PropertyKey pkey = (PropertyKey)vertex.getTypeManager().getPropertyKey(rt.name());
        //                            log.debug("Retrieved key {} for name \"{}\"", pkey, rt.name());
        //                            frel.property(pkey.label(), next.value);
        //                        } else {
        //                            assert next.value instanceof Long;
        //                            EdgeLabel el = (EdgeLabel)vertex.getTypeManager().getEdgeLabel(rt.name());
        //                            log.debug("Retrieved ege label {} for name \"{}\"", el, rt.name());
        //                            frel.setProperty(el, new FaunusVertex(configuration,(Long)next.value));
        //                        }
        //                    }
        //                    for (TitanRelation rel : frel.query().queryAll().relations())
        //                        ((FaunusRelation)rel).setLifeCycle(ElementLifeCycle.Loaded);
        //                }
        //                frel.setLifeCycle(ElementLifeCycle.Loaded);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /*Since we are filtering out system relation types, we might end up with vertices that have no incident relations.
         This is especially true for schema vertices. Those are filtered out.     */
    if (!foundVertexState) {
        log.trace("Vertex {} has unknown lifecycle state", vertexId);
        return null;
    } else if (!tv.edges(Direction.BOTH).hasNext() && !tv.properties().hasNext()) {
        log.trace("Vertex {} has no relations", vertexId);
        return null;
    }
    return tv;
}
Also used : TinkerVertex(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex) TinkerGraph(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph) RelationCache(com.thinkaurelius.titan.graphdb.relations.RelationCache) NoSuchElementException(java.util.NoSuchElementException) Entry(com.thinkaurelius.titan.diskstorage.Entry) RelationReader(com.thinkaurelius.titan.graphdb.database.RelationReader) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) TinkerEdge(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerEdge) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty)

Example 5 with TinkerGraph

use of org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph in project grakn by graknlabs.

the class GraknTxTinkerFactoryTest method whenBuildingTxFromTheSameFactory_ReturnSingletonGraphs.

@Test
public void whenBuildingTxFromTheSameFactory_ReturnSingletonGraphs() {
    GraknTx tx1 = tinkerGraphFactory.open(GraknTxType.WRITE);
    TinkerGraph tinkerGraph1 = ((GraknTxTinker) tx1).getTinkerPopGraph();
    tx1.close();
    GraknTx tx1_copy = tinkerGraphFactory.open(GraknTxType.WRITE);
    tx1_copy.close();
    GraknTx tx2 = tinkerGraphFactory.open(GraknTxType.BATCH);
    TinkerGraph tinkerGraph2 = ((GraknTxTinker) tx2).getTinkerPopGraph();
    tx2.close();
    GraknTx tx2_copy = tinkerGraphFactory.open(GraknTxType.BATCH);
    assertEquals(tx1, tx1_copy);
    assertEquals(tx2, tx2_copy);
    assertNotEquals(tx1, tx2);
    assertEquals(tinkerGraph1, tinkerGraph2);
}
Also used : GraknTx(ai.grakn.GraknTx) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) TinkerGraph(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph) GraknTxTinker(ai.grakn.kb.internal.GraknTxTinker) Test(org.junit.Test)

Aggregations

TinkerGraph (org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph)7 NoSuchElementException (java.util.NoSuchElementException)2 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)2 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)2 TinkerEdge (org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerEdge)2 TinkerVertex (org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex)2 Test (org.junit.Test)2 GraknTx (ai.grakn.GraknTx)1 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)1 GraknTxTinker (ai.grakn.kb.internal.GraknTxTinker)1 Entry (com.thinkaurelius.titan.diskstorage.Entry)1 RelationReader (com.thinkaurelius.titan.graphdb.database.RelationReader)1 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)1 RelationCache (com.thinkaurelius.titan.graphdb.relations.RelationCache)1 Map (java.util.Map)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1 RelationType (org.janusgraph.core.RelationType)1 VertexLabel (org.janusgraph.core.VertexLabel)1 Entry (org.janusgraph.diskstorage.Entry)1 RelationReader (org.janusgraph.graphdb.database.RelationReader)1