Merge branch 'master' into 74-add-jFuzzyLogic

# Conflicts:
#	src/main/java/ru/ulstu/extractor/core/Route.java
#	src/main/java/ru/ulstu/extractor/rule/service/RuleService.java
#	src/main/resources/templates/listRules.html
This commit is contained in:
Anton Romanov 2022-10-25 15:40:27 +04:00
commit 12c28b9ba0
7 changed files with 95 additions and 29 deletions

View File

@ -19,6 +19,7 @@ public class Route {
public static final String LIST_RULE = "listRules"; public static final String LIST_RULE = "listRules";
public static final String ADD_RULE = "addRule"; public static final String ADD_RULE = "addRule";
public static final String RECOMMENDATIONS = "recommendations"; public static final String RECOMMENDATIONS = "recommendations";
public static final String DELETE_RULE = "deleteRule";
public static String getLIST_INDEXED_REPOSITORIES() { public static String getLIST_INDEXED_REPOSITORIES() {
return LIST_INDEXED_REPOSITORIES; return LIST_INDEXED_REPOSITORIES;

View File

@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes; import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import ru.ulstu.extractor.rule.model.AddRuleForm; import ru.ulstu.extractor.rule.model.AddRuleForm;
import ru.ulstu.extractor.rule.service.AntecedentValueService; import ru.ulstu.extractor.rule.service.AntecedentValueService;
@ -14,6 +15,7 @@ import ru.ulstu.extractor.ts.service.TimeSeriesService;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
import static ru.ulstu.extractor.core.Route.ADD_RULE; import static ru.ulstu.extractor.core.Route.ADD_RULE;
import static ru.ulstu.extractor.core.Route.DELETE_RULE;
import static ru.ulstu.extractor.core.Route.LIST_RULE; import static ru.ulstu.extractor.core.Route.LIST_RULE;
@Controller @Controller
@ -38,10 +40,10 @@ public class RuleController {
} }
@GetMapping(ADD_RULE) @GetMapping(ADD_RULE)
public String getAntecedent(Model model) { public String getAntecedent(Model model, @RequestParam(required = false) Integer ruleId) {
model.addAttribute("antecedentValues", antecedentValueService.getList()); model.addAttribute("antecedentValues", antecedentValueService.getList());
model.addAttribute("antecedents", timeSeriesService.getAllTimeSeriesTypes()); model.addAttribute("antecedents", timeSeriesService.getAllTimeSeriesTypes());
model.addAttribute("addRuleForm", new AddRuleForm()); model.addAttribute("addRuleForm", ruleService.getAddRuleFormOrDefault(ruleId));
return ADD_RULE; return ADD_RULE;
} }
@ -50,4 +52,10 @@ public class RuleController {
ruleService.saveRule(ruleForm); ruleService.saveRule(ruleForm);
return "redirect:/" + LIST_RULE; return "redirect:/" + LIST_RULE;
} }
@GetMapping(DELETE_RULE)
public String deleteRule(Model model, @RequestParam Integer id) {
ruleService.deleteById(id);
return "redirect:/" + LIST_RULE;
}
} }

View File

