Search in sources :

Example 26 with TinkerGraph

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

the class BatchGraphTest method testQuadLoading.

public void testQuadLoading() {
    int numEdges = 10000;
    String[][] quads = generateQuads(100, numEdges, new String[] { "knows", "friend" });
    TinkerGraph graph = new TinkerGraph();
    BatchGraph bgraph = new BatchGraph(new WritethroughGraph(graph), VertexIDType.STRING, 1000);
    for (String[] quad : quads) {
        Vertex[] vertices = new Vertex[2];
        for (int i = 0; i < 2; i++) {
            vertices[i] = bgraph.getVertex(quad[i]);
            if (vertices[i] == null)
                vertices[i] = bgraph.addVertex(quad[i]);
        }
        Edge edge = bgraph.addEdge(null, vertices[0], vertices[1], quad[2]);
        edge.setProperty("annotation", quad[3]);
    }
    assertEquals(numEdges, BaseTest.count(graph.getEdges()));
    bgraph.shutdown();
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) IgnoreIdTinkerGraph(com.tinkerpop.blueprints.impls.tg.IgnoreIdTinkerGraph) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Edge(com.tinkerpop.blueprints.Edge)

Example 27 with TinkerGraph

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

the class BatchGraphTest method testLoadingWithExisting2.

public void testLoadingWithExisting2() {
    int numEdges = 1000;
    String[][] quads = generateQuads(100, numEdges, new String[] { "knows", "friend" });
    TinkerGraph tg = new IgnoreIdTinkerGraph();
    BatchGraph bg = new BatchGraph(new WritethroughGraph(tg), VertexIDType.STRING, 100);
    try {
        bg.setLoadingFromScratch(false);
        fail();
    } catch (IllegalStateException e) {
    }
    bg.setVertexIdKey("uid");
    bg.setLoadingFromScratch(false);
    try {
        bg.setVertexIdKey(null);
        fail();
    } catch (IllegalStateException e) {
    }
    Graph graph = null;
    int counter = 0;
    for (String[] quad : quads) {
        if (counter < numEdges / 2)
            graph = tg;
        else
            graph = bg;
        Vertex[] vertices = new Vertex[2];
        for (int i = 0; i < 2; i++) {
            vertices[i] = graph.getVertex(quad[i]);
            if (vertices[i] == null)
                vertices[i] = graph.addVertex(quad[i]);
        }
        Edge edge = graph.addEdge(null, vertices[0], vertices[1], quad[2]);
        edge.setProperty("annotation", quad[3]);
        counter++;
    }
    assertEquals(numEdges, BaseTest.count(tg.getEdges()));
    bg.shutdown();
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) IgnoreIdTinkerGraph(com.tinkerpop.blueprints.impls.tg.IgnoreIdTinkerGraph) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) IgnoreIdTinkerGraph(com.tinkerpop.blueprints.impls.tg.IgnoreIdTinkerGraph) TransactionalGraph(com.tinkerpop.blueprints.TransactionalGraph) IgnoreIdTinkerGraph(com.tinkerpop.blueprints.impls.tg.IgnoreIdTinkerGraph) MockTransactionalGraph(com.tinkerpop.blueprints.impls.tg.MockTransactionalGraph) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) Edge(com.tinkerpop.blueprints.Edge)

Example 28 with TinkerGraph

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

the class GraphSONUtilityTest method edgeFromJsonInputStreamCompactNoIdOnEdge.

@Test
public void edgeFromJsonInputStreamCompactNoIdOnEdge() throws IOException, JSONException {
    Graph g = new TinkerGraph();
    ElementFactory factory = new GraphElementFactory(g);
    Set<String> vertexKeys = new HashSet<String>() {

        {
            add(GraphSONTokens._ID);
        }
    };
    Set<String> edgeKeys = new HashSet<String>() {

        {
            add(GraphSONTokens._IN_V);
        }
    };
    GraphSONUtility graphson = new GraphSONUtility(GraphSONMode.COMPACT, factory, vertexKeys, edgeKeys);
    Vertex v1 = graphson.vertexFromJson(new JSONObject(new JSONTokener(vertexJson1)));
    Vertex v2 = graphson.vertexFromJson(new JSONObject(new JSONTokener(vertexJson2)));
    Edge e = graphson.edgeFromJson(inputStreamEdgeJsonLight, v1, v2);
    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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 29 with TinkerGraph

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

the class GraphSONUtilityTest method edgeFromJsonValid.

@Test
public void edgeFromJsonValid() 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(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 : 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 30 with TinkerGraph

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

the class GraphSONUtilityTest method jsonFromElementNullsNoKeysWithTypes.

@Test
public void jsonFromElementNullsNoKeysWithTypes() throws JSONException {
    Graph g = new TinkerGraph();
    Vertex v = g.addVertex(1);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("innerkey", null);
    List<String> innerList = new ArrayList<String>();
    innerList.add(null);
    innerList.add("innerstring");
    map.put("list", innerList);
    v.setProperty("keyMap", map);
    List<String> list = new ArrayList<String>();
    list.add(null);
    list.add("string");
    v.setProperty("keyList", list);
    JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.EXTENDED);
    Assert.assertNotNull(json);
    JSONObject jsonMap = json.optJSONObject("keyMap").optJSONObject(GraphSONTokens.VALUE);
    Assert.assertNotNull(jsonMap);
    JSONObject jsonObjectMap = jsonMap.optJSONObject("innerkey");
    Assert.assertTrue(jsonObjectMap.isNull(GraphSONTokens.VALUE));
    Assert.assertEquals(GraphSONTokens.TYPE_UNKNOWN, jsonObjectMap.optString(GraphSONTokens.TYPE));
    JSONArray jsonInnerArray = jsonMap.getJSONObject("list").getJSONArray(GraphSONTokens.VALUE);
    Assert.assertNotNull(jsonInnerArray);
    JSONObject jsonObjectInnerListFirst = jsonInnerArray.getJSONObject(0);
    Assert.assertTrue(jsonObjectInnerListFirst.isNull(GraphSONTokens.VALUE));
    Assert.assertEquals(GraphSONTokens.TYPE_UNKNOWN, jsonObjectInnerListFirst.optString(GraphSONTokens.TYPE));
    JSONArray jsonArray = json.getJSONObject("keyList").getJSONArray(GraphSONTokens.VALUE);
    Assert.assertNotNull(jsonArray);
    JSONObject jsonObjectListFirst = jsonArray.getJSONObject(0);
    Assert.assertTrue(jsonObjectListFirst.isNull(GraphSONTokens.VALUE));
    Assert.assertEquals(GraphSONTokens.TYPE_UNKNOWN, jsonObjectListFirst.optString(GraphSONTokens.TYPE));
}
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) JSONObject(org.codehaus.jettison.json.JSONObject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(org.codehaus.jettison.json.JSONArray) JSONObject(org.codehaus.jettison.json.JSONObject) 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