Search in sources :

Example 81 with TinkerGraph

use of com.tinkerpop.blueprints.impls.tg.TinkerGraph in project blueprints by tinkerpop.

the class GraphSONReaderTest method inputGraphExtendedFullCycle.

@Test
public void inputGraphExtendedFullCycle() throws IOException {
    TinkerGraph graph = TinkerGraphFactory.createTinkerGraph();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    GraphSONWriter writer = new GraphSONWriter(graph);
    writer.outputGraph(stream, null, null, GraphSONMode.EXTENDED);
    stream.flush();
    stream.close();
    String jsonString = new String(stream.toByteArray());
    byte[] bytes = jsonString.getBytes();
    InputStream inputStream = new ByteArrayInputStream(bytes);
    TinkerGraph emptyGraph = new TinkerGraph();
    GraphSONReader.inputGraph(emptyGraph, inputStream);
    Assert.assertEquals(6, getIterableCount(emptyGraph.getVertices()));
    Assert.assertEquals(6, getIterableCount(emptyGraph.getEdges()));
    for (Vertex v : graph.getVertices()) {
        Vertex found = emptyGraph.getVertex(v.getId());
        Assert.assertNotNull(v);
        for (String key : found.getPropertyKeys()) {
            Assert.assertEquals(v.getProperty(key), found.getProperty(key));
        }
    }
    for (Edge e : graph.getEdges()) {
        Edge found = emptyGraph.getEdge(e.getId());
        Assert.assertNotNull(e);
        for (String key : found.getPropertyKeys()) {
            Assert.assertEquals(e.getProperty(key), found.getProperty(key));
        }
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 82 with TinkerGraph

use of com.tinkerpop.blueprints.impls.tg.TinkerGraph in project blueprints by tinkerpop.

the class GraphSONUtilityTest method edgeFromJsonStringValid.

@Test
public void edgeFromJsonStringValid() throws IOException, JSONException {
    Graph g = new TinkerGraph();
    ElementFactory factory = new GraphElementFactory(g);
    Vertex v1 = GraphSONUtility.vertexFromJson(vertexJson1, factory, GraphSONMode.NORMAL, null);
    Vertex v2 = GraphSONUtility.vertexFromJson(vertexJson2, factory, GraphSONMode.NORMAL, null);
    Edge e = GraphSONUtility.edgeFromJson(edgeJson, v1, v2, factory, GraphSONMode.NORMAL, null);
    Assert.assertSame(v1, g.getVertex(1));
    Assert.assertSame(v2, g.getVertex(2));
    Assert.assertSame(e, g.getEdge(7));
    // tinkergraph converts id to string
    Assert.assertEquals("7", e.getId());
    Assert.assertEquals(0.5d, e.getProperty("weight"));
    Assert.assertEquals("knows", e.getLabel());
    Assert.assertEquals(v1, e.getVertex(Direction.OUT));
    Assert.assertEquals(v2, e.getVertex(Direction.IN));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 83 with TinkerGraph

use of com.tinkerpop.blueprints.impls.tg.TinkerGraph in project blueprints by tinkerpop.

the class GraphSONUtilityTest method edgeFromJsonIgnoreWeightValid.

@Test
public void edgeFromJsonIgnoreWeightValid() throws IOException, JSONException {
    Graph g = new TinkerGraph();
    ElementFactory factory = new GraphElementFactory(g);
    Vertex v1 = GraphSONUtility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson1)), factory, GraphSONMode.NORMAL, null);
    Vertex v2 = GraphSONUtility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson2)), factory, GraphSONMode.NORMAL, null);
    Set<String> ignoreWeight = new HashSet<String>();
    ignoreWeight.add("weight");
    ElementPropertyConfig config = ElementPropertyConfig.excludeProperties(null, ignoreWeight);
    GraphSONUtility utility = new GraphSONUtility(GraphSONMode.NORMAL, factory, config);
    Edge e = utility.edgeFromJson(new JSONObject(new JSONTokener(edgeJson)), v1, v2);
    Assert.assertSame(v1, g.getVertex(1));
    Assert.assertSame(v2, g.getVertex(2));
    Assert.assertSame(e, g.getEdge(7));
    // tinkergraph converts id to string
    Assert.assertEquals("7", e.getId());
    Assert.assertNull(e.getProperty("weight"));
    Assert.assertEquals("knows", e.getLabel());
    Assert.assertEquals(v1, e.getVertex(Direction.OUT));
    Assert.assertEquals(v2, e.getVertex(Direction.IN));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) JSONTokener(org.codehaus.jettison.json.JSONTokener) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) JSONObject(org.codehaus.jettison.json.JSONObject) Edge(com.tinkerpop.blueprints.Edge) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 84 with TinkerGraph

use of com.tinkerpop.blueprints.impls.tg.TinkerGraph in project blueprints by tinkerpop.

the class GraphSONUtilityTest method vertexFromJsonInputStreamValid.

@Test
public void vertexFromJsonInputStreamValid() throws IOException, JSONException {
    Graph g = new TinkerGraph();
    ElementFactory factory = new GraphElementFactory(g);
    Vertex v = GraphSONUtility.vertexFromJson(inputStreamVertexJson1, factory, GraphSONMode.NORMAL, null);
    Assert.assertSame(v, g.getVertex(1));
    // tinkergraph converts id to string
    Assert.assertEquals("1", v.getId());
    Assert.assertEquals("marko", v.getProperty("name"));
    Assert.assertEquals(29, v.getProperty("age"));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Test(org.junit.Test)

Example 85 with TinkerGraph

use of com.tinkerpop.blueprints.impls.tg.TinkerGraph in project blueprints by tinkerpop.

the class IdGraphTest method testUnsupportCustomVertexOrEdgeIds.

public void testUnsupportCustomVertexOrEdgeIds() throws Exception {
    TinkerGraph baseGraph = new TinkerGraph();
    IdGraph<TinkerGraph> graph = new IdGraph<TinkerGraph>(baseGraph, true, false);
    IdGraph.IdFactory vFactory = new IdGraph.IdFactory() {

        private int count = 0;

        public Object createId() {
            return "vertex" + ++count;
        }
    };
    IdGraph.IdFactory eFactory = new IdGraph.IdFactory() {

        private int count = 0;

        public Object createId() {
            return "vertex" + ++count;
        }
    };
    graph.setVertexIdFactory(vFactory);
    graph.setEdgeIdFactory(eFactory);
    Vertex v1 = graph.addVertex(null);
    assertEquals("vertex1", v1.getId());
    Vertex v2 = graph.addVertex(null);
    assertEquals("vertex2", v2.getId());
    Edge e1 = graph.addEdge(null, v1, v2, "connected-to");
    // the custom id factory for edges is not used, because the base graph's ids are used instead
    assertFalse(e1.getId().equals("edge1"));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Edge(com.tinkerpop.blueprints.Edge)

Aggregations

TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)105 Vertex (com.tinkerpop.blueprints.Vertex)66 Graph (com.tinkerpop.blueprints.Graph)58 Test (org.junit.Test)42 Edge (com.tinkerpop.blueprints.Edge)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 JSONObject (org.codehaus.jettison.json.JSONObject)10 IMetaverseBuilder (org.pentaho.metaverse.api.IMetaverseBuilder)10 InputStream (java.io.InputStream)9 HashSet (java.util.HashSet)9 JSONTokener (org.codehaus.jettison.json.JSONTokener)8 KeyIndexableGraph (com.tinkerpop.blueprints.KeyIndexableGraph)7 Map (java.util.Map)6 MetaverseBuilder (org.pentaho.metaverse.impl.MetaverseBuilder)6 TypedGraphModuleBuilder (com.tinkerpop.frames.modules.typedgraph.TypedGraphModuleBuilder)5 HashMap (java.util.HashMap)5 Before (org.junit.Before)5 IndexableGraph (com.tinkerpop.blueprints.IndexableGraph)4 IgnoreIdTinkerGraph (com.tinkerpop.blueprints.impls.tg.IgnoreIdTinkerGraph)4