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

View File

@ -10,19 +10,24 @@ import java.net.URISyntaxException;
*/ */
public class FastRMean { public class FastRMean {
/** /**
* Invokes the customMean R function passing the given values as arguments. * Invokes the customMean R function passing the given values as arguments.
* *
* @param values the input to the mean script * @param values the input to the mean script
* @return the result of the R script * @return the result of the R script
*/ */
public double mean(int[] values) { public double mean(int[] values) {
Context polyglot = Context.newBuilder().allowAllAccess(true).build(); Context polyglot = Context.newBuilder()
String meanScriptContent = RUtils.getMeanScriptContent(); .allowAllAccess(true)
polyglot.eval("R", meanScriptContent); .build();
Value rBindings = polyglot.getBindings("R"); String meanScriptContent = RUtils.getMeanScriptContent();
Value rInput = rBindings.getMember("c").execute(values); polyglot.eval("R", meanScriptContent);
return rBindings.getMember("customMean").execute(rInput).asDouble(); 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 { public class RCallerMean {
/** /**
* Invokes the customMean R function passing the given values as arguments. * Invokes the customMean R function passing the given values as arguments.
* *
* @param values the input to the mean script * @param values the input to the mean script
* @return the result of the R script * @return the result of the R script
* @throws IOException if any error occurs * @throws IOException if any error occurs
* @throws URISyntaxException if any error occurs * @throws URISyntaxException if any error occurs
*/ */
public double mean(int[] values) throws IOException, URISyntaxException { public double mean(int[] values) throws IOException, URISyntaxException {
String fileContent = RUtils.getMeanScriptContent(); String fileContent = RUtils.getMeanScriptContent();
RCode code = RCode.create(); RCode code = RCode.create();
code.addRCode(fileContent); code.addRCode(fileContent);
code.addIntArray("input", values); code.addIntArray("input", values);
code.addRCode("result <- customMean(input)"); code.addRCode("result <- customMean(input)");
RCaller caller = RCaller.create(code, RCallerOptions.create()); RCaller caller = RCaller.create(code, RCallerOptions.create());
caller.runAndReturnResult("result"); caller.runAndReturnResult("result");
return caller.getParser().getAsDoubleArray("result")[0]; return caller.getParser()
} .getAsDoubleArray("result")[0];
}
} }

View File

@ -15,16 +15,19 @@ import java.util.stream.Collectors;
*/ */
public class RUtils { public class RUtils {
/** /**
* Loads the script.R and returns its content as a string. * Loads the script.R and returns its content as a string.
* *
* @return the script.R content as a string * @return the script.R content as a string
* @throws IOException if any error occurs * @throws IOException if any error occurs
* @throws URISyntaxException if any error occurs * @throws URISyntaxException if any error occurs
*/ */
static String getMeanScriptContent() throws IOException, URISyntaxException { static String getMeanScriptContent() throws IOException, URISyntaxException {
URI rScriptUri = RUtils.class.getClassLoader().getResource("script.R").toURI(); URI rScriptUri = RUtils.class.getClassLoader()
Path inputScript = Paths.get(rScriptUri); .getResource("script.R")
return Files.lines(inputScript).collect(Collectors.joining()); .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 { public class RenjinMean {
/** /**
* Invokes the customMean R function passing the given values as arguments. * Invokes the customMean R function passing the given values as arguments.
* *
* @param values the input to the mean script * @param values the input to the mean script
* @return the result of the R script * @return the result of the R script
* @throws IOException if any error occurs * @throws IOException if any error occurs
* @throws URISyntaxException if any error occurs * @throws URISyntaxException if any error occurs
* @throws ScriptException if any error occurs * @throws ScriptException if any error occurs
*/ */
public double mean(int[] values) throws IOException, URISyntaxException, ScriptException { public double mean(int[] values) throws IOException, URISyntaxException, ScriptException {
RenjinScriptEngine engine = new RenjinScriptEngine(); RenjinScriptEngine engine = new RenjinScriptEngine();
String meanScriptContent = RUtils.getMeanScriptContent(); String meanScriptContent = RUtils.getMeanScriptContent();
engine.put("input", values); engine.put("input", values);
engine.eval(meanScriptContent); engine.eval(meanScriptContent);
DoubleArrayVector result = (DoubleArrayVector) engine.eval("customMean(input)"); DoubleArrayVector result = (DoubleArrayVector) engine.eval("customMean(input)");
return result.asReal(); return result.asReal();
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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