TimeSeriesValidator implements TimeSeriesGraphForm.ValidatorUI, TimeSeriesGraphModel.Validation { @Override public TimeSeries validate(TimeSeries timeSeries, String sourceName, TimeSeriesType type) throws ParseException { String qualifiedSourceName = createQualifiedSourcename(sourceName, type); final Symbol symbol = namespace.resolveSymbol(qualifiedSourceName); if (symbol == null) { throw new ParseException("No variable for identifier '" + qualifiedSourceName + "' registered."); } final String expression = getExpressionFor(qualifiedSourceName); if (expression == null || expression.trim().isEmpty()) { return timeSeries; } final Variable variable = (Variable) symbol; final Term term = parser.parse(expression, namespace); final int seriesCount = timeSeries.getItemCount(); final TimeSeries validatedSeries = new TimeSeries(timeSeries.getKey()); for (int i = 0; i < seriesCount; i++) { final TimeSeriesDataItem dataItem = timeSeries.getDataItem(i); final Number value = dataItem.getValue(); variable.assignD(null, value.doubleValue()); if (term.evalB(null)) { validatedSeries.add(dataItem); } } return validatedSeries; }  @Override JComponent createUI(); @Override void adaptTo(Object timeSeriesKey, AxisMapping axisMapping); @Override TimeSeries validate(TimeSeries timeSeries, String sourceName, TimeSeriesType type); @Override void addValidationListener(TimeSeriesGraphModel.ValidationListener listener);  }
@Test public void testValidateWithDefaultExpression() throws Exception { final TimeSeries unvalidatedTimeSeries = new TimeSeries("insitu1"); unvalidatedTimeSeries.add(ITEM_NAN); unvalidatedTimeSeries.add(ITEM_0); unvalidatedTimeSeries.add(ITEM_24_5); final TimeSeries validatedTimeSeries = validator.validate(unvalidatedTimeSeries, "insitu1", TimeSeriesType.INSITU); assertSame(unvalidatedTimeSeries, validatedTimeSeries); assertEquals(3, validatedTimeSeries.getItemCount()); assertEquals(ITEM_NAN, validatedTimeSeries.getDataItem(0)); assertEquals(ITEM_0, validatedTimeSeries.getDataItem(1)); assertEquals(ITEM_24_5, validatedTimeSeries.getDataItem(2)); }
@Test public void testValidate() throws Exception { final TimeSeries unvalidatedTimeSeries = new TimeSeries("raster1"); unvalidatedTimeSeries.add(ITEM_0); unvalidatedTimeSeries.add(ITEM_4); unvalidatedTimeSeries.add(ITEM_7); unvalidatedTimeSeries.add(ITEM_24_5); assertTrue(validator.setExpression("r.raster1", "r.raster1 > 5")); final TimeSeries validated = validator.validate(unvalidatedTimeSeries, "raster1", TimeSeriesType.PIN); assertNotSame(unvalidatedTimeSeries, validated); assertEquals(2, validated.getItemCount()); assertEquals(ITEM_7, validated.getDataItem(0)); assertEquals(ITEM_24_5, validated.getDataItem(1)); }