Skip to content

Instantly share code, notes, and snippets.

@dmi3coder
Last active April 22, 2020 16:51
Show Gist options
  • Save dmi3coder/c7cf2dec697f3bf50dc9a6e9ba74525c to your computer and use it in GitHub Desktop.
Save dmi3coder/c7cf2dec697f3bf50dc9a6e9ba74525c to your computer and use it in GitHub Desktop.
Example of execution of R function from Java Quarkus
package tech.donau.quarkify;
import io.quarkus.runtime.StartupEvent;
import org.graalvm.polyglot.*;
import javax.enterprise.event.Observes;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.net.URL;
@Path("/cooling")
public class CpuCoolingResource {
public static Context context = Context.newBuilder().allowIO(true).allowAllAccess(true).build();
public void onStart(@Observes StartupEvent e) throws IOException {
final URL cpuCoolingR = getClass().getClassLoader().getResource("cpu_cooling.R");
context.eval(Source.newBuilder("R", cpuCoolingR).build());
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello(@QueryParam("grads") Double grads) {
final Value needs_cooling = context.getBindings("R").getMember("needs_cooling");
final Value result = needs_cooling.execute(grads);
return result.asDouble() > 0.8 ? "Enable cooling" : "No need to enable cooling";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment