list of assessments #85

Merged
romanov73 merged 6 commits from 84-list-of-consequents into master 2023-02-02 13:05:28 +04:00
4 changed files with 59 additions and 26 deletions
Showing only changes of commit 6bb39cc4d6 - Show all commits

View File

@ -29,7 +29,7 @@ public class AssessmentController {
public String getAssessments(Model model, @RequestParam Optional<Integer> 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());

View File

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

View File

@ -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<String, Double> variableValues,
List<AntecedentValue> antecedentValues,
List<String> consequentValues) {
List<Integer> 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<Assessment> getAssessmentsByForecastTendencies(Integer branchId) {
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
List<DbRule> dbRules = ruleService.getList();
return getAssessmentsByTimeSeriesTendencies(dbRules, timeSeries);
}
public List<Assessment> getAssessmentsByLastValues(Integer branchId) {
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
List<DbRule> dbRules = ruleService.getList();
return getAssessmentsByLastValues(dbRules, timeSeries);
}
private List<Assessment> getFuzzyInference(List<DbRule> dbRules, Map<String, Double> variableValues) {
Engine engine = getFuzzyEngine();
List<AntecedentValue> antecedentValues = antecedentValueService.getList();
List<String> consequentValues = ruleService.getConsequentList();
List<AntecedentValue> antecedentValues = Stream.concat(dbRules.stream().map(DbRule::getFirstAntecedentValue),
dbRules.stream().map(DbRule::getSecondAntecedentValue)).distinct().collect(Collectors.toList());
List<Integer> 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<Assessment> getAssessmentsByTimeSeriesTendencies(List<DbRule> dbRules, List<TimeSeries> timeSeries) {
Map<String, Double> 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<Assessment> getAssessmentsByLastValues(List<DbRule> dbRules, List<TimeSeries> timeSeries) {
Map<String, Double> 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<String, Double> variableValues, List<DbRule> dbDbRules) {
@ -144,7 +173,7 @@ public class FuzzyInferenceService {
outputVariable.defuzzify();
}
return (outputVariable == null || Double.isNaN(outputVariable.getValue()))
? "Нет рекомендаций"
? "Нет результата"
: outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName();
}
}

View File

@ -35,18 +35,13 @@
<input type="hidden" th:field="*{branchId}">
</form>
<div th:if="${assessments != null}">
<h3>Состояние репозитория описывается следующими выражениями:</h3>
<h5>Состояние репозитория описывается следующими выражениями:</h5>
<div th:each="assessment: ${assessments}">
<div th:text="${assessment.consequent}"></div>
вследствие тенденции
<div th:text="${assessment.firstAntecedentTendency}"></div>
показателя
<div th:text="${assessment.firstAntecedent}"></div>
и тенденции
<div th:text="${assessment.secondAntecedentTendency}"></div>
показателя
<div th:text="${assessment.secondAntecedent}"></div>
и
<span th:text="${assessment.consequent}"></span>
вследствие тенденции '<span th:text="${assessment.firstAntecedentTendency}"></span>' показателя '<span
th:text="${assessment.firstAntecedent.description}"></span>'
и тенденции '<span th:text="${assessment.secondAntecedentTendency}"></span>' показателя '<span
th:text="${assessment.secondAntecedent.description}"></span>';
</div>
</div>
</div>