Search in sources :

Example 76 with IntCell

use of org.knime.core.data.def.IntCell in project knime-core by knime.

the class ExpressionFactory method constant.

/**
 * {@inheritDoc}
 */
@Override
public Expression constant(final int integer) {
    final IntCell cell = new IntCell(integer);
    final ExpressionValue value = new ExpressionValue(cell, EMPTY_MAP);
    return new ConstantExpression(value);
}
Also used : IntCell(org.knime.core.data.def.IntCell)

Example 77 with IntCell

use of org.knime.core.data.def.IntCell in project knime-core by knime.

the class ExpressionFactory method flowVarRef.

/**
 * {@inheritDoc}
 */
@Override
public Expression flowVarRef(final Map<String, FlowVariable> flowVariables, final String flowVarRef) {
    if (!flowVariables.containsKey(flowVarRef) || flowVariables.get(flowVarRef) == null) {
        throw new IllegalStateException("Not a valid flow variable: " + flowVarRef + " (" + flowVariables + ")");
    }
    final FlowVariable var = flowVariables.get(flowVarRef);
    final boolean isConstant = var.getScope() != Scope.Local;
    final ExpressionValue constant = isConstant ? Util.readFlowVarToExpressionValue(var) : null;
    return new Expression.Base() {

        /**
         * {@inheritDoc}
         */
        @Override
        public List<DataType> getInputArgs() {
            return Collections.emptyList();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public DataType getOutputType() {
            return Util.toDataType(var.getType());
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
            if (constant != null) {
                return constant;
            }
            Object variable = provider.readVariable(flowVarRef, Util.flowVarTypeToClass(var.getType()));
            switch(var.getType()) {
                case DOUBLE:
                    return new ExpressionValue(new DoubleCell(((Double) variable).doubleValue()), EMPTY_MAP);
                case INTEGER:
                    return new ExpressionValue(new IntCell(((Integer) variable).intValue()), EMPTY_MAP);
                case STRING:
                    return new ExpressionValue(new StringCell((String) variable), EMPTY_MAP);
                default:
                    throw new IllegalStateException("Not supported flow variable type: " + var.getType());
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isConstant() {
            return isConstant;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public String toString() {
            return "$$" + flowVarRef + "$$";
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ASTType getTreeType() {
            return ASTType.FlowVarRef;
        }
    };
}
Also used : DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 78 with IntCell

use of org.knime.core.data.def.IntCell in project knime-core by knime.

the class MissingValueHandlingTableIterator method handleMissing.

/* Does the missing value handling on a row. */
private DataRow handleMissing(final DataRow row) {
    DataCell[] cells = new DataCell[row.getNumCells()];
    for (int i = 0; i < row.getNumCells(); i++) {
        ColSetting colset = m_table.getColSetting(i);
        DataCell oldCell = row.getCell(i);
        DataCell newCell;
        if (oldCell.isMissing()) {
            switch(colset.getMethod()) {
                case ColSetting.METHOD_NO_HANDLING:
                    newCell = oldCell;
                    break;
                case ColSetting.METHOD_FIX_VAL:
                    newCell = m_table.getColSetting(i).getFixCell();
                    assert (newCell != null);
                    break;
                case ColSetting.METHOD_MOST_FREQUENT:
                    newCell = m_table.getMostFrequent(i);
                    break;
                case ColSetting.METHOD_MAX:
                    newCell = m_table.getMax(i);
                    break;
                case ColSetting.METHOD_MIN:
                    newCell = m_table.getMin(i);
                    break;
                case ColSetting.METHOD_MEAN:
                    // in contrast to the above, it will return
                    // a newly generate value, thus, only a double
                    double mean = m_table.getMean(i);
                    if (colset.getType() == ColSetting.TYPE_DOUBLE) {
                        newCell = new DoubleCell(mean);
                    } else {
                        assert colset.getType() == ColSetting.TYPE_INT;
                        newCell = new IntCell((int) Math.round(mean));
                    }
                    break;
                case ColSetting.METHOD_IGNORE_ROWS:
                    assert false : "That should have been filtered.";
                    newCell = oldCell;
                default:
                    throw new RuntimeException("Invalid method!");
            }
        } else {
            newCell = oldCell;
        }
        cells[i] = newCell;
    }
    RowKey key = row.getKey();
    return new DefaultRow(key, cells);
}
Also used : RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell)

Example 79 with IntCell

use of org.knime.core.data.def.IntCell in project knime-core by knime.

the class ExpressionFactoryTest method testConstantInt.

/**
 * Test method for {@link ExpressionFactory#constant(int)}.
 */
@Test
public void testConstantInt() {
    final Expression one = m_factory.constant(1);
    assertTrue(one.isConstant());
    assertEquals(new IntCell(1), one.evaluate(null, null).getValue());
    assertEquals(Collections.emptyMap(), one.evaluate(null, null).getMatchedObjects());
}
Also used : IntCell(org.knime.core.data.def.IntCell) Test(org.junit.Test)

Example 80 with IntCell

use of org.knime.core.data.def.IntCell in project knime-core by knime.

the class ExpressionFactoryTest method testTableRef.

/**
 * Test method for {@link ExpressionFactory#tableRef(Rule.TableReference)} .
 */
@Test
public void testTableRef() {
    assertEquals(new LongCell(2), m_factory.tableRef(TableReference.RowCount).evaluate(null, new VariableProvider() {

        @Override
        public Object readVariable(final String name, final Class<?> type) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getRowCountLong() {
            return 2L;
        }

        @Override
        public long getRowIndexLong() {
            return 0L;
        }

        @Deprecated
        @Override
        public int getRowCount() {
            return 0;
        }

        @Deprecated
        @Override
        public int getRowIndex() {
            return 0;
        }
    }).getValue());
    assertEquals(new LongCell(3), m_factory.tableRef(TableReference.RowIndex).evaluate(null, new VariableProvider() {

        @Override
        public Object readVariable(final String name, final Class<?> type) {
            return null;
        }

        @Override
        public long getRowCountLong() {
            return 0L;
        }

        @Override
        public long getRowIndexLong() {
            return 3L;
        }

        @Deprecated
        @Override
        public int getRowCount() {
            return 0;
        }

        @Deprecated
        @Override
        public int getRowIndex() {
            return 0;
        }
    }).getValue());
    assertEquals(new StringCell("Row0"), m_factory.tableRef(TableReference.RowId).evaluate(new DefaultRow(new RowKey("Row0"), new IntCell(3)), new VariableProvider() {

        @Override
        public Object readVariable(final String name, final Class<?> type) {
            // TODO Auto-generated method stub
            return null;
        }

        @Deprecated
        @Override
        public int getRowCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Deprecated
        @Override
        public int getRowIndex() {
            // TODO Auto-generated method stub
            return 0;
        }
    }).getValue());
}
Also used : LongCell(org.knime.core.data.def.LongCell) StringCell(org.knime.core.data.def.StringCell) RowKey(org.knime.core.data.RowKey) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell) Test(org.junit.Test)

Aggregations

IntCell (org.knime.core.data.def.IntCell)109 DataCell (org.knime.core.data.DataCell)79 DoubleCell (org.knime.core.data.def.DoubleCell)67 StringCell (org.knime.core.data.def.StringCell)55 DefaultRow (org.knime.core.data.def.DefaultRow)46 DataRow (org.knime.core.data.DataRow)33 DataTableSpec (org.knime.core.data.DataTableSpec)21 RowKey (org.knime.core.data.RowKey)21 ArrayList (java.util.ArrayList)20 DataType (org.knime.core.data.DataType)20 LongCell (org.knime.core.data.def.LongCell)14 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)14 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)12 BufferedDataTable (org.knime.core.node.BufferedDataTable)12 Test (org.junit.Test)11 DataColumnSpec (org.knime.core.data.DataColumnSpec)11 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)9 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)9 DataContainer (org.knime.core.data.container.DataContainer)8 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)8