Ejemplo n.º 1
0
 private Traverser simpleListTraverser() {
   return Traversal.description()
       .expand(
           Traversal.expanderForTypes(
               DynamicRelationshipType.withName("NEXT"), Direction.OUTGOING))
       .depthFirst()
       .uniqueness(Uniqueness.NODE_GLOBAL)
       .traverse(startNode);
 }
Ejemplo n.º 2
0
  private BranchOrderingPolicy parseOrder(String order) {
    if (order.equals("depth first") || "depth first".startsWith(order.toLowerCase())) {
      return Traversal.preorderDepthFirst();
    }
    if (order.equals("breadth first") || "breadth first".startsWith(order.toLowerCase())) {
      return Traversal.preorderBreadthFirst();
    }

    return (BranchOrderingPolicy) parseEnum(CommonBranchOrdering.class, order, null);
  }
Ejemplo n.º 3
0
 public ListRepresentation getNodeRelationships(
     long nodeId, RelationshipDirection direction, Collection<String> types)
     throws NodeNotFoundException {
   Node node = node(nodeId);
   Expander expander;
   if (types.isEmpty()) {
     expander = Traversal.expanderForAllTypes(direction.internal);
   } else {
     expander = Traversal.emptyExpander();
     for (String type : types) {
       expander = expander.add(DynamicRelationshipType.withName(type), direction.internal);
     }
   }
   return RelationshipRepresentation.list(expander.expand(node));
 }
Ejemplo n.º 4
0
 private TraversalDescription friendToSuggestionTraversalDescription() {
   return Traversal.description()
       .breadthFirst()
       .relationships(ConnectionType.KNOWS)
       .evaluator(Evaluators.excludeStartPosition())
       .evaluator(Evaluators.atDepth(2));
 }
Ejemplo n.º 5
0
 @SuppressWarnings("deprecation")
 @Override
 public TraversalDescription build(
     Object start, Neo4jPersistentProperty property, String... params) {
   return new TraversalDescriptionImpl()
       .relationships(DynamicRelationshipType.withName(params[0]))
       .filter(Traversal.returnAllButStartNode());
 }
 /**
  * returns a traverser for all nodes that have an outgoing relationship of the type KNOWS
  *
  * @param person the startnode
  * @return the Traverser
  */
 private static Traverser getFriends(final Node person) {
   TraversalDescription td =
       RestTraversal.description()
           .maxDepth(10)
           .breadthFirst()
           .relationships(RelTypes.KNOWS, Direction.OUTGOING)
           .filter(Traversal.returnAllButStartNode());
   return td.traverse(person);
 }
  private void testAllRelationshipsAreReturnedOnce(TraversalDescription description)
      throws Exception {
    Traverser traverser =
        Traversal.description()
            .uniqueness(Uniqueness.RELATIONSHIP_GLOBAL)
            .traverse(referenceNode());

    expectRelationships(traverser, THE_WORLD_AS_WE_KNOW_IT);
  }
 /**
  * returns a traverser for all nodes that have a property type == hero in the embedded Database
  *
  * @return the Traverser
  */
 private Traverser getHeroesByNodeProperties() {
   TraversalDescription td =
       Traversal.description()
           .breadthFirst()
           .relationships(RelTypes.PERSONS_REFERENCE, Direction.OUTGOING)
           .relationships(RelTypes.HEROES_REFERENCE, Direction.OUTGOING)
           .relationships(RelTypes.HERO, Direction.OUTGOING)
           .filter(
               new Predicate<Path>() {
                 public boolean accept(Path path) {
                   return path.endNode().getProperty("type", "none").equals("hero");
                 }
               });
   return td.traverse(this.embeddedmdg.getGraphDatabase().getReferenceNode());
 }
 @Test
 public void testSmallestPossibleInit() throws Exception {
   Traverser traversal = Traversal.description().traverse(referenceNode());
   int count = 0;
   for (Path position : traversal) {
     count++;
     assertNotNull(position);
     assertNotNull(position.endNode());
     if (position.length() > 0) {
       assertNotNull(position.lastRelationship());
     }
     assertNotNull(position.length());
   }
   assertFalse("empty traversal", count == 0);
 }
 @SuppressWarnings("deprecation")
 @Test
 @Transactional
 public void testTraverse() throws Exception {
   // final TraversalDescription description =
   // Traversal.description().relationships(KNOWS).prune(Traversal.pruneAfterDepth(1)).filter(Traversal.returnAllButStartNode());
   final TraversalDescription description =
       Traversal.description()
           .relationships(KNOWS)
           .evaluator(Evaluators.toDepth(1))
           .evaluator(Evaluators.excludeStartPosition());
   assertSingleResult(
       "node1",
       neo4jTemplate
           .traverse(referenceNode, description)
           .to(String.class, new PathNodeNameMapper()));
 }
Ejemplo n.º 11
0
 protected static RelationshipExpander toExpander(
     GraphDatabaseService db,
     Direction defaultDirection,
     Map<String, Object> relationshipTypes,
     boolean caseInsensitiveFilters,
     boolean looseFilters)
     throws ShellException {
   defaultDirection = defaultDirection != null ? defaultDirection : Direction.BOTH;
   Map<String, Direction> matches =
       filterMapToTypes(
           db, defaultDirection, relationshipTypes, caseInsensitiveFilters, looseFilters);
   Expander expander = Traversal.emptyExpander();
   if (matches == null) return EMPTY_EXPANDER;
   for (Map.Entry<String, Direction> entry : matches.entrySet()) {
     expander = expander.add(DynamicRelationshipType.withName(entry.getKey()), entry.getValue());
   }
   return expander;
 }
Ejemplo n.º 12
0
  /** @return the dbsed */
  @Transactional
  public DBsE getDbsed() {
    Iterable<DBsE> i =
        findAllByTraversal(
            DBsE.class,
            Traversal.description()
                .relationships(DynamicRelationshipType.withName("DBSED"))
                .breadthFirst()
                .evaluator(
                    new Evaluator() {

                      @Override
                      public Evaluation evaluate(Path path) {
                        if (path.length() == 1) return Evaluation.INCLUDE_AND_PRUNE;
                        else return Evaluation.EXCLUDE_AND_PRUNE;
                      }
                    }));

    return i.iterator().next();
  }
Ejemplo n.º 13
0
 @Test
 public void getAllGroups() throws Exception {
   Transaction tx = graphDb.beginTx();
   try {
     System.out.println("All groups:");
     // START SNIPPET: get-groups
     Node referenceNode = graphDb.getReferenceNode();
     TraversalDescription td =
         Traversal.description()
             .breadthFirst()
             .relationships(RoleRels.ROOT, Direction.INCOMING)
             .relationships(RoleRels.PART_OF, Direction.INCOMING)
             .evaluator(Evaluators.excludeStartPosition());
     for (Node group : td.traverse(referenceNode).nodes()) {
       System.out.println(group.getProperty(NAME));
     }
     // END SNIPPET: get-groups
     tx.success();
   } finally {
     tx.finish();
   }
 }
  public RelationshipFollowingResource(
      SecurityContext securityContext, TypedIdResource typedIdResource) {

    this.traversalDescription =
        Traversal.description()
            .depthFirst()
            .uniqueness(Uniqueness.NODE_GLOBAL)
            .evaluator(Evaluators.excludeStartPosition());
    this.visitedRelationships = new LinkedHashSet<AbstractRelationProperty>();
    this.securityContext = securityContext;
    this.idSet = new LinkedHashSet<Object>();
    this.uriParts = new LinkedList<String>();

    // add TypedIdResource to list of evaluators
    traversalDescription = traversalDescription.evaluator(this);

    // store first and last constraint separately
    // to be able to access them faster afterwards
    firstResource = typedIdResource;
    lastResource = typedIdResource;

    UuidResource idResource = typedIdResource.getIdResource();

    if (idResource instanceof UuidResource) {

      logger.log(Level.FINE, "Adding id {0} to id set", idResource.getUriPart());

      // add uuid from TypedIdResource to idSet
      idSet.add(((UuidResource) idResource).getUriPart());

    } else {

      logger.log(Level.FINE, "Adding id {0} to id set", idResource.getUriPart());

      // add id from TypedIdResource to idSet
      idSet.add(idResource.getUuid());
    }
  }
