Merge pull request 'list of assessments' (#85) from 84-list-of-consequents into master

Reviewed-on: #85
This commit is contained in:
romanov73 2023-02-02 13:05:27 +04:00
commit 09ce44c33e
9 changed files with 152 additions and 39 deletions

View File

@ -1,39 +1,39 @@
package ru.ulstu.extractor.recommendation.controller; package ru.ulstu.extractor.assessment.controller;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; 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.branch.service.BranchService;
import ru.ulstu.extractor.recommendation.model.FilterBranchForm;
import ru.ulstu.extractor.rule.service.FuzzyInferenceService; import ru.ulstu.extractor.rule.service.FuzzyInferenceService;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
import java.util.Optional; import java.util.Optional;
import static ru.ulstu.extractor.core.Route.RECOMMENDATIONS; import static ru.ulstu.extractor.core.Route.ASSESSMENTS;
@Controller @Controller
@ApiIgnore @ApiIgnore
public class RecommendationController { public class AssessmentController {
private final FuzzyInferenceService fuzzyInferenceService; private final FuzzyInferenceService fuzzyInferenceService;
private final BranchService branchService; private final BranchService branchService;
public RecommendationController(FuzzyInferenceService fuzzyInferenceService, public AssessmentController(FuzzyInferenceService fuzzyInferenceService,
BranchService branchService) { BranchService branchService) {
this.fuzzyInferenceService = fuzzyInferenceService; this.fuzzyInferenceService = fuzzyInferenceService;
this.branchService = branchService; this.branchService = branchService;
} }
@GetMapping(RECOMMENDATIONS) @GetMapping(ASSESSMENTS)
public String getRecommendations(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("recommendations", fuzzyInferenceService.getRecommendations(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());
} }
return RECOMMENDATIONS; return ASSESSMENTS;
} }
} }

View File

@ -0,0 +1,40 @@
package ru.ulstu.extractor.assessment.model;
import ru.ulstu.extractor.rule.model.DbRule;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
public class Assessment {
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;
}
public TimeSeriesType getFirstAntecedent() {
return firstAntecedent;
}
public String getFirstAntecedentTendency() {
return firstAntecedentTendency;
}
public TimeSeriesType getSecondAntecedent() {
return secondAntecedent;
}
public String getSecondAntecedentTendency() {
return secondAntecedentTendency;
}
}

View File

@ -1,4 +1,4 @@
package ru.ulstu.extractor.recommendation.model; package ru.ulstu.extractor.assessment.model;
public class FilterBranchForm { public class FilterBranchForm {
private Integer branchId; private Integer branchId;

View File

@ -19,7 +19,7 @@ public class Route {
public static final String STATISTIC = "statistic"; public static final String STATISTIC = "statistic";
public static final String LIST_RULE = "listRules"; public static final String LIST_RULE = "listRules";
public static final String ADD_RULE = "addRule"; 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 final String DELETE_RULE = "deleteRule";
public static String getLIST_INDEXED_REPOSITORIES() { public static String getLIST_INDEXED_REPOSITORIES() {
@ -42,7 +42,7 @@ public class Route {
return STATISTIC; return STATISTIC;
} }
public static String getRECOMMENDATIONS() { public static String getASSESSMENTS() {
return RECOMMENDATIONS; return ASSESSMENTS;
} }
} }

View File

@ -0,0 +1,7 @@
package ru.ulstu.extractor.rule.model;
public class AssessmentException extends RuntimeException {
public AssessmentException(String message) {
super(message);
}
}

View File

@ -12,16 +12,20 @@ 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.AssessmentException;
import ru.ulstu.extractor.rule.model.DbRule; import ru.ulstu.extractor.rule.model.DbRule;
import ru.ulstu.extractor.ts.model.TimeSeries; import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.service.TimeSeriesService; import ru.ulstu.extractor.ts.service.TimeSeriesService;
import java.util.ArrayList;
import java.util.HashMap; 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 {
@ -29,6 +33,7 @@ public class FuzzyInferenceService {
private final static String RULE_TEMPLATE = "if %s is %s and %s is %s then " private final static String RULE_TEMPLATE = "if %s is %s and %s is %s then "
+ OUTPUT_VARIABLE_NAME + OUTPUT_VARIABLE_NAME
+ " is %s"; + " is %s";
private final static String NO_RESULT = "Нет результата";
private final DbRuleService ruleService; private final DbRuleService ruleService;
private final AntecedentValueService antecedentValueService; private final AntecedentValueService antecedentValueService;
private final GitRepositoryService gitRepositoryService; private final GitRepositoryService gitRepositoryService;
@ -44,10 +49,9 @@ public class FuzzyInferenceService {
this.timeSeriesService = timeSeriesService; this.timeSeriesService = timeSeriesService;
} }
public List<String> getRulesFromDb(Map<String, Double> variableValues) { public List<String> getRulesFromDb(List<DbRule> dbRules, Map<String, Double> variableValues) {
List<DbRule> dbDbRules = ruleService.getList(); validateVariables(variableValues, dbRules);
validateVariables(variableValues, dbDbRules); return dbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
return dbDbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
} }
private String getFuzzyRule(DbRule dbRule) { private String getFuzzyRule(DbRule dbRule) {
@ -56,13 +60,14 @@ 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,
List<DbRule> dbRules,
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 +91,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);
@ -98,7 +103,7 @@ public class FuzzyInferenceService {
mamdani.setDisjunction(new BoundedSum()); mamdani.setDisjunction(new BoundedSum());
mamdani.setImplication(new AlgebraicProduct()); mamdani.setImplication(new AlgebraicProduct());
mamdani.setActivation(new Highest()); 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; return mamdani;
} }
@ -109,15 +114,67 @@ public class FuzzyInferenceService {
return engine; return engine;
} }
public String getRecommendations(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();
try {
return getAssessmentsByTimeSeriesTendencies(dbRules, timeSeries);
} catch (AssessmentException ex) {
return new ArrayList<>();
}
}
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, dbRules, 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)))
.map(Assessment::new)
.collect(Collectors.toList());
}
private List<Assessment> getSingleAssessmentByTimeSeriesTendencies(List<DbRule> dbRules, List<TimeSeries> timeSeries) throws AssessmentException {
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(),
engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues, consequentValues)); timeSeriesService.getLastTimeSeriesTendency(ts)
return getConsequent(engine, variableValues); .orElseThrow(() -> new AssessmentException(""))));
return getFuzzyInference(dbRules, variableValues);
}
private List<Assessment> getAssessmentsByTimeSeriesTendencies(List<DbRule> dbRules, List<TimeSeries> timeSeries) {
return dbRules
.stream()
.flatMap(dbRule -> {
Map<String, Double> 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<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 +201,7 @@ public class FuzzyInferenceService {
outputVariable.defuzzify(); outputVariable.defuzzify();
} }
return (outputVariable == null || Double.isNaN(outputVariable.getValue())) return (outputVariable == null || Double.isNaN(outputVariable.getValue()))
? "Нет рекомендаций" ? NO_RESULT
: outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName(); : outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName();
} }
} }

