#14 -- Make forecast request

This commit is contained in:
Anton Romanov 2025-03-20 14:23:43 +04:00
parent dd73517e93
commit 11c4071b68
5 changed files with 127 additions and 27 deletions

View File

@ -2,15 +2,17 @@ package ru.ulstu.http;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono; 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.Base64;
import java.util.concurrent.CompletableFuture; import java.util.List;
import static java.nio.charset.StandardCharsets.UTF_8;
public class HttpService { public class HttpService {
private final Logger log = LoggerFactory.getLogger(HttpService.class); private final Logger log = LoggerFactory.getLogger(HttpService.class);
@ -21,11 +23,13 @@ public class HttpService {
public WebClient getWebClient() { public WebClient getWebClient() {
final int size = 16 * 1024 * 1024 * 10; final int size = 16 * 1024 * 1024 * 10;
final ExchangeStrategies strategies = ExchangeStrategies.builder() /*final ExchangeStrategies strategies = ExchangeStrategies.builder()
.codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size)) .codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
.build(); .build();*/
return WebClient.builder() return WebClient.builder()
.exchangeStrategies(strategies) //.exchangeStrategies(strategies)
.defaultHeader(HttpHeaders.AUTHORIZATION, "Basic " +
Base64.getEncoder().encodeToString((USER_NAME + ":" + PASSWORD).getBytes()))
.build(); .build();
} }
// private void auth() { // private void auth() {
@ -57,28 +61,24 @@ public class HttpService {
// return get(yearStart, yearEnd); // return get(yearStart, yearEnd);
// } // }
public String post(String url, Object requestBody) { public List<OutputValue> post(String url, FuzzyRuleDataDto requestBody) {
String response = null; List<OutputValue> result = new ArrayList<>();
try { try {
System.out.println("set cookies"); OutputValue[] res = client
CompletableFuture<String> cf = client
.post() .post()
.uri(String.format(url)) .uri(url)
.header("Authorization", "Basic " + Base64.getEncoder()
.encodeToString((USER_NAME + ":" + PASSWORD).getBytes(UTF_8)))
.header("IBSession", "finish")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.body(Mono.just(requestBody), String.class) .body(Mono.just(requestBody), FuzzyRuleDataDto.class)
//.cookies(cookies -> cookies.addAll(myCookies))
.retrieve() .retrieve()
.bodyToMono(String.class) .bodyToMono(OutputValue[].class)
.toFuture(); .block();
cf.get(); if (res != null && res.length > 0) {
response = cf.get(); result = Arrays.stream(res).toList();
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return response; return result;
} }
} }

View File

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

View File

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

View File

@ -13,7 +13,6 @@ import ru.ulstu.method.MethodParameter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.DoubleSummaryStatistics; import java.util.DoubleSummaryStatistics;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Component @Component
public class PlainFuzzy extends Method { public class PlainFuzzy extends Method {
@ -28,6 +27,7 @@ public class PlainFuzzy extends Method {
protected Model getModelOfValidTimeSeries(TimeSeries timeSeries, List<MethodParamValue> parameters) { protected Model getModelOfValidTimeSeries(TimeSeries timeSeries, List<MethodParamValue> parameters) {
PlainFuzzyModel model = new PlainFuzzyModel(timeSeries, parameters); PlainFuzzyModel model = new PlainFuzzyModel(timeSeries, parameters);
List<Triangle> fuzzySets = generateFuzzySets(timeSeries, model.getNumberOfFuzzyTerms().getIntValue()); List<Triangle> fuzzySets = generateFuzzySets(timeSeries, model.getNumberOfFuzzyTerms().getIntValue());
model.setFuzzySets(fuzzySets);
for (TimeSeriesValue tsVal : timeSeries.getValues()) { for (TimeSeriesValue tsVal : timeSeries.getValues()) {
model.getTimeSeriesModel().addValue(new TimeSeriesValue( model.getTimeSeriesModel().addValue(new TimeSeriesValue(
tsVal.getDate(), tsVal.getDate(),
@ -72,10 +72,22 @@ public class PlainFuzzy extends Method {
@Override @Override
protected TimeSeries getForecastWithValidParams(Model model, TimeSeries forecast) throws ModelingException { protected TimeSeries getForecastWithValidParams(Model model, TimeSeries forecast) throws ModelingException {
String fuzzyTimeSeries = ((PlainFuzzyModel) model).getFuzzyTimeSeries().stream().map(Triangle::getLabel).collect(Collectors.joining(",")); PlainFuzzyModel pfm = ((PlainFuzzyModel) model);
String result = httpService.post("http://plans.athene.tech/fuzzyRuleRest/generate-rules/3152", fuzzyTimeSeries); List<String> fuzzyTimeSeries = pfm.getFuzzyTimeSeries().stream().map(Triangle::getLabel).toList();
for (TimeSeriesValue tsVal : forecast.getValues()) { List<OutputValue> result = httpService.post("http://plans.athene.tech/inferenceRest/get-inference-by-generated-rules",
tsVal.setValue(0.0); new FuzzyRuleDataDto(fuzzyTimeSeries.toArray(String[]::new), 2, forecast.getLength()));
List<Double> forecastValues = result.stream().map(r -> pfm.getTopValueByLabel(r.getFuzzyTerm())).toList();
List<TimeSeriesValue> 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; return forecast;
} }

View File

@ -13,6 +13,7 @@ import java.util.List;
public class PlainFuzzyModel extends Model { public class PlainFuzzyModel extends Model {
private final MethodParamValue numberOfFuzzyTerms = new MethodParamValue(NumberOfFuzzyTerms.getInstance(), 2); private final MethodParamValue numberOfFuzzyTerms = new MethodParamValue(NumberOfFuzzyTerms.getInstance(), 2);
private final List<Triangle> fuzzyTimeSeries = new ArrayList<>(); private final List<Triangle> fuzzyTimeSeries = new ArrayList<>();
private List<Triangle> fuzzySets = new ArrayList<>();
protected PlainFuzzyModel(TimeSeries ts, List<MethodParamValue> parameters) { protected PlainFuzzyModel(TimeSeries ts, List<MethodParamValue> parameters) {
super(ts); super(ts);
@ -34,4 +35,21 @@ public class PlainFuzzyModel extends Model {
public List<Triangle> getFuzzyTimeSeries() { public List<Triangle> getFuzzyTimeSeries() {
return fuzzyTimeSeries; return fuzzyTimeSeries;
} }
public void setFuzzySets(List<Triangle> fuzzySets) {
this.fuzzySets = fuzzySets;
}
public List<Triangle> getFuzzySets() {
return fuzzySets;
}
public double getTopValueByLabel(String label) {
return fuzzySets
.stream()
.filter(fs -> fs.getLabel().equals(label))
.findAny()
.orElseThrow(() -> new RuntimeException("Неизвестная нечеткая метка"))
.getTop();
}
} }