Декомпозиция #87

Merged
romanov73 merged 2 commits from 86-bl-decomposition into master 2023-02-07 16:40:38 +04:00
5 changed files with 115 additions and 83 deletions
Showing only changes of commit 4216356c35 - Show all commits

View File

@ -6,7 +6,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.extractor.assessment.model.FilterBranchForm;
import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.rule.service.FuzzyInferenceService;
import ru.ulstu.extractor.service.assessment.AssessmentService;
import springfox.documentation.annotations.ApiIgnore;
import java.util.Optional;
@ -16,12 +16,12 @@ import static ru.ulstu.extractor.core.Route.ASSESSMENTS;
@Controller
@ApiIgnore
public class AssessmentController {
private final FuzzyInferenceService fuzzyInferenceService;
private final AssessmentService assessmentService;
private final BranchService branchService;
public AssessmentController(FuzzyInferenceService fuzzyInferenceService,
public AssessmentController(AssessmentService assessmentService,
BranchService branchService) {
this.fuzzyInferenceService = fuzzyInferenceService;
this.assessmentService = assessmentService;
this.branchService = branchService;
}
@ -29,7 +29,7 @@ public class AssessmentController {
public String getAssessments(Model model, @RequestParam Optional<Integer> branchId) {
model.addAttribute("branches", branchService.findAll());
if (branchId.isPresent()) {
model.addAttribute("assessments", fuzzyInferenceService.getAssessmentsByForecastTendencies(branchId.get()));
model.addAttribute("assessments", assessmentService.getAssessmentsByForecastTendencies(branchId.get()));
model.addAttribute("filterBranchForm", new FilterBranchForm(branchId.get()));
} else {
model.addAttribute("filterBranchForm", new FilterBranchForm());

View File

@ -13,15 +13,10 @@ import com.fuzzylite.variable.InputVariable;
import com.fuzzylite.variable.OutputVariable;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.assessment.model.Assessment;
import ru.ulstu.extractor.gitrepository.service.GitRepositoryService;
import ru.ulstu.extractor.rule.model.AntecedentValue;
import ru.ulstu.extractor.rule.model.AssessmentException;
import ru.ulstu.extractor.rule.model.DbRule;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -34,22 +29,8 @@ public class FuzzyInferenceService {
+ OUTPUT_VARIABLE_NAME
+ " is %s";
private final static String NO_RESULT = "Нет результата";
private final DbRuleService ruleService;
private final AntecedentValueService antecedentValueService;
private final GitRepositoryService gitRepositoryService;
private final TimeSeriesService timeSeriesService;
public FuzzyInferenceService(DbRuleService ruleService,
AntecedentValueService antecedentValueService,
GitRepositoryService gitRepositoryService,
TimeSeriesService timeSeriesService) {
this.ruleService = ruleService;
this.antecedentValueService = antecedentValueService;
this.gitRepositoryService = gitRepositoryService;
this.timeSeriesService = timeSeriesService;
}
public List<String> getRulesFromDb(List<DbRule> dbRules, Map<String, Double> variableValues) {
private List<String> getRulesFromDb(List<DbRule> dbRules, Map<String, Double> variableValues) {
validateVariables(variableValues, dbRules);
return dbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
}
@ -114,23 +95,8 @@ public class FuzzyInferenceService {
return engine;
}
public List<Assessment> getAssessmentsByForecastTendencies(Integer branchId) {
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
List<DbRule> dbRules = ruleService.getList();
try {
return getAssessmentsByTimeSeriesTendencies(dbRules, timeSeries);
} catch (AssessmentException ex) {
return new ArrayList<>();
}
}
public List<Assessment> getAssessmentsByLastValues(Integer branchId) {
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
List<DbRule> dbRules = ruleService.getList();
return getAssessmentsByLastValues(dbRules, timeSeries);
}
private List<Assessment> getFuzzyInference(List<DbRule> dbRules, Map<String, Double> variableValues) {
public List<Assessment> getFuzzyInference(List<DbRule> dbRules, Map<String, Double> variableValues) {
Engine engine = getFuzzyEngine();
List<AntecedentValue> antecedentValues = Stream.concat(dbRules.stream().map(DbRule::getFirstAntecedentValue),
dbRules.stream().map(DbRule::getSecondAntecedentValue)).distinct().collect(Collectors.toList());
@ -147,35 +113,6 @@ public class FuzzyInferenceService {
.collect(Collectors.toList());
}
private List<Assessment> getSingleAssessmentByTimeSeriesTendencies(List<DbRule> dbRules, List<TimeSeries> timeSeries) throws AssessmentException {
Map<String, Double> variableValues = new HashMap<>();
timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(),
timeSeriesService.getLastTimeSeriesTendency(ts)
.orElseThrow(() -> new AssessmentException(""))));
return getFuzzyInference(dbRules, variableValues);
}
private List<Assessment> getAssessmentsByTimeSeriesTendencies(List<DbRule> dbRules, List<TimeSeries> timeSeries) {
return dbRules
.stream()
.flatMap(dbRule -> {
Map<String, Double> variableValues = new HashMap<>();
timeSeries
.stream()
.filter(ts -> ts.getTimeSeriesType() == dbRule.getFirstAntecedent()
|| ts.getTimeSeriesType() == dbRule.getSecondAntecedent())
.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService
.getLastTimeSeriesTendency(ts)
.orElseThrow(() -> new AssessmentException(""))));
return getFuzzyInference(List.of(dbRule), variableValues).stream();
}).collect(Collectors.toList());
}
private List<Assessment> getAssessmentsByLastValues(List<DbRule> dbRules, List<TimeSeries> timeSeries) {
Map<String, Double> variableValues = new HashMap<>();
timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), ts.getValues().get(ts.getValues().size() - 1).getValue()));
return getFuzzyInference(dbRules, variableValues);
}
private void validateVariables(Map<String, Double> variableValues, List<DbRule> dbDbRules) {
for (DbRule dbRule : dbDbRules) {

View File

@ -0,0 +1,78 @@
package ru.ulstu.extractor.service.assessment;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.assessment.model.Assessment;
import ru.ulstu.extractor.rule.model.AssessmentException;
import ru.ulstu.extractor.rule.model.DbRule;
import ru.ulstu.extractor.rule.service.DbRuleService;
import ru.ulstu.extractor.rule.service.FuzzyInferenceService;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class AssessmentService {
private final DbRuleService ruleService;
private final TimeSeriesService timeSeriesService;
private final FuzzyInferenceService fuzzyInferenceService;
public AssessmentService(DbRuleService ruleService,
TimeSeriesService timeSeriesService,
FuzzyInferenceService fuzzyInferenceService) {
this.ruleService = ruleService;
this.timeSeriesService = timeSeriesService;
this.fuzzyInferenceService = fuzzyInferenceService;
}
public List<Assessment> getAssessmentsByForecastTendencies(Integer branchId) {
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
List<DbRule> dbRules = ruleService.getList();
try {
return getAssessmentsByTimeSeriesTendencies(dbRules, timeSeries);
} catch (AssessmentException ex) {
return new ArrayList<>();
}
}
public List<Assessment> getAssessmentsByLastValues(Integer branchId) {
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
List<DbRule> dbRules = ruleService.getList();
return getAssessmentsByLastValues(dbRules, timeSeries);
}
private List<Assessment> getSingleAssessmentByTimeSeriesTendencies(List<DbRule> dbRules, List<TimeSeries> timeSeries) throws AssessmentException {
Map<String, Double> variableValues = new HashMap<>();
timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(),
timeSeriesService.getLastTimeSeriesTendency(ts)
.orElseThrow(() -> new AssessmentException(""))));
return fuzzyInferenceService.getFuzzyInference(dbRules, variableValues);
}
private List<Assessment> getAssessmentsByTimeSeriesTendencies(List<DbRule> dbRules, List<TimeSeries> timeSeries) {
return dbRules
.stream()
.flatMap(dbRule -> {
Map<String, Double> variableValues = new HashMap<>();
timeSeries
.stream()
.filter(ts -> ts.getTimeSeriesType() == dbRule.getFirstAntecedent()
|| ts.getTimeSeriesType() == dbRule.getSecondAntecedent())
.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService
.getLastTimeSeriesTendency(ts)
.orElseThrow(() -> new AssessmentException(""))));
return fuzzyInferenceService.getFuzzyInference(List.of(dbRule), variableValues).stream();
}).collect(Collectors.toList());
}
private List<Assessment> getAssessmentsByLastValues(List<DbRule> dbRules, List<TimeSeries> timeSeries) {
Map<String, Double> variableValues = new HashMap<>();
timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), ts.getValues().get(ts.getValues().size() - 1).getValue()));
return fuzzyInferenceService.getFuzzyInference(dbRules, variableValues);
}
}

