Java Code Examples for org.apache.tinkerpop.gremlin.TestHelper#RANDOM

The following examples show how to use org.apache.tinkerpop.gremlin.TestHelper#RANDOM . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StarGraphTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_USER_SUPPLIED_IDS)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_USER_SUPPLIED_IDS)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_USER_SUPPLIED_IDS)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_PROPERTY)
public void shouldAttachWithCreateMethod() {
    final Random random = TestHelper.RANDOM;
    StarGraph starGraph = StarGraph.open();
    Vertex starVertex = starGraph.addVertex(T.label, "person", "name", "stephen", "name", "spmallete");
    starVertex.property("acl", true, "timestamp", random.nextLong(), "creator", "marko");
    for (int i = 0; i < 100; i++) {
        starVertex.addEdge("knows", starGraph.addVertex("person", "name", new UUID(random.nextLong(), random.nextLong()), "since", random.nextLong()));
        starGraph.addVertex(T.label, "project").addEdge("developedBy", starVertex, "public", random.nextBoolean());
    }
    final Vertex createdVertex = starGraph.getStarVertex().attach(Attachable.Method.create(graph));
    starGraph.getStarVertex().edges(Direction.BOTH).forEachRemaining(edge -> ((Attachable<Edge>) edge).attach(Attachable.Method.create(random.nextBoolean() ? graph : createdVertex)));
    TestHelper.validateEquality(starVertex, createdVertex);
}
 
Example 2
Source File: DetachedGraphTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_USER_SUPPLIED_IDS)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_USER_SUPPLIED_IDS)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_USER_SUPPLIED_IDS)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_PROPERTY)
public void testAttachableCreateMethod() {
    final Random random = TestHelper.RANDOM;
    StarGraph starGraph = StarGraph.open();
    Vertex starVertex = starGraph.addVertex(T.label, "person", "name", "stephen", "name", "spmallete");
    starVertex.property("acl", true, "timestamp", random.nextLong(), "creator", "marko");
    for (int i = 0; i < 100; i++) {
        starVertex.addEdge("knows", starGraph.addVertex("person", "name", new UUID(random.nextLong(), random.nextLong()), "since", random.nextLong()));
        starGraph.addVertex(T.label, "project").addEdge("developedBy", starVertex, "public", random.nextBoolean());
    }
    final DetachedVertex detachedVertex = DetachedFactory.detach(starGraph.getStarVertex(), true);
    final Vertex createdVertex = detachedVertex.attach(Attachable.Method.create(graph));
    TestHelper.validateVertexEquality(detachedVertex, createdVertex, false);
    TestHelper.validateVertexEquality(detachedVertex, starVertex, false);

    starGraph.getStarVertex().edges(Direction.BOTH).forEachRemaining(starEdge -> {
        final DetachedEdge detachedEdge = DetachedFactory.detach(starEdge, true);
        final Edge createdEdge = detachedEdge.attach(Attachable.Method.create(random.nextBoolean() ? graph : createdVertex));
        TestHelper.validateEdgeEquality(detachedEdge, starEdge);
        TestHelper.validateEdgeEquality(detachedEdge, createdEdge);
    });

}
 
