From 2a978bab5f377bfd3d2b0496f4072f92ed86c523 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 4 Mar 2025 11:18:42 +0400 Subject: [PATCH 1/5] #23 -- Fix inference --- .../ru/ulstu/fc/config/SecurityConfiguration.java | 2 +- .../fc/rule/service/FuzzyInferenceService.java | 14 +++++++------- src/main/resources/logging.properties | 2 ++ src/main/resources/templates/index.html | 5 ++++- 4 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 src/main/resources/logging.properties diff --git a/src/main/java/ru/ulstu/fc/config/SecurityConfiguration.java b/src/main/java/ru/ulstu/fc/config/SecurityConfiguration.java index a07da1e..2bdf064 100644 --- a/src/main/java/ru/ulstu/fc/config/SecurityConfiguration.java +++ b/src/main/java/ru/ulstu/fc/config/SecurityConfiguration.java @@ -31,7 +31,7 @@ public class SecurityConfiguration { .headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin)) .csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests(auth -> - auth.requestMatchers("/").permitAll() + auth.requestMatchers("/", "get-inference").permitAll() .requestMatchers(permittedUrls).permitAll() .requestMatchers("/swagger-ui.html").hasAuthority(UserRoleConstants.ADMIN) .anyRequest().authenticated()) diff --git a/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java b/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java index c8d251c..7c3cefd 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java @@ -24,6 +24,7 @@ import ru.ulstu.fc.rule.model.ProjectInferenceData; import ru.ulstu.fc.rule.model.Variable; import ru.ulstu.fc.rule.model.dto.VariableValueDto; +import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Map; @@ -52,7 +53,7 @@ public class FuzzyInferenceService { } private List getDemoRules() { - return List.of( + return Arrays.asList( String.format(RULE_TEMPLATE, "возраст", "молодой", "доход", "высокий", "средний"), String.format(RULE_TEMPLATE, "возраст", "средний", "доход", "высокий", "большой"), String.format(RULE_TEMPLATE, "возраст", "старый", "доход", "средний", "средний") @@ -64,7 +65,7 @@ public class FuzzyInferenceService { input.setName(variable.getName()); input.setDescription(""); variable.getFuzzyTerms().sort(Comparator.comparing(FuzzyTerm::getCrispValue)); - input.setRange(0, variable.getFuzzyTerms().get(variable.getFuzzyTerms().size() - 1).getCrispValue()); + input.setRange(0, variable.getFuzzyTerms().getLast().getCrispValue()); input.setEnabled(true); input.setLockValueInRange(false); double prev = 0; @@ -83,7 +84,7 @@ public class FuzzyInferenceService { final OutputVariable output = new OutputVariable(); output.setName(variable.getName()); output.setDescription(""); - output.setRange(0, variable.getFuzzyTerms().get(variable.getFuzzyTerms().size() - 1).getCrispValue()); + output.setRange(0, variable.getFuzzyTerms().getLast().getCrispValue()); output.setEnabled(true); output.setAggregation(new Maximum()); output.setDefuzzifier(new WeightedAverage()); @@ -117,7 +118,6 @@ public class FuzzyInferenceService { mamdani.setImplication(new AlgebraicProduct()); mamdani.setActivation(new General()); rules.forEach(r -> mamdani.addRule(Rule.parse(r, engine))); - mamdani.addRule(new Rule()); return mamdani; } @@ -141,18 +141,18 @@ public class FuzzyInferenceService { public List getFuzzyInference(Map vals) { return getFuzzyInference(getDemoRules(), vals, - List.of(new Variable("возраст", List.of( + Arrays.asList(new Variable("возраст", Arrays.asList( new FuzzyTerm("молодой", 35.0), new FuzzyTerm("средний", 60.0), new FuzzyTerm("старый", 100.0)) ), - new Variable("доход", List.of( + new Variable("доход", Arrays.asList( new FuzzyTerm("небольшой", 35000.0), new FuzzyTerm("средний", 100000.0), new FuzzyTerm("высокий", 500000.0)) ) ), - List.of(new Variable("кредит", List.of( + Arrays.asList(new Variable("кредит", Arrays.asList( new FuzzyTerm("небольшой", 20000.0), new FuzzyTerm("средний", 100000.0), new FuzzyTerm("большой", 1000000.0)))) diff --git a/src/main/resources/logging.properties b/src/main/resources/logging.properties new file mode 100644 index 0000000..37cfff2 --- /dev/null +++ b/src/main/resources/logging.properties @@ -0,0 +1,2 @@ +org.apache.tomcat.level=INFO +org.apache.tomcat.util.net.level=WARNING \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index b1a1070..c576b74 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -50,12 +50,15 @@ -
+
Размер кредита:
Степень принадлежности:
+
+ Нет результата +
-- 2.34.1 From 6c14b5942aa7d50e16747a31dde37385c585b91e Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 4 Mar 2025 11:38:03 +0400 Subject: [PATCH 2/5] #23 -- Fix buttons --- src/main/resources/templates/fuzzyTerm/edit.html | 2 +- src/main/resources/templates/project/edit.html | 4 ++-- src/main/resources/templates/rule/edit.html | 2 +- src/main/resources/templates/variable/edit.html | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/resources/templates/fuzzyTerm/edit.html b/src/main/resources/templates/fuzzyTerm/edit.html index 1663feb..fbc187d 100644 --- a/src/main/resources/templates/fuzzyTerm/edit.html +++ b/src/main/resources/templates/fuzzyTerm/edit.html @@ -48,7 +48,7 @@ onclick="return confirm('Удалить запись?')"> Удалить - Отмена + Назад diff --git a/src/main/resources/templates/project/edit.html b/src/main/resources/templates/project/edit.html index ec19560..7f8955e 100644 --- a/src/main/resources/templates/project/edit.html +++ b/src/main/resources/templates/project/edit.html @@ -24,11 +24,11 @@ - Отмена + Назад
-
+

