use of org.knime.core.data.StringValue in project knime-core by knime.
the class RuleEngine2PortsSimpleSettings method asString.
/**
* Converts a {@link DataCell} to {@link String} for rules.
*
* @param cell A {@link DataCell}.
* @return The value of {@code cell} as a {@link String}, properly escaped.
*/
protected String asString(final DataCell cell) {
if (cell.isMissing()) {
return "\"?\"";
}
if (cell instanceof StringValue) {
StringValue sv = (StringValue) cell;
String s = sv.getStringValue();
if (isTreatOutcomesAsReferences() && s.startsWith("$")) {
return s;
}
return escapedText(s);
}
if (cell instanceof BooleanValue) {
return Boolean.toString(((BooleanValue) cell).getBooleanValue()).toUpperCase();
}
String string = cell.toString();
if (cell instanceof DoubleValue) {
return string;
}
return escapedText(string);
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class RuleEngine2PortsSimpleNodeDialog method updateErrorsAndWarnings.
/**
* Updates the errors table, the warning text area and the computed outcome type.
*/
protected void updateErrorsAndWarnings() {
m_errorsModel.setRowCount(0);
hideErrors();
m_warnings.setText("");
m_outcomeType.setIcon(DataType.getMissingCell().getType().getIcon());
// Checking data from second input port
final int ruleIdx = getRules() == null ? -1 : getRules().getSpec().findColumnIndex(m_ruleColumn.getSelectedColumn());
final int outcomeIdx = getRules() == null ? -1 : getRules().getSpec().findColumnIndex(m_outcomeColumn.getSelectedColumn());
if (getRules() != null && isSpecAvailable() && ruleIdx >= 0) {
RuleFactory factory = ruleFactory();
long lineNo = 0;
boolean wasCatchAll = false;
final boolean firstHit = isFirstHit();
List<Rule> rules = new ArrayList<>();
for (DataRow dataRow : getRules()) {
++lineNo;
DataCell ruleCell = dataRow.getCell(ruleIdx);
if (ruleCell.isMissing()) {
// String cellValue = "?";
// if (ruleCell instanceof MissingValue) {
// cellValue += " (" + ((MissingValue)ruleCell).getError() + ")";
// }
m_errorsModel.addRow(new Object[] { dataRow.getKey(), ruleCell, "Missing cell" });
showErrors();
}
if (ruleCell instanceof StringValue) {
StringValue ruleSV = (StringValue) ruleCell;
String ruleText = ruleSV.getStringValue().replaceAll("[\r\n]+", " ");
if (outcomeIdx >= 0) {
DataCell outcome = dataRow.getCell(outcomeIdx);
String outcomeString;
try {
outcomeString = m_settings.asStringFailForMissing(outcome);
} catch (InvalidSettingsException e) {
outcomeString = "?";
}
if (m_ruleType.onlyBooleanOutcome()) {
if ("\"TRUE\"".equalsIgnoreCase(outcomeString)) {
outcomeString = "TRUE";
} else if ("\"FALSE\"".equalsIgnoreCase(outcomeString)) {
outcomeString = "FALSE";
}
}
ruleText += " => " + outcomeString;
}
try {
Rule rule = factory.parse(ruleText, getDataSpec(), getAvailableFlowVariables());
rules.add(rule);
String origWarning = !m_warnings.getText().isEmpty() ? m_warnings.getText() + "\n" : "";
Condition cond = rule.getCondition();
if (cond.isEnabled()) {
// not comment
if (cond.isCatchAll() && !wasCatchAll && firstHit && lineNo < getRules().size()) {
m_warnings.setText(origWarning + "No rules will match after line " + lineNo + " (" + dataRow.getKey() + "). Because of rule: " + ruleText);
}
wasCatchAll |= cond.isCatchAll() && firstHit;
if (!wasCatchAll && cond.isConstantFalse()) {
m_warnings.setText(origWarning + "The rule in line " + lineNo + " (" + dataRow.getKey() + ") will never match: " + ruleText);
}
}
} catch (ParseException e) {
m_errorsModel.addRow(new Object[] { dataRow.getKey(), ruleText, e.getMessage() });
showErrors();
}
} else {
// Missings were handled previously
if (!ruleCell.isMissing()) {
m_errorsModel.addRow(new Object[] { dataRow.getKey(), ruleCell.toString(), "Wrong type: " + ruleCell.getType() });
}
}
}
final DataColumnSpec outcomeSpec = m_outcomeColumn.getSelectedColumnAsSpec();
DataType dataType = RuleEngineNodeModel.computeOutputType(rules, outcomeSpec == null ? StringCell.TYPE : outcomeSpec.getType(), m_ruleType, getSettings().isDisallowLongOutputForCompatibility());
if (dataType != null) {
m_outcomeType.setIcon(dataType.getIcon());
}
}
}
use of org.knime.core.data.StringValue 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.StringValue in project knime-core by knime.
the class RuleSetToTable method toString.
/**
* Converts a {@link DataCell} to {@link String} for rules.
*
* @param cell A {@link DataCell}.
* @return The value of {@code cell} as a {@link String}, properly escaped.
*/
public static String toString(final DataCell cell) {
if (cell.isMissing()) {
return "\"?\"";
}
if (cell instanceof StringValue) {
StringValue sv = (StringValue) cell;
String s = sv.getStringValue();
return escapedText(s);
}
if (cell instanceof BooleanValue) {
return Boolean.toString(((BooleanValue) cell).getBooleanValue()).toUpperCase();
}
if (cell instanceof DoubleValue) {
return cell.toString();
}
return escapedText(cell.toString());
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class ReadPNGFromURLNodeModel method createColumnRearranger.
private ColumnRearranger createColumnRearranger(final DataTableSpec in, final AtomicLong failCounter) throws InvalidSettingsException {
String colName = m_config.getUrlColName();
if (colName == null) {
// throws ISE
m_config.guessDefaults(in);
colName = m_config.getUrlColName();
setWarningMessage("Auto-configuration: Guessing column \"" + colName + "\" to contain locations");
}
final int colIndex = in.findColumnIndex(colName);
if (colIndex < 0) {
throw new InvalidSettingsException("No such column in input: " + colName);
}
DataColumnSpec colSpec = in.getColumnSpec(colIndex);
if (!colSpec.getType().isCompatible(StringValue.class)) {
throw new InvalidSettingsException("Selected column \"" + colName + "\" is not string-compatible");
}
final String newColName = m_config.getNewColumnName();
DataColumnSpecCreator colSpecCreator;
if (newColName != null) {
String newName = DataTableSpec.getUniqueColumnName(in, newColName);
colSpecCreator = new DataColumnSpecCreator(newName, PNGImageContent.TYPE);
} else {
colSpecCreator = new DataColumnSpecCreator(colSpec);
colSpecCreator.setType(PNGImageContent.TYPE);
colSpecCreator.removeAllHandlers();
colSpecCreator.setDomain(null);
}
DataColumnSpec outColumnSpec = colSpecCreator.createSpec();
ColumnRearranger rearranger = new ColumnRearranger(in);
CellFactory fac = new SingleCellFactory(outColumnSpec) {
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(colIndex);
if (cell.isMissing()) {
return DataType.getMissingCell();
} else {
String url = ((StringValue) cell).getStringValue();
try {
return toPNGCell(url);
} catch (Exception e) {
if (m_config.isFailOnInvalid()) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e.getMessage(), e);
}
} else {
String message = "Failed to read png content from " + "\"" + url + "\": " + e.getMessage();
LOGGER.warn(message, e);
failCounter.incrementAndGet();
return DataType.getMissingCell();
}
}
}
}
};
if (newColName == null) {
rearranger.replace(fac, colIndex);
} else {
rearranger.append(fac);
}
return rearranger;
}
Aggregations