org.apache.tinkerpop.gremlin.structure.Graph类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(17.4k)|赞(0)|评价(0)|浏览(185)

本文整理了Java中org.apache.tinkerpop.gremlin.structure.Graph类的一些代码示例,展示了Graph类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graph类的具体详情如下:
包路径:org.apache.tinkerpop.gremlin.structure.Graph
类名称:Graph

Graph介绍

[英]A Graph is a container object for a collection of Vertex, Edge, VertexProperty, and Property objects.
[中]图形是顶点、边、VertexProperty和特性对象集合的容器对象。

代码示例

代码示例来源:origin: JanusGraph/janusgraph

private void testOr(final Graph aGraph) {
  final GraphTraversalSource g = aGraph.traversal();
  final Vertex hiro = g.addV().property("name", "Hiro").property("age", 2).property("length", 90).next();
  final Vertex totoro = g.addV().property("name", "Totoro").property("age", 1).next();
  final Vertex john = g.addV().property("name", "John").property("age", 3).property("length", 110).next();
  final Vertex mike = g.addV().property("name", "Mike").property("age", 4).property("length", 130).next();
  aGraph.tx().commit();
  assertCount(1, g.V().has("name", "Totoro"));

代码示例来源:origin: thinkaurelius/titan

@Override
public void clear(Graph g, final Configuration configuration) throws Exception {
  if (null != g) {
    while (g instanceof WrappedGraph) g = ((WrappedGraph<? extends Graph>) g).getBaseGraph();
    TitanGraph graph = (TitanGraph) g;
    if (graph.isOpen()) {
      if (g.tx().isOpen()) g.tx().rollback();
      g.close();
    }
  }
  WriteConfiguration config = new CommonsConfiguration(configuration);
  BasicConfiguration readConfig = new BasicConfiguration(GraphDatabaseConfiguration.ROOT_NS, config, BasicConfiguration.Restriction.NONE);
  if (readConfig.has(GraphDatabaseConfiguration.STORAGE_BACKEND)) {
    TitanGraphBaseTest.clearGraph(config);
  }
}

代码示例来源:origin: thinkaurelius/titan

resultgraph = graph.newTransaction();
for (Map.Entry<Long, Map<String, Object>> vprop : mutatedProperties.entrySet()) {
  Vertex v = resultgraph.vertices(vprop.getKey()).next();
  for (Map.Entry<String, Object> prop : vprop.getValue().entrySet()) {
    v.property(VertexProperty.Cardinality.single, prop.getKey(), prop.getValue());

代码示例来源:origin: thinkaurelius/titan

originalGraphStep.getTraversal().getGraph().get().vertices(elementIds) :
originalGraphStep.getTraversal().getGraph().get().edges(elementIds)));

代码示例来源:origin: apache/tinkerpop

@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
public void shouldTriggerAddVertexPropertyChanged() {
  final StubMutationListener listener1 = new StubMutationListener();
  final StubMutationListener listener2 = new StubMutationListener();
  final EventStrategy.Builder builder = EventStrategy.build()
      .addListener(listener1)
      .addListener(listener2);
  if (graph.features().graph().supportsTransactions())
    builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph));
  final EventStrategy eventStrategy = builder.create();
  final Vertex vSome = graph.addVertex("some", "thing");
  vSome.property(VertexProperty.Cardinality.single, "that", "thing");
  final GraphTraversalSource gts = create(eventStrategy);
  final Vertex vAny = gts.V().addV().property("any", "thing").next();
  gts.V(vAny).property(VertexProperty.Cardinality.single, "any", "thing else").next();
  tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.V().has("any", "thing else"))));
  assertEquals(1, listener1.addVertexEventRecorded());
  assertEquals(1, listener2.addVertexEventRecorded());
  assertEquals(1, listener2.vertexPropertyChangedEventRecorded());
  assertEquals(1, listener1.vertexPropertyChangedEventRecorded());
}

代码示例来源:origin: apache/tinkerpop

@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_NUMERIC_IDS)
public void shouldIterateVerticesWithNumericIdSupportUsingLongRepresentations() {
  // if the graph supports id assigned, it should allow it.  if the graph does not, it will generate one
  final Vertex v1 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, 1l) : graph.addVertex();
  final Vertex v2 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, 2l) : graph.addVertex();
  graph.addVertex();
  tryCommit(graph, graph -> {
    assertEquals(2, IteratorUtils.count(graph.vertices(Long.parseLong(v1.id().toString()), Long.parseLong(v2.id().toString()))));
  });
}

