@Test
 public void testSingle() {
   PathFinder<Path> finder = newFinder();
   Path path = finder.findSinglePath(graph.getNode("SOURCE"), graph.getNode("TARGET"));
   assertNotNull(path);
   assertPathDef(path, "SOURCE", "z", "9", "0", "TARGET");
 }
Example #2
0
  @SuppressWarnings("rawtypes")
  public PathRepresentation findSinglePath(long startId, long endId, Map<String, Object> map) {
    FindParams findParams = new FindParams(startId, endId, map).invoke();
    PathFinder finder = findParams.getFinder();
    Node startNode = findParams.getStartNode();
    Node endNode = findParams.getEndNode();

    Path path = finder.findSinglePath(startNode, endNode);
    if (path == null) {
      throw new NotFoundException();
    }
    return findParams.pathRepresentationOf(path);
  }
  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);
  }