use of org.knime.core.data.IntValue in project knime-core by knime.
the class MissingValuePanel method getFixTextField.
/*
* Helper in constructor, generates the text field to enter the replacement
* value.
*/
private static JComponent getFixTextField(final ColSetting setting, final DataColumnSpec spec) {
JComponent fixText;
// FIX text field
DataCell fixCell = setting.getFixCell();
switch(setting.getType()) {
case ColSetting.TYPE_DOUBLE:
fixText = new JFormattedTextField();
((JFormattedTextField) fixText).setColumns(8);
Double doubel;
if (fixCell == null) {
doubel = new Double(0.0);
} else {
double d = ((DoubleValue) fixCell).getDoubleValue();
doubel = new Double(d);
}
((JFormattedTextField) fixText).setValue(doubel);
break;
case ColSetting.TYPE_INT:
fixText = new JFormattedTextField();
((JFormattedTextField) fixText).setColumns(8);
Integer integer;
if (fixCell == null) {
integer = new Integer(0);
} else {
int i = ((IntValue) fixCell).getIntValue();
integer = new Integer(i);
}
((JFormattedTextField) fixText).setValue(integer);
break;
case ColSetting.TYPE_STRING:
DataCell[] vals;
if (spec != null && spec.getDomain().hasValues()) {
vals = spec.getDomain().getValues().toArray(new DataCell[0]);
} else {
vals = new DataCell[0];
}
DefaultComboBoxModel model = new DefaultComboBoxModel(vals);
fixText = new JComboBox(model);
((JComboBox) fixText).setPrototypeDisplayValue("#########");
((JComboBox) fixText).setEditable(true);
((JComboBox) fixText).setRenderer(new DefaultListCellRenderer() {
/**
* Overridden to set tooltip text properly.
* @see DefaultListCellRenderer#getListCellRendererComponent(
* JList, Object, int, boolean, boolean)
*/
@Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (c instanceof JComponent) {
((JComponent) c).setToolTipText(value.toString());
}
return c;
}
});
String string;
if (fixCell == null) {
string = "";
} else {
string = ((StringValue) fixCell).getStringValue();
}
model.setSelectedItem(string);
break;
default:
throw new InternalError("No such type");
}
return fixText;
}
use of org.knime.core.data.IntValue in project knime-core by knime.
the class ARFFWriterNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
checkFileAccess(m_location, false);
URL url = FileUtil.toURL(m_location);
Path localPath = FileUtil.resolveToPath(url);
DataTableSpec inSpec = inData[0].getDataTableSpec();
int numOfCols = inSpec.getNumColumns();
for (int c = 0; c < numOfCols; c++) {
DataType colType = inSpec.getColumnSpec(c).getType();
if (!colType.isCompatible(IntValue.class) && !colType.isCompatible(DoubleValue.class) && !colType.isCompatible(StringValue.class)) {
throw new IllegalStateException("Can only write Double, Int," + " and String columns to ARFF file.");
}
}
LOGGER.info("ARFF Writer: ARFFing into '" + m_location + "'.");
try (BufferedWriter writer = openWriter(localPath, url)) {
// Write ARFF header
writer.write("%\n");
writer.write("% ARFF data file, generated by KNIME\n");
writer.write("%\n");
writer.write("% Date: " + new Date(System.currentTimeMillis()) + "\n");
try {
writer.write("% User: " + System.getProperty("user.name") + "\n");
} catch (SecurityException se) {
// okay - we don't add the user name.
}
writer.write("%\n");
writer.write("\n@RELATION " + m_relationName + "\n");
// write the attribute part, i.e. the columns' name and type
for (int c = 0; c < numOfCols; c++) {
DataColumnSpec cSpec = inSpec.getColumnSpec(c);
writer.write("@ATTRIBUTE ");
if (needsQuotes(cSpec.getName().toString())) {
writer.write("'" + cSpec.getName().toString() + "'");
} else {
writer.write(cSpec.getName().toString());
}
writer.write("\t");
writer.write(colspecToARFFType(cSpec));
writer.write("\n");
}
// finally add the data
writer.write("\n@DATA\n");
long rowCnt = inData[0].size();
long rowNr = 0;
for (DataRow row : inData[0]) {
rowNr++;
exec.setProgress(rowNr / (double) rowCnt, "Writing row " + rowNr + " ('" + row.getKey() + "') of " + rowCnt);
if (m_sparse) {
writer.write("{");
}
// flag to skip comma in first column
boolean first = true;
for (int c = 0; c < row.getNumCells(); c++) {
DataCell cell = row.getCell(c);
if (m_sparse && !cell.isMissing()) {
// we write only non-zero values in a sparse file
if ((cell instanceof IntValue) && (((IntValue) cell).getIntValue() == 0)) {
continue;
}
if ((cell instanceof DoubleValue) && (Math.abs(((DoubleValue) cell).getDoubleValue()) < 1e-29)) {
continue;
}
}
String data = "?";
if (!cell.isMissing()) {
data = cell.toString();
}
// trigger quotes.
if (needsQuotes(data)) {
data = "'" + data + "'";
}
// now spit it out
if (!first) {
// print column separator
writer.write(",");
} else {
first = false;
}
// data in sparse file must be proceeded by the column number
if (m_sparse) {
writer.write("" + c + " ");
}
writer.write(data);
}
if (m_sparse) {
writer.write("}");
}
writer.write("\n");
// see if user told us to stop.
// Check if execution was canceled !
exec.checkCanceled();
}
// while (!rIter.atEnd())
} catch (CanceledExecutionException ex) {
if (localPath != null) {
Files.deleteIfExists(localPath);
LOGGER.debug("File '" + localPath + "' deleted.");
}
throw ex;
}
// execution successful return empty array
return new BufferedDataTable[0];
}
use of org.knime.core.data.IntValue in project knime-core by knime.
the class TableColumnToVariableNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
if (inData[0] instanceof BufferedDataTable) {
final BufferedDataTable table = (BufferedDataTable) inData[0];
final int colIndex = table.getSpec().findColumnIndex(m_column.getStringValue());
assert colIndex >= 0 : colIndex;
for (DataRow dataRow : table) {
DataCell cell = dataRow.getCell(colIndex);
if (cell.isMissing()) {
if (m_ignoreMissing.getBooleanValue()) {
continue;
}
throw new Exception("Missing value in column (" + m_column.getColumnName() + ") in row: " + dataRow.getKey());
}
if (cell instanceof IntValue) {
final IntValue iv = (IntValue) cell;
pushFlowVariableInt(dataRow.getKey().getString(), iv.getIntValue());
} else if (cell instanceof DoubleValue) {
final DoubleValue dv = (DoubleValue) cell;
pushFlowVariableDouble(dataRow.getKey().getString(), dv.getDoubleValue());
} else if (cell instanceof StringValue) {
final StringValue sv = (StringValue) cell;
pushFlowVariableString(dataRow.getKey().getString(), sv.getStringValue());
}
}
}
return new FlowVariablePortObject[] { FlowVariablePortObject.INSTANCE };
}
use of org.knime.core.data.IntValue in project knime-core by knime.
the class DatabaseHelper method fillStatement.
/**
* Set given column value into SQL statement.
* @param stmt statement used
* @param dbIdx database index to update/write
* @param cspec column spec to check type
* @param cell the data cell to write into the statement
* @param tz the {@link TimeZone} to use
* @param columnTypes
* @throws SQLException if the value can't be set
*/
protected void fillStatement(final PreparedStatement stmt, final int dbIdx, final DataColumnSpec cspec, final DataCell cell, final TimeZone tz, final Map<Integer, Integer> columnTypes) throws SQLException {
if (cspec.getType().isCompatible(BooleanValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.BOOLEAN);
} else {
boolean bool = ((BooleanValue) cell).getBooleanValue();
stmt.setBoolean(dbIdx, bool);
}
} else if (cspec.getType().isCompatible(IntValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.INTEGER);
} else {
int integer = ((IntValue) cell).getIntValue();
stmt.setInt(dbIdx, integer);
}
} else if (cspec.getType().isCompatible(LongValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.BIGINT);
} else {
long dbl = ((LongValue) cell).getLongValue();
stmt.setLong(dbIdx, dbl);
}
} else if (cspec.getType().isCompatible(DoubleValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.DOUBLE);
} else {
double dbl = ((DoubleValue) cell).getDoubleValue();
if (Double.isNaN(dbl)) {
stmt.setNull(dbIdx, Types.DOUBLE);
} else {
stmt.setDouble(dbIdx, dbl);
}
}
} else if (cspec.getType().isCompatible(DateAndTimeValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.DATE);
} else {
final DateAndTimeValue dateCell = (DateAndTimeValue) cell;
final long corrDate = dateCell.getUTCTimeInMillis() - tz.getOffset(dateCell.getUTCTimeInMillis());
if (!dateCell.hasTime() && !dateCell.hasMillis()) {
java.sql.Date date = new java.sql.Date(corrDate);
stmt.setDate(dbIdx, date);
} else if (!dateCell.hasDate()) {
java.sql.Time time = new java.sql.Time(corrDate);
stmt.setTime(dbIdx, time);
} else {
java.sql.Timestamp timestamp = new java.sql.Timestamp(corrDate);
stmt.setTimestamp(dbIdx, timestamp);
}
}
} else if (cspec.getType().isCompatible(BinaryObjectDataValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.BLOB);
} else {
try {
BinaryObjectDataValue value = (BinaryObjectDataValue) cell;
InputStream is = value.openInputStream();
if (is == null) {
stmt.setNull(dbIdx, Types.BLOB);
} else {
try {
// to be compatible with JDBC 3.0, the length of the stream is restricted to max integer,
// which are ~2GB; with JDBC 4.0 longs are supported and the respective method can be called
stmt.setBinaryStream(dbIdx, is, (int) value.length());
} catch (SQLException ex) {
// if no supported (i.e. SQLite) set byte array
byte[] bytes = IOUtils.toByteArray(is);
stmt.setBytes(dbIdx, bytes);
}
}
} catch (IOException ioe) {
stmt.setNull(dbIdx, Types.BLOB);
}
}
} else if (cspec.getType().isCompatible(CollectionDataValue.class)) {
fillArray(stmt, dbIdx, cell, tz);
} else if ((columnTypes == null) || cspec.getType().isCompatible(StringValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.VARCHAR);
} else {
stmt.setString(dbIdx, cell.toString());
}
} else {
Integer sqlType = columnTypes.get(dbIdx);
if (sqlType == null) {
sqlType = Types.VARCHAR;
}
if (cell.isMissing()) {
stmt.setNull(dbIdx, sqlType);
} else {
stmt.setObject(dbIdx, cell.toString(), sqlType);
}
}
}
use of org.knime.core.data.IntValue in project knime-core by knime.
the class DatabaseWriterConnection method fillStatement.
/**
* Set given column value into SQL statement.
* @param stmt statement used
* @param dbIdx database index to update/write
* @param cspec column spec to check type
* @param cell the data cell to write into the statement
* @param tz the {@link TimeZone} to use
* @throws SQLException if the value can't be set
*/
private static void fillStatement(final PreparedStatement stmt, final int dbIdx, final DataColumnSpec cspec, final DataCell cell, final TimeZone tz, final Map<Integer, Integer> columnTypes) throws SQLException {
if (cspec.getType().isCompatible(BooleanValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.BOOLEAN);
} else {
boolean bool = ((BooleanValue) cell).getBooleanValue();
stmt.setBoolean(dbIdx, bool);
}
} else if (cspec.getType().isCompatible(IntValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.INTEGER);
} else {
int integer = ((IntValue) cell).getIntValue();
stmt.setInt(dbIdx, integer);
}
} else if (cspec.getType().isCompatible(LongValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.BIGINT);
} else {
long dbl = ((LongValue) cell).getLongValue();
stmt.setLong(dbIdx, dbl);
}
} else if (cspec.getType().isCompatible(DoubleValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.DOUBLE);
} else {
double dbl = ((DoubleValue) cell).getDoubleValue();
if (Double.isNaN(dbl)) {
stmt.setNull(dbIdx, Types.DOUBLE);
} else {
stmt.setDouble(dbIdx, dbl);
}
}
} else if (cspec.getType().isCompatible(DateAndTimeValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.DATE);
} else {
final DateAndTimeValue dateCell = (DateAndTimeValue) cell;
final long corrDate = dateCell.getUTCTimeInMillis() - tz.getOffset(dateCell.getUTCTimeInMillis());
if (!dateCell.hasTime() && !dateCell.hasMillis()) {
java.sql.Date date = new java.sql.Date(corrDate);
stmt.setDate(dbIdx, date);
} else if (!dateCell.hasDate()) {
java.sql.Time time = new java.sql.Time(corrDate);
stmt.setTime(dbIdx, time);
} else {
java.sql.Timestamp timestamp = new java.sql.Timestamp(corrDate);
stmt.setTimestamp(dbIdx, timestamp);
}
}
} else if (cspec.getType().isCompatible(BinaryObjectDataValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.BLOB);
} else {
try {
BinaryObjectDataValue value = (BinaryObjectDataValue) cell;
InputStream is = value.openInputStream();
if (is == null) {
stmt.setNull(dbIdx, Types.BLOB);
} else {
try {
// to be compatible with JDBC 3.0, the length of the stream is restricted to max integer,
// which are ~2GB; with JDBC 4.0 longs are supported and the respective method can be called
stmt.setBinaryStream(dbIdx, is, (int) value.length());
} catch (SQLException ex) {
// if no supported (i.e. SQLite) set byte array
byte[] bytes = IOUtils.toByteArray(is);
stmt.setBytes(dbIdx, bytes);
}
}
} catch (IOException ioe) {
stmt.setNull(dbIdx, Types.BLOB);
}
}
} else if ((columnTypes == null) || cspec.getType().isCompatible(StringValue.class)) {
if (cell.isMissing()) {
stmt.setNull(dbIdx, Types.VARCHAR);
} else {
stmt.setString(dbIdx, cell.toString());
}
} else {
Integer sqlType = columnTypes.get(dbIdx);
if (sqlType == null) {
sqlType = Types.VARCHAR;
}
if (cell.isMissing()) {
stmt.setNull(dbIdx, sqlType);
} else {
stmt.setObject(dbIdx, cell.toString(), sqlType);
}
}
}
Aggregations