|
|
|
@ -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)
|
|
|
|
|
.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(), timeSeriesService.getLastTimeSeriesTendency(ts)));
|
|
|
|
|
engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues, consequentValues));
|
|
|
|
|
return getConsequent(engine, variableValues);
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|