list of assessments #85
@ -0,0 +1,7 @@
|
||||
package ru.ulstu.extractor.rule.model;
|
||||
|
||||
public class AssessmentException extends RuntimeException {
|
||||
public AssessmentException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ 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;
|
||||
@ -48,10 +49,9 @@ public class FuzzyInferenceService {
|
||||
this.timeSeriesService = timeSeriesService;
|
||||
}
|
||||
|
||||
public List<String> getRulesFromDb(Map<String, Double> variableValues) {
|
||||
List<DbRule> dbDbRules = ruleService.getList();
|
||||
validateVariables(variableValues, dbDbRules);
|
||||
return dbDbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
|
||||
public List<String> getRulesFromDb(List<DbRule> dbRules, Map<String, Double> variableValues) {
|
||||
validateVariables(variableValues, dbRules);
|
||||
return dbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private String getFuzzyRule(DbRule dbRule) {
|
||||
@ -64,6 +64,7 @@ public class FuzzyInferenceService {
|
||||
}
|
||||
|
||||
private RuleBlock getRuleBlock(Engine engine,
|
||||
List<DbRule> dbRules,
|
||||
Map<String, Double> variableValues,
|
||||
List<AntecedentValue> antecedentValues,
|
||||
List<Integer> consequentValues) {
|
||||
@ -102,7 +103,7 @@ public class FuzzyInferenceService {
|
||||
mamdani.setDisjunction(new BoundedSum());
|
||||
mamdani.setImplication(new AlgebraicProduct());
|
||||
mamdani.setActivation(new Highest());
|
||||
getRulesFromDb(variableValues).forEach(r -> mamdani.addRule(Rule.parse(r, engine)));
|
||||
getRulesFromDb(dbRules, variableValues).forEach(r -> mamdani.addRule(Rule.parse(r, engine)));
|
||||
return mamdani;
|
||||
}
|
||||
|
||||
@ -116,7 +117,11 @@ public class 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) {
|
||||
@ -130,7 +135,7 @@ public class FuzzyInferenceService {
|
||||
List<AntecedentValue> antecedentValues = Stream.concat(dbRules.stream().map(DbRule::getFirstAntecedentValue),
|
||||
dbRules.stream().map(DbRule::getSecondAntecedentValue)).distinct().collect(Collectors.toList());
|
||||
List<Integer> consequentValues = dbRules.stream().map(DbRule::getId).collect(Collectors.toList());
|
||||
engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues, consequentValues));
|
||||
engine.addRuleBlock(getRuleBlock(engine, dbRules, variableValues, antecedentValues, consequentValues));
|
||||
String consequent = getConsequent(engine, variableValues);
|
||||
if (consequent.equals(NO_RESULT)) {
|
||||
return new ArrayList<>();
|
||||
@ -142,12 +147,30 @@ public class FuzzyInferenceService {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<Assessment> getAssessmentsByTimeSeriesTendencies(List<DbRule> dbRules, List<TimeSeries> 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)));
|
||||
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()));
|
||||
|
@ -115,16 +115,16 @@ public class TimeSeriesService {
|
||||
return timeSeriesRepository.getTimeSeriesByBranchId(branchId);
|
||||
}
|
||||
|
||||
public Double getLastTimeSeriesTendency(TimeSeries ts) {
|
||||
public Optional<Double> getLastTimeSeriesTendency(TimeSeries ts) {
|
||||
if (ts != null && ts.getValues().size() > 5) {
|
||||
JSONObject response = httpService.post(TIME_SERIES_TENDENCY_URL, new JSONObject(new SmoothingTimeSeries(ts)));
|
||||
LOG.debug("Успешно отправлен на сервис сглаживания");
|
||||
if (response.has("response") && response.getString("response").equals("empty")) {
|
||||
return 0.0;
|
||||
return Optional.empty();
|
||||
}
|
||||
JSONArray jsonArray = response.getJSONObject("timeSeries").getJSONArray("values");
|
||||
return jsonArray.getJSONObject(jsonArray.length() - 1).getDouble("value");
|
||||
return Optional.of(jsonArray.getJSONObject(jsonArray.length() - 1).getDouble("value"));
|
||||
}
|
||||
return 0.0;
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user