@ -1,6 +1,7 @@
package ru.ulstu.extractor.rule.model; package ru.ulstu.extractor.rule.model;
public class AddRuleForm { public class AddRuleForm {
private Integer ruleId;
private Integer firstAntecedentValueId; private Integer firstAntecedentValueId;
private String firstAntecedentId; private String firstAntecedentId;
private Integer secondAntecedentValueId; private Integer secondAntecedentValueId;
@ -10,6 +11,23 @@ public class AddRuleForm {
public AddRuleForm() { public AddRuleForm() {
} }
public AddRuleForm(Rule rule) {
this.ruleId = rule.getId();
this.firstAntecedentId = rule.getFirstAntecedent().name();
this.secondAntecedentId = rule.getSecondAntecedent().name();
this.firstAntecedentValueId = rule.getFirstAntecedentValue().getId();
this.secondAntecedentValueId = rule.getSecondAntecedentValue().getId();
this.consequent = rule.getConsequent();
}
public Integer getRuleId() {
return ruleId;
}
public void setRuleId(Integer ruleId) {
this.ruleId = ruleId;
}
public Integer getFirstAntecedentValueId() { public Integer getFirstAntecedentValueId() {
return firstAntecedentValueId; return firstAntecedentValueId;
} }

View File

@ -28,10 +28,36 @@ public class RuleService {
} }
public void saveRule(AddRuleForm addRuleForm) { public void saveRule(AddRuleForm addRuleForm) {
if (addRuleForm.getRuleId() != null) {
DbRule rule = ruleRepository.getOne(addRuleForm.getRuleId());
rule.setConsequent(addRuleForm.getConsequent());
rule.setFirstAntecedent(TimeSeriesType.valueOf(addRuleForm.getFirstAntecedentId()));
rule.setFirstAntecedentValue(antecedentValueService.getById(addRuleForm.getFirstAntecedentValueId()));
rule.setSecondAntecedent(TimeSeriesType.valueOf(addRuleForm.getSecondAntecedentId()));
rule.setSecondAntecedentValue(antecedentValueService.getById(addRuleForm.getSecondAntecedentValueId()));
ruleRepository.save(rule);
} else {
ruleRepository.save(new DbRule(antecedentValueService.getById(addRuleForm.getFirstAntecedentValueId()), ruleRepository.save(new DbRule(antecedentValueService.getById(addRuleForm.getFirstAntecedentValueId()),
TimeSeriesType.valueOf(addRuleForm.getFirstAntecedentId()), TimeSeriesType.valueOf(addRuleForm.getFirstAntecedentId()),
antecedentValueService.getById(addRuleForm.getSecondAntecedentValueId()), antecedentValueService.getById(addRuleForm.getSecondAntecedentValueId()),
TimeSeriesType.valueOf(addRuleForm.getSecondAntecedentId()), TimeSeriesType.valueOf(addRuleForm.getSecondAntecedentId()),
addRuleForm.getConsequent())); addRuleForm.getConsequent()));
} }
}
public Rule findById(Integer id) {
return ruleRepository.getOne(id);
}
public AddRuleForm getAddRuleFormOrDefault(Integer id) {
return id != null
? new AddRuleForm(ruleRepository
.findById(id)
.orElseThrow(() -> new RuntimeException("Правило не найдено")))
: new AddRuleForm();
}
public void deleteById(Integer id) {
ruleRepository.deleteById(id);
}
} }

View File

@ -8,12 +8,13 @@
</head> </head>
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<form action="/listRule" th:action="${@route.ADD_RULE}" th:object="${addRuleForm}" method="post"> <form action="/listRule" th:action="${@route.ADD_RULE}" th:object="${addRuleForm}" method="post">
<input type="hidden" th:field="*{ruleId}">
<div class="row"> <div class="row">
<div class="col-md-2 col-sm-12"> <div class="col-md-2 col-sm-12">
Если Если
</div> </div>
<div class="col-md-6 col-sm-12"> <div class="col-md-6 col-sm-12">
<select id="select-antecedent" class="selectpicker" data-live-search="true" <select id="select-antecedent" class="selectpicker m-2" data-live-search="true"
th:field="*{firstAntecedentId}" th:field="*{firstAntecedentId}"
data-width="90%"> data-width="90%">
<option th:each="antecedent : ${antecedents}" <option th:each="antecedent : ${antecedents}"
@ -26,7 +27,7 @@
имеет тенденцию имеет тенденцию
</div> </div>
<div class="col-md-2 col-sm-12"> <div class="col-md-2 col-sm-12">
<select id="select-measures" class="selectpicker" data-live-search="true" <select id="select-measures" class="selectpicker m-2" data-live-search="true"
th:field="*{firstAntecedentValueId}" th:field="*{firstAntecedentValueId}"
data-width="100%"> data-width="100%">
<option th:each="antecedentValue : ${antecedentValues}" <option th:each="antecedentValue : ${antecedentValues}"
@ -39,7 +40,7 @@
и и
</div> </div>
<div class="col-md-6 col-sm-12"> <div class="col-md-6 col-sm-12">
<select id="select-second-antecedent" class="selectpicker" data-live-search="true" <select id="select-second-antecedent" class="selectpicker m-2" data-live-search="true"
th:field="*{secondAntecedentId}" th:field="*{secondAntecedentId}"
data-width="90%"> data-width="90%">
<option th:each="antecedent : ${antecedents}" <option th:each="antecedent : ${antecedents}"
@ -52,7 +53,7 @@
имеет тенденцию имеет тенденцию
</div> </div>
<div class="col-md-2 col-sm-12"> <div class="col-md-2 col-sm-12">
<select id="select-second-measures" class="selectpicker" data-live-search="true" <select id="select-second-measures" class="selectpicker m-2" data-live-search="true"
th:field="*{secondAntecedentValueId}" th:field="*{secondAntecedentValueId}"
data-width="100%"> data-width="100%">
<option th:each="antecedentValue : ${antecedentValues}" <option th:each="antecedentValue : ${antecedentValues}"
@ -65,13 +66,16 @@
то: то:
</div> </div>
<div class="col-md-6 col-sm-12"> <div class="col-md-6 col-sm-12">
<input type="text" class="form-control" th:field="*{consequent}"> <input type="text" class="form-control m-1" th:field="*{consequent}">
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="col-md-4 col-sm-12"></div> <div class="col-md-4 col-sm-12"></div>
<div class="col-md-4 col-sm-12"> <div class="col-md-4 col-sm-12">
<input type="submit" class="btn btn-outline-success" value="Создать правило"/> <input type="submit" class="btn btn-outline-success m-2" th:if="*{ruleId == null}"
value="Создать правило"/>
<input type="submit" class="btn btn-outline-success m-2" th:if="*{ruleId != null}"
value="Сохранить правило"/>
</div> </div>
</div> </div>
</form> </form>

