Exemple #1
0
    public boolean matches(final Vertex vertex, final Value value) {
      String kind = (String) vertex.getProperty(KIND);
      String val = (String) vertex.getProperty(VALUE);
      if (value instanceof URI) {
        return kind.equals(URI) && val.equals(value.stringValue());
      } else if (value instanceof Literal) {
        if (kind.equals(LITERAL)) {
          if (!val.equals(((Literal) value).getLabel())) {
            return false;
          }

          String type = (String) vertex.getProperty(TYPE);
          String lang = (String) vertex.getProperty(LANG);

          URI vType = ((Literal) value).getDatatype();
          String vLang = ((Literal) value).getLanguage();

          return null == type && null == vType && null == lang && null == vLang
              || null != type && null != vType && type.equals(vType.stringValue())
              || null != lang && null != vLang && lang.equals(vLang);

        } else {
          return false;
        }
      } else if (value instanceof BNode) {
        return kind.equals(BNODE) && ((BNode) value).getID().equals(val);
      } else {
        throw new IllegalStateException("value of unexpected kind: " + value);
      }
    }
  public void testReadingTinkerGraph(Graph graph) throws Exception {
    if (!config.ignoresSuppliedIds) {

      this.stopWatch();
      GraphMLReader.inputGraph(
          graph, GraphMLReader.class.getResourceAsStream("graph-example-1.xml"));
      BaseTest.printPerformance(graph.toString(), null, "graph-example-1 loaded", this.stopWatch());

      assertEquals(count(graph.getVertex("1").getOutEdges()), 3);
      assertEquals(count(graph.getVertex("1").getInEdges()), 0);
      Vertex marko = graph.getVertex("1");
      assertEquals(marko.getProperty("name"), "marko");
      assertEquals(marko.getProperty("age"), 29);
      int counter = 0;
      for (Edge e : graph.getVertex("1").getOutEdges()) {
        if (e.getInVertex().getId().equals("2")) {
          // assertEquals(e.getProperty("weight"), 0.5);
          assertEquals(e.getLabel(), "knows");
          assertEquals(e.getId(), "7");
          counter++;
        } else if (e.getInVertex().getId().equals("3")) {
          assertEquals(Math.round((Float) e.getProperty("weight")), 0);
          assertEquals(e.getLabel(), "created");
          assertEquals(e.getId(), "9");
          counter++;
        } else if (e.getInVertex().getId().equals("4")) {
          assertEquals(Math.round((Float) e.getProperty("weight")), 1);
          assertEquals(e.getLabel(), "knows");
          assertEquals(e.getId(), "8");
          counter++;
        }
      }

      assertEquals(count(graph.getVertex("4").getOutEdges()), 2);
      assertEquals(count(graph.getVertex("4").getInEdges()), 1);
      Vertex josh = graph.getVertex("4");
      assertEquals(josh.getProperty("name"), "josh");
      assertEquals(josh.getProperty("age"), 32);
      for (Edge e : graph.getVertex("4").getOutEdges()) {
        if (e.getInVertex().getId().equals("3")) {
          assertEquals(Math.round((Float) e.getProperty("weight")), 0);
          assertEquals(e.getLabel(), "created");
          assertEquals(e.getId(), "11");
          counter++;
        } else if (e.getInVertex().getId().equals("5")) {
          assertEquals(Math.round((Float) e.getProperty("weight")), 1);
          assertEquals(e.getLabel(), "created");
          assertEquals(e.getId(), "10");
          counter++;
        }
      }

      assertEquals(counter, 5);
    }
  }
  @Test
  public void inputGraphNoTypes() throws IOException {
    TinkerGraph graph = new TinkerGraph();

    String json =
        "{ \"vertices\": [ {\"_id\":1, \"test\": \"please work\", \"testlist\":[1, 2, 3, null], \"testmap\":{\"big\":10000000000, \"small\":0.4954959595959, \"nullKey\":null}}, {\"_id\":2, \"testagain\":\"please work again\"}], \"edges\":[{\"_id\":100, \"_outV\":1, \"_inV\":2, \"_label\":\"works\", \"teste\": \"please worke\", \"keyNull\":null}]}";

    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.getOutVertex());
    Assert.assertEquals(v2, e.getInVertex());
    Assert.assertEquals("please worke", e.getProperty("teste"));
    Assert.assertNull(e.getProperty("keyNull"));
  }
 public void testIncludeStatementPathOverTinkerGraph() {
   GremlinEvaluator ge = new GremlinEvaluator();
   Graph graph = TinkerGraphFactory.createTinkerGraph();
   ge.getVariables().declareVariable(Tokens.GRAPH_VARIABLE, graph);
   ge.getVariables().declareVariable(Tokens.AT_VARIABLE, graph.getVertex("1"));
   assertTrue(
       (Boolean) ge.evaluate("include 'com.tinkerpop.gremlin.statements.TestPathLibrary'").get(0));
   List results = ge.evaluate("./co-developer");
   assertEquals(results.size(), 2);
   for (Vertex vertex : (List<Vertex>) results) {
     if (vertex.getProperty("name").equals("peter") || vertex.getProperty("name").equals("josh")) {
       assertTrue(true);
     } else {
       assertTrue(false);
     }
   }
 }
  @Test
  public void inputGraphWithTypesFullCycle() throws IOException {
    TinkerGraph graph = TinkerGraphFactory.createTinkerGraph();

    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    GraphSONWriter writer = new GraphSONWriter(graph);
    writer.outputGraph(stream, null, null, true);

    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));
      }
    }
  }
 public void testTinkerGraphSoftwareVertices(Graph graph) throws Exception {
   if (config.supportsVertexIteration) {
     this.stopWatch();
     GraphMLReader.inputGraph(
         graph, GraphMLReader.class.getResourceAsStream("graph-example-1.xml"));
     BaseTest.printPerformance(graph.toString(), null, "graph-example-1 loaded", this.stopWatch());
     Set<Vertex> softwareVertices = new HashSet<Vertex>();
     int count = 0;
     for (Vertex v : graph.getVertices()) {
       count++;
       String name = v.getProperty("name").toString();
       if (name.equals("lop") || name.equals("ripple")) {
         softwareVertices.add(v);
       }
     }
     assertEquals(count, 6);
     assertEquals(softwareVertices.size(), 2);
     for (Vertex v : softwareVertices) {
       assertEquals(v.getProperty("lang"), "java");
     }
   }
 }
