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]);
}
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();
}
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));
}
}
Aggregations