#5 -- fix NPE

This commit is contained in:
Anton Romanov 2021-08-11 14:24:45 +04:00
parent 8dd4ec6570
commit 9560190368
5 changed files with 31 additions and 17 deletions

View File

@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import ru.ulstu.datamodel.ChartForm; import ru.ulstu.datamodel.ChartForm;
import ru.ulstu.datamodel.ModelingResult; import ru.ulstu.datamodel.ModelingResult;
import ru.ulstu.datamodel.exception.ModelingException;
import ru.ulstu.datamodel.ts.TimeSeries; import ru.ulstu.datamodel.ts.TimeSeries;
import ru.ulstu.datamodel.ts.TimeSeriesValue; import ru.ulstu.datamodel.ts.TimeSeriesValue;
import ru.ulstu.db.DbService; import ru.ulstu.db.DbService;
@ -22,9 +23,11 @@ import ru.ulstu.service.UtilService;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Controller @Controller
@ -52,7 +55,7 @@ public class IndexController {
} }
@GetMapping("chart") @GetMapping("chart")
public String chart(@ModelAttribute ChartForm chartForm, Model model) throws IOException { public String chart(@ModelAttribute ChartForm chartForm, Model model) throws IOException, ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
model.addAttribute("sets", dbService.getSets()); model.addAttribute("sets", dbService.getSets());
if (chartForm.getSet() != null) { if (chartForm.getSet() != null) {
model.addAttribute("listTimeSeries", dbService.getTimeSeriesMeta(chartForm.getSet())); model.addAttribute("listTimeSeries", dbService.getTimeSeriesMeta(chartForm.getSet()));
@ -65,21 +68,12 @@ public class IndexController {
return "index.html"; return "index.html";
} }
private void addChartToModel(TimeSeries timeSeries, Model model) { private void addChartToModel(TimeSeries timeSeries, Model model) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, ModelingException {
int countForecastPoints = 10; int countForecastPoints = timeSeries.getLength() > 20 ? 10 : timeSeries.getLength() / 3;
TimeSeries timeSeriesModel = null; TimeSeries timeSeriesModel = timeSeriesService.smoothTimeSeries(timeSeries);
ModelingResult modelingResult = null; ModelingResult modelingResult = timeSeriesService.getForecast(timeSeries, countForecastPoints);
TimeSeries forecast = null; TimeSeries forecast = modelingResult.getTimeSeries();
TimeSeries testForecast = null; TimeSeries testForecast = modelingResult.getTestForecast();
try {
timeSeriesModel = timeSeriesService.smoothTimeSeries(timeSeries);
modelingResult = timeSeriesService.getForecast(timeSeries, countForecastPoints);
forecast = modelingResult.getTimeSeries();
testForecast = modelingResult.getTestForecast();
} catch (Exception e) {
LOG.warn(e.getMessage());
}
Set<String> dates = timeSeries.getValues().stream() Set<String> dates = timeSeries.getValues().stream()
.map(v -> v.getDate().format(DateTimeFormatter.ISO_DATE)) .map(v -> v.getDate().format(DateTimeFormatter.ISO_DATE))
.collect(Collectors.toSet()); .collect(Collectors.toSet());

View File

@ -28,7 +28,7 @@ public class Smape extends ScoreMethod {
//double actualValue = getValueOnSameDate(modelValue, original).getValue(); //double actualValue = getValueOnSameDate(modelValue, original).getValue();
double actualValue = Optional.ofNullable(tsValues.get(modelValue.getDate())) double actualValue = Optional.ofNullable(tsValues.get(modelValue.getDate()))
.orElseThrow(() -> new ModelingException("Значение модельного ряда не найдено в оригинальном ряде: " .orElseThrow(() -> new ModelingException("Значение модельного ряда не найдено в оригинальном ряде: "
+ modelValue.getDate())); + modelValue.getDate() + " " + tsValues + " " + model));
sum += abs(modelValue.getValue() - actualValue) sum += abs(modelValue.getValue() - actualValue)
/ ((abs(actualValue) + abs(modelValue.getValue())) / 2); / ((abs(actualValue) + abs(modelValue.getValue())) / 2);
} }

View File

@ -60,6 +60,7 @@ public class MethodParamBruteForce {
if (methodInstance.canMakeForecast(reducedTimeSeries, parametersValues, countPoints)) { if (methodInstance.canMakeForecast(reducedTimeSeries, parametersValues, countPoints)) {
results.add(executors.submit(() -> { results.add(executors.submit(() -> {
TimeSeries forecast = methodInstance.getForecast(reducedTimeSeries, parametersValues, countPoints); TimeSeries forecast = methodInstance.getForecast(reducedTimeSeries, parametersValues, countPoints);
forecast = syncDates(forecast, timeSeries);
return new ModelingResult(forecast, null, return new ModelingResult(forecast, null,
parametersValues, parametersValues,
scoreMethod.getScore(tsValues, forecast), scoreMethod.getScore(tsValues, forecast),
@ -87,6 +88,15 @@ public class MethodParamBruteForce {
bestResult.getTimeSeriesMethod()); bestResult.getTimeSeriesMethod());
} }
private TimeSeries syncDates(TimeSeries forecast, TimeSeries timeSeries) {
List<TimeSeriesValue> forecastValues = forecast.getValues();
for (int i = 1; i <= forecastValues.size(); i++) {
forecastValues.get(forecastValues.size() - i)
.setDate(timeSeries.getValues().get(timeSeries.getValues().size() - i).getDate());
}
return forecast;
}
public TimeSeries getForecastWithOptimalLength(TimeSeries timeSeries) { public TimeSeries getForecastWithOptimalLength(TimeSeries timeSeries) {
throw new RuntimeException("Not implemented"); throw new RuntimeException("Not implemented");
} }

View File

@ -0,0 +1,9 @@
2021-04-30T09:32:57.120554;37.13
2021-05-31T09:32:57.120554;39.89
2021-06-30T09:32:57.120554;41.6
2021-07-30T09:32:57.120554;35.39
2021-08-31T09:32:57.120554;33.46
2021-09-30T09:32:57.120554;45.42
2021-10-30T09:32:57.120554;41.99
2021-11-30T09:32:57.120554;40.93
2021-12-30T09:32:57.120554;36.07
1 2021-04-30T09:32:57.120554 37.13
2 2021-05-31T09:32:57.120554 39.89
3 2021-06-30T09:32:57.120554 41.6
4 2021-07-30T09:32:57.120554 35.39
5 2021-08-31T09:32:57.120554 33.46
6 2021-09-30T09:32:57.120554 45.42
7 2021-10-30T09:32:57.120554 41.99
8 2021-11-30T09:32:57.120554 40.93
9 2021-12-30T09:32:57.120554 36.07

View File

@ -0,0 +1 @@
{"key":"Mazak VCN 410A работа по программе","size":9,"hasDateTime":true}