use of java.time.format.DateTimeParseException in project Chronicle-Queue by OpenHFT.
the class ChronicleReader method execute.
public void execute() {
try {
long lastObservedTailIndex = Long.MAX_VALUE;
long highestReachedIndex = 0L;
boolean isFirstIteration = true;
boolean retryLastOperation = false;
boolean queueHasBeenModified = false;
do {
try (final SingleChronicleQueue queue = createQueue();
final QueueEntryHandler messageConverter = entryHandlerFactory.get()) {
final ExcerptTailer tailer = queue.createTailer();
queueHasBeenModified = false;
if (highestReachedIndex != 0L) {
tailer.moveToIndex(highestReachedIndex);
}
final Bytes textConversionTarget = Bytes.elasticByteBuffer();
try {
moveToSpecifiedPosition(queue, tailer, isFirstIteration);
lastObservedTailIndex = tailer.index();
while (!Thread.currentThread().isInterrupted()) {
try (DocumentContext dc = pollMethod.apply(tailer)) {
if (!dc.isPresent()) {
if (tailInputSource) {
pauser.pause();
}
break;
}
pauser.reset();
if (customPlugin == null) {
messageConverter.accept(dc.wire(), text -> {
applyFiltersAndLog(text, tailer.index());
});
} else {
customPlugin.onReadDocument(dc);
}
}
}
} finally {
textConversionTarget.release();
highestReachedIndex = tailer.index();
isFirstIteration = false;
}
queueHasBeenModified = queueHasBeenModifiedSinceLastCheck(lastObservedTailIndex);
} catch (final RuntimeException e) {
if (e.getCause() != null && e.getCause() instanceof DateTimeParseException) {
// ignore this error - due to a race condition between
// the reader creating a Queue (with default roll-cycle due to no files on disk)
// and the writer appending to the Queue with a non-default roll-cycle
retryLastOperation = true;
} else {
throw e;
}
}
} while (tailInputSource || retryLastOperation || queueHasBeenModified);
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
}
use of java.time.format.DateTimeParseException in project knime-core by knime.
the class StringToDurationPeriodNodeModel method detectTypes.
private void detectTypes(final RowInput rowInput) throws InterruptedException {
final DataTableSpec spec = rowInput.getDataTableSpec();
final String[] includes = m_colSelect.applyTo(spec).getIncludes();
if (m_detectedTypes == null) {
m_detectedTypes = new DataType[includes.length];
}
if (m_type.getStringValue().equals(OutputType.Duration.name())) {
Arrays.fill(m_detectedTypes, DurationCellFactory.TYPE);
}
if (m_type.getStringValue().equals(OutputType.Period.name())) {
Arrays.fill(m_detectedTypes, PeriodCellFactory.TYPE);
}
if (m_type.getStringValue().equals(OutputType.Automatic.name())) {
DataRow row;
while ((row = rowInput.poll()) != null) {
boolean isCellMissing = false;
for (int i = 0; i < includes.length; i++) {
if (m_detectedTypes[i] == null) {
final DataCell cell = row.getCell(spec.findColumnIndex(includes[i]));
if (cell.isMissing()) {
isCellMissing = true;
} else {
final String string = ((StringValue) cell).getStringValue();
try {
DurationPeriodFormatUtils.parseDuration(string);
m_detectedTypes[i] = DurationCellFactory.TYPE;
} catch (DateTimeParseException e1) {
try {
DurationPeriodFormatUtils.parsePeriod(string);
m_detectedTypes[i] = PeriodCellFactory.TYPE;
} catch (DateTimeParseException e2) {
isCellMissing = true;
}
}
}
}
}
if (!isCellMissing) {
// finished - every column type is detected
break;
}
}
}
}
use of java.time.format.DateTimeParseException in project knime-core by knime.
the class CreateDateTimeNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
BufferedDataContainer container = exec.createDataContainer(createOutSpec());
// check and parse duration/period (may be wrong, if controlled by flow variables)
TemporalAmount durationOrPeriod = null;
if (m_durationOrEnd.getStringValue().equals(EndMode.Duration.name()) || m_rowNrOptionSelection.getStringValue().equals(RowNrMode.Variable.name())) {
if (m_duration.getStringValue() != null) {
try {
durationOrPeriod = DurationPeriodFormatUtils.parseDuration(m_duration.getStringValue());
} catch (DateTimeParseException ex1) {
try {
durationOrPeriod = DurationPeriodFormatUtils.parsePeriod(m_duration.getStringValue());
} catch (DateTimeParseException ex2) {
throw new InvalidSettingsException("'" + m_duration.getStringValue() + "' could not be parsed as duration!");
}
}
}
}
// check start and end input (may be wrong, if controlled by flow variables)
final Class<? extends Temporal> classStart = m_start.getSelectedDateTime().getClass();
final Class<? extends Temporal> classEnd = m_end.getSelectedDateTime().getClass();
if (!classStart.equals(classEnd) && !(classStart.equals(ZonedDateTime.class) && classEnd.equals(LocalDateTime.class) && m_selectedNewType.getDataType().equals(ZonedDateTimeCellFactory.TYPE))) {
throw new InvalidSettingsException("The type of start and end time are not compatible: start is " + classStart.getSimpleName() + " but end is " + classEnd.getSimpleName());
}
// in case the end time is controlled by a flow variable holding a zoned date time, remove the zone
m_end.setUseZone(false);
// create date&time rows depending on settings
final Temporal start;
final Temporal end;
if (m_start.getSelectedDateTime() instanceof ZonedDateTime) {
start = m_startUseExecTime.getBooleanValue() ? getTemporalExecTimeWithFormat(((LocalDateTime) m_end.getSelectedDateTime()).atZone(m_start.getZone())) : m_start.getSelectedDateTime();
end = m_endUseExecTime.getBooleanValue() ? getTemporalExecTimeWithFormat(m_start.getSelectedDateTime()) : ZonedDateTime.of((LocalDateTime) m_end.getSelectedDateTime(), m_start.getZone());
} else {
start = m_startUseExecTime.getBooleanValue() ? getTemporalExecTimeWithFormat(m_end.getSelectedDateTime()) : m_start.getSelectedDateTime();
end = m_endUseExecTime.getBooleanValue() ? getTemporalExecTimeWithFormat(m_start.getSelectedDateTime()) : m_end.getSelectedDateTime();
}
if (m_rowNrOptionSelection.getStringValue().equals(RowNrMode.Fixed.name())) {
if (m_durationOrEnd.getStringValue().equals(EndMode.Duration.name())) {
createByFixedRowNrAndDuration(container, m_rowNrFixed.getLongValue(), start, durationOrPeriod, false, hasMillis(start, start) || hasDurationMillis());
} else {
createByFixedRowNrAndEnd(container, m_rowNrFixed.getLongValue(), start, end);
}
} else {
createByVariableRowNr(container, start, end, durationOrPeriod);
}
container.close();
return new BufferedDataTable[] { exec.createBufferedDataTable(container.getTable(), exec) };
}
use of java.time.format.DateTimeParseException in project knime-core by knime.
the class DateTimeShiftNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
ColumnRearranger rearranger = new ColumnRearranger(spec);
String[] includeList = m_colSelect.applyTo(spec).getIncludes();
int[] includeIndices = Arrays.stream(m_colSelect.applyTo(spec).getIncludes()).mapToInt(s -> spec.findColumnIndex(s)).toArray();
int i = 0;
int periodColIndex = spec.findColumnIndex(m_periodColSelect.getStringValue());
int numericalColIndex = spec.findColumnIndex(m_numericalColSelect.getStringValue());
boolean isPeriod;
if (m_periodSelection.isEnabled()) {
if (m_periodColSelect.isEnabled()) {
if (spec.getColumnSpec(periodColIndex).getType().isCompatible(PeriodValue.class)) {
isPeriod = true;
} else {
isPeriod = false;
}
} else {
periodColIndex = -1;
try {
DurationPeriodFormatUtils.parsePeriod(m_periodValue.getStringValue());
isPeriod = true;
} catch (DateTimeParseException e) {
isPeriod = false;
}
}
} else {
if (!m_numericalColSelect.isEnabled()) {
numericalColIndex = -1;
}
if (!Granularity.fromString(m_numericalGranularity.getStringValue()).isPartOfDate()) {
isPeriod = false;
} else {
isPeriod = true;
}
}
for (String includedCol : includeList) {
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
final SingleCellFactory cellFac;
final DataColumnSpec dataColSpec = new DataColumnSpecCreator(includedCol, spec.getColumnSpec(includedCol).getType()).createSpec();
if (isPeriod) {
cellFac = new DateTimeShiftPeriodCellFactory(dataColSpec, includeIndices[i++], periodColIndex, numericalColIndex);
} else {
cellFac = new DateTimeShiftDurationCellFactory(dataColSpec, includeIndices[i++], periodColIndex, numericalColIndex);
}
rearranger.replace(cellFac, includedCol);
} else {
final DataColumnSpec dataColSpec = new UniqueNameGenerator(spec).newColumn(includedCol + m_suffix.getStringValue(), spec.getColumnSpec(includedCol).getType());
final SingleCellFactory cellFac;
if (isPeriod) {
cellFac = new DateTimeShiftPeriodCellFactory(dataColSpec, includeIndices[i++], periodColIndex, numericalColIndex);
} else {
cellFac = new DateTimeShiftDurationCellFactory(dataColSpec, includeIndices[i++], periodColIndex, numericalColIndex);
}
rearranger.append(cellFac);
}
}
return rearranger;
}
use of java.time.format.DateTimeParseException in project knime-core by knime.
the class LoopStartWindowNodeDialog method saveSettingsTo.
/**
* {@inheritDoc}
*/
@Override
protected void saveSettingsTo(final NodeSettingsWO settings) throws InvalidSettingsException {
LoopStartWindowConfiguration config = new LoopStartWindowConfiguration();
config.setEventWindowSize((Integer) m_windowSizeSpinner.getValue());
config.setEventStepSize((Integer) m_stepSizeSpinner.getValue());
config.setLimitWindow(m_limitWindowCheckBox.isSelected());
if (m_forwardRButton.isSelected()) {
config.setWindowDefinition(WindowDefinition.FORWARD);
} else if (m_backwardRButton.isSelected()) {
config.setWindowDefinition(WindowDefinition.BACKWARD);
} else {
config.setWindowDefinition(WindowDefinition.CENTRAL);
}
if (m_rowTrigRButton.isSelected()) {
config.setTrigger(Trigger.ROW);
} else {
config.setTrigger(Trigger.TIME);
if (m_columnSelector == null || m_columnSelector.getSelectedAsSpec() == null) {
throw new InvalidSettingsException("No valid time column has been chosen");
}
/* Check if step size is smaller than 0. */
try {
if (m_stepSizeTime.getText() != null && Double.parseDouble(m_stepSizeTime.getText()) < 0) {
throw new InvalidSettingsException("Step size '" + m_stepSizeTime.getText() + "' invalid. Step size must be greater than 0.");
}
} catch (NumberFormatException e) {
}
try {
if (m_windowSizeTime.getText() != null && Double.parseDouble(m_windowSizeTime.getText()) < 0) {
throw new InvalidSettingsException("Window size '" + m_windowSizeTime.getText() + "' invalid. Window size must be greater than 0.");
}
} catch (NumberFormatException e) {
}
/* Check that either no unit is selected or that the given input does not contain any letters. */
if (m_startTimeUnit.getSelectedItem() != Unit.NO_UNIT && !m_stepSizeTime.getText().matches("^[0-9]+$")) {
throw new InvalidSettingsException("Step size: input '" + m_stepSizeTime.getText() + "' invalid. Only integers are allowed when unit '" + m_startTimeUnit.getSelectedItem() + "' is chosen");
}
if (m_timeWindowUnit.getSelectedItem() != Unit.NO_UNIT && !m_windowSizeTime.getText().matches("^[0-9]+$")) {
throw new InvalidSettingsException("Window size: input '" + m_windowSizeTime.getText() + "' invalid. Only integers are allowed when unit '" + m_timeWindowUnit.getSelectedItem() + "' is chosen");
}
try {
Duration startDur = null;
/* If unit is milliseconds we have it to change to seconds for parsing. */
if (((Unit) m_startTimeUnit.getSelectedItem()) == Unit.MILLISECONDS) {
double tempStart = Double.parseDouble(m_stepSizeTime.getText());
tempStart /= 1000;
startDur = DurationPeriodFormatUtils.parseDuration(tempStart + Unit.SECONDS.getUnitLetter());
} else {
startDur = DurationPeriodFormatUtils.parseDuration(m_stepSizeTime.getText() + ((Unit) m_startTimeUnit.getSelectedItem()).getUnitLetter());
}
/* Limit step size to 24h */
if (m_columnSelector.getSelectedAsSpec().getType().equals(DataType.getType(LocalTimeCell.class))) {
Duration temp = Duration.ofHours(24);
if (startDur.compareTo(temp) > 0) {
throw new InvalidSettingsException("Step size must not be greater than 24h when LocalTime is selected");
} else if (startDur.compareTo(Duration.ZERO) == 0 || startDur.isNegative()) {
throw new InvalidSettingsException("Step size '" + m_stepSizeTime.getText() + ((Unit) m_startTimeUnit.getSelectedItem()).getUnitLetter() + "' invalid. Step size must be greater than 0.");
}
}
if (m_columnSelector.getSelectedAsSpec().getType().equals(DataType.getType(LocalDateCell.class))) {
throw new InvalidSettingsException("Step size: Duration based step size '" + m_stepSizeTime.getText() + "' is not allowed for type LocalDate. Note that 'm' is reserved for minutes, use 'M' for months.");
}
config.setTimeStepSize(m_stepSizeTime.getText());
} catch (DateTimeParseException e) {
try {
Period startPer = DurationPeriodFormatUtils.parsePeriod(m_stepSizeTime.getText() + ((Unit) m_startTimeUnit.getSelectedItem()).getUnitLetter());
/* Period is not allowed. */
if (m_columnSelector.getSelectedAsSpec().getType().equals(DataType.getType(LocalTimeCell.class))) {
throw new InvalidSettingsException("Step size: Date based step size '" + m_stepSizeTime.getText() + "' is not allowed for type LocalTime. Note that 'M' is reserved for months, use 'm' for minutes.");
} else if (m_centralRButton.isSelected()) {
throw new InvalidSettingsException("Step size: Date based step size '" + m_stepSizeTime.getText() + "' is not allowed for central windowing. Note that 'M' is reserved for months, use 'm' for minutes.");
}
if (startPer.isZero() || startPer.isNegative()) {
throw new InvalidSettingsException("Step size '" + m_stepSizeTime.getText() + ((Unit) m_startTimeUnit.getSelectedItem()).getUnitLetter() + "' invalid. Step Size must be greater than 0");
}
config.setTimeStepSize(m_stepSizeTime.getText());
} catch (DateTimeParseException e2) {
throw new InvalidSettingsException("Step size: '" + m_stepSizeTime.getText() + "' is not a valid duration. Note that 'M' is reserved for months, use 'm' for minutes.");
}
}
try {
Duration windowDur = null;
/* If unit is milliseconds we have it to change to seconds for parsing. */
if (((Unit) m_timeWindowUnit.getSelectedItem()) == Unit.MILLISECONDS) {
double tempWindow = Double.parseDouble(m_windowSizeTime.getText());
tempWindow /= 1000;
windowDur = DurationPeriodFormatUtils.parseDuration(tempWindow + Unit.SECONDS.getUnitLetter());
} else {
windowDur = DurationPeriodFormatUtils.parseDuration(m_windowSizeTime.getText() + ((Unit) m_timeWindowUnit.getSelectedItem()).getUnitLetter());
}
/* Limit window to 24h */
if (m_columnSelector.getSelectedAsSpec().getType().equals(DataType.getType(LocalTimeCell.class))) {
Duration temp = Duration.ofHours(24);
if (windowDur.compareTo(temp) > 0) {
throw new InvalidSettingsException("Window size must not be greater than 24h when LocalTime is selected");
} else if (windowDur.isZero() || windowDur.isNegative()) {
throw new InvalidSettingsException("Window size '" + m_windowSizeTime.getText() + ((Unit) m_timeWindowUnit.getSelectedItem()).getUnitLetter() + "' invalid. Window size must be greater than 0");
}
}
if (m_columnSelector.getSelectedAsSpec().getType().equals(DataType.getType(LocalDateCell.class))) {
throw new InvalidSettingsException("Window size: Time based window size '" + m_windowSizeTime.getText() + "' is not allowed for type LocalDate. Note that 'm' is reserved for minutes, use 'M' for months.");
}
config.setTimeWindowSize(m_windowSizeTime.getText());
} catch (DateTimeParseException e) {
try {
Period windowPer = DurationPeriodFormatUtils.parsePeriod(m_windowSizeTime.getText() + ((Unit) m_timeWindowUnit.getSelectedItem()).getUnitLetter());
/* Period is not allowed. */
if (m_columnSelector.getSelectedAsSpec().getType().equals(DataType.getType(LocalTimeCell.class))) {
throw new InvalidSettingsException("Window size: Date based window size '" + m_windowSizeTime.getText() + "' is not allowed for type LocalTime. Note that 'M' is reserved for months, use 'm' for minutes.");
} else if (m_centralRButton.isSelected()) {
throw new InvalidSettingsException("Window size: Date based window size '" + m_windowSizeTime.getText() + "' is not allowed for central windowing. Note that 'M' is reserved for months, use 'm' for minutes.");
}
if (windowPer.isZero() || windowPer.isNegative()) {
throw new InvalidSettingsException("Window size '" + m_windowSizeTime.getText() + ((Unit) m_timeWindowUnit.getSelectedItem()).getUnitLetter() + "' invalid. Window size must be greater than 0");
}
config.setTimeWindowSize(m_windowSizeTime.getText());
} catch (DateTimeParseException e2) {
throw new InvalidSettingsException("Window size: '" + m_windowSizeTime.getText() + "' is not a valid duration. Note that 'M' is reserved for months, use 'm' for minutes.");
}
}
config.setTimeWindowUnit((Unit) m_timeWindowUnit.getSelectedItem());
config.setTimeStepUnit((Unit) m_startTimeUnit.getSelectedItem());
}
config.setUseSpecifiedStartTime(m_useSpecifiedStartTimeCheckBox.isSelected());
config.saveSettingsTo(settings);
m_specifiedStartTime.saveSettingsTo(settings);
m_columnSelector.saveSettingsTo(settings);
}
Aggregations