From e4d32a3e92513fc9b20af837c1d8c4b94dd32eb8 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 2 Feb 2023 13:02:13 +0400 Subject: [PATCH] #84 -- multiple assessments --- .../rule/model/AssessmentException.java | 7 ++++ .../rule/service/FuzzyInferenceService.java | 41 +++++++++++++++---- .../ts/service/TimeSeriesService.java | 8 ++-- 3 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/rule/model/AssessmentException.java diff --git a/src/main/java/ru/ulstu/extractor/rule/model/AssessmentException.java b/src/main/java/ru/ulstu/extractor/rule/model/AssessmentException.java new file mode 100644 index 0000000..d021db6 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/rule/model/AssessmentException.java @@ -0,0 +1,7 @@ +package ru.ulstu.extractor.rule.model; + +public class AssessmentException extends RuntimeException { + public AssessmentException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java index d17d112..9d8bc86 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java @@ -15,6 +15,7 @@ import org.springframework.stereotype.Service; import ru.ulstu.extractor.assessment.model.Assessment; import ru.ulstu.extractor.gitrepository.service.GitRepositoryService; import ru.ulstu.extractor.rule.model.AntecedentValue; +import ru.ulstu.extractor.rule.model.AssessmentException; import ru.ulstu.extractor.rule.model.DbRule; import ru.ulstu.extractor.ts.model.TimeSeries; import ru.ulstu.extractor.ts.service.TimeSeriesService; @@ -48,10 +49,9 @@ public class FuzzyInferenceService { this.timeSeriesService = timeSeriesService; } - public List getRulesFromDb(Map variableValues) { - List dbDbRules = ruleService.getList(); - validateVariables(variableValues, dbDbRules); - return dbDbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList()); + public List getRulesFromDb(List dbRules, Map variableValues) { + validateVariables(variableValues, dbRules); + return dbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList()); } private String getFuzzyRule(DbRule dbRule) { @@ -64,6 +64,7 @@ public class FuzzyInferenceService { } private RuleBlock getRuleBlock(Engine engine, + List dbRules, Map variableValues, List antecedentValues, List consequentValues) { @@ -102,7 +103,7 @@ public class FuzzyInferenceService { mamdani.setDisjunction(new BoundedSum()); mamdani.setImplication(new AlgebraicProduct()); mamdani.setActivation(new Highest()); - getRulesFromDb(variableValues).forEach(r -> mamdani.addRule(Rule.parse(r, engine))); + getRulesFromDb(dbRules, variableValues).forEach(r -> mamdani.addRule(Rule.parse(r, engine))); return mamdani; } @@ -116,7 +117,11 @@ public class FuzzyInferenceService { public List getAssessmentsByForecastTendencies(Integer branchId) { List timeSeries = timeSeriesService.getByBranch(branchId); List dbRules = ruleService.getList(); - return getAssessmentsByTimeSeriesTendencies(dbRules, timeSeries); + try { + return getAssessmentsByTimeSeriesTendencies(dbRules, timeSeries); + } catch (AssessmentException ex) { + return new ArrayList<>(); + } } public List getAssessmentsByLastValues(Integer branchId) { @@ -130,7 +135,7 @@ public class FuzzyInferenceService { List antecedentValues = Stream.concat(dbRules.stream().map(DbRule::getFirstAntecedentValue), dbRules.stream().map(DbRule::getSecondAntecedentValue)).distinct().collect(Collectors.toList()); List consequentValues = dbRules.stream().map(DbRule::getId).collect(Collectors.toList()); - engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues, consequentValues)); + engine.addRuleBlock(getRuleBlock(engine, dbRules, variableValues, antecedentValues, consequentValues)); String consequent = getConsequent(engine, variableValues); if (consequent.equals(NO_RESULT)) { return new ArrayList<>(); @@ -142,12 +147,30 @@ public class FuzzyInferenceService { .collect(Collectors.toList()); } - private List getAssessmentsByTimeSeriesTendencies(List dbRules, List timeSeries) { + private List getSingleAssessmentByTimeSeriesTendencies(List dbRules, List timeSeries) throws AssessmentException { Map variableValues = new HashMap<>(); - timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService.getLastTimeSeriesTendency(ts))); + timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), + timeSeriesService.getLastTimeSeriesTendency(ts) + .orElseThrow(() -> new AssessmentException("")))); return getFuzzyInference(dbRules, variableValues); } + private List getAssessmentsByTimeSeriesTendencies(List dbRules, List timeSeries) { + return dbRules + .stream() + .flatMap(dbRule -> { + Map variableValues = new HashMap<>(); + timeSeries + .stream() + .filter(ts -> ts.getTimeSeriesType() == dbRule.getFirstAntecedent() + || ts.getTimeSeriesType() == dbRule.getSecondAntecedent()) + .forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService + .getLastTimeSeriesTendency(ts) + .orElseThrow(() -> new AssessmentException("")))); + return getFuzzyInference(List.of(dbRule), variableValues).stream(); + }).collect(Collectors.toList()); + } + private List getAssessmentsByLastValues(List dbRules, List timeSeries) { Map variableValues = new HashMap<>(); timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), ts.getValues().get(ts.getValues().size() - 1).getValue())); diff --git a/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java b/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java index 3f8f6cd..4cc2de9 100644 --- a/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java @@ -115,16 +115,16 @@ public class TimeSeriesService { return timeSeriesRepository.getTimeSeriesByBranchId(branchId); } - public Double getLastTimeSeriesTendency(TimeSeries ts) { + public Optional getLastTimeSeriesTendency(TimeSeries ts) { if (ts != null && ts.getValues().size() > 5) { JSONObject response = httpService.post(TIME_SERIES_TENDENCY_URL, new JSONObject(new SmoothingTimeSeries(ts))); LOG.debug("Успешно отправлен на сервис сглаживания"); if (response.has("response") && response.getString("response").equals("empty")) { - return 0.0; + return Optional.empty(); } JSONArray jsonArray = response.getJSONObject("timeSeries").getJSONArray("values"); - return jsonArray.getJSONObject(jsonArray.length() - 1).getDouble("value"); + return Optional.of(jsonArray.getJSONObject(jsonArray.length() - 1).getDouble("value")); } - return 0.0; + return Optional.empty(); } }