WIP: страницы для правил #62

Closed
Ghost wants to merge 132 commits from master into 61-rules-pages
8 changed files with 120 additions and 53 deletions
Showing only changes of commit 4143fcfb20 - Show all commits

View File

@ -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.service.assessment.AssessmentService;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
import java.util.Optional; import java.util.Optional;

View File

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

View File

@ -1,15 +1,18 @@
package ru.ulstu.extractor.service.assessment; package ru.ulstu.extractor.assessment.service;
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.rule.model.AssessmentException; 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.rule.service.AntecedentValueService;
import ru.ulstu.extractor.rule.service.DbRuleService; import ru.ulstu.extractor.rule.service.DbRuleService;
import ru.ulstu.extractor.rule.service.FuzzyInferenceService; import ru.ulstu.extractor.rule.service.FuzzyInferenceService;
import ru.ulstu.extractor.ts.model.TimeSeries; import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService; import ru.ulstu.extractor.ts.service.TimeSeriesService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -18,13 +21,16 @@ import java.util.stream.Collectors;
@Service @Service
public class AssessmentService { public class AssessmentService {
private final DbRuleService ruleService; private final DbRuleService ruleService;
private final AntecedentValueService antecedentValueService;
private final TimeSeriesService timeSeriesService; private final TimeSeriesService timeSeriesService;
private final FuzzyInferenceService fuzzyInferenceService; private final FuzzyInferenceService fuzzyInferenceService;
public AssessmentService(DbRuleService ruleService, public AssessmentService(DbRuleService ruleService,
AntecedentValueService antecedentValueService,
TimeSeriesService timeSeriesService, TimeSeriesService timeSeriesService,
FuzzyInferenceService fuzzyInferenceService) { FuzzyInferenceService fuzzyInferenceService) {
this.ruleService = ruleService; this.ruleService = ruleService;
this.antecedentValueService = antecedentValueService;
this.timeSeriesService = timeSeriesService; this.timeSeriesService = timeSeriesService;
this.fuzzyInferenceService = fuzzyInferenceService; this.fuzzyInferenceService = fuzzyInferenceService;
} }
@ -35,6 +41,7 @@ public class AssessmentService {
try { try {
return getAssessmentsByTimeSeriesTendencies(dbRules, timeSeries); return getAssessmentsByTimeSeriesTendencies(dbRules, timeSeries);
} catch (AssessmentException ex) { } catch (AssessmentException ex) {
ex.printStackTrace();
return new ArrayList<>(); return new ArrayList<>();
} }
} }
@ -50,7 +57,11 @@ public class AssessmentService {
timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(),
timeSeriesService.getLastTimeSeriesTendency(ts) timeSeriesService.getLastTimeSeriesTendency(ts)
.orElseThrow(() -> new AssessmentException("")))); .orElseThrow(() -> new AssessmentException(""))));
return fuzzyInferenceService.getFuzzyInference(dbRules, variableValues); return fuzzyInferenceService.getFuzzyInference(dbRules,
antecedentValueService.getList(),
variableValues,
getTSsMin(timeSeries),
getTSsMax(timeSeries));
} }
private List<Assessment> getAssessmentsByTimeSeriesTendencies(List<DbRule> dbRules, List<TimeSeries> timeSeries) { private List<Assessment> getAssessmentsByTimeSeriesTendencies(List<DbRule> dbRules, List<TimeSeries> timeSeries) {
@ -64,15 +75,60 @@ public class AssessmentService {
|| ts.getTimeSeriesType() == dbRule.getSecondAntecedent()) || ts.getTimeSeriesType() == dbRule.getSecondAntecedent())
.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService .forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService
.getLastTimeSeriesTendency(ts) .getLastTimeSeriesTendency(ts)
.orElseThrow(() -> new AssessmentException("")))); .orElse(ts.getValues().get(ts.getValues().size() - 1).getValue())));
return fuzzyInferenceService.getFuzzyInference(List.of(dbRule), variableValues).stream(); return fuzzyInferenceService.getFuzzyInference(List.of(dbRule),
}).collect(Collectors.toList()); 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) { private List<Assessment> getAssessmentsByLastValues(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(), ts.getValues().get(ts.getValues().size() - 1).getValue())); timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), ts.getValues().get(ts.getValues().size() - 1).getValue()));
return fuzzyInferenceService.getFuzzyInference(dbRules, variableValues); 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;
}
} }

View File

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

View File

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

View File

@ -1,16 +1,18 @@
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.rule.model.AntecedentValue; import ru.ulstu.extractor.rule.model.AntecedentValue;
@ -20,10 +22,12 @@ import java.util.ArrayList;
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
@ -36,7 +40,7 @@ public class FuzzyInferenceService {
} }
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(),
@ -47,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) -> {
@ -54,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);
}); });
@ -66,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);
@ -80,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;
} }
@ -95,21 +113,22 @@ public class FuzzyInferenceService {
return engine; return engine;
} }
public List<Assessment> getFuzzyInference(List<DbRule> dbRules,
public List<Assessment> getFuzzyInference(List<DbRule> dbRules, Map<String, Double> variableValues) { List<AntecedentValue> antecedentValues,
Map<String, Double> variableValues,
Map<String, Double> min,
Map<String, Double> max) {
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());
} }
@ -117,17 +136,17 @@ public class FuzzyInferenceService {
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());
@ -135,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());
} }
} }

View File

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

View File

@ -1,15 +0,0 @@
package ru.ulstu;
import org.junit.Assert;
import org.junit.Test;
import ru.ulstu.extractor.rule.service.FuzzyInferenceService;
public class FuzzyInferenceTest {
private final FuzzyInferenceService fuzzyInferenceService = new FuzzyInferenceService();
@Test
public void test() {
Assert.assertNotNull(fuzzyInferenceService);
}
}