From f76976899d7a0c8ddc7f30664f9899fd0408731d Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 1 Feb 2023 22:39:40 +0400 Subject: [PATCH 1/6] #84 -- change page route and semantic --- .../java/ru/ulstu/extractor/core/Route.java | 6 ++-- .../controller/RecommendationController.java | 8 ++--- .../recommendation/model/Assessment.java | 31 +++++++++++++++++++ .../rule/service/FuzzyInferenceService.java | 2 +- ...{recommendations.html => assessments.html} | 0 5 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/recommendation/model/Assessment.java rename src/main/resources/templates/{recommendations.html => assessments.html} (100%) diff --git a/src/main/java/ru/ulstu/extractor/core/Route.java b/src/main/java/ru/ulstu/extractor/core/Route.java index 614bd69..be9cb53 100644 --- a/src/main/java/ru/ulstu/extractor/core/Route.java +++ b/src/main/java/ru/ulstu/extractor/core/Route.java @@ -19,7 +19,7 @@ public class Route { public static final String STATISTIC = "statistic"; public static final String LIST_RULE = "listRules"; public static final String ADD_RULE = "addRule"; - public static final String RECOMMENDATIONS = "recommendations"; + public static final String ASSESSMENTS = "assessments"; public static final String DELETE_RULE = "deleteRule"; public static String getLIST_INDEXED_REPOSITORIES() { @@ -42,7 +42,7 @@ public class Route { return STATISTIC; } - public static String getRECOMMENDATIONS() { - return RECOMMENDATIONS; + public static String getASSESSMENTS() { + return ASSESSMENTS; } } diff --git a/src/main/java/ru/ulstu/extractor/recommendation/controller/RecommendationController.java b/src/main/java/ru/ulstu/extractor/recommendation/controller/RecommendationController.java index 4c0f803..cb53532 100644 --- a/src/main/java/ru/ulstu/extractor/recommendation/controller/RecommendationController.java +++ b/src/main/java/ru/ulstu/extractor/recommendation/controller/RecommendationController.java @@ -11,7 +11,7 @@ import springfox.documentation.annotations.ApiIgnore; import java.util.Optional; -import static ru.ulstu.extractor.core.Route.RECOMMENDATIONS; +import static ru.ulstu.extractor.core.Route.ASSESSMENTS; @Controller @ApiIgnore @@ -25,15 +25,15 @@ public class RecommendationController { this.branchService = branchService; } - @GetMapping(RECOMMENDATIONS) + @GetMapping(ASSESSMENTS) public String getRecommendations(Model model, @RequestParam Optional branchId) { model.addAttribute("branches", branchService.findAll()); if (branchId.isPresent()) { - model.addAttribute("recommendations", fuzzyInferenceService.getRecommendations(branchId.get())); + model.addAttribute("assessments", fuzzyInferenceService.getAssessments(branchId.get())); model.addAttribute("filterBranchForm", new FilterBranchForm(branchId.get())); } else { model.addAttribute("filterBranchForm", new FilterBranchForm()); } - return RECOMMENDATIONS; + return ASSESSMENTS; } } diff --git a/src/main/java/ru/ulstu/extractor/recommendation/model/Assessment.java b/src/main/java/ru/ulstu/extractor/recommendation/model/Assessment.java new file mode 100644 index 0000000..7b9c213 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/recommendation/model/Assessment.java @@ -0,0 +1,31 @@ +package ru.ulstu.extractor.recommendation.model; + +import ru.ulstu.extractor.ts.model.TimeSeriesType; + +public class Assessment { + private String consequent; + private TimeSeriesType firstAntecedent; + private String firstAntecedentTendency; + private TimeSeriesType secondAntecedent; + private String secondAntecedentTendency; + + public String getConsequent() { + return consequent; + } + + public TimeSeriesType getFirstAntecedent() { + return firstAntecedent; + } + + public String getFirstAntecedentTendency() { + return firstAntecedentTendency; + } + + public TimeSeriesType getSecondAntecedent() { + return secondAntecedent; + } + + public String getSecondAntecedentTendency() { + return secondAntecedentTendency; + } +} 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 706f55c..ce87a89 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java @@ -109,7 +109,7 @@ public class FuzzyInferenceService { return engine; } - public String getRecommendations(Integer branchId) { + public String getAssessments(Integer branchId) { List timeSeries = timeSeriesService.getByBranch(branchId); Engine engine = getFuzzyEngine(); List antecedentValues = antecedentValueService.getList(); diff --git a/src/main/resources/templates/recommendations.html b/src/main/resources/templates/assessments.html similarity index 100% rename from src/main/resources/templates/recommendations.html rename to src/main/resources/templates/assessments.html From 13ff304369cd2f46ee437694ea8e9ca5e56108f6 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 1 Feb 2023 22:50:04 +0400 Subject: [PATCH 2/6] #84 -- change page content --- .../controller/RecommendationController.java | 2 +- src/main/resources/templates/assessments.html | 21 ++++++++++++++----- src/main/resources/templates/default.html | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/recommendation/controller/RecommendationController.java b/src/main/java/ru/ulstu/extractor/recommendation/controller/RecommendationController.java index cb53532..750ef57 100644 --- a/src/main/java/ru/ulstu/extractor/recommendation/controller/RecommendationController.java +++ b/src/main/java/ru/ulstu/extractor/recommendation/controller/RecommendationController.java @@ -26,7 +26,7 @@ public class RecommendationController { } @GetMapping(ASSESSMENTS) - public String getRecommendations(Model model, @RequestParam Optional branchId) { + public String getAssessments(Model model, @RequestParam Optional branchId) { model.addAttribute("branches", branchService.findAll()); if (branchId.isPresent()) { model.addAttribute("assessments", fuzzyInferenceService.getAssessments(branchId.get())); diff --git a/src/main/resources/templates/assessments.html b/src/main/resources/templates/assessments.html index 7d43881..c2cbb47 100644 --- a/src/main/resources/templates/assessments.html +++ b/src/main/resources/templates/assessments.html @@ -7,7 +7,7 @@
-
+
Репозиторий-ветка @@ -26,17 +26,28 @@ ]) ; $('#select-branch').selectpicker('refresh'); -
-
Выбрерите ветку для получения рекомендаций
+
Выбрерите ветку для получения оценки репозитория
-
-
+
+

