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/project/controller/ProjectRunController.java b/src/main/java/ru/ulstu/fc/project/controller/ProjectRunController.java index c2ca79a..9ef1212 100644 --- a/src/main/java/ru/ulstu/fc/project/controller/ProjectRunController.java +++ b/src/main/java/ru/ulstu/fc/project/controller/ProjectRunController.java @@ -6,10 +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.service.ProjectRulesService; +import ru.ulstu.fc.project.model.RunProjectForm; 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 @@ -18,20 +20,32 @@ 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; } @GetMapping("init/{projectId}") public String getProjects(@PathVariable(value = "projectId") Integer projectId, Model model) { model.addAttribute("project", projectService.getById(projectId)); + 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.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 new file mode 100644 index 0000000..7e88744 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/project/model/RunProjectForm.java @@ -0,0 +1,32 @@ +package ru.ulstu.fc.project.model; + +import java.util.HashMap; +import java.util.Map; + +public class RunProjectForm { + private Integer projectId; + private Map variableValues = new HashMap<>(); + + public RunProjectForm() { + } + + public RunProjectForm(Integer projectId) { + this.projectId = projectId; + } + + public Integer getProjectId() { + return projectId; + } + + public void setProjectId(Integer projectId) { + this.projectId = projectId; + } + + public Map getVariableValues() { + return variableValues; + } + + public void setVariableValues(Map variableValues) { + this.variableValues = variableValues; + } +} 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..5e7c3e4 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)))) @@ -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/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/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/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 @@ -
+
Размер кредита:
Степень принадлежности:
+
+ Нет результата +
diff --git a/src/main/resources/templates/project/edit.html b/src/main/resources/templates/project/edit.html index ec19560..cbd5bc1 100644 --- a/src/main/resources/templates/project/edit.html +++ b/src/main/resources/templates/project/edit.html @@ -21,14 +21,16 @@ + Выполнить - Отмена + Назад
-
+

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

diff --git a/src/main/resources/templates/project/init.html b/src/main/resources/templates/project/init.html new file mode 100644 index 0000000..20743d2 --- /dev/null +++ b/src/main/resources/templates/project/init.html @@ -0,0 +1,30 @@ + + + + Ввод переменных + + + +
+

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

+
+ +
+ + +
+
+ + Назад +
+
+
+ diff --git a/src/main/resources/templates/project/result.html b/src/main/resources/templates/project/result.html new file mode 100644 index 0000000..aa2af7d --- /dev/null +++ b/src/main/resources/templates/project/result.html @@ -0,0 +1,26 @@ + + + + Результат + + + +
+

Результат нечеткого логического вывода:

+
+
+
+
Степень принадлежности:
+
+
+
+ Нет результата +
+ +
+ 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('Удалить запись?')"> Удалить - Отмена + Назад