#91 -- Fix data for markup

This commit is contained in:
Anton Romanov 2023-04-23 15:08:26 +04:00
parent 9c3a64b0d0
commit e506566a5d
7 changed files with 98 additions and 150 deletions

View File

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

View File

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

View File

@ -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<TimeSeries> timeSeriesSet = new TreeSet<>();
private Map<TimeSeries, AntecedentValue> 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<TimeSeries> getTimeSeriesSet() {
return timeSeriesSet;
}
public void setTimeSeriesSet(Set<TimeSeries> timeSeriesSet) {
this.timeSeriesSet = timeSeriesSet;
public Map<TimeSeries, AntecedentValue> 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;
}
}

View File

@ -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<Markup, Integer> {
}

View File

@ -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<Map.Entry<TimeSeries, AntecedentValue>> timeSeriesEntry1 = markupRow.getTimeSeriesTendencyMap().entrySet();
Set<Map.Entry<TimeSeries, AntecedentValue>> timeSeriesEntry2 = markupRow.getTimeSeriesTendencyMap().entrySet();
for (Map.Entry<TimeSeries, AntecedentValue> entry1 : timeSeriesEntry1) {
for (Map.Entry<TimeSeries, AntecedentValue> entry2 : timeSeriesEntry2) {
if (!entry1.equals(entry2)) {
dbRuleService.saveRule(entry1.getKey().getTimeSeriesType(), entry1.getValue(),
entry2.getKey().getTimeSeriesType(), entry2.getValue(),
markupRow.getMarkup());
}
}
}
});
}*/
public List<Markup> 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<TimeSeriesForMarkup> getTimeSeriesForMarkup(List<TimeSeries> timeSeriesList) {
List<TimeSeriesForMarkup> 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<TimeSeriesValue> values1 = ts1.getValues();
for (int k = 0; k < values1.size()-1; k++) {
for (int k = 0; k < values1.size() - 1; k++) {
List<TimeSeriesValue> 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());
}
}

View File

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

View File

@ -22,7 +22,9 @@
</option>
</select>
<script th:inline="javascript">
$('#select-branch').val([[*{branchId}]]);
$('#select-branch').val([[ * {branchId}]
])
;
$('#select-branch').selectpicker('refresh');
</script>
</div>
@ -31,22 +33,15 @@
value="Применить"/>
</div>
</div>
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col" colspan="3">Охарактеризуйте периоды вашего проекта</th>
</tr>
</thead>
<tbody>
<tr th:each="m, itemStat: *{timeSeriesForMarkupList}">
<!-- <td><input type="hidden" th:field="*{markupRows[__${itemStat.index}__].timeSeries.id}"></span></td>-->
<td><span class="badge badge-success" th:text="${m.dateFrom}"></span></td>
<td><span class="badge badge-success" th:text="${m.dateTo}"></span></td>
<td><input type="text" ></input></td>
</tr>
</tbody>
</table>
<input type="submit" class="btn btn-outline-success w-100" value="Сохранить"/>
<h5 class="mb-3" th:if="*{timeSeriesForMarkupList != null && #lists.size(timeSeriesForMarkupList) > 0}">
Охарактеризуйте периоды вашего проекта</h5>
<div class="form-group" th:each="m, itemStat: *{timeSeriesForMarkupList}">
<!-- <td><input type="hidden" th:field="*{markupRows[__${itemStat.index}__].timeSeries.id}"></span></td>-->
<label th:text="${#dates.format(m.dateFrom, 'dd.MM.yyyy HH:mm')} + ' - ' + ${#dates.format(m.dateTo, 'dd.MM.yyyy HH:mm')}"></label>
<textarea class="form-control"></textarea>
</div>
<input type="submit" th:if="*{timeSeriesForMarkupList != null && #lists.size(timeSeriesForMarkupList) > 0}"
class="btn btn-outline-success w-100" value="Сгенерировать правила"/>
</form>
</div>
</html>