コード例 #1
0
 // Circular buffer is a linear data structure which follows the FIFO principle
 public static void main(String[] args) {
   CircularFifoBuffer buf = new CircularFifoBuffer(4);
   // We use CircularFifoBuffer from apache commons collection for the implementation
   buf.add("A");
   buf.add("B");
   buf.add("C");
   buf.add("D");
   buf.add("E");
   buf.remove();
   System.out.println("buf = " + buf);
 }
コード例 #2
0
  public static void main(String[] args) {
    if (args.length == 0) {
      System.out.println(
          "SimpleMovingAvgZkConsumer {zookeeper} {group.id} {topic} {window-size} {wait-time}");
      return;
    }

    String next;
    int num;
    SimpleMovingAvgZkConsumer movingAvg = new SimpleMovingAvgZkConsumer();
    String zkUrl = args[0];
    String groupId = args[1];
    String topic = args[2];
    int window = Integer.parseInt(args[3]);
    movingAvg.waitTime = args[4];

    CircularFifoBuffer buffer = new CircularFifoBuffer(window);

    movingAvg.configure(zkUrl, groupId);

    movingAvg.start(topic);

    while ((next = movingAvg.getNextMessage()) != null) {
      int sum = 0;

      try {
        num = Integer.parseInt(next);
        buffer.add(num);
      } catch (NumberFormatException e) {
        // just ignore strings
      }

      for (Object o : buffer) {
        sum += (Integer) o;
      }

      if (buffer.size() > 0) {
        System.out.println("Moving avg is: " + (sum / buffer.size()));
      }

      // uncomment if you wish to commit offsets on every message
      // movingAvg.consumer.commitOffsets();

    }

    movingAvg.consumer.shutdown();
    System.exit(0);
  }
コード例 #3
0
 /**
  * Gets the average buffer value.
  *
  * @param circularFifoBuffer the circular fifo buffer
  * @return returns average value of all buffer entries
  */
 private static float getAverageBufferValue(CircularFifoBuffer circularFifoBuffer) {
   // handle null instance
   if (circularFifoBuffer == null) {
     return 0f;
   }
   // calculate sum of all buffer values
   float bufferSum = 0f;
   @SuppressWarnings("rawtypes")
   Iterator iterator = circularFifoBuffer.iterator();
   while (iterator.hasNext()) {
     bufferSum += (Long) iterator.next();
   }
   // return average value
   float result = bufferSum / circularFifoBuffer.size();
   if (Float.isNaN(result)) {
     result = 0f;
   }
   return result;
 }
コード例 #4
0
 private boolean finalForm() {
   if (!evolvingWord.equals(targetWord)) return false;
   System.out.println(
       "Evolver "
           + Thread.currentThread().getId()
           + " evolved the phrase \""
           + evolvingWord
           + "\" after "
           + generation
           + " generations!");
   System.out.println("Last " + generations.size() + " generations: " + generations);
   return true;
 }
コード例 #5
0
 private void evolve() {
   generation++;
   ArrayList<EvolvedString> strings = new ArrayList<>();
   for (int i = 0; i < 50; i++) strings.add(new EvolvedString(evolvingWord));
   double maxSimilarity = 0;
   String evolvedWord = evolvingWord;
   for (EvolvedString s : strings) {
     double similarity = s.similarity(targetWord);
     if (maxSimilarity < similarity) {
       maxSimilarity = similarity;
       evolvedWord = s.getEvolvedWord();
     }
   }
   evolvingWord = evolvedWord;
   generations.add(evolvedWord);
 }
コード例 #6
0
  @Override
  public String value() {

    final StringBuilder text = new StringBuilder(128);

    if (buffer.isEmpty()) {
      text.append("empty queue");
    } else {
      text.append("last ");
      text.append(size);
      text.append("\n");
      for (final Object item : buffer) {
        text.append(item);
        text.append("\n");
      }
    }

    return text.toString();
  }
コード例 #7
0
 @Override
 public void clear() {
   buffer.clear();
 }
コード例 #8
0
 public void add(final Object item) {
   buffer.add(item);
 }
