Commit 7b7bbbc8 authored by Sören Henning's avatar Sören Henning

handle unavailability of R server

parent 5118f3aa
......@@ -16,10 +16,10 @@ public class ARIMAForecaster extends AbstractRForecaster {
}
@Override
protected double performRForecast(final EquidistantTimeSeries timeSeries) throws REXPMismatchException, REngineException {
protected double performRForecast(final EquidistantTimeSeries timeSeries) throws REngineException {
if (timeSeries.isEmpty()) {
return 0; // TODO
return Double.NaN;
}
// Transfer time series to R
......@@ -27,9 +27,13 @@ public class ARIMAForecaster extends AbstractRForecaster {
// Do ARIMA forecast on time Series
super.rConnection.eval("forecast <- forecast(auto.arima(ts(timeSeries)), h=1)");
// Retrieve result
double result = super.rConnection.eval("as.numeric(forecast$mean)").asDouble();
try {
double result = super.rConnection.eval("as.numeric(forecast$mean)").asDouble();
return result;
} catch (REXPMismatchException e) {
return Double.NaN;
}
return result;
}
}
package teead.forecast;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
......@@ -9,38 +8,52 @@ import teead.timeseries.EquidistantTimeSeries;
public abstract class AbstractRForecaster implements Forecaster {
protected final RConnection rConnection;
public final static String HOST_CONFIGURATION_KEY = "rserve.host";
public final static String PORT_CONFIGURATION_KEY = "rserve.port";
protected RConnection rConnection = null;
private final String host;
private final int port;
public AbstractRForecaster(final String host, final int port) {
try {
this.rConnection = new RConnection(host, port);
this.rConnection.eval("library(forecast)");
} catch (RserveException e) {
// TODO Exception Handling
e.printStackTrace();
throw new IllegalStateException(e); // TODO
}
this.host = host;
this.port = port;
establishRConnection();
}
public AbstractRForecaster(final ForecasterConfiguration configuration) {
this(configuration.get(HOST_CONFIGURATION_KEY), Integer.parseInt(configuration.get(PORT_CONFIGURATION_KEY)));
}
private void establishRConnection() {
// BETTER Do this asynchronously
try {
if (this.rConnection != null) {
this.rConnection.close();
}
this.rConnection = new RConnection(this.host, this.port);
this.rConnection.eval("library(forecast)");
} catch (RserveException e) {
// Could not establish the R connection
this.rConnection = null;
}
}
@Override
public final double forecast(final EquidistantTimeSeries timeSeries) {
// TODO Exception Handling
try {
return performRForecast(timeSeries);
} catch (REXPMismatchException | REngineException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (this.rConnection != null) {
try {
return performRForecast(timeSeries);
} catch (REngineException e) {
establishRConnection();
return Double.NaN;
}
} else {
establishRConnection();
return Double.NaN;
}
return Double.NaN; // TODO
}
protected abstract double performRForecast(final EquidistantTimeSeries timeSeries) throws REXPMismatchException, REngineException;
protected abstract double performRForecast(final EquidistantTimeSeries timeSeries) throws REngineException;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment