Search in sources :

Example 76 with TinkerGraph

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

the class GraphMLWriterTest method testEncoding.

// Note: this is only a very lightweight test of writer/reader encoding.
// It is known that there are characters which, when written by GraphMLWriter,
// cause parse errors for GraphMLReader.
// However, this happens uncommonly enough that is not yet known which characters those are.
public void testEncoding() throws Exception {
    Graph g = new TinkerGraph();
    Vertex v = g.addVertex(1);
    v.setProperty("text", "\u00E9");
    GraphMLWriter w = new GraphMLWriter(g);
    File f = File.createTempFile("test", "txt");
    OutputStream out = new FileOutputStream(f);
    w.outputGraph(out);
    out.close();
    Graph g2 = new TinkerGraph();
    GraphMLReader r = new GraphMLReader(g2);
    InputStream in = new FileInputStream(f);
    r.inputGraph(in);
    in.close();
    Vertex v2 = g2.getVertex(1);
    assertEquals("\u00E9", v2.getProperty("text"));
}
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) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 77 with TinkerGraph

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

the class GraphMLWriterTest method testWithEdgeLabel.

public void testWithEdgeLabel() throws Exception {
    TinkerGraph g = new TinkerGraph();
    GraphMLReader.inputGraph(g, GraphMLReader.class.getResourceAsStream("graph-example-1.xml"));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GraphMLWriter w = new GraphMLWriter(g);
    w.setEdgeLabelKey("label");
    w.setNormalize(true);
    w.outputGraph(bos);
    String expected = streamToString(GraphMLWriterTest.class.getResourceAsStream("graph-example-1-schema-valid.xml"));
    // System.out.println(expected);
    assertEquals(expected.replace("\n", "").replace("\r", ""), bos.toString().replace("\n", "").replace("\r", ""));
}
Also used : TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 78 with TinkerGraph

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

the class GraphSONReaderTest method inputGraphCompactFullCycle.

@Test
public void inputGraphCompactFullCycle() throws IOException {
    TinkerGraph graph = TinkerGraphFactory.createTinkerGraph();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Set<String> edgeKeys = new HashSet<String>();
    edgeKeys.add(GraphSONTokens._ID);
    edgeKeys.add(GraphSONTokens._IN_V);
    edgeKeys.add(GraphSONTokens._OUT_V);
    edgeKeys.add(GraphSONTokens._LABEL);
    Set<String> vertexKeys = new HashSet<String>();
    vertexKeys.add(GraphSONTokens._ID);
    GraphSONWriter writer = new GraphSONWriter(graph);
    writer.outputGraph(stream, vertexKeys, edgeKeys, GraphSONMode.COMPACT);
    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));
        }
        // no properties should be here
        Assert.assertEquals(null, found.getProperty("name"));
    }
    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));
        }
        // no properties should be here
        Assert.assertEquals(null, found.getProperty("weight"));
    }
}
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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 79 with TinkerGraph

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

the class GraphSONReaderTest method inputGraphModeCompact.

