use of org.knime.core.data.DataCell in project knime-core by knime.
the class MovingAggregationTableFactory method getBackwardTable.
private BufferedDataTable getBackwardTable(final ExecutionMonitor exec, final BufferedDataTable table, final BufferedDataContainer dc) throws CanceledExecutionException {
final int rowCount = table.getRowCount();
final LinkedList<DataRow> window = new LinkedList<>();
int rowIdx = 0;
for (final DataRow row : table) {
exec.setProgress(rowIdx / (double) rowCount, "Processing row " + rowIdx++ + " of " + rowCount);
exec.checkCanceled();
window.add(row);
final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
int idx = 0;
// handle the retained columns
for (final int colIdx : m_cols2KeepIdxs) {
cells[idx++] = row.getCell(colIdx);
}
final boolean windowFull = window.size() >= m_windowLength;
for (int i = 0, length = m_ops.length; i < length; i++) {
if (windowFull || m_handleMissings) {
final int colIdx = m_aggrColIdxs[i];
final AggregationOperator op = m_ops[i];
for (final DataRow windowRow : window) {
op.compute(windowRow, colIdx);
}
cells[idx++] = op.getResult();
op.reset();
} else {
// the window is not yet full return missing cells
cells[idx++] = DataType.getMissingCell();
}
}
if (windowFull) {
// remove the first row only when the window is full
// not during the missing value handling phase!
window.removeFirst();
}
dc.addRowToTable(new DefaultRow(row.getKey(), cells));
}
dc.close();
return dc.getTable();
}
use of org.knime.core.data.DataCell in project knime-core by knime.
the class MovingAggregationTableFactory method getCenterTable.
private BufferedDataTable getCenterTable(final ExecutionMonitor exec, final BufferedDataTable table, final BufferedDataContainer dc) throws CanceledExecutionException {
final int rowCount = table.getRowCount();
final LinkedList<DataRow> window = new LinkedList<>();
int rowIdx = 0;
int centerIdx = -1;
for (final DataRow row : table) {
exec.setProgress(rowIdx / (double) rowCount, "Processing row " + rowIdx++ + " of " + rowCount);
exec.checkCanceled();
window.add(row);
// we have to subtract 1 since the indexing of the array starts with 0
centerIdx = window.size() - m_windowLength / 2 - 1;
if (centerIdx < 0) {
// we have to fill the window first
continue;
}
final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
final DataRow centerRow = window.get(centerIdx);
int idx = 0;
// handle the retained columns
for (final int colIdx : m_cols2KeepIdxs) {
cells[idx++] = centerRow.getCell(colIdx);
}
final boolean windowFull = window.size() >= m_windowLength;
for (int i = 0, length = m_ops.length; i < length; i++) {
if (windowFull || m_handleMissings) {
final int colIdx = m_aggrColIdxs[i];
final AggregationOperator op = m_ops[i];
for (final DataRow windowRow : window) {
op.compute(windowRow, colIdx);
}
cells[idx++] = op.getResult();
op.reset();
} else {
// the window is not yet full return missing cells
cells[idx++] = DataType.getMissingCell();
}
}
dc.addRowToTable(new DefaultRow(centerRow.getKey(), cells));
// not during the missing value handling phase!
if (windowFull) {
window.removeFirst();
}
}
// we have to handle the remaining rows in the window
while (window.size() > centerIdx) {
exec.checkCanceled();
final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
int idx = 0;
final DataRow centerRow = window.get(centerIdx);
// handle the retained columns
for (final int colIdx : m_cols2KeepIdxs) {
cells[idx++] = centerRow.getCell(colIdx);
}
for (int i = 0, length = m_ops.length; i < length; i++) {
if (m_handleMissings) {
final int colIdx = m_aggrColIdxs[i];
final AggregationOperator op = m_ops[i];
for (final DataRow windowRow : window) {
op.compute(windowRow, colIdx);
}
cells[idx++] = op.getResult();
op.reset();
} else {
// the window is not yet full return missing cells
cells[idx++] = DataType.getMissingCell();
}
}
window.removeFirst();
dc.addRowToTable(new DefaultRow(centerRow.getKey(), cells));
}
dc.close();
return dc.getTable();
}
use of org.knime.core.data.DataCell in project knime-core by knime.
the class MovingAggregationTableFactory method getForwardTable.
private BufferedDataTable getForwardTable(final ExecutionMonitor exec, final BufferedDataTable table, final BufferedDataContainer dc) throws CanceledExecutionException {
final int rowCount = table.getRowCount();
final LinkedList<DataRow> window = new LinkedList<>();
int rowIdx = 0;
for (final DataRow row : table) {
exec.setProgress(rowIdx / (double) rowCount, "Processing row " + rowIdx++ + " of " + rowCount);
exec.checkCanceled();
window.add(row);
final boolean windowFull = window.size() >= m_windowLength;
if (!windowFull) {
// we have to fill the window first
continue;
}
final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
final DataRow firstRow = window.getFirst();
int idx = 0;
// handle the retained columns
for (final int colIdx : m_cols2KeepIdxs) {
cells[idx++] = firstRow.getCell(colIdx);
}
for (int i = 0, length = m_ops.length; i < length; i++) {
final int colIdx = m_aggrColIdxs[i];
final AggregationOperator op = m_ops[i];
for (final DataRow windowRow : window) {
op.compute(windowRow, colIdx);
}
cells[idx++] = op.getResult();
op.reset();
}
dc.addRowToTable(new DefaultRow(firstRow.getKey(), cells));
// remove the first row only when the window is full
// not during the missing value handling phase!
window.removeFirst();
}
// we have to handle the remaining rows in the window
while (!window.isEmpty()) {
exec.checkCanceled();
final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
int idx = 0;
final DataRow firstRow = window.getFirst();
// handle the retained columns
for (final int colIdx : m_cols2KeepIdxs) {
cells[idx++] = firstRow.getCell(colIdx);
}
for (int i = 0, length = m_ops.length; i < length; i++) {
if (m_handleMissings) {
final int colIdx = m_aggrColIdxs[i];
final AggregationOperator op = m_ops[i];
for (final DataRow windowRow : window) {
op.compute(windowRow, colIdx);
}
cells[idx++] = op.getResult();
op.reset();
} else {
// the window is not yet full return missing cells
cells[idx++] = DataType.getMissingCell();
}
}
window.removeFirst();
dc.addRowToTable(new DefaultRow(firstRow.getKey(), cells));
}
dc.close();
return dc.getTable();
}
use of org.knime.core.data.DataCell 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.core.data.DataCell in project knime-core by knime.
the class ExpressionFactory method or.
/**
* {@inheritDoc}
*/
@Override
public Expression or(final List<Expression> boolExpressions) {
final boolean allIsConstant = checkBooleansAndConstant(boolExpressions);
return new Expression.Base(boolExpressions) {
/**
* {@inheritDoc}
*/
@Override
public DataType getOutputType() {
return BooleanCell.TYPE;
}
/**
* {@inheritDoc}
*/
@Override
public List<DataType> getInputArgs() {
return Collections.singletonList(BooleanCell.TYPE);
}
/**
* {@inheritDoc}
*/
@Override
public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
DataCell ret = BooleanCell.FALSE;
Map<String, Map<String, String>> matchedObjects = new HashMap<String, Map<String, String>>();
for (Expression boolExpression : boolExpressions) {
ExpressionValue v = boolExpression.evaluate(row, provider);
DataCell cell = v.getValue();
assert !cell.isMissing();
if (cell instanceof BooleanValue) {
BooleanValue bool = (BooleanValue) cell;
if (!bool.getBooleanValue()) {
matchedObjects = Util.mergeObjects(matchedObjects, v.getMatchedObjects());
} else {
return new ExpressionValue(BooleanCell.TRUE, matchedObjects);
}
} else if (cell.isMissing()) {
ret = DataType.getMissingCell();
} else {
throw new IllegalStateException("Not boolean: " + v.getValue());
}
}
return new ExpressionValue(ret, matchedObjects);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isConstant() {
return allIsConstant;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "or(" + boolExpressions + ")";
}
/**
* {@inheritDoc}
*/
@Override
public ASTType getTreeType() {
return ASTType.Or;
}
};
}
Aggregations