From d8f51fefc696ff4dd1b1eade731e70c73b5a4390 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 25 Feb 2025 15:48:57 +0400 Subject: [PATCH 1/4] #17 -- Add some rule parsing --- .../FuzzyRuleParseRestController.java | 20 +++++++++ .../ru/ulstu/fc/rule/model/FuzzyRule.java | 5 +++ .../rule/service/FuzzyRuleParseService.java | 41 ++++++++++++++++++- .../fc/rule/service/VariableService.java | 13 ++++++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleParseRestController.java diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleParseRestController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleParseRestController.java new file mode 100644 index 0000000..e2518c1 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleParseRestController.java @@ -0,0 +1,20 @@ +package ru.ulstu.fc.rule.controller; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; +import ru.ulstu.fc.rule.service.FuzzyRuleParseService; + +@RestController("fuzzyRuleParseRest") +public class FuzzyRuleParseRestController { + private final FuzzyRuleParseService fuzzyRuleParseService; + + public FuzzyRuleParseRestController(FuzzyRuleParseService fuzzyRuleParseService) { + this.fuzzyRuleParseService = fuzzyRuleParseService; + } + + @PostMapping("parse/{projectId}") + public void parseRule(@PathVariable("projectId") Integer projectId, String data) { + fuzzyRuleParseService.parseFuzzyRules(data, projectId); + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/FuzzyRule.java b/src/main/java/ru/ulstu/fc/rule/model/FuzzyRule.java index 04b07ec..261c51c 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/FuzzyRule.java +++ b/src/main/java/ru/ulstu/fc/rule/model/FuzzyRule.java @@ -18,6 +18,11 @@ public class FuzzyRule extends BaseEntity { public FuzzyRule() { } + public FuzzyRule(String content, Project project) { + this.content = content; + this.project = project; + } + public Project getProject() { return project; } diff --git a/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleParseService.java b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleParseService.java index 5209a70..065f87b 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleParseService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleParseService.java @@ -3,7 +3,11 @@ package ru.ulstu.fc.rule.service; import com.fuzzylite.Engine; import com.fuzzylite.rule.Rule; import org.springframework.stereotype.Service; +import ru.ulstu.fc.project.service.ProjectService; +import ru.ulstu.fc.rule.model.FuzzyRule; +import ru.ulstu.fc.rule.model.Variable; +import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -20,9 +24,15 @@ public class FuzzyRuleParseService { private final static String ENG_IS_STATEMENT = "is"; private final Engine fuzzyEngine; + private final VariableService variableService; + private final ProjectService projectService; - public FuzzyRuleParseService(Engine fuzzyEngine) { + public FuzzyRuleParseService(Engine fuzzyEngine, + VariableService variableService, + ProjectService projectService) { this.fuzzyEngine = fuzzyEngine; + this.variableService = variableService; + this.projectService = projectService; } public List parseRules(List stringRules) { @@ -41,4 +51,33 @@ public class FuzzyRuleParseService { .replaceFirst(RU_EQ_STATEMENT, ENG_IS_STATEMENT) .replaceAll(RU_THEN_STATEMENT, ENG_THEN_STATEMENT); } + + public List parseFuzzyRules(String stringRules, Integer projectId) { + return Arrays.stream(stringRules.split("\n")).toList() + .stream() + .map(s -> parseFuzzyRule(replaceEnglishKeywords(s), projectId)) + .collect(Collectors.toList()); + } + + private FuzzyRule parseFuzzyRule(String stringRule, Integer projectId) { + String[] antecedentComponents = getAntecedentComponents(getAntecedent(stringRule)); + getVariables(antecedentComponents) + .stream() + .map(v -> variableService.save(v, projectId)); + return new FuzzyRule(stringRule, projectService.getById(projectId)); + } + + private String getAntecedent(String stringRule) { + return stringRule.split(ENG_IF_STATEMENT)[1].trim().split(ENG_THEN_STATEMENT)[0].trim(); + } + + private String[] getAntecedentComponents(String stringAntecedent) { + return stringAntecedent.split(ENG_AND_STATEMENT); + } + + private List getVariables(String[] stringAntecedentComponents) { + return Arrays.stream(stringAntecedentComponents) + .map(a -> new Variable(a.split(ENG_IS_STATEMENT)[0].trim())) + .toList(); + } } diff --git a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java index 5fca4eb..0fcf6fd 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java @@ -36,6 +36,19 @@ public class VariableService { return variableRepository.save(variable); } + public Variable save(Variable variable, Integer projectId) { + Variable dbVariable; + if (variable.getId() == null || variable.getId() == 0) { + dbVariable = new Variable(); + } else { + dbVariable = getById(variable.getId()); + } + dbVariable.setProject(projectService.getById(projectId)); + dbVariable.setName(variable.getName()); + dbVariable.setInput(variable.isInput()); + return variableRepository.save(variable); + } + public void delete(VariableForm ruleForm) { getById(ruleForm.getId()); variableRepository.deleteById(ruleForm.getId()); -- 2.34.1 From 9ad33c4e79996c45b6b55564fdd1fcb6cbb98f24 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 25 Feb 2025 16:00:41 +0400 Subject: [PATCH 2/4] #17 -- REST CRUD for terms --- .../controller/FuzzyTermRestController.java | 47 +++++++++++++++++++ .../ru/ulstu/fc/rule/model/FuzzyTermForm.java | 4 ++ .../fc/rule/service/FuzzyTermService.java | 4 ++ 3 files changed, 55 insertions(+) create mode 100644 src/main/java/ru/ulstu/fc/rule/controller/FuzzyTermRestController.java diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyTermRestController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyTermRestController.java new file mode 100644 index 0000000..962f5b2 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyTermRestController.java @@ -0,0 +1,47 @@ +package ru.ulstu.fc.rule.controller; + +import jakarta.validation.Valid; +import org.springframework.web.bind.annotation.DeleteMapping; +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.RestController; +import ru.ulstu.fc.rule.model.FuzzyTerm; +import ru.ulstu.fc.rule.model.FuzzyTermForm; +import ru.ulstu.fc.rule.service.FuzzyTermService; + +import java.util.List; + +@RestController("fuzzyTermRest") +public class FuzzyTermRestController { + private final FuzzyTermService fuzzyTermService; + + public FuzzyTermRestController(FuzzyTermService fuzzyTermService) { + this.fuzzyTermService = fuzzyTermService; + } + + @GetMapping("/list/{projectId}/{variableId}") + public List getList(@PathVariable(value = "projectId") Integer projectId, + @PathVariable(value = "variableId") Integer variableId) { + return fuzzyTermService.getAll(projectId, variableId); + } + + @GetMapping("/get/{projectId}/{variableId}/{fuzzyTermId}") + public FuzzyTerm getById(@PathVariable(value = "projectId") Integer projectId, + @PathVariable(value = "variableId") Integer variableId, + @PathVariable(value = "fuzzyTermId") Integer fuzzyTermId) { + return fuzzyTermService.getById(fuzzyTermId); + } + + @PostMapping + public FuzzyTerm save(@Valid FuzzyTermForm fuzzyTermForm) { + return fuzzyTermService.save(fuzzyTermForm); + } + + @DeleteMapping + public void delete(@Valid FuzzyTermForm fuzzyTermForm) { + if (fuzzyTermForm != null && fuzzyTermForm.getId() != null) { + fuzzyTermService.delete(fuzzyTermForm); + } + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/FuzzyTermForm.java b/src/main/java/ru/ulstu/fc/rule/model/FuzzyTermForm.java index 755fda3..e660993 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/FuzzyTermForm.java +++ b/src/main/java/ru/ulstu/fc/rule/model/FuzzyTermForm.java @@ -1,7 +1,11 @@ package ru.ulstu.fc.rule.model; +import jakarta.validation.constraints.NotNull; + public class FuzzyTermForm { + @NotNull private Integer projectId; + @NotNull private Integer variableId; private Integer id; private String description; diff --git a/src/main/java/ru/ulstu/fc/rule/service/FuzzyTermService.java b/src/main/java/ru/ulstu/fc/rule/service/FuzzyTermService.java index a9f6d2c..1548f13 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyTermService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyTermService.java @@ -53,4 +53,8 @@ public class FuzzyTermService { } return variableService.getById(variableId).getFuzzyTerms(); } + + public List getAll(Integer projectId, Integer variableId) { + return getByVariableId(variableId); + } } -- 2.34.1 From d10db93950d44a7e2e9b0d53f8bc06b81cfea012 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 25 Feb 2025 16:09:22 +0400 Subject: [PATCH 3/4] #17 -- REST CRUD for variables --- .../controller/FuzzyTermRestController.java | 4 +- .../controller/VariableRestController.java | 47 +++++++++++++++++++ .../rule/repository/VariableRepository.java | 2 + .../fc/rule/service/VariableService.java | 6 +++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ru/ulstu/fc/rule/controller/VariableRestController.java diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyTermRestController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyTermRestController.java index 962f5b2..4247ef5 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyTermRestController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyTermRestController.java @@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.DeleteMapping; 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.RestController; import ru.ulstu.fc.rule.model.FuzzyTerm; import ru.ulstu.fc.rule.model.FuzzyTermForm; @@ -12,7 +13,8 @@ import ru.ulstu.fc.rule.service.FuzzyTermService; import java.util.List; -@RestController("fuzzyTermRest") +@RestController +@RequestMapping("fuzzyTermRest") public class FuzzyTermRestController { private final FuzzyTermService fuzzyTermService; diff --git a/src/main/java/ru/ulstu/fc/rule/controller/VariableRestController.java b/src/main/java/ru/ulstu/fc/rule/controller/VariableRestController.java new file mode 100644 index 0000000..ac8ed1f --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/controller/VariableRestController.java @@ -0,0 +1,47 @@ +package ru.ulstu.fc.rule.controller; + +import jakarta.validation.Valid; +import org.springframework.web.bind.annotation.DeleteMapping; +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.RestController; +import ru.ulstu.fc.rule.model.Variable; +import ru.ulstu.fc.rule.model.VariableForm; +import ru.ulstu.fc.rule.service.VariableService; + +import java.util.List; + +@RestController +@RequestMapping("variableRest") +public class VariableRestController { + private final VariableService variableService; + + public VariableRestController(VariableService variableService) { + this.variableService = variableService; + } + + @GetMapping("/list/{projectId}") + public List getList(@PathVariable(value = "projectId") Integer projectId) { + return variableService.getAllByProject(projectId); + } + + @GetMapping("/get/{projectId}/{variableId}") + public Variable getById(@PathVariable(value = "projectId") Integer projectId, + @PathVariable(value = "variableId") Integer variableId) { + return variableService.getById(variableId); + } + + @PostMapping + public Variable save(@Valid VariableForm variableForm) { + return variableService.save(variableForm); + } + + @DeleteMapping + public void delete(@Valid VariableForm variableForm) { + if (variableForm != null && variableForm.getId() != null) { + variableService.delete(variableForm); + } + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java b/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java index f228db7..9499f09 100644 --- a/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java +++ b/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java @@ -9,4 +9,6 @@ import java.util.List; public interface VariableRepository extends JpaRepository { List findByProject(Project project); + + List getByProject(Project project); } diff --git a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java index 0fcf6fd..20cc790 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java @@ -7,6 +7,8 @@ import ru.ulstu.fc.rule.model.Variable; import ru.ulstu.fc.rule.model.VariableForm; import ru.ulstu.fc.rule.repository.VariableRepository; +import java.util.List; + @Service public class VariableService { private final VariableRepository variableRepository; @@ -59,4 +61,8 @@ public class VariableService { var.getFuzzyTerms().add(ft); variableRepository.save(var); } + + public List getAllByProject(Integer projectId) { + return variableRepository.getByProject(projectService.getById(projectId)); + } } -- 2.34.1 From 92be5c9ac6767140dffd5bffdaafdb6ddc42c4b5 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 25 Feb 2025 18:23:17 +0400 Subject: [PATCH 4/4] #17 -- REST CRUD for rules --- .../controller/FuzzyRuleRestController.java | 40 +++++++++++++++++++ .../fc/rule/service/FuzzyRuleService.java | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java new file mode 100644 index 0000000..03469d9 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java @@ -0,0 +1,40 @@ +package ru.ulstu.fc.rule.controller; + +import jakarta.validation.Valid; +import org.springframework.web.bind.annotation.DeleteMapping; +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.RestController; +import ru.ulstu.fc.rule.model.FuzzyRule; +import ru.ulstu.fc.rule.model.FuzzyRuleForm; +import ru.ulstu.fc.rule.service.FuzzyRuleService; + +@RestController +@RequestMapping("fuzzyRuleRest") +public class FuzzyRuleRestController { + private final FuzzyRuleService ruleService; + + public FuzzyRuleRestController(FuzzyRuleService ruleService) { + this.ruleService = ruleService; + } + + @GetMapping("/get/{projectId}/{ruleId}") + public FuzzyRule get(@PathVariable(value = "projectId") Integer projectId, + @PathVariable(value = "ruleId") Integer id) { + return ruleService.getById(id); + } + + @PostMapping + public FuzzyRule save(@Valid FuzzyRuleForm fuzzyRuleForm) { + return ruleService.save(fuzzyRuleForm); + } + + @DeleteMapping + public void delete(@Valid FuzzyRuleForm fuzzyRuleForm) { + if (fuzzyRuleForm != null && fuzzyRuleForm.getId() != null) { + ruleService.delete(fuzzyRuleForm); + } + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java index e4df2b7..8093133 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java @@ -22,7 +22,7 @@ public class FuzzyRuleService { .orElseThrow(() -> new RuntimeException("Rule not found by id")); } - public Object save(FuzzyRuleForm ruleForm) { + public FuzzyRule save(FuzzyRuleForm ruleForm) { FuzzyRule rule; if (ruleForm.getId() == null || ruleForm.getId() == 0) { rule = new FuzzyRule(); -- 2.34.1