View File

@ -10,7 +10,7 @@
<table class="table table-striped"> <table class="table table-striped">
<thead class="thead-dark"> <thead class="thead-dark">
<tr> <tr>
<th scope="col" colspan="8">Правила</th> <th scope="col" colspan="10">Правила</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -23,11 +23,20 @@
<td><span class="badge badge-success" th:text="${dbRule.secondAntecedentValue.antecedentValue}"></span></td> <td><span class="badge badge-success" th:text="${dbRule.secondAntecedentValue.antecedentValue}"></span></td>
<td><span class="badge badge-info">то</span></td> <td><span class="badge badge-info">то</span></td>
<td><span class="badge badge-warning" th:text="${dbRule.consequent}"></span></td> <td><span class="badge badge-warning" th:text="${dbRule.consequent}"></span></td>
<td>
<a role="button" class="btn btn-info" th:href="@{${@route.ADD_RULE} + '?ruleId=' + ${dbRule.id}}">
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
</a>
</td>
<td>
<a role="button" class="btn btn-danger" th:href="@{'deleteRule?id=' + ${dbRule.id}}"
onclick="return confirm('Удалить правило?')">
<i class="fa fa-times" aria-hidden="true"></i>
</a>
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
<div class="col-md-4 col-sm-12">
<a href="/addRule" class="btn btn-outline-success">Добавить правило</a> <a href="/addRule" class="btn btn-outline-success">Добавить правило</a>
</div>
</div> </div>
</html> </html>

View File

@ -18,8 +18,8 @@ public class TimeSeriesMapperTest {
Calendar c2 = GregorianCalendar.getInstance(); Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 5, 2, 2, 1, 1); c2.set(2020, 5, 2, 2, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый", TimeSeries timeSeries = new TimeSeries("Тестовый",
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10))); new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
Assert.assertEquals(1, timeSeries.getValues().size()); Assert.assertEquals(1, timeSeries.getValues().size());
@ -33,8 +33,8 @@ public class TimeSeriesMapperTest {
Calendar c2 = GregorianCalendar.getInstance(); Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 5, 2, 1, 1, 1); c2.set(2020, 5, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый", TimeSeries timeSeries = new TimeSeries("Тестовый",
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10))); new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
Assert.assertEquals(1, timeSeries.getValues().size()); Assert.assertEquals(1, timeSeries.getValues().size());
@ -48,8 +48,8 @@ public class TimeSeriesMapperTest {
Calendar c2 = GregorianCalendar.getInstance(); Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 5, 2, 1, 1, 1); c2.set(2020, 5, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый", TimeSeries timeSeries = new TimeSeries("Тестовый",
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10))); new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
Assert.assertEquals(1, timeSeries.getValues().size()); Assert.assertEquals(1, timeSeries.getValues().size());
@ -63,8 +63,8 @@ public class TimeSeriesMapperTest {
Calendar c2 = GregorianCalendar.getInstance(); Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 6, 2, 1, 1, 1); c2.set(2020, 6, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый", TimeSeries timeSeries = new TimeSeries("Тестовый",
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10))); new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
Assert.assertEquals(2, timeSeries.getValues().size()); Assert.assertEquals(2, timeSeries.getValues().size());
@ -78,8 +78,8 @@ public class TimeSeriesMapperTest {
Calendar c2 = GregorianCalendar.getInstance(); Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 5, 2, 1, 1, 1); c2.set(2020, 5, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый", TimeSeries timeSeries = new TimeSeries("Тестовый",
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10))); new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries);
Assert.assertEquals(1, timeSeries.getValues().size()); Assert.assertEquals(1, timeSeries.getValues().size());
@ -93,8 +93,8 @@ public class TimeSeriesMapperTest {
Calendar c2 = GregorianCalendar.getInstance(); Calendar c2 = GregorianCalendar.getInstance();
c2.set(2021, 5, 2, 1, 1, 1); c2.set(2021, 5, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый", TimeSeries timeSeries = new TimeSeries("Тестовый",
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10))); new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries);
Assert.assertEquals(2, timeSeries.getValues().size()); Assert.assertEquals(2, timeSeries.getValues().size());