Skip to content

Commit 6fd1b9b

Browse files
committed
scala: strings refactoring
1 parent b8814eb commit 6fd1b9b

1 file changed

Lines changed: 76 additions & 73 deletions

File tree

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

Lines changed: 76 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -68,44 +68,89 @@ class RubyScriptNodeModel (
6868
) extends NodeModel(numInputs, numOutputs) {
6969

7070
protected var scriptHeader: String = ""
71-
72-
protected var scriptFooter: String = ""
73-
7471
protected var script: String = ""
72+
protected var scriptFooter: String = ""
7573

7674
protected var scriptFirstLineNumber: Int = 1
7775

7876
protected var appendCols: Boolean = true
7977

8078
protected var columnNames: Array[String] = _
81-
8279
protected var columnTypes: Array[String] = _
8380

8481
class ScriptError {
85-
86-
var lineNum: Int = _
87-
var columnNum: Int = _
88-
var `type`: String = _
89-
var text: String = _
90-
var trace: String = _
91-
var msg: String = _
92-
93-
clear()
94-
95-
def clear() {
96-
lineNum = -1
97-
columnNum = -1
98-
`type` = "--UnKnown--"
99-
text = "--UnKnown--"
100-
trace = ""
101-
msg = ""
102-
}
82+
var lineNum: Int = -1
83+
var columnNum: Int = -1
84+
var `type`: String = "--UnKnown--"
85+
var text: String = "--UnKnown--"
86+
var trace: String = ""
87+
var msg: String = ""
10388
}
10489

10590
private var script_error: ScriptError = new ScriptError()
10691

10792
def getErrorData(): ScriptError = script_error
10893

94+
protected val templateFlowVar =
95+
"""#
96+
# Flow variables:
97+
# puts FlowVariableList['knime.workspace'] # reading
98+
# FlowVariableList['filename'] = '1.txt' # writing
99+
#"""
100+
101+
protected val templateSnippet =
102+
"""#
103+
# Snippet intended for operations with one row.
104+
# This code places in the special lambda function with argument named row.
105+
# The lambda function must return the row by any available for Ruby ways.
106+
#
107+
# Example script. Add new two columns with String and Int types from current row:
108+
# row << (Cells.new.string('Hi!').int(row.getCell(0).to_s.length))
109+
#
110+
# Default snippet (copy existing row):
111+
#
112+
113+
row
114+
"""
115+
116+
protected val templateScriptMultiInput =
117+
"""# Example starter script.
118+
# Add values for new two columns with String and Int types:
119+
#
120+
# count = $in_data_0.length
121+
# $in_data_0.each_with_index do |row, i|
122+
# $out_data_0 << row << (Cells.new.string('Hi!').int(row.getCell(0).to_s.length))
123+
# setProgress "#{i*100/count}%" if i%100 != 0
124+
# end
125+
#
126+
# Default script:
127+
#
128+
129+
130+
$in_data_0.each do |row|
131+
$out_data_0 << row
132+
end
133+
"""
134+
135+
protected val templateScript =
136+
"""# Example starter script.
137+
# Add values for new two columns with String and Int types:
138+
#
139+
# count = 100000
140+
# count.times do |i|
141+
# $out_data_0 << Cells.new.string('Hi!').int(rand i))
142+
# setProgress "#{i*100/count}%" if i%100 != 0
143+
# end
144+
#
145+
# Default script:
146+
#
147+
148+
149+
10.times do |i|
150+
$outContainer << Cells.new.int(i)
151+
end
152+
"""
153+
109154
var buffer = new StringBuilder()
110155

111156
buffer ++= "require PLUGIN_PATH+'/rb/knime.rb'\n"
@@ -126,65 +171,23 @@ class RubyScriptNodeModel (
126171
buffer ++= "# outContainer%d - container housing output DataTable %d\n".format(i, i + 1)
127172
}
128173

129-
buffer ++= "#\n"
130-
buffer ++= "# Flow variables:\n"
131-
buffer ++= "# puts FlowVariableList['knime.workspace'] # reading \n"
132-
buffer ++= "# FlowVariableList['filename'] = '1.txt' # writing \n"
133-
buffer ++= "#\n#\n"
174+
buffer ++= templateFlowVar
134175

135176
if (snippetMode) {
136-
buffer ++= "# Snippet intended for operations with one row.\n"
137-
buffer ++= "# This code places in the special lambda function with argument named row.\n"
138-
buffer ++= "# The lambda function must return the row by any available for Ruby ways.\n"
139-
buffer ++= "#\n" + "# Example script. "
140-
buffer ++= "Add new two columns with String and Int types from current row:\n"
141-
buffer ++= "# row << (Cells.new.string('Hi!').int(row.getCell(0).to_s.length))\n"
142-
buffer ++= "#\n"
143-
buffer ++= "# Default snippet (copy existing row):\n"
144-
buffer ++= "#\n\n"
145-
buffer ++= " row"
177+
buffer ++= templateSnippet
146178
} else {
147179
if (numInputs > 0) {
148-
buffer ++= "# Example starter script. "
149-
buffer ++= "Add values for new two columns with String and Int types:\n"
150-
buffer ++= "#\n"
151-
buffer ++= "# count = $in_data_0.length\n"
152-
buffer ++= "# $in_data_0.each_with_index do |row, i|\n"
153-
buffer ++= "# $out_data_0 << " +
154-
buffer ++= "row << (Cells.new.string('Hi!').int(row.getCell(0).to_s.length))\n"
155-
buffer ++= "# setProgress \"#{i*100/count}%\" if i%100 != 0\n"
156-
buffer ++= "# end\n"
157-
buffer ++= "#\n"
158-
buffer ++= "# Default script:\n"
159-
buffer ++= "#\n\n"
160-
buffer ++= "$in_data_0.each do |row|\n"
161-
buffer ++= " $out_data_0 << row\n"
162-
buffer ++= "end"
180+
buffer ++= templateScriptMultiInput
163181
} else {
164-
buffer ++= "# Example starter script. "
165-
buffer ++= "Add values for new two columns with String and Int types:\n"
166-
buffer ++= "#\n"
167-
buffer ++= "# count = 100000\n"
168-
buffer ++= "# count.times do |i|\n"
169-
buffer ++= "# $out_data_0 << Cells.new.string('Hi!').int(rand i))\n"
170-
buffer ++= "# setProgress \"#{i*100/count}%\" if i%100 != 0\n"
171-
buffer ++= "# end\n"
172-
buffer ++= "#\n"
173-
buffer ++= "# Default script:\n"
174-
buffer ++= "#\n\n"
175-
buffer ++= "10.times do |i|\n"
176-
buffer ++= " $outContainer << Cells.new.int(i)\n"
177-
buffer ++= "end"
182+
buffer ++= templateScript
178183
}
179184
}
180-
181185
script = buffer.toString()
182186

183187
if (snippetMode) {
184-
buffer = new StringBuilder()
185-
buffer ++= "end\n"
186-
buffer ++= "snippet_runner &func\n"
187-
scriptFooter = buffer.toString
188+
scriptFooter =
189+
"end\n" +
190+
"snippet_runner &func\n"
188191
}
189192

190193
override protected def execute(inData: Array[BufferedDataTable], exec: ExecutionContext): Array[BufferedDataTable] = {
@@ -260,7 +263,7 @@ class RubyScriptNodeModel (
260263
container.put("PLUGIN_PATH", rubyPluginPath)
261264
val script_fn = "node_script.rb"
262265
try {
263-
script_error.clear()
266+
script_error = new ScriptError()
264267
container.setScriptFilename(script_fn)
265268
val unit = container.parse(scriptHeader + script + scriptFooter, -scriptFirstLineNumber)
266269
unit.run()
@@ -341,7 +344,7 @@ class RubyScriptNodeModel (
341344
appendCols = settings.getBoolean(APPEND_COLS, true)
342345
columnNames = settings.getStringArray(COLUMN_NAMES)
343346
columnTypes = settings.getStringArray(COLUMN_TYPES)
344-
script_error.clear()
347+
script_error = new ScriptError()
345348
}
346349

347350
protected def validateSettings(settings: NodeSettingsRO) {

0 commit comments

Comments
 (0)