Состояние репозитория описывается следующими выражениями:

+
+
+ вследствие тенденции +
+ показателя +
+ и тенденции +
+ показателя +
+ и +
diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index d118ca8..45c036d 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -41,7 +41,7 @@ Link
From 14c17284bdc8a033084825cc9960e3a68c5ceec4 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 1 Feb 2023 22:51:12 +0400 Subject: [PATCH 3/6] #84 -- rename package --- .../controller/AssessmentController.java} | 10 +++++----- .../model/Assessment.java | 2 +- .../model/FilterBranchForm.java | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) rename src/main/java/ru/ulstu/extractor/{recommendation/controller/RecommendationController.java => assessment/controller/AssessmentController.java} (80%) rename src/main/java/ru/ulstu/extractor/{recommendation => assessment}/model/Assessment.java (93%) rename src/main/java/ru/ulstu/extractor/{recommendation => assessment}/model/FilterBranchForm.java (87%) diff --git a/src/main/java/ru/ulstu/extractor/recommendation/controller/RecommendationController.java b/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java similarity index 80% rename from src/main/java/ru/ulstu/extractor/recommendation/controller/RecommendationController.java rename to src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java index 750ef57..9743f01 100644 --- a/src/main/java/ru/ulstu/extractor/recommendation/controller/RecommendationController.java +++ b/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java @@ -1,11 +1,11 @@ -package ru.ulstu.extractor.recommendation.controller; +package ru.ulstu.extractor.assessment.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; +import ru.ulstu.extractor.assessment.model.FilterBranchForm; import ru.ulstu.extractor.branch.service.BranchService; -import ru.ulstu.extractor.recommendation.model.FilterBranchForm; import ru.ulstu.extractor.rule.service.FuzzyInferenceService; import springfox.documentation.annotations.ApiIgnore; @@ -15,12 +15,12 @@ import static ru.ulstu.extractor.core.Route.ASSESSMENTS; @Controller @ApiIgnore -public class RecommendationController { +public class AssessmentController { private final FuzzyInferenceService fuzzyInferenceService; private final BranchService branchService; - public RecommendationController(FuzzyInferenceService fuzzyInferenceService, - BranchService branchService) { + public AssessmentController(FuzzyInferenceService fuzzyInferenceService, + BranchService branchService) { this.fuzzyInferenceService = fuzzyInferenceService; this.branchService = branchService; } diff --git a/src/main/java/ru/ulstu/extractor/recommendation/model/Assessment.java b/src/main/java/ru/ulstu/extractor/assessment/model/Assessment.java similarity index 93% rename from src/main/java/ru/ulstu/extractor/recommendation/model/Assessment.java rename to src/main/java/ru/ulstu/extractor/assessment/model/Assessment.java index 7b9c213..adc11d3 100644 --- a/src/main/java/ru/ulstu/extractor/recommendation/model/Assessment.java +++ b/src/main/java/ru/ulstu/extractor/assessment/model/Assessment.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.recommendation.model; +package ru.ulstu.extractor.assessment.model; import ru.ulstu.extractor.ts.model.TimeSeriesType; diff --git a/src/main/java/ru/ulstu/extractor/recommendation/model/FilterBranchForm.java b/src/main/java/ru/ulstu/extractor/assessment/model/FilterBranchForm.java similarity index 87% rename from src/main/java/ru/ulstu/extractor/recommendation/model/FilterBranchForm.java rename to src/main/java/ru/ulstu/extractor/assessment/model/FilterBranchForm.java index 0d331c3..280727b 100644 --- a/src/main/java/ru/ulstu/extractor/recommendation/model/FilterBranchForm.java +++ b/src/main/java/ru/ulstu/extractor/assessment/model/FilterBranchForm.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.recommendation.model; +package ru.ulstu.extractor.assessment.model; public class FilterBranchForm { private Integer branchId; From 6bb39cc4d6e6f285a5c5c3c53e590db7ffb1e67a Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 2 Feb 2023 00:50:57 +0400 Subject: [PATCH 4/6] #84 -- fix output --- .../controller/AssessmentController.java | 2 +- .../assessment/model/Assessment.java | 19 ++++++-- .../rule/service/FuzzyInferenceService.java | 47 +++++++++++++++---- src/main/resources/templates/assessments.html | 17 +++---- 4 files changed, 59 insertions(+), 26 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java b/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java index 9743f01..3e3d3cc 100644 --- a/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java +++ b/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java @@ -29,7 +29,7 @@ public class AssessmentController { public String getAssessments(Model model, @RequestParam Optional branchId) { model.addAttribute("branches", branchService.findAll()); if (branchId.isPresent()) { - model.addAttribute("assessments", fuzzyInferenceService.getAssessments(branchId.get())); + model.addAttribute("assessments", fuzzyInferenceService.getAssessmentsByForecastTendencies(branchId.get())); model.addAttribute("filterBranchForm", new FilterBranchForm(branchId.get())); } else { model.addAttribute("filterBranchForm", new FilterBranchForm()); diff --git a/src/main/java/ru/ulstu/extractor/assessment/model/Assessment.java b/src/main/java/ru/ulstu/extractor/assessment/model/Assessment.java index adc11d3..f6609b4 100644 --- a/src/main/java/ru/ulstu/extractor/assessment/model/Assessment.java +++ b/src/main/java/ru/ulstu/extractor/assessment/model/Assessment.java @@ -1,13 +1,22 @@ package ru.ulstu.extractor.assessment.model; +import ru.ulstu.extractor.rule.model.DbRule; import ru.ulstu.extractor.ts.model.TimeSeriesType; public class Assessment { - private String consequent; - private TimeSeriesType firstAntecedent; - private String firstAntecedentTendency; - private TimeSeriesType secondAntecedent; - private String secondAntecedentTendency; + private final String consequent; + private final TimeSeriesType firstAntecedent; + private final String firstAntecedentTendency; + private final TimeSeriesType secondAntecedent; + private final String secondAntecedentTendency; + + public Assessment(DbRule dbRule) { + this.consequent = dbRule.getConsequent(); + this.firstAntecedent = dbRule.getFirstAntecedent(); + this.firstAntecedentTendency = dbRule.getFirstAntecedentValue().getAntecedentValue(); + this.secondAntecedent = dbRule.getSecondAntecedent(); + this.secondAntecedentTendency = dbRule.getSecondAntecedentValue().getAntecedentValue(); + } public String getConsequent() { return consequent; 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 ce87a89..9e0864b 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java @@ -12,6 +12,7 @@ import com.fuzzylite.term.Triangle; import com.fuzzylite.variable.InputVariable; import com.fuzzylite.variable.OutputVariable; 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.DbRule; @@ -22,6 +23,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.Stream; @Service public class FuzzyInferenceService { @@ -56,13 +58,13 @@ public class FuzzyInferenceService { dbRule.getFirstAntecedentValue().getAntecedentValue(), dbRule.getSecondAntecedent().name(), dbRule.getSecondAntecedentValue().getAntecedentValue(), - dbRule.getConsequent().replaceAll(" ", "_")); + dbRule.getId()); } private RuleBlock getRuleBlock(Engine engine, Map variableValues, List antecedentValues, - List consequentValues) { + List consequentValues) { variableValues.forEach((key, value) -> { InputVariable input = new InputVariable(); input.setName(key); @@ -86,7 +88,7 @@ public class FuzzyInferenceService { output.setDefaultValue(Double.NaN); output.setLockValueInRange(false); for (int i = 0; i < consequentValues.size(); i++) { - output.addTerm(new Triangle(consequentValues.get(i).replaceAll(" ", "_"), i - 0.1, i + 2.1)); + output.addTerm(new Triangle(consequentValues.get(i).toString(), i - 0.1, i + 2.1)); } engine.addOutputVariable(output); @@ -109,15 +111,42 @@ public class FuzzyInferenceService { return engine; } - public String getAssessments(Integer branchId) { + public List getAssessmentsByForecastTendencies(Integer branchId) { List timeSeries = timeSeriesService.getByBranch(branchId); + List dbRules = ruleService.getList(); + return getAssessmentsByTimeSeriesTendencies(dbRules, timeSeries); + } + + public List getAssessmentsByLastValues(Integer branchId) { + List timeSeries = timeSeriesService.getByBranch(branchId); + List dbRules = ruleService.getList(); + return getAssessmentsByLastValues(dbRules, timeSeries); + } + + private List getFuzzyInference(List dbRules, Map variableValues) { Engine engine = getFuzzyEngine(); - List antecedentValues = antecedentValueService.getList(); - List consequentValues = ruleService.getConsequentList(); + 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)); + String consequent = getConsequent(engine, variableValues); + return dbRules + .stream() + .filter(r -> r.getId().equals(Integer.valueOf(consequent))) + .map(Assessment::new) + .collect(Collectors.toList()); + } + + private List getAssessmentsByTimeSeriesTendencies(List dbRules, List timeSeries) { Map variableValues = new HashMap<>(); timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService.getLastTimeSeriesTendency(ts))); - engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues, consequentValues)); - return getConsequent(engine, variableValues); + return getFuzzyInference(dbRules, variableValues); + } + + 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())); + return getFuzzyInference(dbRules, variableValues); } private void validateVariables(Map variableValues, List dbDbRules) { @@ -144,7 +173,7 @@ public class FuzzyInferenceService { outputVariable.defuzzify(); } return (outputVariable == null || Double.isNaN(outputVariable.getValue())) - ? "Нет рекомендаций" + ? "Нет результата" : outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName(); } } diff --git a/src/main/resources/templates/assessments.html b/src/main/resources/templates/assessments.html index c2cbb47..013e37c 100644 --- a/src/main/resources/templates/assessments.html +++ b/src/main/resources/templates/assessments.html @@ -35,18 +35,13 @@
-

