Example #1
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));
 }
  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 PathFinder<Path> newFinder() {
   return new ExactDepthPathFinder(Traversal.expanderForAllTypes(), 4, 4);
 }
 private void initFinder() {
   this.finder = GraphAlgoFactory.allSimplePaths(Traversal.expanderForAllTypes(), 7);
 }