From 3723881909b2f4e4ab876c1e0930b12101dbf0d9 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 19 Feb 2025 17:02:37 +0400 Subject: [PATCH] #5 -- Add variables and terms to db --- .../fc/config/SecurityConfiguration.java | 1 - .../project/controller/ProjectController.java | 7 ++- .../project/service/ProjectRulesService.java | 6 +- .../service/ProjectVariableService.java | 28 ++++++++++ .../controller/InferenceMvcController.java | 27 +++++---- .../fc/rule/controller/RuleController.java | 15 +++-- .../rule/controller/VariableController.java | 47 ++++++++++++++++ .../ru/ulstu/fc/rule/model/Antecedent.java | 27 --------- .../ru/ulstu/fc/rule/model/Consequent.java | 29 ---------- .../{RuleForm.java => FuzzyRuleForm.java} | 6 +- .../java/ru/ulstu/fc/rule/model/Term.java | 19 +++++++ .../java/ru/ulstu/fc/rule/model/Variable.java | 44 ++++++++++++--- .../ru/ulstu/fc/rule/model/VariableForm.java | 40 +++++++++++++ .../ru/ulstu/fc/rule/model/VariableValue.java | 33 ----------- ...pository.java => FuzzyRuleRepository.java} | 2 +- .../rule/repository/VariableRepository.java | 13 +++++ .../rule/service/FuzzyInferenceService.java | 40 ++++++------- .../fc/rule/service/FuzzyRuleService.java | 42 ++++++++++++++ .../ru/ulstu/fc/rule/service/RuleService.java | 43 -------------- .../fc/rule/service/VariableService.java | 42 ++++++++++++++ .../fc/user/controller/UserController.java | 1 - .../java/ru/ulstu/fc/user/model/User.java | 7 +-- .../fc/user/service/IpAddressResolver.java | 9 ++- .../user/service/UserSessionLoginHandler.java | 18 +++--- .../service/UserSessionLogoutHandler.java | 18 +++--- .../resources/templates/project/edit.html | 56 ++++++++++--------- src/main/resources/templates/var/edit.html | 38 +++++++++++++ 27 files changed, 413 insertions(+), 245 deletions(-) create mode 100644 src/main/java/ru/ulstu/fc/project/service/ProjectVariableService.java create mode 100644 src/main/java/ru/ulstu/fc/rule/controller/VariableController.java delete mode 100644 src/main/java/ru/ulstu/fc/rule/model/Antecedent.java delete mode 100644 src/main/java/ru/ulstu/fc/rule/model/Consequent.java rename src/main/java/ru/ulstu/fc/rule/model/{RuleForm.java => FuzzyRuleForm.java} (84%) create mode 100644 src/main/java/ru/ulstu/fc/rule/model/VariableForm.java delete mode 100644 src/main/java/ru/ulstu/fc/rule/model/VariableValue.java rename src/main/java/ru/ulstu/fc/rule/repository/{RuleRepository.java => FuzzyRuleRepository.java} (76%) create mode 100644 src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java create mode 100644 src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java delete mode 100644 src/main/java/ru/ulstu/fc/rule/service/RuleService.java create mode 100644 src/main/java/ru/ulstu/fc/rule/service/VariableService.java create mode 100644 src/main/resources/templates/var/edit.html diff --git a/src/main/java/ru/ulstu/fc/config/SecurityConfiguration.java b/src/main/java/ru/ulstu/fc/config/SecurityConfiguration.java index 18900be..be6b4ea 100644 --- a/src/main/java/ru/ulstu/fc/config/SecurityConfiguration.java +++ b/src/main/java/ru/ulstu/fc/config/SecurityConfiguration.java @@ -4,7 +4,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; diff --git a/src/main/java/ru/ulstu/fc/project/controller/ProjectController.java b/src/main/java/ru/ulstu/fc/project/controller/ProjectController.java index a835938..e248e4b 100644 --- a/src/main/java/ru/ulstu/fc/project/controller/ProjectController.java +++ b/src/main/java/ru/ulstu/fc/project/controller/ProjectController.java @@ -12,6 +12,7 @@ import ru.ulstu.fc.project.model.Project; import ru.ulstu.fc.project.model.ProjectForm; 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.user.model.UserRoleConstants; @Controller @@ -21,11 +22,14 @@ import ru.ulstu.fc.user.model.UserRoleConstants; public class ProjectController { private final ProjectService projectService; private final ProjectRulesService projectRulesService; + private final ProjectVariableService projectVariableService; public ProjectController(ProjectService projectService, - ProjectRulesService projectRulesService) { + ProjectRulesService projectRulesService, + ProjectVariableService projectVariableService) { this.projectService = projectService; this.projectRulesService = projectRulesService; + this.projectVariableService = projectVariableService; } @GetMapping("list") @@ -42,6 +46,7 @@ public class ProjectController { : new Project())); model.addAttribute("rules", projectRulesService.getByProjectId(id)); + model.addAttribute("variables", projectVariableService.getByProjectId(id)); return "project/edit"; } diff --git a/src/main/java/ru/ulstu/fc/project/service/ProjectRulesService.java b/src/main/java/ru/ulstu/fc/project/service/ProjectRulesService.java index 9b17e2c..88fe5d1 100644 --- a/src/main/java/ru/ulstu/fc/project/service/ProjectRulesService.java +++ b/src/main/java/ru/ulstu/fc/project/service/ProjectRulesService.java @@ -5,15 +5,15 @@ import java.util.List; import org.springframework.stereotype.Service; -import ru.ulstu.fc.rule.repository.RuleRepository; +import ru.ulstu.fc.rule.repository.FuzzyRuleRepository; import ru.ulstu.fc.rule.model.FuzzyRule; @Service public class ProjectRulesService { - private final RuleRepository ruleRepository; + private final FuzzyRuleRepository ruleRepository; private final ProjectService projectService; - public ProjectRulesService(RuleRepository ruleRepository, + public ProjectRulesService(FuzzyRuleRepository ruleRepository, ProjectService projectService) { this.ruleRepository = ruleRepository; this.projectService = projectService; diff --git a/src/main/java/ru/ulstu/fc/project/service/ProjectVariableService.java b/src/main/java/ru/ulstu/fc/project/service/ProjectVariableService.java new file mode 100644 index 0000000..e05031a --- /dev/null +++ b/src/main/java/ru/ulstu/fc/project/service/ProjectVariableService.java @@ -0,0 +1,28 @@ +package ru.ulstu.fc.project.service; + +import java.util.Collections; +import java.util.List; + +import org.springframework.stereotype.Service; + +import ru.ulstu.fc.rule.repository.VariableRepository; +import ru.ulstu.fc.rule.model.Variable; + +@Service +public class ProjectVariableService { + private final VariableRepository variableRepository; + private final ProjectService projectService; + + public ProjectVariableService(VariableRepository variableRepository, + ProjectService projectService) { + this.variableRepository = variableRepository; + this.projectService = projectService; + } + + public List getByProjectId(Integer projectId) { + if (projectId == null || projectId == 0) { + return Collections.emptyList(); + } + return variableRepository.findByProject(projectService.getById(projectId)); + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/controller/InferenceMvcController.java b/src/main/java/ru/ulstu/fc/rule/controller/InferenceMvcController.java index ebd0f13..5bc7614 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/InferenceMvcController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/InferenceMvcController.java @@ -7,10 +7,9 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import ru.ulstu.fc.rule.model.Antecedent; import ru.ulstu.fc.rule.model.InferenceForm; +import ru.ulstu.fc.rule.model.Term; import ru.ulstu.fc.rule.model.Variable; -import ru.ulstu.fc.rule.model.VariableValue; import ru.ulstu.fc.rule.service.FuzzyInferenceService; import java.util.Arrays; @@ -45,21 +44,21 @@ public class InferenceMvcController { return "index"; } - private List getAgeValues() { + private List getAgeValues() { Variable var = new Variable("Age"); - var.getValues().addAll(Arrays.asList( - new VariableValue("молодой", 30.0), - new VariableValue("средний", 45.0), - new VariableValue("старый", 60.0))); - return var.getValues(); + var.getTerms().addAll(Arrays.asList( + new Term("молодой", 30.0), + new Term("средний", 45.0), + new Term("старый", 60.0))); + return var.getTerms(); } - private List getIncomeValues() { + private List getIncomeValues() { Variable var = new Variable("Income"); - var.getValues().addAll(Arrays.asList( - new VariableValue("небольшой", 20000.0), - new VariableValue("средний", 90000.0), - new VariableValue("высокий", 200000.0))); - return var.getValues(); + var.getTerms().addAll(Arrays.asList( + new Term("небольшой", 20000.0), + new Term("средний", 90000.0), + new Term("высокий", 200000.0))); + return var.getTerms(); } } diff --git a/src/main/java/ru/ulstu/fc/rule/controller/RuleController.java b/src/main/java/ru/ulstu/fc/rule/controller/RuleController.java index 5a52826..093feeb 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/RuleController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/RuleController.java @@ -7,16 +7,15 @@ 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.rule.model.FuzzyRule; -import ru.ulstu.fc.rule.model.RuleForm; -import ru.ulstu.fc.rule.service.RuleService; +import ru.ulstu.fc.rule.model.VariableForm; +import ru.ulstu.fc.rule.service.VariableService; @Controller @RequestMapping("rule") public class RuleController { - private final RuleService ruleService; + private final VariableService ruleService; - public RuleController(RuleService ruleService) { + public RuleController(VariableService ruleService) { this.ruleService = ruleService; } @@ -25,7 +24,7 @@ public class RuleController { @PathVariable(value = "ruleId") Integer id, Model model) { model.addAttribute("projectId", projectId); model.addAttribute("rule", - new RuleForm(id, (id != null && id != 0) + new VariableForm(id, (id != null && id != 0) ? ruleService.getById(id).getProject().getId() : projectId)); @@ -33,13 +32,13 @@ public class RuleController { } @PostMapping(value = "save", params = "save") - public String save(RuleForm ruleForm, Model model) { + public String save(VariableForm ruleForm, Model model) { model.addAttribute("rule", ruleService.save(ruleForm)); return "redirect:/project/edit/" + ruleForm.getProjectId(); } @PostMapping(value = "save", params = "delete") - public String delete(RuleForm ruleForm) { + public String delete(VariableForm ruleForm) { if (ruleForm != null && ruleForm.getId() != null) { ruleService.delete(ruleForm); } diff --git a/src/main/java/ru/ulstu/fc/rule/controller/VariableController.java b/src/main/java/ru/ulstu/fc/rule/controller/VariableController.java new file mode 100644 index 0000000..ce79620 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/controller/VariableController.java @@ -0,0 +1,47 @@ +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.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +import ru.ulstu.fc.rule.model.VariableForm; +import ru.ulstu.fc.rule.service.VariableService; + +@Controller +@RequestMapping("var") +public class VariableController { + private final VariableService variableService; + + public VariableController(VariableService variableService) { + this.variableService = variableService; + } + + @GetMapping("/edit/{projectId}/{varId}") + public String edit(@PathVariable(value = "projectId") Integer projectId, + @PathVariable(value = "varId") Integer id, Model model) { + model.addAttribute("projectId", projectId); + model.addAttribute("var", + new VariableForm(id, (id != null && id != 0) + ? variableService.getById(id).getProject().getId() + : projectId)); + + return "var/edit"; + } + + @PostMapping(value = "save", params = "save") + public String save(VariableForm variableForm, Model model) { + model.addAttribute("rule", variableService.save(variableForm)); + return "redirect:/project/edit/" + variableForm.getProjectId(); + } + + @PostMapping(value = "save", params = "delete") + public String delete(VariableForm variableForm) { + if (variableForm != null && variableForm.getId() != null) { + variableService.delete(variableForm); + } + return "redirect:/project/edit/" + variableForm.getProjectId(); + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/Antecedent.java b/src/main/java/ru/ulstu/fc/rule/model/Antecedent.java deleted file mode 100644 index dbf82f6..0000000 --- a/src/main/java/ru/ulstu/fc/rule/model/Antecedent.java +++ /dev/null @@ -1,27 +0,0 @@ -package ru.ulstu.fc.rule.model; - -import jakarta.persistence.Entity; -import jakarta.persistence.ManyToOne; -import ru.ulstu.fc.core.model.BaseEntity; - - -public class Antecedent extends BaseEntity { - @ManyToOne - private Variable variable; - @ManyToOne - private Term term; - - public Variable getVariable() { - return variable; - } - public void setVariable(Variable variable) { - this.variable = variable; - } - public Term getTerm() { - return term; - } - public void setTerm(Term term) { - this.term = term; - } - -} diff --git a/src/main/java/ru/ulstu/fc/rule/model/Consequent.java b/src/main/java/ru/ulstu/fc/rule/model/Consequent.java deleted file mode 100644 index 36d894a..0000000 --- a/src/main/java/ru/ulstu/fc/rule/model/Consequent.java +++ /dev/null @@ -1,29 +0,0 @@ -package ru.ulstu.fc.rule.model; - -import jakarta.persistence.Entity; -import jakarta.persistence.ManyToOne; -import ru.ulstu.fc.core.model.BaseEntity; - - -public class Consequent extends BaseEntity { - @ManyToOne - private Variable variable; - private String value; - - public Variable getVariable() { - return variable; - } - - public void setVariable(Variable variable) { - this.variable = variable; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - -} diff --git a/src/main/java/ru/ulstu/fc/rule/model/RuleForm.java b/src/main/java/ru/ulstu/fc/rule/model/FuzzyRuleForm.java similarity index 84% rename from src/main/java/ru/ulstu/fc/rule/model/RuleForm.java rename to src/main/java/ru/ulstu/fc/rule/model/FuzzyRuleForm.java index eb5eddc..9599573 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/RuleForm.java +++ b/src/main/java/ru/ulstu/fc/rule/model/FuzzyRuleForm.java @@ -1,14 +1,14 @@ package ru.ulstu.fc.rule.model; -public class RuleForm { +public class FuzzyRuleForm { private Integer id; private Integer projectId; private String content; - public RuleForm() { + public FuzzyRuleForm() { } - public RuleForm(Integer id, Integer projectId) { + public FuzzyRuleForm(Integer id, Integer projectId) { this.id = id; this.projectId = projectId; } diff --git a/src/main/java/ru/ulstu/fc/rule/model/Term.java b/src/main/java/ru/ulstu/fc/rule/model/Term.java index 17ffeb3..edfe7b1 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/Term.java +++ b/src/main/java/ru/ulstu/fc/rule/model/Term.java @@ -3,8 +3,18 @@ package ru.ulstu.fc.rule.model; import jakarta.persistence.Entity; import ru.ulstu.fc.core.model.BaseEntity; +@Entity public class Term extends BaseEntity { private String description; + private Double value; + + public Term() { + } + + public Term(String description, Double value) { + this.description = description; + this.value = value; + } public String getDescription() { return description; @@ -13,4 +23,13 @@ public class Term extends BaseEntity { public void setDescription(String description) { this.description = description; } + + public Double getValue() { + return value; + } + + public void setValue(Double value) { + this.value = value; + } + } diff --git a/src/main/java/ru/ulstu/fc/rule/model/Variable.java b/src/main/java/ru/ulstu/fc/rule/model/Variable.java index a130457..7a7e2f5 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/Variable.java +++ b/src/main/java/ru/ulstu/fc/rule/model/Variable.java @@ -6,29 +6,57 @@ import java.util.List; import jakarta.persistence.Entity; import jakarta.persistence.OneToMany; import ru.ulstu.fc.core.model.BaseEntity; +import ru.ulstu.fc.project.model.Project; +@Entity public class Variable extends BaseEntity { private String name; + private Project project; + private boolean isInput; @OneToMany - private List values = new ArrayList<>(); + private List terms = new ArrayList<>(); public Variable() { } - public Variable(String name, List values) { - this.name = name; - this.values = values; - } - public Variable(String name) { this.name = name; } + public Variable(String name, List terms) { + this.name = name; + this.terms = terms; + } + public String getName() { return name; } - public List getValues() { - return values; + public void setName(String name) { + this.name = name; + } + + public List getTerms() { + return terms; + } + + public void setTerms(List terms) { + this.terms = terms; + } + + public Project getProject() { + return project; + } + + public void setProject(Project project) { + this.project = project; + } + + public boolean isInput() { + return isInput; + } + + public void setInput(boolean isInput) { + this.isInput = isInput; } } diff --git a/src/main/java/ru/ulstu/fc/rule/model/VariableForm.java b/src/main/java/ru/ulstu/fc/rule/model/VariableForm.java new file mode 100644 index 0000000..803290b --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/model/VariableForm.java @@ -0,0 +1,40 @@ +package ru.ulstu.fc.rule.model; + +public class VariableForm { + private Integer id; + private Integer projectId; + private String name; + + public VariableForm() { + } + + public VariableForm(Integer id, Integer projectId) { + this.id = id; + this.projectId = projectId; + } + + public Integer getProjectId() { + return projectId; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public void setProjectId(Integer projectId) { + this.projectId = projectId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/VariableValue.java b/src/main/java/ru/ulstu/fc/rule/model/VariableValue.java deleted file mode 100644 index 097a577..0000000 --- a/src/main/java/ru/ulstu/fc/rule/model/VariableValue.java +++ /dev/null @@ -1,33 +0,0 @@ -package ru.ulstu.fc.rule.model; - -import jakarta.persistence.Entity; -import ru.ulstu.fc.core.model.BaseEntity; - -public class VariableValue extends BaseEntity { - private String fuzzyTerm; - private Double value; - - public VariableValue() { - } - - public VariableValue(String fuzzyTerm, Double value) { - this.fuzzyTerm = fuzzyTerm; - this.value = value; - } - - public String getFuzzyTerm() { - return fuzzyTerm; - } - - public void setFuzzyTerm(String fuzzyTerm) { - this.fuzzyTerm = fuzzyTerm; - } - - public Double getValue() { - return value; - } - - public void setValue(Double value) { - this.value = value; - } -} diff --git a/src/main/java/ru/ulstu/fc/rule/repository/RuleRepository.java b/src/main/java/ru/ulstu/fc/rule/repository/FuzzyRuleRepository.java similarity index 76% rename from src/main/java/ru/ulstu/fc/rule/repository/RuleRepository.java rename to src/main/java/ru/ulstu/fc/rule/repository/FuzzyRuleRepository.java index a41bea7..ed36909 100644 --- a/src/main/java/ru/ulstu/fc/rule/repository/RuleRepository.java +++ b/src/main/java/ru/ulstu/fc/rule/repository/FuzzyRuleRepository.java @@ -7,7 +7,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import ru.ulstu.fc.project.model.Project; import ru.ulstu.fc.rule.model.FuzzyRule; -public interface RuleRepository extends JpaRepository { +public interface FuzzyRuleRepository extends JpaRepository { List findByProject(Project project); } diff --git a/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java b/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java new file mode 100644 index 0000000..f0d4cdc --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java @@ -0,0 +1,13 @@ +package ru.ulstu.fc.rule.repository; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; + +import ru.ulstu.fc.project.model.Project; +import ru.ulstu.fc.rule.model.Variable; + +public interface VariableRepository extends JpaRepository { + + List findByProject(Project project); +} 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 f11e02a..5287623 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java @@ -15,8 +15,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import ru.ulstu.fc.rule.model.OutputValue; +import ru.ulstu.fc.rule.model.Term; import ru.ulstu.fc.rule.model.Variable; -import ru.ulstu.fc.rule.model.VariableValue; import java.util.List; import java.util.Map; @@ -45,15 +45,15 @@ public class FuzzyInferenceService { final InputVariable input = new InputVariable(); input.setName(variable.getName()); input.setDescription(""); - input.setRange(0, variable.getValues().get(variable.getValues().size() - 1).getValue()); + input.setRange(0, variable.getTerms().get(variable.getTerms().size() - 1).getValue()); input.setEnabled(true); input.setLockValueInRange(false); double prev = 0; - for (int i = 0; i < variable.getValues().size(); i++) { - Triangle term = new Triangle(variable.getValues().get(i).getFuzzyTerm(), + for (int i = 0; i < variable.getTerms().size(); i++) { + Triangle term = new Triangle(variable.getTerms().get(i).getDescription(), prev, - variable.getValues().get(i).getValue(), - variable.getValues().get(i).getValue() + variable.getValues().get(i).getValue() - prev); + variable.getTerms().get(i).getValue(), + variable.getTerms().get(i).getValue() + variable.getTerms().get(i).getValue() - prev); prev = term.getVertexB(); input.addTerm(term); } @@ -64,19 +64,19 @@ public class FuzzyInferenceService { final OutputVariable output = new OutputVariable(); output.setName(variable.getName()); output.setDescription(""); - output.setRange(0, variable.getValues().get(variable.getValues().size() - 1).getValue()); + output.setRange(0, variable.getTerms().get(variable.getTerms().size() - 1).getValue()); output.setEnabled(true); output.setAggregation(new Maximum()); output.setDefuzzifier(new WeightedAverage()); output.setDefaultValue(Double.NaN); output.setLockValueInRange(false); double prev = 0; - for (int i = 0; i < variable.getValues().size(); i++) { + for (int i = 0; i < variable.getTerms().size(); i++) { Triangle term = new Triangle( - variable.getValues().get(i).getFuzzyTerm(), + variable.getTerms().get(i).getDescription(), prev, - variable.getValues().get(i).getValue(), - variable.getValues().get(i).getValue() + variable.getValues().get(i).getValue() - prev); + variable.getTerms().get(i).getValue(), + variable.getTerms().get(i).getValue() + variable.getTerms().get(i).getValue() - prev); prev = term.getVertexB(); output.addTerm(term); } @@ -131,20 +131,20 @@ public class FuzzyInferenceService { public List getFuzzyInference(Map vals) { return getFuzzyInference(getDemoRules(), vals, List.of(new Variable("возраст", List.of( - new VariableValue("молодой", 35.0), - new VariableValue("средний", 60.0), - new VariableValue("старый", 100.0)) + new Term("молодой", 35.0), + new Term("средний", 60.0), + new Term("старый", 100.0)) ), new Variable("доход", List.of( - new VariableValue("небольшой", 35000.0), - new VariableValue("средний", 100000.0), - new VariableValue("высокий", 500000.0)) + new Term("небольшой", 35000.0), + new Term("средний", 100000.0), + new Term("высокий", 500000.0)) ) ), new Variable("кредит", List.of( - new VariableValue("небольшой", 20000.0), - new VariableValue("средний", 100000.0), - new VariableValue("большой", 1000000.0))) + new Term("небольшой", 20000.0), + new Term("средний", 100000.0), + new Term("большой", 1000000.0))) ); } diff --git a/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java new file mode 100644 index 0000000..6f2d1c6 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java @@ -0,0 +1,42 @@ +package ru.ulstu.fc.rule.service; + +import org.springframework.stereotype.Service; + +import ru.ulstu.fc.rule.repository.FuzzyRuleRepository; +import ru.ulstu.fc.project.service.ProjectService; +import ru.ulstu.fc.rule.model.FuzzyRule; +import ru.ulstu.fc.rule.model.FuzzyRuleForm; + +@Service +public class FuzzyRuleService { + private final FuzzyRuleRepository ruleRepository; + private final ProjectService projectService; + + public FuzzyRuleService(FuzzyRuleRepository ruleRepository, ProjectService projectService) { + this.ruleRepository = ruleRepository; + this.projectService = projectService; + } + + public FuzzyRule getById(Integer id) { + return ruleRepository + .findById(id) + .orElseThrow(() -> new RuntimeException("Rule not found by id")); + } + + public Object save(FuzzyRuleForm ruleForm) { + FuzzyRule rule; + if (ruleForm.getId() == null || ruleForm.getId() == 0) { + rule = new FuzzyRule(); + } else { + rule = getById(ruleForm.getId()); + } + rule.setProject(projectService.getById(ruleForm.getProjectId())); + rule.setContent(ruleForm.getContent()); + return ruleRepository.save(rule); + } + + public void delete(FuzzyRuleForm ruleForm) { + getById(ruleForm.getId()); + ruleRepository.deleteById(ruleForm.getId()); + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/service/RuleService.java b/src/main/java/ru/ulstu/fc/rule/service/RuleService.java deleted file mode 100644 index 95fc3e1..0000000 --- a/src/main/java/ru/ulstu/fc/rule/service/RuleService.java +++ /dev/null @@ -1,43 +0,0 @@ -package ru.ulstu.fc.rule.service; - -import org.springframework.stereotype.Service; - -import ru.ulstu.fc.rule.repository.RuleRepository; -import ru.ulstu.fc.project.service.ProjectService; -import ru.ulstu.fc.rule.model.FuzzyRule; -import ru.ulstu.fc.rule.model.RuleForm; - -@Service -public class RuleService { - private final RuleRepository ruleRepository; - private final ProjectService projectService; - - public RuleService(RuleRepository ruleRepository, ProjectService projectService) { - this.ruleRepository = ruleRepository; - this.projectService = projectService; - } - - public FuzzyRule getById(Integer id) { - return ruleRepository - .findById(id) - .orElseThrow(() -> new RuntimeException("Rule not found by id")); - } - - public Object save(RuleForm ruleForm) { - if (ruleForm.getId() == null || ruleForm.getId() == 0) { - FuzzyRule rule = new FuzzyRule(); - rule.setProject(projectService.getById(ruleForm.getProjectId())); - rule.setContent(ruleForm.getContent()); - return ruleRepository.save(rule); - } - FuzzyRule dbRule = getById(ruleForm.getId()); - dbRule.setProject(projectService.getById(ruleForm.getProjectId())); - dbRule.setContent(ruleForm.getContent()); - return ruleRepository.save(dbRule); - } - - public void delete(RuleForm ruleForm) { - getById(ruleForm.getId()); - ruleRepository.deleteById(ruleForm.getId()); - } -} diff --git a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java new file mode 100644 index 0000000..22d1c4d --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java @@ -0,0 +1,42 @@ +package ru.ulstu.fc.rule.service; + +import org.springframework.stereotype.Service; + +import ru.ulstu.fc.rule.repository.VariableRepository; +import ru.ulstu.fc.project.service.ProjectService; +import ru.ulstu.fc.rule.model.Variable; +import ru.ulstu.fc.rule.model.VariableForm; + +@Service +public class VariableService { + private final VariableRepository variableRepository; + private final ProjectService projectService; + + public VariableService(VariableRepository variableRepository, ProjectService projectService) { + this.variableRepository = variableRepository; + this.projectService = projectService; + } + + public Variable getById(Integer id) { + return variableRepository + .findById(id) + .orElseThrow(() -> new RuntimeException("Variable not found by id")); + } + + public Object save(VariableForm variableForm) { + Variable variable; + if (variableForm.getId() == null || variableForm.getId() == 0) { + variable = new Variable(); + } else { + variable = getById(variableForm.getId()); + } + variable.setProject(projectService.getById(variableForm.getProjectId())); + variable.setName(variableForm.getName()); + return variableRepository.save(variable); + } + + public void delete(VariableForm ruleForm) { + getById(ruleForm.getId()); + variableRepository.deleteById(ruleForm.getId()); + } +} diff --git a/src/main/java/ru/ulstu/fc/user/controller/UserController.java b/src/main/java/ru/ulstu/fc/user/controller/UserController.java index 97fe0d6..c238db5 100644 --- a/src/main/java/ru/ulstu/fc/user/controller/UserController.java +++ b/src/main/java/ru/ulstu/fc/user/controller/UserController.java @@ -7,7 +7,6 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.context.request.WebRequest; -import org.springframework.web.servlet.ModelAndView; import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; diff --git a/src/main/java/ru/ulstu/fc/user/model/User.java b/src/main/java/ru/ulstu/fc/user/model/User.java index 1c81271..352808e 100644 --- a/src/main/java/ru/ulstu/fc/user/model/User.java +++ b/src/main/java/ru/ulstu/fc/user/model/User.java @@ -1,21 +1,20 @@ package ru.ulstu.fc.user.model; +import java.util.HashSet; +import java.util.Set; + import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinTable; import jakarta.persistence.ManyToMany; import jakarta.persistence.Table; -import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Pattern; import jakarta.validation.constraints.Size; import ru.ulstu.fc.config.Constants; import ru.ulstu.fc.core.model.BaseEntity; -import java.util.HashSet; -import java.util.Set; - @Entity @Table(name = "is_users") public class User extends BaseEntity { diff --git a/src/main/java/ru/ulstu/fc/user/service/IpAddressResolver.java b/src/main/java/ru/ulstu/fc/user/service/IpAddressResolver.java index 51a0f36..ae95a27 100644 --- a/src/main/java/ru/ulstu/fc/user/service/IpAddressResolver.java +++ b/src/main/java/ru/ulstu/fc/user/service/IpAddressResolver.java @@ -1,19 +1,18 @@ package ru.ulstu.fc.user.service; import jakarta.servlet.http.HttpServletRequest; -import org.springframework.util.StringUtils; public final class IpAddressResolver { - private static final String CLIENT_IP_HEADER = "Client-IP"; - private static final String FORWARDED_FOR_HEADER = "X-Forwarded-For"; + //private static final String CLIENT_IP_HEADER = "Client-IP"; + //private static final String FORWARDED_FOR_HEADER = "X-Forwarded-For"; public static String getRemoteAddr(HttpServletRequest request) { String headerClientIp = request.getHeader(""); String headerXForwardedFor = request.getHeader(HttpServletRequest.FORM_AUTH); - if (StringUtils.isEmpty(request.getRemoteAddr()) && !StringUtils.isEmpty(headerClientIp)) { + if (request.getRemoteAddr().isEmpty() && !headerClientIp.isEmpty()) { return headerClientIp; } - if (!StringUtils.isEmpty(headerXForwardedFor)) { + if (!headerXForwardedFor.isEmpty()) { return headerXForwardedFor; } return request.getRemoteAddr(); diff --git a/src/main/java/ru/ulstu/fc/user/service/UserSessionLoginHandler.java b/src/main/java/ru/ulstu/fc/user/service/UserSessionLoginHandler.java index b05710d..44d7904 100644 --- a/src/main/java/ru/ulstu/fc/user/service/UserSessionLoginHandler.java +++ b/src/main/java/ru/ulstu/fc/user/service/UserSessionLoginHandler.java @@ -1,21 +1,21 @@ package ru.ulstu.fc.user.service; +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; +import org.springframework.stereotype.Component; + import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.security.core.Authentication; -import org.springframework.security.web.authentication.AuthenticationSuccessHandler; -import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; -import org.springframework.stereotype.Component; import ru.ulstu.fc.config.Constants; -import java.io.IOException; - @Component -public class UserSessionLoginHandler extends SavedRequestAwareAuthenticationSuccessHandler implements AuthenticationSuccessHandler { +public class UserSessionLoginHandler extends SavedRequestAwareAuthenticationSuccessHandler { private final Logger log = LoggerFactory.getLogger(UserSessionLoginHandler.class); private final UserSessionService userSessionService; diff --git a/src/main/java/ru/ulstu/fc/user/service/UserSessionLogoutHandler.java b/src/main/java/ru/ulstu/fc/user/service/UserSessionLogoutHandler.java index 98d50fe..9703e9e 100644 --- a/src/main/java/ru/ulstu/fc/user/service/UserSessionLogoutHandler.java +++ b/src/main/java/ru/ulstu/fc/user/service/UserSessionLogoutHandler.java @@ -1,21 +1,21 @@ package ru.ulstu.fc.user.service; +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler; +import org.springframework.stereotype.Component; + import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.security.core.Authentication; -import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; -import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler; -import org.springframework.stereotype.Component; import ru.ulstu.fc.config.Constants; -import java.io.IOException; - @Component -public class UserSessionLogoutHandler extends SimpleUrlLogoutSuccessHandler implements LogoutSuccessHandler { +public class UserSessionLogoutHandler extends SimpleUrlLogoutSuccessHandler { private final Logger log = LoggerFactory.getLogger(UserSessionLogoutHandler.class); private final UserSessionService userSessionService; diff --git a/src/main/resources/templates/project/edit.html b/src/main/resources/templates/project/edit.html index ac4409f..79ec99b 100644 --- a/src/main/resources/templates/project/edit.html +++ b/src/main/resources/templates/project/edit.html @@ -1,10 +1,10 @@ - + + Редактирование проекта - +

Редактирование проекта:

@@ -12,14 +12,8 @@
- -

+ +

Не может быть пустым

@@ -27,22 +21,30 @@
- Отмена -
-

Список правил

-
-
- +
+
+
+

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

+
+
+ +
+
+ Добавить преременную
- -
- Добавить правило -
- +
+ Добавить правило + + + + \ No newline at end of file diff --git a/src/main/resources/templates/var/edit.html b/src/main/resources/templates/var/edit.html new file mode 100644 index 0000000..44d956a --- /dev/null +++ b/src/main/resources/templates/var/edit.html @@ -0,0 +1,38 @@ + + + + Редактирование переменной + + +
+

Редактирование переменной:

+
+ + +
+ + +

+ Не может быть пустым +

+
+ + + + Отмена +
+
+