コード例 #9
0
 private void updateLastModifiedTime() {
   lastModifiedTimesMillis.add(now());
 }
コード例 #10
0
 public int secondsSinceOldestModification() {
   long modifiedTimeMillis = ((Long) lastModifiedTimesMillis.get()).longValue();
   return (int) ((now() - modifiedTimeMillis) / MILLIS_IN_SEC);
 }
コード例 #11
0
 private void initLastModifiedTimesMillis() {
   long nowCached = now();
   for (int i = 0; i < lastModifiedTimesMillis.maxSize(); i++) {
     lastModifiedTimesMillis.add(Long.valueOf(nowCached));
   }
 }
コード例 #12
0
  /**
   * A stream with one sentence per line is read in, sentences containing a query token (or alias
   * thereof) are retained and printed out, formatted.
   *
   * @param args
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {
    if (args.length < 1 || args.length > 2) {
      System.err.println("Format <query_expanded.xml> [extraction_window:int]");
      System.err.println("A stream with one sentece per line is read in, sentences containing");
      System.err.println("a query token are retained and printed out, formatted.");
      System.err.println(
          "The optional extraction_window parameter must be an integer between -1"
              + " and inf. It specifies the number of sentences printed out before and "
              + " after a sentence containing a query. E.g., extraction_window=1 will"
              + " print sentences immediately preceding and following query sentences."
              + " If set to -1, all sentences will be printed. Default is 0.");
      return;
    }
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
    QueryList ql = new QueryList(args[0]);
    int extractionWin = 0;
    if (args.length > 1) {
      try {
        extractionWin = Integer.parseInt(args[1]);
        if (extractionWin < -1) {
          throw new NumberFormatException();
        }
      } catch (NumberFormatException e) {
        System.err.println("Extraction window must be an integer between -1" + " and inf.");
        return;
      }
    }

    // look behind
    CircularFifoBuffer lastSentences = null;
    if (extractionWin > 0) {
      lastSentences = new CircularFifoBuffer(extractionWin);
    }
    // look forward
    int currentExtractionWin = 0;

    int sNr = 1;
    TextIdentifier docQueryId = null;
    for (String line; (line = br.readLine()) != null; ) {
      if (line.startsWith("#")) {
        docQueryId = TextIdentifier.fromDelimited(line.substring(1));
        if (docQueryId.getQueryId() == null || docQueryId.getQueryId().isEmpty()) {
          throw new IllegalArgumentException("TextIdentifier without query id:" + line);
        }
        sNr = 1;
        if (lastSentences != null) {
          lastSentences.clear();
        }
        currentExtractionWin = 0;
      } else {
        assert (docQueryId != null);
        TextIdentifier docQuerySnrId = new TextIdentifier(docQueryId).setSentNr(sNr);
        try {
          if (extractionWin == -1
              || hasQueryMatch(ql.getQueryById(docQuerySnrId.getQueryId()), line)) {
            // old sentences in buffer
            if (lastSentences != null) {
              int oldSNr = sNr - lastSentences.size();
              while (lastSentences.size() > 0) {
                Object lineStrObject = lastSentences.remove();
                writeLine(
                    out,
                    (String) lineStrObject,
                    new TextIdentifier(docQueryId).setSentNr(oldSNr++));
              }
            }
            // current sentence
            writeLine(out, line, new TextIdentifier(docQueryId).setSentNr(sNr));
            currentExtractionWin = extractionWin;
          } else { // no match in sentence
            // within look-forward window of last query-containing sentence
            if (currentExtractionWin > 0) {
              writeLine(out, line, new TextIdentifier(docQueryId).setSentNr(sNr));
              --currentExtractionWin;
            } else { // possibly in look-behind window
              if (lastSentences != null) {
                lastSentences.add(line);
              }
            }
          }
        } catch (IllegalArgumentException e) {
          throw new IllegalArgumentException(
              "Error processing sentence with id:"
                  + " \n"
                  + docQueryId
                  + "\n"
                  + docQuerySnrId
                  + "\n"
                  + e);
        }
        sNr += 1;
      }
    }
    out.flush();
    br.close();
  }