diff --git a/src/main/java/ru/ulstu/fc/project/model/ProjectDto.java b/src/main/java/ru/ulstu/fc/project/model/ProjectDto.java index 1031b37..199dfeb 100644 --- a/src/main/java/ru/ulstu/fc/project/model/ProjectDto.java +++ b/src/main/java/ru/ulstu/fc/project/model/ProjectDto.java @@ -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(); diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java index 72bc3f6..badcaf1 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java @@ -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); } } diff --git a/src/main/java/ru/ulstu/fc/rule/controller/InferenceRestController.java b/src/main/java/ru/ulstu/fc/rule/controller/InferenceRestController.java index b95cd74..0f0619d 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/InferenceRestController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/InferenceRestController.java @@ -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 getProjectInference(@RequestBody ProjectInferenceData projectInferenceData) { return fuzzyInferenceService.getProjectFuzzyInference(projectInferenceData); } + + @RequestMapping(value = "get-inference-by-generated-rules", method = RequestMethod.POST) + public List getInferenceByGenerateRules(@RequestBody FuzzyRuleDataDto fuzzyRuleDataDto) { + return fuzzyInferenceService.getProjectFuzzyInferenceByGeneratedRules(fuzzyRuleDataDto); + } } diff --git a/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java b/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java index aa0ea79..a93a129 100644 --- a/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java +++ b/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java @@ -24,4 +24,7 @@ public interface VariableRepository extends JpaRepository { @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); } diff --git a/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java b/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java index 5e7c3e4..811818d 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java @@ -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 getProjectFuzzyInferenceByGeneratedRules(FuzzyRuleDataDto fuzzyRuleDataDto) { + ProjectDto projectDto = projectService.save(new ProjectDto(new Date().toString())); + fuzzyRuleParseService.generateRules(projectDto.getId(), fuzzyRuleDataDto); + Map variableValues = new HashMap<>(); + List 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); + } +} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java index e63718b..7141a98 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java @@ -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); + } }