From b88ed0211faf5069d54e620f1cc3f14fe6dfa271 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 11 Sep 2023 19:05:13 +0400 Subject: [PATCH] #3 -- Add rule controller --- .../fc/rule/controller/RuleController.java | 37 ++++++++++ .../ru/ulstu/fc/rule/model/AddRuleForm.java | 13 ++++ .../fc/rule/service/RuleParseService.java | 44 ++++++++++++ src/main/resources/templates/addRule.html | 67 ++----------------- src/main/resources/templates/default.html | 3 + 5 files changed, 101 insertions(+), 63 deletions(-) create mode 100644 src/main/java/ru/ulstu/fc/rule/controller/RuleController.java create mode 100644 src/main/java/ru/ulstu/fc/rule/model/AddRuleForm.java create mode 100644 src/main/java/ru/ulstu/fc/rule/service/RuleParseService.java diff --git a/src/main/java/ru/ulstu/fc/rule/controller/RuleController.java b/src/main/java/ru/ulstu/fc/rule/controller/RuleController.java new file mode 100644 index 0000000..9dcd845 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/controller/RuleController.java @@ -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"; + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/AddRuleForm.java b/src/main/java/ru/ulstu/fc/rule/model/AddRuleForm.java new file mode 100644 index 0000000..4ddac74 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/model/AddRuleForm.java @@ -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; + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/service/RuleParseService.java b/src/main/java/ru/ulstu/fc/rule/service/RuleParseService.java new file mode 100644 index 0000000..7461a17 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/service/RuleParseService.java @@ -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 parseRules(List 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); + } +} diff --git a/src/main/resources/templates/addRule.html b/src/main/resources/templates/addRule.html index 0abb1e8..dfe987d 100644 --- a/src/main/resources/templates/addRule.html +++ b/src/main/resources/templates/addRule.html @@ -7,75 +7,16 @@
-
- +
- Если -
-
- -
-
- имеет тенденцию -
-
- -
-
- и -
-
- -
-
- имеет тенденцию -
-
- -
-
- то: -
-
- +
+
-
- - +
diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index a8d2088..916fa56 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -24,6 +24,9 @@ +