#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; package ru.ulstu.services;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.models.Forecast; import ru.ulstu.models.Forecast;
import ru.ulstu.models.TimeSeries; import ru.ulstu.models.TimeSeries;
import ru.ulstu.models.TimeSeriesValue; import ru.ulstu.models.TimeSeriesValue;
import ru.ulstu.models.exceptions.TimeSeriesValidateException;
import ru.ulstu.tsMethods.exponential.NoTrendNoSeason; import ru.ulstu.tsMethods.exponential.NoTrendNoSeason;
import java.time.LocalDate; import java.time.LocalDate;
@ -11,6 +14,8 @@ import java.time.LocalDate;
@Service @Service
public class TimeSeriesService { public class TimeSeriesService {
private static final Logger LOGGER = LoggerFactory.getLogger(TimeSeriesService.class);
public TimeSeries getRandomTimeSeries(int length) { public TimeSeries getRandomTimeSeries(int length) {
TimeSeries ts = new TimeSeries("Random time series"); TimeSeries ts = new TimeSeries("Random time series");
LocalDate dateStart = LocalDate.now().minusDays(length); LocalDate dateStart = LocalDate.now().minusDays(length);
@ -22,9 +27,14 @@ public class TimeSeriesService {
} }
public Forecast getForecast(TimeSeries timeSeries, int countForecast) { public Forecast getForecast(TimeSeries timeSeries, int countForecast) {
NoTrendNoSeason nn = new NoTrendNoSeason(timeSeries, countForecast); try {
nn.setAlpa(0.1); NoTrendNoSeason nn = new NoTrendNoSeason(timeSeries);
nn.createModel(); nn.setAlpa(0.1);
return nn.getForecast(); 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.Forecast;
import ru.ulstu.models.ModelTimeSeries; import ru.ulstu.models.ModelTimeSeries;
import ru.ulstu.models.TimeSeries; import ru.ulstu.models.TimeSeries;
import ru.ulstu.models.exceptions.TimeSeriesValidateException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -14,7 +15,9 @@ public abstract class TimeSeriesMethod {
protected int countForecast; protected int countForecast;
protected Map<Param, Double> parameters = new HashMap<>(); 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); model = new ModelTimeSeries(originalTimeSeries);
forecast = new Forecast(originalTimeSeries); forecast = new Forecast(originalTimeSeries);
} }
@ -32,4 +35,16 @@ public abstract class TimeSeriesMethod {
} }
public abstract void createModel(); 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; package ru.ulstu.tsMethods.exponential;
import ru.ulstu.models.TimeSeries; import ru.ulstu.models.TimeSeries;
import ru.ulstu.models.exceptions.TimeSeriesValidateException;
import ru.ulstu.tsMethods.Param; import ru.ulstu.tsMethods.Param;
import ru.ulstu.tsMethods.TimeSeriesMethod; import ru.ulstu.tsMethods.TimeSeriesMethod;
public class NoTrendNoSeason extends TimeSeriesMethod { public class NoTrendNoSeason extends TimeSeriesMethod {
public NoTrendNoSeason(TimeSeries originalTimeSeries, int countForecast) { public NoTrendNoSeason(TimeSeries originalTimeSeries) throws TimeSeriesValidateException {
this.originalTimeSeries = originalTimeSeries; super(originalTimeSeries);
this.countForecast = countForecast;
if (originalTimeSeries.isEmpty()) {
throw new RuntimeException("Time series must not empty");
}
}
@Override
protected void init() {
super.init();
model.addValue(originalTimeSeries.getValues().get(0)); model.addValue(originalTimeSeries.getValues().get(0));
forecast.addValue(originalTimeSeries.getValues().get(0)); forecast.addValue(originalTimeSeries.getValues().get(0));
parameters.put(Param.ALPHA, 1.0); parameters.put(Param.ALPHA, 1.0);
@ -27,7 +19,6 @@ public class NoTrendNoSeason extends TimeSeriesMethod {
@Override @Override
public void createModel() { public void createModel() {
init();
double e; double e;
//выполняется проход модели по сглаживанию и прогнозированию countPointForecast точек //выполняется проход модели по сглаживанию и прогнозированию countPointForecast точек