Skip to content

Commit a7012e1

Browse files
committed
scala: loops optimizations
1 parent 414f501 commit a7012e1

3 files changed

Lines changed: 42 additions & 52 deletions

File tree

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,10 @@ class RubyScriptNodeDialog(private var factory: RubyScriptNodeFactory)
8989
val model = table.getModel
9090
.asInstanceOf[ScriptNodeOutputColumnsTableModel]
9191
val columns = model.getDataTableColumnNames
92-
var found: Boolean = false
9392
do {
94-
found = false
9593
name = "script output " + columnCounter
9694
columnCounter += 1
97-
for (s <- columns if name == s) {
98-
found = true
99-
//break
100-
}
101-
} while (found);
95+
} while (columns.indexWhere(_ == name) >= 0);
10296
model.addRow(name, "String")
10397
}
10498
})

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

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class RubyScriptNodeModel (
8383
class ScriptError {
8484
var lineNum: Int = -1
8585
var columnNum: Int = -1
86-
var `type`: String = "--UnKnown--"
86+
var errType: String = "--UnKnown--"
8787
var text: String = "--UnKnown--"
8888
var trace: String = ""
8989
var msg: String = ""
@@ -258,18 +258,14 @@ end
258258
container.setError(new LoggerOutputStream(logger, NodeLogger.LEVEL.ERROR))
259259
container.put("$num_inputs", numInputs)
260260
container.put("$input_datatable_arr", inData)
261-
i = 0
262-
while (i < numInputs) {
261+
for (i <- 0 until numInputs) {
263262
container.put("$inData%d".format(i), inData(i))
264263
container.put("$in_data_%d".format(i), inData(i))
265-
i += 1
266264
}
267265
container.put("$output_datatable_arr", outContainer)
268-
i = 0
269-
while (i < numOutputs) {
266+
for (i <- 0 until numOutputs) {
270267
container.put("$outContainer%d".format(i), outContainer(i))
271268
container.put("$out_data_%d".format(i), outContainer(i))
272-
i += 1
273269
}
274270
container.put("$outContainer", outContainer(0))
275271
container.put("$outColumnNames", columnNames)
@@ -341,14 +337,9 @@ end
341337
val newColumn = new DataColumnSpecCreator(columnNames(i), newColumnType).createSpec()
342338
newSpec = AppendedColumnTable.getTableSpec(newSpec, newColumn)
343339
}
344-
if (script == null) {
345-
script = ""
346-
}
347-
val result = Array.ofDim[DataTableSpec](numOutputs)
348-
for (i <- 0 until numOutputs) {
349-
result(i) = newSpec
350-
}
351-
result
340+
341+
val result = for (i <- 0 until numOutputs) yield { newSpec }
342+
result.toArray
352343
}
353344

354345
protected override def loadInternals(nodeInternDir: File, exec: ExecutionMonitor) {
@@ -399,7 +390,8 @@ end
399390
logger.debug("SyntaxError: " + script_error.text)
400391
script_error.lineNum = line.toInt
401392
script_error.columnNum = -1
402-
script_error.`type` = "SyntaxError"
393+
script_error.errType = "SyntaxError"
394+
case _ =>
403395
}
404396

405397
} else {
@@ -408,24 +400,26 @@ end
408400
// if (mLine.find()) {
409401
// script_error.`type` = mLine.group(1)
410402
// }
411-
script_error.`type` = """(?<=\()(\w*)""".r
412-
.findFirstMatchIn(err).map(_ group 2).getOrElse(script_error.`type`)
403+
script_error.errType = """(?<=\()(\w*)""".r
404+
.findFirstMatchIn(err).map(_ group 2).getOrElse(script_error.errType)
413405

414406
val cause = thr.getCause
415-
for (line <- cause.getStackTrace if line.getFileName == filename) {
416-
script_error.text = cause.getMessage
417-
script_error.columnNum = -1
418-
script_error.lineNum = line.getLineNumber
419-
script_error.text = thr.getMessage
420-
// val knimeType = Pattern.compile("(?<=org.knime.)(.*)(?=:)")
421-
// val mKnimeType = knimeType.matcher(script_error.text)
422-
// script_error.`type` =
423-
// if (mKnimeType.find()) mKnimeType.group(1) else "RuntimeError"
424-
425-
script_error.`type` = """(?<=org.knime.)(.*)(?=:)""".r
426-
.findFirstMatchIn(err).map(_ group 1).getOrElse("RuntimeError")
427-
//break
428-
}
407+
cause.getStackTrace
408+
.filter(line => line.getFileName == filename)
409+
.map(line => {
410+
script_error.text = cause.getMessage
411+
script_error.columnNum = -1
412+
script_error.lineNum = line.getLineNumber
413+
script_error.text = thr.getMessage
414+
// val knimeType = Pattern.compile("(?<=org.knime.)(.*)(?=:)")
415+
// val mKnimeType = knimeType.matcher(script_error.text)
416+
// script_error.`type` =
417+
// if (mKnimeType.find()) mKnimeType.group(1) else "RuntimeError"
418+
419+
script_error.errType = """(?<=org.knime.)(.*)(?=:)""".r
420+
.findFirstMatchIn(err).map(_ group 1).getOrElse("RuntimeError")
421+
//break
422+
}).head
429423
}
430424
script_error.msg = "script" + (script_error.lineNum match {
431425
case -1 => "] stopped with error at line --unknown--"
@@ -442,8 +436,8 @@ end
442436
// } else {
443437
// script_error.msg += "] stopped with error at line --unknown--"
444438
// }
445-
if (script_error.`type` == "RuntimeError") {
446-
logger.error(script_error.msg + "\n" + script_error.`type` + " ( " + script_error.text + " )")
439+
if (script_error.errType == "RuntimeError") {
440+
logger.error(script_error.msg + "\n" + script_error.errType + " ( " + script_error.text + " )")
447441
val cause = thr.getCause
448442
val stack = cause.getStackTrace
449443
val builder = new StringBuilder()
@@ -465,7 +459,7 @@ end
465459
script_error.trace +
466460
"--- Traceback --- end --------------")
467461
}
468-
} else if (script_error.`type` != "SyntaxError") {
462+
} else if (script_error.errType != "SyntaxError") {
469463
logger.error(script_error.msg)
470464
logger.error("Could not evaluate error source nor reason. Analyze StackTrace!")
471465
logger.error(err)

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ScriptNodeOutputColumnsTableModel extends AbstractTableModel {
1414

1515
private var m_readOnly: Boolean = false
1616

17-
def getColumnName(col: Int): String = columnNames.get(col).toString
17+
override def getColumnName(col: Int): String = columnNames.get(col).toString
1818

1919
def getRowCount(): Int = data.size
2020

@@ -25,13 +25,13 @@ class ScriptNodeOutputColumnsTableModel extends AbstractTableModel {
2525
rowList.get(col).asInstanceOf[AnyRef]
2626
}
2727

28-
def isCellEditable(row: Int, col: Int): Boolean = !m_readOnly
28+
override def isCellEditable(row: Int, col: Int): Boolean = !m_readOnly
2929

3030
def setReadOnly(readOnly: Boolean) {
3131
m_readOnly = readOnly
3232
}
3333

34-
def setValueAt(value: AnyRef, row: Int, col: Int) {
34+
override def setValueAt(value: AnyRef, row: Int, col: Int) {
3535
val rowList = data.get(row)
3636
rowList.set(col, value)
3737
fireTableCellUpdated(row, col)
@@ -60,13 +60,15 @@ class ScriptNodeOutputColumnsTableModel extends AbstractTableModel {
6060
def getDataTableColumnTypes(): Array[String] = getDataTableValues(1)
6161

6262
private def getDataTableValues(colIndex: Int): Array[String] = {
63-
val dataTableColumnValues = Array.ofDim[String](data.size)
64-
var rowNum = 0
65-
for (row <- data) {
66-
dataTableColumnValues(rowNum) = row.get(colIndex).asInstanceOf[String]
67-
rowNum += 1
68-
}
69-
dataTableColumnValues
63+
// val dataTableColumnValues = Array.ofDim[String](data.size)
64+
// var rowNum = 0
65+
// for (row <- data) {
66+
// dataTableColumnValues(rowNum) = row.get(colIndex).asInstanceOf[String]
67+
// rowNum += 1
68+
// }
69+
// dataTableColumnValues
70+
71+
data.map(_.get(colIndex).asInstanceOf[String]).toArray
7072
}
7173

7274
def clearRows() {

0 commit comments

Comments
 (0)