Ejemplo n.º 1
0
    @Override
    public void run() {
      final Node head = this.graph.getNodeAt(this.start);
      if (head == null) {
        throw new IllegalStateException("Start point not in graph");
      }
      final Node tail = this.graph.getNodeAt(this.end);
      if (tail == null) {
        throw new IllegalStateException("End point not in graph");
      }

      // Traverse through the graph, finding the shortest distance between
      // any two nodes starting from 'head' until reaching 'tail'.
      Node current = head;
      HashSet<Node> workingSet = new HashSet<>();
      do {
        current = this.exploreNode(workingSet, current);
      } while (!current.equals(tail));

      // Reverse through the graph, finding the closest node (using
      // shortest distances from above traversal) starting from 'tail'
      // until reaching 'head'.
      current = tail;
      while (!current.equals(head)) {
        this.resultantPath.add(0, current);
        current = this.closestNode(this.graph.getConnectedNodes(current));
      }
      this.resultantPath.add(0, current);
    }
Ejemplo n.º 2
0
  /**
   * Helper method to turn a Collection of "Paths" into a Collection of Routes
   *
   * @param source
   * @param weights
   * @param prevNodes
   * @return
   */
  private Map<Node, Route> makeRouteMap(
      Node source, Map<Node, Integer> weights, Map<Node, Node> prevNodes) {

    Map<Node, Route> routes = new HashMap<Node, Route>();

    for (Map.Entry<Node, Integer> entry : weights.entrySet()) {
      Integer hops = 1;
      Node node = entry.getKey();
      if (node.equals(source)) {
        continue;
      }
      Route route = new Route();
      route.setDistance(entry.getValue());

      Node nextHop = node;
      route.getPath().add(0, nextHop);
      while ((nextHop = prevNodes.get(nextHop)) != null) {
        if (nextHop.equals(source)) {
          continue;
        }
        hops++;
        route.getPath().add(0, nextHop);
      }
      route.setHops(hops);
      routes.put(node, route);
    }

    return routes;
  }
 @Override
 public boolean equals(Object obj) {
   if (obj == null) return false;
   else if (obj instanceof DirectedNeighbor) {
     return _neighbor.equals(((DirectedNeighbor) obj).getNeighbor());
   } else if (obj instanceof Node) {
     return _neighbor.equals((Node) obj);
   } else return false;
 }
Ejemplo n.º 4
0
 /**
  * Remove the element in position
  *
  * @param i
  */
 public void remove(int position) {
   Node<T> node = getNode(position);
   if (first.equals(node)) {
     removeFirst();
   } else if (last.equals(node)) {
     removeLast();
   } else {
     node.getBefore().setAfter(node.getAfter());
     node.getAfter().setBefore(node.getBefore());
     // node is free for gc
   }
 }
 ReificationStatementMask(Triple t) {
   mask = HasNada;
   Node p = t.getPredicate();
   if (p != null) {
     if (p.equals(RDF.Nodes.subject)) mask = HasSubj;
     else if (p.equals(RDF.Nodes.predicate)) mask = HasPred;
     else if (p.equals(RDF.Nodes.object)) mask = HasObj;
     else if (p.equals(RDF.Nodes.type)) {
       Node o = t.getObject();
       if (o.equals(RDF.Nodes.Statement)) mask = HasType;
     }
   }
 }
Ejemplo n.º 6
0
 public boolean equals(Object obj) {
   if (!(obj instanceof Control)) {
     return false;
   }
   Control c = ((Control) obj);
   if (from == null) {
     return c.from == null && to.equals(c.to);
   }
   if (to == null) {
     return c.to == null && from.equals(c.from);
   }
   return from.equals(c.from) && to.equals(c.to);
 }
Ejemplo n.º 7
0
 /** Remove the current element. */
 protected void remove(Node<T> t) {
   if (t.equals(first) && t.equals(last)) {
     first = null;
     last = null;
   } else if (t.equals(first)) {
     first = t.getAfter();
     first.setBefore(null);
   } else if (t.equals(last)) {
     last = t.getBefore();
     last.setAfter(null);
   } else {
     t.getBefore().setAfter(t.getAfter());
     t.getAfter().setBefore(t.getBefore());
   }
 }
