Example #1
0
  private String labelString(Node node) {
    try (ResourceIterator<Label> labels = node.getLabels().iterator()) {
      if (!labels.hasNext()) return "";

      StringBuilder result = new StringBuilder();
      while (labels.hasNext()) {
        Label next = labels.next();
        result.append(":").append(quote(next.name()));
      }
      return result.toString();
    }
  }
Example #2
0
 static Vertex addNode(Graph graph, Node node) {
   Vertex vertex = graph.getVertex(node.getId());
   if (null == vertex) {
     vertex = graph.addVertex(node.getId());
     copyProperties(node, vertex);
     Set<String> labels = new HashSet<>();
     for (Label label : node.getLabels()) {
       labels.add(label.name());
     }
     vertex.setProperty("types", labels);
   }
   return vertex;
 }
Example #3
0
  @Test
  public void testPun() {
    Node i = getNode("http://example.org/i");
    Node j = getNode("http://example.org/j");
    Node k = getNode("http://example.org/k");

    RelationshipType p = DynamicRelationshipType.withName("p");
    Relationship relationship = getOnlyElement(GraphUtil.getRelationships(i, j, p));
    assertThat("OPE edge should start with the subject.", relationship.getStartNode(), is(i));
    assertThat("OPE edge should end with the target.", relationship.getEndNode(), is(j));
    relationship =
        getOnlyElement(GraphUtil.getRelationships(i, k, OwlRelationships.RDFS_SUBCLASS_OF));
    assertThat("Subclass edge should start with i.", relationship.getStartNode(), is(i));
    assertThat("Subclass edge should end with k.", relationship.getEndNode(), is(k));
    assertThat(
        "i is both a class an a named individual",
        i.getLabels(),
        is(
            IsIterableContainingInAnyOrder.containsInAnyOrder(
                OwlLabels.OWL_CLASS, OwlLabels.OWL_NAMED_INDIVIDUAL)));
  }
  protected void verifyData(int nodeCount, GraphDatabaseService db) {
    // Verify that all the labels are in place
    Set<Label> expectedLabels = new HashSet<>();
    for (String label : LABELS) {
      expectedLabels.add(DynamicLabel.label(label));
    }
    GlobalGraphOperations globalOps = GlobalGraphOperations.at(db);
    Set<Label> allLabels = Iterables.toSet(globalOps.getAllLabels());
    assertThat(allLabels, is(expectedLabels));

    // Sample some nodes for deeper inspection of their contents
    Random random = new Random();
    for (int i = 0; i < nodeCount / 10; i++) {
      Node node = db.getNodeById(random.nextInt(nodeCount));
      int count = count(node.getRelationships());
      assertEquals("For node " + node, count, node.getDegree());
      for (String key : node.getPropertyKeys()) {
        node.getProperty(key);
        Set<Label> actualLabels = Iterables.toSet(node.getLabels());
        assertThat(actualLabels, is(expectedLabels));
      }
    }
  }
Example #5
0
  private void extractTypes(final Node node) {

    // first try: labels
    // AM 2015-06-26: Changed the behaviour here: In case of multiple labels, don't put them all
    // into the set of potential types but rather create a combined type.
    // E.g. a node with the two labels 'Person' and 'Entity' will get a type 'EntityPerson'
    final List<String> labelStrings = new ArrayList<>();
    for (final Label label : node.getLabels()) {
      labelStrings.add(label.name());
    }
    // Collections.sort(labelStrings);
    types.add(StringUtils.join(labelStrings, ""));

    // second try: type attribute
    if (node.hasProperty("type")) {

      final String type = node.getProperty("type").toString();
      types.add(type.replaceAll("[\\W]+", ""));
    }

    // deactivate relationship type nodes for now..

    /*

    // third try: incoming relationships
    final Set<String> incomingTypes = new LinkedHashSet<>();
    for (final Relationship incoming : node.getRelationships(Direction.INCOMING)) {
    	incomingTypes.add(incoming.getType().name());
    }

    // (if all incoming relationships are of the same type,
    // it is very likely that this is a type-defining trait)
    if (incomingTypes.size() == 1) {
    	 return CaseHelper.toUpperCamelCase(incomingTypes.iterator().next().toLowerCase());
    }

    // forth try: outgoing relationships
    final Set<String> outgoingTypes = new LinkedHashSet<>();
    for (final Relationship outgoing : node.getRelationships(Direction.OUTGOING)) {
    	outgoingTypes.add(outgoing.getType().name());
    }

    // (if all outgoing relationships are of the same type,
    // it is very likely that this is a type-defining trait)
    if (outgoingTypes.size() == 1) {
    	return CaseHelper.toUpperCamelCase(outgoingTypes.iterator().next().toLowerCase());
    }
    */

    if (types.isEmpty() && !properties.keySet().isEmpty()) {

      // fifth try: analyze properties
      final StringBuilder buf = new StringBuilder("NodeWith");
      for (final String key : properties.keySet()) {

        buf.append(StringUtils.capitalize(key));
      }

      types.add(buf.toString());
    }
  }