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