From 0dc7124bdf46644098bc6d736ee6fcc93b528cff Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 3 Mar 2025 11:25:07 +0400 Subject: [PATCH 1/7] #2 -- Add inference dtos --- .../controller/InferenceRestController.java | 6 ++++ .../fc/rule/model/ProjectInferenceData.java | 26 ++++++++++++++ .../ulstu/fc/rule/model/dto/FuzzyTermDto.java | 22 ++++++++++++ .../ulstu/fc/rule/model/dto/VariableDto.java | 24 +++++++++++++ .../fc/rule/model/dto/VariableValueDto.java | 22 ++++++++++++ .../rule/service/FuzzyInferenceService.java | 35 ++++++++++++++++++- 6 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ru/ulstu/fc/rule/model/ProjectInferenceData.java create mode 100644 src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyTermDto.java create mode 100644 src/main/java/ru/ulstu/fc/rule/model/dto/VariableDto.java create mode 100644 src/main/java/ru/ulstu/fc/rule/model/dto/VariableValueDto.java 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 dcb0a8f..453b155 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/InferenceRestController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/InferenceRestController.java @@ -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 getProjectInference(@RequestBody ProjectInferenceData projectInferenceData) { + return fuzzyInferenceService.getProjectFuzzyInference(projectInferenceData); + } } diff --git a/src/main/java/ru/ulstu/fc/rule/model/ProjectInferenceData.java b/src/main/java/ru/ulstu/fc/rule/model/ProjectInferenceData.java new file mode 100644 index 0000000..3518972 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/model/ProjectInferenceData.java @@ -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 variableValues; + + public Integer getProjectId() { + return projectId; + } + + public void setProjectId(Integer projectId) { + this.projectId = projectId; + } + + public List getVariableValues() { + return variableValues; + } + + public void setVariableValues(List variableValues) { + this.variableValues = variableValues; + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyTermDto.java b/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyTermDto.java new file mode 100644 index 0000000..3dd831a --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyTermDto.java @@ -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; + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/dto/VariableDto.java b/src/main/java/ru/ulstu/fc/rule/model/dto/VariableDto.java new file mode 100644 index 0000000..46828b8 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/model/dto/VariableDto.java @@ -0,0 +1,24 @@ +package ru.ulstu.fc.rule.model.dto; + +import java.util.List; + +public class VariableDto { + private String name; + private List terms; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getTerms() { + return terms; + } + + public void setTerms(List terms) { + this.terms = terms; + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/dto/VariableValueDto.java b/src/main/java/ru/ulstu/fc/rule/model/dto/VariableValueDto.java new file mode 100644 index 0000000..6984841 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/model/dto/VariableValueDto.java @@ -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; + } +} 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 3812d83..3754307 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java @@ -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 getDemoRules() { @@ -152,4 +166,23 @@ public class FuzzyInferenceService { fuzzyEngine.addRuleBlock(getRuleBlock(fuzzyEngine, rules, inputVariables, outputVariable)); return getConsequent(fuzzyEngine, values); } + + public List getProjectFuzzyInference(ProjectInferenceData projectInferenceData) { + List fuzzyRules = projectRulesService.getByProjectId(projectInferenceData.getProjectId()) + .stream() + .map(FuzzyRule::getContent) + .toList(); + Map variableValues = projectInferenceData.getVariableValues() + .stream() + .collect(Collectors.toMap( + VariableValueDto::getVariableName, + VariableValueDto::getValue)); + List inputVariables = projectVariableService.getByProjectId(projectInferenceData.getProjectId()); + + + return getFuzzyInference(fuzzyRules, + variableValues, + inputVariables, + new Variable()); + } } -- 2.34.1 From 131449c8c3b371dcd78e1ccbfc48390ccec205dd Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 3 Mar 2025 11:59:38 +0400 Subject: [PATCH 2/7] #2 -- Fix using dto --- .../rule/controller/FuzzyRuleController.java | 9 ++++---- .../ulstu/fc/rule/model/dto/FuzzyTermDto.java | 20 ++++++++++++++++ .../ulstu/fc/rule/model/dto/VariableDto.java | 23 ++++++++++++++++++- .../fc/rule/service/VariableService.java | 8 +++++++ src/main/resources/public/js/fuzzyRule.js | 14 +++-------- .../resources/templates/project/edit.html | 4 ++-- 6 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java index a19f79f..4ae9a30 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java @@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import ru.ulstu.fc.rule.model.FuzzyRuleForm; import ru.ulstu.fc.rule.model.FuzzyTerm; -import ru.ulstu.fc.rule.model.Variable; +import ru.ulstu.fc.rule.model.dto.VariableDto; import ru.ulstu.fc.rule.service.FuzzyRuleService; import ru.ulstu.fc.rule.service.FuzzyTermService; import ru.ulstu.fc.rule.service.VariableService; @@ -65,11 +65,10 @@ public class FuzzyRuleController { @ResponseBody @GetMapping("/getVariables/{projectId}") - public List getVariables(@PathVariable("projectId") Integer projectId, - final HttpServletResponse response) { + public List getVariables(@PathVariable("projectId") Integer projectId, + final HttpServletResponse response) { response.addHeader("Cache-Control", "max-age=60, must-revalidate, no-transform"); - //TODO: return DTO without terms - return variableService.getAllByProject(projectId); + return variableService.getAllDtoByProject(projectId); } @ResponseBody diff --git a/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyTermDto.java b/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyTermDto.java index 3dd831a..85a3bf9 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyTermDto.java +++ b/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyTermDto.java @@ -1,9 +1,21 @@ package ru.ulstu.fc.rule.model.dto; +import ru.ulstu.fc.rule.model.FuzzyTerm; + public class FuzzyTermDto { + private Integer id; private String description; private Double crispValue; + public FuzzyTermDto() { + } + + public FuzzyTermDto(FuzzyTerm fuzzyTerm) { + this.description = fuzzyTerm.getDescription(); + this.crispValue = fuzzyTerm.getCrispValue(); + this.id = fuzzyTerm.getId(); + } + public String getDescription() { return description; } @@ -19,4 +31,12 @@ public class FuzzyTermDto { public void setCrispValue(Double crispValue) { this.crispValue = crispValue; } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } } diff --git a/src/main/java/ru/ulstu/fc/rule/model/dto/VariableDto.java b/src/main/java/ru/ulstu/fc/rule/model/dto/VariableDto.java index 46828b8..f708cfa 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/dto/VariableDto.java +++ b/src/main/java/ru/ulstu/fc/rule/model/dto/VariableDto.java @@ -1,10 +1,23 @@ package ru.ulstu.fc.rule.model.dto; +import ru.ulstu.fc.rule.model.Variable; + +import java.util.ArrayList; import java.util.List; public class VariableDto { + private Integer id; private String name; - private List terms; + private List terms = new ArrayList<>(); + + public VariableDto() { + } + + public VariableDto(Variable variable) { + this.id = variable.getId(); + this.name = variable.getName(); + this.terms = variable.getFuzzyTerms().stream().map(FuzzyTermDto::new).toList(); + } public String getName() { return name; @@ -21,4 +34,12 @@ public class VariableDto { public void setTerms(List terms) { this.terms = terms; } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } } 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 732aebb..44e94c3 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java @@ -5,6 +5,7 @@ import ru.ulstu.fc.project.service.ProjectService; import ru.ulstu.fc.rule.model.FuzzyTerm; import ru.ulstu.fc.rule.model.Variable; import ru.ulstu.fc.rule.model.VariableForm; +import ru.ulstu.fc.rule.model.dto.VariableDto; import ru.ulstu.fc.rule.repository.VariableRepository; import java.util.List; @@ -69,6 +70,13 @@ public class VariableService { return variableRepository.getByProject(projectService.getById(projectId)); } + public List getAllDtoByProject(Integer projectId) { + return variableRepository.getByProject(projectService.getById(projectId)) + .stream() + .map(VariableDto::new) + .toList(); + } + public void checkIsCurrentUserVariableWithThrow(Variable variable) { projectService.checkIsCurrentUserProjectWithThrow(variable.getProject()); } diff --git a/src/main/resources/public/js/fuzzyRule.js b/src/main/resources/public/js/fuzzyRule.js index e678cd9..9838bd7 100644 --- a/src/main/resources/public/js/fuzzyRule.js +++ b/src/main/resources/public/js/fuzzyRule.js @@ -5,8 +5,7 @@ function getAntecedent(rule) { return withoutIf[1].trim().split('then')[0].trim(); } -// TODO: remove duplicate -function getAntecedentComponents(antecedent) { +function getRuleComponents(antecedent) { return antecedent.split('and').map((i) => i.trim()); } @@ -16,11 +15,6 @@ function getConsequent(rule) { return withoutIf[1].trim().split('then')[1].trim(); } -// TODO: remove duplicate -function getConsequentComponents(consequent) { - return consequent.split('and').map((i) => i.trim()); -} - // common function getVariable(variableComponents) { return variableComponents.split('is')[0].trim(); @@ -56,8 +50,6 @@ function showFeedbackMessage(message, type) { /* exported errorHandler */ function errorHandler(response, callBack, errorCallBack) { if (!isEmpty(response.error)) { - // TODO: add l10n - // showFeedbackMessage(response.error.code + ": " + response.error.message, MessageTypesEnum.DANGER); if (!isEmpty(errorCallBack)) { errorCallBack(response.data); } @@ -237,7 +229,7 @@ function addConsequent(parentElement, projectId, variableVal, termVal) { } function addAntecedentFromRule(parentElement, projectId, ruleContent) { - let antecedentComponents = getAntecedentComponents(getAntecedent(ruleContent)); + let antecedentComponents = getRuleComponents(getAntecedent(ruleContent)); for (let i = 0; i < antecedentComponents.length; i++) { let a = antecedentComponents[i]; addAntecedent(parentElement, projectId, getVariable(a), getVariableValue(a)); @@ -245,7 +237,7 @@ function addAntecedentFromRule(parentElement, projectId, ruleContent) { } function addConsequentFromRule(parentElement, projectId, ruleContent) { - let consequentComponents = getConsequentComponents(getConsequent(ruleContent)); + let consequentComponents = getRuleComponents(getConsequent(ruleContent)); for (let i = 0; i < consequentComponents.length; i++) { let c = consequentComponents[i]; addConsequent(parentElement, projectId, getVariable(c), getVariableValue(c)); diff --git a/src/main/resources/templates/project/edit.html b/src/main/resources/templates/project/edit.html index 9ab2669..209e768 100644 --- a/src/main/resources/templates/project/edit.html +++ b/src/main/resources/templates/project/edit.html @@ -61,8 +61,8 @@ - + + diff --git a/src/main/resources/templates/project/edit.html b/src/main/resources/templates/project/edit.html index 209e768..ec19560 100644 --- a/src/main/resources/templates/project/edit.html +++ b/src/main/resources/templates/project/edit.html @@ -35,7 +35,13 @@ @@ -48,7 +54,7 @@ @@ -70,9 +76,9 @@ } else { ruleHtml += "
"; } - ruleHtml += "
"+getVariable(a)+"
"; + ruleHtml += "
" + getVariable(a) + "
"; ruleHtml += "
есть
"; - ruleHtml += "
"+getVariableValue(a)+"
"; + ruleHtml += "
" + getVariableValue(a) + "
"; } ruleHtml += "
То
" for (let i = 0; i < consequentComponents.length; i++) { @@ -87,11 +93,12 @@ ruleHtml += "
" + getVariableValue(c) + "
"; } $(el).html(ruleHtml); + $(el).removeClass('d-none'); } - $('.rule').each(function(index) { + + $('.rule').each(function (index) { addRule(index, $(this), $(this).text()); }); - diff --git a/src/main/resources/templates/project/list.html b/src/main/resources/templates/project/list.html index e5f6165..32d1359 100644 --- a/src/main/resources/templates/project/list.html +++ b/src/main/resources/templates/project/list.html @@ -7,16 +7,19 @@ diff --git a/src/main/resources/templates/project/listRules.html b/src/main/resources/templates/project/listRules.html deleted file mode 100644 index 04a8a7f..0000000 --- a/src/main/resources/templates/project/listRules.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - Список правил - - -
-

