17-parse-rules-rest #18
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -18,6 +18,11 @@ public class FuzzyRule extends BaseEntity {
|
|||||||
public FuzzyRule() {
|
public FuzzyRule() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FuzzyRule(String content, Project project) {
|
||||||
|
this.content = content;
|
||||||
|
this.project = project;
|
||||||
|
}
|
||||||
|
|
||||||
public Project getProject() {
|
public Project getProject() {
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,11 @@ 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 org.springframework.stereotype.Service;
|
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.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -20,9 +24,15 @@ public class FuzzyRuleParseService {
|
|||||||
private final static String ENG_IS_STATEMENT = "is";
|
private final static String ENG_IS_STATEMENT = "is";
|
||||||
|
|
||||||
private final Engine fuzzyEngine;
|
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.fuzzyEngine = fuzzyEngine;
|
||||||
|
this.variableService = variableService;
|
||||||
|
this.projectService = projectService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Rule> parseRules(List<String> stringRules) {
|
public List<Rule> parseRules(List<String> stringRules) {
|
||||||
@ -41,4 +51,33 @@ public class FuzzyRuleParseService {
|
|||||||
.replaceFirst(RU_EQ_STATEMENT, ENG_IS_STATEMENT)
|
.replaceFirst(RU_EQ_STATEMENT, ENG_IS_STATEMENT)
|
||||||
.replaceAll(RU_THEN_STATEMENT, ENG_THEN_STATEMENT);
|
.replaceAll(RU_THEN_STATEMENT, ENG_THEN_STATEMENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<FuzzyRule> 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<Variable> getVariables(String[] stringAntecedentComponents) {
|
||||||
|
return Arrays.stream(stringAntecedentComponents)
|
||||||
|
.map(a -> new Variable(a.split(ENG_IS_STATEMENT)[0].trim()))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,19 @@ public class VariableService {
|
|||||||
return variableRepository.save(variable);
|
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) {
|
public void delete(VariableForm ruleForm) {
|
||||||
getById(ruleForm.getId());
|
getById(ruleForm.getId());
|
||||||
variableRepository.deleteById(ruleForm.getId());
|
variableRepository.deleteById(ruleForm.getId());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user