Fixed whitespace formatting.

This commit is contained in:
Donato Rimenti 2020-04-24 16:51:50 +02:00
parent 16ca52363c
commit 7d6e096d28
10 changed files with 184 additions and 177 deletions

View File

@ -128,24 +128,21 @@
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.rosuda.REngine</groupId>
<artifactId>Rserve</artifactId>
<version>${rserve.version}</version>
</dependency>
<dependency>
<groupId>com.github.jbytecode</groupId>
<artifactId>RCaller</artifactId>
<version>${rcaller.version}</version>
</dependency>
<dependency>
<groupId>org.renjin</groupId>
<artifactId>renjin-script-engine</artifactId>
<version>${renjin.version}</version>
</dependency>
<dependency>
<groupId>org.rosuda.REngine</groupId>
<artifactId>Rserve</artifactId>
<version>${rserve.version}</version>
</dependency>
<dependency>
<groupId>com.github.jbytecode</groupId>
<artifactId>RCaller</artifactId>
<version>${rcaller.version}</version>
</dependency>
<dependency>
<groupId>org.renjin</groupId>
<artifactId>renjin-script-engine</artifactId>
<version>${renjin.version}</version>
</dependency>
</dependencies>
<repositories>
@ -157,11 +154,11 @@
</repository>
<!-- Needed for Renjin -->
<repository>
<id>bedatadriven</id>
<name>bedatadriven public repo</name>
<url>https://nexus.bedatadriven.com/content/groups/public/</url>
</repository>
<repository>
<id>bedatadriven</id>
<name>bedatadriven public repo</name>
<url>https://nexus.bedatadriven.com/content/groups/public/</url>
</repository>
</repositories>
<properties>
@ -183,22 +180,22 @@
<rserve.version>1.8.1</rserve.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- Excludes FastR classes from compilations since they require GraalVM -->
<excludes>
<exclude>com/baeldung/r/FastRMean.java</exclude>
</excludes>
<testExcludes>
<exclude>com/baeldung/r/FastRMeanUnitTest.java</exclude>
</testExcludes>
</configuration>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- Excludes FastR classes from compilations since they require GraalVM -->
<excludes>
<exclude>com/baeldung/r/FastRMean.java</exclude>
</excludes>
<testExcludes>
<exclude>com/baeldung/r/FastRMeanUnitTest.java</exclude>
</testExcludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -10,19 +10,24 @@ import java.net.URISyntaxException;
*/
public class FastRMean {
/**
* Invokes the customMean R function passing the given values as arguments.
*
* @param values the input to the mean script
* @return the result of the R script
*/
public double mean(int[] values) {
Context polyglot = Context.newBuilder().allowAllAccess(true).build();
String meanScriptContent = RUtils.getMeanScriptContent();
polyglot.eval("R", meanScriptContent);
Value rBindings = polyglot.getBindings("R");
Value rInput = rBindings.getMember("c").execute(values);
return rBindings.getMember("customMean").execute(rInput).asDouble();
}
/**
* Invokes the customMean R function passing the given values as arguments.
*
* @param values the input to the mean script
* @return the result of the R script
*/
public double mean(int[] values) {
Context polyglot = Context.newBuilder()
.allowAllAccess(true)
.build();
String meanScriptContent = RUtils.getMeanScriptContent();
polyglot.eval("R", meanScriptContent);
Value rBindings = polyglot.getBindings("R");
Value rInput = rBindings.getMember("c")
.execute(values);
return rBindings.getMember("customMean")
.execute(rInput)
.asDouble();
}
}

View File

@ -14,23 +14,24 @@ import com.github.rcaller.rstuff.RCode;
*/
public class RCallerMean {
/**
* Invokes the customMean R function passing the given values as arguments.
*
* @param values the input to the mean script
* @return the result of the R script
* @throws IOException if any error occurs
* @throws URISyntaxException if any error occurs
*/
public double mean(int[] values) throws IOException, URISyntaxException {
String fileContent = RUtils.getMeanScriptContent();
RCode code = RCode.create();
code.addRCode(fileContent);
code.addIntArray("input", values);
code.addRCode("result <- customMean(input)");
RCaller caller = RCaller.create(code, RCallerOptions.create());
caller.runAndReturnResult("result");
return caller.getParser().getAsDoubleArray("result")[0];
}
/**
* Invokes the customMean R function passing the given values as arguments.
*
* @param values the input to the mean script
* @return the result of the R script
* @throws IOException if any error occurs
* @throws URISyntaxException if any error occurs
*/
public double mean(int[] values) throws IOException, URISyntaxException {
String fileContent = RUtils.getMeanScriptContent();
RCode code = RCode.create();
code.addRCode(fileContent);
code.addIntArray("input", values);
code.addRCode("result <- customMean(input)");
RCaller caller = RCaller.create(code, RCallerOptions.create());
caller.runAndReturnResult("result");
return caller.getParser()
.getAsDoubleArray("result")[0];
}
}