代码示例来源:origin: apache/tinkerpop

public static void assertNoEdgeGraph(final Graph g1, final boolean assertDouble, final boolean lossyForId) {
  assertEquals(2, IteratorUtils.count(g1.vertices()));
  assertEquals(1, IteratorUtils.count(g1.edges()));
  final Vertex v1 = g1.traversal().V().has("name", "marko").next();
  assertEquals(29, v1.<Integer>value("age").intValue());
  assertEquals(2, v1.keys().size());
  assertEquals(Vertex.DEFAULT_LABEL, v1.label());
  assertId(g1, lossyForId, v1, 1);
  final List<Edge> v1Edges = IteratorUtils.list(v1.edges(Direction.BOTH));
  assertEquals(1, v1Edges.size());
  v1Edges.forEach(e -> {
    if (e.inVertex().value("name").equals("vadas")) {
      assertEquals(Edge.DEFAULT_LABEL, e.label());
      if (assertDouble)
        assertWeightLoosely(0.5d, e);
      else
        assertWeightLoosely(0.5f, e);
      assertEquals(1, e.keys().size());
      assertId(g1, lossyForId, e, 7);
    } else {
      fail("Edge not expected");
    }
  });
}

代码示例来源:origin: apache/tinkerpop

@Test
@LoadGraphWith(MODERN)
public void shouldSupportMultipleScopesWithEdgeFunction() throws ExecutionException, InterruptedException {
  final ComputerResult result = graphProvider.getGraphComputer(graph).program(new MultiScopeVertexWithEdgeFunctionProgram()).submit().get();
  assertEquals(result.graph().traversal().V().has("name", "josh").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 0L);
  assertEquals(result.graph().traversal().V().has("name", "lop").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 4L);
  assertEquals(result.graph().traversal().V().has("name", "ripple").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 10L);
  assertEquals(result.graph().traversal().V().has("name", "marko").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 20L);
}

代码示例来源:origin: apache/tinkerpop

@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_NUMERIC_IDS)
public void shouldIterateEdgesWithNumericIdSupportUsingLongRepresentations() {
  // if the graph supports id assigned, it should allow it.  if the graph does not, it will generate one
  final Vertex v = graph.addVertex();
  final Edge e1 = graph.features().edge().supportsUserSuppliedIds() ? v.addEdge("self", v, T.id, 1l) : v.addEdge("self", v);
  final Edge e2 = graph.features().edge().supportsUserSuppliedIds() ? v.addEdge("self", v, T.id, 2l) : v.addEdge("self", v);
  v.addEdge("self", v);
  tryCommit(graph, graph -> {
    assertEquals(2, IteratorUtils.count(graph.edges(Long.parseLong(e1.id().toString()), Long.parseLong(e2.id().toString()))));
  });
}

代码示例来源:origin: apache/tinkerpop

@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE)
public void shouldTriggerAddEdgeByPath() {
  final StubMutationListener listener1 = new StubMutationListener();
  final StubMutationListener listener2 = new StubMutationListener();
  final EventStrategy.Builder builder = EventStrategy.build()
      .addListener(listener1)
      .addListener(listener2);
  if (graph.features().graph().supportsTransactions())
    builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph));
  final EventStrategy eventStrategy = builder.create();
  final Vertex v = graph.addVertex();
  v.addEdge("self", v);
  final GraphTraversalSource gts = create(eventStrategy);
  gts.V(v).as("a").addE("self").to("a").next();
  tryCommit(graph, g -> assertEquals(2, IteratorUtils.count(gts.E())));
  assertEquals(0, listener1.addVertexEventRecorded());
  assertEquals(0, listener2.addVertexEventRecorded());
  assertEquals(1, listener1.addEdgeEventRecorded());
  assertEquals(1, listener2.addEdgeEventRecorded());
}

代码示例来源:origin: apache/tinkerpop

public static Vertex createVertex(final Attachable<Vertex> attachableVertex, final Graph hostGraph) {
  final Vertex baseVertex = attachableVertex.get();
  final Vertex vertex = hostGraph.features().vertex().willAllowId(baseVertex.id()) ?
      hostGraph.addVertex(T.id, baseVertex.id(), T.label, baseVertex.label()) :
      hostGraph.addVertex(T.label, baseVertex.label());
  baseVertex.properties().forEachRemaining(vp -> {
    final VertexProperty vertexProperty = hostGraph.features().vertex().properties().willAllowId(vp.id()) ?
        vertex.property(hostGraph.features().vertex().getCardinality(vp.key()), vp.key(), vp.value(), T.id, vp.id()) :
        vertex.property(hostGraph.features().vertex().getCardinality(vp.key()), vp.key(), vp.value());
    vp.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value()));
  });
  return vertex;
}

代码示例来源:origin: apache/tinkerpop

@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_PERSISTENCE)
public void shouldPersistDataOnClose() throws Exception {
  final Vertex v = graph.addVertex();
  final Vertex u = graph.addVertex();
  if (graph.features().vertex().properties().supportsStringValues()) {
    v.property(VertexProperty.Cardinality.single, "name", "marko");
    u.property(VertexProperty.Cardinality.single, "name", "pavel");
  }
  final Edge e = v.addEdge(graphProvider.convertLabel("collaborator"), u);
  if (graph.features().edge().properties().supportsStringValues())
    e.property("location", "internet");
  tryCommit(graph, getAssertVertexEdgeCounts(2, 1));
  graph.close();
  final Graph reopenedGraph = graphProvider.standardTestGraph(this.getClass(), name.getMethodName(), null);
  assertVertexEdgeCounts(reopenedGraph, 2, 1);
  if (graph.features().vertex().properties().supportsStringValues()) {
    reopenedGraph.vertices().forEachRemaining(vertex -> {
      assertTrue(vertex.property("name").value().equals("marko") || vertex.property("name").value().equals("pavel"));
    });
  }
  reopenedGraph.edges().forEachRemaining(edge -> {
    assertEquals(graphProvider.convertLabel("collaborator"), edge.label());
    if (graph.features().edge().properties().supportsStringValues())
      assertEquals("internet", edge.property("location").value());
  });
  graphProvider.clear(reopenedGraph, graphProvider.standardGraphConfiguration(this.getClass(), name.getMethodName(), null));
}

代码示例来源:origin: apache/tinkerpop

public static VertexProperty createVertexProperty(final Attachable<VertexProperty> attachableVertexProperty, final Graph hostGraph) {
  final VertexProperty<Object> baseVertexProperty = attachableVertexProperty.get();
  final Iterator<Vertex> vertexIterator = hostGraph.vertices(baseVertexProperty.element().id());
  if (vertexIterator.hasNext()) {
    final VertexProperty vertexProperty = hostGraph.features().vertex().properties().willAllowId(baseVertexProperty.id()) ?
        vertexIterator.next().property(hostGraph.features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value(), T.id, baseVertexProperty.id()) :
        vertexIterator.next().property(hostGraph.features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value());
    baseVertexProperty.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value()));
    return vertexProperty;
  }
  throw new IllegalStateException("Could not find vertex to create the attachable vertex property on");
}

代码示例来源:origin: apache/tinkerpop

@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE)
public void shouldTriggerEdgePropertyChanged() {
  final StubMutationListener listener1 = new StubMutationListener();
  final StubMutationListener listener2 = new StubMutationListener();
  final EventStrategy.Builder builder = EventStrategy.build()
      .addListener(listener1)
      .addListener(listener2);
  if (graph.features().graph().supportsTransactions())
    builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph));
  final EventStrategy eventStrategy = builder.create();
  final Vertex v = graph.addVertex();
  final Edge e = v.addEdge("self", v);
  e.property("some", "thing");
  final GraphTraversalSource gts = create(eventStrategy);
  gts.E(e).property("some", "other thing").next();
  tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.E().has("some", "other thing"))));
  assertEquals(0, listener1.addVertexEventRecorded());
  assertEquals(0, listener2.addVertexEventRecorded());
  assertEquals(0, listener1.addEdgeEventRecorded());
  assertEquals(0, listener2.addEdgeEventRecorded());
  assertEquals(1, listener2.edgePropertyChangedEventRecorded());
  assertEquals(1, listener1.edgePropertyChangedEventRecorded());
}

代码示例来源:origin: thinkaurelius/titan

@Test
public void testReadWideVertexWithManyProperties() {
  int numProps = 1 << 16;
  long numV  = 1;
  mgmt.makePropertyKey("p").cardinality(Cardinality.LIST).dataType(Integer.class).make();
  mgmt.commit();
  finishSchema();
  for (int j = 0; j < numV; j++) {
    Vertex v = graph.addVertex();
    for (int i = 0; i < numProps; i++) {
      v.property("p", i);
    }
  }
  graph.tx().commit();
  assertEquals(numV, (long) graph.traversal().V().count().next());
  Map<String, Object> propertiesOnVertex = graph.traversal().V().valueMap().next();
  List<?> valuesOnP = (List)propertiesOnVertex.values().iterator().next();
  assertEquals(numProps, valuesOnP.size());
  Graph g = GraphFactory.open("target/test-classes/cassandra-read.properties");
  GraphTraversalSource t = g.traversal(GraphTraversalSource.computer(SparkGraphComputer.class));
  assertEquals(numV, (long) t.V().count().next());
  propertiesOnVertex = t.V().valueMap().next();
  valuesOnP = (List)propertiesOnVertex.values().iterator().next();
  assertEquals(numProps, valuesOnP.size());
}

代码示例来源:origin: apache/tinkerpop

@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
public void shouldTriggerAddVertexFromStart() {
  final StubMutationListener listener1 = new StubMutationListener();
  final StubMutationListener listener2 = new StubMutationListener();
  final EventStrategy.Builder builder = EventStrategy.build()
      .addListener(listener1)
      .addListener(listener2);
  if (graph.features().graph().supportsTransactions())
    builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph));
  final EventStrategy eventStrategy = builder.create();
  graph.addVertex("some", "thing");
  final GraphTraversalSource gts = create(eventStrategy);
  gts.addV().property("any", "thing").next();
  tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.V().has("any", "thing"))));
  assertEquals(1, listener1.addVertexEventRecorded());
  assertEquals(1, listener2.addVertexEventRecorded());
}

代码示例来源:origin: apache/tinkerpop

@Test
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS)
public void shouldRollbackElementAutoTransactionByDefault() {
  final Vertex v1 = graph.addVertex();
  final Edge e1 = v1.addEdge("l", v1);
  assertVertexEdgeCounts(graph, 1, 1);
  assertEquals(v1.id(), graph.vertices(v1.id()).next().id());
  assertEquals(e1.id(), graph.edges(e1.id()).next().id());
  g.tx().rollback();
  assertVertexEdgeCounts(graph, 0, 0);
}

代码示例来源:origin: apache/tinkerpop

@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializeVertexProperty() throws Exception {
  final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_0).create().createMapper();
  final VertexProperty vp = graph.vertices(convertToVertexId("marko")).next().property("name");
  final String json = mapper.writeValueAsString(vp);
  final VertexProperty detached = mapper.readValue(json, VertexProperty.class);
  assertNotNull(detached);
  assertEquals(vp.label(), detached.label());
  assertEquals(vp.id(), detached.id());
  assertEquals(vp.value(), detached.value());
}

代码示例来源:origin: apache/tinkerpop

@Test
@LoadGraphWith(MODERN)
@IgnoreEngine(TraversalEngine.Type.COMPUTER) // we can't modify the graph in computer mode
public void shouldProperlyHandleMetaProperties() throws Exception {
  graph.traversal().V().has("name", "marko").properties("name").property("alias", "okram").iterate();
  final BulkLoaderVertexProgram blvp = BulkLoaderVertexProgram.build()
      .userSuppliedIds(true)
      .writeGraph(getWriteGraphConfiguration()).create(graph);
  graphProvider.getGraphComputer(graph).workers(1).program(blvp).submit().get();
  assertGraphEquality(graph, getWriteGraph());
}

代码示例来源:origin: apache/tinkerpop

@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
public void shouldGenerateDefaultIdOnGraphAddVWithGeneratedDefaultId() throws Exception {
  final ElementIdStrategy strategy = ElementIdStrategy.build().create();
  final GraphTraversalSource sg = create(strategy);
  final Vertex v = sg.addV().property("name", "stephen").next();
  assertEquals("stephen", v.value("name"));
  final Traversal t1 = graph.traversal().V(v);
  t1.asAdmin().applyStrategies();
  logger.info(t1.toString());
  final Traversal t2 = sg.V(v);
  t2.asAdmin().applyStrategies();
  logger.info(t2.toString());
  assertNotNull(UUID.fromString(sg.V(v).id().next().toString()));
}

相关文章