Merge pull request '17-parse-rules-rest' (#18) from 17-parse-rules-rest into master
Some checks failed
CI fuzzy controller / container-test-job (push) Failing after 39s

Reviewed-on: #18
This commit is contained in:
romanov73 2025-02-25 18:25:21 +04:00
commit 71a7adc5fd
11 changed files with 231 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -0,0 +1,40 @@
package ru.ulstu.fc.rule.controller;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.fc.rule.model.FuzzyRule;
import ru.ulstu.fc.rule.model.FuzzyRuleForm;
import ru.ulstu.fc.rule.service.FuzzyRuleService;
@RestController
@RequestMapping("fuzzyRuleRest")
public class FuzzyRuleRestController {
private final FuzzyRuleService ruleService;
public FuzzyRuleRestController(FuzzyRuleService ruleService) {
this.ruleService = ruleService;
}
@GetMapping("/get/{projectId}/{ruleId}")
public FuzzyRule get(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "ruleId") Integer id) {
return ruleService.getById(id);
}
@PostMapping
public FuzzyRule save(@Valid FuzzyRuleForm fuzzyRuleForm) {
return ruleService.save(fuzzyRuleForm);
}
@DeleteMapping
public void delete(@Valid FuzzyRuleForm fuzzyRuleForm) {
if (fuzzyRuleForm != null && fuzzyRuleForm.getId() != null) {
ruleService.delete(fuzzyRuleForm);
}
}
}

View File

@ -0,0 +1,49 @@
package ru.ulstu.fc.rule.controller;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.fc.rule.model.FuzzyTerm;
import ru.ulstu.fc.rule.model.FuzzyTermForm;
import ru.ulstu.fc.rule.service.FuzzyTermService;
import java.util.List;
@RestController
@RequestMapping("fuzzyTermRest")
public class FuzzyTermRestController {
private final FuzzyTermService fuzzyTermService;
public FuzzyTermRestController(FuzzyTermService fuzzyTermService) {
this.fuzzyTermService = fuzzyTermService;
}
@GetMapping("/list/{projectId}/{variableId}")
public List<FuzzyTerm> getList(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "variableId") Integer variableId) {
return fuzzyTermService.getAll(projectId, variableId);
}
@GetMapping("/get/{projectId}/{variableId}/{fuzzyTermId}")
public FuzzyTerm getById(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "variableId") Integer variableId,
@PathVariable(value = "fuzzyTermId") Integer fuzzyTermId) {
return fuzzyTermService.getById(fuzzyTermId);
}
@PostMapping
public FuzzyTerm save(@Valid FuzzyTermForm fuzzyTermForm) {
return fuzzyTermService.save(fuzzyTermForm);
}
@DeleteMapping
public void delete(@Valid FuzzyTermForm fuzzyTermForm) {
if (fuzzyTermForm != null && fuzzyTermForm.getId() != null) {
fuzzyTermService.delete(fuzzyTermForm);
}
}
}

View File

@ -0,0 +1,47 @@
package ru.ulstu.fc.rule.controller;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.fc.rule.model.Variable;
import ru.ulstu.fc.rule.model.VariableForm;
import ru.ulstu.fc.rule.service.VariableService;
import java.util.List;
@RestController
@RequestMapping("variableRest")
public class VariableRestController {
private final VariableService variableService;
public VariableRestController(VariableService variableService) {
this.variableService = variableService;
}
@GetMapping("/list/{projectId}")
public List<Variable> getList(@PathVariable(value = "projectId") Integer projectId) {
return variableService.getAllByProject(projectId);
}
@GetMapping("/get/{projectId}/{variableId}")
public Variable getById(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "variableId") Integer variableId) {
return variableService.getById(variableId);
}
@PostMapping
public Variable save(@Valid VariableForm variableForm) {
return variableService.save(variableForm);
}
@DeleteMapping
public void delete(@Valid VariableForm variableForm) {
if (variableForm != null && variableForm.getId() != null) {
variableService.delete(variableForm);
}
}
}

View File

@ -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;
}

View File

@ -1,7 +1,11 @@
package ru.ulstu.fc.rule.model;
import jakarta.validation.constraints.NotNull;
public class FuzzyTermForm {
@NotNull
private Integer projectId;
@NotNull
private Integer variableId;
private Integer id;
private String description;

View File

@ -9,4 +9,6 @@ import java.util.List;
public interface VariableRepository extends JpaRepository<Variable, Integer> {
List<Variable> findByProject(Project project);
List<Variable> getByProject(Project project);
}

View File

@ -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<Rule> parseRules(List<String> stringRules) {
@ -41,4 +51,33 @@ public class FuzzyRuleParseService {
.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();
}
}

View File

@ -22,7 +22,7 @@ public class FuzzyRuleService {
.orElseThrow(() -> new RuntimeException("Rule not found by id"));
}
public Object save(FuzzyRuleForm ruleForm) {
public FuzzyRule save(FuzzyRuleForm ruleForm) {
FuzzyRule rule;
if (ruleForm.getId() == null || ruleForm.getId() == 0) {
rule = new FuzzyRule();

View File

@ -53,4 +53,8 @@ public class FuzzyTermService {
}
return variableService.getById(variableId).getFuzzyTerms();
}
public List<FuzzyTerm> getAll(Integer projectId, Integer variableId) {
return getByVariableId(variableId);
}
}

View File

@ -7,6 +7,8 @@ import ru.ulstu.fc.rule.model.Variable;
import ru.ulstu.fc.rule.model.VariableForm;
import ru.ulstu.fc.rule.repository.VariableRepository;
import java.util.List;
@Service
public class VariableService {
private final VariableRepository variableRepository;
@ -36,6 +38,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());
@ -46,4 +61,8 @@ public class VariableService {
var.getFuzzyTerms().add(ft);
variableRepository.save(var);
}
public List<Variable> getAllByProject(Integer projectId) {
return variableRepository.getByProject(projectService.getById(projectId));
}
}