From 7482d0bcaddd5cd01b3587c4c332d0b9f7578c75 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 12 Sep 2020 14:48:52 +0400 Subject: [PATCH] #1 -- add some models --- .../controllers/TimeSeriesController.java | 7 ++++-- src/main/java/ru/ulstu/models/Forecast.java | 23 +++++++++++++++++++ .../ulstu/models/ForecastRequestParams.java | 22 ++++++++++++++++++ src/main/java/ru/ulstu/models/TimeSeries.java | 6 ++++- .../ru/ulstu/services/TimeSeriesService.java | 5 ++-- .../ru/ulstu/tsMethods/TimeSeriesMethod.java | 11 ++++----- .../exponential/NoTrendNoSeason.java | 2 +- 7 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 src/main/java/ru/ulstu/models/Forecast.java create mode 100644 src/main/java/ru/ulstu/models/ForecastRequestParams.java diff --git a/src/main/java/ru/ulstu/controllers/TimeSeriesController.java b/src/main/java/ru/ulstu/controllers/TimeSeriesController.java index 50a70d5..da0884f 100644 --- a/src/main/java/ru/ulstu/controllers/TimeSeriesController.java +++ b/src/main/java/ru/ulstu/controllers/TimeSeriesController.java @@ -10,6 +10,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import ru.ulstu.configurations.ApiConfiguration; +import ru.ulstu.models.Forecast; +import ru.ulstu.models.ForecastRequestParams; import ru.ulstu.models.TimeSeries; import ru.ulstu.services.TimeSeriesService; @@ -37,7 +39,8 @@ public class TimeSeriesController { @PostMapping("getForecast") @ApiOperation("Получить прогноз временного ряда") - public ResponseEntity getForecastTimeSeries(@RequestBody TimeSeries timeSeries) { - return new ResponseEntity<>(timeSeriesService.getForecast(timeSeries), HttpStatus.OK); + public ResponseEntity getForecastTimeSeries(@RequestBody ForecastRequestParams forecastRequestParams) { + return new ResponseEntity<>(timeSeriesService.getForecast(forecastRequestParams.getOriginalTimeSeries(), + forecastRequestParams.getCountForecast()), HttpStatus.OK); } } diff --git a/src/main/java/ru/ulstu/models/Forecast.java b/src/main/java/ru/ulstu/models/Forecast.java new file mode 100644 index 0000000..c3413fc --- /dev/null +++ b/src/main/java/ru/ulstu/models/Forecast.java @@ -0,0 +1,23 @@ +package ru.ulstu.models; + +public class Forecast { + private TimeSeries originalTimeSeries; + private TimeSeries forecast; + + public Forecast(TimeSeries originalTimeSeries) { + this.originalTimeSeries = originalTimeSeries; + this.forecast = new TimeSeries("Forecast time series of '" + originalTimeSeries.getName() + "'"); + } + + public TimeSeries getOriginalTimeSeries() { + return originalTimeSeries; + } + + public TimeSeries getForecast() { + return forecast; + } + + public void addValue(TimeSeriesValue timeSeriesValue) { + forecast.addValue(timeSeriesValue); + } +} diff --git a/src/main/java/ru/ulstu/models/ForecastRequestParams.java b/src/main/java/ru/ulstu/models/ForecastRequestParams.java new file mode 100644 index 0000000..3ec6f85 --- /dev/null +++ b/src/main/java/ru/ulstu/models/ForecastRequestParams.java @@ -0,0 +1,22 @@ +package ru.ulstu.models; + +public class ForecastRequestParams { + private TimeSeries originalTimeSeries; + private int countForecast; + + public TimeSeries getOriginalTimeSeries() { + return originalTimeSeries; + } + + public void setOriginalTimeSeries(TimeSeries originalTimeSeries) { + this.originalTimeSeries = originalTimeSeries; + } + + public int getCountForecast() { + return countForecast; + } + + public void setCountForecast(int countForecast) { + this.countForecast = countForecast; + } +} diff --git a/src/main/java/ru/ulstu/models/TimeSeries.java b/src/main/java/ru/ulstu/models/TimeSeries.java index f5c95d2..7515cf3 100644 --- a/src/main/java/ru/ulstu/models/TimeSeries.java +++ b/src/main/java/ru/ulstu/models/TimeSeries.java @@ -53,6 +53,10 @@ public class TimeSeries { } public TimeSeriesValue getLastValue() { - return values.get(values.size()-1); + return values.get(values.size() - 1); + } + + public int getLength() { + return values.size(); } } diff --git a/src/main/java/ru/ulstu/services/TimeSeriesService.java b/src/main/java/ru/ulstu/services/TimeSeriesService.java index 8c98db3..668e547 100644 --- a/src/main/java/ru/ulstu/services/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/services/TimeSeriesService.java @@ -1,6 +1,7 @@ package ru.ulstu.services; import org.springframework.stereotype.Service; +import ru.ulstu.models.Forecast; import ru.ulstu.models.TimeSeries; import ru.ulstu.models.TimeSeriesValue; import ru.ulstu.tsMethods.exponential.NoTrendNoSeason; @@ -20,8 +21,8 @@ public class TimeSeriesService { return ts; } - public TimeSeries getForecast(TimeSeries timeSeries) { - NoTrendNoSeason nn = new NoTrendNoSeason(timeSeries, 5); + public Forecast getForecast(TimeSeries timeSeries, int countForecast) { + NoTrendNoSeason nn = new NoTrendNoSeason(timeSeries, countForecast); nn.setAlpa(0.1); nn.createModel(); return nn.getForecastTimeSeries(); diff --git a/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java b/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java index 219e506..dc86a73 100644 --- a/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java +++ b/src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java @@ -1,5 +1,6 @@ package ru.ulstu.tsMethods; +import ru.ulstu.models.Forecast; import ru.ulstu.models.TimeSeries; import java.util.HashMap; @@ -8,13 +9,13 @@ import java.util.Map; public abstract class TimeSeriesMethod { protected TimeSeries originalTimeSeries; protected TimeSeries modelTimeSeries; - protected TimeSeries forecastTimeSeries; + protected Forecast forecastTimeSeries; protected int countForecast; protected Map parameters = new HashMap<>(); protected void init() { modelTimeSeries = new TimeSeries("Model time series of '" + originalTimeSeries.getName() + "'"); - forecastTimeSeries = new TimeSeries("Forecast time series of '" + originalTimeSeries.getName() + "'"); + forecastTimeSeries = new Forecast(originalTimeSeries); } public TimeSeries getOriginalTimeSeries() { @@ -25,11 +26,7 @@ public abstract class TimeSeriesMethod { return modelTimeSeries; } - public int getCountForecast() { - return countForecast; - } - - public TimeSeries getForecastTimeSeries() { + public Forecast getForecastTimeSeries() { return forecastTimeSeries; } diff --git a/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java b/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java index 9bd990d..be15776 100644 --- a/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java +++ b/src/main/java/ru/ulstu/tsMethods/exponential/NoTrendNoSeason.java @@ -34,7 +34,7 @@ public class NoTrendNoSeason extends TimeSeriesMethod { for (int t = 0; t < originalTimeSeries.getValues().size() - 1 + countForecast; t++) { // пока не дошли до конца ряда - сглаживаем, иначе строим прогноз if (t < originalTimeSeries.getValues().size()) { - e = originalTimeSeries.getValues().get(t).getValue() - forecastTimeSeries.getValues().get(t).getValue(); + e = originalTimeSeries.getValues().get(t).getValue() - forecastTimeSeries.getForecast().getValues().get(t).getValue(); } else { e = 0; }