public TimeSeries getSeriesByNameNonlocking(String name) {
   for (int i = 0; i < mSeries.size(); i++) {
     TimeSeries ts = mSeries.get(i);
     if (ts != null && ts.getDbRow().getCategoryName().equals(name)) return ts;
   }
   return null;
 }
  private void updateTimeSeriesMeta(CategoryDbTable.Row row, boolean disable) {
    TimeSeries ts = getSeriesByIdNonlocking(row.getId());

    if (ts == null) {
      if (mDefaultPainter == null) {
        TimeSeriesPainter p = new TimeSeriesPainter.Default();
        ts = new TimeSeries(row, mHistory, mSmoothing, p);
      } else {
        ts = new TimeSeries(row, mHistory, mSmoothing, mDefaultPainter);
      }
      mSeries.add(ts);
      mDatapointCache.addCacheableCategory(row.getId(), mHistory);
    }

    ts.setDbRow(row);
    setSeriesInterpolator(ts, row.getInterpolation());

    if (row.getSynthetic() == true) {
      Formula formula = mFormulaCache.getFormula(Long.valueOf(row.getId()));
      if (formula == null) formula = new Formula();
      formula.setFormula(row.getFormula());
      mFormulaCache.setFormula(row.getId(), formula);
    }

    if (disable) ts.setEnabled(false);

    setDependents(ts);
    setDependees(ts);
  }
 private TimeSeries getSeriesByIdNonlocking(long catId) {
   for (int i = 0; i < mSeries.size(); i++) {
     TimeSeries ts = mSeries.get(i);
     if (ts != null && ts.getDbRow().getId() == catId) return ts;
   }
   return null;
 }
 public void setSeriesEnabled(long catId, boolean b) {
   waitForLock();
   TimeSeries ts = getSeriesByIdNonlocking(catId);
   if (ts != null) ts.setEnabled(b);
   unlock();
   return;
 }
  public synchronized void gatherLatestDatapointsLocking(long catId, int history) {
    waitForLock();
    mDatapointCache.populateLatest(catId, history);
    TimeSeries ts = getSeriesByIdNonlocking(catId);
    if (ts == null) {
      unlock();
      return;
    }

    if (ts.getDbRow().getSynthetic() == false) {
      ts.clearSeries();

      EntryDbTable.Row entry = mDbh.fetchLastCategoryEntry(catId);
      if (entry != null) {
        ArrayList<Datapoint> l = mDatapointCache.getLast(catId, history);
        if (l == null
            || l.size() < 1
            || entry.getTimestamp() > l.get(0).mMillis
            || entry.getValue() != l.get(0).mValue.y) {
          mDatapointCache.clearCache(catId);
          mDatapointCache.populateLatest(catId, history);
          l = mDatapointCache.getLast(catId, history);
        }

        l = aggregateDatapoints(l, ts.getDbRow().getType());
        ts.setDatapoints(null, l, null, true);
      }
    }

    unlock();
  }
 public ArrayList<TimeSeries> getAllEnabledSeries() {
   ArrayList<TimeSeries> list = new ArrayList<TimeSeries>();
   for (int i = 0; i < mSeries.size(); i++) {
     TimeSeries ts = mSeries.get(i);
     if (ts != null && ts.isEnabled()) list.add(ts);
   }
   return list;
 }
 public long getSeriesIdLocking(int i) {
   long id = -1;
   waitForLock();
   TimeSeries ts = getSeries(i);
   if (ts != null) id = ts.getDbRow().getId();
   unlock();
   return id;
 }
 public CategoryDbTable.Row getSeriesMetaLocking(int i) {
   CategoryDbTable.Row row = null;
   waitForLock();
   TimeSeries ts = getSeries(i);
   row = new CategoryDbTable.Row(ts.getDbRow());
   unlock();
   return row;
 }
  public void toggleSeriesEnabled(long catId) {
    waitForLock();
    TimeSeries ts = getSeriesByIdNonlocking(catId);

    if (ts.isEnabled()) ts.setEnabled(false);
    else ts.setEnabled(true);
    unlock();
    return;
  }
 public boolean isSeriesEnabled(long catId) {
   boolean b;
   waitForLock();
   TimeSeries ts = getSeriesByIdNonlocking(catId);
   if (ts == null) b = false;
   else b = ts.isEnabled();
   unlock();
   return b;
 }
 /**
  * Create a TimeSeriesDataItem, add it to a TimeSeries. Now, modifying the original
  * TimeSeriesDataItem should NOT affect the TimeSeries.
  */
 @Test
 public void testAdd_TimeSeriesDataItem() {
   TimeSeriesDataItem item = new TimeSeriesDataItem(new Year(2009), 1.0);
   TimeSeries series = new TimeSeries("S1");
   series.add(item);
   assertTrue(item.equals(series.getDataItem(0)));
   item.setValue(new Double(99.9));
   assertFalse(item.equals(series.getDataItem(0)));
 }
 public void updateTimeSeriesData(long start, long end, boolean flushCache) {
   for (int i = 0; i < mSeries.size(); i++) {
     TimeSeries ts = mSeries.get(i);
     if (ts != null && ts.isEnabled() == true) {
       long catId = ts.getDbRow().getId();
       updateTimeSeriesDataLocking(catId, start, end, flushCache);
     }
   }
 }
  private void generateSynthetics() {
    for (int i = 0; i < mSeries.size(); i++) {
      TimeSeries synth = mSeries.get(i);
      if (synth == null || synth.getDbRow().getSynthetic() == false || synth.isEnabled() == false)
        continue;

      generateSynthetic(synth);
    }
  }
