#5 -- Rule model as a string
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m3s
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m3s
This commit is contained in:
parent
0d7f970629
commit
486981c0ff
@ -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);
|
|
||||||
}
|
|
@ -1,21 +1,28 @@
|
|||||||
package ru.ulstu.fc.project.service;
|
package ru.ulstu.fc.project.service;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import ru.ulstu.fc.project.repository.RuleRepository;
|
import ru.ulstu.fc.rule.repository.RuleRepository;
|
||||||
import ru.ulstu.fc.rule.model.Rule;
|
import ru.ulstu.fc.rule.model.FuzzyRule;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ProjectRulesService {
|
public class ProjectRulesService {
|
||||||
private final RuleRepository ruleRepository;
|
private final RuleRepository ruleRepository;
|
||||||
|
private final ProjectService projectService;
|
||||||
|
|
||||||
public ProjectRulesService(RuleRepository ruleRepository) {
|
public ProjectRulesService(RuleRepository ruleRepository,
|
||||||
|
ProjectService projectService) {
|
||||||
this.ruleRepository = ruleRepository;
|
this.ruleRepository = ruleRepository;
|
||||||
|
this.projectService = projectService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Rule> getByProjectId(Integer projectId) {
|
public List<FuzzyRule> getByProjectId(Integer projectId) {
|
||||||
return ruleRepository.findByProjectId(projectId);
|
if (projectId == null || projectId == 0) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
return ruleRepository.findByProject(projectService.getById(projectId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ 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.Rule;
|
import ru.ulstu.fc.rule.model.FuzzyRule;
|
||||||
import ru.ulstu.fc.rule.model.RuleForm;
|
import ru.ulstu.fc.rule.model.RuleForm;
|
||||||
import ru.ulstu.fc.rule.service.RuleService;
|
import ru.ulstu.fc.rule.service.RuleService;
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ public class RuleController {
|
|||||||
model.addAttribute("rule",
|
model.addAttribute("rule",
|
||||||
new RuleForm((id != null && id != 0)
|
new RuleForm((id != null && id != 0)
|
||||||
? ruleService.getById(id)
|
? ruleService.getById(id)
|
||||||
: new Rule()));
|
: new FuzzyRule()));
|
||||||
|
|
||||||
return "rule/edit";
|
return "rule/edit";
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,12 @@ import jakarta.persistence.Entity;
|
|||||||
import jakarta.persistence.ManyToOne;
|
import jakarta.persistence.ManyToOne;
|
||||||
import ru.ulstu.fc.core.model.BaseEntity;
|
import ru.ulstu.fc.core.model.BaseEntity;
|
||||||
|
|
||||||
@Entity
|
|
||||||
public class Antecedent extends BaseEntity {
|
public class Antecedent extends BaseEntity {
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
private Variable variable;
|
private Variable variable;
|
||||||
private String value;
|
@ManyToOne
|
||||||
|
private Term term;
|
||||||
|
|
||||||
public Variable getVariable() {
|
public Variable getVariable() {
|
||||||
return variable;
|
return variable;
|
||||||
@ -16,10 +17,11 @@ public class Antecedent extends BaseEntity {
|
|||||||
public void setVariable(Variable variable) {
|
public void setVariable(Variable variable) {
|
||||||
this.variable = variable;
|
this.variable = variable;
|
||||||
}
|
}
|
||||||
public String getValue() {
|
public Term getTerm() {
|
||||||
return value;
|
return term;
|
||||||
}
|
}
|
||||||
public void setValue(String value) {
|
public void setTerm(Term term) {
|
||||||
this.value = value;
|
this.term = term;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import jakarta.persistence.Entity;
|
|||||||
import jakarta.persistence.ManyToOne;
|
import jakarta.persistence.ManyToOne;
|
||||||
import ru.ulstu.fc.core.model.BaseEntity;
|
import ru.ulstu.fc.core.model.BaseEntity;
|
||||||
|
|
||||||
@Entity
|
|
||||||
public class Consequent extends BaseEntity {
|
public class Consequent extends BaseEntity {
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
private Variable variable;
|
private Variable variable;
|
||||||
|
32
src/main/java/ru/ulstu/fc/rule/model/FuzzyRule.java
Normal file
32
src/main/java/ru/ulstu/fc/rule/model/FuzzyRule.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -3,11 +3,12 @@ package ru.ulstu.fc.rule.model;
|
|||||||
public class RuleForm {
|
public class RuleForm {
|
||||||
private Integer id;
|
private Integer id;
|
||||||
private Integer projectId;
|
private Integer projectId;
|
||||||
|
private String value;
|
||||||
|
|
||||||
public RuleForm() {
|
public RuleForm() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public RuleForm(Rule rule) {
|
public RuleForm(FuzzyRule rule) {
|
||||||
this.projectId = (rule == null || rule.getProject() == null)
|
this.projectId = (rule == null || rule.getProject() == null)
|
||||||
? null
|
? null
|
||||||
: rule.getProject().getId();
|
: rule.getProject().getId();
|
||||||
@ -29,4 +30,12 @@ public class RuleForm {
|
|||||||
this.projectId = projectId;
|
this.projectId = projectId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
16
src/main/java/ru/ulstu/fc/rule/model/Term.java
Normal file
16
src/main/java/ru/ulstu/fc/rule/model/Term.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -7,7 +7,6 @@ import jakarta.persistence.Entity;
|
|||||||
import jakarta.persistence.OneToMany;
|
import jakarta.persistence.OneToMany;
|
||||||
import ru.ulstu.fc.core.model.BaseEntity;
|
import ru.ulstu.fc.core.model.BaseEntity;
|
||||||
|
|
||||||
@Entity
|
|
||||||
public class Variable extends BaseEntity {
|
public class Variable extends BaseEntity {
|
||||||
private String name;
|
private String name;
|
||||||
@OneToMany
|
@OneToMany
|
||||||
|
@ -3,7 +3,6 @@ package ru.ulstu.fc.rule.model;
|
|||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
import ru.ulstu.fc.core.model.BaseEntity;
|
import ru.ulstu.fc.core.model.BaseEntity;
|
||||||
|
|
||||||
@Entity
|
|
||||||
public class VariableValue extends BaseEntity {
|
public class VariableValue extends BaseEntity {
|
||||||
private String fuzzyTerm;
|
private String fuzzyTerm;
|
||||||
private Double value;
|
private Double value;
|
||||||
|
@ -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);
|
||||||
|
}
|
@ -98,6 +98,7 @@ public class FuzzyInferenceService {
|
|||||||
mamdani.setImplication(new AlgebraicProduct());
|
mamdani.setImplication(new AlgebraicProduct());
|
||||||
mamdani.setActivation(new General());
|
mamdani.setActivation(new General());
|
||||||
rules.forEach(r -> mamdani.addRule(Rule.parse(r, engine)));
|
rules.forEach(r -> mamdani.addRule(Rule.parse(r, engine)));
|
||||||
|
mamdani.addRule(new Rule());
|
||||||
return mamdani;
|
return mamdani;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,9 +2,9 @@ package ru.ulstu.fc.rule.service;
|
|||||||
|
|
||||||
import org.springframework.stereotype.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.project.service.ProjectService;
|
||||||
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.model.RuleForm;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -17,7 +17,7 @@ public class RuleService {
|
|||||||
this.projectService = projectService;
|
this.projectService = projectService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Rule getById(Integer id) {
|
public FuzzyRule getById(Integer id) {
|
||||||
return ruleRepository
|
return ruleRepository
|
||||||
.findById(id)
|
.findById(id)
|
||||||
.orElseThrow(() -> new RuntimeException("Rule not found by id"));
|
.orElseThrow(() -> new RuntimeException("Rule not found by id"));
|
||||||
@ -25,14 +25,14 @@ public class RuleService {
|
|||||||
|
|
||||||
public Object save(RuleForm ruleForm) {
|
public Object save(RuleForm ruleForm) {
|
||||||
if (ruleForm.getId() == null) {
|
if (ruleForm.getId() == null) {
|
||||||
Rule rule = new Rule();
|
FuzzyRule rule = new FuzzyRule();
|
||||||
rule.setProject(projectService.getById(ruleForm.getProjectId()));
|
rule.setProject(projectService.getById(ruleForm.getProjectId()));
|
||||||
// rule.set...
|
rule.setContent(ruleForm.getValue());
|
||||||
return ruleRepository.save(rule);
|
return ruleRepository.save(rule);
|
||||||
}
|
}
|
||||||
Rule dbRule = getById(ruleForm.getId());
|
FuzzyRule dbRule = getById(ruleForm.getId());
|
||||||
dbRule.setProject(projectService.getById(ruleForm.getProjectId()));
|
dbRule.setProject(projectService.getById(ruleForm.getProjectId()));
|
||||||
// dbRule.set ...
|
dbRule.setContent(ruleForm.getValue());
|
||||||
return ruleRepository.save(dbRule);
|
return ruleRepository.save(dbRule);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,8 +12,17 @@
|
|||||||
<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">
|
||||||
<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>
|
</div>
|
||||||
|
|
||||||
<button name="save" type="submit" class="btn btn-outline-dark">Сохранить</button>
|
<button name="save" type="submit" class="btn btn-outline-dark">Сохранить</button>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user