#5 -- Fix save variables
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m11s
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m11s
This commit is contained in:
parent
fb01034fc8
commit
972284fad3
@ -1,19 +1,20 @@
|
||||
package ru.ulstu.fc.project.model;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import ru.ulstu.fc.core.model.BaseEntity;
|
||||
import ru.ulstu.fc.user.model.User;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Entity
|
||||
public class Project extends BaseEntity {
|
||||
@NotEmpty(message = "Текст новости не может быть пустым")
|
||||
private String name;
|
||||
private Date createDate = new Date();
|
||||
@ManyToOne
|
||||
@ManyToOne(cascade = CascadeType.MERGE)
|
||||
private User user;
|
||||
|
||||
public Project() {
|
||||
|
@ -46,19 +46,19 @@ public class InferenceMvcController {
|
||||
|
||||
private List<FuzzyTerm> getAgeValues() {
|
||||
Variable var = new Variable("Age");
|
||||
var.getTerms().addAll(Arrays.asList(
|
||||
var.getFuzzyTerms().addAll(Arrays.asList(
|
||||
new FuzzyTerm("молодой", 30.0),
|
||||
new FuzzyTerm("средний", 45.0),
|
||||
new FuzzyTerm("старый", 60.0)));
|
||||
return var.getTerms();
|
||||
return var.getFuzzyTerms();
|
||||
}
|
||||
|
||||
private List<FuzzyTerm> getIncomeValues() {
|
||||
Variable var = new Variable("Income");
|
||||
var.getTerms().addAll(Arrays.asList(
|
||||
var.getFuzzyTerms().addAll(Arrays.asList(
|
||||
new FuzzyTerm("небольшой", 20000.0),
|
||||
new FuzzyTerm("средний", 90000.0),
|
||||
new FuzzyTerm("высокий", 200000.0)));
|
||||
return var.getTerms();
|
||||
return var.getFuzzyTerms();
|
||||
}
|
||||
}
|
||||
|
@ -7,15 +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.VariableForm;
|
||||
import ru.ulstu.fc.rule.service.VariableService;
|
||||
import ru.ulstu.fc.rule.model.FuzzyRuleForm;
|
||||
import ru.ulstu.fc.rule.service.FuzzyRuleService;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("rule")
|
||||
public class RuleController {
|
||||
private final VariableService ruleService;
|
||||
private final FuzzyRuleService ruleService;
|
||||
|
||||
public RuleController(VariableService ruleService) {
|
||||
public RuleController(FuzzyRuleService ruleService) {
|
||||
this.ruleService = ruleService;
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ public class RuleController {
|
||||
@PathVariable(value = "ruleId") Integer id, Model model) {
|
||||
model.addAttribute("projectId", projectId);
|
||||
model.addAttribute("rule",
|
||||
new VariableForm(id, (id != null && id != 0)
|
||||
new FuzzyRuleForm(id, (id != null && id != 0)
|
||||
? ruleService.getById(id).getProject().getId()
|
||||
: projectId));
|
||||
|
||||
@ -32,13 +32,13 @@ public class RuleController {
|
||||
}
|
||||
|
||||
@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));
|
||||
return "redirect:/project/edit/" + ruleForm.getProjectId();
|
||||
}
|
||||
|
||||
@PostMapping(value = "save", params = "delete")
|
||||
public String delete(VariableForm ruleForm) {
|
||||
public String delete(FuzzyRuleForm ruleForm) {
|
||||
if (ruleForm != null && ruleForm.getId() != null) {
|
||||
ruleService.delete(ruleForm);
|
||||
}
|
||||
|
@ -3,7 +3,11 @@ package ru.ulstu.fc.rule.model;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import ru.ulstu.fc.core.model.BaseEntity;
|
||||
import ru.ulstu.fc.project.model.Project;
|
||||
@ -11,10 +15,12 @@ import ru.ulstu.fc.project.model.Project;
|
||||
@Entity
|
||||
public class Variable extends BaseEntity {
|
||||
private String name;
|
||||
@ManyToOne
|
||||
private Project project;
|
||||
private boolean isInput;
|
||||
@OneToMany
|
||||
private List<FuzzyTerm> terms = new ArrayList<>();
|
||||
private boolean isInput = true;
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "variable_id", unique = true)
|
||||
private List<FuzzyTerm> fuzzyTerms = new ArrayList<>();
|
||||
|
||||
public Variable() {
|
||||
}
|
||||
@ -23,9 +29,9 @@ public class Variable extends BaseEntity {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Variable(String name, List<FuzzyTerm> terms) {
|
||||
public Variable(String name, List<FuzzyTerm> fuzzyTerms) {
|
||||
this.name = name;
|
||||
this.terms = terms;
|
||||
this.fuzzyTerms = fuzzyTerms;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@ -36,12 +42,12 @@ public class Variable extends BaseEntity {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<FuzzyTerm> getTerms() {
|
||||
return terms;
|
||||
public List<FuzzyTerm> getFuzzyTerms() {
|
||||
return fuzzyTerms;
|
||||
}
|
||||
|
||||
public void setTerms(List<FuzzyTerm> terms) {
|
||||
this.terms = terms;
|
||||
public void setFuzzyTerms(List<FuzzyTerm> fuzzyTerms) {
|
||||
this.fuzzyTerms = fuzzyTerms;
|
||||
}
|
||||
|
||||
public Project getProject() {
|
||||
|
@ -45,15 +45,15 @@ public class FuzzyInferenceService {
|
||||
final InputVariable input = new InputVariable();
|
||||
input.setName(variable.getName());
|
||||
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.setLockValueInRange(false);
|
||||
double prev = 0;
|
||||
for (int i = 0; i < variable.getTerms().size(); i++) {
|
||||
Triangle term = new Triangle(variable.getTerms().get(i).getDescription(),
|
||||
for (int i = 0; i < variable.getFuzzyTerms().size(); i++) {
|
||||
Triangle term = new Triangle(variable.getFuzzyTerms().get(i).getDescription(),
|
||||
prev,
|
||||
variable.getTerms().get(i).getValue(),
|
||||
variable.getTerms().get(i).getValue() + variable.getTerms().get(i).getValue() - prev);
|
||||
variable.getFuzzyTerms().get(i).getValue(),
|
||||
variable.getFuzzyTerms().get(i).getValue() + variable.getFuzzyTerms().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.getTerms().get(variable.getTerms().size() - 1).getValue());
|
||||
output.setRange(0, variable.getFuzzyTerms().get(variable.getFuzzyTerms().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.getTerms().size(); i++) {
|
||||
for (int i = 0; i < variable.getFuzzyTerms().size(); i++) {
|
||||
Triangle term = new Triangle(
|
||||
variable.getTerms().get(i).getDescription(),
|
||||
variable.getFuzzyTerms().get(i).getDescription(),
|
||||
prev,
|
||||
variable.getTerms().get(i).getValue(),
|
||||
variable.getTerms().get(i).getValue() + variable.getTerms().get(i).getValue() - prev);
|
||||
variable.getFuzzyTerms().get(i).getValue(),
|
||||
variable.getFuzzyTerms().get(i).getValue() + variable.getFuzzyTerms().get(i).getValue() - prev);
|
||||
prev = term.getVertexB();
|
||||
output.addTerm(term);
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ public class VariableService {
|
||||
.orElseThrow(() -> new RuntimeException("Variable not found by id"));
|
||||
}
|
||||
|
||||
public Object save(VariableForm variableForm) {
|
||||
public Variable save(VariableForm variableForm) {
|
||||
Variable variable;
|
||||
if (variableForm.getId() == null || variableForm.getId() == 0) {
|
||||
variable = new Variable();
|
||||
|
@ -8,7 +8,7 @@
|
||||
</head>
|
||||
<div class="container" layout:fragment="content">
|
||||
<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="*{id}">
|
||||
<div class="form-group">
|
||||
|
Loading…
x
Reference in New Issue
Block a user