#2 -- Add inference dtos
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 2m25s

This commit is contained in:
Anton Romanov 2025-03-03 11:25:07 +04:00
parent 3f4072a757
commit 0dc7124bdf
6 changed files with 134 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.fc.rule.model.InferenceData;
import ru.ulstu.fc.rule.model.OutputValue;
import ru.ulstu.fc.rule.model.ProjectInferenceData;
import ru.ulstu.fc.rule.service.FuzzyInferenceService;
import java.util.List;
@ -26,4 +27,9 @@ public class InferenceRestController {
inferenceData.getInputVariables(),
inferenceData.getOutputVariable());
}
@RequestMapping(value = "getProjectInference", method = RequestMethod.POST)
public List<OutputValue> getProjectInference(@RequestBody ProjectInferenceData projectInferenceData) {
return fuzzyInferenceService.getProjectFuzzyInference(projectInferenceData);
}
}

View File

@ -0,0 +1,26 @@
package ru.ulstu.fc.rule.model;
import ru.ulstu.fc.rule.model.dto.VariableValueDto;
import java.util.List;
public class ProjectInferenceData {
private Integer projectId;
private List<VariableValueDto> variableValues;
public Integer getProjectId() {
return projectId;
}
public void setProjectId(Integer projectId) {
this.projectId = projectId;
}
public List<VariableValueDto> getVariableValues() {
return variableValues;
}
public void setVariableValues(List<VariableValueDto> variableValues) {
this.variableValues = variableValues;
}
}

View File

@ -0,0 +1,22 @@
package ru.ulstu.fc.rule.model.dto;
public class FuzzyTermDto {
private String description;
private Double crispValue;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getCrispValue() {
return crispValue;
}
public void setCrispValue(Double crispValue) {
this.crispValue = crispValue;
}
}

View File

@ -0,0 +1,24 @@
package ru.ulstu.fc.rule.model.dto;
import java.util.List;
public class VariableDto {
private String name;
private List<FuzzyTermDto> terms;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<FuzzyTermDto> getTerms() {
return terms;
}
public void setTerms(List<FuzzyTermDto> terms) {
this.terms = terms;
}
}

View File

@ -0,0 +1,22 @@
package ru.ulstu.fc.rule.model.dto;
public class VariableValueDto {
private String variableName;
private Double value;
public String getVariableName() {
return variableName;
}
public void setVariableName(String variableName) {
this.variableName = variableName;
}
public Double getValue() {
return value;
}
public void setValue(Double value) {
this.value = value;
}
}

View File

@ -14,9 +14,15 @@ import com.fuzzylite.variable.OutputVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.project.service.ProjectRulesService;
import ru.ulstu.fc.project.service.ProjectService;
import ru.ulstu.fc.project.service.ProjectVariableService;
import ru.ulstu.fc.rule.model.FuzzyRule;
import ru.ulstu.fc.rule.model.FuzzyTerm;
import ru.ulstu.fc.rule.model.OutputValue;
import ru.ulstu.fc.rule.model.ProjectInferenceData;
import ru.ulstu.fc.rule.model.Variable;
import ru.ulstu.fc.rule.model.dto.VariableValueDto;
import java.util.List;
import java.util.Map;
@ -31,9 +37,17 @@ public class FuzzyInferenceService {
+ " is %s";
private final static String NO_RESULT = "Нет результата";
private final Engine fuzzyEngine;
private final ProjectService projectService;
private final FuzzyRuleService fuzzyRuleService;
private final ProjectRulesService projectRulesService;
private final ProjectVariableService projectVariableService;
public FuzzyInferenceService(Engine fuzzyEngine) {
public FuzzyInferenceService(Engine fuzzyEngine, ProjectService projectService, FuzzyRuleService fuzzyRuleService, ProjectRulesService projectRulesService, ProjectVariableService projectVariableService) {
this.fuzzyEngine = fuzzyEngine;
this.projectService = projectService;
this.fuzzyRuleService = fuzzyRuleService;
this.projectRulesService = projectRulesService;
this.projectVariableService = projectVariableService;
}
private List<String> getDemoRules() {
@ -152,4 +166,23 @@ public class FuzzyInferenceService {
fuzzyEngine.addRuleBlock(getRuleBlock(fuzzyEngine, rules, inputVariables, outputVariable));
return getConsequent(fuzzyEngine, values);
}
public List<OutputValue> getProjectFuzzyInference(ProjectInferenceData projectInferenceData) {
List<String> fuzzyRules = projectRulesService.getByProjectId(projectInferenceData.getProjectId())
.stream()
.map(FuzzyRule::getContent)
.toList();
Map<String, Double> variableValues = projectInferenceData.getVariableValues()
.stream()
.collect(Collectors.toMap(
VariableValueDto::getVariableName,
VariableValueDto::getValue));
List<Variable> inputVariables = projectVariableService.getByProjectId(projectInferenceData.getProjectId());
return getFuzzyInference(fuzzyRules,
variableValues,
inputVariables,
new Variable());
}
}