Пример #1
0
    private PathFinder<? extends Path> getAlgorithm(
        String algorithm, RelationshipExpander expander, int maxDepth) {
      if (algorithm.equals("shortestPath")) {
        return GraphAlgoFactory.shortestPath(expander, maxDepth);
      } else if (algorithm.equals("allSimplePaths")) {
        return GraphAlgoFactory.allSimplePaths(expander, maxDepth);
      } else if (algorithm.equals("allPaths")) {
        return GraphAlgoFactory.allPaths(expander, maxDepth);
      } else if (algorithm.equals("dijkstra")) {
        String costProperty = (String) map.get("cost_property");
        Number defaultCost = (Number) map.get("default_cost");
        CostEvaluator<Double> costEvaluator =
            defaultCost == null
                ? CommonEvaluators.doubleCostEvaluator(costProperty)
                : CommonEvaluators.doubleCostEvaluator(costProperty, defaultCost.doubleValue());
        representationCreator = WEIGHTED_PATH_REPRESENTATION_CREATOR;
        return GraphAlgoFactory.dijkstra(expander, costEvaluator);
      }

      throw new RuntimeException("Failed to find matching algorithm");
    }
  public List<?> getBaconPath(final Actor actor) {
    if (actor == null) throw new IllegalArgumentException("Null actor");

    Actor bacon = actorRepository.findByPropertyValue("name", "Bacon, Kevin");

    Path path =
        GraphAlgoFactory.shortestPath(
                (PathExpander) StandardExpander.DEFAULT.add(RelTypes.ACTS_IN), 10)
            .findSinglePath(bacon.getPersistentState(), actor.getPersistentState());
    if (path == null) return Collections.emptyList();
    return convertNodesToActorsAndMovies(path);
  }
 private static Iterator<Node> findPath(Node s, Node e) {
   Iterator<Node> it;
   try (Transaction tx = graphDb.beginTx()) {
     PathFinder<Path> finder =
         GraphAlgoFactory.shortestPath(
             PathExpanders.forTypeAndDirection(RelTypes.STD, Direction.OUTGOING), 15);
     Iterable<Path> paths = finder.findAllPaths(s, e);
     Path path = paths.iterator().next();
     it = path.nodes().iterator();
     if (it.hasNext()) {
       System.out.println(it.next().getProperty("name"));
     }
     if (it.hasNext()) {
       System.out.println(it.next().getProperty("name"));
     }
     if (it.hasNext()) {
       System.out.println(it.next().getProperty("name"));
     }
     tx.success();
   }
   return it;
 }
Пример #4
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);
  }
 private void initFinder() {
   this.finder = GraphAlgoFactory.allSimplePaths(Traversal.expanderForAllTypes(), 7);
 }