Exemple #14
0
  /**
   * Makes a plot showing overlaid individual normalized count for every word in WORDS from
   * STARTYEAR to ENDYEAR using NGM as a data source.
   */
  public static void plotAllWords(NGramMap ngm, String[] words, int startYear, int endYear) {
    Chart chart =
        new ChartBuilder().width(800).height(600).xAxisTitle("years").yAxisTitle("data").build();

    for (String word : words) {
      TimeSeries bundle = ngm.weightHistory(word, startYear, endYear);
      chart.addSeries(word, bundle.years(), bundle.data());
    }
    new SwingWrapper(chart).displayChart();
  }
 /** Test for bug report 3446965. */
 @Test
 public void testBug3446965() {
   TimeSeries s = new TimeSeries("s");
   s.addOrUpdate(new Year(2011), 100.0);
   s.addOrUpdate(new Year(2012), 150.0);
   s.addOrUpdate(new Year(2013), 200.0);
   s.addOrUpdate(new Year(2012), 250.0); // this line triggers the defect
   assertEquals(100.0, s.getMinY(), EPSILON);
   assertEquals(250.0, s.getMaxY(), EPSILON);
 }
  @Test
  public void testSetKey() {
    TimeSeries s1 = new TimeSeries("S");
    s1.setKey("S1");
    assertEquals("S1", s1.getKey());

    TimeSeriesCollection c = new TimeSeriesCollection();
    c.addSeries(s1);
    TimeSeries s2 = new TimeSeries("S2");
    c.addSeries(s2);

    // now we should be allowed to change s1's key to anything but "S2"
    s1.setKey("OK");
    assertEquals("OK", s1.getKey());

    try {
      s1.setKey("S2");
      fail("Expect an exception here.");
    } catch (IllegalArgumentException e) {
      // OK
    }

    // after s1 is removed from the collection, we should be able to set
    // the key to anything we want...
    c.removeSeries(s1);
    s1.setKey("S2");

    // check that removing by index also works
    s1.setKey("S1");
    c.addSeries(s1);
    c.removeSeries(1);
    s1.setKey("S2");
  }
  /** Basic tests for the delete() method. */
  @Test
  public void testDelete2() {
    TimeSeries s1 = new TimeSeries("Series", Year.class);
    s1.add(new Year(2000), 13.75);
    s1.add(new Year(2001), 11.90);
    s1.add(new Year(2002), null);
    s1.addChangeListener(this);
    this.gotSeriesChangeEvent = false;
    s1.delete(new Year(2001));
    assertTrue(this.gotSeriesChangeEvent);
    assertEquals(2, s1.getItemCount());
    assertEquals(null, s1.getValue(new Year(2001)));

    // try deleting a time period that doesn't exist...
    this.gotSeriesChangeEvent = false;
    s1.delete(new Year(2006));
    assertFalse(this.gotSeriesChangeEvent);

    // try deleting null
    try {
      s1.delete(null);
      fail("Expected IllegalArgumentException.");
    } catch (IllegalArgumentException e) {
      // expected
    }
  }
