#21 -- Fix rules generation
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m3s
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m3s
This commit is contained in:
parent
17759d3d83
commit
017ecf7cd5
@ -27,6 +27,8 @@ public class FuzzyRuleParseService {
|
||||
private final static String RU_IS_STATEMENT = "является";
|
||||
private final static String RU_EQ_STATEMENT = "равно";
|
||||
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 VariableService variableService;
|
||||
@ -98,35 +100,54 @@ public class FuzzyRuleParseService {
|
||||
variableService.clearVariables(projectId);
|
||||
fuzzyRuleService.clearRules(projectId);
|
||||
|
||||
final String variableName = "fuzzyLevel";
|
||||
Variable variable = new Variable();
|
||||
variable.setName(variableName);
|
||||
variable = variableService.save(variable, projectId);
|
||||
|
||||
List<String> 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;
|
||||
createVariables(fuzzyRuleDataDto, projectId);
|
||||
|
||||
int ruleStartPos = 0;
|
||||
List<String> terms = Arrays.stream(fuzzyRuleDataDto.getFuzzyTerms()).toList();
|
||||
while (current + fuzzyRuleDataDto.getWindow() < terms.size() - 1) {
|
||||
while (ruleStartPos + fuzzyRuleDataDto.getWindow() < terms.size()) {
|
||||
StringBuilder stringRule = new StringBuilder("if ");
|
||||
int start = current;
|
||||
for (int i = start; i < fuzzyRuleDataDto.getWindow(); i++) {
|
||||
if (i > start) {
|
||||
int antecedentStartPos = ruleStartPos;
|
||||
int variableNum = 0;
|
||||
int i = antecedentStartPos;
|
||||
while (i < fuzzyRuleDataDto.getWindow() + antecedentStartPos) {
|
||||
if (i > antecedentStartPos) {
|
||||
stringRule.append(" and ");
|
||||
}
|
||||
stringRule.append(variableName + " is ").append(terms.get(i));
|
||||
stringRule
|
||||
.append(INPUT_VARIABLE_TEMPLATE_NAME)
|
||||
.append(variableNum)
|
||||
.append(" is ")
|
||||
.append(terms.get(i));
|
||||
variableNum++;
|
||||
i++;
|
||||
}
|
||||
stringRule
|
||||
.append(" then ").append(variableName).append(" is ")
|
||||
.append(terms.get(current + fuzzyRuleDataDto.getWindow()));
|
||||
current++;
|
||||
.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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user