#84 -- fix output

This commit is contained in:
Anton Romanov 2023-02-02 00:50:57 +04:00
parent 14c17284bd
commit 6bb39cc4d6
4 changed files with 59 additions and 26 deletions

View File

@ -29,7 +29,7 @@ public class AssessmentController {
public String getAssessments(Model model, @RequestParam Optional<Integer> branchId) { public String getAssessments(Model model, @RequestParam Optional<Integer> branchId) {
model.addAttribute("branches", branchService.findAll()); model.addAttribute("branches", branchService.findAll());
if (branchId.isPresent()) { if (branchId.isPresent()) {
model.addAttribute("assessments", fuzzyInferenceService.getAssessments(branchId.get())); model.addAttribute("assessments", fuzzyInferenceService.getAssessmentsByForecastTendencies(branchId.get()));
model.addAttribute("filterBranchForm", new FilterBranchForm(branchId.get())); model.addAttribute("filterBranchForm", new FilterBranchForm(branchId.get()));
} else { } else {
model.addAttribute("filterBranchForm", new FilterBranchForm()); model.addAttribute("filterBranchForm", new FilterBranchForm());

View File

@ -1,13 +1,22 @@
package ru.ulstu.extractor.assessment.model; package ru.ulstu.extractor.assessment.model;
import ru.ulstu.extractor.rule.model.DbRule;
import ru.ulstu.extractor.ts.model.TimeSeriesType; import ru.ulstu.extractor.ts.model.TimeSeriesType;
public class Assessment { public class Assessment {
private String consequent; private final String consequent;
private TimeSeriesType firstAntecedent; private final TimeSeriesType firstAntecedent;
private String firstAntecedentTendency; private final String firstAntecedentTendency;
private TimeSeriesType secondAntecedent; private final TimeSeriesType secondAntecedent;
private String secondAntecedentTendency; 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() { public String getConsequent() {
return consequent; return consequent;

View File

@ -12,6 +12,7 @@ import com.fuzzylite.term.Triangle;
import com.fuzzylite.variable.InputVariable; import com.fuzzylite.variable.InputVariable;
import com.fuzzylite.variable.OutputVariable; import com.fuzzylite.variable.OutputVariable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.extractor.assessment.model.Assessment;
import ru.ulstu.extractor.gitrepository.service.GitRepositoryService; import ru.ulstu.extractor.gitrepository.service.GitRepositoryService;
import ru.ulstu.extractor.rule.model.AntecedentValue; import ru.ulstu.extractor.rule.model.AntecedentValue;
import ru.ulstu.extractor.rule.model.DbRule; import ru.ulstu.extractor.rule.model.DbRule;
@ -22,6 +23,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service @Service
public class FuzzyInferenceService { public class FuzzyInferenceService {
@ -56,13 +58,13 @@ public class FuzzyInferenceService {
dbRule.getFirstAntecedentValue().getAntecedentValue(), dbRule.getFirstAntecedentValue().getAntecedentValue(),
dbRule.getSecondAntecedent().name(), dbRule.getSecondAntecedent().name(),
dbRule.getSecondAntecedentValue().getAntecedentValue(), dbRule.getSecondAntecedentValue().getAntecedentValue(),
dbRule.getConsequent().replaceAll(" ", "_")); dbRule.getId());
} }
private RuleBlock getRuleBlock(Engine engine, private RuleBlock getRuleBlock(Engine engine,
Map<String, Double> variableValues, Map<String, Double> variableValues,
List<AntecedentValue> antecedentValues, List<AntecedentValue> antecedentValues,
List<String> consequentValues) { List<Integer> consequentValues) {
variableValues.forEach((key, value) -> { variableValues.forEach((key, value) -> {
InputVariable input = new InputVariable(); InputVariable input = new InputVariable();
input.setName(key); input.setName(key);
@ -86,7 +88,7 @@ public class FuzzyInferenceService {
output.setDefaultValue(Double.NaN); output.setDefaultValue(Double.NaN);
output.setLockValueInRange(false); output.setLockValueInRange(false);
for (int i = 0; i < consequentValues.size(); i++) { 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); engine.addOutputVariable(output);
@ -109,15 +111,42 @@ public class FuzzyInferenceService {
return engine; return engine;
} }
public String getAssessments(Integer branchId) { public List<Assessment> getAssessmentsByForecastTendencies(Integer branchId) {
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(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(); Engine engine = getFuzzyEngine();
List<AntecedentValue> antecedentValues = antecedentValueService.getList(); List<AntecedentValue> antecedentValues = Stream.concat(dbRules.stream().map(DbRule::getFirstAntecedentValue),
List<String> consequentValues = ruleService.getConsequentList(); 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<>(); Map<String, Double> 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)));
engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues, consequentValues)); return getFuzzyInference(dbRules, variableValues);
return getConsequent(engine, 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) { private void validateVariables(Map<String, Double> variableValues, List<DbRule> dbDbRules) {
@ -144,7 +173,7 @@ public class FuzzyInferenceService {
outputVariable.defuzzify(); outputVariable.defuzzify();
} }
return (outputVariable == null || Double.isNaN(outputVariable.getValue())) return (outputVariable == null || Double.isNaN(outputVariable.getValue()))
? "Нет рекомендаций" ? "Нет результата"
: outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName(); : outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName();
} }
} }

View File

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