#31 -- Fix inference
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 59s

This commit is contained in:
Anton Romanov 2025-03-19 23:36:37 +04:00
parent 38642e593e
commit 0fb80f03d2
6 changed files with 57 additions and 9 deletions

View File

@ -13,6 +13,11 @@ public class ProjectDto {
public ProjectDto() {
}
public ProjectDto(String name) {
this.name = name;
}
public ProjectDto(Project project) {
this.id = project.getId();
this.name = project.getName();

View File

@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.fc.project.service.ProjectRulesService;
@ -59,7 +60,7 @@ public class FuzzyRuleRestController {
}
@PostMapping("generate-rules/{projectId}")
public void generateRules(@PathVariable("projectId") Integer projectId, FuzzyRuleDataDto fuzzyRuleDataDto) {
public void generateRules(@PathVariable("projectId") Integer projectId, @RequestBody FuzzyRuleDataDto fuzzyRuleDataDto) {
fuzzyRuleParseService.generateRules(projectId, fuzzyRuleDataDto);
}
}

View File

@ -7,6 +7,7 @@ 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.model.dto.FuzzyRuleDataDto;
import ru.ulstu.fc.rule.service.FuzzyInferenceService;
import java.util.List;
@ -28,8 +29,13 @@ public class InferenceRestController {
inferenceData.getOutputVariables());
}
@RequestMapping(value = "getProjectInference", method = RequestMethod.POST)
@RequestMapping(value = "get-project-inference", method = RequestMethod.POST)
public List<OutputValue> getProjectInference(@RequestBody ProjectInferenceData projectInferenceData) {
return fuzzyInferenceService.getProjectFuzzyInference(projectInferenceData);
}
@RequestMapping(value = "get-inference-by-generated-rules", method = RequestMethod.POST)
public List<OutputValue> getInferenceByGenerateRules(@RequestBody FuzzyRuleDataDto fuzzyRuleDataDto) {
return fuzzyInferenceService.getProjectFuzzyInferenceByGeneratedRules(fuzzyRuleDataDto);
}
}

View File

@ -24,4 +24,7 @@ public interface VariableRepository extends JpaRepository<Variable, Integer> {
@Query("DELETE FROM Variable v WHERE v.project.id = :projectId")
@Modifying
void deleteAllByProjectId(@Param("projectId") Integer projectId);
@Query("SELECT v FROM Variable v WHERE v.project = :project AND v.input = true AND v.name = :name")
Variable getByProjectAndName(@Param("project") Project project, @Param("name") String name);
}

View File

@ -11,9 +11,8 @@ import com.fuzzylite.rule.RuleBlock;
import com.fuzzylite.term.Triangle;
import com.fuzzylite.variable.InputVariable;
import com.fuzzylite.variable.OutputVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.project.model.ProjectDto;
import ru.ulstu.fc.project.service.ProjectRulesService;
import ru.ulstu.fc.project.service.ProjectService;
import ru.ulstu.fc.project.service.ProjectVariableService;
@ -22,17 +21,20 @@ 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.FuzzyRuleDataDto;
import ru.ulstu.fc.rule.model.dto.VariableValueDto;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class FuzzyInferenceService {
private final static Logger LOG = LoggerFactory.getLogger(FuzzyInferenceService.class);
private final static String OUTPUT_VARIABLE_NAME = "кредит";
private final static String RULE_TEMPLATE = "if %s is %s and %s is %s then "
+ OUTPUT_VARIABLE_NAME
@ -40,14 +42,21 @@ public class FuzzyInferenceService {
private final static String NO_RESULT = "Нет результата";
private final Engine fuzzyEngine;
private final ProjectService projectService;
private final FuzzyRuleService fuzzyRuleService;
private final VariableService variableService;
private final FuzzyRuleParseService fuzzyRuleParseService;
private final ProjectRulesService projectRulesService;
private final ProjectVariableService projectVariableService;
public FuzzyInferenceService(Engine fuzzyEngine, ProjectService projectService, FuzzyRuleService fuzzyRuleService, ProjectRulesService projectRulesService, ProjectVariableService projectVariableService) {
public FuzzyInferenceService(Engine fuzzyEngine,
ProjectService projectService,
VariableService variableService,
FuzzyRuleParseService fuzzyRuleParseService,
ProjectRulesService projectRulesService,
ProjectVariableService projectVariableService) {
this.fuzzyEngine = fuzzyEngine;
this.projectService = projectService;
this.fuzzyRuleService = fuzzyRuleService;
this.variableService = variableService;
this.fuzzyRuleParseService = fuzzyRuleParseService;
this.projectRulesService = projectRulesService;
this.projectVariableService = projectVariableService;
}
@ -202,4 +211,24 @@ public class FuzzyInferenceService {
inputVariables,
outputVariables);
}
}
public List<OutputValue> getProjectFuzzyInferenceByGeneratedRules(FuzzyRuleDataDto fuzzyRuleDataDto) {
ProjectDto projectDto = projectService.save(new ProjectDto(new Date().toString()));
fuzzyRuleParseService.generateRules(projectDto.getId(), fuzzyRuleDataDto);
Map<String, Double> variableValues = new HashMap<>();
List<String> labels = new LinkedList<>(Arrays.asList(fuzzyRuleDataDto.getFuzzyTerms()));
while (labels.size() > fuzzyRuleDataDto.getWindow()) {
labels.removeFirst();
}
for (int i = 0; i < labels.size(); i++) {
String label = labels.get(i);
variableValues.put("fuzzyLevel" + i,
variableService.getByProjectAndName(projectDto.getId(), "fuzzyLevel" + i)
.getFuzzyTerms()
.stream()
.filter(ft -> ft.getDescription().equals(label))
.mapToDouble(FuzzyTerm::getCrispValue).findAny().getAsDouble());
}
return getProjectFuzzyInference(projectDto.getId(), variableValues);
}
}

View File

@ -89,4 +89,8 @@ public class VariableService {
public void clearVariables(Integer projectId) {
variableRepository.deleteAllByProjectId(projectId);
}
public Variable getByProjectAndName(Integer projectId, String name) {
return variableRepository.getByProjectAndName(projectService.getById(projectId), name);
}
}