I am preparing an R wrapper for a java code that I didn't write myself (and in fact I don't know java). I am trying to use rJava for the first time and I am struggling to get the .jcall right.
Here is an extract of the java code for which I write a wrapper:
public class Model4R{
[...cut...]
public String[][] runModel(String dir, String initFileName, String[] variableNames, int numSims) throws Exception {
[...cut...]
dir and initFileName are character strings for the directory and file name with initial conditions, variable names is a list of character strings that I would write like this in R: c("var1", "var2", "var3", ...) and can be of length from one to five. Finally, numSim is an integer.
Here is my tentative R code for a wrapper function:
runmodel <- function(dir, inFile, varNames, numSim){
hjw <- .jnew("Model4R")
out <- .jcall(hjw, "[[Ljava/lang/String", "runModel", as.character(dir), as.character(inFile), as.vector(varNames), as.integer(numSim))
return(out)
}
The error in R is:
Error in .jcall(hjw, "[[Ljava/lang/String", "runModel", as.character(dir),
: method runModel with signature (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;II)[[Ljava/lang/String not found
I suspect that the JNI type isn't correct for String[][]. Anyhow, any help that could direct me towards a solution would be welcome!
You're missing a semicolon at the end of the JNI for String[][] - it should be "[[Ljava/lang/String;". Also, I think you need to call .jarray instead of as.vector on varNames. The R error is telling you that rJava thinks the class of the third argument is Ljava/lang/String; instead of [Ljava/lang/String;.
Related
I am trying to build my own parser based on the existing Java grammar.
Even if I use the Java7 grammar from the source repo, generate the parser and use the TestRig from antlr-4.9.3-complete.jar given the code:
1 public class Test {
2 public static void main() {
3 test
4 int b = 1;
5 }
6 }
I get the following error:
line 4:8 no viable alternative at input 'test\n int'
So for some reason it concatenates the incorrect "test" line with correct "int" line.
Also it says "line 4:8" pointing at the "int" line when it should be pointing to "test" (line 3).
(In a regular Java editor I would see a correct error highlighting for the "test" word which would sound like):
"Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration"
What do I do to arrive at a similar error with ANTLR so it only picks on the wrong "test" line?
Most likely it's just my misunderstanding how antlr interprets the errors, then how would I get the listener to at least report correctly the starting line?
You can't compare a sophisticated editor/IDE with a parser (generated by ANTLR). A text editor/IDE knows more about the input source and can look up if test is a valid type, and give a meaningful error message if the type cannot be found.
ANTLR's parser rule "sees" test int b as an Identifier, an INT and another Identifier token and cannot match any parser rule for these tokens, resulting in the error starting at the identifier test.
For example, if class test {} was in the classpath, then input without int would be valid:
public class Test {
public static void main() {
test
/*int*/ a = 1;
}
}
It wouldn't compile of course, but the syntax would be correct:
I'm using Xtext to generate a parser for my language. But after the java files are generated, i am getting errors :"The code for the static initializer is exceeding the 65535 bytes limit" in my lexer classes.
I found out it's due to one String Array being 13k lines long.
The code is automatically generated so I have no real control about how that array is used.
I tried moving it into a separate class, but the Array itself is too big for the 65k limit.
package com.intercope.analysis1.parser.antlr.internal;
public class ExternalArrays {
static final String[] DFA28_transitionS = {" {
"\11\76\2\75\2\76\1\75\22\76\1\75\1\10\1\73\1\65\3\76\1\74\1\43\1\44\1\61\1\57\1\45\1\60\1\76\1\46\2\70\1\64\2\70\1\66\1\67\3\70\1\76\1\36\1\6\1\5\1\7\2\76\1\3\1\52\1\16\1\20\1\24\1\31\1\37\1\72\1\34\2\72\1\25\1\23\1\22\1\1\1\14\1\72\1\21\1\27\1\32\1\12\1\63\1\41\3\72\3\76\1\71\1\72\1\76\1\4\1\51\1\15\1\17\1\56\1\30\1\40\1\72\1\33\2\72\1\50\1\53\1\72\1\2\1\13\1\72\1\35\1\26\1\47\1\11\1\62\1\42\3\72\1\54\1\76\1\55\uff82\76",
"\1\103\2\uffff\1\101\1\uffff\1\77\1\102\3\uffff\1\100", ...};
}
Since these are values depending on my lexer grammar I cant generate them using a function.
Any suggestions on how I can fix this is appreciated ^^
Edit: An excerpt from the array:
"\1\u0310\1\uffff\1\u0313\7\uffff\1\u0312\1\uffff\1\u030f\2\uffff\1\u0311",
"\1\u0314",
"\1\u0315",
"\1\u0316",
"\1\u0317",
"\1\u0318",
"\1\u0319",
"\12\104\7\uffff\32\104\4\uffff\1\104\1\uffff\32\104",
"\1\u031b",
"\12\104\7\uffff\32\104\4\uffff\1\104\1\uffff\32\104",
"\12\104\7\uffff\32\104\4\uffff\1\104\1\uffff\32\104",
"\1\u031e",
"\1\u031f",
"\1\u0320",
"\1\u0321",
"\1\u0322",
"\12\104\7\uffff\32\104\4\uffff\1\104\1\uffff\32\104",
"\1\u0324",
"\12\104\7\uffff\32\104\4\uffff\1\u0325\1\uffff\32\104",
"\12\104\7\uffff\32\104\4\uffff\1\104\1\uffff\32\104",
"\1\u0328",
"\1\u032c\1\uffff\1\u032b\12\uffff\1\u0329\2\uffff\1\u032a\1\uffff\1\u032d",
"\1\u032e",
"\1\u032f",
"\1\u0330",
"\1\u0331",
"\1\u0332",
"\1\u0333",
"\1\u0334",
"\1\u0335",
"\1\u0336",
I want to use synonyms () described in 'Intro to the tm package' for R. It uses the wordnet package. The wordnet package downloaded from CRAN does not have Dict (dictionary) in its directory. I downloaded it from the Princeton site and copied it over to the directory. After using sys.setenv() and setDict() for setting paths, I still get this error:
Error in sort(unique(unlist(lapply(synsets, getWord))))
error in evaluating the argument 'x' in selecting a method for function 'sort': Error in unique(unlist(lapply(synsets, getWord))) :
error in evaluating the argument 'x' in selecting a method for function 'unique': Error in .jcall(synset, "Ljava/util/List;", "getWord") :
java.lang.NumberFormatException: For input string: "t"
when I try synonyms("company", pos = "NOUN") or another English word in place of 'company'. The problem is in getSynonyms() called from synonyms(). Any idea on how to fix this problem?
Different combinations lead to different input string NumberFormatException. My Java is version 1.8. I tried all the online resources. I added two paths to PATH for R's bin and RJava's jri. Discussion on the exception indicates it is a string to numeric conversion issue. I have made sure that Java to R linkage (via rJava) works (URL: https://www.rforge.net/rJava/ ).
Im trying to add arguments to the arguments list of a MethodInvocation and it doesnt seem to work, I can remove objects but I cant see to add them.
My end goal is to take 2 MethodInvocation that invoke the same method with different arguments and convert it to 1 MethodInvocation that has a ConditionalExpression as an argument.
Example:
if (A){
System.out.println("hi");
} else {
System.out.println("hey");
}
Will be converted to:
System.out.println((A ? "hi" : "hey"));
So I would also appreciate it if someone knwos how to convert the argument list to 1 big Expression I can place in the ConditionalExpression.
Thanks!
EDIT: sorry forgot to mention is it a code formatting plug-in for ecplise
EDIT2: the code I am trying to run:
final ExpressionStatement thenStmnt=(ExpressionStatement)((Block)node.getThenStatement()).statements().get(0),
elseStmnt=(ExpressionStatement)((Block)node.getElseStatement()).statements().get(0);
MethodInvocation thenMethod=(MethodInvocation)thenStmnt.getExpression(),
elseMethod=(MethodInvocation)elseStmnt.getExpression();
final MethodInvocation method=ast.newMethodInvocation();
method.setName(ast.newSimpleName("add"));
method.arguments().add(0, elseMethod.arguments().get(0));
ast is a given leagal AST and node is a given leagal IfStatement.
Solved, problem was here:
method.arguments().add(0, elseMethod.arguments().get(0));
If you want to take or copy something that is already part of your original code, meaning already exist in the AST you have to use r.createCopyTarget, like so:
method.arguments().add(0, r.createCopyTarget(elseMethod.arguments().get(0)));
I am trying to make classes to do the following:
Get a folder, get what folder it contains, and for each folder that is in the main folder - get the files in it and move them into two groups - ones that are mp4, mkv, vlc and avi types and ones that are srt types.
My current code is:
Show.class: http://pastebin.com/vZ7ipFJc
Season.class: http://pastebin.com/tqUJ7S9v
Main.class: http://pastebin.com/LaEvuARW
The stacktrace leads me to many different places:
Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.ArrayList.toArray(Unknown Source)
at Show.<init>(Show.java:47)
at Main.getShows(Main.java:24)
at Main.setupShowsBox(Main.java:70)
at Main.<init>(Main.java:96)
at Main.main(Main.java:54)
(The lines in pastebin are the same as in code) And the problem seems to be where I convert the arraylist into an array, and I dont know what the problem is. What is the problem and how can I fix it?
So here's the line causing the error:
List<Season> seasons;
File[] seasonFile;
this.seasonsFile=seasons.toArray(new File[seasons.size()]);
You're trying to convert a list of Seasons to an array of Files. You can convert a list of Files to an array of Files, or you can convert a list of Seasons to an array of Seasons using toArray().
If you have a way of converting Season objects to File objects you could put that in a toFile() method on your Season object. Then to get your file array:
int idx = 0;
for (season : seasons) {
seasonsFile[idx] = season.toFile();
idx++;
}
You have this line 47 in Show.java:
this.seasonsFile=seasons.toArray(new File[seasons.size()])
seasons is an ArrayList of Season. ArrayList#toArray is not going to be able to squeeze a load of Seasons into an array of Files. Maybe you wanted this.seasonsFile = seasons.getShows;?
Also, you shouldn't have package private variable names following the convention of method names. It's going to confuse everyone.