Skip to content

Commit 58115d7

Browse files
committed
prepared for output table constraction with columns of any type
1 parent b790635 commit 58115d7

3 files changed

Lines changed: 45 additions & 45 deletions

File tree

RubyScript/rb/knime.rb

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,28 @@ def add_cell(cell)
2222
@cells << cell
2323
end
2424

25-
def int(val)
26-
add_cell IntCell.new(val)
27-
self
28-
end
29-
30-
def long(val)
31-
add_cell LongCell.new(val)
32-
self
33-
end
34-
35-
def string(val)
36-
add_cell StringCell.new(val)
37-
self
38-
end
39-
40-
def double(val)
41-
add_cell DoubleCell.new(val)
42-
self
43-
end
44-
45-
# workadound for RowKey class
46-
def stringCell(val)
47-
add_cell StringCell.new(val)
48-
self
49-
end
25+
# generate methods for compatibility with previous release
26+
[
27+
[:int, IntCell],
28+
[:long, LongCell],
29+
[:string, StringCell],
30+
[:double, DoubleCell],
31+
[:stringCell, StringCell] # workaround for RowKey class
32+
].each do |name, cls|
33+
Object.send(:define_method, name) do |val|
34+
add_cell cls.new(val)
35+
self
36+
end
37+
end
38+
39+
# generate an appropriate methods for any types annotated in the output model
40+
$outColumnTypes.each do |name|
41+
cls = const_get name
42+
Object.send(:define_method, name) do |val|
43+
add_cell cls.new(val)
44+
self
45+
end
46+
end
5047
end
5148

5249
class Cells
@@ -56,7 +53,7 @@ class Cells
5653
end
5754

5855
# This method allows to display any text to indicate current
59-
# calclulation progress
56+
# calculation progress
6057
def setProgress(*val)
6158
$exec.setProgress(*val)
6259
end
@@ -88,25 +85,15 @@ def snippetRunner
8885
end
8986
end
9087
end
91-
include Knime
92-
93-
# Extended knime class
94-
class DoubleCell
95-
include DataConverter
96-
end
9788

98-
# Extended knime class
99-
class IntCell
100-
include DataConverter
101-
end
89+
include Knime
10290

103-
# Extended knime class
104-
class LongCell
105-
include DataConverter
91+
[DoubleCell, IntCell, LongCell].each do |cls|
92+
cls.class_exec { include DataConverter }
10693
end
10794

10895
# Extended knime class
109-
class Java::OrgKnimeCoreDataContainer::BlobSupportDataRow
96+
class BlobSupportDataRow
11097
include CellUtility
11198

11299
# Append new columns by previously added chain of cells
@@ -130,7 +117,7 @@ def [](idx)
130117
end
131118

132119
# Extended knime class
133-
class Java::OrgKnimeCoreNode::BufferedDataTable
120+
class BufferedDataTable
134121
# Add Ruby specific methods
135122
def length
136123
getRowCount
@@ -154,7 +141,7 @@ def new_row(obj_cells = nil)
154141
end
155142

156143
# Extended knime class
157-
class Java::OrgKnimeCoreDataContainer::DataContainer
144+
class DataContainer
158145
# Add row in the data container.
159146
# Row can be copied from input data container or created.
160147
def <<(obj)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ public void actionPerformed(final ActionEvent e) {
116116

117117
TableColumn typeColumn = table.getColumnModel().getColumn(1);
118118
JComboBox<String> typeSelector = new JComboBox<String>();
119-
typeSelector.addItem("String");
120-
typeSelector.addItem("Integer");
121-
typeSelector.addItem("Double");
119+
typeSelector.addItem("StringCell");
120+
typeSelector.addItem("IntegerCell");
121+
typeSelector.addItem("DoubleCell");
122122
typeColumn.setCellEditor(new DefaultCellEditor(typeSelector));
123123

124124
// construct the panel for script loading/authoring

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.eclipse.core.runtime.Path;
1919
import org.eclipse.core.runtime.Platform;
2020
import org.knime.base.data.append.column.AppendedColumnTable;
21+
import org.knime.core.data.DataCell;
2122
import org.knime.core.data.DataColumnSpec;
2223
import org.knime.core.data.DataColumnSpecCreator;
2324
import org.knime.core.data.DataTableSpec;
@@ -306,6 +307,18 @@ protected final DataTableSpec[] configure(final DataTableSpec[] inSpecs)
306307
type = IntCell.TYPE;
307308
} else if ("Double".equals(columnType)) {
308309
type = DoubleCell.TYPE;
310+
} else {
311+
try {
312+
Class<DataCell> cls = (Class<DataCell>) Class.forName(columnType);
313+
if (cls != null)
314+
type = DataType.getType(cls);
315+
else
316+
columnType = "StringCell";
317+
318+
} catch (ClassNotFoundException e) {
319+
// e.printStackTrace();
320+
columnType = "StringCell";
321+
}
309322
}
310323
DataColumnSpec newColumn = new DataColumnSpecCreator(
311324
columnNames[i], type).createSpec();

0 commit comments

Comments
 (0)