fuzzy-controller/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleParseService.java
Anton Romanov d8f51fefc6
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 3m0s
#17 -- Add some rule parsing
2025-02-25 15:48:57 +04:00

84 lines
3.3 KiB
Java

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;
@Service
public class FuzzyRuleParseService {
private final static String RU_IF_STATEMENT = "если";
private final static String ENG_IF_STATEMENT = "if";
private final static String RU_THEN_STATEMENT = "то";
private final static String ENG_THEN_STATEMENT = "then";
private final static String RU_AND_STATEMENT = "и";
private final static String ENG_AND_STATEMENT = "and";
private final static String RU_IS_STATEMENT = "является";
private final static String RU_EQ_STATEMENT = "равно";
private final static String ENG_IS_STATEMENT = "is";
private final Engine fuzzyEngine;
private final VariableService variableService;
private final ProjectService projectService;
public FuzzyRuleParseService(Engine fuzzyEngine,
VariableService variableService,
ProjectService projectService) {
this.fuzzyEngine = fuzzyEngine;
this.variableService = variableService;
this.projectService = projectService;
}
public List<Rule> parseRules(List<String> stringRules) {
return stringRules
.stream()
.map(s -> Rule.parse(replaceEnglishKeywords(s), fuzzyEngine))
.collect(Collectors.toList());
}
private String replaceEnglishKeywords(String stringRule) {
stringRule = stringRule.toLowerCase();
return stringRule
.replaceFirst(RU_IF_STATEMENT, ENG_IF_STATEMENT)
.replaceFirst(RU_AND_STATEMENT, ENG_AND_STATEMENT)
.replaceFirst(RU_IS_STATEMENT, ENG_IS_STATEMENT)
.replaceFirst(RU_EQ_STATEMENT, ENG_IS_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();
}
}