View File

@ -115,16 +115,16 @@ public class TimeSeriesService {
return timeSeriesRepository.getTimeSeriesByBranchId(branchId); return timeSeriesRepository.getTimeSeriesByBranchId(branchId);
} }
public Double getLastTimeSeriesTendency(TimeSeries ts) { public Optional<Double> getLastTimeSeriesTendency(TimeSeries ts) {
if (ts != null && ts.getValues().size() > 5) { if (ts != null && ts.getValues().size() > 5) {
JSONObject response = httpService.post(TIME_SERIES_TENDENCY_URL, new JSONObject(new SmoothingTimeSeries(ts))); JSONObject response = httpService.post(TIME_SERIES_TENDENCY_URL, new JSONObject(new SmoothingTimeSeries(ts)));
LOG.debug("Успешно отправлен на сервис сглаживания"); LOG.debug("Успешно отправлен на сервис сглаживания");
if (response.has("response") && response.getString("response").equals("empty")) { if (response.has("response") && response.getString("response").equals("empty")) {
return 0.0; return Optional.empty();
} }
JSONArray jsonArray = response.getJSONObject("timeSeries").getJSONArray("values"); 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();
} }
} }

View File

@ -7,7 +7,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head> </head>
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<form action="#" th:action="${@route.RECOMMENDATIONS}" th:object="${filterBranchForm}" method="get"> <form action="#" th:action="${@route.ASSESSMENTS}" th:object="${filterBranchForm}" method="get">
<div class="row"> <div class="row">
<div class="col-md-2 col-sm-12"> <div class="col-md-2 col-sm-12">
Репозиторий-ветка Репозиторий-ветка
@ -26,17 +26,26 @@
]) ])
; ;
$('#select-branch').selectpicker('refresh'); $('#select-branch').selectpicker('refresh');
</script> </script>
</div> </div>
<input type="submit" class="btn btn-outline-success w-100" value="Применить фильтр"/> <input type="submit" class="btn btn-outline-success w-100" value="Применить фильтр"/>
</div> </div>
<div th:if="*{branchId == null}">Выбрерите ветку для получения рекомендаций</div> <div th:if="*{branchId == null}">Выбрерите ветку для получения оценки репозитория</div>
<input type="hidden" th:field="*{branchId}"> <input type="hidden" th:field="*{branchId}">
</form> </form>
<div th:each="recommendation: ${recommendations}"> <div th:if="${assessments != null && #lists.size(assessments) > 0}">
<div th:text="${recommendation}"></div> <h5>Состояние репозитория описывается следующими выражениями:</h5>
<div th:each="assessment: ${assessments}">
<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 th:if="${assessments != null && #lists.size(assessments) == 0}">
<h5>Нет результатов</h5>
</div> </div>
</div> </div>
</html> </html>

View File

@ -41,7 +41,7 @@
<a class="nav-link" href="/listRules" th:text="Правила">Link</a> <a class="nav-link" href="/listRules" th:text="Правила">Link</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/recommendations" th:text="Рекомендации">Link</a> <a class="nav-link" href="/assessments" th:text="Рекомендации">Link</a>
</li> </li>
</ul> </ul>
</div> </div>