diff --git a/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java b/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java index 3e3d3cc..b960b4d 100644 --- a/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java +++ b/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java @@ -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 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()); diff --git a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java index 9d8bc86..38a6a58 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java @@ -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 getRulesFromDb(List dbRules, Map variableValues) { + private List getRulesFromDb(List dbRules, Map variableValues) { validateVariables(variableValues, dbRules); return dbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList()); } @@ -114,23 +95,8 @@ public class FuzzyInferenceService { return engine; } - public List getAssessmentsByForecastTendencies(Integer branchId) { - List timeSeries = timeSeriesService.getByBranch(branchId); - List dbRules = ruleService.getList(); - try { - return getAssessmentsByTimeSeriesTendencies(dbRules, timeSeries); - } catch (AssessmentException ex) { - return new ArrayList<>(); - } - } - public List getAssessmentsByLastValues(Integer branchId) { - List timeSeries = timeSeriesService.getByBranch(branchId); - List dbRules = ruleService.getList(); - return getAssessmentsByLastValues(dbRules, timeSeries); - } - - private List getFuzzyInference(List dbRules, Map variableValues) { + public List getFuzzyInference(List dbRules, Map variableValues) { Engine engine = getFuzzyEngine(); List 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 getSingleAssessmentByTimeSeriesTendencies(List dbRules, List timeSeries) throws AssessmentException { - Map variableValues = new HashMap<>(); - timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), - timeSeriesService.getLastTimeSeriesTendency(ts) - .orElseThrow(() -> new AssessmentException("")))); - return getFuzzyInference(dbRules, variableValues); - } - - private List getAssessmentsByTimeSeriesTendencies(List dbRules, List timeSeries) { - return dbRules - .stream() - .flatMap(dbRule -> { - Map 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 getAssessmentsByLastValues(List dbRules, List timeSeries) { - Map 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 variableValues, List dbDbRules) { for (DbRule dbRule : dbDbRules) { diff --git a/src/main/java/ru/ulstu/extractor/service/assessment/AssessmentService.java b/src/main/java/ru/ulstu/extractor/service/assessment/AssessmentService.java new file mode 100644 index 0000000..5edd2ec --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/service/assessment/AssessmentService.java @@ -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 getAssessmentsByForecastTendencies(Integer branchId) { + List timeSeries = timeSeriesService.getByBranch(branchId); + List dbRules = ruleService.getList(); + try { + return getAssessmentsByTimeSeriesTendencies(dbRules, timeSeries); + } catch (AssessmentException ex) { + return new ArrayList<>(); + } + } + + public List getAssessmentsByLastValues(Integer branchId) { + List timeSeries = timeSeriesService.getByBranch(branchId); + List dbRules = ruleService.getList(); + return getAssessmentsByLastValues(dbRules, timeSeries); + } + + private List getSingleAssessmentByTimeSeriesTendencies(List dbRules, List timeSeries) throws AssessmentException { + Map variableValues = new HashMap<>(); + timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), + timeSeriesService.getLastTimeSeriesTendency(ts) + .orElseThrow(() -> new AssessmentException("")))); + return fuzzyInferenceService.getFuzzyInference(dbRules, variableValues); + } + + private List getAssessmentsByTimeSeriesTendencies(List dbRules, List timeSeries) { + return dbRules + .stream() + .flatMap(dbRule -> { + Map 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 getAssessmentsByLastValues(List dbRules, List timeSeries) { + Map variableValues = new HashMap<>(); + timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), ts.getValues().get(ts.getValues().size() - 1).getValue())); + return fuzzyInferenceService.getFuzzyInference(dbRules, variableValues); + } + +} diff --git a/src/test/java/ru/ulstu/FuzzyInferenceTest.java b/src/test/java/ru/ulstu/FuzzyInferenceTest.java new file mode 100644 index 0000000..99d69ef --- /dev/null +++ b/src/test/java/ru/ulstu/FuzzyInferenceTest.java @@ -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); + } +} diff --git a/src/test/java/ru/ulstu/TimeSeriesMapperTest.java b/src/test/java/ru/ulstu/TimeSeriesMapperTest.java index d799a2b..9beb443 100644 --- a/src/test/java/ru/ulstu/TimeSeriesMapperTest.java +++ b/src/test/java/ru/ulstu/TimeSeriesMapperTest.java @@ -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()); } }