#5 -- Fix save variables
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m11s

This commit is contained in:
Anton Romanov 2025-02-20 13:52:14 +04:00
parent fb01034fc8
commit 972284fad3
7 changed files with 42 additions and 35 deletions

View File

@ -1,19 +1,20 @@
package ru.ulstu.fc.project.model; package ru.ulstu.fc.project.model;
import java.util.Date;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotEmpty;
import ru.ulstu.fc.core.model.BaseEntity; import ru.ulstu.fc.core.model.BaseEntity;
import ru.ulstu.fc.user.model.User; import ru.ulstu.fc.user.model.User;
import java.util.Date;
@Entity @Entity
public class Project extends BaseEntity { public class Project extends BaseEntity {
@NotEmpty(message = "Текст новости не может быть пустым") @NotEmpty(message = "Текст новости не может быть пустым")
private String name; private String name;
private Date createDate = new Date(); private Date createDate = new Date();
@ManyToOne @ManyToOne(cascade = CascadeType.MERGE)
private User user; private User user;
public Project() { public Project() {

View File

@ -46,19 +46,19 @@ public class InferenceMvcController {
private List<FuzzyTerm> getAgeValues() { private List<FuzzyTerm> getAgeValues() {
Variable var = new Variable("Age"); Variable var = new Variable("Age");
var.getTerms().addAll(Arrays.asList( var.getFuzzyTerms().addAll(Arrays.asList(
new FuzzyTerm("молодой", 30.0), new FuzzyTerm("молодой", 30.0),
new FuzzyTerm("средний", 45.0), new FuzzyTerm("средний", 45.0),
new FuzzyTerm("старый", 60.0))); new FuzzyTerm("старый", 60.0)));
return var.getTerms(); return var.getFuzzyTerms();
} }
private List<FuzzyTerm> getIncomeValues() { private List<FuzzyTerm> getIncomeValues() {
Variable var = new Variable("Income"); Variable var = new Variable("Income");
var.getTerms().addAll(Arrays.asList( var.getFuzzyTerms().addAll(Arrays.asList(
new FuzzyTerm("небольшой", 20000.0), new FuzzyTerm("небольшой", 20000.0),
new FuzzyTerm("средний", 90000.0), new FuzzyTerm("средний", 90000.0),
new FuzzyTerm("высокий", 200000.0))); new FuzzyTerm("высокий", 200000.0)));
return var.getTerms(); return var.getFuzzyTerms();
} }
} }

View File

@ -7,15 +7,15 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import ru.ulstu.fc.rule.model.VariableForm; import ru.ulstu.fc.rule.model.FuzzyRuleForm;
import ru.ulstu.fc.rule.service.VariableService; import ru.ulstu.fc.rule.service.FuzzyRuleService;
@Controller @Controller
@RequestMapping("rule") @RequestMapping("rule")
public class RuleController { public class RuleController {
private final VariableService ruleService; private final FuzzyRuleService ruleService;
public RuleController(VariableService ruleService) { public RuleController(FuzzyRuleService ruleService) {
this.ruleService = ruleService; this.ruleService = ruleService;
} }
@ -24,7 +24,7 @@ public class RuleController {
@PathVariable(value = "ruleId") Integer id, Model model) { @PathVariable(value = "ruleId") Integer id, Model model) {
model.addAttribute("projectId", projectId); model.addAttribute("projectId", projectId);
model.addAttribute("rule", model.addAttribute("rule",
new VariableForm(id, (id != null && id != 0) new FuzzyRuleForm(id, (id != null && id != 0)
? ruleService.getById(id).getProject().getId() ? ruleService.getById(id).getProject().getId()
: projectId)); : projectId));
@ -32,13 +32,13 @@ public class RuleController {
} }
@PostMapping(value = "save", params = "save") @PostMapping(value = "save", params = "save")
public String save(VariableForm ruleForm, Model model) { public String save(FuzzyRuleForm ruleForm, Model model) {
model.addAttribute("rule", ruleService.save(ruleForm)); model.addAttribute("rule", ruleService.save(ruleForm));
return "redirect:/project/edit/" + ruleForm.getProjectId(); return "redirect:/project/edit/" + ruleForm.getProjectId();
} }
@PostMapping(value = "save", params = "delete") @PostMapping(value = "save", params = "delete")
public String delete(VariableForm ruleForm) { public String delete(FuzzyRuleForm ruleForm) {
if (ruleForm != null && ruleForm.getId() != null) { if (ruleForm != null && ruleForm.getId() != null) {
ruleService.delete(ruleForm); ruleService.delete(ruleForm);
} }

View File

@ -3,7 +3,11 @@ package ru.ulstu.fc.rule.model;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany; import jakarta.persistence.OneToMany;
import ru.ulstu.fc.core.model.BaseEntity; import ru.ulstu.fc.core.model.BaseEntity;
import ru.ulstu.fc.project.model.Project; import ru.ulstu.fc.project.model.Project;
@ -11,10 +15,12 @@ import ru.ulstu.fc.project.model.Project;
@Entity @Entity
public class Variable extends BaseEntity { public class Variable extends BaseEntity {
private String name; private String name;
@ManyToOne
private Project project; private Project project;
private boolean isInput; private boolean isInput = true;
@OneToMany @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<FuzzyTerm> terms = new ArrayList<>(); @JoinColumn(name = "variable_id", unique = true)
private List<FuzzyTerm> fuzzyTerms = new ArrayList<>();
public Variable() { public Variable() {
} }
@ -23,9 +29,9 @@ public class Variable extends BaseEntity {
this.name = name; this.name = name;
} }
public Variable(String name, List<FuzzyTerm> terms) { public Variable(String name, List<FuzzyTerm> fuzzyTerms) {
this.name = name; this.name = name;
this.terms = terms; this.fuzzyTerms = fuzzyTerms;
} }
public String getName() { public String getName() {
@ -36,12 +42,12 @@ public class Variable extends BaseEntity {
this.name = name; this.name = name;
} }
public List<FuzzyTerm> getTerms() { public List<FuzzyTerm> getFuzzyTerms() {
return terms; return fuzzyTerms;
} }
public void setTerms(List<FuzzyTerm> terms) { public void setFuzzyTerms(List<FuzzyTerm> fuzzyTerms) {
this.terms = terms; this.fuzzyTerms = fuzzyTerms;
} }
public Project getProject() { public Project getProject() {

View File

@ -45,15 +45,15 @@ public class FuzzyInferenceService {
final InputVariable input = new InputVariable(); final InputVariable input = new InputVariable();
input.setName(variable.getName()); input.setName(variable.getName());
input.setDescription(""); input.setDescription("");
input.setRange(0, variable.getTerms().get(variable.getTerms().size() - 1).getValue()); input.setRange(0, variable.getFuzzyTerms().get(variable.getFuzzyTerms().size() - 1).getValue());
input.setEnabled(true); input.setEnabled(true);
input.setLockValueInRange(false); input.setLockValueInRange(false);
double prev = 0; double prev = 0;
for (int i = 0; i < variable.getTerms().size(); i++) { for (int i = 0; i < variable.getFuzzyTerms().size(); i++) {
Triangle term = new Triangle(variable.getTerms().get(i).getDescription(), Triangle term = new Triangle(variable.getFuzzyTerms().get(i).getDescription(),
prev, prev,
variable.getTerms().get(i).getValue(), variable.getFuzzyTerms().get(i).getValue(),
variable.getTerms().get(i).getValue() + variable.getTerms().get(i).getValue() - prev); variable.getFuzzyTerms().get(i).getValue() + variable.getFuzzyTerms().get(i).getValue() - prev);
prev = term.getVertexB(); prev = term.getVertexB();
input.addTerm(term); input.addTerm(term);
} }
@ -64,19 +64,19 @@ public class FuzzyInferenceService {
final OutputVariable output = new OutputVariable(); final OutputVariable output = new OutputVariable();
output.setName(variable.getName()); output.setName(variable.getName());
output.setDescription(""); output.setDescription("");
output.setRange(0, variable.getTerms().get(variable.getTerms().size() - 1).getValue()); output.setRange(0, variable.getFuzzyTerms().get(variable.getFuzzyTerms().size() - 1).getValue());
output.setEnabled(true); output.setEnabled(true);
output.setAggregation(new Maximum()); output.setAggregation(new Maximum());
output.setDefuzzifier(new WeightedAverage()); output.setDefuzzifier(new WeightedAverage());
output.setDefaultValue(Double.NaN); output.setDefaultValue(Double.NaN);
output.setLockValueInRange(false); output.setLockValueInRange(false);
double prev = 0; double prev = 0;
for (int i = 0; i < variable.getTerms().size(); i++) { for (int i = 0; i < variable.getFuzzyTerms().size(); i++) {
Triangle term = new Triangle( Triangle term = new Triangle(
variable.getTerms().get(i).getDescription(), variable.getFuzzyTerms().get(i).getDescription(),
prev, prev,
variable.getTerms().get(i).getValue(), variable.getFuzzyTerms().get(i).getValue(),
variable.getTerms().get(i).getValue() + variable.getTerms().get(i).getValue() - prev); variable.getFuzzyTerms().get(i).getValue() + variable.getFuzzyTerms().get(i).getValue() - prev);
prev = term.getVertexB(); prev = term.getVertexB();
output.addTerm(term); output.addTerm(term);
} }

View File

@ -23,7 +23,7 @@ public class VariableService {
.orElseThrow(() -> new RuntimeException("Variable not found by id")); .orElseThrow(() -> new RuntimeException("Variable not found by id"));
} }
public Object save(VariableForm variableForm) { public Variable save(VariableForm variableForm) {
Variable variable; Variable variable;
if (variableForm.getId() == null || variableForm.getId() == 0) { if (variableForm.getId() == null || variableForm.getId() == 0) {
variable = new Variable(); variable = new Variable();

View File

@ -8,7 +8,7 @@
</head> </head>
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<h3>Редактирование переменной:</h3> <h3>Редактирование переменной:</h3>
<form th:action="@{/rule/save}" th:object="${var}" method="post"> <form th:action="@{/var/save}" th:object="${var}" method="post">
<input type="hidden" th:field="*{projectId}"> <input type="hidden" th:field="*{projectId}">
<input type="hidden" th:field="*{id}"> <input type="hidden" th:field="*{id}">
<div class="form-group"> <div class="form-group">