@Test
public void inputGraphModeCompact() throws IOException {
    TinkerGraph graph = new TinkerGraph();
    String json = "{ \"mode\":\"COMPACT\",\"vertices\": [ {\"_id\":1, \"test\": \"please work\", \"testlist\":[1, 2, 3, null], \"testmap\":{\"big\":10000000000, \"small\":0.4954959595959}}, {\"_id\":2, \"testagain\":\"please work again\"}], \"edges\":[{\"_id\":100, \"_outV\":1, \"_inV\":2, \"_label\":\"works\", \"teste\": \"please worke\"}]}";
    byte[] bytes = json.getBytes();
    InputStream inputStream = new ByteArrayInputStream(bytes);
    GraphSONReader.inputGraph(graph, inputStream);
    Assert.assertEquals(2, getIterableCount(graph.getVertices()));
    Assert.assertEquals(1, getIterableCount(graph.getEdges()));
    Vertex v1 = graph.getVertex(1);
    Assert.assertNotNull(v1);
    Assert.assertEquals("please work", v1.getProperty("test"));
    Map map = (Map) v1.getProperty("testmap");
    Assert.assertNotNull(map);
    Assert.assertEquals(10000000000l, Long.parseLong(map.get("big").toString()));
    Assert.assertEquals(0.4954959595959, Double.parseDouble(map.get("small").toString()), 0);
    // Assert.assertNull(map.get("nullKey"));
    List list = (List) v1.getProperty("testlist");
    Assert.assertEquals(4, list.size());
    boolean foundNull = false;
    for (int ix = 0; ix < list.size(); ix++) {
        if (list.get(ix) == null) {
            foundNull = true;
            break;
        }
    }
    Assert.assertTrue(foundNull);
    Vertex v2 = graph.getVertex(2);
    Assert.assertNotNull(v2);
    Assert.assertEquals("please work again", v2.getProperty("testagain"));
    Edge e = graph.getEdge(100);
    Assert.assertNotNull(e);
    Assert.assertEquals("works", e.getLabel());
    Assert.assertEquals(v1, e.getVertex(Direction.OUT));
    Assert.assertEquals(v2, e.getVertex(Direction.IN));
    Assert.assertEquals("please worke", e.getProperty("teste"));
    Assert.assertNull(e.getProperty("keyNull"));
}
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) List(java.util.List) Map(java.util.Map) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 80 with TinkerGraph

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

the class GraphSONReaderTest method inputGraphModeNormal.

@Test
public void inputGraphModeNormal() throws IOException {
    TinkerGraph graph = new TinkerGraph();
    String json = "{ \"mode\":\"NORMAL\",\"vertices\": [ {\"_id\":1, \"_type\":\"vertex\", \"test\": \"please work\", \"testlist\":[1, 2, 3, null], \"testmap\":{\"big\":10000000000, \"small\":0.4954959595959}}, {\"_id\":2, \"_type\":\"vertex\", \"testagain\":\"please work again\"}], \"edges\":[{\"_id\":100, \"_type\":\"edge\", \"_outV\":1, \"_inV\":2, \"_label\":\"works\", \"teste\": \"please worke\"}]}";
    byte[] bytes = json.getBytes();
    InputStream inputStream = new ByteArrayInputStream(bytes);
    GraphSONReader.inputGraph(graph, inputStream);
    Assert.assertEquals(2, getIterableCount(graph.getVertices()));
    Assert.assertEquals(1, getIterableCount(graph.getEdges()));
    Vertex v1 = graph.getVertex(1);
    Assert.assertNotNull(v1);
    Assert.assertEquals("please work", v1.getProperty("test"));
    Map map = (Map) v1.getProperty("testmap");
    Assert.assertNotNull(map);
    Assert.assertEquals(10000000000l, Long.parseLong(map.get("big").toString()));
    Assert.assertEquals(0.4954959595959, Double.parseDouble(map.get("small").toString()), 0);
    Assert.assertNull(map.get("nullKey"));
    List list = (List) v1.getProperty("testlist");
    Assert.assertEquals(4, list.size());
    boolean foundNull = false;
    for (int ix = 0; ix < list.size(); ix++) {
        if (list.get(ix) == null) {
            foundNull = true;
            break;
        }
    }
    Assert.assertTrue(foundNull);
    Vertex v2 = graph.getVertex(2);
    Assert.assertNotNull(v2);
    Assert.assertEquals("please work again", v2.getProperty("testagain"));
    Edge e = graph.getEdge(100);
    Assert.assertNotNull(e);
    Assert.assertEquals("works", e.getLabel());
    Assert.assertEquals(v1, e.getVertex(Direction.OUT));
    Assert.assertEquals(v2, e.getVertex(Direction.IN));
    Assert.assertEquals("please worke", e.getProperty("teste"));
    Assert.assertNull(e.getProperty("keyNull"));
}
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) List(java.util.List) Map(java.util.Map) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

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