Merge pull request 'Декомпозиция' (#87) from 86-bl-decomposition into master
Reviewed-on: #87pull/88/head
commit
5f9b91fd87
@ -0,0 +1,134 @@
|
|||||||
|
package ru.ulstu.extractor.assessment.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.ulstu.extractor.assessment.model.Assessment;
|
||||||
|
import ru.ulstu.extractor.rule.model.AssessmentException;
|
||||||
|
import ru.ulstu.extractor.rule.model.DbRule;
|
||||||
|
import ru.ulstu.extractor.rule.service.AntecedentValueService;
|
||||||
|
import ru.ulstu.extractor.rule.service.DbRuleService;
|
||||||
|
import ru.ulstu.extractor.rule.service.FuzzyInferenceService;
|
||||||
|
import ru.ulstu.extractor.ts.model.TimeSeries;
|
||||||
|
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
|
||||||
|
import ru.ulstu.extractor.ts.service.TimeSeriesService;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AssessmentService {
|
||||||
|
private final DbRuleService ruleService;
|
||||||
|
private final AntecedentValueService antecedentValueService;
|
||||||
|
private final TimeSeriesService timeSeriesService;
|
||||||
|
private final FuzzyInferenceService fuzzyInferenceService;
|
||||||
|
|
||||||
|
public AssessmentService(DbRuleService ruleService,
|
||||||
|
AntecedentValueService antecedentValueService,
|
||||||
|
TimeSeriesService timeSeriesService,
|
||||||
|
FuzzyInferenceService fuzzyInferenceService) {
|
||||||
|
this.ruleService = ruleService;
|
||||||
|
this.antecedentValueService = antecedentValueService;
|
||||||
|
this.timeSeriesService = timeSeriesService;
|
||||||
|
this.fuzzyInferenceService = fuzzyInferenceService;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
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> 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)
|
||||||
|
.orElseThrow(() -> new AssessmentException(""))));
|
||||||
|
return fuzzyInferenceService.getFuzzyInference(dbRules,
|
||||||
|
antecedentValueService.getList(),
|
||||||
|
variableValues,
|
||||||
|
getTSsMin(timeSeries),
|
||||||
|
getTSsMax(timeSeries));
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
.orElse(ts.getValues().get(ts.getValues().size() - 1).getValue())));
|
||||||
|
return fuzzyInferenceService.getFuzzyInference(List.of(dbRule),
|
||||||
|
antecedentValueService.getList(),
|
||||||
|
variableValues,
|
||||||
|
getTSsMin(timeSeries),
|
||||||
|
getTSsMax(timeSeries)).stream();
|
||||||
|
})
|
||||||
|
.sorted(Comparator.comparing(Assessment::getDegree))
|
||||||
|
.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 fuzzyInferenceService.getFuzzyInference(dbRules,
|
||||||
|
antecedentValueService.getList(),
|
||||||
|
variableValues,
|
||||||
|
getTSsMin(timeSeries),
|
||||||
|
getTSsMax(timeSeries));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Double getMin(List<Double> values) {
|
||||||
|
return values.stream().mapToDouble(v -> v).min().getAsDouble();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map.Entry<String, Double> getTSMin(TimeSeries ts) {
|
||||||
|
return Map.entry(ts.getTimeSeriesType().name(),
|
||||||
|
getMin(ts.getValues().stream().map(TimeSeriesValue::getValue).collect(Collectors.toList())));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Double> getTSsMin(List<TimeSeries> tss) {
|
||||||
|
Map<String, Double> res = new HashMap<>();
|
||||||
|
tss.forEach(ts -> {
|
||||||
|
Map.Entry<String, Double> entry = getTSMin(ts);
|
||||||
|
res.put(entry.getKey(), entry.getValue());
|
||||||
|
});
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Double getMax(List<Double> values) {
|
||||||
|
return values.stream().mapToDouble(v -> v).max().getAsDouble();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map.Entry<String, Double> getTSMax(TimeSeries ts) {
|
||||||
|
return Map.entry(ts.getTimeSeriesType().name(),
|
||||||
|
getMax(ts.getValues().stream().map(TimeSeriesValue::getValue).collect(Collectors.toList())));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Double> getTSsMax(List<TimeSeries> tss) {
|
||||||
|
Map<String, Double> res = new HashMap<>();
|
||||||
|
tss.forEach(ts -> {
|
||||||
|
Map.Entry<String, Double> entry = getTSMax(ts);
|
||||||
|
res.put(entry.getKey(), entry.getValue());
|
||||||
|
});
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue