use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.
the class ExtensibleUtilityFactoryTest method testGetRendererFamily.
/**
* Checks if the creation of actual renderer (via the family) works correct.
*/
@Test
public void testGetRendererFamily() {
DataValueRendererFamily family = StringCell.TYPE.getRenderer(new DataColumnSpecCreator("Test", StringCell.TYPE).createSpec());
assertThat("Wrong number of renderers in family", family.getRendererDescriptions().length, is(2));
IEclipsePreferences corePrefs = InstanceScope.INSTANCE.getNode(FrameworkUtil.getBundle(DataValueRendererFactory.class).getSymbolicName());
DataValueRendererFactory rendererFactory = new DoubleGrayValueRenderer.Factory();
corePrefs.put(ExtensibleUtilityFactory.getPreferenceKey(DoubleValue.class), rendererFactory.getId());
DataColumnSpec colSpec = new DataColumnSpecCreator("Test", DoubleCell.TYPE).createSpec();
family = DoubleCell.TYPE.getRenderer(colSpec);
assertThat("Wrong preferred renderer in family list at position 0", family.getRendererDescriptions()[0], is(rendererFactory.createRenderer(colSpec).getDescription()));
}
use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.
the class AbstractColumnTableSorterTest method setUp.
/**
* @throws java.lang.Exception
*/
@SuppressWarnings("rawtypes")
@Before
public void setUp() throws Exception {
@SuppressWarnings("unchecked") NodeFactory<NodeModel> dummyFactory = (NodeFactory) new VirtualParallelizedChunkPortObjectInNodeFactory(new PortType[0]);
m_exec = new ExecutionContext(new DefaultNodeProgressMonitor(), new Node(dummyFactory), SingleNodeContainer.MemoryPolicy.CacheOnDisc, new HashMap<Integer, ContainerTable>());
DataColumnSpec[] colSpecs = new DataColumnSpec[] { new DataColumnSpecCreator(FEATURE1, DoubleCell.TYPE).createSpec(), new DataColumnSpecCreator(FEATURE2, DoubleCell.TYPE).createSpec(), new DataColumnSpecCreator(STRING_FEATURE, StringCell.TYPE).createSpec(), new DataColumnSpecCreator(CLASS, StringCell.TYPE).createSpec() };
DataTableSpec spec = new DataTableSpec(colSpecs);
final BufferedDataContainer container = m_exec.createDataContainer(spec);
int i = 0;
container.addRowToTable(creatRow(i++, 1, 8, "A", "AClass8"));
container.addRowToTable(creatRow(i++, 2, 2, "Z", "ZClass2"));
container.addRowToTable(creatRow(i++, 3, 5, "B", "BClass5"));
container.addRowToTable(creatRow(i++, 4, 0, "E", "EClass0"));
container.addRowToTable(creatRow(i++, 5, 1, "F", "FClass1"));
container.addRowToTable(creatRow(i++, 6, 7, "G", "GClass7"));
container.addRowToTable(creatRow(i++, 7, 9, "H", "HClass9"));
container.addRowToTable(creatRow(i++, 8, 8, null, "Class8"));
container.close();
testTable = container.getTable();
final BufferedDataContainer emptyContainer = m_exec.createDataContainer(spec);
emptyContainer.close();
emptyTestTable = emptyContainer.getTable();
MemoryAlertSystemTest.forceGC();
}
use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.
the class AppendedRowsTable method generateDataTableSpec.
/**
* Factory method that determines the final {@link DataTableSpec} given the
* tables.
*
* @param tableSpecs the table specs as in the constructor
* @return the outcoming {qlink DataTableSpec}
* @see #AppendedRowsTable(DataTable[])
*/
public static final DataTableSpec generateDataTableSpec(final DataTableSpec... tableSpecs) {
// memorize the first column spec in the argument array for
// each column name, we use it later on to initialize the column
// spec creator.
LinkedHashMap<String, DataColumnSpec> columnSet = new LinkedHashMap<String, DataColumnSpec>();
LinkedHashMap<String, DataType> typeSet = new LinkedHashMap<String, DataType>();
LinkedHashMap<String, DataColumnDomain> domainSet = new LinkedHashMap<String, DataColumnDomain>();
// create final data table spec
for (int i = 0; i < tableSpecs.length; i++) {
DataTableSpec cur = tableSpecs[i];
for (int c = 0; c < cur.getNumColumns(); c++) {
DataColumnSpec colSpec = cur.getColumnSpec(c);
String colName = colSpec.getName();
// set the spec for this column if not yet done
if (!columnSet.containsKey(colName)) {
columnSet.put(colName, colSpec);
}
DataType colType = colSpec.getType();
DataColumnDomain colDomain = colSpec.getDomain();
// duplicates are welcome - but only if they match the type
if (typeSet.containsKey(colName)) {
DataType oldType = typeSet.get(colName);
DataColumnDomain oldDomain = domainSet.get(colName);
// the base type they share
DataType type = DataType.getCommonSuperType(oldType, colType);
assert type.isASuperTypeOf(oldType);
assert type.isASuperTypeOf(colType);
// that shouldn't happen though, eh: shit happens.
if (!oldType.equals(type)) {
LOGGER.info("Confusing data types for column \"" + colName + "\": " + oldType.toString() + " vs. " + colType.toString() + "\n" + "Using common base type " + type.toString());
// that must not change the order.
typeSet.put(colName, type);
}
DataColumnDomain newDomain = merge(oldDomain, colDomain, type.getComparator());
domainSet.put(colName, newDomain);
} else {
// doesn't contain the key
typeSet.put(colName, colType);
domainSet.put(colName, colDomain);
}
}
// for all columns in the current table spec
}
// for all tables
DataColumnSpec[] colSpecs = new DataColumnSpec[typeSet.size()];
int i = 0;
for (Map.Entry<String, DataType> entry : typeSet.entrySet()) {
String name = entry.getKey();
DataType type = entry.getValue();
// domain is null, if we did not remember it (e.g. "keepDomain" was
// false)
DataColumnDomain domain = domainSet.get(name);
DataColumnSpec initSpec = columnSet.get(name);
DataColumnSpecCreator specCreator = new DataColumnSpecCreator(initSpec);
specCreator.setDomain(domain);
specCreator.setType(type);
colSpecs[i++] = specCreator.createSpec();
}
return new DataTableSpec(colSpecs);
}
use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.
the class StringReplacerNodeModel method createColumnRearranger.
/**
* Creates the column rearranger that computes the new cells.
*
* @param spec the spec of the input table
* @return a column rearranger
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
final Pattern pattern = createPattern(m_settings);
DataColumnSpec colSpec;
if (m_settings.createNewColumn()) {
colSpec = new DataColumnSpecCreator(m_settings.newColumnName(), StringCell.TYPE).createSpec();
} else {
colSpec = new DataColumnSpecCreator(m_settings.columnName(), StringCell.TYPE).createSpec();
}
final String replacement;
if (m_settings.patternIsRegex()) {
replacement = m_settings.replacement();
} else {
replacement = m_settings.replacement().replaceAll("(\\$\\d+)", "\\\\$1");
}
final int index = spec.findColumnIndex(m_settings.columnName());
SingleCellFactory cf = new SingleCellFactory(colSpec) {
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(index);
if (cell.isMissing()) {
return cell;
}
final String stringValue = ((StringValue) cell).getStringValue();
Matcher m = pattern.matcher(stringValue);
if (m_settings.replaceAllOccurrences()) {
return new StringCell(m.replaceAll(replacement));
} else if (m.matches()) {
if (".*".equals(pattern.pattern())) {
// therefore the replacement value is doubled
return new StringCell(replacement);
} else {
return new StringCell(m.replaceAll(replacement));
}
} else {
return new StringCell(stringValue);
}
}
};
ColumnRearranger crea = new ColumnRearranger(spec);
if (m_settings.createNewColumn()) {
if (spec.containsName(m_settings.newColumnName())) {
throw new InvalidSettingsException("Duplicate column name: " + m_settings.newColumnName());
}
crea.append(cf);
} else {
crea.replace(cf, m_settings.columnName());
}
return crea;
}
use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.
the class ColorManager2NodeModel method getOutSpec.
/**
* Appends the given <code>ColorHandler</code> to the given
* <code>DataTableSpec</code> for the given column. If the spec
* already contains a ColorHandler, it will be removed and replaced by
* the new one.
* @param spec to which the ColorHandler is appended
* @param columnName for this column
* @param colorHdl ColorHandler
* @return a new spec with ColorHandler
*/
static final DataTableSpec getOutSpec(final DataTableSpec spec, final String columnName, final ColorHandler colorHdl) {
DataColumnSpec[] cspecs = new DataColumnSpec[spec.getNumColumns()];
for (int i = 0; i < cspecs.length; i++) {
DataColumnSpec cspec = spec.getColumnSpec(i);
DataColumnSpecCreator cr = new DataColumnSpecCreator(cspec);
if (cspec.getName().equals(columnName)) {
cr.setColorHandler(colorHdl);
} else {
// delete other ColorHandler
cr.setColorHandler(null);
}
cspecs[i] = cr.createSpec();
}
return new DataTableSpec(cspecs);
}
Aggregations