diff --git a/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java b/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java index 94d1ec4..3aef93a 100644 --- a/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java +++ b/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java @@ -65,9 +65,10 @@ public class TimeSeriesMarkupController { ); MarkupForm markupFormWithTs = new MarkupForm(markupForm.getBranchId(), tss); model.addAttribute("markupForm", markupFormWithTs); -// if (markupForm.getMarkupRows() != null && !markupForm.getMarkupRows().isEmpty()) { -// markupService.saveMarkup(markupForm); -// } + if (markupForm.getTimeSeriesForMarkupList() != null + && !markupForm.getTimeSeriesForMarkupList().isEmpty()) { + markupService.generateRules(markupForm); + } } return "markup"; } diff --git a/src/main/java/ru/ulstu/extractor/markup/model/Markup.java b/src/main/java/ru/ulstu/extractor/markup/model/Markup.java deleted file mode 100644 index f119e41..0000000 --- a/src/main/java/ru/ulstu/extractor/markup/model/Markup.java +++ /dev/null @@ -1,38 +0,0 @@ -package ru.ulstu.extractor.markup.model; - -import ru.ulstu.extractor.core.BaseEntity; -import ru.ulstu.extractor.ts.model.TimeSeries; - -import javax.persistence.Entity; -import javax.persistence.ManyToOne; - -@Entity -public class Markup extends BaseEntity { - @ManyToOne - private TimeSeries timeSeries; - private String markup; - - public Markup() { - } - - public Markup(TimeSeries timeSeries, String markup) { - this.timeSeries = timeSeries; - this.markup = markup; - } - - public TimeSeries getTimeSeries() { - return timeSeries; - } - - public void setTimeSeries(TimeSeries timeSeries) { - this.timeSeries = timeSeries; - } - - public String getMarkup() { - return markup; - } - - public void setMarkup(String markup) { - this.markup = markup; - } -} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/extractor/markup/model/TimeSeriesForMarkup.java b/src/main/java/ru/ulstu/extractor/markup/model/TimeSeriesForMarkup.java index 3bb84ff..9aaa903 100644 --- a/src/main/java/ru/ulstu/extractor/markup/model/TimeSeriesForMarkup.java +++ b/src/main/java/ru/ulstu/extractor/markup/model/TimeSeriesForMarkup.java @@ -1,29 +1,27 @@ package ru.ulstu.extractor.markup.model; +import ru.ulstu.extractor.rule.model.AntecedentValue; import ru.ulstu.extractor.ts.model.TimeSeries; import java.util.Date; -import java.util.Set; -import java.util.TreeSet; +import java.util.HashMap; +import java.util.Map; public class TimeSeriesForMarkup { - private Set timeSeriesSet = new TreeSet<>(); + private Map timeSeriesTendencyMap = new HashMap<>(); + private String markup; private Date dateFrom; private Date dateTo; - public TimeSeriesForMarkup(TimeSeries ts1, TimeSeries ts2, Date dateFrom, Date dateTo) { + public TimeSeriesForMarkup(TimeSeries ts1, AntecedentValue tendency1, TimeSeries ts2, AntecedentValue tendency2, Date dateFrom, Date dateTo) { this.dateFrom = dateFrom; this.dateTo = dateTo; - this.timeSeriesSet.add(ts1); - this.timeSeriesSet.add(ts2); + this.timeSeriesTendencyMap.put(ts1, tendency1); + this.timeSeriesTendencyMap.put(ts2, tendency2); } - public Set getTimeSeriesSet() { - return timeSeriesSet; - } - - public void setTimeSeriesSet(Set timeSeriesSet) { - this.timeSeriesSet = timeSeriesSet; + public Map getTimeSeriesTendencyMap() { + return timeSeriesTendencyMap; } public Date getDateFrom() { @@ -41,4 +39,8 @@ public class TimeSeriesForMarkup { public void setDateTo(Date dateTo) { this.dateTo = dateTo; } + + public String getMarkup() { + return markup; + } } diff --git a/src/main/java/ru/ulstu/extractor/markup/repository/MarkupRepository.java b/src/main/java/ru/ulstu/extractor/markup/repository/MarkupRepository.java deleted file mode 100644 index 0d149f1..0000000 --- a/src/main/java/ru/ulstu/extractor/markup/repository/MarkupRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package ru.ulstu.extractor.markup.repository; - -import org.springframework.data.jpa.repository.JpaRepository; -import ru.ulstu.extractor.markup.model.Markup; - -public interface MarkupRepository extends JpaRepository { -} 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 c222d39..12ee087 100644 --- a/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java +++ b/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java @@ -1,68 +1,97 @@ package ru.ulstu.extractor.markup.service; import org.springframework.stereotype.Service; -import ru.ulstu.extractor.markup.model.Markup; +import ru.ulstu.extractor.markup.model.MarkupForm; import ru.ulstu.extractor.markup.model.TimeSeriesForMarkup; -import ru.ulstu.extractor.markup.repository.MarkupRepository; +import ru.ulstu.extractor.rule.model.AntecedentValue; +import ru.ulstu.extractor.rule.service.AntecedentValueService; +import ru.ulstu.extractor.rule.service.DbRuleService; import ru.ulstu.extractor.ts.model.TimeSeries; import ru.ulstu.extractor.ts.model.TimeSeriesValue; -import ru.ulstu.extractor.ts.service.TimeSeriesService; +import javax.transaction.Transactional; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; @Service public class MarkupService { - private final MarkupRepository markupRepository; - private final TimeSeriesService timeSeriesService; + private final DbRuleService dbRuleService; + private final AntecedentValueService antecedentValueService; - public MarkupService(MarkupRepository markupRepository, - TimeSeriesService timeSeriesService) { - this.markupRepository = markupRepository; - this.timeSeriesService = timeSeriesService; + public MarkupService(DbRuleService dbRuleService, + AntecedentValueService antecedentValueService) { + this.dbRuleService = dbRuleService; + this.antecedentValueService = antecedentValueService; } - /*@Transactional - public void saveMarkup(MarkupForm markupForm) { - markupForm.getMarkupRows().forEach(markupRow -> { - TimeSeries timeSeries = timeSeriesService.getById(markupRow.getTimeSeries().getId()); - markupRepository.save(new Markup(timeSeries, markupRow.getMarkup())); + @Transactional + public void generateRules(MarkupForm markupForm) { + markupForm.getTimeSeriesForMarkupList().forEach(markupRow -> { + Set> timeSeriesEntry1 = markupRow.getTimeSeriesTendencyMap().entrySet(); + Set> timeSeriesEntry2 = markupRow.getTimeSeriesTendencyMap().entrySet(); + for (Map.Entry entry1 : timeSeriesEntry1) { + for (Map.Entry entry2 : timeSeriesEntry2) { + if (!entry1.equals(entry2)) { + dbRuleService.saveRule(entry1.getKey().getTimeSeriesType(), entry1.getValue(), + entry2.getKey().getTimeSeriesType(), entry2.getValue(), + markupRow.getMarkup()); + } + } + } }); - }*/ - - public List findAll() { - return markupRepository.findAll(); } + private AntecedentValue getAntecedent(Double diff) { + String antecedentValue; + if (diff < 0) { + antecedentValue = "спад"; + } else if (diff > 0) { + antecedentValue = "рост"; + } else { + antecedentValue = "стабильно"; + } + return antecedentValueService.getByValue(antecedentValue); + } + + public List getTimeSeriesForMarkup(List timeSeriesList) { List tsForMarkup = new ArrayList<>(); for (int i = 0; i < timeSeriesList.size(); i++) { TimeSeries ts1 = timeSeriesList.get(i); - for (int j = i+1; j < timeSeriesList.size(); j++) { + for (int j = i + 1; j < timeSeriesList.size(); j++) { TimeSeries ts2 = timeSeriesList.get(j); List values1 = ts1.getValues(); - for (int k = 0; k < values1.size()-1; k++) { + for (int k = 0; k < values1.size() - 1; k++) { List values2 = ts2.getValues(); - for (int l = 0; l < values2.size()-1; l++) { + for (int l = 0; l < values2.size() - 1; l++) { if (values1.get(k).getDate().equals(values2.get(l).getDate()) - && values1.get(k+1).getDate().equals(values2.get(l+1).getDate())) { + && values1.get(k + 1).getDate().equals(values2.get(l + 1).getDate())) { final int index = k; TimeSeriesForMarkup found = tsForMarkup .stream() - .filter(m -> m.getDateFrom().equals(values1.get(index).getDate()) && m.getDateTo().equals(values1.get(index+1).getDate())) + .filter(m -> m.getDateFrom().equals(values1.get(index).getDate()) && m.getDateTo().equals(values1.get(index + 1).getDate())) .findAny() .orElse(null); + AntecedentValue antecedentValue1 = getAntecedent(values1.get(index + 1).getValue() - values1.get(index).getValue()); + AntecedentValue antecedentValue2 = getAntecedent(values2.get(l + 1).getValue() - values2.get(l).getValue()); if (found == null) { - tsForMarkup.add(new TimeSeriesForMarkup(ts1, ts2, values1.get(index).getDate(), values1.get(index+1).getDate())); + tsForMarkup.add(new TimeSeriesForMarkup(ts1, antecedentValue1, ts2, antecedentValue2, values1.get(index).getDate(), values1.get(index + 1).getDate())); } else { - found.getTimeSeriesSet().add(ts1); - found.getTimeSeriesSet().add(ts2); + found.getTimeSeriesTendencyMap().put(ts1, antecedentValue1); + found.getTimeSeriesTendencyMap().put(ts2, antecedentValue2); } } } } } } - return tsForMarkup; + return tsForMarkup + .stream() + .sorted(Comparator.comparing(TimeSeriesForMarkup::getDateFrom)) + .collect(Collectors.toList()); } } 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 c0a2dc5..44fe3cf 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/DbRuleService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/DbRuleService.java @@ -1,16 +1,11 @@ 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; import java.util.stream.Collectors; @@ -19,15 +14,11 @@ import java.util.stream.Collectors; public class DbRuleService { private final RuleRepository ruleRepository; private final AntecedentValueService antecedentValueService; - private final MarkupService markupService; public DbRuleService(RuleRepository ruleRepository, - TimeSeriesService timeSeriesService, - AntecedentValueService antecedentValueService, - MarkupService markupService) { + AntecedentValueService antecedentValueService) { this.ruleRepository = ruleRepository; this.antecedentValueService = antecedentValueService; - this.markupService = markupService; } public List getList() { @@ -52,6 +43,17 @@ public class DbRuleService { } } + public void saveRule(TimeSeriesType timeSeriesType1, AntecedentValue antecedentValue1, + TimeSeriesType timeSeriesType2, AntecedentValue antecedentValue2, + String consequent) { + ruleRepository.save(new DbRule(antecedentValueService.getById(antecedentValue1.getId()), + timeSeriesType1, + antecedentValueService.getById(antecedentValue2.getId()), + timeSeriesType2, + consequent)); + + } + public DbRule findById(Integer id) { return ruleRepository.getOne(id); } @@ -71,40 +73,4 @@ 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); - } } diff --git a/src/main/resources/templates/markup.html b/src/main/resources/templates/markup.html index 87746b9..0d0fc58 100644 --- a/src/main/resources/templates/markup.html +++ b/src/main/resources/templates/markup.html @@ -22,7 +22,9 @@ @@ -31,22 +33,15 @@ value="Применить"/> - - - - - - - - - - - - - - -
Охарактеризуйте периоды вашего проекта
- +
+ Охарактеризуйте периоды вашего проекта
+
+ + + +
+