use of org.knime.ext.sun.nodes.script.calculator.ColumnCalculator in project knime-core by knime.
the class StringManipulationVariableNodeModel method calculate.
/**
* @throws CompilationFailedException
* @throws InstantiationException
* @throws Exception
*/
private void calculate() throws InvalidSettingsException, CompilationFailedException, InstantiationException {
if (m_settings == null || m_settings.getExpression() == null) {
throw new InvalidSettingsException("No expression has been set.");
}
JavaScriptingSettings settings = m_settings.createJavaScriptingSettings();
settings.setInputAndCompile(new DataTableSpec());
// calculate the result
ColumnCalculator cc = new ColumnCalculator(settings, this);
DataCell calculate = null;
try {
calculate = cc.calculate(new DefaultRow(new RowKey(""), new DataCell[] {}));
} catch (NoSuchElementException e) {
throw new InvalidSettingsException(e.getMessage());
}
String newVariableName;
Map<String, FlowVariable> inputFlowVariables = getAvailableInputFlowVariables();
if (m_settings.isReplace()) {
newVariableName = m_settings.getColName();
CheckUtils.checkSettingNotNull(inputFlowVariables.get(newVariableName), "Can't replace input variable '%s' -- it does not exist in the input", newVariableName);
} else {
newVariableName = new UniqueNameGenerator(inputFlowVariables.keySet()).newName(m_settings.getColName());
}
// convert and push result as flow variable
CheckUtils.checkSetting(!calculate.isMissing(), "Calculation returned missing value");
Class<? extends DataCell> cellType = calculate.getClass();
if (cellType.equals(IntCell.class)) {
pushFlowVariableInt(newVariableName, ((IntCell) calculate).getIntValue());
} else if (cellType.equals(DoubleCell.class)) {
pushFlowVariableDouble(newVariableName, ((DoubleCell) calculate).getDoubleValue());
} else if (cellType.equals(StringCell.class)) {
pushFlowVariableString(newVariableName, ((StringCell) calculate).getStringValue());
} else {
throw new RuntimeException("Invalid variable class: " + cellType);
}
}
use of org.knime.ext.sun.nodes.script.calculator.ColumnCalculator in project knime-core by knime.
the class JavaIfSwitchNodeModel method calculate.
/**
*/
private int calculate() throws InvalidSettingsException {
if (m_settings == null || m_settings.getExpression() == null) {
throw new InvalidSettingsException("No expression has been set.");
}
try {
m_settings.setInputAndCompile(EMPTY_SPEC);
ColumnCalculator cc = new ColumnCalculator(m_settings, this);
DataCell result = cc.calculate(EMPTY_ROW);
if (result.isMissing()) {
throw new InvalidSettingsException("Calculation returned missing value");
}
Class<?> ret = m_settings.getReturnType();
if (!JavaSnippetIntType.INSTANCE.getJavaClass(false).equals(ret)) {
throw new InvalidSettingsException("Return type must be int: " + ret != null ? ret.getName() : "<null>");
}
Integer i = JavaSnippetIntType.INSTANCE.asJavaObject(result);
return i;
} catch (InvalidSettingsException e) {
throw e;
} catch (Exception e) {
throw new InvalidSettingsException(e.getMessage(), e);
}
}
use of org.knime.ext.sun.nodes.script.calculator.ColumnCalculator in project knime-core by knime.
the class JavaRowSplitterNodeModel method execute.
private void execute(final RowInput inData, final RowOutput[] outputs, final ExecutionContext exec) throws Exception {
DataTableSpec spec = inData.getDataTableSpec();
m_settings.setInputAndCompile(spec);
ColumnCalculator cc = new ColumnCalculator(m_settings, this);
int rowIndex = 0;
DataRow r;
RowOutput trueMatch = outputs[0];
RowOutput falseMatch = outputs.length > 1 ? outputs[1] : null;
while ((r = inData.poll()) != null) {
cc.setProgress(rowIndex, m_rowCount, r.getKey(), exec);
DataCell result = cc.calculate(r);
boolean b;
if (result.isMissing()) {
b = false;
setWarningMessage("Expression returned missing value for some rows (interpreted as no match)");
} else {
b = ((BooleanValue) result).getBooleanValue();
}
if (b) {
trueMatch.push(r);
} else if (falseMatch != null) {
falseMatch.push(r);
}
exec.checkCanceled();
rowIndex++;
}
trueMatch.close();
if (falseMatch != null) {
falseMatch.close();
}
}
use of org.knime.ext.sun.nodes.script.calculator.ColumnCalculator in project knime-core by knime.
the class JavaScriptingNodeModel method createColumnRearranger.
private ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
if (m_settings == null || m_settings.getExpression() == null) {
throw new InvalidSettingsException("No expression has been set.");
}
boolean isReplace = m_settings.isReplace();
String colName = m_settings.getColName();
try {
m_settings.setInputAndCompile(spec);
ColumnCalculator cc = new ColumnCalculator(m_settings, this);
ColumnRearranger result = new ColumnRearranger(spec);
if (isReplace) {
result.replace(cc, colName);
} else {
if (spec.containsName(colName)) {
throw new InvalidSettingsException("Can't create new column \"" + colName + "\" as input spec already contains such column");
}
result.append(cc);
}
return result;
} catch (Exception e) {
throw new InvalidSettingsException(e.getMessage(), e);
}
}
use of org.knime.ext.sun.nodes.script.calculator.ColumnCalculator in project knime-core by knime.
the class JavaEditVariableNodeModel method calculate.
/**
*/
private void calculate() throws InvalidSettingsException {
if (m_settings == null || m_settings.getExpression() == null) {
throw new InvalidSettingsException("No expression has been set.");
}
try {
m_settings.setInputAndCompile(EMPTY_SPEC);
ColumnCalculator cc = new ColumnCalculator(m_settings, this);
DataCell result = cc.calculate(EMPTY_ROW);
if (result.isMissing()) {
throw new InvalidSettingsException("Calculation returned missing value");
}
Class<?> ret = m_settings.getReturnType();
if (ret.equals(JavaSnippetIntType.INSTANCE.getJavaClass(false))) {
Integer i = JavaSnippetIntType.INSTANCE.asJavaObject(result);
pushFlowVariableInt(m_settings.getColName(), i);
} else if (ret.equals(JavaSnippetDoubleType.INSTANCE.getJavaClass(false))) {
Double d = JavaSnippetDoubleType.INSTANCE.asJavaObject(result);
pushFlowVariableDouble(m_settings.getColName(), d);
} else {
String s = JavaSnippetStringType.INSTANCE.asJavaObject(result);
pushFlowVariableString(m_settings.getColName(), s);
}
} catch (InvalidSettingsException e) {
throw e;
} catch (Exception e) {
throw new InvalidSettingsException(e.getMessage(), e);
}
}
Aggregations