Skip to content

Commit

Permalink
Java project to show Java/R interop
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaroslav Tulach committed Apr 11, 2016
1 parent 9e18938 commit 4549d53
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
18 changes: 18 additions & 0 deletions R/nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.jdkPlatform>GraalVM</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>
27 changes: 27 additions & 0 deletions R/nbactions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>debug</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>exec:exec</goal>
</goals>
<properties>
<debug>-J:-Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address}</debug>
<jpda.listen>true</jpda.listen>
</properties>
</action>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>exec:exec</goal>
</goals>
</action>
</actions>
40 changes: 40 additions & 0 deletions R/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apidesign.demo</groupId>
<artifactId>sieve-r</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Sieve with R in Java</name>
<dependencies>
<dependency>
<groupId>com.oracle.truffle</groupId>
<artifactId>truffle-api</artifactId>
<version>0.10</version>
<type>jar</type>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<executable>${java.home}/bin/graalvm</executable>
<arguments>
<arguments>${debug}</arguments>
<argument>-cp</argument>
<classpath/>
<argument>org.apidesign.demo.sieve.r.Main</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
34 changes: 34 additions & 0 deletions R/src/main/java/org/apidesign/demo/sieve/r/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.apidesign.demo.sieve.r;

import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.vm.PolyglotEngine;
import java.io.File;
import java.net.URL;

public final class Main {
private Main() {
}

public static void main(String[] args) throws Exception {
System.err.println("Setting up PolyglotEngine");
PolyglotEngine vm = PolyglotEngine.newBuilder().
build();

vm.eval(Source.fromText("", "warmup.R").withMimeType("text/x-r"));

URL url = Main.class.getProtectionDomain().getCodeSource().getLocation();
File local = new File(url.toURI());
for (;;) {
File sieveInR = new File(local, "sieve.R");
if (sieveInR.exists()) {
break;
}
local = local.getParentFile();
}
System.err.println("engine is ready");

Source r = Source.fromFileName(new File(local, "sieve.R").getPath());

vm.eval(r);
}
}

0 comments on commit 4549d53

Please sign in to comment.