Ejemplo n.º 8
0
  /* initialize nodes and links. the graph is complete ! :) */
  public void initCircleGraph(Integer xMax, Integer yMax, Integer nbNode) {

    int xCenter = xMax / 2;
    int yCenter = yMax / 2;
    int rayon = Math.max(xMax, yMax) / 3;

    double angle = Math.PI * 2 / nbNode;

    /* init nodes around the circle */
    for (Integer i = 0; i < nbNode; i++) {
      double x = xCenter + rayon * Math.cos(angle * i);
      double y = yCenter + rayon * Math.sin(angle * i);
      Node n = new Node((int) x, (int) y);
      nodes.add(n);
    }

    for (Node source : nodes) {
      for (Node dest : nodes) {
        if (!source.equals(dest)) {
          Link link1 = new Link(source, dest);
          source.getLinks().add(link1);
          Link link2 = new Link(dest, source);
          dest.getLinks().add(link2);
          links.add(link1);
          links.add(link2);
        }
      }
    }

    System.out.println("nb link générés = " + links.size());
  }
Ejemplo n.º 9
0
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.CYAN);
    // draw basic board
    for (int i = 0, j = 0; j < 17; i += BLANK_WIDTH, j++) {
      g.drawLine(0, i, 640, i);
      g.drawLine(i, 0, i, 640);
    }

    // draw nodes
    ArrayList<Node> nodeList = NodeList.getList();
    for (Node point : nodeList) {
      int x = (point.getX() - 1) * BLANK_WIDTH;
      int y = (point.getY() - 1) * BLANK_WIDTH;

      // draw black
      if (point.getColor() == Node.BLACK) {
        g.setColor(new Color(255, 192, 203));
      }
      // draw white
      else if (point.getColor() == Node.WHITE) {
        g.setColor(Color.WHITE);
      }
      g.fillOval(x - RADIUS, y - RADIUS, RADIUS * 2, RADIUS * 2);
      // newly point
      if (point.equals(NodeList.getLastNode())) {
        g.setColor(Color.RED);
        g.drawRect(x - RADIUS, y - RADIUS, RADIUS * 2, RADIUS * 2);
      }
    }
  }
    /** {@inheritDoc} */
    @Override
    public boolean remove(T value) {
      // Find the node
      Node<T> node = head;
      while (node != null && (!node.value.equals(value))) {
        node = node.nextNode;
      }
      if (node == null) return false;

      // Update the tail, if needed
      if (node.equals(tail)) tail = node.previousNode;

      Node<T> prev = node.previousNode;
      Node<T> next = node.nextNode;
      if (prev != null && next != null) {
        prev.nextNode = next;
        next.previousNode = prev;
      } else if (prev != null && next == null) {
        prev.nextNode = null;
      } else if (prev == null && next != null) {
        // Node is the head
        next.previousNode = null;
        head = next;
      } else {
        // prev==null && next==null
        head = null;
      }
      size--;
      return true;
    }
Ejemplo n.º 11
0
  private void remClient(Client client) {

    Node item = this.clients;
    while (item != null) {
      if (item.client.equals(client)) {
        // Eliminar client i Node
        // Si es el node cap, apuntar al seguent
        if (item.equals(this.clients)) {

          this.clients = item.getSeguent();
          item = null;

          return;
        }
        // Si no es el node cap, trobar anterior i apuntar al seguent
        else {
          Node anterior = this.clients;
          while (anterior.getSeguent() != null) {
            if (anterior.getSeguent().client.equals(client)) {

              anterior.setSeguent(item.getSeguent());
              item = null;

              return;
            }
          }
        }
      }
      item = item.getSeguent();
    }
  }
