use of org.knime.core.data.append.AppendedRowsTable in project knime-core by knime.
the class EndifNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] rawInData, final ExecutionContext exec) throws Exception {
if (m_enableHiliting) {
// create empty hilite translation map (so we correctly
// handle the internals even if we return with a IBPO:
Map<RowKey, Set<RowKey>> map = new HashMap<RowKey, Set<RowKey>>();
m_hiliteTranslator.setMapper(new DefaultHiLiteMapper(map));
}
if (rawInData[0] instanceof InactiveBranchPortObject) {
return new PortObject[] { rawInData[1] };
}
if (rawInData[1] instanceof InactiveBranchPortObject) {
return new PortObject[] { rawInData[0] };
}
// no inactive branch - check compatibility of specs - which in
// this case must be BFT Specs!
DataTableSpec spec0 = (DataTableSpec) (rawInData[0].getSpec());
DataTableSpec spec1 = (DataTableSpec) (rawInData[1].getSpec());
if (spec0.equalStructure(spec1)) {
// concatenate tables and return result
BufferedDataTable[] inData = new BufferedDataTable[2];
inData[0] = (BufferedDataTable) rawInData[0];
inData[1] = (BufferedDataTable) rawInData[1];
int totalRowCount = 0;
for (BufferedDataTable t : inData) {
totalRowCount += t.getRowCount();
}
AppendedRowsTable out = new AppendedRowsTable((m_isAppendSuffix ? m_suffix : null), inData);
// note, this iterator throws runtime exceptions when canceled.
AppendedRowsIterator it = out.iterator(exec, totalRowCount);
BufferedDataContainer c = exec.createDataContainer(out.getDataTableSpec());
try {
while (it.hasNext()) {
// may throw exception, also sets progress
c.addRowToTable(it.next());
}
} catch (AppendedRowsIterator.RuntimeCanceledExecutionException rcee) {
throw rcee.getCause();
} finally {
c.close();
}
if (it.getNrRowsSkipped() > 0) {
setWarningMessage("Filtered out " + it.getNrRowsSkipped() + " duplicate row id(s).");
}
if (m_enableHiliting) {
// create hilite translation map
Map<RowKey, Set<RowKey>> map = new HashMap<RowKey, Set<RowKey>>();
// map of all RowKeys and duplicate RowKeys in the resulting table
Map<RowKey, RowKey> dupMap = it.getDuplicateNameMap();
for (Map.Entry<RowKey, RowKey> e : dupMap.entrySet()) {
// if a duplicate key
if (!e.getKey().equals(e.getValue())) {
Set<RowKey> set = Collections.singleton(e.getValue());
// put duplicate key and original key into map
map.put(e.getKey(), set);
} else {
// skip duplicate keys
if (!dupMap.containsKey(new RowKey(e.getKey().getString() + m_suffix))) {
Set<RowKey> set = Collections.singleton(e.getValue());
map.put(e.getKey(), set);
}
}
}
m_hiliteTranslator.setMapper(new DefaultHiLiteMapper(map));
}
return new BufferedDataTable[] { c.getTable() };
}
throw new Exception("Both input ports have data but the tables " + "have incompatible specs");
}
use of org.knime.core.data.append.AppendedRowsTable in project knime-core by knime.
the class EndcaseNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
Vector<BufferedDataTable> tables = new Vector<BufferedDataTable>();
for (int i = 0; i < getNrInPorts(); i++) {
if (inData[i] != null) {
// if connected...
if (!(inData[i] instanceof InactiveBranchPortObject)) {
// ...and active, add it:
tables.add((BufferedDataTable) inData[i]);
}
}
}
if (tables.size() == 0) {
// be connected!)
assert inData[0] instanceof InactiveBranchPortObject;
if (m_enableHiliting) {
// create empty hilite translation map (so we correctly
// handle the internals).
Map<RowKey, Set<RowKey>> map = new HashMap<RowKey, Set<RowKey>>();
m_hiliteTranslator.setMapper(new DefaultHiLiteMapper(map));
}
return new PortObject[] { inData[0] };
}
assert tables.size() > 0;
// check compatibility of specs against first spec in list
for (int i = 1; i < tables.size(); i++) {
if (!(tables.get(0).getSpec().equalStructure(tables.get(i).getSpec()))) {
// incompatible - refuse to execute
throw new Exception("The data table structures of the active " + "ports are not compatible.");
}
}
int totalRowCount = 0;
DataTable[] dtables = new DataTable[tables.size()];
int i = 0;
for (BufferedDataTable t : tables) {
totalRowCount += t.getRowCount();
dtables[i] = t;
i++;
}
AppendedRowsTable out = new AppendedRowsTable((m_isAppendSuffix ? m_suffix : null), dtables);
// note, this iterator throws runtime exceptions when canceled.
AppendedRowsIterator it = out.iterator(exec, totalRowCount);
BufferedDataContainer c = exec.createDataContainer(out.getDataTableSpec());
try {
while (it.hasNext()) {
// may throw exception, also sets progress
c.addRowToTable(it.next());
}
} catch (RuntimeCanceledExecutionException rcee) {
throw rcee.getCause();
} finally {
c.close();
}
if (it.getNrRowsSkipped() > 0) {
setWarningMessage("Filtered out " + it.getNrRowsSkipped() + " duplicate row id(s).");
}
if (m_enableHiliting) {
// create hilite translation map
Map<RowKey, Set<RowKey>> map = new HashMap<RowKey, Set<RowKey>>();
// map of all RowKeys and duplicate RowKeys in the resulting table
Map<RowKey, RowKey> dupMap = it.getDuplicateNameMap();
for (Map.Entry<RowKey, RowKey> e : dupMap.entrySet()) {
// if a duplicate key
if (!e.getKey().equals(e.getValue())) {
Set<RowKey> set = Collections.singleton(e.getValue());
// put duplicate key and original key into map
map.put(e.getKey(), set);
} else {
// skip duplicate keys
if (!dupMap.containsKey(new RowKey(e.getKey().getString() + m_suffix))) {
Set<RowKey> set = Collections.singleton(e.getValue());
map.put(e.getKey(), set);
}
}
}
m_hiliteTranslator.setMapper(new DefaultHiLiteMapper(map));
}
return new BufferedDataTable[] { c.getTable() };
}
use of org.knime.core.data.append.AppendedRowsTable in project knime-core by knime.
the class ConcatenateTableFactory method copyTablesIntoOneTable.
/**
* Copies all tables, except the last still not-closed table, into an entire new table
*/
private void copyTablesIntoOneTable(final ExecutionContext exec) throws CanceledExecutionException {
BufferedDataTable[] tables = new BufferedDataTable[m_tables.size() - 1];
for (int i = 0; i < tables.length; i++) {
tables[i] = m_tables.get(i).getTable();
}
AppendedRowsTable wrapper = new AppendedRowsTable(org.knime.core.data.append.AppendedRowsTable.DuplicatePolicy.Fail, null, tables);
BufferedDataContainer con = exec.createDataContainer(wrapper.getDataTableSpec());
RowIterator rowIt = wrapper.iterator();
exec.setProgress("Too many tables. Copy tables into one table.");
while (rowIt.hasNext()) {
exec.checkCanceled();
con.addRowToTable(rowIt.next());
}
con.close();
BufferedDataContainer last = m_tables.get(m_tables.size() - 1);
m_tables.clear();
m_tables.add(con);
m_tables.add(last);
exec.setProgress("Tables copied into one.");
}
use of org.knime.core.data.append.AppendedRowsTable in project knime-core by knime.
the class AppendedRowsNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] rawInData, final ExecutionContext exec) throws Exception {
// remove all null tables first (optional input data)
BufferedDataTable[] noNullArray = noNullArray(rawInData);
DataTableSpec[] noNullSpecs = new DataTableSpec[noNullArray.length];
for (int i = 0; i < noNullArray.length; i++) {
noNullSpecs[i] = noNullArray[i].getDataTableSpec();
}
// table can only be wrapped if a suffix is to be append or the node fails in case of duplicate row ID's
if (m_isAppendSuffix || m_isFailOnDuplicate) {
// just wrap the tables virtually instead of traversing it and copying the rows
// virtually create the concatenated table (no traverse necessary)
Optional<String> suffix = m_isAppendSuffix ? Optional.of(m_suffix) : Optional.empty();
BufferedDataTable concatTable = exec.createConcatenateTable(exec, suffix, m_isFailOnDuplicate, noNullArray);
if (m_isIntersection) {
// wrap the table and filter the non-intersecting columns
DataTableSpec actualOutSpec = getOutputSpec(noNullSpecs);
DataTableSpec currentOutSpec = concatTable.getDataTableSpec();
String[] intersectCols = getIntersection(actualOutSpec, currentOutSpec);
ColumnRearranger cr = new ColumnRearranger(currentOutSpec);
cr.keepOnly(intersectCols);
concatTable = exec.createColumnRearrangeTable(concatTable, cr, exec);
}
if (m_enableHiliting) {
AppendedRowsTable tmp = new AppendedRowsTable(DuplicatePolicy.Fail, null, noNullArray);
Map<RowKey, Set<RowKey>> map = createHiliteTranslationMap(createDuplicateMap(tmp, exec, m_suffix == null ? "" : m_suffix));
m_hiliteTranslator.setMapper(new DefaultHiLiteMapper(map));
}
return new BufferedDataTable[] { concatTable };
} else {
// traverse the table and copy the rows
long totalRowCount = 0L;
RowInput[] inputs = new RowInput[noNullArray.length];
for (int i = 0; i < noNullArray.length; i++) {
totalRowCount += noNullArray[i].size();
inputs[i] = new DataTableRowInput(noNullArray[i]);
}
DataTableSpec outputSpec = getOutputSpec(noNullSpecs);
BufferedDataTableRowOutput output = new BufferedDataTableRowOutput(exec.createDataContainer(outputSpec));
run(inputs, output, exec, totalRowCount);
return new BufferedDataTable[] { output.getDataTable() };
}
}
Aggregations