Exemple #18
0
    private final void populateFieldFromMapTypeFamily(
        T entity, Field field, KijiColumn column, KijiRowData row)
        throws IOException, IllegalAccessException {

      LOG.debug("Populating field '{}' from map-type family '{}'.", field, column.family());

      if (column.pageSize() > 0) {
        // Field is a closeable iterator of map-family entries (qualifier, timestamp, value).
        LOG.debug(
            "Populating field '{}' from paging-enabled map-type family '{}'.",
            field,
            column.family());
        final MapFamilyVersionIterator<?> iterator =
            new MapFamilyVersionIterator<Object>(
                row, column.family(), column.pageSize(), column.pageSize());
        field.set(entity, iterator);

      } else if (column.maxVersions() == 1) {
        // Field is a map: qualifier -> single value:

        LOG.debug("Populating single version map field '{}'.", field);
        Object value = null;

        if (field.getType() == KijiCellIterator.class) {
          Iterator<KijiCell<Object>> it = row.iterator(column.family());
          value = new KijiCellIterator<Object>(it);
        } else if (field.getType() == MapTypeCell.class) {
          value = new MapTypeCell<Object>(row.getMostRecentCells(column.family()));
        } else if (field.getType() == MapTypeValue.class) {
          value = new MapTypeValue<Object>(row.getMostRecentValues(column.family()));
        }
        field.set(entity, value);

      } else {
        // Field is a map: qualifier -> time-series
        LOG.debug("Populating map field '{}'.", field);
        Object value = null;
        if (field.getType() == KijiCellIterator.class) {
          Iterator<KijiCell<Object>> it = row.iterator(column.family());
          value = new KijiCellIterator<Object>(it);
        } else if (field.getType() == TSMapTypeValue.class) {
          // TODO: ARGH. This is awful.
          TSMapTypeValue<Object> tsValues = new TSMapTypeValue<Object>();
          for (String s : row.getQualifiers(column.family())) {
            final TimeSeries<Object> timeseries = new TimeSeries<Object>();
            for (final KijiCell<Object> cell : row.asIterable(column.family(), s)) {
              timeseries.put(cell.getTimestamp(), cell.getData());
            }
            tsValues.put(s, timeseries);
          }
          value = tsValues;
        }
        field.set(entity, value);
      }
    }