Состояние репозитория описывается следующими выражениями:

+
Состояние репозитория описывается следующими выражениями:
-
- вследствие тенденции -
- показателя -
- и тенденции -
- показателя -
- и + + вследствие тенденции '' показателя '' + и тенденции '' показателя '';
From 36e2b095685fb5d1912e5f2f65052cd1ab0459e3 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 2 Feb 2023 01:01:03 +0400 Subject: [PATCH 5/6] #84 -- fix integer cast --- .../extractor/rule/service/FuzzyInferenceService.java | 7 ++++++- src/main/resources/templates/assessments.html | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) 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 9e0864b..d17d112 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java @@ -19,6 +19,7 @@ import ru.ulstu.extractor.rule.model.DbRule; import ru.ulstu.extractor.ts.model.TimeSeries; import ru.ulstu.extractor.ts.service.TimeSeriesService; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -31,6 +32,7 @@ public class FuzzyInferenceService { private final static String RULE_TEMPLATE = "if %s is %s and %s is %s then " + OUTPUT_VARIABLE_NAME + " is %s"; + private final static String NO_RESULT = "Нет результата"; private final DbRuleService ruleService; private final AntecedentValueService antecedentValueService; private final GitRepositoryService gitRepositoryService; @@ -130,6 +132,9 @@ public class FuzzyInferenceService { List consequentValues = dbRules.stream().map(DbRule::getId).collect(Collectors.toList()); engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues, consequentValues)); String consequent = getConsequent(engine, variableValues); + if (consequent.equals(NO_RESULT)) { + return new ArrayList<>(); + } return dbRules .stream() .filter(r -> r.getId().equals(Integer.valueOf(consequent))) @@ -173,7 +178,7 @@ public class FuzzyInferenceService { outputVariable.defuzzify(); } return (outputVariable == null || Double.isNaN(outputVariable.getValue())) - ? "Нет результата" + ? NO_RESULT : outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName(); } } diff --git a/src/main/resources/templates/assessments.html b/src/main/resources/templates/assessments.html index 013e37c..516628a 100644 --- a/src/main/resources/templates/assessments.html +++ b/src/main/resources/templates/assessments.html @@ -34,7 +34,7 @@ -
+
Состояние репозитория описывается следующими выражениями:
@@ -44,5 +44,8 @@ th:text="${assessment.secondAntecedent.description}">';
+
+
Нет результатов
+
From e4d32a3e92513fc9b20af837c1d8c4b94dd32eb8 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 2 Feb 2023 13:02:13 +0400 Subject: [PATCH 6/6] #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(); } }