Exemplo n.º 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);
      }
    }
Exemplo n.º 2
0
 @Override
 public Edge addEdge(
     final Object id, final Vertex outVertex, final Vertex inVertex, final String label) {
   final Resource inV = model.createResource(inVertex.getId().toString());
   final Resource outV = model.createResource(outVertex.getId().toString());
   final Property edge = model.createProperty(label);
   final Statement statement = model.createStatement(outV, edge, inV);
   model.add(statement);
   return new JenaEdge(model, edge, inV, outV);
 }
Exemplo n.º 3
0
    public Vertex addVertex(final Value value) {
      Vertex v = graph.addVertex(null);

      if (value instanceof URI) {
        v.setProperty(KIND, URI);
        v.setProperty(VALUE, value.stringValue());
      } else if (value instanceof Literal) {
        Literal l = (Literal) value;
        v.setProperty(KIND, LITERAL);
        v.setProperty(VALUE, l.getLabel());
        if (null != l.getDatatype()) {
          v.setProperty(TYPE, l.getDatatype().stringValue());
        }
        if (null != l.getLanguage()) {
          v.setProperty(LANG, l.getLanguage());
        }
      } else if (value instanceof BNode) {
        BNode b = (BNode) value;
        v.setProperty(KIND, BNODE);
        v.setProperty(VALUE, b.getID());
      } else {
        throw new IllegalStateException("value of unexpected type: " + value);
      }

      return v;
    }
Exemplo n.º 4
0
 @Override
 public void removeVertex(final Vertex vertex) {
   // find all statements that have the Vertex id (URI) as either
   // the subject or the object
   if (vertex.getId() == null) {
     return;
   }
   final StmtIterator statements =
       model.listStatements(
           new SimpleSelector() {
             @Override
             public boolean selects(final Statement s) {
               final Resource subject = s.getSubject();
               final RDFNode object = s.getObject();
               if (subject.getURI().equals(vertex.getId().toString())) {
                 return true;
               } else if (object.isResource()
                   && object.asResource().getURI().equals(vertex.getId().toString())) {
                 return true;
               }
               return false;
             }
           });
   // anything that matched should be removed
   final List<Statement> statementsToRemove = new ArrayList<Statement>();
   while (statements.hasNext()) {
     final Statement statement = statements.next();
     statementsToRemove.add(statement);
   }
   for (final Statement statement : statementsToRemove) {
     model.remove(statement);
   }
 }
Exemplo n.º 5
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());
   }
 }
Exemplo n.º 6
0
 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);
     }
   }
 }
Exemplo n.º 7
0
  /**
   * The object id must be a Map&lt;String,Object&gt; or null. The id is the properties written when
   * the vertex is created. While it is possible to Edge.setProperty(), this method is faster.
   *
   * @param id a id of properties which can be null
   * @return the newly created vertex
   */
  public Edge addEdge(
      final Object id, final Vertex outVertex, final Vertex inVertex, final String label) {
    final Map<String, Object> finalProperties;
    if (id == null || !(id instanceof Map)) finalProperties = new HashMap<String, Object>();
    else finalProperties = makePropertyMap((Map<String, Object>) id);
    final Long finalId =
        this.rawGraph.createRelationship(
            (Long) outVertex.getId(),
            (Long) inVertex.getId(),
            DynamicRelationshipType.withName(label),
            finalProperties);

    final Neo4jBatchEdge edge = new Neo4jBatchEdge(this, finalId, label);
    if (finalProperties.size() > 0) {
      for (final Neo4jBatchAutomaticIndex<Neo4jBatchEdge> index :
          this.automaticEdgeIndices.values()) {
        index.autoUpdate(edge, finalProperties);
      }
    }
    return edge;
  }
 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");
     }
   }
 }
 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 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);
    }
  }
Exemplo n.º 11
0
  @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"));
  }
Exemplo n.º 12
0
  @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));
      }
    }
  }
Exemplo n.º 13
0
  public void testNeo4jGraph() throws Exception {
    double totalTime = 0.0d;
    Graph graph = graphTest.getGraphInstance();
    GraphMLReader.inputGraph(graph, GraphMLReader.class.getResourceAsStream("graph-example-2.xml"));
    graph.shutdown();

    for (int i = 0; i < TOTAL_RUNS; i++) {
      graph = graphTest.getGraphInstance();
      this.stopWatch();
      int counter = 0;
      for (final Vertex vertex : graph.getVertices()) {
        counter++;
        for (final Edge edge : vertex.getOutEdges()) {
          counter++;
          final Vertex vertex2 = edge.getInVertex();
          counter++;
          for (final Edge edge2 : vertex2.getOutEdges()) {
            counter++;
            final Vertex vertex3 = edge2.getInVertex();
            counter++;
            for (final Edge edge3 : vertex3.getOutEdges()) {
              counter++;
              edge3.getOutVertex();
              counter++;
            }
          }
        }
      }
      double currentTime = this.stopWatch();
      totalTime = totalTime + currentTime;
      BaseTest.printPerformance(
          graph.toString(), counter, "Neo4jGraph elements touched", currentTime);
      graph.shutdown();
    }
    BaseTest.printPerformance(
        "Neo4jGraph", 1, "Neo4jGraph experiment average", totalTime / (double) TOTAL_RUNS);
  }
  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));
    }
  }
Exemplo n.º 15
0
 public String getValueOf(final Vertex v) {
   return (String) v.getProperty(VALUE);
 }
Exemplo n.º 16
0
 public Vertex addVertex(final String id) {
   Vertex v = graph.addVertex(null);
   // vertices.put(VALUE, id, store.namespaces);
   v.setProperty(VALUE, id);
   return v;
 }