#13 -- Fix rule models
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m19s

This commit is contained in:
Anton Romanov 2025-02-17 12:09:24 +04:00
parent ef9b78251c
commit 9666419ed2
8 changed files with 130 additions and 47 deletions

View File

@ -9,6 +9,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import ru.ulstu.fc.rule.model.Antecedent; import ru.ulstu.fc.rule.model.Antecedent;
import ru.ulstu.fc.rule.model.InferenceForm; import ru.ulstu.fc.rule.model.InferenceForm;
import ru.ulstu.fc.rule.model.Variable;
import ru.ulstu.fc.rule.model.VariableValue;
import ru.ulstu.fc.rule.service.FuzzyInferenceService; import ru.ulstu.fc.rule.service.FuzzyInferenceService;
import java.util.Arrays; import java.util.Arrays;
@ -26,35 +28,38 @@ public class InferenceMvcController {
@GetMapping("/") @GetMapping("/")
public String initInference(Model model) { public String initInference(Model model) {
model.addAttribute("ageAntecedents", getAgeAntecedents()); model.addAttribute("ageValues", getAgeValues());
model.addAttribute("incomeAntecedents", getIncomeAntecedents()); model.addAttribute("incomeValues", getIncomeValues());
model.addAttribute("inferenceForm", new InferenceForm()); model.addAttribute("inferenceForm", new InferenceForm());
return "index"; return "index";
} }
@RequestMapping(value = "get-inference", method = RequestMethod.POST) @RequestMapping(value = "get-inference", method = RequestMethod.POST)
public String getInference(@ModelAttribute InferenceForm inferenceForm, Model model) { public String getInference(@ModelAttribute InferenceForm inferenceForm, Model model) {
model.addAttribute("ageAntecedents", getAgeAntecedents()); model.addAttribute("ageValues", getAgeValues());
model.addAttribute("incomeAntecedents", getIncomeAntecedents()); model.addAttribute("incomeValues", getIncomeValues());
model.addAttribute("inferenceForm", inferenceForm); model.addAttribute("inferenceForm", inferenceForm);
model.addAttribute("response", fuzzyInferenceService.getFuzzyInference( model.addAttribute("response", fuzzyInferenceService.getFuzzyInference(
Map.of("возраст", Double.valueOf(inferenceForm.getAgeAntecedent()), Map.of("возраст", Double.valueOf(inferenceForm.getAgeValue()),
"доход", Double.valueOf(inferenceForm.getIncomeAntecedent()) "доход", Double.valueOf(inferenceForm.getIncomeValue()))));
)));
return "index"; return "index";
} }
private List<Antecedent> getAgeAntecedents() { private List<VariableValue> getAgeValues() {
return Arrays.asList( Variable var = new Variable("Age");
new Antecedent("молодой", "30"), var.getValues().addAll(Arrays.asList(
new Antecedent("средний", "45"), new VariableValue("молодой", 30.0),
new Antecedent("старый", "60")); new VariableValue("средний", 45.0),
new VariableValue("старый", 60.0)));
return var.getValues();
} }
private List<Antecedent> getIncomeAntecedents() { private List<VariableValue> getIncomeValues() {
return Arrays.asList( Variable var = new Variable("Income");
new Antecedent("небольшой", "20000"), var.getValues().addAll(Arrays.asList(
new Antecedent("средний", "90000"), new VariableValue("небольшой", 20000.0),
new Antecedent("высокий", "200000")); new VariableValue("средний", 90000.0),
new VariableValue("высокий", 200000.0)));
return var.getValues();
} }
} }

View File

@ -1,19 +1,29 @@
package ru.ulstu.fc.rule.model; package ru.ulstu.fc.rule.model;
public class Antecedent { import jakarta.persistence.Entity;
private String value; import jakarta.persistence.ManyToOne;
private String description; import ru.ulstu.fc.core.model.BaseEntity;
public Antecedent(String description, String value) { @Entity
this.description = description; public class Antecedent extends BaseEntity {
this.value = value; @ManyToOne
private Variable variable;
private String value;
public Variable getVariable() {
return variable;
}
public void setVariable(Variable variable) {
this.variable = variable;
} }
public String getValue() { public String getValue() {
return value; return value;
} }
public String getDescription() { public void setValue(String value) {
return description; this.value = value;
} }
} }

