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 b08fa01..72bc3f6 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java @@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import ru.ulstu.fc.project.service.ProjectRulesService; import ru.ulstu.fc.rule.model.FuzzyRule; +import ru.ulstu.fc.rule.model.dto.FuzzyRuleDataDto; import ru.ulstu.fc.rule.model.dto.FuzzyRuleDto; import ru.ulstu.fc.rule.service.FuzzyRuleParseService; import ru.ulstu.fc.rule.service.FuzzyRuleService; @@ -53,7 +54,12 @@ public class FuzzyRuleRestController { } @PostMapping("parse/{projectId}") - public void parseRule(@PathVariable("projectId") Integer projectId, String data) { - fuzzyRuleParseService.parseFuzzyRules(data, projectId); + public List parseRule(@PathVariable("projectId") Integer projectId, String data) { + return fuzzyRuleParseService.parseFuzzyRules(data, projectId); + } + + @PostMapping("generate-rules/{projectId}") + public void generateRules(@PathVariable("projectId") Integer projectId, FuzzyRuleDataDto fuzzyRuleDataDto) { + fuzzyRuleParseService.generateRules(projectId, fuzzyRuleDataDto); } } diff --git a/src/main/java/ru/ulstu/fc/rule/model/FuzzyTermForm.java b/src/main/java/ru/ulstu/fc/rule/model/FuzzyTermForm.java index e660993..83f5c35 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/FuzzyTermForm.java +++ b/src/main/java/ru/ulstu/fc/rule/model/FuzzyTermForm.java @@ -20,9 +20,11 @@ public class FuzzyTermForm { } public FuzzyTermForm(Integer id, Integer projectId, Integer variableId) { - this.projectId = projectId; - this.variableId = variableId; - this.id = id; + this(id, projectId, variableId, null); + } + + public FuzzyTermForm(Integer projectId, Integer variableId, String description, Double value) { + this(null, projectId, variableId, new FuzzyTerm(description, value)); } public FuzzyTermForm(Integer id, Integer projectId, Integer variableId, FuzzyTerm fuzzyTerm) { diff --git a/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyRuleDataDto.java b/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyRuleDataDto.java new file mode 100644 index 0000000..7f48c9a --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyRuleDataDto.java @@ -0,0 +1,22 @@ +package ru.ulstu.fc.rule.model.dto; + +public class FuzzyRuleDataDto { + private String[] fuzzyTerms; + private int window = 3; + + public String[] getFuzzyTerms() { + return fuzzyTerms; + } + + public void setFuzzyTerms(String[] fuzzyTerms) { + this.fuzzyTerms = fuzzyTerms; + } + + public int getWindow() { + return window; + } + + public void setWindow(int window) { + this.window = window; + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyRuleDto.java b/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyRuleDto.java index 726c6b1..9510a41 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyRuleDto.java +++ b/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyRuleDto.java @@ -20,6 +20,11 @@ public class FuzzyRuleDto { this.projectId = projectId; } + public FuzzyRuleDto(Integer projectId, String content) { + this.projectId = projectId; + this.content = content; + } + public FuzzyRuleDto(FuzzyRule fuzzyRule) { this.id = fuzzyRule.getId(); this.projectId = fuzzyRule.getProject().getId(); diff --git a/src/main/java/ru/ulstu/fc/rule/repository/FuzzyRuleRepository.java b/src/main/java/ru/ulstu/fc/rule/repository/FuzzyRuleRepository.java index deb0a71..9edaf49 100644 --- a/src/main/java/ru/ulstu/fc/rule/repository/FuzzyRuleRepository.java +++ b/src/main/java/ru/ulstu/fc/rule/repository/FuzzyRuleRepository.java @@ -1,6 +1,9 @@ package ru.ulstu.fc.rule.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import ru.ulstu.fc.project.model.Project; import ru.ulstu.fc.rule.model.FuzzyRule; @@ -9,4 +12,8 @@ import java.util.List; public interface FuzzyRuleRepository extends JpaRepository { List findByProject(Project project); + + @Query("DELETE FROM FuzzyRule fr WHERE fr.project.id = :projectId") + @Modifying + void deleteAllByProjectId(@Param("projectId") Integer projectId); } diff --git a/src/main/java/ru/ulstu/fc/rule/repository/FuzzyTermRepository.java b/src/main/java/ru/ulstu/fc/rule/repository/FuzzyTermRepository.java index 7f5f7ff..ab45076 100644 --- a/src/main/java/ru/ulstu/fc/rule/repository/FuzzyTermRepository.java +++ b/src/main/java/ru/ulstu/fc/rule/repository/FuzzyTermRepository.java @@ -1,6 +1,7 @@ package ru.ulstu.fc.rule.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import ru.ulstu.fc.rule.model.FuzzyTerm; @@ -10,4 +11,8 @@ public interface FuzzyTermRepository extends JpaRepository { @Query("SELECT v FROM Variable v LEFT JOIN v.fuzzyTerms ft WHERE ft = :fuzzyTerm") Variable findByFuzzyTerm(@Param("fuzzyTerm") FuzzyTerm fuzzyTerm); + + @Query("DELETE FROM FuzzyTerm ft WHERE ft in (SELECT v.fuzzyTerms FROM Variable v WHERE v.project.id = :projectId)") + @Modifying + void deleteAllByProjectId(@Param("projectId") Integer projectId); } 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 881ce65..aa0ea79 100644 --- a/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java +++ b/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java @@ -1,6 +1,7 @@ package ru.ulstu.fc.rule.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import ru.ulstu.fc.project.model.Project; @@ -19,4 +20,8 @@ public interface VariableRepository extends JpaRepository { @Query("SELECT v FROM Variable v WHERE v.project = :project AND v.input = false") List getOutputByProject(@Param("project") Project project); + + @Query("DELETE FROM Variable v WHERE v.project.id = :projectId") + @Modifying + void deleteAllByProjectId(@Param("projectId") Integer projectId); } diff --git a/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleParseService.java b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleParseService.java index 065f87b..1dc0c5b 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleParseService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleParseService.java @@ -2,12 +2,17 @@ package ru.ulstu.fc.rule.service; import com.fuzzylite.Engine; import com.fuzzylite.rule.Rule; +import jakarta.transaction.Transactional; import org.springframework.stereotype.Service; import ru.ulstu.fc.project.service.ProjectService; import ru.ulstu.fc.rule.model.FuzzyRule; +import ru.ulstu.fc.rule.model.FuzzyTermForm; import ru.ulstu.fc.rule.model.Variable; +import ru.ulstu.fc.rule.model.dto.FuzzyRuleDataDto; +import ru.ulstu.fc.rule.model.dto.FuzzyRuleDto; import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; @@ -26,13 +31,19 @@ public class FuzzyRuleParseService { private final Engine fuzzyEngine; private final VariableService variableService; private final ProjectService projectService; + private final FuzzyTermService fuzzyTermService; + private final FuzzyRuleService fuzzyRuleService; public FuzzyRuleParseService(Engine fuzzyEngine, VariableService variableService, - ProjectService projectService) { + ProjectService projectService, + FuzzyTermService fuzzyTermService, + FuzzyRuleService fuzzyRuleService) { this.fuzzyEngine = fuzzyEngine; this.variableService = variableService; this.projectService = projectService; + this.fuzzyTermService = fuzzyTermService; + this.fuzzyRuleService = fuzzyRuleService; } public List parseRules(List stringRules) { @@ -80,4 +91,42 @@ public class FuzzyRuleParseService { .map(a -> new Variable(a.split(ENG_IS_STATEMENT)[0].trim())) .toList(); } + + @Transactional + public void generateRules(Integer projectId, FuzzyRuleDataDto fuzzyRuleDataDto) { + fuzzyTermService.clearTerms(projectId); + variableService.clearVariables(projectId); + fuzzyRuleService.clearRules(projectId); + + final String variableName = "fuzzyLevel"; + Variable variable = new Variable(); + variable.setName(variableName); + variable = variableService.save(variable, projectId); + + List uniqueTerms = new HashSet<>(Arrays.stream(fuzzyRuleDataDto.getFuzzyTerms()).toList()).stream().toList(); + for (int i = 0; i < uniqueTerms.size(); i++) { + fuzzyTermService.save(new FuzzyTermForm(projectId, variable.getId(), uniqueTerms.get(i), Double.valueOf(i))); + } + + //save rules + int current = 0; + + List terms = Arrays.stream(fuzzyRuleDataDto.getFuzzyTerms()).toList(); + while (current + fuzzyRuleDataDto.getWindow() < terms.size() - 1) { + StringBuilder stringRule = new StringBuilder("if "); + int start = current; + for (int i = start; i < fuzzyRuleDataDto.getWindow(); i++) { + if (i > start) { + stringRule.append(" and "); + } + stringRule.append(variableName + " is ").append(terms.get(i)); + } + stringRule + .append(" then ").append(variableName).append(" is ") + .append(terms.get(current + fuzzyRuleDataDto.getWindow())); + current++; + FuzzyRuleDto fuzzyRule = new FuzzyRuleDto(projectId, stringRule.toString()); + fuzzyRuleService.save(fuzzyRule); + } + } } diff --git a/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java index bd4b617..85c1b63 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java @@ -10,11 +10,13 @@ import ru.ulstu.fc.rule.repository.FuzzyRuleRepository; public class FuzzyRuleService { private final FuzzyRuleRepository ruleRepository; private final ProjectService projectService; + private final FuzzyRuleRepository fuzzyRuleRepository; public FuzzyRuleService(FuzzyRuleRepository ruleRepository, - ProjectService projectService) { + ProjectService projectService, FuzzyRuleRepository fuzzyRuleRepository) { this.ruleRepository = ruleRepository; this.projectService = projectService; + this.fuzzyRuleRepository = fuzzyRuleRepository; } public FuzzyRule getById(Integer id) { @@ -29,15 +31,15 @@ public class FuzzyRuleService { return new FuzzyRuleDto(getById(id)); } - public FuzzyRule save(FuzzyRuleDto ruleForm) { + public FuzzyRule save(FuzzyRuleDto fuzzyRuleDto) { FuzzyRule rule; - if (ruleForm.getId() == null || ruleForm.getId() == 0) { + if (fuzzyRuleDto.getId() == null || fuzzyRuleDto.getId() == 0) { rule = new FuzzyRule(); } else { - rule = getById(ruleForm.getId()); + rule = getById(fuzzyRuleDto.getId()); } - rule.setProject(projectService.getById(ruleForm.getProjectId())); - rule.setContent(ruleForm.getContent()); + rule.setProject(projectService.getById(fuzzyRuleDto.getProjectId())); + rule.setContent(fuzzyRuleDto.getContent()); return ruleRepository.save(rule); } @@ -48,4 +50,8 @@ public class FuzzyRuleService { public void checkIsCurrentUserFuzzyRuleWithThrow(FuzzyRule fuzzyRule) { projectService.checkIsCurrentUserProjectWithThrow(fuzzyRule.getProject()); } + + public void clearRules(Integer projectId) { + fuzzyRuleRepository.deleteAllByProjectId(projectId); + } } diff --git a/src/main/java/ru/ulstu/fc/rule/service/FuzzyTermService.java b/src/main/java/ru/ulstu/fc/rule/service/FuzzyTermService.java index 28a7594..7c87138 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyTermService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyTermService.java @@ -67,4 +67,8 @@ public class FuzzyTermService { public void checkIsCurrentUserFuzzyTermWithThrow(FuzzyTerm fuzzyTerm) { projectService.checkIsCurrentUserProjectWithThrow(fuzzyTermRepository.findByFuzzyTerm(fuzzyTerm).getProject()); } + + public void clearTerms(Integer projectId) { + fuzzyTermRepository.deleteAllByProjectId(projectId); + } } 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 dc5a357..e63718b 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java @@ -85,4 +85,8 @@ public class VariableService { .map(VariableDto::new) .toList(); } + + public void clearVariables(Integer projectId) { + variableRepository.deleteAllByProjectId(projectId); + } }