View File

@ -0,0 +1,15 @@
package ru.ulstu;
import org.junit.Assert;
import org.junit.Test;
import ru.ulstu.extractor.rule.service.FuzzyInferenceService;
public class FuzzyInferenceTest {
private final FuzzyInferenceService fuzzyInferenceService = new FuzzyInferenceService();
@Test
public void test() {
Assert.assertNotNull(fuzzyInferenceService);
}
}

View File

@ -2,7 +2,9 @@ package ru.ulstu;
import org.junit.Assert;
import org.junit.Test;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.util.TimeSeriesDateMapper;
@ -17,13 +19,13 @@ public class TimeSeriesMapperTest {
c1.set(2020, 5, 1, 1, 1, 1);
Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 5, 2, 2, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый",
TimeSeries timeSeries = new TimeSeries("Тестовый", new Branch(), TimeSeriesType.COMMITS,
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
timeSeries = TimeSeriesDateMapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
Assert.assertEquals(1, timeSeries.getValues().size());
Assert.assertEquals(Integer.valueOf(20), timeSeries.getValues().get(0).getValue());
Assert.assertEquals(Double.valueOf(20), timeSeries.getValues().get(0).getValue());
}
@Test
@ -32,13 +34,13 @@ public class TimeSeriesMapperTest {
c1.set(2020, 5, 1, 1, 1, 1);
Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 5, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый",
TimeSeries timeSeries = new TimeSeries("Тестовый", new Branch(), TimeSeriesType.COMMITS,
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
Assert.assertEquals(1, timeSeries.getValues().size());
Assert.assertEquals(Integer.valueOf(20), timeSeries.getValues().get(0).getValue());
Assert.assertEquals(Double.valueOf(20), timeSeries.getValues().get(0).getValue());
}
@Test
@ -47,13 +49,13 @@ public class TimeSeriesMapperTest {
c1.set(2020, 5, 1, 1, 1, 1);
Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 5, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый",
TimeSeries timeSeries = new TimeSeries("Тестовый", new Branch(), TimeSeriesType.COMMITS,
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
Assert.assertEquals(1, timeSeries.getValues().size());
Assert.assertEquals(Integer.valueOf(20), timeSeries.getValues().get(0).getValue());
Assert.assertEquals(Double.valueOf(20), timeSeries.getValues().get(0).getValue());
}
@Test
@ -62,13 +64,13 @@ public class TimeSeriesMapperTest {
c1.set(2020, 5, 1, 1, 1, 1);
Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 6, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый",
TimeSeries timeSeries = new TimeSeries("Тестовый", new Branch(), TimeSeriesType.COMMITS,
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
Assert.assertEquals(2, timeSeries.getValues().size());
Assert.assertEquals(Integer.valueOf(10), timeSeries.getValues().get(0).getValue());
Assert.assertEquals(Double.valueOf(10), timeSeries.getValues().get(0).getValue());
}
@Test
@ -77,13 +79,13 @@ public class TimeSeriesMapperTest {
c1.set(2020, 5, 1, 1, 1, 1);
Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 5, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый",
TimeSeries timeSeries = new TimeSeries("Тестовый", new Branch(), TimeSeriesType.COMMITS,
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries);
Assert.assertEquals(1, timeSeries.getValues().size());
Assert.assertEquals(Integer.valueOf(20), timeSeries.getValues().get(0).getValue());
Assert.assertEquals(Double.valueOf(20), timeSeries.getValues().get(0).getValue());
}
@Test
@ -92,12 +94,12 @@ public class TimeSeriesMapperTest {
c1.set(2020, 5, 1, 1, 1, 1);
Calendar c2 = GregorianCalendar.getInstance();
c2.set(2021, 5, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый",
TimeSeries timeSeries = new TimeSeries("Тестовый", new Branch(), TimeSeriesType.COMMITS,
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries);
Assert.assertEquals(2, timeSeries.getValues().size());
Assert.assertEquals(Integer.valueOf(10), timeSeries.getValues().get(0).getValue());
Assert.assertEquals(Double.valueOf(10), timeSeries.getValues().get(0).getValue());
}
}