Exemple #19
0
  /**
   * Creates a plot of the TimeSeries TS. Labels the graph with the given TITLE, XLABEL, YLABEL, and
   * LEGEND.
   */
  public static void plotTS(
      TimeSeries ts, String title, String xlabel, String ylabel, String legend) {
    Collection years = ts.years();
    Collection counts = ts.data();

    // Create Chart
    Chart chart = QuickChart.getChart(title, ylabel, xlabel, legend, years, counts);

    // Show it
    new SwingWrapper(chart).displayChart();
  }
 /** Calling removeAgedItems() on an empty series should not throw any exception. */
 @Test
 public void testRemoveAgedItems3() {
   TimeSeries s = new TimeSeries("Test");
   boolean pass = true;
   try {
     s.removeAgedItems(0L, true);
   } catch (Exception e) {
     pass = false;
   }
   assertTrue(pass);
 }
 /** Serialize an instance, restore it, and check for equality. */
 @Test
 public void testSerialization() {
   TimeSeries s1 = new TimeSeries("A test");
   s1.add(new Year(2000), 13.75);
   s1.add(new Year(2001), 11.90);
   s1.add(new Year(2002), null);
   s1.add(new Year(2005), 19.32);
   s1.add(new Year(2007), 16.89);
   TimeSeries s2 = (TimeSeries) TestUtilities.serialised(s1);
   assertTrue(s1.equals(s2));
 }
  public synchronized void gatherSeries(long milliStart, long milliEnd) {
    ArrayList<Datapoint> pre, range, post;
    boolean has_data;
    long oldAggregationMs = mAggregationMs;

    mQueryStart = milliStart;
    mQueryEnd = milliEnd;

    setCollectionTimes(milliStart, milliEnd);
    for (int i = 0; i < mSeries.size(); i++) {
      has_data = false;

      TimeSeries ts = mSeries.get(i);
      if (ts == null || ts.getDbRow().getSynthetic()) continue;

      if (ts.isEnabled() == false) {
        boolean skip = true;
        for (int j = 0; j < ts.getDependees().size(); j++) {
          if (ts.getDependees().get(j).isEnabled() == true) {
            skip = false;
            break;
          }
        }
        if (skip == true) continue;
      }

      mDatapointCache.populateRange(
          ts.getDbRow().getId(), mCollectionStart, mCollectionEnd, mAggregationMs);

      pre = mDatapointCache.getDataBefore(ts.getDbRow().getId(), mHistory, mCollectionStart);
      if (pre != null && pre.size() > 0) has_data = true;

      range =
          mDatapointCache.getDataInRange(ts.getDbRow().getId(), mCollectionStart, mCollectionEnd);
      if (range != null && range.size() > 0) has_data = true;

      post = mDatapointCache.getDataAfter(ts.getDbRow().getId(), 1, mCollectionEnd);
      if (post != null && range.size() > 0) has_data = true;

      if (has_data == true) ts.setDatapoints(pre, range, post, true);
    }

    generateSynthetics();

    ArrayList<TimeSeries> enabledSeries = getAllEnabledSeries();
    for (int i = 0; i < enabledSeries.size(); i++) {
      aggregateDatapoints(enabledSeries.get(i));
    }

    mAggregationMs = oldAggregationMs;

    return;
  }
Exemple #23
0
    private final void populateFieldFromFullyQualifiedColumn(
        T entity, Field field, KijiColumn column, KijiRowData row)
        throws IOException, IllegalAccessException {

      if (column.maxVersions() == 1) {
        // Field represents a single value from a fully-qualified column:
        LOG.debug(
            "Populating field '{}' from column '{}:{}'.",
            field,
            column.family(),
            column.qualifier());
        KijiCell<?> cell = row.getMostRecentCell(column.family(), column.qualifier());
        if (cell == null) return;
        Object value = cell.getData();

        if (field.getType() == KijiCell.class) {
          value = cell;
        } else if (field.getType() == String.class && value != null) {
          value = value.toString();
        }

        // If there is no cell for a field with a primitive type, use the default value:
        if ((null == value) && field.getType().isPrimitive()) {
          value = Defaults.defaultValue(field.getType());
        }

        field.set(entity, value);
      } else {
        // Field represents a time-series from a fully-qualified column:
        if (column.pageSize() > 0) {
          final ColumnVersionIterator<?> iterator =
              new ColumnVersionIterator<Object>(
                  row, column.family(), column.qualifier(), column.pageSize());
          field.set(entity, iterator);
        } else {
          Object value = null;
          if (field.getType() == KijiCellValueIterator.class) {
            value =
                new KijiCellValueIterator<Object>(
                    row.iterator(column.family(), column.qualifier()));
          } else if (field.getType() == TimeSeries.class) {
            final TimeSeries<Object> timeseries = new TimeSeries<Object>();
            for (final KijiCell<Object> cell :
                row.asIterable(column.family(), column.qualifier())) {
              timeseries.put(cell.getTimestamp(), cell.getData());
            }
            value = timeseries;
          }
          field.set(entity, value);
        }
      }
    }