Список переменных

diff --git a/src/main/resources/templates/rule/edit.html b/src/main/resources/templates/rule/edit.html index 04a26a9..70fa5ec 100644 --- a/src/main/resources/templates/rule/edit.html +++ b/src/main/resources/templates/rule/edit.html @@ -46,7 +46,7 @@ onclick="return confirm('Удалить запись?')"> Удалить - Отмена + Назад
+ +
+

Ввод переменных:

+
+ +
+ + +
+
+ + Назад +
+
+
+ -- 2.34.1 From a3e59c6461090e1841f65023548feb981d80754a Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 4 Mar 2025 13:50:37 +0400 Subject: [PATCH 4/5] #23 -- Add run project controller methods --- .../controller/ProjectRunController.java | 17 +++++++++++++---- .../ulstu/fc/project/model/RunProjectForm.java | 11 +++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/ulstu/fc/project/controller/ProjectRunController.java b/src/main/java/ru/ulstu/fc/project/controller/ProjectRunController.java index 03c740a..e07f0ec 100644 --- a/src/main/java/ru/ulstu/fc/project/controller/ProjectRunController.java +++ b/src/main/java/ru/ulstu/fc/project/controller/ProjectRunController.java @@ -6,11 +6,12 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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 ru.ulstu.fc.project.model.RunProjectForm; -import ru.ulstu.fc.project.service.ProjectRulesService; import ru.ulstu.fc.project.service.ProjectService; import ru.ulstu.fc.project.service.ProjectVariableService; +import ru.ulstu.fc.rule.service.FuzzyInferenceService; import ru.ulstu.fc.user.model.UserRoleConstants; @Controller @@ -19,14 +20,14 @@ import ru.ulstu.fc.user.model.UserRoleConstants; @Secured({UserRoleConstants.ADMIN}) public class ProjectRunController { private final ProjectService projectService; - private final ProjectRulesService projectRulesService; + private final FuzzyInferenceService fuzzyInferenceService; private final ProjectVariableService projectVariableService; public ProjectRunController(ProjectService projectService, - ProjectRulesService projectRulesService, + FuzzyInferenceService fuzzyInferenceService, ProjectVariableService projectVariableService) { this.projectService = projectService; - this.projectRulesService = projectRulesService; + this.fuzzyInferenceService = fuzzyInferenceService; this.projectVariableService = projectVariableService; } @@ -37,4 +38,12 @@ public class ProjectRunController { model.addAttribute("variables", projectVariableService.getInputByProjectId(projectId)); return "project/init"; } + + @PostMapping("run") + public String run(RunProjectForm runProjectForm, Model model) { + model.addAttribute("response", + fuzzyInferenceService.getFuzzyInference( + runProjectForm.getVariableValues())); + return "project/result"; + } } diff --git a/src/main/java/ru/ulstu/fc/project/model/RunProjectForm.java b/src/main/java/ru/ulstu/fc/project/model/RunProjectForm.java index 1910fe8..e60c094 100644 --- a/src/main/java/ru/ulstu/fc/project/model/RunProjectForm.java +++ b/src/main/java/ru/ulstu/fc/project/model/RunProjectForm.java @@ -1,7 +1,10 @@ package ru.ulstu.fc.project.model; +import java.util.Map; + public class RunProjectForm { private Integer projectId; + private Map variableValues; public Integer getProjectId() { return projectId; @@ -10,4 +13,12 @@ public class RunProjectForm { public void setProjectId(Integer projectId) { this.projectId = projectId; } + + public Map getVariableValues() { + return variableValues; + } + + public void setVariableValues(Map variableValues) { + this.variableValues = variableValues; + } } -- 2.34.1 From 83c9893efd4ca6b92357329a331f350ab6ce623b Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 4 Mar 2025 14:17:36 +0400 Subject: [PATCH 5/5] #23 -- Show inference result --- .../controller/ProjectRunController.java | 6 +++-- .../fc/project/model/RunProjectForm.java | 10 ++++++- .../rule/service/FuzzyInferenceService.java | 14 ++++++++++ .../resources/templates/project/init.html | 3 ++- .../resources/templates/project/result.html | 26 +++++++++++++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/templates/project/result.html diff --git a/src/main/java/ru/ulstu/fc/project/controller/ProjectRunController.java b/src/main/java/ru/ulstu/fc/project/controller/ProjectRunController.java index e07f0ec..9ef1212 100644 --- a/src/main/java/ru/ulstu/fc/project/controller/ProjectRunController.java +++ b/src/main/java/ru/ulstu/fc/project/controller/ProjectRunController.java @@ -34,15 +34,17 @@ public class ProjectRunController { @GetMapping("init/{projectId}") public String getProjects(@PathVariable(value = "projectId") Integer projectId, Model model) { model.addAttribute("project", projectService.getById(projectId)); - model.addAttribute("runProjectForm", new RunProjectForm()); + model.addAttribute("runProjectForm", new RunProjectForm(projectId)); model.addAttribute("variables", projectVariableService.getInputByProjectId(projectId)); return "project/init"; } @PostMapping("run") public String run(RunProjectForm runProjectForm, Model model) { + model.addAttribute("projectId", runProjectForm.getProjectId()); model.addAttribute("response", - fuzzyInferenceService.getFuzzyInference( + fuzzyInferenceService.getProjectFuzzyInference( + runProjectForm.getProjectId(), runProjectForm.getVariableValues())); return "project/result"; } diff --git a/src/main/java/ru/ulstu/fc/project/model/RunProjectForm.java b/src/main/java/ru/ulstu/fc/project/model/RunProjectForm.java index e60c094..7e88744 100644 --- a/src/main/java/ru/ulstu/fc/project/model/RunProjectForm.java +++ b/src/main/java/ru/ulstu/fc/project/model/RunProjectForm.java @@ -1,10 +1,18 @@ package ru.ulstu.fc.project.model; +import java.util.HashMap; import java.util.Map; public class RunProjectForm { private Integer projectId; - private Map variableValues; + private Map variableValues = new HashMap<>(); + + public RunProjectForm() { + } + + public RunProjectForm(Integer projectId) { + this.projectId = projectId; + } public Integer getProjectId() { return projectId; diff --git a/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java b/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java index 7c3cefd..5e7c3e4 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java @@ -188,4 +188,18 @@ public class FuzzyInferenceService { inputVariables, outputVariables); } + + public List getProjectFuzzyInference(Integer projectId, Map variableValues) { + List fuzzyRules = projectRulesService.getByProjectId(projectId) + .stream() + .map(FuzzyRule::getContent) + .toList(); + List inputVariables = projectVariableService.getInputByProjectId(projectId); + List outputVariables = projectVariableService.getOutputByProjectId(projectId); + + return getFuzzyInference(fuzzyRules, + variableValues, + inputVariables, + outputVariables); + } } diff --git a/src/main/resources/templates/project/init.html b/src/main/resources/templates/project/init.html index 4de51bf..20743d2 100644 --- a/src/main/resources/templates/project/init.html +++ b/src/main/resources/templates/project/init.html @@ -13,7 +13,8 @@
-