Example 3
Source File: IoIntegrateTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@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_ADD_PROPERTY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_PROPERTY)
public void shouldHaveSizeOfStarGraphLessThanDetached() throws Exception {
    final Random random = TestHelper.RANDOM;
    final Vertex vertex = graph.addVertex("person");
    for (int i = 0; i < 100; i++) { // vertex properties and meta properties
        vertex.property(VertexProperty.Cardinality.list, UUID.randomUUID().toString(), random.nextDouble(),
                "acl", random.nextBoolean() ? "public" : "private",
                "created", random.nextLong());
    }
    for (int i = 0; i < 50000; i++) {  // edges and edge properties
        vertex.addEdge("knows", graph.addVertex("person"), "since", random.nextLong(), "acl", random.nextBoolean() ? "public" : "private");
        graph.addVertex("software").addEdge("createdBy", vertex, "date", random.nextLong());
        graph.addVertex("group").addEdge("hasMember", vertex);
    }
    ///////////////
    Pair<StarGraph, Integer> pair = serializeDeserialize(StarGraph.of(vertex));
    int starGraphSize = pair.getValue1();
    TestHelper.validateEquality(vertex, pair.getValue0().getStarVertex());
    ///
    pair = serializeDeserialize(pair.getValue0());
    assertEquals(starGraphSize, pair.getValue1().intValue());
    starGraphSize = pair.getValue1();
    TestHelper.validateEquality(vertex, pair.getValue0().getStarVertex());
    ///
    pair = serializeDeserialize(pair.getValue0());
    assertEquals(starGraphSize, pair.getValue1().intValue());
    starGraphSize = pair.getValue1();
    TestHelper.validateEquality(vertex, pair.getValue0().getStarVertex());
    ///
    // this is a rough approximation of "detached" serialization of all vertices and edges.
    // now that writeVertex in gryo writes StarGraph that approach can't be used anymore to test
    // serialization size of detached.
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final GryoWriter writer = graph.io(IoCore.gryo()).writer().create();
    writer.writeObject(outputStream, DetachedFactory.detach(vertex, true));
    vertex.edges(Direction.BOTH).forEachRemaining(e -> writer.writeObject(outputStream, DetachedFactory.detach(e, true)));
    final int detachedVertexSize = outputStream.size();
    assertTrue(starGraphSize < detachedVertexSize);

    logger.info("Size of star graph:        {}", starGraphSize);
    logger.info("Size of detached vertex:   {}", detachedVertexSize);
    logger.info("Size reduction:            {}", (float) detachedVertexSize / (float) starGraphSize);
}
 
Example 4
Source File: AbstractNeo4jGraphProvider.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
private void createIndices(final Neo4jGraph graph, final LoadGraphWith.GraphData graphData) {
    final Random random = TestHelper.RANDOM;
    final boolean pick = random.nextBoolean();
    if (graphData.equals(LoadGraphWith.GraphData.GRATEFUL)) {
        if (pick) {
            graph.tx().readWrite();
            if (random.nextBoolean())
                graph.cypher("CREATE INDEX ON :artist(name)").iterate();
            if (random.nextBoolean())
                graph.cypher("CREATE INDEX ON :song(name)").iterate();
            if (random.nextBoolean())
                graph.cypher("CREATE INDEX ON :song(songType)").iterate();
            if (random.nextBoolean())
                graph.cypher("CREATE INDEX ON :song(performances)").iterate();
            graph.tx().commit();
        } // else no indices
    } else if (graphData.equals(LoadGraphWith.GraphData.MODERN)) {
        if (pick) {
            graph.tx().readWrite();
            if (random.nextBoolean())
                graph.cypher("CREATE INDEX ON :person(name)").iterate();
            if (random.nextBoolean())
                graph.cypher("CREATE INDEX ON :person(age)").iterate();
            if (random.nextBoolean())
                graph.cypher("CREATE INDEX ON :software(name)").iterate();
            if (random.nextBoolean()) {
                graph.cypher("CREATE INDEX ON :software(lang)").iterate();
            }
            graph.tx().commit();
        } // else no indices
    } else if (graphData.equals(LoadGraphWith.GraphData.CLASSIC)) {
        if (pick) {
            graph.tx().readWrite();
            if (random.nextBoolean())
                graph.cypher("CREATE INDEX ON :vertex(name)").iterate();
            if (random.nextBoolean())
                graph.cypher("CREATE INDEX ON :vertex(age)").iterate();
            if (random.nextBoolean())
                graph.cypher("CREATE INDEX ON :vertex(lang)").iterate();
            graph.tx().commit();
        } // else no indices
    }
}