#3 -- Add rule controller

This commit is contained in:
Anton Romanov 2023-09-11 19:05:13 +04:00
parent 1c8eabfc08
commit b88ed0211f
5 changed files with 101 additions and 63 deletions

View File

@ -0,0 +1,37 @@
package ru.ulstu.fc.rule.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import ru.ulstu.fc.rule.model.AddRuleForm;
import ru.ulstu.fc.rule.service.RuleParseService;
import java.util.List;
@Controller
public class RuleController {
private final RuleParseService ruleParseService;
public RuleController(RuleParseService ruleParseService) {
this.ruleParseService = ruleParseService;
}
@GetMapping("addRule")
public String addRule(Model model) {
model.addAttribute("addRuleForm", new AddRuleForm());
return "addRule";
}
@PostMapping("addRule")
public String parse(@ModelAttribute AddRuleForm addRuleForm, Model model) {
try {
System.out.println(ruleParseService.parseRules(List.of(addRuleForm.getRule())));
} catch (Exception ex) {
return "addRule";
}
model.addAttribute("addRuleForm", addRuleForm);
return "listRules";
}
}

View File

@ -0,0 +1,13 @@
package ru.ulstu.fc.rule.model;
public class AddRuleForm {
private String rule;
public String getRule() {
return rule;
}
public void setRule(String rule) {
this.rule = rule;
}
}

View File

@ -0,0 +1,44 @@
package ru.ulstu.fc.rule.service;
import com.fuzzylite.Engine;
import com.fuzzylite.rule.Rule;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class RuleParseService {
private final static String RU_IF_STATEMENT = "если";
private final static String ENG_IF_STATEMENT = "if";
private final static String RU_THEN_STATEMENT = "то";
private final static String ENG_THEN_STATEMENT = "then";
private final static String RU_AND_STATEMENT = "и";
private final static String ENG_AND_STATEMENT = "and";
private final static String RU_IS_STATEMENT = "является";
private final static String RU_EQ_STATEMENT = "равно";
private final static String ENG_IS_STATEMENT = "is";
private final Engine fuzzyEngine;
public RuleParseService(Engine fuzzyEngine) {
this.fuzzyEngine = fuzzyEngine;
}
public List<Rule> parseRules(List<String> stringRules) {
return stringRules
.stream()
.map(s -> Rule.parse(replaceEnglishKeywords(s), fuzzyEngine))
.collect(Collectors.toList());
}
private String replaceEnglishKeywords(String stringRule) {
stringRule = stringRule.toLowerCase();
return stringRule
.replaceFirst(RU_IF_STATEMENT, ENG_IF_STATEMENT)
.replaceFirst(RU_AND_STATEMENT, ENG_AND_STATEMENT)
.replaceFirst(RU_IS_STATEMENT, ENG_IS_STATEMENT)
.replaceFirst(RU_EQ_STATEMENT, ENG_IS_STATEMENT)
.replaceAll(RU_THEN_STATEMENT, ENG_THEN_STATEMENT);
}
}

View File

@ -7,75 +7,16 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</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="/addRule" 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">
Если <input class="form-control" type="text" th:field="*{rule}">
</div>
<div class="col-md-6 col-sm-12">
<select id="select-antecedent" class="selectpicker m-2" data-live-search="true"
th:field="*{firstAntecedentId}"
data-width="90%">
<option th:each="antecedent : ${antecedents}"
th:value="${antecedent}"
th:utext="${antecedent.description}">
</option>
</select>
</div>
<div class="col-md-2 col-sm-12">
имеет тенденцию
</div>
<div class="col-md-2 col-sm-12">
<select id="select-measures" class="selectpicker m-2" data-live-search="true"
th:field="*{firstAntecedentValueId}"
data-width="100%">
<option th:each="antecedentValue : ${antecedentValues}"
th:value="${antecedentValue.id}"
th:utext="${antecedentValue.antecedentValue}">
</option>
</select>
</div>
<div class="col-md-2 col-sm-12">
и
</div>
<div class="col-md-6 col-sm-12">
<select id="select-second-antecedent" class="selectpicker m-2" data-live-search="true"
th:field="*{secondAntecedentId}"
data-width="90%">
<option th:each="antecedent : ${antecedents}"
th:value="${antecedent}"
th:utext="${antecedent.description}">
</option>
</select>
</div>
<div class="col-md-2 col-sm-12">
имеет тенденцию
</div>
<div class="col-md-2 col-sm-12">
<select id="select-second-measures" class="selectpicker m-2" data-live-search="true"
th:field="*{secondAntecedentValueId}"
data-width="100%">
<option th:each="antecedentValue : ${antecedentValues}"
th:value="${antecedentValue.id}"
th:utext="${antecedentValue.antecedentValue}">
</option>
</select>
</div>
<div class="col-md-2 col-sm-12">
то:
</div>
<div class="col-md-6 col-sm-12">
<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 class="col-md-4 col-sm-12">
<input type="submit" class="btn btn-outline-success m-2" th:if="*{ruleId == null}" <input type="submit" class="btn btn-outline-success m-2" value="Создать правило"/>
value="Создать правило"/>
<input type="submit" class="btn btn-outline-success m-2" th:if="*{ruleId != null}"
value="Сохранить правило"/>
</div> </div>
</div> </div>
</form> </form>

View File

@ -24,6 +24,9 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/listRules">Правила</a> <a class="nav-link" href="/listRules">Правила</a>
</li> </li>
<li class="nav-item">
<a class="nav-link" href="/addRule">Добавить правило</a>
</li>
</ul> </ul>
</div> </div>
</nav> </nav>