How to write java code in KNIME?

I want to write and implement a java code in knime program

how can I do this?

and how I relate a result of a node performed in knime with my code?

e.g. I made association on a data set and i want the result to be an input to my code, how can i do this?

Thanks in advance

Hi Singing Bird,

there are multiple ways to achieve your goals.

1. External Tool Node: Allows you to run external executables and feed the table into them

2. Java Snippet: Allows you to write code in java in the node which is evaluated on a row-by-row basis.

3. Write your own KNIME node: https://tech.knime.org/developers

Hope this helps,

Christian

Thank you so much

the java code gives me error, I  don't know why

the node cannot be executed

Do you have a java code for suffix tree clustering algorithm?

the one i searched for gives me errors

Hi singing bird,

could you please explain what exactly you tried? Christian has given several options and it is not clear which option you have chosen.

Please also provide any Java code you have written. Otherwise it is hard to tell what went wrong.

Hi Knot,

this is the code i tried to implement

  1. Java Program to Implement Suffix Tree
  2.  */
  3.  





  4. import java.io.*;



  5.  





  6. /** Class Node **/




  7. class Node




  8. {




  9. public int suffix_node;




  10. public static int Count = 1;



  11.  





  12. /** Constructor **/




  13. public Node()




  14. {




  15. suffix_node = -1;




  16. }




  17. }



  18.  





  19. /** Class Suffix Tree **/




  20. class SuffixTree




  21. {




  22. private static final int MAX_LENGTH = 1000;




  23. private static final int HASH_TABLE_SIZE = 2179;



  24.  





  25. private char[] T = new char[ MAX_LENGTH ];




  26. private int N;




  27. private Edge[] Edges ;




  28. private Node[] Nodes ;



  29.  





  30. private Suffix active;



  31.  





  32. /** Class Suffix **/




  33. class Suffix




  34. {




  35. public int origin_node;




  36. public int first_char_index;




  37. public int last_char_index;



  38.  





  39. /** Constructor **/




  40. public Suffix(int node, int start, int stop )




  41. {




  42. origin_node = node ;




  43. first_char_index = start ;




  44. last_char_index = stop;




  45. }



  46.  





  47. /** Function Implicit **/




  48. public boolean Implicit()




  49. {




  50. return first_char_index > last_char_index;




  51. }



  52.  





  53. /** Function Explicit **/




  54. public boolean Explicit()




  55. {




  56. return first_char_index > last_char_index;




  57. }



  58.  





  59. /** Function Canonize()




  60.   * A suffix in the tree is denoted by a Suffix structure




  61.   * that denotes its last character. The canonical




  62.   * representation of a suffix for this algorithm requires




  63.   * that the origin_node by the closest node to the end




  64.   * of the tree. To force this to be true, we have to




  65.   * slide down every edge in our current path until we




  66.   * reach the final node




  67.   **/




  68. public void Canonize()




  69. {




  70. if (!Explicit() )




  71. {




  72. Edge edge = Find( origin_node, T[ first_char_index ] );




  73. int edge_span = edge.last_char_index - edge.first_char_index;



  74.  





  75. while ( edge_span <= ( last_char_index - first_char_index ) )




  76. {




  77. first_char_index = first_char_index + edge_span + 1;




  78. origin_node = edge.end_node;




  79. if ( first_char_index <= last_char_index )




  80. {




  81. edge = Find( edge.end_node, T[ first_char_index ] );




  82. edge_span = edge.last_char_index - edge.first_char_index;




  83. }




  84. }




  85. }




  86. }




  87. }



  88.  





  89. /** Class Edge **/




  90. class Edge




  91. {




  92. public int first_char_index;




  93. public int last_char_index;




  94. public int end_node;




  95. public int start_node;



  96.  





  97. /** Constructor **/




  98. public Edge()




  99. {




  100. start_node = -1;




  101. }



  102.  





  103. /** Constructor **/




  104. public Edge( int init_first, int init_last, int parent_node )




  105. {




  106. first_char_index = init_first;




  107. last_char_index = init_last;




  108. start_node = parent_node;




  109. end_node = Node.Count++;




  110. }



  111.  





  112. /** function Insert ()




  113.   * A given edge gets a copy of itself inserted into the table




  114.   * with this function. It uses a linear probe technique, which




  115.   * means in the case of a collision, we just step forward through




  116.   * the table until we find the first unused slot.




  117.   **/




  118. public void Insert()




  119. {




  120. int i = Hash( start_node, T[ first_char_index ] );




  121. while ( Edges[ i ].start_node != -1 )




  122. i = ++i % HASH_TABLE_SIZE;




  123. Edges[ i ] = this;




  124. }



  125.  





  126. /** function SplitEdge ()




  127.   * This function is called




  128.   * to split an edge at the point defined by the Suffix argument




  129.   **/




  130. public int SplitEdge( Suffix s )




  131. {




  132. Remove();




  133. Edge new_edge = new Edge( first_char_index, first_char_index + s.last_char_index - s.first_char_index, s.origin_node );




  134. new_edge.Insert();




  135. Nodes[ new_edge.end_node ].suffix_node = s.origin_node;




  136. first_char_index += s.last_char_index - s.first_char_index + 1;




  137. start_node = new_edge.end_node;




  138. Insert();




  139. return new_edge.end_node;




  140. }



  141.  





  142. /** function Remove ()




  143.   * This function is called to remove an edge from hash table




  144.   **/




  145. public void Remove()




  146. {




  147. int i = Hash( start_node, T[ first_char_index ] );




  148. while ( Edges[ i ].start_node != start_node ||




  149. Edges[ i ].first_char_index != first_char_index )




  150. i = ++i % HASH_TABLE_SIZE;




  151. for ( ; ; )




  152. {




  153. Edges[ i ].start_node = -1;




  154. int j = i;




  155. for ( ; ; )




  156. {




  157. i = ++i % HASH_TABLE_SIZE;




  158. if ( Edges[ i ].start_node == -1 )




  159. return;




  160. int r = Hash( Edges[ i ].start_node, T[ Edges[ i ].first_char_index ] );




  161. if ( i >= r && r > j )




  162. continue;




  163. if ( r > j && j > i )




  164. continue;




  165. if ( j > i && i >= r )




  166. continue;




  167. break;




  168. }




  169. Edges[ j ] = Edges[ i ];




  170. }




  171. }




  172. }



  173.  





  174. /** Constructor */




  175. public SuffixTree()




  176. {




  177. Edges = new Edge[ HASH_TABLE_SIZE ];




  178. for (int i = 0; i < HASH_TABLE_SIZE; i++)




  179. Edges[i] = new Edge();




  180. Nodes = new Node[ MAX_LENGTH * 2 ];




  181. for (int i = 0; i < MAX_LENGTH * 2 ; i++)




  182. Nodes[i] = new Node();




  183. active = new Suffix( 0, 0, -1 );




  184. }



  185.  





  186. /** Function Find() - function to find an edge **/




  187. public Edge Find( int node, int c )




  188. {




  189. int i = Hash( node, c );




  190. for ( ; ; )




  191. {




  192. if ( Edges[ i ].start_node == node )




  193. if ( c == T[ Edges[ i ].first_char_index ] )




  194. return Edges[ i ];




  195. if ( Edges[ i ].start_node == -1 )




  196. return Edges[ i ];




  197. i = ++i % HASH_TABLE_SIZE;




  198. }




  199. }



  200.  





  201. /** Function Hash() - edges are inserted into the hash table using this hashing function **/




  202. public static int Hash( int node, int c )




  203. {




  204. return (( node << 8 ) + c ) % HASH_TABLE_SIZE;




  205. }



  206.  





  207. /** Function AddPrefix() - called repetitively, once for each of the prefixes of the input string **/




  208. public void AddPrefix( Suffix active, int last_char_index )




  209. {




  210. int parent_node;




  211. int last_parent_node = -1;



  212.  





  213. for ( ; ; )




  214. {




  215. Edge edge;




  216. parent_node = active.origin_node;



  217.  





  218. if ( active.Explicit() )




  219. {




  220. edge = Find( active.origin_node, T[ last_char_index ] );




  221. if ( edge.start_node != -1 )




  222. break;




  223. }




  224. else




  225. {




  226. edge = Find( active.origin_node, T[ active.first_char_index ] );




  227. int span = active.last_char_index - active.first_char_index;




  228. if ( T[ edge.first_char_index + span + 1 ] == T[ last_char_index ] )




  229. break;




  230. parent_node = edge.SplitEdge( active );




  231. }



  232.  





  233. Edge new_edge = new Edge( last_char_index, N, parent_node );




  234. new_edge.Insert();




  235. if ( last_parent_node > 0 )




  236. Nodes[ last_parent_node ].suffix_node = parent_node;




  237. last_parent_node = parent_node;



  238.  





  239. if ( active.origin_node == 0 )




  240. active.first_char_index++;




  241. else




  242. active.origin_node = Nodes[ active.origin_node ].suffix_node;




  243. active.Canonize();




  244. }




  245. if ( last_parent_node > 0 )




  246. Nodes[ last_parent_node ].suffix_node = parent_node;




  247. active.last_char_index++;




  248. active.Canonize();




  249. }



  250.  





  251. /** Function to print all contents and details of suffix tree **/




  252. public void dump_edges(int current_n )




  253. {




  254. System.out.println(" Start End Suf First Last String\n");




  255. for ( int j = 0 ; j < HASH_TABLE_SIZE ; j++ )




  256. {




  257. Edge s = Edges[j];




  258. if ( s.start_node == -1 )




  259. continue;




  260. System.out.printf("%5d %5d %3d %5d %6d ", s.start_node , s.end_node, Nodes[ s.end_node ].suffix_node, s.first_char_index, s.last_char_index);



  261.  





  262. int top;




  263. if ( current_n > s.last_char_index )




  264. top = s.last_char_index;




  265. else




  266. top = current_n;




  267. for ( int l = s.first_char_index ; l <= top; l++)




  268. System.out.print( T[ l ]);




  269. System.out.println();




  270. }




  271. }




  272. /** Main Function **/




  273. public static void main(String[] args) throws IOException




  274. {




  275. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));



  276.  





  277. System.out.println(“Suffix Tree Test\n”);




  278. System.out.println(“Enter string\n”);




  279. String str = br.readLine();



  280.  





  281. /** Construct Suffix Tree **/




  282. SuffixTree st = new SuffixTree();




  283. st.T = str.toCharArray();




  284. st.N = st.T.length - 1;



  285.  





  286. for (int i = 0 ; i <= st.N ; i++ )




  287. st.AddPrefix( st.active, i );



  288.  





  289. st.dump_edges( st.N );




  290. }




  291. }

 

This code is for suffix tree clustering technique

It is from the internet

but I want the input to be a node previously implemented in KNIME (I don't want the input or the data to be clustered using this algorithm to be put inside the code, instead I want it from a node already implemented in KNIME)

How can I do this? 

I tried java snippet node but errors are released

What can I do to implement this code with an implemented  node as input?

It is not possible to define Java Classes within the KNIME Java snippets but the code above tries exactly that. You can only define methods in the snippets. In general KNIME should be used in such a way that Java coding is not necessary (but it is not always avoidable).

 

If you still want to use the code, you will need to program your own node in Java. Do you already know Java programming? If yes, then have a look at this page: https://tech.knime.org/developers

Otherwise you might want to check out the external tool node which allows you to call scripts on your system: https://www.youtube.com/watch?v=YTPDBTQUtzw