/**
  * Reads events from <tt>eventStream</tt> into a linked list. The predicates associated with each
  * event are counted and any which occur at least <tt>cutoff</tt> times are added to the
  * <tt>predicatesInOut</tt> map along with a unique integer index.
  *
  * @param eventStream an <code>EventStream</code> value
  * @param eventStore a writer to which the events are written to for later processing.
  * @param predicatesInOut a <code>TObjectIntHashMap</code> value
  * @param cutoff an <code>int</code> value
  */
 private int computeEventCounts(
     EventStream eventStream, Writer eventStore, TObjectIntHashMap predicatesInOut, int cutoff)
     throws IOException {
   TObjectIntHashMap counter = new TObjectIntHashMap();
   int predicateIndex = 0;
   int eventCount = 0;
   while (eventStream.hasNext()) {
     Event ev = eventStream.nextEvent();
     eventCount++;
     eventStore.write(FileEventStream.toLine(ev));
     String[] ec = ev.getContext();
     for (int j = 0; j < ec.length; j++) {
       if (!predicatesInOut.containsKey(ec[j])) {
         if (counter.increment(ec[j])) {
         } else {
           counter.put(ec[j], 1);
         }
         if (counter.get(ec[j]) >= cutoff) {
           predicatesInOut.put(ec[j], predicateIndex++);
           counter.remove(ec[j]);
         }
       }
     }
   }
   predicatesInOut.trimToSize();
   eventStore.close();
   return eventCount;
 }
  @Test
  public void countDownTest() {
    EventSource<Void> src1 = new EventSource<Void>();
    EventSource<Void> src2 = new EventSource<Void>();
    EventSource<Void> reset = new EventSource<Void>();

    BiFunction<Integer, Void, Tuple2<Integer, Optional<String>>> countdown =
        (s, i) -> s == 1 ? t(3, Optional.of("COUNTDOWN REACHED")) : t(s - 1, Optional.empty());

    EventStream<String> countdowns =
        StateMachine.init(3)
            .on(src1)
            .transmit(countdown)
            .on(src2)
            .transmit(countdown)
            .on(reset)
            .transition((s, i) -> 3)
            .toEventStream();

    Counter counter = new Counter();
    Subscription sub = countdowns.hook(x -> counter.inc()).pin();

    src1.push(null);
    src2.push(null);
    assertEquals(0, counter.get());

    src1.push(null);
    assertEquals(1, counter.getAndReset());

    src2.push(null);
    src2.push(null);
    reset.push(null);
    assertEquals(0, counter.get());
    src2.push(null);
    assertEquals(0, counter.get());
    src1.push(null);
    assertEquals(0, counter.get());
    src2.push(null);
    assertEquals(1, counter.getAndReset());

    sub.unsubscribe();
    src1.push(null);
    src1.push(null);
    src1.push(null);
    src1.push(null);
    src1.push(null);
    src1.push(null);
    assertEquals(0, counter.get());
  }
 /**
  * Reads events from <tt>eventStream</tt> into a linked list. The predicates associated with each
  * event are counted and any which occur at least <tt>cutoff</tt> times are added to the
  * <tt>predicatesInOut</tt> map along with a unique integer index.
  *
  * @param eventStream an <code>EventStream</code> value
  * @param predicatesInOut a <code>TObjectIntHashMap</code> value
  * @param cutoff an <code>int</code> value
  * @return a <code>TLinkedList</code> value
  */
 private LinkedList<Event> computeEventCounts(
     EventStream eventStream, Map<String, Integer> predicatesInOut, int cutoff) {
   Set predicateSet = new HashSet();
   Map<String, Integer> counter = new HashMap<String, Integer>();
   LinkedList<Event> events = new LinkedList<Event>();
   while (eventStream.hasNext()) {
     Event ev = eventStream.next();
     events.addLast(ev);
     update(ev.getContext(), predicateSet, counter, cutoff);
   }
   predCounts = new int[predicateSet.size()];
   int index = 0;
   for (Iterator pi = predicateSet.iterator(); pi.hasNext(); index++) {
     String predicate = (String) pi.next();
     predCounts[index] = counter.get(predicate);
     predicatesInOut.put(predicate, index);
   }
   return events;
 }
  private List index(int numEvents, EventStream es, TObjectIntHashMap predicateIndex) {
    TObjectIntHashMap omap = new TObjectIntHashMap();
    int outcomeCount = 0;
    List eventsToCompare = new ArrayList(numEvents);
    TIntArrayList indexedContext = new TIntArrayList();
    while (es.hasNext()) {
      Event ev = es.nextEvent();
      String[] econtext = ev.getContext();
      ComparableEvent ce;

      int ocID;
      String oc = ev.getOutcome();

      if (omap.containsKey(oc)) {
        ocID = omap.get(oc);
      } else {
        ocID = outcomeCount++;
        omap.put(oc, ocID);
      }

      for (int i = 0; i < econtext.length; i++) {
        String pred = econtext[i];
        if (predicateIndex.containsKey(pred)) {
          indexedContext.add(predicateIndex.get(pred));
        }
      }

      // drop events with no active features
      if (indexedContext.size() > 0) {
        ce = new ComparableEvent(ocID, indexedContext.toNativeArray());
        eventsToCompare.add(ce);
      } else {
        System.err.println(
            "Dropped event " + ev.getOutcome() + ":" + Arrays.asList(ev.getContext()));
      }
      // recycle the TIntArrayList
      indexedContext.resetQuick();
    }
    outcomeLabels = toIndexedStringArray(omap);
    predLabels = toIndexedStringArray(predicateIndex);
    return eventsToCompare;
  }
Exemple #5
0
  @Test
  public void testZipObservableOfObservables() {
    EventStream.getEventStream("HTTP-ClusterB", 20)
        .groupBy(e -> e.instanceId)
        // now we have streams of cluster+instanceId
        .flatMap(
            ge -> {
              return ge.scan(
                  new HashMap<String, String>(),
                  (accum, perInstanceEvent) -> {
                    accum.put("instance", ge.key());
                    return accum;
                  });
            })
        .take(10)
        .toBlocking()
        .forEach(System.out::println);

    System.out.println("**** finished");
  }