Search in sources :

Example 61 with Edge

use of com.tinkerpop.blueprints.Edge in project blueprints by tinkerpop.

the class GraphSONUtilityTest method jsonFromElementEdgeCompactIdOnlyAsExclude.

@Test
public void jsonFromElementEdgeCompactIdOnlyAsExclude() throws JSONException {
    ElementFactory factory = new GraphElementFactory(this.graph);
    Vertex v1 = this.graph.addVertex(1);
    Vertex v2 = this.graph.addVertex(2);
    Edge e = this.graph.addEdge(3, v1, v2, "test");
    e.setProperty("weight", 0.5f);
    e.setProperty("x", "y");
    Set<String> propertiesToExclude = new HashSet<String>() {

        {
            add(GraphSONTokens._TYPE);
            add(GraphSONTokens._LABEL);
            add(GraphSONTokens._IN_V);
            add(GraphSONTokens._OUT_V);
            add("weight");
        }
    };
    ElementPropertyConfig config = new ElementPropertyConfig(null, propertiesToExclude, ElementPropertyConfig.ElementPropertiesRule.INCLUDE, ElementPropertyConfig.ElementPropertiesRule.EXCLUDE);
    GraphSONUtility utility = new GraphSONUtility(GraphSONMode.COMPACT, factory, config);
    JSONObject json = utility.jsonFromElement(e);
    Assert.assertNotNull(json);
    Assert.assertFalse(json.has(GraphSONTokens._TYPE));
    Assert.assertFalse(json.has(GraphSONTokens._LABEL));
    Assert.assertFalse(json.has(GraphSONTokens._IN_V));
    Assert.assertFalse(json.has(GraphSONTokens._OUT_V));
    Assert.assertTrue(json.has(GraphSONTokens._ID));
    Assert.assertFalse(json.has("weight"));
    Assert.assertTrue(json.has("x"));
    Assert.assertEquals("y", json.optString("x"));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) JSONObject(org.codehaus.jettison.json.JSONObject) Edge(com.tinkerpop.blueprints.Edge) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 62 with Edge

use of com.tinkerpop.blueprints.Edge in project blueprints by tinkerpop.

the class GMLReaderTest method exampleGMLGetsCorrectTopology.

@Test
public void exampleGMLGetsCorrectTopology() throws IOException {
    TinkerGraph graph = new TinkerGraph();
    GMLReader.inputGraph(graph, GMLReader.class.getResourceAsStream("example.gml"));
    Vertex v1 = graph.getVertex(1);
    Vertex v2 = graph.getVertex(2);
    Vertex v3 = graph.getVertex(3);
    Iterable<Edge> out1 = v1.getEdges(Direction.OUT);
    Edge e1 = out1.iterator().next();
    Assert.assertEquals(v2, e1.getVertex(Direction.IN));
    Iterable<Edge> out2 = v2.getEdges(Direction.OUT);
    Edge e2 = out2.iterator().next();
    Assert.assertEquals(v3, e2.getVertex(Direction.IN));
    Iterable<Edge> out3 = v3.getEdges(Direction.OUT);
    Edge e3 = out3.iterator().next();
    Assert.assertEquals(v1, e3.getVertex(Direction.IN));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 63 with Edge

use of com.tinkerpop.blueprints.Edge in project blueprints by tinkerpop.

the class GraphSONReaderTest method inputGraphModeExtended.

@Test
public void inputGraphModeExtended() throws IOException {
    TinkerGraph graph = new TinkerGraph();
    String json = "{ \"mode\":\"EXTENDED\", \"vertices\": [ {\"_id\":1, \"_type\":\"vertex\", \"test\": { \"type\":\"string\", \"value\":\"please work\"}, \"testlist\":{\"type\":\"list\", \"value\":[{\"type\":\"int\", \"value\":1}, {\"type\":\"int\",\"value\":2}, {\"type\":\"int\",\"value\":3}]}, \"testmap\":{\"type\":\"map\", \"value\":{\"big\":{\"type\":\"long\", \"value\":10000000000}, \"small\":{\"type\":\"double\", \"value\":0.4954959595959}}}}, {\"_id\":2, \"_type\":\"vertex\", \"testagain\":{\"type\":\"string\", \"value\":\"please work again\"}}], \"edges\":[{\"_id\":100, \"_type\":\"edge\", \"_outV\":1, \"_inV\":2, \"_label\":\"works\", \"teste\": {\"type\":\"string\", \"value\":\"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(3, 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 64 with Edge

use of com.tinkerpop.blueprints.Edge 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 65 with Edge

use of com.tinkerpop.blueprints.Edge in project blueprints by tinkerpop.

the class GraphSONUtilityTest method jsonFromElementEdgeCompactAllKeys.

@Test
public void jsonFromElementEdgeCompactAllKeys() throws JSONException {
    Vertex v1 = this.graph.addVertex(1);
    Vertex v2 = this.graph.addVertex(2);
    Edge e = this.graph.addEdge(3, v1, v2, "test");
    e.setProperty("weight", 0.5f);
    JSONObject json = GraphSONUtility.jsonFromElement(e, null, GraphSONMode.COMPACT);
    Assert.assertNotNull(json);
    Assert.assertTrue(json.has(GraphSONTokens._ID));
    Assert.assertTrue(json.has(GraphSONTokens._TYPE));
    Assert.assertTrue(json.has(GraphSONTokens._LABEL));
    Assert.assertTrue(json.has(GraphSONTokens._IN_V));
    Assert.assertTrue(json.has(GraphSONTokens._OUT_V));
    Assert.assertEquals(0.5d, json.optDouble("weight"), 0.0d);
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) JSONObject(org.codehaus.jettison.json.JSONObject) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Aggregations

Edge (com.tinkerpop.blueprints.Edge)214 Vertex (com.tinkerpop.blueprints.Vertex)141 Test (org.junit.Test)53 Graph (com.tinkerpop.blueprints.Graph)49 TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)49 HashSet (java.util.HashSet)28 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)13 ArrayList (java.util.ArrayList)13 Collection (java.util.Collection)11 JSONObject (org.codehaus.jettison.json.JSONObject)11 HashMap (java.util.HashMap)10 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)9 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)9 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)8 Map (java.util.Map)8 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)7 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)6 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)6 KeyIndexableGraph (com.tinkerpop.blueprints.KeyIndexableGraph)6 URI (org.openrdf.model.URI)6