Java Code Examples for org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph#open()

The following examples show how to use org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph#open() . 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: GraphSONMessageSerializerV1d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeEdgeProperty() throws Exception {
    final Graph g = TinkerGraph.open();
    final Vertex v1 = g.addVertex();
    final Vertex v2 = g.addVertex();
    final Edge e = v1.addEdge("test", v2);
    e.property("abc", 123);

    final Iterable<Property<Object>> iterable = IteratorUtils.list(e.properties("abc"));
    final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create());

    final JsonNode json = mapper.readTree(results);

    assertNotNull(json);
    assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
    final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);

    assertNotNull(converted);
    assertEquals(1, converted.size());

    final JsonNode propertyAsJson = converted.get(0);
    assertNotNull(propertyAsJson);

    assertEquals(123, propertyAsJson.get("value").asInt());
}
 
Example 2
Source File: PolymorphicTypeResolverTest.java    From Ferma with Apache License 2.0 6 votes vote down vote up
@Test
public void testHasTypeParentFromPackageRecursively() {
    final Graph godGraph = TinkerGraph.open();
    final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_MODEL_PACKAGE);

    //add a single node to the graph, a programmer.
    framedGraph.addFramedVertex(GodExtended.class);

    //make sure the newly added node is actually a programmer
    final God god = framedGraph.traverse(input -> framedGraph.getTypeResolver().hasType(input.V(), God.class)).next(God.class);
    Assert.assertTrue(god instanceof GodExtended);

    //change the type resolution to person
    god.setTypeResolution(God.class);

    //make sure the newly added node is not actually a programmer
    final Person godAgain = framedGraph.traverse(input -> input.V()).next(Person.class);
    Assert.assertFalse(godAgain instanceof GodExtended);
}
 
Example 3
Source File: PropertyMethodHandlerTest.java    From Ferma with Apache License 2.0 6 votes vote down vote up
@Test
public void testObtainName() {
    final Graph godGraph = TinkerGraph.open();
    GodGraphLoader.load(godGraph);

    final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES);

    final List<? extends God> gods = framedGraph.traverse(
        input -> input.V().has("name", "jupiter")).toList(God.class);

    final God father = gods.iterator().next();
    Assert.assertTrue(father != null);
    final VertexFrame fatherVertex = father;
    Assert.assertEquals(fatherVertex.getProperty("name"), "jupiter");
    Assert.assertEquals("jupiter", father.obtainName());
}
 
Example 4
Source File: IncidenceMethodHandlerTest.java    From Ferma with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSonEdgesDefault() {
    final TinkerGraph godGraph = TinkerGraph.open();
    GodGraphLoader.load(godGraph);

    final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES);

    final List<? extends God> gods = framedGraph.traverse(
        input -> input.V().has("name", "jupiter")).toList(God.class);

    final God father = gods.iterator().next();
    Assert.assertTrue(father != null);
    final VertexFrame fatherVertex = father;
    Assert.assertEquals(fatherVertex.getProperty("name"), "jupiter");

    final Iterator<? extends EdgeFrame> childEdgeIterator = father.getSonEdges();
    Assert.assertTrue(childEdgeIterator.hasNext());
    final EdgeFrame childEdge = childEdgeIterator.next();
    Assert.assertEquals(childEdge.getElement().outVertex().property("name").value(), "hercules");
}
 
Example 5
Source File: PropertyMethodHandlerTest.java    From Ferma with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyName() {
    final Graph godGraph = TinkerGraph.open();
    GodGraphLoader.load(godGraph);

    final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES);

    List<? extends God> gods = framedGraph.traverse(
        input -> input.V().has("name", "jupiter")).toList(God.class);

    God father = gods.iterator().next();
    Assert.assertTrue(father != null);
    VertexFrame fatherVertex = father;
    Assert.assertEquals(fatherVertex.getProperty("name"), "jupiter");
    father.applyName("joopiter");

    gods = framedGraph.traverse(
        input -> input.V().has("name", "joopiter")).toList(God.class);

    father = gods.iterator().next();
    Assert.assertTrue(father != null);
    fatherVertex = father;
    Assert.assertEquals(fatherVertex.getProperty("name"), "joopiter");
}
 
Example 6
Source File: FramedGraphTest.java    From Ferma with Apache License 2.0 6 votes vote down vote up
@Test
public void testJavaTypingNextExplicit() {
    final Graph g = TinkerGraph.open();
    final FramedGraph fg = new DelegatingFramedGraph(g, true, false);

    final Person p1 = fg.addFramedVertex(Programmer.DEFAULT_INITIALIZER);
    p1.setName("Bryn");

    final Person p2 = fg.addFramedVertex(Person.DEFAULT_INITIALIZER);
    p2.setName("Julia");

    final Person bryn = fg.traverse(
        input -> input.V().has("name", "Bryn")).nextExplicit(Person.class);
    final Person julia = fg.traverse(
        input -> input.V().has("name", "Julia")).nextExplicit(Person.class);

    Assert.assertEquals(Person.class, bryn.getClass());
    Assert.assertEquals(Person.class, julia.getClass());

    Assert.assertNotNull(bryn.getElement().property(PolymorphicTypeResolver.TYPE_RESOLUTION_KEY).value());
    Assert.assertNotNull(julia.getElement().property(PolymorphicTypeResolver.TYPE_RESOLUTION_KEY).value());
}
 
