#91 -- Add some rule generator

This commit is contained in:
Anton Romanov 2023-03-11 00:55:43 +04:00
parent d7790dff70
commit 71652c7b79
7 changed files with 67 additions and 7 deletions

View File

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

View File

@ -71,8 +71,9 @@ public class AssessmentService {
Map<String, Double> 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())));

View File

@ -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<Markup> findAll() {
return markupRepository.findAll();
}
}

View File

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

View File

@ -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<AntecedentValue, Integer> {
Optional<AntecedentValue> findByAntecedentValue(String antecedentValue);
}

View File

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

View File

@ -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<DbRule> getList() {
@ -65,4 +71,40 @@ public class DbRuleService {
public List<String> getConsequentList() {
return ruleRepository.findAll().stream().map(DbRule::getConsequent).collect(Collectors.toList());
}
@Transactional
public void generateRulesFromMarkup() {
List<Markup> markups = markupService.findAll();
for (Markup markup1 : markups) {
for (Markup markup2 : markups) {
if (!markup1.equals(markup2)) {
List<TimeSeriesValue> values1 = markup1.getTimeSeries().getValues();
for (int i = 1; i < values1.size(); i++) {
List<TimeSeriesValue> 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);
}
}