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 @@
-

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

+
Состояние репозитория описывается следующими выражениями:
-
- вследствие тенденции -
- показателя -
- и тенденции -
- показателя -
- и + + вследствие тенденции '' показателя '' + и тенденции '' показателя '';