use of org.knime.core.util.Pair in project knime-core by knime.
the class VariableToTableNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
DataTableSpec spec = createOutSpec();
BufferedDataContainer cont = exec.createDataContainer(spec);
List<Pair<String, FlowVariable.Type>> vars;
if (m_settings.getIncludeAll()) {
vars = getAllVariables();
} else {
vars = m_settings.getVariablesOfInterest();
}
DataCell[] specs = new DataCell[vars.size()];
List<String> lostVariables = new ArrayList<String>();
for (int i = 0; i < vars.size(); i++) {
Pair<String, FlowVariable.Type> c = vars.get(i);
String name = c.getFirst();
// fallback
DataCell cell = DataType.getMissingCell();
switch(c.getSecond()) {
case DOUBLE:
try {
double dValue = peekFlowVariableDouble(c.getFirst());
cell = new DoubleCell(dValue);
} catch (NoSuchElementException e) {
lostVariables.add(name + " (Double)");
}
break;
case INTEGER:
try {
int iValue = peekFlowVariableInt(c.getFirst());
cell = new IntCell(iValue);
} catch (NoSuchElementException e) {
lostVariables.add(name + " (Integer)");
}
break;
case STRING:
try {
String sValue = peekFlowVariableString(c.getFirst());
sValue = sValue == null ? "" : sValue;
cell = new StringCell(sValue);
} catch (NoSuchElementException e) {
lostVariables.add(name + " (String)");
}
break;
}
specs[i] = cell;
}
cont.addRowToTable(new DefaultRow("values", specs));
cont.close();
return new BufferedDataTable[] { cont.getTable() };
}
use of org.knime.core.util.Pair in project knime-core by knime.
the class RearrangeColumnsTable method calcNewCellsForRow.
/**
* Calls for an input row the list of cell factories to produce the output row (contains only the new cells, merged
* later).
*
* @param unconvertedRow The input row to be processed
* @param producerMap For each new (or replaced) column the factory.
* @return The output row.
*/
static DataRow calcNewCellsForRow(final DataRow unconvertedRow, final NewColumnsProducerMapping producerMap) {
final int newColCount = producerMap.getAllNewColumnsList().size();
DataCell[] newCells = new DataCell[newColCount];
DataRow row = applyDataTypeConverters(unconvertedRow, producerMap, newCells);
IdentityHashMap<CellFactory, List<Pair<Integer, Integer>>> uniqueCellFactoryMap = producerMap.getUniqueCellFactoryMap();
for (Map.Entry<CellFactory, List<Pair<Integer, Integer>>> e : uniqueCellFactoryMap.entrySet()) {
CellFactory factory = e.getKey();
List<Pair<Integer, Integer>> list = e.getValue();
DataCell[] fromFac = factory.getCells(row);
if (fromFac.length != list.size()) {
String error = String.format("New cells array length conflict: expected %d, actual %d (class %s)", list.size(), fromFac.length, factory.getClass().getName());
if (fromFac.length < list.size()) {
throw new IndexOutOfBoundsException(error);
} else {
// such problems were ignored until 2.6 -- print warning only
if (codingProblemsCellFactoryClasses == null) {
codingProblemsCellFactoryClasses = new HashSet<Class<? extends CellFactory>>();
}
if (codingProblemsCellFactoryClasses.add(factory.getClass())) {
LOGGER.coding(error);
}
}
}
final int length = list.size();
for (int i = 0; i < length; i++) {
Pair<Integer, Integer> indexPair = list.get(i);
int indexInNewCellsArray = indexPair.getFirst();
int indexInFactory = indexPair.getSecond();
assert newCells[indexInNewCellsArray] == null : "New cells array at index expected to be null";
newCells[indexInNewCellsArray] = fromFac[indexInFactory];
}
}
DataRow appendix = new DefaultRow(row.getKey(), newCells);
return appendix;
}
use of org.knime.core.util.Pair in project knime-core by knime.
the class RearrangeColumnsTable method applyDataTypeConverters.
/**
* Used when {@link ColumnRearranger#ensureColumnIsConverted(DataCellTypeConverter, int)} is called. It
* preproccesses the row and replaces the column to be converted by the the result of the given converter.
*
* @param row The original input row.
* @param producerMap The object having the converter list (or not)
* @param newCells
* @return The input row if no converter applied or a modified copy of the input row.
*/
private static DataRow applyDataTypeConverters(final DataRow row, final NewColumnsProducerMapping producerMap, final DataCell[] newCells) {
List<Pair<SpecAndFactoryObject, Integer>> converterToIndexMap = producerMap.getConverterToIndexMap();
if (!converterToIndexMap.isEmpty()) {
DataCell[] inputRowCells = new DataCell[row.getNumCells()];
for (int i = 0; i < inputRowCells.length; i++) {
inputRowCells[i] = row instanceof BlobSupportDataRow ? ((BlobSupportDataRow) row).getRawCell(i) : row.getCell(i);
}
for (Pair<SpecAndFactoryObject, Integer> entry : converterToIndexMap) {
SpecAndFactoryObject specAndObject = entry.getFirst();
DataCellTypeConverter converter = specAndObject.getConverter();
int converterIndex = specAndObject.getConverterIndex();
Integer index = entry.getSecond();
DataCell convertedCell = converter.callConvert(row.getCell(converterIndex));
newCells[index] = convertedCell;
inputRowCells[converterIndex] = convertedCell;
}
return new BlobSupportDataRow(row.getKey(), inputRowCells);
}
return row;
}
use of org.knime.core.util.Pair in project knime-core by knime.
the class ClassUtil method getMethodsWithAnnotation.
/**
* Get a Collection of methods which are annotated with the given {@link Annotation} paired with the Annotation
* itself.
*
* @param cls Class to get annotated methods from
* @param annotationClass the annotation
* @return Collection of pairs of methods together with the instance of <code>annotationClass</code> they are
* annotated with
*/
public static <A extends Annotation> Collection<Pair<Method, A>> getMethodsWithAnnotation(final Class<?> cls, final Class<A> annotationClass) {
// result list of annotated methods
final ArrayList<Pair<Method, A>> annotatedMethods = new ArrayList<>();
// all methods declared in exactly this class (not in superclasses)
final Method[] declaredMethods = cls.getDeclaredMethods();
for (final Method m : declaredMethods) {
// get an instance of the given annotation
final A a = m.getAnnotation(annotationClass);
if (a != null) {
// present, therefore add to the list
annotatedMethods.add(new Pair<>(m, a));
}
}
return annotatedMethods;
}
use of org.knime.core.util.Pair in project knime-core by knime.
the class ExtendedStatisticsNodeModel method loadInternals.
/**
* {@inheritDoc}
*/
@Override
protected void loadInternals(final File internDir, final ExecutionMonitor exec) throws IOException {
NodeSettingsRO sett = NodeSettings.loadFromXML(new FileInputStream(new File(internDir, "statistic.xml.gz")));
try {
m_statTable = Statistics3Table.load(sett);
} catch (InvalidSettingsException ise) {
throw new IOException(ise);
}
double[] means = getStatTable().getMean();
if (m_enableHiLite.getBooleanValue()) {
File histogramsGz = new File(internDir, HISTOGRAMS_GZ);
File dataArrayGz = new File(internDir, DATA_ARRAY_GZ);
try {
Set<String> nominalColumnNames = new LinkedHashSet<String>();
String[] columnNames = getStatTable().getColumnNames();
for (int i = 0; i < columnNames.length; ++i) {
if (getStatTable().getNominalValues(i) != null) {
nominalColumnNames.add(columnNames[i]);
}
}
Pair<Pair<Map<Integer, ? extends HistogramModel<?>>, Map<Integer, Map<Integer, Set<RowKey>>>>, Map<Integer, Map<DataValue, Set<RowKey>>>> ppair = HistogramColumn.loadHistograms(histogramsGz, dataArrayGz, nominalColumnNames, BIN_SELECTION_STRATEGY, means);
m_histograms = ppair.getFirst().getFirst();
m_buckets = ppair.getFirst().getSecond();
m_nominalKeys = ppair.getSecond();
// m_nominalTypes = ppair.getSecond().getSecond();
} catch (InvalidSettingsException e) {
getLogger().error("Failed to load settings for the HiLite, please rerun.", e);
m_histograms = Collections.emptyMap();
m_buckets = Collections.emptyMap();
m_nominalKeys = Collections.emptyMap();
// m_nominalTypes = Collections.emptyMap();
}
} else {
File histogramsGz = new File(internDir, HISTOGRAMS_GZ);
m_nominalKeys = Collections.emptyMap();
try {
m_buckets = new HashMap<Integer, Map<Integer, Set<RowKey>>>();
m_histograms = HistogramColumn.loadHistograms(histogramsGz, m_buckets, BIN_SELECTION_STRATEGY, means);
m_buckets.clear();
} catch (InvalidSettingsException e) {
m_histograms = Collections.emptyMap();
m_buckets = Collections.emptyMap();
}
}
}
Aggregations