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

Closed
Ghost wants to merge 132 commits from master into 61-rules-pages
3 changed files with 30 additions and 7 deletions
Showing only changes of commit 9ef1bb3072 - Show all commits

View File

@ -8,6 +8,7 @@ import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.service.TimeSeriesService; import ru.ulstu.extractor.ts.service.TimeSeriesService;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Service @Service
public class DbRuleService { public class DbRuleService {
@ -60,4 +61,8 @@ public class DbRuleService {
public void deleteById(Integer id) { public void deleteById(Integer id) {
ruleRepository.deleteById(id); ruleRepository.deleteById(id);
} }
public List<String> getConsequentList() {
return ruleRepository.findAll().stream().map(DbRule::getConsequent).collect(Collectors.toList());
}
} }

View File

@ -44,8 +44,9 @@ public class FuzzyInferenceService {
this.timeSeriesService = timeSeriesService; this.timeSeriesService = timeSeriesService;
} }
public List<String> getRulesFromDb() { public List<String> getRulesFromDb(Map<String, Double> variableValues) {
List<DbRule> dbDbRules = ruleService.getList(); List<DbRule> dbDbRules = ruleService.getList();
validateVariables(variableValues, dbDbRules);
return dbDbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList()); return dbDbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
} }
@ -58,7 +59,10 @@ public class FuzzyInferenceService {
dbRule.getConsequent()); dbRule.getConsequent());
} }
private RuleBlock getRuleBlock(Engine engine, Map<String, Double> variableValues, List<AntecedentValue> antecedentValues) { private RuleBlock getRuleBlock(Engine engine,
Map<String, Double> variableValues,
List<AntecedentValue> antecedentValues,
List<String> consequentValues) {
variableValues.forEach((key, value) -> { variableValues.forEach((key, value) -> {
InputVariable input = new InputVariable(); InputVariable input = new InputVariable();
input.setName(key); input.setName(key);
@ -81,8 +85,8 @@ public class FuzzyInferenceService {
output.setDefuzzifier(new Centroid(100)); output.setDefuzzifier(new Centroid(100));
output.setDefaultValue(Double.NaN); output.setDefaultValue(Double.NaN);
output.setLockValueInRange(false); output.setLockValueInRange(false);
for (int i = 0; i < antecedentValues.size(); i++) { for (int i = 0; i < consequentValues.size(); i++) {
output.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i - 0.1, i + 2.1)); output.addTerm(new Triangle(consequentValues.get(i), i - 0.1, i + 2.1));
} }
engine.addOutputVariable(output); engine.addOutputVariable(output);
@ -94,7 +98,7 @@ public class FuzzyInferenceService {
mamdani.setDisjunction(new BoundedSum()); mamdani.setDisjunction(new BoundedSum());
mamdani.setImplication(new AlgebraicProduct()); mamdani.setImplication(new AlgebraicProduct());
mamdani.setActivation(new Highest()); mamdani.setActivation(new Highest());
getRulesFromDb().forEach(r -> mamdani.addRule(Rule.parse(r, engine))); getRulesFromDb(variableValues).forEach(r -> mamdani.addRule(Rule.parse(r, engine)));
return mamdani; return mamdani;
} }
@ -109,12 +113,26 @@ public class FuzzyInferenceService {
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId); List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
Engine engine = getFuzzyEngine(); Engine engine = getFuzzyEngine();
List<AntecedentValue> antecedentValues = antecedentValueService.getList(); List<AntecedentValue> antecedentValues = antecedentValueService.getList();
List<String> consequentValues = ruleService.getConsequentList();
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)); engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues, consequentValues));
return getConsequent(engine, variableValues); return getConsequent(engine, variableValues);
} }
private void validateVariables(Map<String, Double> variableValues, List<DbRule> dbDbRules) {
for (DbRule dbRule : dbDbRules) {
if (!variableValues.containsKey(dbRule.getFirstAntecedent().name())) {
throw new RuntimeException(String.format("Переменной в правиле не задано значение (нет временного ряда): %s ",
dbRule.getFirstAntecedent().name()));
}
if (!variableValues.containsKey(dbRule.getSecondAntecedent().name())) {
throw new RuntimeException(String.format("Переменной в правиле не задано значение (нет временного ряда): %s ",
dbRule.getSecondAntecedent().name()));
}
}
}
private String getConsequent(Engine engine, Map<String, Double> variableValues) { private String 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()) {

View File

@ -34,7 +34,7 @@ public class TimeSeriesService {
private final TimeSeriesDateMapper.TimeSeriesInterval timeSeriesInterval = TimeSeriesDateMapper.TimeSeriesInterval.HOUR; private final TimeSeriesDateMapper.TimeSeriesInterval timeSeriesInterval = TimeSeriesDateMapper.TimeSeriesInterval.HOUR;
private final HttpService httpService; private final HttpService httpService;
private final static String TIME_SERIES_SAVE_SERVICE_URL = "http://time-series.athene.tech/api/1.0/add-time-series?setKey=git-extractor"; private final static String TIME_SERIES_SAVE_SERVICE_URL = "http://time-series.athene.tech/api/1.0/add-time-series?setKey=git-extractor";
private final static String TIME_SERIES_TENDENCY_URL = "http://time-series.athene.tech/api/1.0/add-time-series?setKey=git-extractor"; private final static String TIME_SERIES_TENDENCY_URL = "http://time-series.athene.tech/api/1.0/getSpecificMethodSmoothed";
public TimeSeriesService(TimeSeriesRepository timeSeriesRepository, public TimeSeriesService(TimeSeriesRepository timeSeriesRepository,
TimeSeriesValueRepository timeSeriesValueRepository, TimeSeriesValueRepository timeSeriesValueRepository,