Search in sources :

Example 71 with DataColumnSpecCreator

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()));
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) DoubleValue(org.knime.core.data.DoubleValue) ExtensibleUtilityFactory(org.knime.core.data.ExtensibleUtilityFactory) UtilityFactory(org.knime.core.data.DataValue.UtilityFactory) Test(org.junit.Test)

Example 72 with DataColumnSpecCreator

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();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) HashMap(java.util.HashMap) Node(org.knime.core.node.Node) NodeModel(org.knime.core.node.NodeModel) ExecutionContext(org.knime.core.node.ExecutionContext) DataColumnSpec(org.knime.core.data.DataColumnSpec) VirtualParallelizedChunkPortObjectInNodeFactory(org.knime.core.node.workflow.virtual.parchunk.VirtualParallelizedChunkPortObjectInNodeFactory) NodeFactory(org.knime.core.node.NodeFactory) VirtualParallelizedChunkPortObjectInNodeFactory(org.knime.core.node.workflow.virtual.parchunk.VirtualParallelizedChunkPortObjectInNodeFactory) DefaultNodeProgressMonitor(org.knime.core.node.DefaultNodeProgressMonitor) PortType(org.knime.core.node.port.PortType) Before(org.junit.Before)

Example 73 with DataColumnSpecCreator

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);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) LinkedHashMap(java.util.LinkedHashMap) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) DataType(org.knime.core.data.DataType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 74 with DataColumnSpecCreator

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;
}
Also used : Pattern(java.util.regex.Pattern) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) WildcardMatcher(org.knime.base.util.WildcardMatcher) Matcher(java.util.regex.Matcher) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) StringCell(org.knime.core.data.def.StringCell) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 75 with DataColumnSpecCreator

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);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator)

Aggregations

DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)267 DataColumnSpec (org.knime.core.data.DataColumnSpec)210 DataTableSpec (org.knime.core.data.DataTableSpec)132 DataCell (org.knime.core.data.DataCell)92 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)77 DataType (org.knime.core.data.DataType)74 DataRow (org.knime.core.data.DataRow)73 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)57 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)51 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)48 ArrayList (java.util.ArrayList)46 DoubleCell (org.knime.core.data.def.DoubleCell)45 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)44 StringCell (org.knime.core.data.def.StringCell)29 BufferedDataTable (org.knime.core.node.BufferedDataTable)23 DoubleValue (org.knime.core.data.DoubleValue)22 HashSet (java.util.HashSet)19 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)17 DataColumnDomain (org.knime.core.data.DataColumnDomain)16 DefaultRow (org.knime.core.data.def.DefaultRow)16