/**
   * Follow all edges related to a group of nodes by a given distance.
   *
   * @param nodes The nodes to start from.
   * @param existingNodes
   * @param distance The distance to travel.
   * @param relatedEdgeModule The related edge module.
   */
  public void findLinks(
      Collection<DAGNode> nodes,
      Collection<DAGNode> existingNodes,
      int distance,
      RelatedEdgeModule relatedEdgeModule) {
    if (distance == 0) return;

    // Move through the distance in a breadth-first fashion (level-by-level)
    Collection<DAGNode> completed = new HashSet<>(existingNodes);
    completed.addAll(nodes);
    Collection<DAGNode> currentLevel = nodes;
    for (int d = distance; d > 0; d--) {
      Collection<DAGNode> nextLevel = new HashSet<>();
      // For every node on this level
      for (DAGNode n : currentLevel) {
        // Find linked edges.
        Collection<Edge> relatedEdges = relatedEdgeModule.execute(n, -1, n);
        for (Edge e : relatedEdges) {
          // Grab every non-predicate argument
          Node[] args = e.getNodes();
          for (int i = 1; i < args.length; i++) {
            // Add to next level if not already looked at
            if (args[i] instanceof DAGNode && !completed.contains(args[i]))
              nextLevel.add((DAGNode) args[i]);
          }
        }
      }
      currentLevel = nextLevel;
      // Add all the next level nodes
      nodes.addAll(nextLevel);
    }
  }
Example #2
0
 private int getAllPaths(Graph g, Vertex s, Vertex d) {
   if (s.equals(d)) return 0;
   if (g.isDirectEdge(s, d)) return 1;
   int count = 0;
   for (Edge e : g.getAllEdgesForVertex(s)) {
     count += getAllPaths(g, e.getTo(), d);
   }
   return count;
 }
  /**
   * Finds all edges which involve the nodes as arguments. There cannot be any edges that introduce
   * new DAGNodes (though non-DAG nodes are fine). With the exception of introducing predicates that
   * define the edge.
   *
   * @param linkedNodes The nodes that have already been linked up together
   * @param toBeLinkedNodes The nodes to be added and linked up.
   * @param relatedEdgeModule The Related Edge Module access.
   * @return The collection of edges linking these nodes
   */
  public Collection<DAGEdge> incorporateNewAndLinkEdges(
      Collection<DAGNode> linkedNodes,
      Collection<DAGNode> toBeLinkedNodes,
      RelatedEdgeModule relatedEdgeModule) {
    Collection<DAGEdge> linkedEdges = new HashSet<>();
    boolean addPredicate = true;

    // Incorporate the new nodes into the linked nodes
    linkedNodes.addAll(toBeLinkedNodes);

    // Link up the newly incorporated nodes with edges.
    Collection<DAGNode> predicates = new HashSet<>();
    for (DAGNode newNode : toBeLinkedNodes) {
      Collection<Edge> relatedEdges = relatedEdgeModule.execute(newNode, -1, newNode);
      // Check every edge
      for (Edge e : relatedEdges) {
        if (linkedEdges.contains(e)) continue;
        Node[] args = e.getNodes();
        boolean addEdge = true;
        for (int i = 1; i < args.length; i++) {
          if (args[i] instanceof DAGNode && !linkedNodes.contains(args[i])) {
            addEdge = false;
            break;
          }
        }

        if (addEdge) {
          if (addPredicate || linkedNodes.contains(args[0])) {
            // Add the predicate
            if (!linkedNodes.contains(args[0])) predicates.add((DAGNode) args[0]);

            linkedEdges.add((DAGEdge) e);
          }
        }
      }
    }

    // Store predicates as to-be-linked
    toBeLinkedNodes.clear();
    toBeLinkedNodes.addAll(predicates);
    return linkedEdges;
  }