Ejemplo n.º 12
0
 @Override
 public void verify() throws TransformException {
   ItemList targets = mod.nearest(NodeTarget.class).targets();
   List<Node> references = mod.references();
   ItemList records = mod.supplementQuery().all("sort-proxy");
   assert targets.size() == references.size();
   assert records.size() == references.size();
   for (int i = 0; i < targets.size(); i++) {
     Node target = targets.get(i).node();
     Node restoredProxy = references.get(i);
     ItemList items = query.runOn(mod.scope(target.query()));
     if (items.size() != 1)
       throw new TransformException(
           "sort by corresponding node must select one node per target, but instead selected "
               + items.size()
               + ": "
               + items);
     Node proxy = items.get(0).node();
     if (!restoredProxy.equals(proxy)) throw new TransformException("computed proxy changed");
     if (!proxy
         .query()
         .single(
             "(count(preceding::*) + count(ancestor::*)) eq xs:integer($_1/@position)",
             records.get(i))
         .booleanValue()) throw new SortingException("computed proxy moved");
   }
 }
Ejemplo n.º 13
0
  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null || !(obj instanceof NumericalNode)) {
      return false;
    }

    NumericalNode node = (NumericalNode) obj;

    return attr == node.attr
        && split == node.split
        && loChild.equals(node.loChild)
        && hiChild.equals(node.hiChild);
  }
Ejemplo n.º 14
0
  /**
   * This method is invoked when the builtin is called in a rule body.
   *
   * @param args the array of argument values for the builtin, this is an array of Nodes, some of
   *     which may be Node_RuleVariables.
   * @param length the length of the argument list, may be less than the length of the args array
   *     for some rule engines
   * @param context an execution context giving access to other relevant data
   * @return return true if the buildin predicate is deemed to have succeeded in the current
   *     environment
   */
  public boolean bodyCall(Node[] args, int length, RuleContext context) {
    if (length != 3) {
      throw new BuiltinException(
          this, context, "builtin " + getName() + " requires 3 arguments but saw " + length);
    }
    Node obj = getArg(2, args, context);
    Node subj = getArg(0, args, context);
    // Allow variables in subject position to correspond to a wild card
    if (subj.isVariable()) {
      subj = null;
    }

    // Allow variables in the predicate position to correspond to a wild card
    Node pred = getArg(1, args, context);
    if (pred.isVariable()) {
      pred = null;
    }
    boolean bContainsObj = context.contains(subj, pred, obj);
    if (!bContainsObj) {
      return false;
    }
    // does it contain anything else?
    ClosableIterator<Triple> citr = context.find((Node) null, pred, obj);
    boolean otherSubject = false;
    while (citr.hasNext()) {
      Object o = citr.next();
      Node tmpSubj = ((Triple) o).getSubject();
      if (!tmpSubj.equals(subj)) {
        otherSubject = true;
        break;
      }
    }
    citr.close();
    return !otherSubject;
  }
Ejemplo n.º 15
0
  /**
   * Returns the node after the given one
   *
   * @param x node
   * @return next node
   * @throws HsqlException
   */
  Node next(Node x) throws HsqlException {

    if (x == null) {
      return null;
    }

    Node r = x.getRight();

    if (r != null) {
      x = r;

      Node l = x.getLeft();

      while (l != null) {
        x = l;
        l = x.getLeft();
      }

      return x;
    }

    Node ch = x;

    x = x.getParent();

    while (x != null && ch.equals(x.getRight())) {
      ch = x;
      x = x.getParent();
    }

    return x;
  }
Ejemplo n.º 16
0
 public Node getOpen(int x, int y) {
   for (Node node : OpenList) {
     if (node.equals(x, y)) {
       return node;
     }
   }
   return null;
 }
