@Override
  public final boolean incrementToken() throws IOException {
    clearAttributes();

    // Fill the queue on first call
    if (!readQueueResetted) {
      readQueueResetted = true;
      readQueue.clear();
      for (PositionedTokenStream pts : positionedTokenStreams) {
        if (pts == null) {
          continue;
        }
        // Read first token
        pts.clearAttributes();
        if (pts.incrementToken()) {
          // PositionedTokenStream.incrementToken() initialized internal
          // variables to perform proper ordering.
          // Therefore we can only add it to the queue now!
          readQueue.add(pts);
        } // no token left (no token at all)
      }
    }

    // Read from the first token
    PositionedTokenStream toRead = readQueue.peek();
    if (toRead == null) {
      return false; // end of streams
    }
    // Look position to see if it will be increased, see usage a bit below
    int pos = toRead.getPosition();

    // Copy the current token attributes from the sub-TokenStream to our AttributeSource
    restoreState(toRead.captureState());
    // Override the PositionIncrementAttribute
    this.getAttribute(PositionIncrementAttribute.class)
        .setPositionIncrement(Math.max(0, pos - lastPosition));

    // Prepare next read
    // We did not remove the TokenStream from the queue yet,
    // because if we have another token available at the same position,
    // we can save a queue movement.
    toRead.clearAttributes();
    if (!toRead.incrementToken()) {
      // No more token to read, remove from the queue
      readQueue.poll();
    } else {
      // Check if token position changed
      if (readQueue.size() > 1) {
        // If yes, re-enter in the priority queue
        readQueue.add(readQueue.poll());
      } // Otherwise, next call will continue with the same TokenStream (less queue movements)
    }

    lastPosition = pos;

    return true;
  }
예제 #2
0
 @Override
 public void run() {
   Message message;
   List<Agent> agents = equipe.getAgents();
   while (threadRunning) {
     message = messages.poll();
     if (message != null) {
       for (Agent agent : agents) {
         if (!agent.equals(message.emmetteur) && agent.estEnVie()) {
           agent.recoitMessage(message.contenu);
         }
       }
     }
   }
 }
  /**
   * Method createDensityTable TODO: document me!!
   *
   * @param vertexTable
   * @param sizePoints
   * @return
   * @throws SQLException
   */
  private ArrayList<DensityEntry> createDensityTable(String vertexTable, int[] sizePoints)
      throws SQLException {
    ArrayList<DensityEntry> result = new ArrayList<DensityEntry>();
    int vertexSize = DBUtility.getTotalVertexSize(connection, vertexTable, true);
    Vertex[] V = new Vertex[vertexSize];

    int maxSize = sizePoints[sizePoints.length - 1];

    DBResult dbResult = DBUtility.getAllVerticesSortedByX(connection, vertexTable);
    ResultSet rSet = dbResult.getResultSet();
    int i = 0;
    while (rSet.next()) { // filling the array with values
      V[i++] = new Vertex(rSet.getInt(1), rSet.getDouble(2), rSet.getDouble(3));
    }

    double[] distances = new double[maxSize];

    // defining comparator
    Comparator<Double> descendingCmp =
        new Comparator<Double>() {
          @Override
          public int compare(Double o1, Double o2) {
            if (o1 < o2) return 1;
            if (o1 > o2) return -1;
            return 0;
          }
        };

    AbstractQueue<Double> heap = new PriorityQueue<Double>(maxSize, descendingCmp);
    for (i = 0; i < vertexSize; i++) {
      System.out.println("Iteration: " + i + "/" + vertexSize);
      double k = Double.POSITIVE_INFINITY;
      int size = 0;

      int l = i, r = i, idx = 0;
      while (V[r].x - V[l].x <= 2 * k && idx < vertexSize - 1) {
        if (l == 0) { // left out of range condition
          idx = ++r;
        } else if (r == vertexSize - 1) { // right out of range condition
          idx = --l;
        } else { // choose between left and right element that one with smallest distance to its
                 // neighbor
          idx = V[i].x - V[l - 1].x < V[r + 1].x - V[i].x ? --l : ++r;
        }
        if (size < maxSize) { // as long we do not reach max size we simply add in heap and sort
          heap.add(eDist(V[i], V[idx]));
          size++;
        } else {
          double d = eDist(V[i], V[idx]);
          if (heap.peek() > d) {
            heap.poll();
            heap.add(d);
            k = heap.peek();
          }
        }
      }

      // convert heap to a sorted array
      int j = distances.length - 1;

      while (!heap.isEmpty()) {
        distances[j--] = heap.poll();
      }

      // reading for each size the corresponding euclidean distance
      for (j = 0; j < sizePoints.length; j++) {
        int p = sizePoints[j];
        result.add(new DensityEntry(V[i].id, p, distances[p - 1]));
      }
    }
    return result;
  }