From 1873df74a1d617051ed5c1be3a09312a15c266b3 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 27 Feb 2025 11:23:37 +0400 Subject: [PATCH 01/17] #4 -- Read rule string --- src/main/resources/templates/rule/edit.html | 31 ++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/resources/templates/rule/edit.html b/src/main/resources/templates/rule/edit.html index bbe6824..83d97e1 100644 --- a/src/main/resources/templates/rule/edit.html +++ b/src/main/resources/templates/rule/edit.html @@ -26,25 +26,29 @@
-
-
-
-
@@ -57,6 +61,25 @@ Удалить Отмена + -- 2.34.1 From 3359db7b978b7450f51184dfd2227a6df2027eab Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 27 Feb 2025 11:39:07 +0400 Subject: [PATCH 02/17] #4 -- Add ajax methods for variables and terms --- .../rule/controller/FuzzyRuleController.java | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java index 7febe09..5ab299e 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java @@ -8,16 +8,29 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; import ru.ulstu.fc.rule.model.FuzzyRuleForm; +import ru.ulstu.fc.rule.model.FuzzyTerm; +import ru.ulstu.fc.rule.model.Variable; import ru.ulstu.fc.rule.service.FuzzyRuleService; +import ru.ulstu.fc.rule.service.FuzzyTermService; +import ru.ulstu.fc.rule.service.VariableService; + +import java.util.List; @Controller @RequestMapping("rule") public class FuzzyRuleController { - private final FuzzyRuleService ruleService; + private final FuzzyRuleService fuzzyRuleService; + private final VariableService variableService; + private final FuzzyTermService fuzzyTermService; - public FuzzyRuleController(FuzzyRuleService ruleService) { - this.ruleService = ruleService; + public FuzzyRuleController(FuzzyRuleService fuzzyRuleService, + VariableService variableService, + FuzzyTermService fuzzyTermService) { + this.fuzzyRuleService = fuzzyRuleService; + this.variableService = variableService; + this.fuzzyTermService = fuzzyTermService; } @GetMapping("/edit/{projectId}/{ruleId}") @@ -26,7 +39,7 @@ public class FuzzyRuleController { model.addAttribute("projectId", projectId); model.addAttribute("fuzzyRuleForm", (id != null && id != 0) - ? new FuzzyRuleForm(id, ruleService.getById(id)) + ? new FuzzyRuleForm(id, fuzzyRuleService.getById(id)) : new FuzzyRuleForm(id, projectId)); return "rule/edit"; } @@ -37,15 +50,27 @@ public class FuzzyRuleController { model.addAttribute("projectId", fuzzyRuleForm.getProjectId()); return "rule/edit"; } - ruleService.save(fuzzyRuleForm); + fuzzyRuleService.save(fuzzyRuleForm); return "redirect:/project/edit/" + fuzzyRuleForm.getProjectId(); } @PostMapping(value = "save", params = "delete") public String delete(FuzzyRuleForm fuzzyRuleForm) { if (fuzzyRuleForm != null && fuzzyRuleForm.getId() != null) { - ruleService.delete(fuzzyRuleForm); + fuzzyRuleService.delete(fuzzyRuleForm); } return "redirect:/project/edit/" + fuzzyRuleForm.getProjectId(); } + + @ResponseBody + @GetMapping("/getVariables/{projectId}") + public List getVariables(@PathVariable("projectId") Integer projectId) { + return variableService.getAllByProject(projectId); + } + + @ResponseBody + @GetMapping("/getFuzzyTerms/{variableId}") + public List getTerms(@PathVariable("variableId") Integer variableId) { + return fuzzyTermService.getByVariableId(variableId); + } } -- 2.34.1 From 235bedb510314e47c1bae1da09f7994bbe2e6787 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 28 Feb 2025 11:08:46 +0400 Subject: [PATCH 03/17] #4 -- Add event listeners on value change --- .../rule/controller/FuzzyRuleController.java | 1 + src/main/resources/public/js/fuzzyRule.js | 126 ++++++++++++++++++ src/main/resources/templates/rule/edit.html | 28 ++-- 3 files changed, 137 insertions(+), 18 deletions(-) create mode 100644 src/main/resources/public/js/fuzzyRule.js diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java index 5ab299e..c0c291c 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java @@ -65,6 +65,7 @@ public class FuzzyRuleController { @ResponseBody @GetMapping("/getVariables/{projectId}") public List getVariables(@PathVariable("projectId") Integer projectId) { + //TODO: return DTO without terms return variableService.getAllByProject(projectId); } diff --git a/src/main/resources/public/js/fuzzyRule.js b/src/main/resources/public/js/fuzzyRule.js new file mode 100644 index 0000000..5d775f3 --- /dev/null +++ b/src/main/resources/public/js/fuzzyRule.js @@ -0,0 +1,126 @@ +/* exported MessageTypesEnum */ +var MessageTypesEnum = { + INFO: "info", + SUCCESS: "success", + WARNING: "warning", + DANGER: "danger" +}; +Object.freeze(MessageTypesEnum); + +function isEmpty(value) { + if (typeof value === "function") { + return false; + } + return (value == null || value.length === 0); +} + +/* exported showFeedbackMessage */ +function showFeedbackMessage(message, type) { + alert(type + ' ' + message); +} +/* exported errorHandler */ +function errorHandler(response, callBack, errorCallBack) { + if (!isEmpty(response.error)) { + // TODO: add l10n + // showFeedbackMessage(response.error.code + ": " + response.error.message, MessageTypesEnum.DANGER); + if (!isEmpty(errorCallBack)) { + errorCallBack(response.data); + } + throw response.error.code + ": " + response.error.message + + " / Details: " + response.error.data; + } + if (!isEmpty(callBack)) { + callBack(response); + } +} + +/* exported getFromRest */ +function getFromRest(url, callBack) { + $.ajax({ + url: url, + cache: false, + success: function (response) { + errorHandler(response, callBack); + } + }); +} + +/* exported createRule */ + function createRule(ruleContentElement) { + var ruleString = "if "; + var inp = $('.selectpicker.inputVar').map(function() {return $(this).val();}).get(); + var inpVal = $('.selectpicker.inputVal').map(function() {return $(this).val();}).get(); + for (var i = 0; i < inp.length; i++) { + if (i > 0) { + ruleString += ' and '; + } + ruleString += inp[i] + " is " + inpVal[i]; + } + $(ruleContentElement).val(ruleString); +} + +/* exported fillSelect */ +function fillSelect(selectElement, values) { + $(selectElement).html(""); + $.each(values, function (key, value) { + $(selectElement).append($("