diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleParseRestController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleParseRestController.java new file mode 100644 index 0000000..e2518c1 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleParseRestController.java @@ -0,0 +1,20 @@ +package ru.ulstu.fc.rule.controller; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; +import ru.ulstu.fc.rule.service.FuzzyRuleParseService; + +@RestController("fuzzyRuleParseRest") +public class FuzzyRuleParseRestController { + private final FuzzyRuleParseService fuzzyRuleParseService; + + public FuzzyRuleParseRestController(FuzzyRuleParseService fuzzyRuleParseService) { + this.fuzzyRuleParseService = fuzzyRuleParseService; + } + + @PostMapping("parse/{projectId}") + public void parseRule(@PathVariable("projectId") Integer projectId, String data) { + fuzzyRuleParseService.parseFuzzyRules(data, projectId); + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/model/FuzzyRule.java b/src/main/java/ru/ulstu/fc/rule/model/FuzzyRule.java index 04b07ec..261c51c 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/FuzzyRule.java +++ b/src/main/java/ru/ulstu/fc/rule/model/FuzzyRule.java @@ -18,6 +18,11 @@ public class FuzzyRule extends BaseEntity { public FuzzyRule() { } + public FuzzyRule(String content, Project project) { + this.content = content; + this.project = project; + } + public Project getProject() { return project; } 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 5209a70..065f87b 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleParseService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleParseService.java @@ -3,7 +3,11 @@ package ru.ulstu.fc.rule.service; import com.fuzzylite.Engine; import com.fuzzylite.rule.Rule; 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.Variable; +import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -20,9 +24,15 @@ public class FuzzyRuleParseService { private final static String ENG_IS_STATEMENT = "is"; private final Engine fuzzyEngine; + private final VariableService variableService; + private final ProjectService projectService; - public FuzzyRuleParseService(Engine fuzzyEngine) { + public FuzzyRuleParseService(Engine fuzzyEngine, + VariableService variableService, + ProjectService projectService) { this.fuzzyEngine = fuzzyEngine; + this.variableService = variableService; + this.projectService = projectService; } public List parseRules(List stringRules) { @@ -41,4 +51,33 @@ public class FuzzyRuleParseService { .replaceFirst(RU_EQ_STATEMENT, ENG_IS_STATEMENT) .replaceAll(RU_THEN_STATEMENT, ENG_THEN_STATEMENT); } + + public List parseFuzzyRules(String stringRules, Integer projectId) { + return Arrays.stream(stringRules.split("\n")).toList() + .stream() + .map(s -> parseFuzzyRule(replaceEnglishKeywords(s), projectId)) + .collect(Collectors.toList()); + } + + private FuzzyRule parseFuzzyRule(String stringRule, Integer projectId) { + String[] antecedentComponents = getAntecedentComponents(getAntecedent(stringRule)); + getVariables(antecedentComponents) + .stream() + .map(v -> variableService.save(v, projectId)); + return new FuzzyRule(stringRule, projectService.getById(projectId)); + } + + private String getAntecedent(String stringRule) { + return stringRule.split(ENG_IF_STATEMENT)[1].trim().split(ENG_THEN_STATEMENT)[0].trim(); + } + + private String[] getAntecedentComponents(String stringAntecedent) { + return stringAntecedent.split(ENG_AND_STATEMENT); + } + + private List getVariables(String[] stringAntecedentComponents) { + return Arrays.stream(stringAntecedentComponents) + .map(a -> new Variable(a.split(ENG_IS_STATEMENT)[0].trim())) + .toList(); + } } 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 5fca4eb..0fcf6fd 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java @@ -36,6 +36,19 @@ public class VariableService { return variableRepository.save(variable); } + public Variable save(Variable variable, Integer projectId) { + Variable dbVariable; + if (variable.getId() == null || variable.getId() == 0) { + dbVariable = new Variable(); + } else { + dbVariable = getById(variable.getId()); + } + dbVariable.setProject(projectService.getById(projectId)); + dbVariable.setName(variable.getName()); + dbVariable.setInput(variable.isInput()); + return variableRepository.save(variable); + } + public void delete(VariableForm ruleForm) { getById(ruleForm.getId()); variableRepository.deleteById(ruleForm.getId());