Exemple #7
0
 private static void debugVertex(final Vertex v) {
   System.out.println("vertex " + v + ":");
   for (String key : v.getPropertyKeys()) {
     System.out.println("\t" + key + ":\t'" + v.getProperty(key) + "'");
   }
   Iterator<Edge> i;
   i = v.getInEdges().iterator();
   System.out.println("\t[in edges]:");
   while (i.hasNext()) {
     System.out.println("\t\t" + i.next());
   }
   i = v.getOutEdges().iterator();
   System.out.println("\t[out edges]:");
   while (i.hasNext()) {
     System.out.println("\t\t" + i.next());
   }
 }
 public void testTinkerGraphVertices(Graph graph) throws Exception {
   if (config.supportsVertexIteration) {
     this.stopWatch();
     GraphMLReader.inputGraph(
         graph, GraphMLReader.class.getResourceAsStream("graph-example-1.xml"));
     BaseTest.printPerformance(graph.toString(), null, "graph-example-1 loaded", this.stopWatch());
     Set<String> vertexNames = new HashSet<String>();
     int count = 0;
     for (Vertex v : graph.getVertices()) {
       count++;
       vertexNames.add(v.getProperty("name").toString());
       // System.out.println(v);
     }
     assertEquals(count, 6);
     assertEquals(vertexNames.size(), 6);
     assertTrue(vertexNames.contains("marko"));
     assertTrue(vertexNames.contains("josh"));
     assertTrue(vertexNames.contains("peter"));
     assertTrue(vertexNames.contains("vadas"));
     assertTrue(vertexNames.contains("ripple"));
     assertTrue(vertexNames.contains("lop"));
   }
 }
  public void testTinkerGraphVertexAndEdges(Graph graph) throws Exception {
    if (config.supportsVertexIteration) {
      this.stopWatch();
      GraphMLReader.inputGraph(
          graph, GraphMLReader.class.getResourceAsStream("graph-example-1.xml"));
      BaseTest.printPerformance(graph.toString(), null, "graph-example-1 loaded", this.stopWatch());
      Vertex marko = null;
      Vertex peter = null;
      Vertex josh = null;
      Vertex vadas = null;
      Vertex lop = null;
      Vertex ripple = null;
      int count = 0;
      for (Vertex v : graph.getVertices()) {
        count++;
        String name = v.getProperty("name").toString();
        if (name.equals("marko")) {
          marko = v;
        } else if (name.equals("peter")) {
          peter = v;
        } else if (name.equals("josh")) {
          josh = v;
        } else if (name.equals("vadas")) {
          vadas = v;
        } else if (name.equals("lop")) {
          lop = v;
        } else if (name.equals("ripple")) {
          ripple = v;
        } else {
          assertTrue(false);
        }
      }
      assertEquals(count, 6);
      assertTrue(null != marko);
      assertTrue(null != peter);
      assertTrue(null != josh);
      assertTrue(null != vadas);
      assertTrue(null != lop);
      assertTrue(null != ripple);

      // test marko
      Set<Vertex> vertices = new HashSet<Vertex>();
      assertEquals(marko.getProperty("name"), "marko");
      assertEquals(marko.getProperty("age"), 29);
      assertEquals(marko.getPropertyKeys().size(), 2);
      assertEquals(count(marko.getOutEdges()), 3);
      assertEquals(count(marko.getInEdges()), 0);
      for (Edge e : marko.getOutEdges()) {
        vertices.add(e.getInVertex());
      }
      assertEquals(vertices.size(), 3);
      assertTrue(vertices.contains(lop));
      assertTrue(vertices.contains(josh));
      assertTrue(vertices.contains(vadas));
      // test peter
      vertices = new HashSet<Vertex>();
      assertEquals(peter.getProperty("name"), "peter");
      assertEquals(peter.getProperty("age"), 35);
      assertEquals(peter.getPropertyKeys().size(), 2);
      assertEquals(count(peter.getOutEdges()), 1);
      assertEquals(count(peter.getInEdges()), 0);
      for (Edge e : peter.getOutEdges()) {
        vertices.add(e.getInVertex());
      }
      assertEquals(vertices.size(), 1);
      assertTrue(vertices.contains(lop));
      // test josh
      vertices = new HashSet<Vertex>();
      assertEquals(josh.getProperty("name"), "josh");
      assertEquals(josh.getProperty("age"), 32);
      assertEquals(josh.getPropertyKeys().size(), 2);
      assertEquals(count(josh.getOutEdges()), 2);
      assertEquals(count(josh.getInEdges()), 1);
      for (Edge e : josh.getOutEdges()) {
        vertices.add(e.getInVertex());
      }
      assertEquals(vertices.size(), 2);
      assertTrue(vertices.contains(lop));
      assertTrue(vertices.contains(ripple));
      vertices = new HashSet<Vertex>();
      for (Edge e : josh.getInEdges()) {
        vertices.add(e.getOutVertex());
      }
      assertEquals(vertices.size(), 1);
      assertTrue(vertices.contains(marko));
      // test vadas
      vertices = new HashSet<Vertex>();
      assertEquals(vadas.getProperty("name"), "vadas");
      assertEquals(vadas.getProperty("age"), 27);
      assertEquals(vadas.getPropertyKeys().size(), 2);
      assertEquals(count(vadas.getOutEdges()), 0);
      assertEquals(count(vadas.getInEdges()), 1);
      for (Edge e : vadas.getInEdges()) {
        vertices.add(e.getOutVertex());
      }
      assertEquals(vertices.size(), 1);
      assertTrue(vertices.contains(marko));
      // test lop
      vertices = new HashSet<Vertex>();
      assertEquals(lop.getProperty("name"), "lop");
      assertEquals(lop.getProperty("lang"), "java");
      assertEquals(lop.getPropertyKeys().size(), 2);
      assertEquals(count(lop.getOutEdges()), 0);
      assertEquals(count(lop.getInEdges()), 3);
      for (Edge e : lop.getInEdges()) {
        vertices.add(e.getOutVertex());
      }
      assertEquals(vertices.size(), 3);
      assertTrue(vertices.contains(marko));
      assertTrue(vertices.contains(josh));
      assertTrue(vertices.contains(peter));
      // test ripple
      vertices = new HashSet<Vertex>();
      assertEquals(ripple.getProperty("name"), "ripple");
      assertEquals(ripple.getProperty("lang"), "java");
      assertEquals(ripple.getPropertyKeys().size(), 2);
      assertEquals(count(ripple.getOutEdges()), 0);
      assertEquals(count(ripple.getInEdges()), 1);
      for (Edge e : ripple.getInEdges()) {
        vertices.add(e.getOutVertex());
      }
      assertEquals(vertices.size(), 1);
      assertTrue(vertices.contains(josh));
    }
  }
Exemple #10
0
 public String getValueOf(final Vertex v) {
   return (String) v.getProperty(VALUE);
 }