Skip to content

Commit b32d324

Browse files
committed
rb: added possibility to access to columns by name. Dynamic methods with the same names are generated
1 parent fdb286a commit b32d324

2 files changed

Lines changed: 40 additions & 9 deletions

File tree

RubyScript/rb/knime.rb

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
# for convinient Ruby script writing for KNIME
2020
# See also https://tech.knime.org/javadoc-api
2121
module Knime
22+
23+
private # hide methods from subclasses of Object class
24+
2225
# This module defines a container for columns for using it in
2326
# style Cell.new.StringCell('str').IntegerCell(123).DoubleCell()...
2427
module CellUtility
@@ -86,6 +89,37 @@ def to_l
8689
end
8790
end
8891

92+
# module for extending DataRow implementation classes.
93+
module CellAccessor
94+
# Get cells by index in Ruby style
95+
def [](idx)
96+
if idx >= 0
97+
getCell(idx)
98+
else
99+
getCell(getNumCells + idx) # -1 - last element
100+
end
101+
end
102+
103+
# Generate dynamic methods for accessing to cells by column name
104+
# from BlobSupportDataRow.
105+
# All names are translated in low case. All symbols except :word: are
106+
# changed to underline symbol.
107+
# For input 0 only is generated simple names. For all inputs methods has
108+
# a following format: i#{input_num}_translated_column_name
109+
#
110+
$num_inputs ||= 0
111+
(0...$num_inputs).each do |i|
112+
table = $input_datatable_arr[i]
113+
col_names = table.getDataTableSpec.getColumnNames.map do |str|
114+
str.downcase.gsub(/[^[[:word:]]]/, '_').gsub(/\_+/, '_').chomp('_')
115+
end
116+
col_names.each_with_index do |name, num|
117+
define_method(name) { getCell(num) } if num == 0
118+
define_method("i#{num}_#{name}") { getCell(num) }
119+
end
120+
end
121+
end
122+
89123
def snippet_runner
90124
count, step = $inData0.length, 0x2FF
91125
coef = step / count.to_f
@@ -105,6 +139,7 @@ def snippet_runner
105139
# Extended knime class
106140
class BlobSupportDataRow
107141
include CellUtility
142+
include CellAccessor
108143

109144
# Append new columns by previously added chain of cells
110145
def append
@@ -115,15 +150,6 @@ def append
115150
def <<(cells)
116151
AppendedColumnRow.new(self, *cells.cells)
117152
end
118-
119-
# Get cells by index in Ruby style
120-
def [](idx)
121-
if idx >= 0
122-
getCell(idx)
123-
else
124-
getCell(getNumCells + idx) # -1 - last element
125-
end
126-
end
127153
end
128154

129155
# Extended knime class

RubyScript/src/org/knime/ext/jruby/RubyScriptNodeModel.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ protected final BufferedDataTable[] execute(final BufferedDataTable[] inData,
291291
container.setError(new LoggerOutputStream(logger,
292292
NodeLogger.LEVEL.ERROR));
293293

294+
container.put("$num_inputs", numInputs);
295+
container.put("$input_datatable_arr", inData);
296+
294297
for (i = 0; i < numInputs; i++) {
295298
container.put(String.format("$inData%d", i), in[i]);
296299
}
@@ -302,6 +305,8 @@ protected final BufferedDataTable[] execute(final BufferedDataTable[] inData,
302305

303306
container.put("$outColumnNames", columnNames);
304307
container.put("$outColumnTypes", columnTypes);
308+
container.put("$num_outputs", numOutputs);
309+
305310
container.put("$exec", exec);
306311
container.put("PLUGIN_PATH", rubyPluginPath);
307312
String script_fn = "node_script.rb";

0 commit comments

Comments
 (0)