/**
  * check if rest traversal and traversal via the collection node return the same result
  *
  * @throws Exception
  */
 @Test
 public void checkTraverseByCollectionNode() throws Exception {
   Traverser heroesTraverserRest = getHeroesViaRest();
   Traverser heroesTraverserByCollection = getHeroesByCollectionNodeViaRest();
   assertEquals(
       heroesTraverserRest.nodes().iterator().next(),
       heroesTraverserByCollection.nodes().iterator().next());
 }
 /**
  * check if different REST Traversals for all heroes return the same
  *
  * @throws Exception
  */
 @Test
 public void checkTraverseByPropertiesRest() throws Exception {
   Traverser heroesTraverserRest = getHeroesViaRest();
   Traverser heroesTraverserByPropertiesRest = getHeroesByNodePropertiesViaRest();
   assertEquals(
       heroesTraverserRest.nodes().iterator().next(),
       heroesTraverserByPropertiesRest.nodes().iterator().next());
 }
 @Test
 public void testTraverseToNeighbour() {
   final Relationship rel = relationship();
   final TraversalDescription traversalDescription =
       RestTraversal.description().maxDepth(1).breadthFirst();
   System.out.println("traversalDescription = " + traversalDescription);
   final Traverser traverser = traversalDescription.traverse(rel.getStartNode());
   final Iterable<Node> nodes = traverser.nodes();
   Assert.assertEquals(rel.getEndNode(), nodes.iterator().next());
 }
示例#4
0
 public SearchResults searchIndex(SearchFilter filter) {
   try (Transaction tx = database.beginTx()) {
     SearchEvaluator searchEvaluator = new SearchEvaluator(filter);
     TraversalDescription td =
         database
             .traversalDescription()
             .depthFirst()
             .relationships(RTreeRelationshipTypes.RTREE_CHILD, Direction.OUTGOING)
             .relationships(RTreeRelationshipTypes.RTREE_REFERENCE, Direction.OUTGOING)
             .evaluator(searchEvaluator);
     Traverser traverser = td.traverse(getIndexRoot());
     SearchResults results = new SearchResults(traverser.nodes());
     tx.success();
     return results;
   }
 }
示例#5
0
  private void findAndCreateSuggestions(final Node node) {
    if (!node.hasRelationship(ConnectionType.KNOWS)) {
      return;
    }

    final Traverser traverser = friendToSuggestionTraversalDescription().traverse(node);
    for (final Node suggestionNode : traverser.nodes()) {
      if (!this.isSuggestionRelationshipPresent(node, suggestionNode)) {
        node.createRelationshipTo(suggestionNode, ConnectionType.SUGGESTED);
        if (logger.isInfoEnabled()) {
          logger.info(
              "New Suggestion Between {} and {}",
              node.getPropertyValues(),
              suggestionNode.getPropertyValues());
        }
      }
    }
  }
示例#6
0
  public String getGroups(@Name("filter") String filter) {
    StringWriter resultWriter = new StringWriter();
    JsonFactory f = new JsonFactory();
    f.setCodec(om);
    JsonGenerator g;
    try {
      g = f.createGenerator(resultWriter);

      final Node target = DB.getIndex().get("id", "agents").getSingle();
      TraversalDescription td =
          Traversal.description()
              .relationships(RelTypes.SUBGROUP, Direction.OUTGOING)
              .uniqueness(Uniqueness.NODE_PATH);

      Traverser results = td.traverse(target);
      g.writeStartArray();
      Iterator<Path> paths = results.iterator();
      while (paths.hasNext()) {
        Path path = paths.next();
        if (!path.endNode().hasProperty("name")) continue;
        g.writeStartObject();
        g.writeStringField("name", (String) path.endNode().getProperty("name"));
        g.writeArrayFieldStart("path");
        for (Node pathStep : path.nodes()) {
          if (pathStep.hasProperty("name")) g.writeString((String) pathStep.getProperty("name"));
        }
        g.writeEndArray();
        g.writeEndObject();
      }
      g.writeEndArray();
      g.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
    resultWriter.flush();
    return resultWriter.toString();
  }
示例#7
0
 @Override
 public TraversalMetadata metadata() {
   return lastTraverser.metadata();
 }
示例#8
0
  public String getContacts(@Name("filter") String filter) {
    StringWriter resultWriter = new StringWriter();
    JsonFactory f = new JsonFactory();
    f.setCodec(om);
    JsonGenerator g;
    try {
      g = f.createGenerator(resultWriter);

      final Node target = DB.getIndex().get("id", "agents").getSingle();
      TraversalDescription td =
          Traversal.description()
              .relationships(RelTypes.GROUPMEMBER, Direction.OUTGOING)
              .relationships(RelTypes.SUBGROUP, Direction.OUTGOING)
              .uniqueness(Uniqueness.NODE_PATH)
              .evaluator(
                  new Evaluator() {
                    @Override
                    public Evaluation evaluate(Path path) {
                      if (path.endNode().hasProperty("name")
                          && path.lastRelationship().isType(RelTypes.GROUPMEMBER)) {
                        return Evaluation.INCLUDE_AND_PRUNE;
                      }
                      return Evaluation.EXCLUDE_AND_CONTINUE;
                    }
                  });

      Traverser results = td.traverse(target);

      Multimap<Long, Path> groupByNode = ArrayListMultimap.create();

      Iterator<Path> paths = results.iterator();
      while (paths.hasNext()) {
        Path path = paths.next();
        groupByNode.put(path.endNode().getId(), path);
      }
      g.writeStartArray();
      for (Long nodeId : groupByNode.keySet()) {
        List<Path> pathList = (List<Path>) groupByNode.get(nodeId);
        g.writeStartObject();
        Node node = pathList.get(0).endNode();
        g.writeStringField("name", (String) node.getProperty("name"));
        g.writeArrayFieldStart("groups");
        for (Path path : pathList) {
          g.writeStartObject();
          g.writeStringField(
              "name", (String) path.lastRelationship().getStartNode().getProperty("name"));
          g.writeArrayFieldStart("path");
          for (Node pathStep : path.nodes()) {
            if (!pathStep.equals(node) && pathStep.hasProperty("name"))
              g.writeString((String) pathStep.getProperty("name"));
          }
          g.writeEndArray();
          g.writeEndObject();
        }
        g.writeEndArray();
        g.writeEndObject();
      }
      g.writeEndArray();
      g.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
    resultWriter.flush();
    return resultWriter.toString();
  }