View File

@ -0,0 +1,29 @@
package ru.ulstu.fc.rule.model;
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;
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;
}
}

View File

@ -1,22 +1,22 @@
package ru.ulstu.fc.rule.model; package ru.ulstu.fc.rule.model;
public class InferenceForm { public class InferenceForm {
private String ageAntecedent; private String ageValue;
private String incomeAntecedent; private String incomeValue;
public String getAgeAntecedent() { public String getAgeValue() {
return ageAntecedent; return ageValue;
} }
public void setAgeAntecedent(String ageAntecedent) { public void setAgeValue(String ageAntecedent) {
this.ageAntecedent = ageAntecedent; this.ageValue = ageAntecedent;
} }
public String getIncomeAntecedent() { public String getIncomeValue() {
return incomeAntecedent; return incomeValue;
} }
public void setIncomeAntecedent(String incomeAntecedent) { public void setIncomeValue(String incomeAntecedent) {
this.incomeAntecedent = incomeAntecedent; this.incomeValue = incomeAntecedent;
} }
} }

View File

@ -1,7 +1,10 @@
package ru.ulstu.fc.rule.model; package ru.ulstu.fc.rule.model;
import java.util.List;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
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;
@ -9,6 +12,10 @@ import ru.ulstu.fc.project.model.Project;
public class Rule extends BaseEntity { public class Rule extends BaseEntity {
@ManyToOne @ManyToOne
private Project project; private Project project;
@OneToMany
private List<Antecedent> antecedents; //TODO: AND / OR?
@ManyToOne
private Consequent consequent;
public Project getProject() { public Project getProject() {
return project; return project;
@ -16,5 +23,22 @@ public class Rule extends BaseEntity {
public void setProject(Project project) { public void setProject(Project project) {
this.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

@ -1,10 +1,17 @@
package ru.ulstu.fc.rule.model; package ru.ulstu.fc.rule.model;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Variable { import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import ru.ulstu.fc.core.model.BaseEntity;
@Entity
public class Variable extends BaseEntity {
private String name; private String name;
private List<VariableValue> values; @OneToMany
private List<VariableValue> values = new ArrayList<>();
public Variable() { public Variable() {
} }
@ -14,6 +21,10 @@ public class Variable {
this.values = values; this.values = values;
} }
public Variable(String name) {
this.name = name;
}
public String getName() { public String getName() {
return name; return name;
} }

View File

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

View File

@ -25,11 +25,11 @@
</div> </div>
<div class="col-md-6 col-sm-12"> <div class="col-md-6 col-sm-12">
<select id="select-age-antecedent" class="selectpicker m-2" data-live-search="true" <select id="select-age-antecedent" class="selectpicker m-2" data-live-search="true"
th:field="*{ageAntecedent}" th:field="*{ageValue}"
data-width="90%"> data-width="90%">
<option th:each="ageAntecedent : ${ageAntecedents}" <option th:each="ageValue : ${ageValues}"
th:value="${ageAntecedent.value}" th:value="${ageValue.value}"
th:utext="${ageAntecedent.description}"> th:utext="${ageValue.fuzzyTerm}">
</option> </option>
</select> </select>
</div> </div>
@ -40,11 +40,11 @@
</div> </div>
<div class="col-md-6 col-sm-12"> <div class="col-md-6 col-sm-12">
<select id="select-income-antecedent" class="selectpicker m-2" data-live-search="true" <select id="select-income-antecedent" class="selectpicker m-2" data-live-search="true"
th:field="*{incomeAntecedent}" th:field="*{incomeValue}"
data-width="90%"> data-width="90%">
<option th:each="incomeAntecedent : ${incomeAntecedents}" <option th:each="incomeValue : ${incomeValues}"
th:value="${incomeAntecedent.value}" th:value="${incomeValue.value}"
th:utext="${incomeAntecedent.description}"> th:utext="${incomeValue.fuzzyTerm}">
</option> </option>
</select> </select>
</div> </div>