Search in sources :

Example 16 with MissingCell

use of org.knime.core.data.MissingCell in project knime-core by knime.

the class DataCellToJavaConversionTest method testNestedCollectionTypes.

/**
 * Test ListCell(ListCell(IntCell)) -> Integer[][] conversion.
 *
 * @throws Exception When something went wrong
 */
@Test
public void testNestedCollectionTypes() throws Exception {
    ArrayList<DataCell> coll = new ArrayList<>();
    for (int i = 0; i < 5; ++i) {
        coll.add(new IntCell(i * i));
    }
    // collection cells can always contain missing cells.
    coll.add(new MissingCell("42"));
    final ListCell listCell = CollectionCellFactory.createListCell(Arrays.asList(CollectionCellFactory.createListCell(coll)));
    final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Integer[][]>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(listCell.getType(), Integer[][].class).stream().findFirst();
    assertTrue(factory.isPresent());
    final DataCellToJavaConverter<DataCell, Integer[][]> converter = (DataCellToJavaConverter<DataCell, Integer[][]>) factory.get().create();
    assertNotNull(converter);
    final Integer[][] array = converter.convert(listCell);
    for (int i = 0; i < 5; ++i) {
        assertEquals(new Integer(i * i), array[0][i]);
    }
    assertNull(array[0][5]);
}
Also used : DataCellToJavaConverter(org.knime.core.data.convert.java.DataCellToJavaConverter) MissingCell(org.knime.core.data.MissingCell) ListCell(org.knime.core.data.collection.ListCell) ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell) BinaryObjectDataCell(org.knime.core.data.blob.BinaryObjectDataCell) IntCell(org.knime.core.data.def.IntCell) Test(org.junit.Test)

Example 17 with MissingCell

use of org.knime.core.data.MissingCell in project knime-core by knime.

the class DateTimeDifferenceNodeModel method execute.

/**
 * helper method to compute output
 */
private void execute(final RowInput inData, final RowOutput output, final ExecutionContext exec, final long rowCount) throws Exception {
    DataRow row;
    DataRow previousRow = inData.poll();
    final int colIdx1 = inData.getDataTableSpec().findColumnIndex(m_col1stSelectModel.getStringValue());
    final DataType type = inData.getDataTableSpec().getColumnSpec(colIdx1).getType();
    long currentRowIndex = 0;
    output.push(new AppendedColumnRow(previousRow, new MissingCell("No previous row for calculating difference available.")));
    while ((row = inData.poll()) != null) {
        exec.checkCanceled();
        // set progress if not streaming
        if (rowCount >= 0) {
            exec.setProgress(currentRowIndex / (double) rowCount);
        }
        final long currentRowIndexFinal = currentRowIndex;
        exec.setMessage(() -> "Row " + currentRowIndexFinal + "/" + rowCount);
        // compute new cells
        DataCell newCell;
        if (type.isCompatible(LocalDateValue.class)) {
            newCell = calculateDate(previousRow.getCell(colIdx1), row.getCell(colIdx1), null);
        } else {
            newCell = calculateTime(previousRow.getCell(colIdx1), row.getCell(colIdx1), null);
        }
        output.push(new AppendedColumnRow(row, newCell));
        previousRow = row;
        currentRowIndex++;
    }
    inData.close();
    output.close();
}
Also used : MissingCell(org.knime.core.data.MissingCell) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow) AppendedColumnRow(org.knime.core.data.append.AppendedColumnRow)

Example 18 with MissingCell

use of org.knime.core.data.MissingCell in project knime-core by knime.

the class DateTimeDifferenceNodeModel method calculateTime.

private DataCell calculateTime(final DataCell cell, final DataCell referenceCell, final ZonedDateTime fixedDateTime) {
    if (cell.isMissing()) {
        return new MissingCell("Cell for calculating difference is missing.");
    } else if (fixedDateTime == null && referenceCell.isMissing()) {
        return new MissingCell("Reference cell for calculating difference is missing.");
    }
    final Temporal temporal1;
    final Temporal temporal2;
    if (cell instanceof ZonedDateTimeValue) {
        temporal1 = ((ZonedDateTimeValue) cell).getZonedDateTime();
        temporal2 = fixedDateTime == null ? ((ZonedDateTimeValue) referenceCell).getZonedDateTime() : fixedDateTime;
    } else if (cell instanceof LocalDateTimeValue) {
        temporal1 = ((LocalDateTimeValue) cell).getLocalDateTime();
        temporal2 = fixedDateTime == null ? ((LocalDateTimeValue) referenceCell).getLocalDateTime() : fixedDateTime.toLocalDateTime();
    } else {
        temporal1 = ((LocalTimeValue) cell).getLocalTime();
        temporal2 = fixedDateTime == null ? ((LocalTimeValue) referenceCell).getLocalTime() : fixedDateTime.toLocalTime();
    }
    if (m_calculationSelectModel.getStringValue().equals(OutputMode.Duration.name())) {
        final Duration diffDuration = Duration.between(temporal1, temporal2);
        return DurationCellFactory.create(diffDuration);
    } else {
        final Granularity granularity = Granularity.fromString(m_granularityModel.getStringValue());
        return LongCellFactory.create(granularity.between(temporal1, temporal2));
    }
}
Also used : LocalTimeValue(org.knime.core.data.time.localtime.LocalTimeValue) Temporal(java.time.temporal.Temporal) MissingCell(org.knime.core.data.MissingCell) Duration(java.time.Duration) Granularity(org.knime.time.util.Granularity) ZonedDateTimeValue(org.knime.core.data.time.zoneddatetime.ZonedDateTimeValue) LocalDateTimeValue(org.knime.core.data.time.localdatetime.LocalDateTimeValue)

Aggregations

MissingCell (org.knime.core.data.MissingCell)18 DataCell (org.knime.core.data.DataCell)13 DataColumnSpec (org.knime.core.data.DataColumnSpec)6 DoubleCell (org.knime.core.data.def.DoubleCell)6 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)5 DefaultRow (org.knime.core.data.def.DefaultRow)5 StringCell (org.knime.core.data.def.StringCell)5 ArrayList (java.util.ArrayList)4 DataRow (org.knime.core.data.DataRow)4 RowKey (org.knime.core.data.RowKey)4 IntCell (org.knime.core.data.def.IntCell)4 LinkedHashMap (java.util.LinkedHashMap)3 DataType (org.knime.core.data.DataType)3 DoubleValue (org.knime.core.data.DoubleValue)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 IOException (java.io.IOException)2 Test (org.junit.Test)2 BinaryObjectDataCell (org.knime.core.data.blob.BinaryObjectDataCell)2 ListCell (org.knime.core.data.collection.ListCell)2 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)2