Java RCaller reuse a function - java

I'm using RCaller in order to execute a R file in Java.
Basically, it looks like this:
RCaller caller = new RCaller();
RCode code = new RCode();
caller.setRscriptExecutable(PATH_TO_EXECUTABLE);
caller.cleanRCode();
code.R_source(PATH_TO_FILE);
code.addRCode("result<-test('" + param + "')");
caller.setRCode(code);
caller.runAndReturnResult("result");
... my problem is that I'm using Rcpp in this R file (PATH_TO_FILE) and it takes several seconds to compile this external C++ file in R. Therefore I want to reuse the function and not to compile this file all the time when I execute .runAndReturnResult(...).
The R file looks like this:
library(Rcpp)
sourceCpp("/cppTest.cpp")
myfunc<- test(param)
{
t<-cppTest(4)
return(t)
}
Does anyone have an idea how to achieve this using RCaller or any other suggestions in case of other libraries which are capable of this?
Help is much appreciated!
Thanks in advance,
Chris

I don't see this as a Rcpp problem. It works as designed -- your deployment within Java may hamper you.
Reuse of R code is commonly achieved by creating a package which reliably allows you to store and reload object code. That is (among other things) what a package is for.
You essentially ask for a reinvention of sourceCpp() to also give you the benefits of a package. That won't happen. Please use a package.
And with that this then has nothing to do with the embedding inside a Java app.

Related

Create AEM packages via code

Is there a way to create an AEM package via a java code ?
We need to package some content every night via a service run by a cron job.
I checked online and it seems to be possible using a curl command. But either way, I'd need this done via a daily service running a java code.
Please refer to some of the links given below :
1)https://helpx.adobe.com/experience-manager/using/dynamic_aem_packages.html
2)http://cq5experiences.blogspot.in/2014/01/creating-packages-using-java-code-in-cq5.html
The main code goes something like this :
final JcrPackage jcrPackage = getPackageHelper().createPackageFromPathFilterSets(packageResources,
request.getResourceResolver().adaptTo(Session.class),
properties.get(PACKAGE_GROUP_NAME, getDefaultPackageGroupName()),
properties.get(PACKAGE_NAME, getDefaultPackageName()),
properties.get(PACKAGE_VERSION, DEFAULT_PACKAGE_VERSION),
PackageHelper.ConflictResolution.valueOf(properties.get(CONFLICT_RESOLUTION,
PackageHelper.ConflictResolution.IncrementVersion.toString())),
packageDefinitionProperties
);
So first of all you can create a scheduler and in the scheduler's run method you can write the logic to package the required filter paths .
Hoping this is helpful for you.

Java method that writes to file does nothing when invoked from a JSP

Hey, all! I have a class method who's primary function is to get a Map object, which works fine; however, it's an expensive operation that doesn't need to be done every time, so I'd like to have the results stored in an XML file using JAXB, to be read from for the majority of calls and updated infrequently.
When I run a class that calls it out of NetBeans the file is created no problem with exactly what I want -- but when I have my JSP call the method nothing happens whatsoever, even though the rest of the information is passed normally. I have the feeling it's somehow lacking write privileges, but the file is just in the root directory so I'm not sure what I'm missing. Thanks for the help!
The code looks roughly like this:
public class DataHandler() {
...
public void config() {
MapHolder bucket = new MapHolder();
MapExporter exp = new MapExporter();
Map map = makeMap();
bucket.setMap(map);
exp.exportMap(bucket);
}
}
And then the JSP has a javabean of Datahandler, and this line:
databean.config();
It's probably a tad more fragmented than it needs to be; the whole bucket rigamarole was because I was stumbling trying to learn how to write a map to an xml file. Mapholder is just a class that I wrap around the map, and MapExporter just uses a JAXB marshaller, and it all does work properly when run from NetBeans.
OK turns out I'm just dumb; everything was working fine, the file was just being stored in a folder at the localhost location. Whoops! That'd be my inexperience with web development at work.

How to run a .m (matlab) file through java and matlab control?

I have 2 .m files. One is the function and the other one (read.m) reads then function and exports the results into an excel file. I have a java program that makes some changes to the .m files. After the changes I want to automate the execution/running of the .m files. I have downloaded the matlabcontrol.jar and I am looking for a way to use it to invoke and run the read.m file that then reads the function.
Can anyone help me with the code? Thanks
I have tried this code but it does not work.
public static void tomatlab() throws MatlabConnectionException, MatlabInvocationException {
MatlabProxyFactoryOptions options =
new MatlabProxyFactoryOptions.Builder()
.setUsePreviouslyControlledSession(true)
.build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
MatlabProxy proxy = factory.getProxy();
proxy.eval("addpath('C:\\path_to_read.m')");
proxy.feval("read");
proxy.eval("rmpath('C:\\path_to_read.m')");
// close connection
proxy.disconnect();
}
Based on the official tutorial in the Wiki of the project, it seems quite straightforward to start with this API.
The path-manipulation might be a bit tricky, but I would give a try to loading the whole script into a string and passing it to eval (please note I have no prior experience with this specific Matlab library). That could be done quite easily (with joining Files.readAllLines() for example).
Hope that helps something.

Stanford-parser in Ruby does not create Preprocesser

I am trying to use Stanford-parser for Ruby and get a RuntimeError: Constructor not found
I had to install 'rbj' and 'treebank' gems to get it running.
Now I can
require 'stanfordparser'
but can't get to
preproc = StanfordParser::DocumentPreprocessor.new
The funciton that returns the error is here (ruby-1.9.3-p0/gems/stanfordparser-2.2.0/lib/java_object.rb:40:in `new'):
def initialize(obj, *args)
#java_object = obj.class == String ?
Rjb::import(obj).send(:new, *args) : obj
end
I saw a couple posts on some forums about this issue, but it seems no one has figured it out.
Any ideas are greatly appreciated!
It seems like no one has updated either of the two Ruby interfaces to the Stanford Parser recently, and so there may well be interface rot, with the API changes we made in version 2.0 to accommodate multithreading.
Would it be a choice to run the parser within StanfordCoreNLP? A gem for that was written very recently and is actively being developed: stanford-core-nlp.

Can android execute string as code?

So basically I would like my app to read info from a database, this info would be a string that is valid java code. Once read, I would like my app to execute this string as if it were code. Is there any functionality packaged with the android sdk that supports this?
Essentially I want the phone to populate some data with information queried from a database. One of these pieces of information would be a statement like:
"return data.ZombieKillTotal >= 100000;"
Which would be used inside a statement like:
registerAchievement(new Achievement("Zombie Killer", new AchievementValidator() {
public boolean isSatisfied(Data data) { ExecStringAsCode(query result) }
});
I just don't know what to use for 'ExecStringAsCode' :(
I ended up using the JRE library and imported javax.script.*;
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/
The Java programming language doesn't really provide a feature for the execution of code in text strings.
You could embed an interpreter for a more script-friendly language (perhaps Python) and execute code that way.
if you want to execute code loaded dynamicly you will have to use somthing like COdeDOM to create code and compile it then use reflection to load and excute it.

Categories

Resources