Example 7
Source File: PolymorphicTypeResolverTest.java    From Ferma with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomTypeKeyExplicitByClass() {
    final Graph g = TinkerGraph.open();
    final ReflectionCache cache = new ReflectionCache(TEST_TYPES);
    final FramedGraph fg = new DelegatingFramedGraph(g, new AnnotationFrameFactory(cache), new PolymorphicTypeResolver(cache, CUSTOM_TYPE_KEY));

    final Person p1 = fg.addFramedVertexExplicit(Programmer.class);
    p1.setName("Bryn");

    final Person p2 = fg.addFramedVertexExplicit(Person.class);
    p2.setName("Julia");

    final Person bryn = fg.traverse(input -> input.V().has("name", "Bryn")).next(Person.class);
    final Person julia = fg.traverse(input -> input.V().has("name", "Julia")).next(Person.class);

    Assert.assertEquals(Person.class, bryn.getClass());
    Assert.assertEquals(Person.class, julia.getClass());

    Assert.assertFalse(bryn.getElement().property(CUSTOM_TYPE_KEY).isPresent());
    Assert.assertFalse(bryn.getElement().property(PolymorphicTypeResolver.TYPE_RESOLUTION_KEY).isPresent());
    Assert.assertFalse(julia.getElement().property(CUSTOM_TYPE_KEY).isPresent());
    Assert.assertFalse(julia.getElement().property(PolymorphicTypeResolver.TYPE_RESOLUTION_KEY).isPresent());
}
 
Example 8
Source File: GraphSONUtilityTest.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void typeNodeReadAndVertexNotAddedToGraph() {
    JsonNode entityNode = getDbType();
    TinkerGraph tg = TinkerGraph.open();
    GraphSONUtility gu = new GraphSONUtility(emptyRelationshipCache);
    gu.vertexFromJson(tg, entityNode);

    Assert.assertEquals((long) tg.traversal().V().count().next(), 0L);
}
 
Example 9
Source File: FramedGraphTest.java    From Ferma with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomFrameBuilderByClass() {
    final Person o = new Person();
    final Graph g = TinkerGraph.open();
    final FramedGraph fg = new DelegatingFramedGraph(g, new FrameFactory() {

        @SuppressWarnings("unchecked")
        @Override
        public <T> T create(final Element e, final Class<T> kind) {
            return (T) o;
        }
    }, new PolymorphicTypeResolver());
    final Person person = fg.addFramedVertex(Person.class);
    Assert.assertEquals(o, person);
}
 
Example 10
Source File: ParameterizedGroovyTranslatorTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldIncludeCustomTypeTranslationForSomethingSilly() throws Exception {
    final TinkerGraph graph = TinkerGraph.open();
    final ParameterizedSillyClass notSillyEnough = ParameterizedSillyClass.from("not silly enough", 100);
    final GraphTraversalSource g = graph.traversal();

    // without type translation we get uglinesss
    final Script parameterizedScriptBad = GroovyTranslator.of("g", true).
            translate(g.inject(notSillyEnough).asAdmin().getBytecode());
    Bindings bindings = new SimpleBindings();
    parameterizedScriptBad.getParameters().ifPresent(bindings::putAll);
    assertEquals(String.format("g.inject(%s)", "_args_0"), parameterizedScriptBad.getScript());
    assertEquals(1, bindings.size());
    assertEquals(notSillyEnough, bindings.get("_args_0"));
    bindings.clear();

    // with type translation we get valid gremlin
    final Script parameterizedScriptGood = GroovyTranslator.of("g", new ParameterizedSillyClassTranslatorCustomizer().createTypeTranslator()).
            translate(g.inject(notSillyEnough).asAdmin().getBytecode());
    parameterizedScriptGood.getParameters().ifPresent(bindings::putAll);
    assertEquals(2, bindings.size());
    assertEquals(notSillyEnough.getX(), bindings.get("_args_0"));
    assertEquals(notSillyEnough.getY(), bindings.get("_args_1"));
    assertEquals("g.inject(org.apache.tinkerpop.gremlin.groovy.jsr223.ParameterizedGroovyTranslatorTest.ParameterizedSillyClass.from(_args_0,_args_1))", parameterizedScriptGood.getScript());
    bindings.put("g", g);
    assertThatScriptOk(parameterizedScriptGood.getScript(), bindings);

    final GremlinGroovyScriptEngine customEngine = new GremlinGroovyScriptEngine(new ParameterizedSillyClassTranslatorCustomizer());
    final Traversal t = customEngine.eval(g.inject(notSillyEnough).asAdmin().getBytecode(), bindings, "g");
    final ParameterizedSillyClass sc = (ParameterizedSillyClass) t.next();
    assertEquals(notSillyEnough.getX(), sc.getX());
    assertEquals(notSillyEnough.getY(), sc.getY());
    assertThat(t.hasNext(), is(false));
}
 