Ejemplo n.º 15
0
 @Test
 public void getAllAdmins() {
   Transaction tx = graphDb.beginTx();
   try {
     System.out.println("All admins:");
     // START SNIPPET: get-admins
     Node admins = getGroupByName("Admins");
     TraversalDescription td =
         Traversal.description()
             .breadthFirst()
             .relationships(RoleRels.PART_OF, Direction.INCOMING)
             .relationships(RoleRels.MEMBER_OF, Direction.INCOMING)
             .evaluator(Evaluators.excludeStartPosition());
     for (Path path : td.traverse(admins)) {
       Node part = path.endNode();
       System.out.println(part.getProperty(NAME) + " " + (path.length() - 1));
     }
     // END SNIPPET: get-admins
     tx.success();
   } finally {
     tx.finish();
   }
 }
Ejemplo n.º 16
0
 @Test
 public void getJalesMemberships() throws Exception {
   Transaction tx = graphDb.beginTx();
   try {
     System.out.println("Jale's memberships:");
     // START SNIPPET: get-user-memberships
     Node jale = getUserByName("Jale");
     TraversalDescription td =
         Traversal.description()
             .depthFirst()
             .relationships(RoleRels.MEMBER_OF, Direction.OUTGOING)
             .relationships(RoleRels.PART_OF, Direction.OUTGOING)
             .evaluator(Evaluators.excludeStartPosition());
     for (Path path : td.traverse(jale)) {
       Node membership = path.endNode();
       System.out.println(membership.getProperty(NAME) + " " + (path.length() - 1));
     }
     // END SNIPPET: get-user-memberships
     tx.success();
   } finally {
     tx.finish();
   }
 }
Ejemplo n.º 17
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();
  }
Ejemplo n.º 18
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();
  }
 @Test
 public void canPreFilterNodesBreadthFirst() {
   canPreFilterNodes(Traversal.description().breadthFirst());
 }
 @Test
 public void canPreFilterNodesDepthFirst() {
   canPreFilterNodes(Traversal.description().depthFirst());
 }
 @Test
 public void canPruneTraversalAtSpecificDepthBreadthFirst() {
   canPruneTraversalAtSpecificDepth(Traversal.description().breadthFirst());
 }
Ejemplo n.º 22
0
 /**
  * @param profile to start from
  * @param types to filter
  * @return list of concepts
  */
 public ArrayList<Concept> getConceptsForProfile(final Profile person, final List<Type> types) {
   final Node node = template.getNode(person.getNodeId());
   ArrayList<Concept> concepts = new ArrayList<Concept>();
   TraversalDescription td =
       Traversal.description()
           .depthFirst()
           .relationships(RelationshipTypes.LIKES, Direction.OUTGOING)
           .relationships(RelationshipTypes.FRIEND, Direction.OUTGOING)
           .relationships(RelationshipTypes.BORN_IN, Direction.OUTGOING)
           .relationships(RelationshipTypes.LIVES_IN, Direction.OUTGOING)
           .relationships(RelationshipTypes.EDUCATED_AT, Direction.OUTGOING)
           .relationships(RelationshipTypes.WORKS_AT, Direction.OUTGOING)
           .relationships(RelationshipTypes.BASED_IN, Direction.OUTGOING)
           .evaluator(
               new Evaluator() {
                 @Override
                 public Evaluation evaluate(Path path) {
                   if (path.lastRelationship() != null) {
                     if (path.lastRelationship().getType().equals(RelationshipTypes.FRIEND)) {
                       if (!path.lastRelationship().getStartNode().equals(node)) {
                         return Evaluation.EXCLUDE_AND_PRUNE;
                       } else {
                         return Evaluation.EXCLUDE_AND_CONTINUE;
                       }
                     } else {
                       if (path.endNode().hasProperty(NamespaceConstants.TYPE)) {
                         String conceptType =
                             path.endNode().getProperty(NamespaceConstants.TYPE).toString();
                         //									if(things.isInTypes(types,conceptType) |
                         // (FType.getTypeForFBType(conceptType) != null &&
                         // things.findThing(FType.getTypeForFBType(conceptType).getUri()) !=
                         // null)){
                         //										return Evaluation.INCLUDE_AND_PRUNE;
                         //									} else {
                         //										return Evaluation.EXCLUDE_AND_PRUNE;
                         //									}
                         if (conceptType.startsWith("http")) {
                           if (things.isInTypes(types, conceptType)) {
                             return Evaluation.INCLUDE_AND_PRUNE;
                           } else {
                             return Evaluation.EXCLUDE_AND_PRUNE;
                           }
                         } else {
                           if (things.isInTypesFromFBTypes(types, conceptType)) {
                             return Evaluation.INCLUDE_AND_PRUNE;
                           } else {
                             return Evaluation.EXCLUDE_AND_PRUNE;
                           }
                         }
                       } else {
                         return Evaluation.EXCLUDE_AND_PRUNE;
                       }
                     }
                   } else return Evaluation.EXCLUDE_AND_CONTINUE;
                 }
               });
   Traverser t = td.traverse(node);
   for (Path path : t) {
     Concept c = getByNode(path.endNode());
     concepts.add(c);
   }
   return concepts;
 }
 private void initFinder() {
   this.finder = GraphAlgoFactory.allSimplePaths(Traversal.expanderForAllTypes(), 7);
 }
 @Test
 public void testAllRelationshipsAreReturnedOnceBreadthFirst() throws Exception {
   testAllRelationshipsAreReturnedOnce(Traversal.description().breadthFirst());
 }
