list of assessments #85

Merged
romanov73 merged 6 commits from 84-list-of-consequents into master 2023-02-02 13:05:28 +04:00
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.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;
import java.util.Optional;
import static ru.ulstu.extractor.core.Route.RECOMMENDATIONS;
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;
}
@GetMapping(RECOMMENDATIONS)
public String getRecommendations(Model model, @RequestParam Optional<Integer> branchId) {
@GetMapping(ASSESSMENTS)
public String getAssessments(Model model, @RequestParam Optional<Integer> branchId) {
model.addAttribute("branches", branchService.findAll());
if (branchId.isPresent()) {
model.addAttribute("recommendations", fuzzyInferenceService.getRecommendations(branchId.get()));
model.addAttribute("assessments", fuzzyInferenceService.getAssessmentsByForecastTendencies(branchId.get()));
model.addAttribute("filterBranchForm", new FilterBranchForm(branchId.get()));
} else {
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 {
private Integer branchId;

View File

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

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.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.AssessmentException;
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;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
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 "
+ OUTPUT_VARIABLE_NAME
+ " is %s";
private final static String NO_RESULT = "Нет результата";
private final DbRuleService ruleService;
private final AntecedentValueService antecedentValueService;
private final GitRepositoryService gitRepositoryService;
@ -44,10 +49,9 @@ public class FuzzyInferenceService {
this.timeSeriesService = timeSeriesService;
}
public List<String> getRulesFromDb(Map<String, Double> variableValues) {
List<DbRule> dbDbRules = ruleService.getList();
validateVariables(variableValues, dbDbRules);
return dbDbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
public List<String> getRulesFromDb(List<DbRule> dbRules, Map<String, Double> variableValues) {
validateVariables(variableValues, dbRules);
return dbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
}
private String getFuzzyRule(DbRule dbRule) {
@ -56,13 +60,14 @@ public class FuzzyInferenceService {
dbRule.getFirstAntecedentValue().getAntecedentValue(),
dbRule.getSecondAntecedent().name(),
dbRule.getSecondAntecedentValue().getAntecedentValue(),
dbRule.getConsequent().replaceAll(" ", "_"));
dbRule.getId());
}
private RuleBlock getRuleBlock(Engine engine,
List<DbRule> dbRules,
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 +91,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);
@ -98,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;
}
@ -109,15 +114,67 @@ public class FuzzyInferenceService {
return engine;
}
public String getRecommendations(Integer branchId) {
public List<Assessment> getAssessmentsByForecastTendencies(Integer 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();
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, 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<>();
timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService.getLastTimeSeriesTendency(ts)));
engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues, consequentValues));
return getConsequent(engine, variableValues);
timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(),
timeSeriesService.getLastTimeSeriesTendency(ts)
.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) {
@ -144,7 +201,7 @@ public class FuzzyInferenceService {
outputVariable.defuzzify();
}
return (outputVariable == null || Double.isNaN(outputVariable.getValue()))
? "Нет рекомендаций"
? NO_RESULT
: outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName();
}
}

View File

@ -115,16 +115,16 @@ public class TimeSeriesService {
return timeSeriesRepository.getTimeSeriesByBranchId(branchId);
}
public Double getLastTimeSeriesTendency(TimeSeries ts) {
public Optional<Double> 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();
}
}

View File

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

View File

@ -41,7 +41,7 @@
<a class="nav-link" href="/listRules" th:text="Правила">Link</a>
</li>
<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>
</ul>
</div>