Ejemplo n.º 17
0
 /**
  * Tests whether a graph node exists in the graph
  *
  * @param node graph node
  * @return true if node exists
  */
 public boolean nodeExists(Node node) {
   for (Node aNode : nodeList) {
     if (aNode.equals(node)) {
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 18
0
 public boolean containClose(int x, int y) {
   for (Node node : CloseList) {
     if (node.equals(x, y)) {
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 19
0
 public boolean containOpen(int x, int y) {
   for (Node node : OpenList) {
     if (node.equals(x, y)) {
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 20
0
 public Node[] jjtRemoveObject(Node node) throws VisitorException {
   for (int i = 0; i < jjtGetNumChildren(); i++) {
     if (node.equals(children[i])) {
       return jjtRemoveChild(i);
     }
   }
   return children;
 }
Ejemplo n.º 21
0
 /**
  * @param map 地图 数据
  * @param sx 开始 x 坐标
  * @param sy 开始 y 坐标
  * @param ex 结束 x 坐标
  * @param ey 结束 y 坐标
  * @param isPass 可通过的 数据集
  * @return 路线
  */
 public ArrayList<Node> find(int[][] map, int sx, int sy, int ex, int ey, int[] isPass) {
   Node start = new Node(sx, sy);
   Node end = new Node(ex, ey);
   Node node = start;
   CloseList.add(node);
   boolean flag = true;
   while (flag) {
     for (int i = 0; i < 4; i++) {
       int fx = node.x + XMove[i];
       int fy = node.y + YMove[i];
       if (fx >= map.length && fy >= map[0].length) {
         continue;
       }
       if (end.equals(fx, fy)) {
         end.parent = node;
         flag = false;
         break;
       }
       if (containClose(fx, fy)) {
         continue;
       }
       if (containOpen(fx, fy)) {
         Node node3 = getOpen(fx, fy);
         if (node.G + 10 < node3.G) {
           node3.parent = node;
           node3.G = node.G + 10;
           node3.F = node3.G + node3.H;
         }
         continue;
       }
       if (Arrays.binarySearch(isPass, map[fy][fx]) >= 0) {
         Node node2 = new Node(fx, fy);
         node2.parent = node;
         node2.G = node.G + 10;
         // 采用manhattan启发算法  两点中的直角 距离
         node2.H = Math.abs((ex - fx + ey - fy) * 10);
         node2.F = node2.G + node2.H;
         OpenList.add(node2);
       }
     }
     if (flag == false) {
       break;
     }
     if (OpenList.size() == 0) {
       return null;
     }
     node = MinF(OpenList);
     OpenList.remove(node);
     CloseList.add(node);
   }
   ArrayList<Node> Path = new ArrayList<Node>();
   node = end;
   while (node.parent != null) {
     Path.add(node);
     node = node.parent;
   }
   return Path;
 }
Ejemplo n.º 22
0
    void updateForDelete(Node node) {

      try {
        if (node.equals(nextnode)) {
          nextnode = index.next(node);
        }
      } catch (Exception e) {
      }
    }
Ejemplo n.º 23
0
  /**
   * Simple A*. Computes shortest paths to all reachable nodes from the source node given. This
   * method will cache the result so that if it is called later on the same node and nothing has
   * been changed (no nodes added or removed), it will not compute the result again, but will return
   * the cached value.
   *
   * @param source
   * @return
   */
  public Map<Node, Route> computeShortestRoutes(Node source) {

    // Don't perform calculation if it's been done before and nothings changed
    if (!dirty && (shortestRoutesCache.get(source) != null)) {
      return shortestRoutesCache.get(source);
    }

    List<Node> nodes = new LinkedList<Node>();
    Map<Node, Integer> weights = new HashMap<Node, Integer>();
    Map<Node, Node> prevNodes = new HashMap<Node, Node>();

    weights.put(source, 0);

    // Initialize all nodes to have effectively infinite distance
    for (Node node : allNodes) {
      if (!node.equals(source)) {
        weights.put(node, Integer.MAX_VALUE);
        prevNodes.put(node, null);
      }
      nodes.add(node);
    }

    // A*
    while (!nodes.isEmpty()) {
      Node node = findMinimum(nodes, weights);
      if (weights.get(node).equals(Integer.MAX_VALUE)) {
        continue;
      }
      for (Node neighbor : node.getNeighbors()) {
        Integer alternateWeight = weights.get(node) + node.getEdgeForNeighbor(neighbor).getWeight();
        if (alternateWeight < weights.get(neighbor)) {
          weights.put(neighbor, alternateWeight);
          prevNodes.put(neighbor, node);
        }
      }
    }

    List<Node> unreachableNodes = new ArrayList<Node>();

    for (Map.Entry<Node, Integer> entry : weights.entrySet()) {
      if (entry.getValue().equals(Integer.MAX_VALUE)) {
        unreachableNodes.add(entry.getKey());
      }
    }

    for (Node unreachableNode : unreachableNodes) {
      weights.remove(unreachableNode);
    }
    Map<Node, Route> routeMap = makeRouteMap(source, weights, prevNodes);

    // Cache result
    shortestRoutesCache.put(source, routeMap);

    dirty = Boolean.FALSE;

    return routeMap;
  }
Ejemplo n.º 24
0
 /** Add After */
 protected void addAfter(Node<T> current, T element) {
   if (current.equals(last)) {
     addLast(element);
   } else {
     Node<T> node = new Node<T>(element, current, current.getAfter());
     current.getAfter().setBefore(node);
     current.setAfter(node);
   }
 }
Ejemplo n.º 25
0
 /**
  * determines if the provided Node is the parent, or parent's parent, etc. of this Spatial.
  *
  * @param ancestor the ancestor object to look for.
  * @return true if the ancestor is found, false otherwise.
  */
 public boolean hasAncestor(Node ancestor) {
   if (parent == null) {
     return false;
   } else if (parent.equals(ancestor)) {
     return true;
   } else {
     return parent.hasAncestor(ancestor);
   }
 }
Ejemplo n.º 26
0
  /**
   * Replies true if this way contains the node <code>node</code>, false otherwise. Replies false if
   * <code>node</code> is null.
   *
   * @param node the node. May be null.
   * @return true if this way contains the node <code>node</code>, false otherwise
   * @since 1911
   */
  public boolean containsNode(Node node) {
    if (node == null) return false;

    Node[] nodes = this.nodes;
    for (Node n : nodes) {
      if (n.equals(node)) return true;
    }
    return false;
  }
Ejemplo n.º 27
0
  /* initialize nodes and links. the graph is complete ! :) */
  public void initRandomGraph(Integer xMax, Integer yMax, Integer nbNode) {
    Random rand = new Random();

    /* init nodes at random positions */
    for (Integer i = 0; i < nbNode; i++) {
      Integer x = rand.nextInt(xMax);
      Integer y = rand.nextInt(yMax);
      Node n = new Node(x, y);
      nodes.add(n);
    }

    /* remove nodes that unfortunately spawned at the exact same position */
    List<Node> doublons = new ArrayList<Node>();
    for (Node source : nodes) {
      for (Node dest : nodes) {
        if (!source.equals(dest)
            && source.getX().equals(dest.getX())
            && source.getY().equals(dest.getY())) {
          doublons.add(source);
        }
      }
    }
    if (!doublons.isEmpty()) {
      nodes.removeAll(doublons);
      System.out.println(
          "Lors de la génération aléatoire du graphes, "
              + doublons.size()
              + " doublons ont étés supprimés");
    }

    /* init links for a complete graph */
    for (Node source : nodes) {
      for (Node dest : nodes) {
        if (!source.equals(dest)) {
          Link link1 = new Link(source, dest);
          source.getLinks().add(link1);
          Link link2 = new Link(dest, source);
          dest.getLinks().add(link2);
          links.add(link1);
          links.add(link2);
        }
      }
    }
  }
Ejemplo n.º 28
0
 /**
  * Tests equality over another {@code NodeRef} based on {@link #getParentPath() parent path},
  * {@link #getNode() node} name and id, and {@link #getMetadataId()}
  */
 @Override
 public boolean equals(Object o) {
   if (!(o instanceof NodeRef)) {
     return false;
   }
   NodeRef r = (NodeRef) o;
   return parentPath.equals(r.parentPath)
       && node.equals(r.node)
       && getMetadataId().equals(r.getMetadataId());
 }
 // Override 'equals' to be DOM-based...
 public boolean equals(Object o) {
   if (o == null) {
     return false;
   }
   if (!(o instanceof DOMBased)) {
     return super.equals(o);
   }
   DOMBased db = (DOMBased) o;
   Node dbNode = db.getDOMNode();
   return dbNode.equals(getDOMNode());
 }
Ejemplo n.º 30
0
  /**
   * Replace two nodes
   *
   * @param x node
   * @param n node
   * @throws HsqlException
   */
  private void replace(Session session, Node x, Node n) throws HsqlException {

    if (x.equals(getRoot(session))) {
      setRoot(session, n);

      if (n != null) {
        n.setParent(null);
      }
    } else {
      set(x.getParent(), x.isFromLeft(), n);
    }
  }