Ejemplo n.º 25
0
  @Override
  protected Continuation exec(AppCommandParser parser, Session session, Output out)
      throws ShellException, RemoteException {
    assertCurrentIsNode(session);

    Node node = this.getCurrent(session).asNode();
    boolean caseInsensitiveFilters = parser.options().containsKey("i");
    boolean looseFilters = parser.options().containsKey("l");
    boolean quiet = parser.options().containsKey("q");

    // Order
    TraversalDescription description = Traversal.description();
    String order = parser.options().get("o");
    if (order != null) {
      description = description.order(parseOrder(order));
    }

    // Relationship types / expander
    String relationshipTypes = parser.options().get("r");
    if (relationshipTypes != null) {
      Map<String, Object> types = parseFilter(relationshipTypes, out);
      description =
          description.expand(
              toExpander(getServer().getDb(), null, types, caseInsensitiveFilters, looseFilters));
    }

    // Uniqueness
    String uniqueness = parser.options().get("u");
    if (uniqueness != null) {
      description = description.uniqueness(parseUniqueness(uniqueness));
    }

    // Depth limit
    String depthLimit = parser.options().get("d");
    if (depthLimit != null) {
      description = description.evaluator(toDepth(parseInt(depthLimit)));
    }

    String filterString = parser.options().get("f");
    Map<String, Object> filterMap = filterString != null ? parseFilter(filterString, out) : null;
    String commandToRun = parser.options().get("c");
    Collection<String> commandsToRun = new ArrayList<String>();
    if (commandToRun != null) {
      commandsToRun.addAll(Arrays.asList(commandToRun.split(Pattern.quote("&&"))));
    }
    for (Path path : description.traverse(node)) {
      boolean hit = false;
      if (filterMap == null) {
        hit = true;
      } else {
        Node endNode = path.endNode();
        Map<String, Boolean> matchPerFilterKey = new HashMap<String, Boolean>();
        for (String key : endNode.getPropertyKeys()) {
          for (Map.Entry<String, Object> filterEntry : filterMap.entrySet()) {
            String filterKey = filterEntry.getKey();
            if (matchPerFilterKey.containsKey(filterKey)) {
              continue;
            }

            if (matches(
                newPattern(filterKey, caseInsensitiveFilters),
                key,
                caseInsensitiveFilters,
                looseFilters)) {
              Object value = endNode.getProperty(key);
              String filterPattern =
                  filterEntry.getValue() != null ? filterEntry.getValue().toString() : null;
              if (matches(
                  newPattern(filterPattern, caseInsensitiveFilters),
                  value.toString(),
                  caseInsensitiveFilters,
                  looseFilters)) {
                matchPerFilterKey.put(filterKey, true);
              }
            }
          }
        }

        if (matchPerFilterKey.size() == filterMap.size()) {
          hit = true;
        }
      }
      if (hit) {
        if (commandsToRun.isEmpty()) {
          printPath(path, quiet, session, out);
        } else {
          printAndInterpretTemplateLines(
              commandsToRun,
              false,
              true,
              NodeOrRelationship.wrap(path.endNode()),
              getServer(),
              session,
              out);
        }
      }
    }
    return Continuation.INPUT_COMPLETE;
  }
 @Test
 public void testRelationshipsAreReturnedOnceWhenSufficientRecentlyUniqueBreadthFirst()
     throws Exception {
   testRelationshipsAreReturnedOnceWhenSufficientRecentlyUnique(
       Traversal.description().breadthFirst());
 }
 private PathFinder<Path> newFinder() {
   return new ExactDepthPathFinder(Traversal.expanderForAllTypes(), 4, 4);
 }
 @Test
 public void testAllUniqueNodePathsAreReturnedDepthFirst() throws Exception {
   testAllUniqueNodePathsAreReturned(Traversal.description().depthFirst());
 }
