use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class TimeDifferenceNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
// get the selected granularity level
final Granularity g = Granularity.valueOf(m_granularity.getStringValue());
// create rearranger
ColumnRearranger rearranger = new ColumnRearranger(inData[0].getDataTableSpec());
String typeofref = m_typeofreference.getStringValue();
if (typeofref.equals(TimeDifferenceNodeDialog.CFG_COLUMN)) {
// append the new column with single cell factory
rearranger.append(new SingleCellFactory(createOutputColumnSpec(inData[0].getDataTableSpec(), m_newColName.getStringValue())) {
/**
* Value for the new column is based on the values of two column
* of the row (first and second date column), the selected
* granularity, and the fraction digits for rounding.
*
* @param row the current row
* @return the difference between the two date values with the
* given granularity and rounding
*/
@Override
public DataCell getCell(final DataRow row) {
DataCell cell1 = row.getCell(m_col1Idx);
DataCell cell2 = row.getCell(m_col2Idx);
if ((cell1.isMissing()) || (cell2.isMissing())) {
return DataType.getMissingCell();
}
long first = ((DateAndTimeValue) cell1).getUTCTimeInMillis();
long last = ((DateAndTimeValue) cell2).getUTCTimeInMillis();
return getRoundedTimeDifference(first, last, g);
}
});
} else if (typeofref.equals(TimeDifferenceNodeDialog.CFG_ROW_DIFF)) {
// option for producing the time difference between current and the
// previous row.
// append the new column with single cell factory
rearranger.append(new SingleCellFactory(createOutputColumnSpec(inData[0].getDataTableSpec(), m_newColName.getStringValue())) {
/**
* saves the previous time value (contained in the last row)
*/
private DateAndTimeValue m_previous = null;
/**
* Value for the new column is based on the values of the
* current row and the value of the previous row.
* Therefore both rows must contain a DateAndTimeValue,
* the selected granularity, and the fraction digits for
* rounding.
*
* @param row the current row
* @return the difference between the two date values with the
* given granularity and rounding
*/
@Override
public DataCell getCell(final DataRow row) {
DataCell cell1 = row.getCell(m_col1Idx);
// value
if ((cell1.isMissing()) || !cell1.getType().isCompatible(DateAndTimeValue.class)) {
m_previous = null;
return DataType.getMissingCell();
}
// (e.g. we are in the first row)
if (m_previous == null) {
m_previous = (DateAndTimeValue) cell1;
return DataType.getMissingCell();
}
long first = m_previous.getUTCTimeInMillis();
long last = ((DateAndTimeValue) cell1).getUTCTimeInMillis();
m_previous = (DateAndTimeValue) cell1;
return getRoundedTimeDifference(first, last, g);
}
});
} else {
final long time;
if (typeofref.equals(TimeDifferenceNodeDialog.CFG_FIXDATE)) {
time = m_timemodel.getCalendar().getTimeInMillis();
} else {
time = System.currentTimeMillis() + TimeZone.getDefault().getOffset(System.currentTimeMillis());
}
// append the new column with single cell factory
rearranger.append(new SingleCellFactory(createOutputColumnSpec(inData[0].getDataTableSpec(), m_newColName.getStringValue())) {
/**
* Value for the new column is based on the values of two column
* of the row (first and second date column), the selected
* granularity, and the fraction digits for rounding.
*
* @param row the current row
* @return the difference between the two date values with the
* given granularity and rounding
*/
@Override
public DataCell getCell(final DataRow row) {
DataCell cell1 = row.getCell(m_col1Idx);
if ((cell1.isMissing())) {
return DataType.getMissingCell();
}
long first = ((DateAndTimeValue) cell1).getUTCTimeInMillis();
return getRoundedTimeDifference(first, time, g);
}
});
}
BufferedDataTable out = exec.createColumnRearrangeTable(inData[0], rearranger, exec);
return new BufferedDataTable[] { out };
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class MaskTimeNodeModel method createCellFactory.
private SingleCellFactory createCellFactory(final DataColumnSpec spec, final int colIdx, final String maskMode) {
return new SingleCellFactory(spec) {
@Override
public DataCell getCell(final DataRow row) {
DataCell dc = row.getCell(colIdx);
if (dc.isMissing()) {
return DataType.getMissingCell();
}
if (dc.getType().isCompatible(DateAndTimeValue.class)) {
DateAndTimeValue v = (DateAndTimeValue) dc;
Calendar time = v.getUTCCalendarClone();
if (maskMode.equals(MASK_DATE)) {
DateAndTimeCell.resetDateFields(time);
if (!v.hasTime()) {
// date is masked and no time -> missing value
m_nrInvalids++;
return DataType.getMissingCell();
}
m_onlyInvalids = false;
return new DateAndTimeCell(time.getTimeInMillis(), false, v.hasTime(), v.hasMillis());
} else if (maskMode.equals(MASK_TIME)) {
DateAndTimeCell.resetTimeFields(time);
if (!v.hasDate()) {
// time is masked and no date -> missing cell
m_nrInvalids++;
return DataType.getMissingCell();
}
m_onlyInvalids = false;
return new DateAndTimeCell(time.getTimeInMillis(), v.hasDate(), false, false);
} else if (maskMode.equals(MASK_MILLIS)) {
resetMilliSeconds(time);
m_onlyInvalids = false;
return new DateAndTimeCell(time.getTimeInMillis(), v.hasDate(), v.hasTime(), false);
}
}
LOGGER.error("Unsupported data type: " + dc.getType() + "!");
return DataType.getMissingCell();
}
};
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class String2DateNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
* @throws InvalidSettingsException
* @since 2.6
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec, final SimpleStreamableOperatorInternals emptyInternals) throws InvalidSettingsException {
if (m_formatModel.getStringValue() == null) {
throw new InvalidSettingsException("No format selected.");
}
try {
m_dateFormat = new SimpleDateFormat(m_formatModel.getStringValue());
} catch (IllegalArgumentException ex) {
throw new InvalidSettingsException("Invalid format: " + m_formatModel.getStringValue(), ex);
}
m_dateFormat.setTimeZone(DateAndTimeCell.UTC_TIMEZONE);
String selectedCol = m_selectedColModel.getStringValue();
if (selectedCol == null || selectedCol.isEmpty()) {
// try to find first String compatible one and auto-guess it
for (DataColumnSpec cs : spec) {
if (cs.getType().isCompatible(StringValue.class)) {
m_selectedColModel.setStringValue(cs.getName());
setWarningMessage("Auto-guessing first String compatible column: " + cs.getName());
break;
}
}
}
// if still null -> no String compatible column at all
if (selectedCol == null || selectedCol.isEmpty()) {
throw new InvalidSettingsException("No String compatible column found!");
}
final int colIndex = spec.findColumnIndex(selectedCol);
if (colIndex < 0) {
throw new InvalidSettingsException("No such column: " + selectedCol);
}
DataColumnSpec colSpec = spec.getColumnSpec(colIndex);
if (!colSpec.getType().isCompatible(StringValue.class)) {
throw new InvalidSettingsException("Column \"" + selectedCol + "\" does not contain string values: " + colSpec.getType().toString());
}
ColumnRearranger result = new ColumnRearranger(spec);
String uniqueColName = selectedCol;
if (!m_replace.getBooleanValue()) {
// if we do not have a default new column name yet
// create one as done in
// check whether the new column name is unique...
uniqueColName = DataTableSpec.getUniqueColumnName(spec, m_newColNameModel.getStringValue());
m_newColNameModel.setStringValue(uniqueColName);
}
DataColumnSpec newColSpec = new DataColumnSpecCreator(uniqueColName, DateAndTimeCell.TYPE).createSpec();
m_dateFormat = new SimpleDateFormat(m_formatModel.getStringValue());
m_dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
SingleCellFactory c = new SingleCellFactory(newColSpec) {
private int m_failCounter = 0;
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(colIndex);
if (cell.isMissing() || !(cell instanceof StringValue)) {
return DataType.getMissingCell();
}
try {
String source = ((StringValue) cell).getStringValue();
Date date = m_dateFormat.parse(source);
Calendar calendar = DateAndTimeCell.getUTCCalendar();
calendar.setTimeInMillis(date.getTime());
// dependent on the type create the referring cell
return new DateAndTimeCell(calendar.getTimeInMillis(), m_useDate, m_useTime, m_useMillis);
} catch (ParseException pe) {
m_failCounter++;
if (m_cancelOnFail.getBooleanValue() && m_failCounter >= m_failNumberModel.getIntValue()) {
throw new IllegalArgumentException("Maximum number of fails reached: " + m_failNumberModel.getIntValue());
}
return DataType.getMissingCell();
}
}
@Override
public void afterProcessing() {
setFailMessage(m_failCounter);
emptyInternals.getConfig().addLong(INTERNALS_KEY_FAIL_COUNT, m_failCounter);
}
};
if (m_replace.getBooleanValue()) {
result.replace(c, colIndex);
} else {
result.append(c);
}
return result;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class Time2StringNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
* @since 2.6
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec inSpec) throws InvalidSettingsException {
// check if input has dateandtime column
if (!inSpec.containsCompatibleType(DateAndTimeValue.class)) {
throw new InvalidSettingsException("Input table must contain at least timestamp column!");
}
// currently selected column still there?
String selectedColName = m_selectedCol.getStringValue();
if (selectedColName != null && !selectedColName.isEmpty()) {
if (!inSpec.containsName(selectedColName)) {
throw new InvalidSettingsException("Column " + selectedColName + " not found in input spec!");
}
} else {
// no value set: auto-configure -> choose first timeseries
for (DataColumnSpec colSpec : inSpec) {
if (colSpec.getType().isCompatible(DateAndTimeValue.class)) {
String colName = colSpec.getName();
m_selectedCol.setStringValue(colName);
m_newColName.setStringValue(colName + "_" + COL_NAME_SUFFIX);
setWarningMessage("Auto-selected column: '" + colName + "'");
break;
}
}
}
ColumnRearranger rearranger = new ColumnRearranger(inSpec);
// if replace -> use original column name
final boolean replace = m_replaceCol.getBooleanValue();
String colName = DataTableSpec.getUniqueColumnName(inSpec, m_newColName.getStringValue());
if (replace) {
colName = m_selectedCol.getStringValue();
}
DataColumnSpecCreator specCreator = new DataColumnSpecCreator(colName, StringCell.TYPE);
final SimpleDateFormat dateFormat = new SimpleDateFormat(m_pattern.getStringValue());
dateFormat.setTimeZone(DateAndTimeCell.UTC_TIMEZONE);
final int colIdx = inSpec.findColumnIndex(m_selectedCol.getStringValue());
SingleCellFactory factory = new SingleCellFactory(specCreator.createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
DataCell dc = row.getCell(colIdx);
if (dc.isMissing()) {
return DataType.getMissingCell();
}
if (dc.getType().isCompatible(DateAndTimeValue.class)) {
DateAndTimeValue v = (DateAndTimeValue) dc;
String result = dateFormat.format(v.getUTCCalendarClone().getTime());
return new StringCell(result);
}
LOGGER.error("Encountered unsupported data type: " + dc.getType() + " in row: " + row.getKey());
return DataType.getMissingCell();
}
};
if (!replace) {
rearranger.append(factory);
} else {
rearranger.replace(factory, m_selectedCol.getStringValue());
}
return rearranger;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class PMMLRuleEditorNodeModel method createRearranger.
/**
* Creates the {@link ColumnRearranger} that can compute the new column.
*
* @param tableSpec The spec of the input table.
* @param ruleSet The {@link RuleSet} xml object where the rules should be added.
* @param parser The parser for the rules.
* @return The {@link ColumnRearranger}.
* @throws ParseException Problem during parsing.
* @throws InvalidSettingsException if settings are invalid
*/
private ColumnRearranger createRearranger(final DataTableSpec tableSpec, final RuleSet ruleSet, final PMMLRuleParser parser) throws ParseException, InvalidSettingsException {
if (m_settings.isAppendColumn() && m_settings.getNewColName().isEmpty()) {
throw new InvalidSettingsException("No name for prediction column provided");
}
Set<String> outcomes = new LinkedHashSet<String>();
List<DataType> outcomeTypes = new ArrayList<DataType>();
int line = 0;
final List<Pair<PMMLPredicate, Expression>> rules = new ArrayList<Pair<PMMLPredicate, Expression>>();
for (String ruleText : m_settings.rules()) {
++line;
if (RuleSupport.isComment(ruleText)) {
continue;
}
try {
ParseState state = new ParseState(ruleText);
PMMLPredicate expression = parser.parseBooleanExpression(state);
SimpleRule simpleRule = ruleSet.addNewSimpleRule();
setCondition(simpleRule, expression);
state.skipWS();
state.consumeText("=>");
state.skipWS();
Expression outcome = parser.parseOutcomeOperand(state, null);
// Only constants are allowed in the outcomes.
assert outcome.isConstant() : outcome;
rules.add(new Pair<PMMLPredicate, Expression>(expression, outcome));
outcomeTypes.add(outcome.getOutputType());
simpleRule.setScore(outcome.toString());
// simpleRule.setConfidence(confidenceForRule(simpleRule, line, ruleText));
simpleRule.setWeight(weightForRule(simpleRule, line, ruleText));
outcomes.add(simpleRule.getScore());
} catch (ParseException e) {
throw Util.addContext(e, ruleText, line);
}
}
DataType outcomeType = RuleEngineNodeModel.computeOutputType(outcomeTypes, true);
ColumnRearranger rearranger = new ColumnRearranger(tableSpec);
DataColumnSpecCreator specProto = new DataColumnSpecCreator(m_settings.isAppendColumn() ? DataTableSpec.getUniqueColumnName(tableSpec, m_settings.getNewColName()) : m_settings.getReplaceColumn(), outcomeType);
specProto.setDomain(new DataColumnDomainCreator(toCells(outcomes, outcomeType)).createDomain());
SingleCellFactory cellFactory = new SingleCellFactory(true, specProto.createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
for (Pair<PMMLPredicate, Expression> pair : rules) {
if (pair.getFirst().evaluate(row, tableSpec) == Boolean.TRUE) {
return pair.getSecond().evaluate(row, null).getValue();
}
}
return DataType.getMissingCell();
}
};
if (m_settings.isAppendColumn()) {
rearranger.append(cellFactory);
} else {
rearranger.replace(cellFactory, m_settings.getReplaceColumn());
}
return rearranger;
}
Aggregations