#2 -- create validation method

This commit is contained in:
Anton Romanov 2020-09-14 16:40:35 +04:00
parent cfb0782825
commit 3cd0d77e0c
5 changed files with 47 additions and 17 deletions

View File

@ -0,0 +1,7 @@
package ru.ulstu.models.exceptions;
public class ModelingException extends Exception {
public ModelingException(String message) {
super(message);
}
}

View File

@ -0,0 +1,7 @@
package ru.ulstu.models.exceptions;
public class TimeSeriesValidateException extends Exception {
public TimeSeriesValidateException(String message) {
super(message);
}
}

View File

@ -1,9 +1,12 @@
package ru.ulstu.services;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import ru.ulstu.models.Forecast;
import ru.ulstu.models.TimeSeries;
import ru.ulstu.models.TimeSeriesValue;
import ru.ulstu.models.exceptions.TimeSeriesValidateException;
import ru.ulstu.tsMethods.exponential.NoTrendNoSeason;
import java.time.LocalDate;
@ -11,6 +14,8 @@ import java.time.LocalDate;
@Service
public class TimeSeriesService {
private static final Logger LOGGER = LoggerFactory.getLogger(TimeSeriesService.class);
public TimeSeries getRandomTimeSeries(int length) {
TimeSeries ts = new TimeSeries("Random time series");
LocalDate dateStart = LocalDate.now().minusDays(length);
@ -22,9 +27,14 @@ public class TimeSeriesService {
}
public Forecast getForecast(TimeSeries timeSeries, int countForecast) {
NoTrendNoSeason nn = new NoTrendNoSeason(timeSeries, countForecast);
nn.setAlpa(0.1);
nn.createModel();
return nn.getForecast();
try {
NoTrendNoSeason nn = new NoTrendNoSeason(timeSeries);
nn.setAlpa(0.1);
nn.createModel();
return nn.getForecast();
} catch (TimeSeriesValidateException ex) {
LOGGER.error("Некорректная инициализация метода моделирования", ex);
return null;
}
}
}

View File

@ -3,6 +3,7 @@ package ru.ulstu.tsMethods;
import ru.ulstu.models.Forecast;
import ru.ulstu.models.ModelTimeSeries;
import ru.ulstu.models.TimeSeries;
import ru.ulstu.models.exceptions.TimeSeriesValidateException;
import java.util.HashMap;
import java.util.Map;
@ -14,7 +15,9 @@ public abstract class TimeSeriesMethod {
protected int countForecast;
protected Map<Param, Double> parameters = new HashMap<>();
protected void init() {
protected TimeSeriesMethod(TimeSeries originalTimeSeries) throws TimeSeriesValidateException {
validateTimeSeries(originalTimeSeries);
this.originalTimeSeries = originalTimeSeries;
model = new ModelTimeSeries(originalTimeSeries);
forecast = new Forecast(originalTimeSeries);
}
@ -32,4 +35,16 @@ public abstract class TimeSeriesMethod {
}
public abstract void createModel();
private void validateTimeSeries(TimeSeries timeSeries) throws TimeSeriesValidateException {
if (timeSeries == null || timeSeries.isEmpty()) {
throw new TimeSeriesValidateException("Временной ряд должен быть не пустым");
}
if (timeSeries.getValues().stream().anyMatch(val -> val == null || val.getValue() == null)) {
throw new TimeSeriesValidateException("Временной ряд содержит пустые значения");
}
if (timeSeries.getValues().stream().anyMatch(val -> val.getDate() == null)) {
throw new TimeSeriesValidateException("Временной ряд должен иметь отметки времени");
}
}
}

View File

@ -1,21 +1,13 @@
package ru.ulstu.tsMethods.exponential;
import ru.ulstu.models.TimeSeries;
import ru.ulstu.models.exceptions.TimeSeriesValidateException;
import ru.ulstu.tsMethods.Param;
import ru.ulstu.tsMethods.TimeSeriesMethod;
public class NoTrendNoSeason extends TimeSeriesMethod {
public NoTrendNoSeason(TimeSeries originalTimeSeries, int countForecast) {
this.originalTimeSeries = originalTimeSeries;
this.countForecast = countForecast;
if (originalTimeSeries.isEmpty()) {
throw new RuntimeException("Time series must not empty");
}
}
@Override
protected void init() {
super.init();
public NoTrendNoSeason(TimeSeries originalTimeSeries) throws TimeSeriesValidateException {
super(originalTimeSeries);
model.addValue(originalTimeSeries.getValues().get(0));
forecast.addValue(originalTimeSeries.getValues().get(0));
parameters.put(Param.ALPHA, 1.0);
@ -27,7 +19,6 @@ public class NoTrendNoSeason extends TimeSeriesMethod {
@Override
public void createModel() {
init();
double e;
//выполняется проход модели по сглаживанию и прогнозированию countPointForecast точек