Список правил

-
-
- 1. Если -
-
- Переменная -
-
- есть -
-
- значение -
-
- И / ИЛИ -
-
-
- - \ No newline at end of file diff --git a/src/main/resources/templates/variable/edit.html b/src/main/resources/templates/variable/edit.html index 2f9979d..cad10ef 100644 --- a/src/main/resources/templates/variable/edit.html +++ b/src/main/resources/templates/variable/edit.html @@ -39,8 +39,10 @@ -- 2.34.1 From efd0cb66c732d086696f313e9adcb7889d76e6dd Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 3 Mar 2025 20:15:30 +0400 Subject: [PATCH 4/7] #2 -- Add async-await --- .../service/ProjectVariableService.java | 4 +- .../rule/controller/FuzzyRuleController.java | 16 +++ .../rule/repository/VariableRepository.java | 4 +- .../fc/rule/service/VariableService.java | 14 +++ src/main/resources/public/js/fuzzyRule.js | 113 ++++++++++-------- src/main/resources/templates/rule/edit.html | 12 +- 6 files changed, 100 insertions(+), 63 deletions(-) diff --git a/src/main/java/ru/ulstu/fc/project/service/ProjectVariableService.java b/src/main/java/ru/ulstu/fc/project/service/ProjectVariableService.java index 1ad8658..ad8b281 100644 --- a/src/main/java/ru/ulstu/fc/project/service/ProjectVariableService.java +++ b/src/main/java/ru/ulstu/fc/project/service/ProjectVariableService.java @@ -29,13 +29,13 @@ public class ProjectVariableService { if (projectId == null || projectId == 0) { return Collections.emptyList(); } - return variableRepository.findInputByProject(projectService.getById(projectId)); + return variableRepository.getInputByProject(projectService.getById(projectId)); } public List getOutputByProjectId(Integer projectId) { if (projectId == null || projectId == 0) { return Collections.emptyList(); } - return variableRepository.findOutputByProject(projectService.getById(projectId)); + return variableRepository.getOutputByProject(projectService.getById(projectId)); } } diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java index 4ae9a30..e13f1c6 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java @@ -71,6 +71,22 @@ public class FuzzyRuleController { return variableService.getAllDtoByProject(projectId); } + @ResponseBody + @GetMapping("/getInputVariables/{projectId}") + public List getInputVariables(@PathVariable("projectId") Integer projectId, + final HttpServletResponse response) { + response.addHeader("Cache-Control", "max-age=60, must-revalidate, no-transform"); + return variableService.getInputVariablesDtoByProject(projectId); + } + + @ResponseBody + @GetMapping("/getOutputVariables/{projectId}") + public List getOutputVariables(@PathVariable("projectId") Integer projectId, + final HttpServletResponse response) { + response.addHeader("Cache-Control", "max-age=60, must-revalidate, no-transform"); + return variableService.getOutputVariablesDtoByProject(projectId); + } + @ResponseBody @GetMapping("/getFuzzyTerms/{variableId}") public List getTerms(@PathVariable("variableId") Integer variableId, 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 ae56a66..881ce65 100644 --- a/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java +++ b/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java @@ -15,8 +15,8 @@ public interface VariableRepository extends JpaRepository { List getByProject(Project project); @Query("SELECT v FROM Variable v WHERE v.project = :project AND v.input = true") - List findInputByProject(@Param("project") Project project); + List getInputByProject(@Param("project") Project project); @Query("SELECT v FROM Variable v WHERE v.project = :project AND v.input = false") - List findOutputByProject(@Param("project") Project project); + List getOutputByProject(@Param("project") Project project); } 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 44e94c3..93e3f94 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java @@ -80,4 +80,18 @@ public class VariableService { public void checkIsCurrentUserVariableWithThrow(Variable variable) { projectService.checkIsCurrentUserProjectWithThrow(variable.getProject()); } + + public List getInputVariablesDtoByProject(Integer projectId) { + return variableRepository.getInputByProject(projectService.getById(projectId)) + .stream() + .map(VariableDto::new) + .toList(); + } + + public List getOutputVariablesDtoByProject(Integer projectId) { + return variableRepository.getOutputByProject(projectService.getById(projectId)) + .stream() + .map(VariableDto::new) + .toList(); + } } diff --git a/src/main/resources/public/js/fuzzyRule.js b/src/main/resources/public/js/fuzzyRule.js index 9838bd7..da67636 100644 --- a/src/main/resources/public/js/fuzzyRule.js +++ b/src/main/resources/public/js/fuzzyRule.js @@ -62,15 +62,12 @@ function errorHandler(response, callBack, errorCallBack) { } /* exported getFromRest */ -function getFromRest(url, callBack) { - $.ajax({ +async function getFromRest(url) { + return await $.ajax({ url: url, method: 'get', cache: true, - dataType: 'json', - success: function (response) { - errorHandler(response, callBack); - } + dataType: 'json' }); } @@ -112,60 +109,63 @@ function fillSelect(selectElement, values, selectedVal) { $(selectElement).append($("