Ejemplo n.º 29
0
  public Double shortestPath() {

    s1nodes = Neo4jGraphUtils.getSourceNodes(graph, s1);
    s2nodes = Neo4jGraphUtils.getSourceNodes(graph, s2);

    log.info(
        "Compute pairwise cheapest path: S{} ({} nodes), S{} ({} nodes)",
        s1.getSnippetId(),
        s1nodes.size(),
        s2.getSnippetId(),
        s2nodes.size());

    int pathLenghtSum = 0;
    int pathCount = 0;
    int commonNodeCount = 0;

    int cacheCounter = 0;

    for (Vertex v1 : s1nodes) {
      for (Vertex v2 : s2nodes) {

        Long v1id = (Long) (v1.getId());
        Long v2id = (Long) (v2.getId());

        if (v1id.compareTo(v2id) == 0) {
          commonNodeCount++;
          // pathCount++;
          // pathLenghtSum += 0
          continue;
        }

        log.debug("Processing Node {} and Node {}", v1id, v2id);

        Node n1 = graph.getRawGraph().getNodeById(v1id);
        Node n2 = graph.getRawGraph().getNodeById(v2id);

        // Get shortest path

        Integer pathLen = null; // p.length();

        Tuple<Integer, Double> path = pathCache.getPath(v1id, v2id, log.isDebugEnabled());

        if (path != null) {

          pathLen = path.k;

          cacheCounter++;

          log.debug(
              "Path between Node{} and Node{} found, Length {}, Weight {} (from mysql cache)",
              v1id,
              v2id,
              pathLen);
        } else {

          log.debug("Start ShortestPath Node {} and Node {}", v1id, v2id);

          PathFinder<Path> shortestPathAlgo =
              GraphAlgoFactory.shortestPath(
                  (PathExpander<?>) Traversal.expanderForAllTypes(), maxPathLen.intValue());

          Path shortestPath = shortestPathAlgo.findSinglePath(n1, n2);
          StringBuffer shorestPathSteps = null;

          if (shortestPath != null) {

            pathLen = shortestPath.length();

            // Constructing path for debug logging
            if (log.isDebugEnabled()) {

              Iterator<PropertyContainer> iter = shortestPath.iterator();
              shorestPathSteps = new StringBuffer();

              while (iter.hasNext()) {
                Object e = iter.next();
                if (e instanceof NodeProxy) {
                  Node n = (Node) e;
                  shorestPathSteps.append("(" + n.getProperty("label") + ")");
                } else if (e instanceof RelationshipProxy) {
                  Relationship r = (Relationship) e;
                  shorestPathSteps.append("-[" + r.getType() + "]-");
                } else {
                  log.error("ERROR");
                }
              }

              log.debug(
                  "Path between Node{} and Node{} found, Length {}, Path {}",
                  v1id,
                  v2id,
                  pathLen,
                  shorestPathSteps);
            }
          } else {
            log.debug("Path between Node{} and Node{} not found.", v1id, v2id);
          }

          // Update mysql db cache
          if (log.isDebugEnabled()) {
            pathCache.setPath(v1id, v2id, pathLen, null, shorestPathSteps);
          } else {
            pathCache.setPath(v1id, v2id, pathLen, null);
          }
        }

        // Getting shortest path data
        if (pathLen != null) {

          if (pathLen <= maxPathLen) {
            pathLenghtSum += pathLen;

            pathCount++;
          }
        }
      }
    }

    log.info(
        "Similarity measures S{}, S{}: CommonNodes {}, PathCnt {}, MaxTheoPathCnt {}, SumPathLen {}, CacheCnt {}",
        s1.getSnippetId(),
        s2.getSnippetId(),
        commonNodeCount,
        pathCount,
        s1nodes.size() * s2nodes.size(),
        pathLenghtSum,
        cacheCounter);

    // log.info("Total path score S{}, S{}: {}", s1.getSnippetId(), s2.getSnippetId(), sim);
    return Double.valueOf(pathLenghtSum);
  }
 @Test
 public void testAllUniqueRelationshipPathsAreReturnedBreadthFirst() throws Exception {
   testAllUniqueRelationshipPathsAreReturned(Traversal.description().breadthFirst());
 }