From 9ef1bb3072c7e7b6ea178efa01cb1725f3938862 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 28 Nov 2022 00:58:36 +0400 Subject: [PATCH] #76 -- fix rules --- .../extractor/rule/service/DbRuleService.java | 5 ++++ .../rule/service/FuzzyInferenceService.java | 30 +++++++++++++++---- .../ts/service/TimeSeriesService.java | 2 +- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/rule/service/DbRuleService.java b/src/main/java/ru/ulstu/extractor/rule/service/DbRuleService.java index a35cca2..198d486 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/DbRuleService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/DbRuleService.java @@ -8,6 +8,7 @@ import ru.ulstu.extractor.ts.model.TimeSeriesType; import ru.ulstu.extractor.ts.service.TimeSeriesService; import java.util.List; +import java.util.stream.Collectors; @Service public class DbRuleService { @@ -60,4 +61,8 @@ public class DbRuleService { public void deleteById(Integer id) { ruleRepository.deleteById(id); } + + public List getConsequentList() { + return ruleRepository.findAll().stream().map(DbRule::getConsequent).collect(Collectors.toList()); + } } diff --git a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java index f011aaf..46a8008 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java @@ -44,8 +44,9 @@ public class FuzzyInferenceService { this.timeSeriesService = timeSeriesService; } - public List getRulesFromDb() { + public List getRulesFromDb(Map variableValues) { List dbDbRules = ruleService.getList(); + validateVariables(variableValues, dbDbRules); return dbDbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList()); } @@ -58,7 +59,10 @@ public class FuzzyInferenceService { dbRule.getConsequent()); } - private RuleBlock getRuleBlock(Engine engine, Map variableValues, List antecedentValues) { + private RuleBlock getRuleBlock(Engine engine, + Map variableValues, + List antecedentValues, + List consequentValues) { variableValues.forEach((key, value) -> { InputVariable input = new InputVariable(); input.setName(key); @@ -81,8 +85,8 @@ public class FuzzyInferenceService { output.setDefuzzifier(new Centroid(100)); output.setDefaultValue(Double.NaN); output.setLockValueInRange(false); - for (int i = 0; i < antecedentValues.size(); i++) { - output.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i - 0.1, i + 2.1)); + for (int i = 0; i < consequentValues.size(); i++) { + output.addTerm(new Triangle(consequentValues.get(i), i - 0.1, i + 2.1)); } engine.addOutputVariable(output); @@ -94,7 +98,7 @@ public class FuzzyInferenceService { mamdani.setDisjunction(new BoundedSum()); mamdani.setImplication(new AlgebraicProduct()); 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; } @@ -109,12 +113,26 @@ public class FuzzyInferenceService { List timeSeries = timeSeriesService.getByBranch(branchId); Engine engine = getFuzzyEngine(); List antecedentValues = antecedentValueService.getList(); + List consequentValues = ruleService.getConsequentList(); Map variableValues = new HashMap<>(); 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); } + private void validateVariables(Map variableValues, List 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 variableValues) { OutputVariable outputVariable = engine.getOutputVariable(OUTPUT_VARIABLE_NAME); for (Map.Entry variableValue : variableValues.entrySet()) { diff --git a/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java b/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java index 4a4b2f2..2faf0bf 100644 --- a/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java @@ -34,7 +34,7 @@ public class TimeSeriesService { private final TimeSeriesDateMapper.TimeSeriesInterval timeSeriesInterval = TimeSeriesDateMapper.TimeSeriesInterval.HOUR; 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_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, TimeSeriesValueRepository timeSeriesValueRepository,