From 71652c7b79225bcef75d5fa5c7433334a3c1c8c9 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 11 Mar 2023 00:55:43 +0400 Subject: [PATCH] #91 -- Add some rule generator --- .../extractor/GitExtractorApplication.java | 6 ++- .../assessment/service/AssessmentService.java | 3 +- .../markup/service/MarkupService.java | 6 +++ .../extractor/rule/model/AntecedentValue.java | 3 +- .../repository/AntecedentValueRepository.java | 3 ++ .../rule/service/AntecedentValueService.java | 5 ++ .../extractor/rule/service/DbRuleService.java | 48 +++++++++++++++++-- 7 files changed, 67 insertions(+), 7 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java b/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java index 8491e6c..95e1008 100644 --- a/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java +++ b/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java @@ -7,15 +7,18 @@ import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import ru.ulstu.extractor.gitrepository.service.IndexService; +import ru.ulstu.extractor.rule.service.DbRuleService; @SpringBootApplication @EnableScheduling @EnableAsync public class GitExtractorApplication { private final IndexService indexService; + private final DbRuleService dbRuleService; - public GitExtractorApplication(IndexService indexService) { + public GitExtractorApplication(IndexService indexService, DbRuleService dbRuleService) { this.indexService = indexService; + this.dbRuleService = dbRuleService; } public static void main(String[] args) { @@ -25,5 +28,6 @@ public class GitExtractorApplication { @EventListener(ApplicationReadyEvent.class) public void doSomethingAfterStartup() { indexService.indexFailedBranchesOnStart(); + //dbRuleService.generateRulesFromMarkup(); } } diff --git a/src/main/java/ru/ulstu/extractor/assessment/service/AssessmentService.java b/src/main/java/ru/ulstu/extractor/assessment/service/AssessmentService.java index d221806..2514e6a 100644 --- a/src/main/java/ru/ulstu/extractor/assessment/service/AssessmentService.java +++ b/src/main/java/ru/ulstu/extractor/assessment/service/AssessmentService.java @@ -71,8 +71,9 @@ public class AssessmentService { Map variableValues = new HashMap<>(); timeSeries .stream() - .filter(ts -> ts.getTimeSeriesType() == dbRule.getFirstAntecedent() + .filter(ts -> (ts.getTimeSeriesType() == dbRule.getFirstAntecedent() || ts.getTimeSeriesType() == dbRule.getSecondAntecedent()) + && ts.getValues().size() > 0) .forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService .getLastTimeSeriesTendency(ts) .orElse(ts.getValues().get(ts.getValues().size() - 1).getValue()))); diff --git a/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java b/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java index ae3e35f..c4d3fd3 100644 --- a/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java +++ b/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java @@ -8,6 +8,8 @@ import ru.ulstu.extractor.markup.repository.MarkupRepository; import ru.ulstu.extractor.ts.model.TimeSeries; import ru.ulstu.extractor.ts.service.TimeSeriesService; +import java.util.List; + @Service public class MarkupService { private final MarkupRepository markupRepository; @@ -26,4 +28,8 @@ public class MarkupService { markupRepository.save(new Markup(timeSeries, markupRow.getMarkup())); }); } + + public List findAll() { + return markupRepository.findAll(); + } } diff --git a/src/main/java/ru/ulstu/extractor/rule/model/AntecedentValue.java b/src/main/java/ru/ulstu/extractor/rule/model/AntecedentValue.java index 3583e6a..39b5eb6 100644 --- a/src/main/java/ru/ulstu/extractor/rule/model/AntecedentValue.java +++ b/src/main/java/ru/ulstu/extractor/rule/model/AntecedentValue.java @@ -11,8 +11,7 @@ public class AntecedentValue extends BaseEntity { public AntecedentValue() { } - public AntecedentValue(Integer id, String antecedentValue) { - this.setId(id); + public AntecedentValue(String antecedentValue) { this.antecedentValue = antecedentValue; } diff --git a/src/main/java/ru/ulstu/extractor/rule/repository/AntecedentValueRepository.java b/src/main/java/ru/ulstu/extractor/rule/repository/AntecedentValueRepository.java index 1ea84d7..3ae5795 100644 --- a/src/main/java/ru/ulstu/extractor/rule/repository/AntecedentValueRepository.java +++ b/src/main/java/ru/ulstu/extractor/rule/repository/AntecedentValueRepository.java @@ -4,5 +4,8 @@ package ru.ulstu.extractor.rule.repository; import org.springframework.data.jpa.repository.JpaRepository; import ru.ulstu.extractor.rule.model.AntecedentValue; +import java.util.Optional; + public interface AntecedentValueRepository extends JpaRepository { + Optional findByAntecedentValue(String antecedentValue); } diff --git a/src/main/java/ru/ulstu/extractor/rule/service/AntecedentValueService.java b/src/main/java/ru/ulstu/extractor/rule/service/AntecedentValueService.java index ef3c124..8de502a 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/AntecedentValueService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/AntecedentValueService.java @@ -22,4 +22,9 @@ public class AntecedentValueService { return antecedentValueRepository.findById(antecedentValueId) .orElseThrow(() -> new RuntimeException("Antecedent value not found by id " + antecedentValueId)); } + + public AntecedentValue getByValue(String antecedentValue) { + return antecedentValueRepository.findByAntecedentValue(antecedentValue) + .orElseThrow(() -> new RuntimeException("Antecedent value not found by value " + antecedentValue)); + } } 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 198d486..c0a2dc5 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/DbRuleService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/DbRuleService.java @@ -1,10 +1,15 @@ package ru.ulstu.extractor.rule.service; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.extractor.markup.model.Markup; +import ru.ulstu.extractor.markup.service.MarkupService; import ru.ulstu.extractor.rule.model.AddRuleForm; +import ru.ulstu.extractor.rule.model.AntecedentValue; import ru.ulstu.extractor.rule.model.DbRule; import ru.ulstu.extractor.rule.repository.RuleRepository; import ru.ulstu.extractor.ts.model.TimeSeriesType; +import ru.ulstu.extractor.ts.model.TimeSeriesValue; import ru.ulstu.extractor.ts.service.TimeSeriesService; import java.util.List; @@ -13,15 +18,16 @@ import java.util.stream.Collectors; @Service public class DbRuleService { private final RuleRepository ruleRepository; - private final TimeSeriesService timeSeriesService; private final AntecedentValueService antecedentValueService; + private final MarkupService markupService; public DbRuleService(RuleRepository ruleRepository, TimeSeriesService timeSeriesService, - AntecedentValueService antecedentValueService) { + AntecedentValueService antecedentValueService, + MarkupService markupService) { this.ruleRepository = ruleRepository; - this.timeSeriesService = timeSeriesService; this.antecedentValueService = antecedentValueService; + this.markupService = markupService; } public List getList() { @@ -65,4 +71,40 @@ public class DbRuleService { public List getConsequentList() { return ruleRepository.findAll().stream().map(DbRule::getConsequent).collect(Collectors.toList()); } + + @Transactional + public void generateRulesFromMarkup() { + List markups = markupService.findAll(); + for (Markup markup1 : markups) { + for (Markup markup2 : markups) { + if (!markup1.equals(markup2)) { + List values1 = markup1.getTimeSeries().getValues(); + for (int i = 1; i < values1.size(); i++) { + List values2 = markup2.getTimeSeries().getValues(); + for (int j = 1; j < values2.size(); j++) { + ruleRepository.save(new DbRule( + getAntecedent(values1.get(i).getValue() - values1.get(i - 1).getValue()), + markup1.getTimeSeries().getTimeSeriesType(), + getAntecedent(values2.get(j).getValue() - values2.get(j - 1).getValue()), + markup2.getTimeSeries().getTimeSeriesType(), + "сгенерированный консеквент" + )); + } + } + } + } + } + } + + private AntecedentValue getAntecedent(Double diff) { + String antecedentValue; + if (diff < 0) { + antecedentValue = "спад"; + } else if (diff > 0) { + antecedentValue = "рост"; + } else { + antecedentValue = "стабильно"; + } + return antecedentValueService.getByValue(antecedentValue); + } }