Exemple #24
0
  /**
   * Creates overlaid category weight plots for each category label in CATEGORYLABELS from STARTYEAR
   * to ENDYEAR using NGM and WN as data sources.
   */
  public static void plotCategoryWeights(
      NGramMap ngm, WordNet wn, String[] categoryLabels, int startYear, int endYear) {

    Chart chart =
        new ChartBuilder().width(800).height(600).xAxisTitle("years").yAxisTitle("data").build();

    for (String categoryLabel : categoryLabels) {
      Set words = wn.hyponyms(categoryLabel);
      TimeSeries bundle = ngm.summedWeightHistory(words, startYear, endYear);
      chart.addSeries(categoryLabel, bundle.years(), bundle.data());
    }
    new SwingWrapper(chart).displayChart();
  }
  public void draw(double[] v) {
    TimeSeries data = new TimeSeries("symbol", FixedMillisecond.class);
    dataset1.addSeries(data);

    for (int i = 0; i < v.length; i++) {
      try {
        data.add(new FixedMillisecond(i), v[i]);
      } catch (Exception e) {
        System.out.println("Unresolved error");
        // e.printStackTrace();
      }
    }
  }
  public void clearSeriesLocking() {
    TimeSeries ts;

    waitForLock();
    for (int i = 0; i < mSeries.size(); i++) {
      ts = mSeries.get(i);
      if (ts != null) {
        ts.clearSeries();
      }
    }
    mSeries.clear();
    unlock();
  }
 /** Test for bug report 1864222. */
 @Test
 public void testBug1864222() {
   TimeSeries s = new TimeSeries("S");
   s.add(new Day(19, 8, 2005), 1);
   s.add(new Day(31, 1, 2006), 1);
   boolean pass = true;
   try {
     s.createCopy(new Day(1, 12, 2005), new Day(18, 1, 2006));
   } catch (CloneNotSupportedException e) {
     pass = false;
   }
   assertTrue(pass);
 }
 /** Some checks for the addOrUpdate() method. */
 @Test
 public void testAddOrUpdate() {
   TimeSeries s1 = new TimeSeries("S1", Year.class);
   s1.setMaximumItemCount(2);
   s1.addOrUpdate(new Year(2000), 100.0);
   assertEquals(1, s1.getItemCount());
   s1.addOrUpdate(new Year(2001), 101.0);
   assertEquals(2, s1.getItemCount());
   s1.addOrUpdate(new Year(2001), 102.0);
   assertEquals(2, s1.getItemCount());
   s1.addOrUpdate(new Year(2002), 103.0);
   assertEquals(2, s1.getItemCount());
 }
  /** Some checks for the getDataItem(RegularTimePeriod) method. */
  @Test
  public void testGetDataItem2() {
    TimeSeries series = new TimeSeries("S", Year.class);
    assertNull(series.getDataItem(new Year(2006)));

    // try a null argument
    boolean pass = false;
    try {
      /* TimeSeriesDataItem item = */ series.getDataItem(null);
    } catch (IllegalArgumentException e) {
      pass = true;
    }
    assertTrue(pass);
  }
  /** Test that the addOrUpdate() method won't allow multiple time period classes. */
  @Test
  public void testAddOrUpdate3() {
    TimeSeries s1 = new TimeSeries("S1");
    s1.addOrUpdate(new Year(2010), 1.1);
    assertEquals(Year.class, s1.getTimePeriodClass());

    boolean pass = false;
    try {
      s1.addOrUpdate(new Month(1, 2009), 0.0);
    } catch (SeriesException e) {
      pass = true;
    }
    assertTrue(pass);
  }