diff --git a/src/main/java/ru/ulstu/models/exceptions/ModelingException.java b/src/main/java/ru/ulstu/models/exceptions/ModelingException.java new file mode 100644 index 0000000..4bebd12 --- /dev/null +++ b/src/main/java/ru/ulstu/models/exceptions/ModelingException.java @@ -0,0 +1,7 @@ +package ru.ulstu.models.exceptions; + +public class ModelingException extends Exception { + public ModelingException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/ulstu/models/exceptions/TimeSeriesValidateException.java b/src/main/java/ru/ulstu/models/exceptions/TimeSeriesValidateException.java new file mode 100644 index 0000000..7138862 --- /dev/null +++ b/src/main/java/ru/ulstu/models/exceptions/TimeSeriesValidateException.java @@ -0,0 +1,7 @@ +package ru.ulstu.models.exceptions; + +public class TimeSeriesValidateException extends Exception { + public TimeSeriesValidateException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/ulstu/services/TimeSeriesService.java b/src/main/java/ru/ulstu/services/TimeSeriesService.java index 2eb8037..4bf9a40 100644 --- a/src/main/java/ru/ulstu/services/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/services/TimeSeriesService.java @@ -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; + } } } diff --git a/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java b/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java index 58206c4..0eb6d70 100644 --- a/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java +++ b/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java @@ -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 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("Временной ряд должен иметь отметки времени"); + } + } } diff --git a/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java b/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java index bd78381..cdc8d50 100644 --- a/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java +++ b/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java @@ -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 точек