hi,
i've been trying to use the Lucene StandardTokenizer in Knime, via the Java Snippet, and cannot get around an exception that complains about the Version enum.
The code is as follows, and interesting lines are 13,36-47:
// system imports import org.knime.base.node.jsnippet.expression.AbstractJSnippet; import org.knime.base.node.jsnippet.expression.Abort; import org.knime.base.node.jsnippet.expression.Cell; import org.knime.base.node.jsnippet.expression.ColumnException; import org.knime.base.node.jsnippet.expression.TypeException; import static org.knime.base.node.jsnippet.expression.Type.*; import java.util.Date; import java.util.Calendar; import org.w3c.dom.Document; // Your custom imports: import org.apache.lucene.util.Version; import java.io.StringReader; import org.apache.lucene.analysis.standard.StandardTokenizer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import java.io.IOException; // system variables public class JSnippet extends AbstractJSnippet { // Fields for input columns /** Input column: "Term" */ public String c_Term; // Fields for output columns /** Output column: "tokenized" */ public String[] out_tokenized; // Your custom variables: // expression start public void snippet() throws TypeException, ColumnException, Abort { // Enter your code here: StringReader sr = new StringReader(c_Term); Version v = null; try{ //1st attempt //v=Version.LUCENE_47; //2nd attempt v= Version.parseLeniently("Lucene_47"); //3rd attempt //Version[] vs = Version.values(); }catch(Exception e){ logError(e.getMessage()); throw new RuntimeException(e); } StandardTokenizer st = new StandardTokenizer(v,sr); String[] tokens = new String[20]; int i =0; try{ while (st.incrementToken() && i<20) { CharTermAttribute termAttribute = st.getAttribute(CharTermAttribute.class); tokens[i++] = termAttribute.toString(); } }catch(IOException ioex){ ioex.printStackTrace(); } //return new String[]{"a"}; out_tokenized = tokens; // expression end } }
The errors I get are:
1st attempt:
WARN JavaSnippetCellFactory Evaluation of java snippet failed for row "Row194". The exception is caused by line 36 of the snippet. Exception message:LUCENE_47
2nd attempt:
WARN JavaSnippetCellFactory Evaluation of java snippet failed for row "Row217". The exception is caused by line 36 of the snippet. Exception message:org.apache.lucene.util.Version.parseLeniently(Ljava/lang/String;)Lorg/apache/lucene/util/Version;
3rd attempt:
WARN JavaSnippetCellFactory Evaluation of java snippet failed for row "Row233". The exception is caused by line 36 of the snippet. Exception message:org.apache.lucene.util.Version.values()[Lorg/apache/lucene/util/Version;
I am using the JRE that came with Knime, 1.7.0_60 (confirmed via System.getProperty("java.version") in the code of the java snippet). The required libraries are loaded in the "Additional Libraries" tab and the code autocompletes fine. The environment is OSX with default java 1.8
Looking in the logs, i see that it is unable to find the fields and methods of the enum type Version:
2014-10-28 18:59:04,777 DEBUG KNIME-WFM-Parent-Notifier NodeContainer : ROOT has new state: EXECUTING 2014-10-28 18:59:04,777 DEBUG KNIME-Worker-74 WorkflowManager : Java Snippet 2:6 doBeforePreExecution 2014-10-28 18:59:04,777 DEBUG KNIME-Worker-74 NodeContainer : Java Snippet 2:6 has new state: PREEXECUTE 2014-10-28 18:59:04,777 DEBUG KNIME-Worker-74 WorkflowManager : Java Snippet 2:6 doBeforeExecution 2014-10-28 18:59:04,777 DEBUG KNIME-Worker-74 NodeContainer : Java Snippet 2:6 has new state: EXECUTING 2014-10-28 18:59:04,777 DEBUG KNIME-Worker-74 LocalNodeExecutionJob : Java Snippet 2:6 Start execute 2014-10-28 18:59:04,778 DEBUG KNIME-Worker-74 WorkflowFileStoreHandlerRepository : Adding handler 95fbd547-a7d9-4c91-a803-a950a6545a10 (Java Snippet 2:6: <no directory>) - 3 in total 2014-10-28 18:59:04,863 WARN KNIME-Worker-74 JavaSnippetCellFactory : Evaluation of java snippet failed for row "Row0". The exception is caused by line 41 of the snippet. Exception message:org.apache.lucene.util.Version.parseLeniently(Ljava/lang/String;)Lorg/apache/lucene/util/Version; 2014-10-28 18:59:04,863 DEBUG KNIME-Worker-74 JavaSnippetCellFactory : Evaluation of java snippet failed for row "Row0". The exception is caused by line 41 of the snippet. Exception message:org.apache.lucene.util.Version.parseLeniently(Ljava/lang/String;)Lorg/apache/lucene/util/Version; java.lang.NoSuchMethodError: org.apache.lucene.util.Version.parseLeniently(Ljava/lang/String;)Lorg/apache/lucene/util/Version;
and
2014-10-28 18:56:18,075 WARN KNIME-Worker-73 JavaSnippetCellFactory : Evaluation of java snippet failed for row "Row0". The exception is caused by line 39 of the snippet. Exception message:LUCENE_47 2014-10-28 18:56:18,075 DEBUG KNIME-Worker-73 JavaSnippetCellFactory : Evaluation of java snippet failed for row "Row0". The exception is caused by line 39 of the snippet. Exception message:LUCENE_47 java.lang.NoSuchFieldError: LUCENE_47
The problem is of course - how can I understand why it is not seeing the fields/methods? It clearly compiles ok and it seems it is attempting to use the correct Version enum.
As can be seen from the code, i've tried to catch the exception, but it's apparently handled before it can be cought - the try-catch code around it is useless.
Any help would be greatly appreciated.