Example 11
Source File: VertexPropertyTest.java    From peapod with Apache License 2.0 5 votes vote down vote up
@Test
public void testVertexProperty() {
    FramedGraph g = new FramedGraph(TinkerGraph.open(), Person.class.getPackage());

    Person marko = g.addVertex(Person.class);
    marko.addName("marko");
    marko.addName("marko a. rodriguez");

    assertEquals(2, marko.getNames().size());
    assertThat(marko.getNames().stream().map(Name::getValue).toArray(), arrayContainingInAnyOrder("marko", "marko a. rodriguez"));

    Name name = marko.getName("marko");
    assertNotNull(name);
    name.setAcl("private");

    name = marko.getName("marko a. rodriguez");
    name.setAcl("public");

    name = marko.getNameWithAcl("public");
    assertEquals("marko a. rodriguez", name.getValue());
    assertEquals("public", name.getAcl());
    marko.removeName("marko a. rodriguez");
    assertNull(marko.getNameWithAcl("public"));

    name = marko.getNameWithAcl("private");
    assertEquals("private", name.getAcl());
    assertEquals("marko", name.getValue());

    name.setDate(2014);
    name.setCreator("stephen");

    name = marko.getNameWithAcl("private");
    assertEquals("stephen", name.getCreator());
    assertEquals(new Integer(2014), name.getDate());
}
 
Example 12
Source File: GraphSONMessageSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeEdge() throws Exception {
    final Graph g = TinkerGraph.open();
    final Vertex v1 = g.addVertex();
    final Vertex v2 = g.addVertex();
    final Edge e = v1.addEdge("test", v2);
    e.property("abc", 123);

    final Iterable<Edge> iterable = IteratorUtils.list(g.edges());
    final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create());

    final JsonNode json = mapper.readTree(results);

    assertNotNull(json);
    assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
    final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);

    assertNotNull(converted);
    assertEquals(1, converted.size());

    final JsonNode edgeAsJson = converted.get(0).get(GraphSONTokens.VALUEPROP);
    assertNotNull(edgeAsJson);

    assertEquals(((Long) e.id()).longValue(), edgeAsJson.get(GraphSONTokens.ID).get(GraphSONTokens.VALUEPROP).asLong());
    assertEquals(((Long) v1.id()).longValue(), edgeAsJson.get(GraphSONTokens.OUT).get(GraphSONTokens.VALUEPROP).asLong());
    assertEquals(((Long) v2.id()).longValue(), edgeAsJson.get(GraphSONTokens.IN).get(GraphSONTokens.VALUEPROP).asLong());
    assertEquals(e.label(), edgeAsJson.get(GraphSONTokens.LABEL).asText());

    final JsonNode properties = edgeAsJson.get(GraphSONTokens.PROPERTIES);
    assertNotNull(properties);
    assertEquals(123, properties.get("abc").get(GraphSONTokens.VALUEPROP).get("value").get(GraphSONTokens.VALUEPROP).asInt());
}
 
Example 13
Source File: GraphSONUtilityTest.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void dataNodeReadAndVertexAddedToGraph() {
    JsonNode entityNode = getCol1();
    TinkerGraph tg = TinkerGraph.open();
    GraphSONUtility gu = new GraphSONUtility(emptyRelationshipCache);
    Map<String, Object> map = gu.vertexFromJson(tg, entityNode);

    assertNull(map);
    assertEquals((long) tg.traversal().V().count().next(), 1L);

    Vertex v = tg.vertices().next();
    assertTrue(v.property(VERTEX_ID_IN_IMPORT_KEY).isPresent());
}
 
Example 14
Source File: GremlinResultSetIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleVertexResultFromTraversalBulked() throws Exception {
    final Graph graph = TinkerGraph.open();
    final GraphTraversalSource g = graph.traversal();
    final Client aliased = client.alias("gmodern");
    final ResultSet resultSetUnrolled = aliased.submit(g.V().both().barrier().both().barrier());
    final List<Result> results = resultSetUnrolled.all().get();

    assertThat(results.get(0).getObject(), CoreMatchers.instanceOf(Traverser.class));
    assertEquals(6, results.size());
}
 
