Skip to content

Commit 0989bb3

Browse files
committed
prepared for using flow variables. Example: FlowVariableList['filename1'] = 'test.txt'
1 parent ba263e4 commit 0989bb3

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

RubyScript/rb/knime.rb

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
java_import org.knime.core.data.container.BlobSupportDataRow
1616
java_import org.knime.core.data.DataCell
1717

18+
java_import org.knime.core.node.workflow.FlowVariable
19+
1820
# This module contains utility methods and classes
1921
# for convinient Ruby script writing for KNIME
2022
# See also https://tech.knime.org/javadoc-api
@@ -55,7 +57,7 @@ def add_cell(cell)
5557
add_cell rb_cls.new(val)
5658
self
5759
end
58-
end
60+
end
5961
end
6062

6163
# Instances of this class are intended for a columns container.
@@ -120,14 +122,42 @@ def [](idx)
120122
end
121123
end
122124

125+
# utility class for access to flow variables
126+
class FlowVariableList
127+
def self.[](name)
128+
@vars ||= $node.getAvailableFlowVariables
129+
if (flowvar = @vars[name])
130+
case flowvar.getType
131+
when FlowVariable::Type::DOUBLE
132+
flowvar.getDoubleValue
133+
when FlowVariable::Type::INTEGER
134+
flowvar.getIntValue
135+
when FlowVariable::Type::STRING
136+
flowvar.getStringValue
137+
end
138+
end
139+
end
140+
141+
def self.[]=(name, val)
142+
case val.class
143+
when Integer
144+
$node.pushFlowVariableInteger(name, val)
145+
when Float
146+
$node.pushFlowVariableDouble(name, val)
147+
else
148+
$node.pushFlowVariableString(name, val.to_s)
149+
end
150+
end
151+
end
152+
123153
def snippet_runner
124154
count, step = $in_data_0.length, 0x2FF
125155
coef = step / count.to_f
126156
$in_data_0.each_with_index do |row, i|
127157
$out_data_0 << (yield row)
128158
setProgress "#{i * coef}%" if (i & step) == 0
129159
end
130-
end
160+
end
131161
end
132162

133163
include Knime

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ protected final BufferedDataTable[] execute(final BufferedDataTable[] inData,
316316
container.put("$num_outputs", m_numOutputs);
317317

318318
container.put("$exec", exec);
319+
container.put("$node", this);
319320
container.put("PLUGIN_PATH", rubyPluginPath);
320321
}
321322

0 commit comments

Comments
 (0)