#5 -- Rule model as a string
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m3s

This commit is contained in:
Anton Romanov 2025-02-18 17:54:30 +04:00
parent 0d7f970629
commit 486981c0ff
15 changed files with 114 additions and 86 deletions

View File

@ -1,12 +0,0 @@
package ru.ulstu.fc.project.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.fc.rule.model.Rule;
public interface RuleRepository extends JpaRepository<Rule, Integer> {
List<Rule> findByProjectId(Integer projectId);
}

View File

@ -1,21 +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.project.repository.RuleRepository;
import ru.ulstu.fc.rule.model.Rule;
import ru.ulstu.fc.rule.repository.RuleRepository;
import ru.ulstu.fc.rule.model.FuzzyRule;
@Service
public class ProjectRulesService {
private final RuleRepository ruleRepository;
private final ProjectService projectService;
public ProjectRulesService(RuleRepository ruleRepository) {
public ProjectRulesService(RuleRepository ruleRepository,
ProjectService projectService) {
this.ruleRepository = ruleRepository;
this.projectService = projectService;
}
public List<Rule> getByProjectId(Integer projectId) {
return ruleRepository.findByProjectId(projectId);
public List<FuzzyRule> getByProjectId(Integer projectId) {
if (projectId == null || projectId == 0) {
return Collections.emptyList();
}
return ruleRepository.findByProject(projectService.getById(projectId));
}
}

View File

@ -7,7 +7,7 @@ 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.Rule;
import ru.ulstu.fc.rule.model.FuzzyRule;
import ru.ulstu.fc.rule.model.RuleForm;
import ru.ulstu.fc.rule.service.RuleService;
@ -27,7 +27,7 @@ public class RuleController {
model.addAttribute("rule",
new RuleForm((id != null && id != 0)
? ruleService.getById(id)
: new Rule()));
: new FuzzyRule()));
return "rule/edit";
}

View File

@ -4,11 +4,12 @@ import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import ru.ulstu.fc.core.model.BaseEntity;
@Entity
public class Antecedent extends BaseEntity {
@ManyToOne
private Variable variable;
private String value;
@ManyToOne
private Term term;
public Variable getVariable() {
return variable;
@ -16,10 +17,11 @@ public class Antecedent extends BaseEntity {
public void setVariable(Variable variable) {
this.variable = variable;
}
public String getValue() {
return value;
public Term getTerm() {
return term;
}
public void setValue(String value) {
this.value = value;
public void setTerm(Term term) {
this.term = term;
}
}

View File

@ -4,7 +4,7 @@ import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import ru.ulstu.fc.core.model.BaseEntity;
@Entity
public class Consequent extends BaseEntity {
@ManyToOne
private Variable variable;

View File

@ -0,0 +1,32 @@
package ru.ulstu.fc.rule.model;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import ru.ulstu.fc.core.model.BaseEntity;
import ru.ulstu.fc.project.model.Project;
@Entity
public class FuzzyRule extends BaseEntity {
@ManyToOne
private Project project;
private String content;
public FuzzyRule() {
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}

View File

@ -1,47 +0,0 @@
package ru.ulstu.fc.rule.model;
import java.util.List;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import ru.ulstu.fc.core.model.BaseEntity;
import ru.ulstu.fc.project.model.Project;
@Entity
public class Rule extends BaseEntity {
@ManyToOne
private Project project;
@OneToMany
private List<Antecedent> antecedents; //TODO: AND / OR?
@ManyToOne
private Consequent consequent;
public Rule() {
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
public List<Antecedent> getAntecedents() {
return antecedents;
}
public void setAntecedents(List<Antecedent> antecedents) {
this.antecedents = antecedents;
}
public Consequent getConsequent() {
return consequent;
}
public void setConsequent(Consequent consequent) {
this.consequent = consequent;
}
}

View File

@ -3,11 +3,12 @@ package ru.ulstu.fc.rule.model;
public class RuleForm {
private Integer id;
private Integer projectId;
private String value;
public RuleForm() {
}
public RuleForm(Rule rule) {
public RuleForm(FuzzyRule rule) {
this.projectId = (rule == null || rule.getProject() == null)
? null
: rule.getProject().getId();
@ -29,4 +30,12 @@ public class RuleForm {
this.projectId = projectId;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,16 @@
package ru.ulstu.fc.rule.model;
import jakarta.persistence.Entity;
import ru.ulstu.fc.core.model.BaseEntity;
public class Term extends BaseEntity {
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -7,7 +7,6 @@ import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import ru.ulstu.fc.core.model.BaseEntity;
@Entity
public class Variable extends BaseEntity {
private String name;
@OneToMany

View File

@ -3,7 +3,6 @@ package ru.ulstu.fc.rule.model;
import jakarta.persistence.Entity;
import ru.ulstu.fc.core.model.BaseEntity;
@Entity
public class VariableValue extends BaseEntity {
private String fuzzyTerm;
private Double value;

View File

@ -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.FuzzyRule;
public interface RuleRepository extends JpaRepository<FuzzyRule, Integer> {
List<FuzzyRule> findByProject(Project project);
}

View File

@ -98,6 +98,7 @@ 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;
}

View File

@ -2,9 +2,9 @@ package ru.ulstu.fc.rule.service;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.project.repository.RuleRepository;
import ru.ulstu.fc.rule.repository.RuleRepository;
import ru.ulstu.fc.project.service.ProjectService;
import ru.ulstu.fc.rule.model.Rule;
import ru.ulstu.fc.rule.model.FuzzyRule;
import ru.ulstu.fc.rule.model.RuleForm;
@Service
@ -17,7 +17,7 @@ public class RuleService {
this.projectService = projectService;
}
public Rule getById(Integer id) {
public FuzzyRule getById(Integer id) {
return ruleRepository
.findById(id)
.orElseThrow(() -> new RuntimeException("Rule not found by id"));
@ -25,14 +25,14 @@ public class RuleService {
public Object save(RuleForm ruleForm) {
if (ruleForm.getId() == null) {
Rule rule = new Rule();
FuzzyRule rule = new FuzzyRule();
rule.setProject(projectService.getById(ruleForm.getProjectId()));
// rule.set...
rule.setContent(ruleForm.getValue());
return ruleRepository.save(rule);
}
Rule dbRule = getById(ruleForm.getId());
FuzzyRule dbRule = getById(ruleForm.getId());
dbRule.setProject(projectService.getById(ruleForm.getProjectId()));
// dbRule.set ...
dbRule.setContent(ruleForm.getValue());
return ruleRepository.save(dbRule);
}

View File

@ -12,8 +12,17 @@
<input type="hidden" th:field="${projectId}">
<input type="hidden" th:field="*{id}">
<div class="form-group">
<label for="name">Название</label>
<label for="value">Правило</label>
<input th:field="*{value}"
id="value"
type="text"
required
class="form-control"
placeholder="Правило">
<p th:if="${#fields.hasErrors('value')}"
th:class="${#fields.hasErrors('value')}? error">
Не может быть пустым
</p>
</div>
<button name="save" type="submit" class="btn btn-outline-dark">Сохранить</button>