Compare commits
No commits in common. "38642e593e7c5e56f678d4fd1ef7e1950a6a0ab6" and "17240b8fd27ada05b13717b9cb328dda77f9aca0" have entirely different histories.
38642e593e
...
17240b8fd2
@ -9,7 +9,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import ru.ulstu.fc.project.service.ProjectRulesService;
|
import ru.ulstu.fc.project.service.ProjectRulesService;
|
||||||
import ru.ulstu.fc.rule.model.FuzzyRule;
|
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.model.dto.FuzzyRuleDto;
|
||||||
import ru.ulstu.fc.rule.service.FuzzyRuleParseService;
|
import ru.ulstu.fc.rule.service.FuzzyRuleParseService;
|
||||||
import ru.ulstu.fc.rule.service.FuzzyRuleService;
|
import ru.ulstu.fc.rule.service.FuzzyRuleService;
|
||||||
@ -54,12 +53,7 @@ public class FuzzyRuleRestController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("parse/{projectId}")
|
@PostMapping("parse/{projectId}")
|
||||||
public List<FuzzyRule> parseRule(@PathVariable("projectId") Integer projectId, String data) {
|
public void parseRule(@PathVariable("projectId") Integer projectId, String data) {
|
||||||
return fuzzyRuleParseService.parseFuzzyRules(data, projectId);
|
fuzzyRuleParseService.parseFuzzyRules(data, projectId);
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("generate-rules/{projectId}")
|
|
||||||
public void generateRules(@PathVariable("projectId") Integer projectId, FuzzyRuleDataDto fuzzyRuleDataDto) {
|
|
||||||
fuzzyRuleParseService.generateRules(projectId, fuzzyRuleDataDto);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,11 +20,9 @@ public class FuzzyTermForm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public FuzzyTermForm(Integer id, Integer projectId, Integer variableId) {
|
public FuzzyTermForm(Integer id, Integer projectId, Integer variableId) {
|
||||||
this(id, projectId, variableId, null);
|
this.projectId = projectId;
|
||||||
}
|
this.variableId = variableId;
|
||||||
|
this.id = id;
|
||||||
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) {
|
public FuzzyTermForm(Integer id, Integer projectId, Integer variableId, FuzzyTerm fuzzyTerm) {
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -20,11 +20,6 @@ public class FuzzyRuleDto {
|
|||||||
this.projectId = projectId;
|
this.projectId = projectId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FuzzyRuleDto(Integer projectId, String content) {
|
|
||||||
this.projectId = projectId;
|
|
||||||
this.content = content;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FuzzyRuleDto(FuzzyRule fuzzyRule) {
|
public FuzzyRuleDto(FuzzyRule fuzzyRule) {
|
||||||
this.id = fuzzyRule.getId();
|
this.id = fuzzyRule.getId();
|
||||||
this.projectId = fuzzyRule.getProject().getId();
|
this.projectId = fuzzyRule.getProject().getId();
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package ru.ulstu.fc.rule.repository;
|
package ru.ulstu.fc.rule.repository;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
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.project.model.Project;
|
||||||
import ru.ulstu.fc.rule.model.FuzzyRule;
|
import ru.ulstu.fc.rule.model.FuzzyRule;
|
||||||
|
|
||||||
@ -12,8 +9,4 @@ import java.util.List;
|
|||||||
public interface FuzzyRuleRepository extends JpaRepository<FuzzyRule, Integer> {
|
public interface FuzzyRuleRepository extends JpaRepository<FuzzyRule, Integer> {
|
||||||
|
|
||||||
List<FuzzyRule> findByProject(Project project);
|
List<FuzzyRule> findByProject(Project project);
|
||||||
|
|
||||||
@Query("DELETE FROM FuzzyRule fr WHERE fr.project.id = :projectId")
|
|
||||||
@Modifying
|
|
||||||
void deleteAllByProjectId(@Param("projectId") Integer projectId);
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package ru.ulstu.fc.rule.repository;
|
package ru.ulstu.fc.rule.repository;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
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.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
import ru.ulstu.fc.rule.model.FuzzyTerm;
|
import ru.ulstu.fc.rule.model.FuzzyTerm;
|
||||||
@ -11,8 +10,4 @@ public interface FuzzyTermRepository extends JpaRepository<FuzzyTerm, Integer> {
|
|||||||
|
|
||||||
@Query("SELECT v FROM Variable v LEFT JOIN v.fuzzyTerms ft WHERE ft = :fuzzyTerm")
|
@Query("SELECT v FROM Variable v LEFT JOIN v.fuzzyTerms ft WHERE ft = :fuzzyTerm")
|
||||||
Variable findByFuzzyTerm(@Param("fuzzyTerm") FuzzyTerm 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);
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package ru.ulstu.fc.rule.repository;
|
package ru.ulstu.fc.rule.repository;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
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.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
import ru.ulstu.fc.project.model.Project;
|
import ru.ulstu.fc.project.model.Project;
|
||||||
@ -20,8 +19,4 @@ public interface VariableRepository extends JpaRepository<Variable, Integer> {
|
|||||||
|
|
||||||
@Query("SELECT v FROM Variable v WHERE v.project = :project AND v.input = false")
|
@Query("SELECT v FROM Variable v WHERE v.project = :project AND v.input = false")
|
||||||
List<Variable> getOutputByProject(@Param("project") Project project);
|
List<Variable> getOutputByProject(@Param("project") Project project);
|
||||||
|
|
||||||
@Query("DELETE FROM Variable v WHERE v.project.id = :projectId")
|
|
||||||
@Modifying
|
|
||||||
void deleteAllByProjectId(@Param("projectId") Integer projectId);
|
|
||||||
}
|
}
|
||||||
|
@ -2,17 +2,12 @@ package ru.ulstu.fc.rule.service;
|
|||||||
|
|
||||||
import com.fuzzylite.Engine;
|
import com.fuzzylite.Engine;
|
||||||
import com.fuzzylite.rule.Rule;
|
import com.fuzzylite.rule.Rule;
|
||||||
import jakarta.transaction.Transactional;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ru.ulstu.fc.project.service.ProjectService;
|
import ru.ulstu.fc.project.service.ProjectService;
|
||||||
import ru.ulstu.fc.rule.model.FuzzyRule;
|
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.Variable;
|
||||||
import ru.ulstu.fc.rule.model.dto.FuzzyRuleDataDto;
|
|
||||||
import ru.ulstu.fc.rule.model.dto.FuzzyRuleDto;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -27,25 +22,17 @@ public class FuzzyRuleParseService {
|
|||||||
private final static String RU_IS_STATEMENT = "является";
|
private final static String RU_IS_STATEMENT = "является";
|
||||||
private final static String RU_EQ_STATEMENT = "равно";
|
private final static String RU_EQ_STATEMENT = "равно";
|
||||||
private final static String ENG_IS_STATEMENT = "is";
|
private final static String ENG_IS_STATEMENT = "is";
|
||||||
private final static String INPUT_VARIABLE_TEMPLATE_NAME = "fuzzyLevel";
|
|
||||||
private final static String OUTPUT_VARIABLE_TEMPLATE_NAME = "outputFuzzyLevel";
|
|
||||||
|
|
||||||
private final Engine fuzzyEngine;
|
private final Engine fuzzyEngine;
|
||||||
private final VariableService variableService;
|
private final VariableService variableService;
|
||||||
private final ProjectService projectService;
|
private final ProjectService projectService;
|
||||||
private final FuzzyTermService fuzzyTermService;
|
|
||||||
private final FuzzyRuleService fuzzyRuleService;
|
|
||||||
|
|
||||||
public FuzzyRuleParseService(Engine fuzzyEngine,
|
public FuzzyRuleParseService(Engine fuzzyEngine,
|
||||||
VariableService variableService,
|
VariableService variableService,
|
||||||
ProjectService projectService,
|
ProjectService projectService) {
|
||||||
FuzzyTermService fuzzyTermService,
|
|
||||||
FuzzyRuleService fuzzyRuleService) {
|
|
||||||
this.fuzzyEngine = fuzzyEngine;
|
this.fuzzyEngine = fuzzyEngine;
|
||||||
this.variableService = variableService;
|
this.variableService = variableService;
|
||||||
this.projectService = projectService;
|
this.projectService = projectService;
|
||||||
this.fuzzyTermService = fuzzyTermService;
|
|
||||||
this.fuzzyRuleService = fuzzyRuleService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Rule> parseRules(List<String> stringRules) {
|
public List<Rule> parseRules(List<String> stringRules) {
|
||||||
@ -93,61 +80,4 @@ public class FuzzyRuleParseService {
|
|||||||
.map(a -> new Variable(a.split(ENG_IS_STATEMENT)[0].trim()))
|
.map(a -> new Variable(a.split(ENG_IS_STATEMENT)[0].trim()))
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public void generateRules(Integer projectId, FuzzyRuleDataDto fuzzyRuleDataDto) {
|
|
||||||
fuzzyTermService.clearTerms(projectId);
|
|
||||||
variableService.clearVariables(projectId);
|
|
||||||
fuzzyRuleService.clearRules(projectId);
|
|
||||||
|
|
||||||
createVariables(fuzzyRuleDataDto, projectId);
|
|
||||||
|
|
||||||
int ruleStartPos = 0;
|
|
||||||
List<String> terms = Arrays.stream(fuzzyRuleDataDto.getFuzzyTerms()).toList();
|
|
||||||
while (ruleStartPos + fuzzyRuleDataDto.getWindow() < terms.size()) {
|
|
||||||
StringBuilder stringRule = new StringBuilder("if ");
|
|
||||||
int antecedentStartPos = ruleStartPos;
|
|
||||||
int variableNum = 0;
|
|
||||||
int i = antecedentStartPos;
|
|
||||||
while (i < fuzzyRuleDataDto.getWindow() + antecedentStartPos) {
|
|
||||||
if (i > antecedentStartPos) {
|
|
||||||
stringRule.append(" and ");
|
|
||||||
}
|
|
||||||
stringRule
|
|
||||||
.append(INPUT_VARIABLE_TEMPLATE_NAME)
|
|
||||||
.append(variableNum)
|
|
||||||
.append(" is ")
|
|
||||||
.append(terms.get(i));
|
|
||||||
variableNum++;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
stringRule
|
|
||||||
.append(" then ").append(OUTPUT_VARIABLE_TEMPLATE_NAME)
|
|
||||||
.append(" is ")
|
|
||||||
.append(terms.get(ruleStartPos + fuzzyRuleDataDto.getWindow()));
|
|
||||||
ruleStartPos++;
|
|
||||||
FuzzyRuleDto fuzzyRule = new FuzzyRuleDto(projectId, stringRule.toString());
|
|
||||||
fuzzyRuleService.save(fuzzyRule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createVariables(FuzzyRuleDataDto fuzzyRuleDataDto, Integer projectId) {
|
|
||||||
List<String> uniqueTerms = new HashSet<>(Arrays.stream(fuzzyRuleDataDto.getFuzzyTerms()).toList()).stream().toList();
|
|
||||||
|
|
||||||
for (int i = 0; i < fuzzyRuleDataDto.getWindow(); i++) {
|
|
||||||
createVariable(INPUT_VARIABLE_TEMPLATE_NAME + i, true, projectId, uniqueTerms);
|
|
||||||
}
|
|
||||||
|
|
||||||
createVariable(OUTPUT_VARIABLE_TEMPLATE_NAME, false, projectId, uniqueTerms);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createVariable(String variableName, boolean isInput, Integer projectId, List<String> uniqueTerms) {
|
|
||||||
Variable variable = new Variable();
|
|
||||||
variable.setName(variableName);
|
|
||||||
variable.setInput(isInput);
|
|
||||||
variable = variableService.save(variable, projectId);
|
|
||||||
for (int j = 0; j < uniqueTerms.size(); j++) {
|
|
||||||
fuzzyTermService.save(new FuzzyTermForm(projectId, variable.getId(), uniqueTerms.get(j), Double.valueOf(j)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -10,13 +10,11 @@ import ru.ulstu.fc.rule.repository.FuzzyRuleRepository;
|
|||||||
public class FuzzyRuleService {
|
public class FuzzyRuleService {
|
||||||
private final FuzzyRuleRepository ruleRepository;
|
private final FuzzyRuleRepository ruleRepository;
|
||||||
private final ProjectService projectService;
|
private final ProjectService projectService;
|
||||||
private final FuzzyRuleRepository fuzzyRuleRepository;
|
|
||||||
|
|
||||||
public FuzzyRuleService(FuzzyRuleRepository ruleRepository,
|
public FuzzyRuleService(FuzzyRuleRepository ruleRepository,
|
||||||
ProjectService projectService, FuzzyRuleRepository fuzzyRuleRepository) {
|
ProjectService projectService) {
|
||||||
this.ruleRepository = ruleRepository;
|
this.ruleRepository = ruleRepository;
|
||||||
this.projectService = projectService;
|
this.projectService = projectService;
|
||||||
this.fuzzyRuleRepository = fuzzyRuleRepository;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public FuzzyRule getById(Integer id) {
|
public FuzzyRule getById(Integer id) {
|
||||||
@ -31,15 +29,15 @@ public class FuzzyRuleService {
|
|||||||
return new FuzzyRuleDto(getById(id));
|
return new FuzzyRuleDto(getById(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
public FuzzyRule save(FuzzyRuleDto fuzzyRuleDto) {
|
public FuzzyRule save(FuzzyRuleDto ruleForm) {
|
||||||
FuzzyRule rule;
|
FuzzyRule rule;
|
||||||
if (fuzzyRuleDto.getId() == null || fuzzyRuleDto.getId() == 0) {
|
if (ruleForm.getId() == null || ruleForm.getId() == 0) {
|
||||||
rule = new FuzzyRule();
|
rule = new FuzzyRule();
|
||||||
} else {
|
} else {
|
||||||
rule = getById(fuzzyRuleDto.getId());
|
rule = getById(ruleForm.getId());
|
||||||
}
|
}
|
||||||
rule.setProject(projectService.getById(fuzzyRuleDto.getProjectId()));
|
rule.setProject(projectService.getById(ruleForm.getProjectId()));
|
||||||
rule.setContent(fuzzyRuleDto.getContent());
|
rule.setContent(ruleForm.getContent());
|
||||||
return ruleRepository.save(rule);
|
return ruleRepository.save(rule);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,8 +48,4 @@ public class FuzzyRuleService {
|
|||||||
public void checkIsCurrentUserFuzzyRuleWithThrow(FuzzyRule fuzzyRule) {
|
public void checkIsCurrentUserFuzzyRuleWithThrow(FuzzyRule fuzzyRule) {
|
||||||
projectService.checkIsCurrentUserProjectWithThrow(fuzzyRule.getProject());
|
projectService.checkIsCurrentUserProjectWithThrow(fuzzyRule.getProject());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearRules(Integer projectId) {
|
|
||||||
fuzzyRuleRepository.deleteAllByProjectId(projectId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -67,8 +67,4 @@ public class FuzzyTermService {
|
|||||||
public void checkIsCurrentUserFuzzyTermWithThrow(FuzzyTerm fuzzyTerm) {
|
public void checkIsCurrentUserFuzzyTermWithThrow(FuzzyTerm fuzzyTerm) {
|
||||||
projectService.checkIsCurrentUserProjectWithThrow(fuzzyTermRepository.findByFuzzyTerm(fuzzyTerm).getProject());
|
projectService.checkIsCurrentUserProjectWithThrow(fuzzyTermRepository.findByFuzzyTerm(fuzzyTerm).getProject());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearTerms(Integer projectId) {
|
|
||||||
fuzzyTermRepository.deleteAllByProjectId(projectId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -85,8 +85,4 @@ public class VariableService {
|
|||||||
.map(VariableDto::new)
|
.map(VariableDto::new)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearVariables(Integer projectId) {
|
|
||||||
variableRepository.deleteAllByProjectId(projectId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user