Search in sources :

Example 41 with TinkerGraph

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

the class GMLWriterTest 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 GMLWriter,
// 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");
    GMLWriter w = new GMLWriter(g);
    File f = File.createTempFile("test", "txt");
    f.deleteOnExit();
    OutputStream out = new FileOutputStream(f);
    w.outputGraph(out);
    out.close();
    Graph g2 = new TinkerGraph();
    GMLReader r = new GMLReader(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) ByteArrayInputStream(java.io.ByteArrayInputStream) 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 42 with TinkerGraph

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

the class GraphSONUtilityTest method edgeFromJsonNormalLabelOrIdOnEdge.

@Test
public void edgeFromJsonNormalLabelOrIdOnEdge() 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);
    Edge e = GraphSONUtility.edgeFromJson(new JSONObject(new JSONTokener(edgeJsonLight)), v1, v2, factory, GraphSONMode.NORMAL, null);
    Assert.assertSame(v1, g.getVertex(1));
    Assert.assertSame(v2, g.getVertex(2));
    Assert.assertSame(e, g.getEdge(0));
}
Also used : JSONTokener(org.codehaus.jettison.json.JSONTokener) Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) JSONObject(org.codehaus.jettison.json.JSONObject) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 43 with TinkerGraph

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

the class GraphSONUtilityTest method vertexFromJsonIgnoreKeyValid.

@Test
public void vertexFromJsonIgnoreKeyValid() throws IOException, JSONException {
    Graph g = new TinkerGraph();
    ElementFactory factory = new GraphElementFactory(g);
    Set<String> ignoreAge = new HashSet<String>();
    ignoreAge.add("age");
    ElementPropertyConfig config = ElementPropertyConfig.excludeProperties(ignoreAge, null);
    GraphSONUtility utility = new GraphSONUtility(GraphSONMode.NORMAL, factory, config);
    Vertex v = utility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson1)));
    Assert.assertSame(v, g.getVertex(1));
    // tinkergraph converts id to string
    Assert.assertEquals("1", v.getId());
    Assert.assertEquals("marko", v.getProperty("name"));
    Assert.assertNull(v.getProperty("age"));
}
Also used : JSONTokener(org.codehaus.jettison.json.JSONTokener) Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) JSONObject(org.codehaus.jettison.json.JSONObject) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 44 with TinkerGraph

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

the class PartitionGraphTest method testSpecificBehavior.

public void testSpecificBehavior() {
    TinkerGraph rawGraph = new TinkerGraph();
    PartitionIndexableGraph graph = new PartitionIndexableGraph(rawGraph, "_writeGraph", "a");
    assertEquals(graph.getReadPartitions().size(), 1);
    assertTrue(graph.getReadPartitions().contains("a"));
    assertEquals(graph.getWritePartition(), "a");
    Vertex marko = graph.addVertex(null);
    Vertex rawMarko = ((PartitionVertex) marko).getBaseVertex();
    assertEquals(marko.getPropertyKeys().size(), 0);
    assertEquals(rawMarko.getPropertyKeys().size(), 1);
    assertNull(marko.getProperty("_writeGraph"));
    assertEquals(rawMarko.getProperty("_writeGraph"), "a");
    assertEquals(((PartitionVertex) marko).getPartition(), "a");
    assertEquals(count(graph.getVertices()), 1);
    assertEquals(graph.getVertices().iterator().next(), marko);
    assertEquals(count(graph.getEdges()), 0);
    graph.setWritePartition("b");
    assertEquals(graph.getReadPartitions().size(), 1);
    assertTrue(graph.getReadPartitions().contains("a"));
    assertEquals(graph.getWritePartition(), "b");
    Vertex peter = graph.addVertex(null);
    Vertex rawPeter = ((PartitionVertex) peter).getBaseVertex();
    assertEquals(peter.getPropertyKeys().size(), 0);
    assertEquals(rawPeter.getPropertyKeys().size(), 1);
    assertNull(peter.getProperty("_writeGraph"));
    assertEquals(rawPeter.getProperty("_writeGraph"), "b");
    assertEquals(((PartitionVertex) peter).getPartition(), "b");
    assertEquals(count(graph.getVertices()), 1);
    assertEquals(graph.getVertices().iterator().next(), marko);
    assertEquals(count(graph.getEdges()), 0);
    graph.removeReadPartition("a");
    assertEquals(graph.getReadPartitions().size(), 0);
    assertEquals(graph.getWritePartition(), "b");
    assertEquals(count(graph.getVertices()), 0);
    assertEquals(count(graph.getEdges()), 0);
    graph.addReadPartition("b");
    assertEquals(graph.getReadPartitions().size(), 1);
    assertTrue(graph.getReadPartitions().contains("b"));
    assertEquals(graph.getWritePartition(), "b");
    assertEquals(count(graph.getVertices()), 1);
    assertEquals(graph.getVertices().iterator().next(), peter);
    assertEquals(count(graph.getEdges()), 0);
    graph.addReadPartition("a");
    assertEquals(graph.getReadPartitions().size(), 2);
    assertTrue(graph.getReadPartitions().contains("a"));
    assertTrue(graph.getReadPartitions().contains("b"));
    assertEquals(graph.getWritePartition(), "b");
    assertEquals(count(graph.getVertices()), 2);
    assertEquals(count(graph.getEdges()), 0);
    graph.setWritePartition("c");
    assertEquals(graph.getReadPartitions().size(), 2);
    assertTrue(graph.getReadPartitions().contains("a"));
    assertTrue(graph.getReadPartitions().contains("b"));
    assertEquals(graph.getWritePartition(), "c");
    Edge knows = graph.addEdge(null, marko, peter, "knows");
    Edge rawKnows = ((PartitionEdge) knows).getBaseEdge();
    assertEquals(count(graph.getVertices()), 2);
    assertEquals(count(graph.getEdges()), 0);
    graph.addReadPartition("c");
    assertEquals(count(graph.getVertices()), 2);
    assertEquals(count(graph.getEdges()), 1);
    assertEquals(knows.getPropertyKeys().size(), 0);
    assertEquals(rawKnows.getPropertyKeys().size(), 1);
    assertNull(knows.getProperty("_writeGraph"));
    assertEquals(rawKnows.getProperty("_writeGraph"), "c");
    assertEquals(((PartitionEdge) knows).getPartition(), "c");
    assertEquals(graph.getEdges().iterator().next(), knows);
    graph.removeReadPartition("a");
    graph.removeReadPartition("b");
    assertEquals(graph.getReadPartitions().size(), 1);
    assertTrue(graph.getReadPartitions().contains("c"));
    assertEquals(graph.getWritePartition(), "c");
    assertEquals(count(graph.getVertices()), 0);
    assertEquals(count(graph.getEdges()), 1);
    assertNull(knows.getVertex(Direction.IN));
    assertNull(knows.getVertex(Direction.OUT));
    // testing indices
    /*marko.setProperty("name", "marko");
        peter.setProperty("name", "peter");
        assertEquals(count(graph.getIndex(Index.VERTICES, Vertex.class).get("name", "marko")), 0);
        graph.addReadPartition("a");
        assertEquals(count(graph.getIndex(Index.VERTICES, Vertex.class).get("name", "marko")), 1);
        assertEquals(count(graph.getIndex(Index.VERTICES, Vertex.class).get("name", "peter")), 0);
        assertEquals(graph.getIndex(Index.VERTICES, Vertex.class).get("name", "marko").next(), marko);
        graph.removeReadPartition("a");
        graph.addReadPartition("b");
        assertEquals(count(graph.getIndex(Index.VERTICES, Vertex.class).get("name", "peter")), 1);
        assertEquals(count(graph.getIndex(Index.VERTICES, Vertex.class).get("name", "marko")), 0);
        assertEquals(graph.getIndex(Index.VERTICES, Vertex.class).get("name", "peter").next(), peter);
        graph.addReadPartition("a");
        assertEquals(count(graph.getIndex(Index.VERTICES, Vertex.class).get("name", "peter")), 1);
        assertEquals(count(graph.getIndex(Index.VERTICES, Vertex.class).get("name", "marko")), 1);

        assertEquals(count(graph.getIndex(Index.EDGES, Edge.class).get("label", "knows")), 1);
        assertEquals(graph.getIndex(Index.EDGES, Edge.class).get("label", "knows").next(), knows);
        graph.removeReadPartition("c");
        assertEquals(count(graph.getIndex(Index.EDGES, Edge.class).get("label", "knows")), 0);
        */
    graph.shutdown();
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Edge(com.tinkerpop.blueprints.Edge)

Example 45 with TinkerGraph

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

the class PartitionGraphTest method testVerticesSeparatedByEdgeInDifferentPartition.

public void testVerticesSeparatedByEdgeInDifferentPartition() {
    TinkerGraph rawGraph = new TinkerGraph();
    PartitionIndexableGraph graph = new PartitionIndexableGraph(rawGraph, "_writeGraph", "p1");
    Vertex inp1 = graph.addVertex("inp1");
    graph.setWritePartition("p2");
    Vertex inp2 = graph.addVertex("inp2");
    inp2.setProperty("key", "value");
    graph.setWritePartition("p3");
    graph.addEdge("inp3", inp1, inp2, "links");
    assertNull(graph.getVertex("inp2"));
    graph.addReadPartition("p2");
    graph.addReadPartition("p3");
    assertNotNull(graph.getVertex("inp1"));
    assertNotNull(graph.getVertex("inp2"));
    assertTrue(graph.getVertex("inp1").getEdges(Direction.OUT).iterator().hasNext());
    graph.removeReadPartition("p2");
    assertTrue(graph.getVertex("inp1").getEdges(Direction.OUT).iterator().hasNext());
    // the vertex at the end of this traversal is in the p2 partition which was removed above.  it should return
    // null, throw exception, something....
    assertNull(graph.getVertex("inp1").getEdges(Direction.OUT).iterator().next().getVertex(Direction.IN));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph)

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