Example 15
Source File: MyBenchmark.java    From peapod with Apache License 2.0 5 votes vote down vote up
@Setup
public void init() {
    godGraph = TinkerGraph.open();
    GodGraphLoader.load(godGraph);

    peapodGraph = new peapod.FramedGraph(godGraph, PeapodGod.class.getPackage());
    fermaGraph = new DelegatingFramedGraph<Graph>(godGraph, true, false);
}
 
Example 16
Source File: JsonNodeParsersTest.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void parseVertex() {
    JsonNode nd = getCol1();
    final int COL1_ORIGINAL_ID = 98336;

    Object nodeId = getId(nd);
    TinkerGraph tg = TinkerGraph.open();

    JsonNodeParsers.ParseElement pe = new JsonNodeParsers.ParseVertex();
    pe.setContext(graphSONUtility);
    pe.parse(tg, new MappedElementCache(),  nd);

    Vertex v = tg.vertices().next();
    Vertex vUsingPe = (Vertex) pe.get(tg, nodeId);
    Vertex vUsingOriginalId = (Vertex) pe.getByOriginalId(tg, COL1_ORIGINAL_ID);
    Vertex vUsingOriginalId2 = (Vertex) pe.getByOriginalId(tg, nd);

    updateParseElement(tg, pe, vUsingPe);

    assertNotNull(v);
    assertNotNull(vUsingPe);
    assertNotNull(vUsingOriginalId);

    assertEquals(v.id(), vUsingPe.id());
    assertEquals(nodeId, pe.getId(nd));
    assertFalse(pe.isTypeNode(nd));
    assertEquals(pe.getType(nd), "\"hive_column\"");
    assertEquals(vUsingOriginalId.id(), v.id());
    assertEquals(vUsingOriginalId2.id(), v.id());

    assertProperties(vUsingPe);
}
 
Example 17
Source File: MappedElementCacheTest.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void edgeFetch() {
    MappedElementCache cache = new MappedElementCache();
    TinkerGraph tg = TinkerGraph.open();

    addEdge(tg, cache);

    assertEquals(cache.lruVertexCache.size(), 4);
}
 
Example 18
Source File: MappedElementCacheTest.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void vertexFetch()  {
    JsonNode node = getCol1();
    MappedElementCache cache = new MappedElementCache();
    TinkerGraph tg = TinkerGraph.open();

    addVertex(tg, node);

    Vertex vx = cache.getMappedVertex(tg, 98336);
    assertNotNull(vx);
    assertEquals(cache.lruVertexCache.size(), 1);
}
 
Example 19
Source File: MigrationProgressServiceTest.java    From atlas with Apache License 2.0 5 votes vote down vote up
private TinkerGraph createUpdateStatusNode(TinkerGraph tg, long currentIndex, long totalIndex, String status) {
    if(tg == null) {
        tg = TinkerGraph.open();
    }

    ReaderStatusManager rsm = new ReaderStatusManager(tg, tg);
    rsm.update(tg, currentIndex, false);
    rsm.end(tg, totalIndex, status);
    return tg;
}
 
Example 20
Source File: GraphSONMessageSerializerGremlinV2d0Test.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldSerializeVertexWithEmbeddedMap() throws Exception {
    final Graph graph = TinkerGraph.open();
    final Vertex v = graph.addVertex();
    final Map<String, Object> map = new HashMap<>();
    map.put("x", 500);
    map.put("y", "some");

    final ArrayList<Object> friends = new ArrayList<>();
    friends.add("x");
    friends.add(5);
    friends.add(map);

    v.property(VertexProperty.Cardinality.single, "friends", friends);

    final List list = IteratorUtils.list(graph.vertices());

    final ResponseMessage response = convert(list);
    assertCommon(response);

    final List<Vertex> vertexList = (List<Vertex>) response.getResult().getData();
    assertEquals(1, vertexList.size());

    final Vertex deserializedVertex = vertexList.get(0);
    assertEquals(v.id(), deserializedVertex.id());
    assertEquals(Vertex.DEFAULT_LABEL, deserializedVertex.label());

    final List<VertexProperty> properties = new ArrayList<>();
    deserializedVertex.properties().forEachRemaining(properties::add);
    assertEquals(1, properties.size());

    final VertexProperty friendsProperty = properties.get(0);
    final List<Object> deserializedInnerList = (List<Object>) friendsProperty.value();

    assertEquals(3, deserializedInnerList.size());
    assertEquals("x", deserializedInnerList.get(0));
    assertEquals(5, deserializedInnerList.get(1));

    final Map<String, Object> deserializedInnerInnerMap = (Map<String, Object>) deserializedInnerList.get(2);
    assertEquals(2, deserializedInnerInnerMap.size());
    assertEquals(500, deserializedInnerInnerMap.get("x"));
    assertEquals("some", deserializedInnerInnerMap.get("y"));
}