fuzzy-controller/src/main/java/ru/ulstu/fc/rule/controller/InferenceMvcController.java
Anton Romanov 9666419ed2
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m19s
#13 -- Fix rule models
2025-02-17 12:09:24 +04:00

66 lines
2.6 KiB
Java

package ru.ulstu.fc.rule.controller;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import ru.ulstu.fc.rule.model.Antecedent;
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 java.util.Arrays;
import java.util.List;
import java.util.Map;
@Controller
@Hidden
public class InferenceMvcController {
private final FuzzyInferenceService fuzzyInferenceService;
public InferenceMvcController(FuzzyInferenceService fuzzyInferenceService) {
this.fuzzyInferenceService = fuzzyInferenceService;
}
@GetMapping("/")
public String initInference(Model model) {
model.addAttribute("ageValues", getAgeValues());
model.addAttribute("incomeValues", getIncomeValues());
model.addAttribute("inferenceForm", new InferenceForm());
return "index";
}
@RequestMapping(value = "get-inference", method = RequestMethod.POST)
public String getInference(@ModelAttribute InferenceForm inferenceForm, Model model) {
model.addAttribute("ageValues", getAgeValues());
model.addAttribute("incomeValues", getIncomeValues());
model.addAttribute("inferenceForm", inferenceForm);
model.addAttribute("response", fuzzyInferenceService.getFuzzyInference(
Map.of("возраст", Double.valueOf(inferenceForm.getAgeValue()),
"доход", Double.valueOf(inferenceForm.getIncomeValue()))));
return "index";
}
private List<VariableValue> getAgeValues() {
Variable var = new Variable("Age");
var.getValues().addAll(Arrays.asList(
new VariableValue("молодой", 30.0),
new VariableValue("средний", 45.0),
new VariableValue("старый", 60.0)));
return var.getValues();
}
private List<VariableValue> getIncomeValues() {
Variable var = new Variable("Income");
var.getValues().addAll(Arrays.asList(
new VariableValue("небольшой", 20000.0),
new VariableValue("средний", 90000.0),
new VariableValue("высокий", 200000.0)));
return var.getValues();
}
}