View File

@ -15,16 +15,19 @@ import java.util.stream.Collectors;
*/
public class RUtils {
/**
* Loads the script.R and returns its content as a string.
*
* @return the script.R content as a string
* @throws IOException if any error occurs
* @throws URISyntaxException if any error occurs
*/
static String getMeanScriptContent() throws IOException, URISyntaxException {
URI rScriptUri = RUtils.class.getClassLoader().getResource("script.R").toURI();
Path inputScript = Paths.get(rScriptUri);
return Files.lines(inputScript).collect(Collectors.joining());
}
/**
* Loads the script.R and returns its content as a string.
*
* @return the script.R content as a string
* @throws IOException if any error occurs
* @throws URISyntaxException if any error occurs
*/
static String getMeanScriptContent() throws IOException, URISyntaxException {
URI rScriptUri = RUtils.class.getClassLoader()
.getResource("script.R")
.toURI();
Path inputScript = Paths.get(rScriptUri);
return Files.lines(inputScript)
.collect(Collectors.joining());
}
}

View File

@ -15,22 +15,22 @@ import org.renjin.sexp.DoubleArrayVector;
*/
public class RenjinMean {
/**
* Invokes the customMean R function passing the given values as arguments.
*
* @param values the input to the mean script
* @return the result of the R script
* @throws IOException if any error occurs
* @throws URISyntaxException if any error occurs
* @throws ScriptException if any error occurs
*/
public double mean(int[] values) throws IOException, URISyntaxException, ScriptException {
RenjinScriptEngine engine = new RenjinScriptEngine();
String meanScriptContent = RUtils.getMeanScriptContent();
engine.put("input", values);
engine.eval(meanScriptContent);
DoubleArrayVector result = (DoubleArrayVector) engine.eval("customMean(input)");
return result.asReal();
}
/**
* Invokes the customMean R function passing the given values as arguments.
*
* @param values the input to the mean script
* @return the result of the R script
* @throws IOException if any error occurs
* @throws URISyntaxException if any error occurs
* @throws ScriptException if any error occurs
*/
public double mean(int[] values) throws IOException, URISyntaxException, ScriptException {
RenjinScriptEngine engine = new RenjinScriptEngine();
String meanScriptContent = RUtils.getMeanScriptContent();
engine.put("input", values);
engine.eval(meanScriptContent);
DoubleArrayVector result = (DoubleArrayVector) engine.eval("customMean(input)");
return result.asReal();
}
}

View File

@ -11,19 +11,20 @@ import org.rosuda.REngine.Rserve.RConnection;
*/
public class RserveMean {
/**
* Connects to the Rserve istance listening on 127.0.0.1:6311 and invokes the
* customMean R function passing the given values as arguments.
*
* @param values the input to the mean script
* @return the result of the R script
* @throws REngineException if any error occurs
* @throws REXPMismatchException if any error occurs
*/
public double mean(int[] values) throws REngineException, REXPMismatchException {
RConnection c = new RConnection();
c.assign("input", values);
return c.eval("customMean(input)").asDouble();
}
/**
* Connects to the Rserve istance listening on 127.0.0.1:6311 and invokes the
* customMean R function passing the given values as arguments.
*
* @param values the input to the mean script
* @return the result of the R script
* @throws REngineException if any error occurs
* @throws REXPMismatchException if any error occurs
*/
public double mean(int[] values) throws REngineException, REXPMismatchException {
RConnection c = new RConnection();
c.assign("input", values);
return c.eval("customMean(input)")
.asDouble();
}
}

View File

@ -12,18 +12,18 @@ import org.junit.Test;
@Ignore
public class FastRMeanUnitTest {
/**
* Object to test.
*/
private FastRMean fastrMean = new FastRMean();
/**
* Object to test.
*/
private FastRMean fastrMean = new FastRMean();
/**
* Test for {@link FastRMeanUnitTest#mean(int[])}.
*/
@Test
public void givenValues_whenMean_thenCorrect() {
int[] input = { 1, 2, 3, 4, 5 };
double result = fastrMean.mean(input);
Assert.assertEquals(3.0, result, 0.000001);
}
/**
* Test for {@link FastRMeanUnitTest#mean(int[])}.
*/
@Test
public void givenValues_whenMean_thenCorrect() {
int[] input = { 1, 2, 3, 4, 5 };
double result = fastrMean.mean(input);
Assert.assertEquals(3.0, result, 0.000001);
}
}

View File

@ -17,21 +17,21 @@ import org.junit.Test;
@Ignore
public class RCallerMeanIntegrationTest {
/**
* Object to test.
*/
private RCallerMean rcallerMean = new RCallerMean();
/**
* Object to test.
*/
private RCallerMean rcallerMean = new RCallerMean();
/**
* Test for {@link RCallerMeanIntegrationTest#mean(int[])}.
*
* @throws ScriptException if an error occurs
* @throws URISyntaxException if an error occurs
*/
@Test
public void givenValues_whenMean_thenCorrect() throws IOException, URISyntaxException {
int[] input = { 1, 2, 3, 4, 5 };
double result = rcallerMean.mean(input);
Assert.assertEquals(3.0, result, 0.000001);
}
/**
* Test for {@link RCallerMeanIntegrationTest#mean(int[])}.
*
* @throws ScriptException if an error occurs
* @throws URISyntaxException if an error occurs
*/
@Test
public void givenValues_whenMean_thenCorrect() throws IOException, URISyntaxException {
int[] input = { 1, 2, 3, 4, 5 };
double result = rcallerMean.mean(input);
Assert.assertEquals(3.0, result, 0.000001);
}
}

View File

@ -16,22 +16,22 @@ import org.junit.Assert;
*/
public class RenjinMeanUnitTest {
/**
* Object to test.
*/
private RenjinMean renjinMean = new RenjinMean();
/**
* Object to test.
*/
private RenjinMean renjinMean = new RenjinMean();
/**
* Test for {@link RenjinMeanUnitTest#mean(int[])}.
*
* @throws ScriptException if an error occurs
* @throws URISyntaxException if an error occurs
* @throws IOException if an error occurs
*/
@Test
public void givenValues_whenMean_thenCorrect() throws IOException, URISyntaxException, ScriptException {
int[] input = { 1, 2, 3, 4, 5 };
double result = renjinMean.mean(input);
Assert.assertEquals(3.0, result, 0.000001);
}
/**
* Test for {@link RenjinMeanUnitTest#mean(int[])}.
*
* @throws ScriptException if an error occurs
* @throws URISyntaxException if an error occurs
* @throws IOException if an error occurs
*/
@Test
public void givenValues_whenMean_thenCorrect() throws IOException, URISyntaxException, ScriptException {
int[] input = { 1, 2, 3, 4, 5 };
double result = renjinMean.mean(input);
Assert.assertEquals(3.0, result, 0.000001);
}
}

View File

@ -14,21 +14,21 @@ import org.rosuda.REngine.REngineException;
@Ignore
public class RserveMeanIntegrationTest {
/**
* Object to test.
*/
private RserveMean rserveMean = new RserveMean();
/**
* Object to test.
*/
private RserveMean rserveMean = new RserveMean();
/**
* Test for {@link RserveMeanIntegrationTest#mean(int[])}.
*
* @throws REXPMismatchException if an error occurs
* @throws REngineException if an error occurs
*/
@Test
public void givenValues_whenMean_thenCorrect() throws REngineException, REXPMismatchException {
int[] input = { 1, 2, 3, 4, 5 };
double result = rserveMean.mean(input);
Assert.assertEquals(3.0, result, 0.000001);
}
/**
* Test for {@link RserveMeanIntegrationTest#mean(int[])}.
*
* @throws REXPMismatchException if an error occurs
* @throws REngineException if an error occurs
*/
@Test
public void givenValues_whenMean_thenCorrect() throws REngineException, REXPMismatchException {
int[] input = { 1, 2, 3, 4, 5 };
double result = rserveMean.mean(input);
Assert.assertEquals(3.0, result, 0.000001);
}
}