Декомпозиция #87
@ -5,8 +5,8 @@ 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.assessment.model.FilterBranchForm;
|
||||||
|
import ru.ulstu.extractor.assessment.service.AssessmentService;
|
||||||
import ru.ulstu.extractor.branch.service.BranchService;
|
import ru.ulstu.extractor.branch.service.BranchService;
|
||||||
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;
|
||||||
@ -16,12 +16,12 @@ import static ru.ulstu.extractor.core.Route.ASSESSMENTS;
|
|||||||
@Controller
|
@Controller
|
||||||
@ApiIgnore
|
@ApiIgnore
|
||||||
public class AssessmentController {
|
public class AssessmentController {
|
||||||
private final FuzzyInferenceService fuzzyInferenceService;
|
private final AssessmentService assessmentService;
|
||||||
private final BranchService branchService;
|
private final BranchService branchService;
|
||||||
|
|
||||||
public AssessmentController(FuzzyInferenceService fuzzyInferenceService,
|
public AssessmentController(AssessmentService assessmentService,
|
||||||
BranchService branchService) {
|
BranchService branchService) {
|
||||||
this.fuzzyInferenceService = fuzzyInferenceService;
|
this.assessmentService = assessmentService;
|
||||||
this.branchService = branchService;
|
this.branchService = branchService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ public class AssessmentController {
|
|||||||
public String getAssessments(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("assessments", fuzzyInferenceService.getAssessmentsByForecastTendencies(branchId.get()));
|
model.addAttribute("assessments", assessmentService.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());
|
||||||
|
@ -9,13 +9,15 @@ public class Assessment {
|
|||||||
private final String firstAntecedentTendency;
|
private final String firstAntecedentTendency;
|
||||||
private final TimeSeriesType secondAntecedent;
|
private final TimeSeriesType secondAntecedent;
|
||||||
private final String secondAntecedentTendency;
|
private final String secondAntecedentTendency;
|
||||||
|
private final Double degree;
|
||||||
|
|
||||||
public Assessment(DbRule dbRule) {
|
public Assessment(DbRule dbRule, Double degree) {
|
||||||
this.consequent = dbRule.getConsequent();
|
this.consequent = dbRule.getConsequent();
|
||||||
this.firstAntecedent = dbRule.getFirstAntecedent();
|
this.firstAntecedent = dbRule.getFirstAntecedent();
|
||||||
this.firstAntecedentTendency = dbRule.getFirstAntecedentValue().getAntecedentValue();
|
this.firstAntecedentTendency = dbRule.getFirstAntecedentValue().getAntecedentValue();
|
||||||
this.secondAntecedent = dbRule.getSecondAntecedent();
|
this.secondAntecedent = dbRule.getSecondAntecedent();
|
||||||
this.secondAntecedentTendency = dbRule.getSecondAntecedentValue().getAntecedentValue();
|
this.secondAntecedentTendency = dbRule.getSecondAntecedentValue().getAntecedentValue();
|
||||||
|
this.degree = degree;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getConsequent() {
|
public String getConsequent() {
|
||||||
@ -37,4 +39,8 @@ public class Assessment {
|
|||||||
public String getSecondAntecedentTendency() {
|
public String getSecondAntecedentTendency() {
|
||||||
return secondAntecedentTendency;
|
return secondAntecedentTendency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Double getDegree() {
|
||||||
|
return degree;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -283,7 +283,6 @@ public class GitRepositoryService {
|
|||||||
List<FileChange> changes = new ArrayList<>();
|
List<FileChange> changes = new ArrayList<>();
|
||||||
String[] strings = output.split("\n");
|
String[] strings = output.split("\n");
|
||||||
Map<String, List<String>> filesContent = getFilesContent(strings);
|
Map<String, List<String>> filesContent = getFilesContent(strings);
|
||||||
System.out.println(filesContent);
|
|
||||||
for(Map.Entry<String, List<String>> fileSterings: filesContent.entrySet()) {
|
for(Map.Entry<String, List<String>> fileSterings: filesContent.entrySet()) {
|
||||||
FileChange fileChange = new FileChange();
|
FileChange fileChange = new FileChange();
|
||||||
fileChange.setFile(fileSterings.getKey());
|
fileChange.setFile(fileSterings.getKey());
|
||||||
|
@ -11,7 +11,8 @@ public class AntecedentValue extends BaseEntity {
|
|||||||
public AntecedentValue() {
|
public AntecedentValue() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public AntecedentValue(String antecedentValue) {
|
public AntecedentValue(Integer id, String antecedentValue) {
|
||||||
|
this.setId(id);
|
||||||
this.antecedentValue = antecedentValue;
|
this.antecedentValue = antecedentValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,61 +1,46 @@
|
|||||||
package ru.ulstu.extractor.rule.service;
|
package ru.ulstu.extractor.rule.service;
|
||||||
|
|
||||||
import com.fuzzylite.Engine;
|
import com.fuzzylite.Engine;
|
||||||
import com.fuzzylite.activation.Highest;
|
import com.fuzzylite.activation.General;
|
||||||
import com.fuzzylite.defuzzifier.Centroid;
|
import com.fuzzylite.defuzzifier.Centroid;
|
||||||
import com.fuzzylite.norm.s.BoundedSum;
|
|
||||||
import com.fuzzylite.norm.s.Maximum;
|
import com.fuzzylite.norm.s.Maximum;
|
||||||
import com.fuzzylite.norm.t.AlgebraicProduct;
|
import com.fuzzylite.norm.t.AlgebraicProduct;
|
||||||
|
import com.fuzzylite.norm.t.Minimum;
|
||||||
import com.fuzzylite.rule.Rule;
|
import com.fuzzylite.rule.Rule;
|
||||||
import com.fuzzylite.rule.RuleBlock;
|
import com.fuzzylite.rule.RuleBlock;
|
||||||
import com.fuzzylite.term.Triangle;
|
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.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ru.ulstu.extractor.assessment.model.Assessment;
|
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.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.service.TimeSeriesService;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
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;
|
|
||||||
|
import static java.lang.String.format;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class FuzzyInferenceService {
|
public class FuzzyInferenceService {
|
||||||
|
private final static Logger LOG = LoggerFactory.getLogger(FuzzyInferenceService.class);
|
||||||
private final static String OUTPUT_VARIABLE_NAME = "state";
|
private final static String OUTPUT_VARIABLE_NAME = "state";
|
||||||
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 static String NO_RESULT = "Нет результата";
|
||||||
private final DbRuleService ruleService;
|
|
||||||
private final AntecedentValueService antecedentValueService;
|
|
||||||
private final GitRepositoryService gitRepositoryService;
|
|
||||||
private final TimeSeriesService timeSeriesService;
|
|
||||||
|
|
||||||
public FuzzyInferenceService(DbRuleService ruleService,
|
private List<String> getRulesFromDb(List<DbRule> dbRules, Map<String, Double> variableValues) {
|
||||||
AntecedentValueService antecedentValueService,
|
|
||||||
GitRepositoryService gitRepositoryService,
|
|
||||||
TimeSeriesService timeSeriesService) {
|
|
||||||
this.ruleService = ruleService;
|
|
||||||
this.antecedentValueService = antecedentValueService;
|
|
||||||
this.gitRepositoryService = gitRepositoryService;
|
|
||||||
this.timeSeriesService = timeSeriesService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getRulesFromDb(List<DbRule> dbRules, Map<String, Double> variableValues) {
|
|
||||||
validateVariables(variableValues, dbRules);
|
validateVariables(variableValues, dbRules);
|
||||||
return dbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
|
return dbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getFuzzyRule(DbRule dbRule) {
|
private String getFuzzyRule(DbRule dbRule) {
|
||||||
return String.format(RULE_TEMPLATE,
|
return format(RULE_TEMPLATE,
|
||||||
dbRule.getFirstAntecedent().name(),
|
dbRule.getFirstAntecedent().name(),
|
||||||
dbRule.getFirstAntecedentValue().getAntecedentValue(),
|
dbRule.getFirstAntecedentValue().getAntecedentValue(),
|
||||||
dbRule.getSecondAntecedent().name(),
|
dbRule.getSecondAntecedent().name(),
|
||||||
@ -66,6 +51,8 @@ public class FuzzyInferenceService {
|
|||||||
private RuleBlock getRuleBlock(Engine engine,
|
private RuleBlock getRuleBlock(Engine engine,
|
||||||
List<DbRule> dbRules,
|
List<DbRule> dbRules,
|
||||||
Map<String, Double> variableValues,
|
Map<String, Double> variableValues,
|
||||||
|
Map<String, Double> min,
|
||||||
|
Map<String, Double> max,
|
||||||
List<AntecedentValue> antecedentValues,
|
List<AntecedentValue> antecedentValues,
|
||||||
List<Integer> consequentValues) {
|
List<Integer> consequentValues) {
|
||||||
variableValues.forEach((key, value) -> {
|
variableValues.forEach((key, value) -> {
|
||||||
@ -73,10 +60,19 @@ public class FuzzyInferenceService {
|
|||||||
input.setName(key);
|
input.setName(key);
|
||||||
input.setDescription("");
|
input.setDescription("");
|
||||||
input.setEnabled(true);
|
input.setEnabled(true);
|
||||||
input.setRange(-0.1, antecedentValues.size() + 1.1);
|
double delta = antecedentValues.size() > 1
|
||||||
|
? (max.get(key) - min.get(key)) / (antecedentValues.size() - 1)
|
||||||
|
: (max.get(key) - min.get(key));
|
||||||
|
input.setRange(min.get(key), max.get(key));
|
||||||
input.setLockValueInRange(false);
|
input.setLockValueInRange(false);
|
||||||
for (int i = 0; i < antecedentValues.size(); i++) {
|
for (int i = 0; i < antecedentValues.size(); i++) {
|
||||||
input.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i - 0.1, i + 2.1));
|
input.addTerm(
|
||||||
|
new Triangle(
|
||||||
|
antecedentValues.get(i).getAntecedentValue(),
|
||||||
|
min.get(key) + i * delta - 0.5 * delta,
|
||||||
|
min.get(key) + i * delta + delta + 0.5 * delta
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
engine.addInputVariable(input);
|
engine.addInputVariable(input);
|
||||||
});
|
});
|
||||||
@ -85,13 +81,13 @@ public class FuzzyInferenceService {
|
|||||||
output.setName(OUTPUT_VARIABLE_NAME);
|
output.setName(OUTPUT_VARIABLE_NAME);
|
||||||
output.setDescription("");
|
output.setDescription("");
|
||||||
output.setEnabled(true);
|
output.setEnabled(true);
|
||||||
output.setRange(-0.1, consequentValues.size() + 0.1);
|
output.setRange(0, consequentValues.size() + 0.1);
|
||||||
output.setAggregation(new Maximum());
|
output.setAggregation(new Maximum());
|
||||||
output.setDefuzzifier(new Centroid(100));
|
output.setDefuzzifier(new Centroid(10));
|
||||||
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).toString(), i - 0.1, i + 2.1));
|
output.addTerm(new Triangle(consequentValues.get(i).toString(), i, i + 2.1));
|
||||||
}
|
}
|
||||||
engine.addOutputVariable(output);
|
engine.addOutputVariable(output);
|
||||||
|
|
||||||
@ -99,11 +95,14 @@ public class FuzzyInferenceService {
|
|||||||
mamdani.setName("mamdani");
|
mamdani.setName("mamdani");
|
||||||
mamdani.setDescription("");
|
mamdani.setDescription("");
|
||||||
mamdani.setEnabled(true);
|
mamdani.setEnabled(true);
|
||||||
mamdani.setConjunction(new AlgebraicProduct());
|
mamdani.setConjunction(new Minimum());
|
||||||
mamdani.setDisjunction(new BoundedSum());
|
//mamdani.setDisjunction(null);
|
||||||
mamdani.setImplication(new AlgebraicProduct());
|
mamdani.setImplication(new AlgebraicProduct());
|
||||||
mamdani.setActivation(new Highest());
|
mamdani.setActivation(new General());
|
||||||
getRulesFromDb(dbRules, variableValues).forEach(r -> mamdani.addRule(Rule.parse(r, engine)));
|
getRulesFromDb(dbRules, variableValues).forEach(r -> {
|
||||||
|
LOG.info(r);
|
||||||
|
mamdani.addRule(Rule.parse(r, engine));
|
||||||
|
});
|
||||||
return mamdani;
|
return mamdani;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,83 +113,40 @@ public class FuzzyInferenceService {
|
|||||||
return engine;
|
return engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Assessment> getAssessmentsByForecastTendencies(Integer branchId) {
|
public List<Assessment> getFuzzyInference(List<DbRule> dbRules,
|
||||||
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
|
List<AntecedentValue> antecedentValues,
|
||||||
List<DbRule> dbRules = ruleService.getList();
|
Map<String, Double> variableValues,
|
||||||
try {
|
Map<String, Double> min,
|
||||||
return getAssessmentsByTimeSeriesTendencies(dbRules, timeSeries);
|
Map<String, Double> max) {
|
||||||
} 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 = 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());
|
List<Integer> consequentValues = dbRules.stream().map(DbRule::getId).collect(Collectors.toList());
|
||||||
engine.addRuleBlock(getRuleBlock(engine, dbRules, variableValues, antecedentValues, consequentValues));
|
engine.addRuleBlock(getRuleBlock(engine, dbRules, variableValues, min, max, antecedentValues, consequentValues));
|
||||||
String consequent = getConsequent(engine, variableValues);
|
Map.Entry<String, Double> consequent = getConsequent(engine, variableValues);
|
||||||
if (consequent.equals(NO_RESULT)) {
|
if (consequent.getKey().equals(NO_RESULT)) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
return dbRules
|
return dbRules
|
||||||
.stream()
|
.stream()
|
||||||
.filter(r -> r.getId().equals(Integer.valueOf(consequent)))
|
.filter(r -> r.getId().equals(Integer.valueOf(consequent.getKey())))
|
||||||
.map(Assessment::new)
|
.map(r -> new Assessment(r, consequent.getValue()))
|
||||||
.collect(Collectors.toList());
|
.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(), 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) {
|
||||||
for (DbRule dbRule : dbDbRules) {
|
for (DbRule dbRule : dbDbRules) {
|
||||||
if (!variableValues.containsKey(dbRule.getFirstAntecedent().name())) {
|
if (!variableValues.containsKey(dbRule.getFirstAntecedent().name())) {
|
||||||
throw new RuntimeException(String.format("Переменной в правиле не задано значение (нет временного ряда): %s ",
|
throw new RuntimeException(format("Переменной в правиле не задано значение (нет временного ряда): %s ",
|
||||||
dbRule.getFirstAntecedent().name()));
|
dbRule.getFirstAntecedent().name()));
|
||||||
}
|
}
|
||||||
if (!variableValues.containsKey(dbRule.getSecondAntecedent().name())) {
|
if (!variableValues.containsKey(dbRule.getSecondAntecedent().name())) {
|
||||||
throw new RuntimeException(String.format("Переменной в правиле не задано значение (нет временного ряда): %s ",
|
throw new RuntimeException(format("Переменной в правиле не задано значение (нет временного ряда): %s ",
|
||||||
dbRule.getSecondAntecedent().name()));
|
dbRule.getSecondAntecedent().name()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getConsequent(Engine engine, Map<String, Double> variableValues) {
|
private Map.Entry<String, Double> getConsequent(Engine engine, Map<String, Double> variableValues) {
|
||||||
OutputVariable outputVariable = engine.getOutputVariable(OUTPUT_VARIABLE_NAME);
|
OutputVariable outputVariable = engine.getOutputVariable(OUTPUT_VARIABLE_NAME);
|
||||||
for (Map.Entry<String, Double> variableValue : variableValues.entrySet()) {
|
for (Map.Entry<String, Double> variableValue : variableValues.entrySet()) {
|
||||||
InputVariable inputVariable = engine.getInputVariable(variableValue.getKey());
|
InputVariable inputVariable = engine.getInputVariable(variableValue.getKey());
|
||||||
@ -198,10 +154,10 @@ public class FuzzyInferenceService {
|
|||||||
}
|
}
|
||||||
engine.process();
|
engine.process();
|
||||||
if (outputVariable != null) {
|
if (outputVariable != null) {
|
||||||
outputVariable.defuzzify();
|
LOG.info("Output: {}", outputVariable.getValue());
|
||||||
}
|
}
|
||||||
return (outputVariable == null || Double.isNaN(outputVariable.getValue()))
|
return (outputVariable == null || Double.isNaN(outputVariable.getValue()))
|
||||||
? NO_RESULT
|
? Map.entry(NO_RESULT, 0.0)
|
||||||
: outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName();
|
: Map.entry(outputVariable.highestMembershipTerm(outputVariable.getValue()).getName(), outputVariable.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,7 @@
|
|||||||
th:text="${assessment.firstAntecedent.description}"></span>'
|
th:text="${assessment.firstAntecedent.description}"></span>'
|
||||||
и тенденции '<span th:text="${assessment.secondAntecedentTendency}"></span>' показателя '<span
|
и тенденции '<span th:text="${assessment.secondAntecedentTendency}"></span>' показателя '<span
|
||||||
th:text="${assessment.secondAntecedent.description}"></span>';
|
th:text="${assessment.secondAntecedent.description}"></span>';
|
||||||
|
<span class="badge badge-warning" th:text="${assessment.degree}"></span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div th:if="${assessments != null && #lists.size(assessments) == 0}">
|
<div th:if="${assessments != null && #lists.size(assessments) == 0}">
|
||||||
|
@ -2,7 +2,9 @@ package ru.ulstu;
|
|||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import ru.ulstu.extractor.branch.model.Branch;
|
||||||
import ru.ulstu.extractor.ts.model.TimeSeries;
|
import ru.ulstu.extractor.ts.model.TimeSeries;
|
||||||
|
import ru.ulstu.extractor.ts.model.TimeSeriesType;
|
||||||
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
|
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
|
||||||
import ru.ulstu.extractor.ts.util.TimeSeriesDateMapper;
|
import ru.ulstu.extractor.ts.util.TimeSeriesDateMapper;
|
||||||
|
|
||||||
@ -17,13 +19,13 @@ public class TimeSeriesMapperTest {
|
|||||||
c1.set(2020, 5, 1, 1, 1, 1);
|
c1.set(2020, 5, 1, 1, 1, 1);
|
||||||
Calendar c2 = GregorianCalendar.getInstance();
|
Calendar c2 = GregorianCalendar.getInstance();
|
||||||
c2.set(2020, 5, 2, 2, 1, 1);
|
c2.set(2020, 5, 2, 2, 1, 1);
|
||||||
TimeSeries timeSeries = new TimeSeries("Тестовый",
|
TimeSeries timeSeries = new TimeSeries("Тестовый", new Branch(), TimeSeriesType.COMMITS,
|
||||||
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
|
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
|
||||||
new TimeSeriesValue(c2.getTime(), 10.0)));
|
new TimeSeriesValue(c2.getTime(), 10.0)));
|
||||||
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
|
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
|
||||||
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
|
timeSeries = TimeSeriesDateMapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
|
||||||
Assert.assertEquals(1, timeSeries.getValues().size());
|
Assert.assertEquals(1, timeSeries.getValues().size());
|
||||||
Assert.assertEquals(Integer.valueOf(20), timeSeries.getValues().get(0).getValue());
|
Assert.assertEquals(Double.valueOf(20), timeSeries.getValues().get(0).getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -32,13 +34,13 @@ public class TimeSeriesMapperTest {
|
|||||||
c1.set(2020, 5, 1, 1, 1, 1);
|
c1.set(2020, 5, 1, 1, 1, 1);
|
||||||
Calendar c2 = GregorianCalendar.getInstance();
|
Calendar c2 = GregorianCalendar.getInstance();
|
||||||
c2.set(2020, 5, 2, 1, 1, 1);
|
c2.set(2020, 5, 2, 1, 1, 1);
|
||||||
TimeSeries timeSeries = new TimeSeries("Тестовый",
|
TimeSeries timeSeries = new TimeSeries("Тестовый", new Branch(), TimeSeriesType.COMMITS,
|
||||||
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
|
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
|
||||||
new TimeSeriesValue(c2.getTime(), 10.0)));
|
new TimeSeriesValue(c2.getTime(), 10.0)));
|
||||||
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
|
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
|
||||||
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
|
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
|
||||||
Assert.assertEquals(1, timeSeries.getValues().size());
|
Assert.assertEquals(1, timeSeries.getValues().size());
|
||||||
Assert.assertEquals(Integer.valueOf(20), timeSeries.getValues().get(0).getValue());
|
Assert.assertEquals(Double.valueOf(20), timeSeries.getValues().get(0).getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -47,13 +49,13 @@ public class TimeSeriesMapperTest {
|
|||||||
c1.set(2020, 5, 1, 1, 1, 1);
|
c1.set(2020, 5, 1, 1, 1, 1);
|
||||||
Calendar c2 = GregorianCalendar.getInstance();
|
Calendar c2 = GregorianCalendar.getInstance();
|
||||||
c2.set(2020, 5, 2, 1, 1, 1);
|
c2.set(2020, 5, 2, 1, 1, 1);
|
||||||
TimeSeries timeSeries = new TimeSeries("Тестовый",
|
TimeSeries timeSeries = new TimeSeries("Тестовый", new Branch(), TimeSeriesType.COMMITS,
|
||||||
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
|
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
|
||||||
new TimeSeriesValue(c2.getTime(), 10.0)));
|
new TimeSeriesValue(c2.getTime(), 10.0)));
|
||||||
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
|
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
|
||||||
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
|
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
|
||||||
Assert.assertEquals(1, timeSeries.getValues().size());
|
Assert.assertEquals(1, timeSeries.getValues().size());
|
||||||
Assert.assertEquals(Integer.valueOf(20), timeSeries.getValues().get(0).getValue());
|
Assert.assertEquals(Double.valueOf(20), timeSeries.getValues().get(0).getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -62,13 +64,13 @@ public class TimeSeriesMapperTest {
|
|||||||
c1.set(2020, 5, 1, 1, 1, 1);
|
c1.set(2020, 5, 1, 1, 1, 1);
|
||||||
Calendar c2 = GregorianCalendar.getInstance();
|
Calendar c2 = GregorianCalendar.getInstance();
|
||||||
c2.set(2020, 6, 2, 1, 1, 1);
|
c2.set(2020, 6, 2, 1, 1, 1);
|
||||||
TimeSeries timeSeries = new TimeSeries("Тестовый",
|
TimeSeries timeSeries = new TimeSeries("Тестовый", new Branch(), TimeSeriesType.COMMITS,
|
||||||
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
|
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
|
||||||
new TimeSeriesValue(c2.getTime(), 10.0)));
|
new TimeSeriesValue(c2.getTime(), 10.0)));
|
||||||
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
|
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
|
||||||
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
|
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
|
||||||
Assert.assertEquals(2, timeSeries.getValues().size());
|
Assert.assertEquals(2, timeSeries.getValues().size());
|
||||||
Assert.assertEquals(Integer.valueOf(10), timeSeries.getValues().get(0).getValue());
|
Assert.assertEquals(Double.valueOf(10), timeSeries.getValues().get(0).getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -77,13 +79,13 @@ public class TimeSeriesMapperTest {
|
|||||||
c1.set(2020, 5, 1, 1, 1, 1);
|
c1.set(2020, 5, 1, 1, 1, 1);
|
||||||
Calendar c2 = GregorianCalendar.getInstance();
|
Calendar c2 = GregorianCalendar.getInstance();
|
||||||
c2.set(2020, 5, 2, 1, 1, 1);
|
c2.set(2020, 5, 2, 1, 1, 1);
|
||||||
TimeSeries timeSeries = new TimeSeries("Тестовый",
|
TimeSeries timeSeries = new TimeSeries("Тестовый", new Branch(), TimeSeriesType.COMMITS,
|
||||||
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
|
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
|
||||||
new TimeSeriesValue(c2.getTime(), 10.0)));
|
new TimeSeriesValue(c2.getTime(), 10.0)));
|
||||||
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
|
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
|
||||||
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries);
|
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries);
|
||||||
Assert.assertEquals(1, timeSeries.getValues().size());
|
Assert.assertEquals(1, timeSeries.getValues().size());
|
||||||
Assert.assertEquals(Integer.valueOf(20), timeSeries.getValues().get(0).getValue());
|
Assert.assertEquals(Double.valueOf(20), timeSeries.getValues().get(0).getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -92,12 +94,12 @@ public class TimeSeriesMapperTest {
|
|||||||
c1.set(2020, 5, 1, 1, 1, 1);
|
c1.set(2020, 5, 1, 1, 1, 1);
|
||||||
Calendar c2 = GregorianCalendar.getInstance();
|
Calendar c2 = GregorianCalendar.getInstance();
|
||||||
c2.set(2021, 5, 2, 1, 1, 1);
|
c2.set(2021, 5, 2, 1, 1, 1);
|
||||||
TimeSeries timeSeries = new TimeSeries("Тестовый",
|
TimeSeries timeSeries = new TimeSeries("Тестовый", new Branch(), TimeSeriesType.COMMITS,
|
||||||
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
|
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
|
||||||
new TimeSeriesValue(c2.getTime(), 10.0)));
|
new TimeSeriesValue(c2.getTime(), 10.0)));
|
||||||
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
|
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
|
||||||
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries);
|
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries);
|
||||||
Assert.assertEquals(2, timeSeries.getValues().size());
|
Assert.assertEquals(2, timeSeries.getValues().size());
|
||||||
Assert.assertEquals(Integer.valueOf(10), timeSeries.getValues().get(0).getValue());
|
Assert.assertEquals(Double.valueOf(10), timeSeries.getValues().get(0).getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user