diff --git a/src/main/java/ru/ulstu/http/HttpService.java b/src/main/java/ru/ulstu/http/HttpService.java index 43a8058..7ef4484 100644 --- a/src/main/java/ru/ulstu/http/HttpService.java +++ b/src/main/java/ru/ulstu/http/HttpService.java @@ -2,15 +2,17 @@ package ru.ulstu.http; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; -import org.springframework.web.reactive.function.client.ExchangeStrategies; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; +import ru.ulstu.method.fuzzy.FuzzyRuleDataDto; +import ru.ulstu.method.fuzzy.OutputValue; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Base64; -import java.util.concurrent.CompletableFuture; - -import static java.nio.charset.StandardCharsets.UTF_8; +import java.util.List; public class HttpService { private final Logger log = LoggerFactory.getLogger(HttpService.class); @@ -21,11 +23,13 @@ public class HttpService { public WebClient getWebClient() { final int size = 16 * 1024 * 1024 * 10; - final ExchangeStrategies strategies = ExchangeStrategies.builder() + /*final ExchangeStrategies strategies = ExchangeStrategies.builder() .codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size)) - .build(); + .build();*/ return WebClient.builder() - .exchangeStrategies(strategies) + //.exchangeStrategies(strategies) + .defaultHeader(HttpHeaders.AUTHORIZATION, "Basic " + + Base64.getEncoder().encodeToString((USER_NAME + ":" + PASSWORD).getBytes())) .build(); } // private void auth() { @@ -57,28 +61,24 @@ public class HttpService { // return get(yearStart, yearEnd); // } - public String post(String url, Object requestBody) { - String response = null; + public List post(String url, FuzzyRuleDataDto requestBody) { + List result = new ArrayList<>(); try { - System.out.println("set cookies"); - CompletableFuture cf = client + OutputValue[] res = client .post() - .uri(String.format(url)) - .header("Authorization", "Basic " + Base64.getEncoder() - .encodeToString((USER_NAME + ":" + PASSWORD).getBytes(UTF_8))) - .header("IBSession", "finish") + .uri(url) .contentType(MediaType.APPLICATION_JSON) - .body(Mono.just(requestBody), String.class) - //.cookies(cookies -> cookies.addAll(myCookies)) + .body(Mono.just(requestBody), FuzzyRuleDataDto.class) .retrieve() - .bodyToMono(String.class) - .toFuture(); - cf.get(); - response = cf.get(); + .bodyToMono(OutputValue[].class) + .block(); + if (res != null && res.length > 0) { + result = Arrays.stream(res).toList(); + } } catch (Exception e) { e.printStackTrace(); } - return response; + return result; } } diff --git a/src/main/java/ru/ulstu/method/fuzzy/FuzzyRuleDataDto.java b/src/main/java/ru/ulstu/method/fuzzy/FuzzyRuleDataDto.java new file mode 100644 index 0000000..37eb0ea --- /dev/null +++ b/src/main/java/ru/ulstu/method/fuzzy/FuzzyRuleDataDto.java @@ -0,0 +1,33 @@ +package ru.ulstu.method.fuzzy; + +public class FuzzyRuleDataDto { + private String[] fuzzyTerms; + private int window = 3; + private int horizon = 1; + + public FuzzyRuleDataDto(String[] fuzzyTimeSeries, int window, int horizon) { + this.fuzzyTerms = fuzzyTimeSeries; + this.window = window; + this.horizon = horizon; + } + + public String[] getFuzzyTerms() { + return fuzzyTerms; + } + + public void setFuzzyTerms(String[] fuzzyTerms) { + this.fuzzyTerms = fuzzyTerms; + } + + public int getWindow() { + return window; + } + + public void setWindow(int window) { + this.window = window; + } + + public int getHorizon() { + return horizon; + } +} diff --git a/src/main/java/ru/ulstu/method/fuzzy/OutputValue.java b/src/main/java/ru/ulstu/method/fuzzy/OutputValue.java new file mode 100644 index 0000000..ea40027 --- /dev/null +++ b/src/main/java/ru/ulstu/method/fuzzy/OutputValue.java @@ -0,0 +1,37 @@ +package ru.ulstu.method.fuzzy; + +public class OutputValue { + private String variable; + private String fuzzyTerm; + private Double degree; + + public OutputValue(String variable, String fuzzyTerm, Double degree) { + this.variable = variable; + this.fuzzyTerm = fuzzyTerm; + this.degree = degree; + } + + public String getFuzzyTerm() { + return fuzzyTerm; + } + + public void setFuzzyTerm(String fuzzyTerm) { + this.fuzzyTerm = fuzzyTerm; + } + + public Double getDegree() { + return degree; + } + + public void setDegree(Double degree) { + this.degree = degree; + } + + public String getVariable() { + return variable; + } + + public void setVariable(String variable) { + this.variable = variable; + } +} diff --git a/src/main/java/ru/ulstu/method/fuzzy/PlainFuzzy.java b/src/main/java/ru/ulstu/method/fuzzy/PlainFuzzy.java index a73899a..c0c5b9b 100644 --- a/src/main/java/ru/ulstu/method/fuzzy/PlainFuzzy.java +++ b/src/main/java/ru/ulstu/method/fuzzy/PlainFuzzy.java @@ -13,7 +13,6 @@ import ru.ulstu.method.MethodParameter; import java.util.ArrayList; import java.util.DoubleSummaryStatistics; import java.util.List; -import java.util.stream.Collectors; @Component public class PlainFuzzy extends Method { @@ -28,6 +27,7 @@ public class PlainFuzzy extends Method { protected Model getModelOfValidTimeSeries(TimeSeries timeSeries, List parameters) { PlainFuzzyModel model = new PlainFuzzyModel(timeSeries, parameters); List fuzzySets = generateFuzzySets(timeSeries, model.getNumberOfFuzzyTerms().getIntValue()); + model.setFuzzySets(fuzzySets); for (TimeSeriesValue tsVal : timeSeries.getValues()) { model.getTimeSeriesModel().addValue(new TimeSeriesValue( tsVal.getDate(), @@ -72,10 +72,22 @@ public class PlainFuzzy extends Method { @Override protected TimeSeries getForecastWithValidParams(Model model, TimeSeries forecast) throws ModelingException { - String fuzzyTimeSeries = ((PlainFuzzyModel) model).getFuzzyTimeSeries().stream().map(Triangle::getLabel).collect(Collectors.joining(",")); - String result = httpService.post("http://plans.athene.tech/fuzzyRuleRest/generate-rules/3152", fuzzyTimeSeries); - for (TimeSeriesValue tsVal : forecast.getValues()) { - tsVal.setValue(0.0); + PlainFuzzyModel pfm = ((PlainFuzzyModel) model); + List fuzzyTimeSeries = pfm.getFuzzyTimeSeries().stream().map(Triangle::getLabel).toList(); + List result = httpService.post("http://plans.athene.tech/inferenceRest/get-inference-by-generated-rules", + new FuzzyRuleDataDto(fuzzyTimeSeries.toArray(String[]::new), 2, forecast.getLength())); + List forecastValues = result.stream().map(r -> pfm.getTopValueByLabel(r.getFuzzyTerm())).toList(); + List values = forecast.getValues(); + for (int i = 0; i < values.size(); i++) { + if (forecastValues.isEmpty()) { + values.get(i).setValue(pfm.getTimeSeriesModel().getValues().getLast().getValue()); + } else { + if (forecastValues.size() > i) { + values.get(i).setValue(forecastValues.get(i)); + } else { + values.get(i).setValue(forecastValues.getLast()); + } + } } return forecast; } diff --git a/src/main/java/ru/ulstu/method/fuzzy/PlainFuzzyModel.java b/src/main/java/ru/ulstu/method/fuzzy/PlainFuzzyModel.java index a0e0553..97bf6eb 100644 --- a/src/main/java/ru/ulstu/method/fuzzy/PlainFuzzyModel.java +++ b/src/main/java/ru/ulstu/method/fuzzy/PlainFuzzyModel.java @@ -13,6 +13,7 @@ import java.util.List; public class PlainFuzzyModel extends Model { private final MethodParamValue numberOfFuzzyTerms = new MethodParamValue(NumberOfFuzzyTerms.getInstance(), 2); private final List fuzzyTimeSeries = new ArrayList<>(); + private List fuzzySets = new ArrayList<>(); protected PlainFuzzyModel(TimeSeries ts, List parameters) { super(ts); @@ -34,4 +35,21 @@ public class PlainFuzzyModel extends Model { public List getFuzzyTimeSeries() { return fuzzyTimeSeries; } + + public void setFuzzySets(List fuzzySets) { + this.fuzzySets = fuzzySets; + } + + public List getFuzzySets() { + return fuzzySets; + } + + public double getTopValueByLabel(String label) { + return fuzzySets + .stream() + .filter(fs -> fs.getLabel().equals(label)) + .findAny() + .orElseThrow(() -> new RuntimeException("Неизвестная нечеткая метка")) + .getTop(); + } }