Search in sources :

Example 1 with FilterHandler

use of org.knime.core.data.property.filter.FilterHandler in project knime-core by knime.

the class DataColumnSpecCreator method merge.

/**
 * Merges the existing {@link DataColumnSpec} with a second
 * {@link DataColumnSpec}. If they have equal structure, the domain
 * information and properties from both DataColumnSpecs is merged,
 * Color, Shape and Size-Handlers are compared (must be equal).
 *
 * @param cspec2 the second {@link DataColumnSpec}.
 *
 * @see DataTableSpec#mergeDataTableSpecs(DataTableSpec...)
 * @throws IllegalArgumentException if the structure (type and name) does
 *             not match, if the domain cannot be merged, if the Color-,
 *             Shape- or SizeHandlers are different or the sub element
 *             names are not equal.
 */
public void merge(final DataColumnSpec cspec2) {
    if (!cspec2.getName().equals(m_name) || !cspec2.getType().equals(m_type)) {
        throw new IllegalArgumentException("Structures of DataColumnSpecs" + " do not match.");
    }
    DataColumnDomain domain2 = cspec2.getDomain();
    boolean hasDomainChanged = false;
    final Set<DataCell> myValues = m_domain.getValues();
    final Set<DataCell> oValues = domain2.getValues();
    Set<DataCell> newValues;
    if (myValues == null || oValues == null) {
        newValues = null;
        hasDomainChanged |= myValues != null;
    } else if (myValues.equals(oValues)) {
        newValues = myValues;
    } else {
        newValues = new LinkedHashSet<DataCell>(myValues);
        newValues.addAll(oValues);
        hasDomainChanged = true;
    }
    DataValueComparator comparator = m_type.getComparator();
    final DataCell myLower = m_domain.getLowerBound();
    final DataCell oLower = domain2.getLowerBound();
    DataCell newLower;
    if (myLower == null || oLower == null) {
        newLower = null;
        hasDomainChanged |= myLower != null;
    } else if (myLower.equals(oLower)) {
        newLower = myLower;
    } else if (comparator.compare(myLower, oLower) > 0) {
        newLower = oLower;
        hasDomainChanged = true;
    } else {
        newLower = myLower;
    }
    final DataCell myUpper = m_domain.getUpperBound();
    final DataCell oUpper = domain2.getUpperBound();
    DataCell newUpper;
    if (myUpper == null || oUpper == null) {
        newUpper = null;
        hasDomainChanged |= myUpper != null;
    } else if (myUpper.equals(oUpper)) {
        newUpper = myUpper;
    } else if (comparator.compare(myUpper, oUpper) < 0) {
        newUpper = oUpper;
        hasDomainChanged = true;
    } else {
        newUpper = myUpper;
    }
    if (hasDomainChanged) {
        setDomain(new DataColumnDomain(newLower, newUpper, newValues));
    }
    // check for redundant color handler
    ColorHandler colorHandler2 = cspec2.getColorHandler();
    if (!Objects.equals(m_colorHandler, colorHandler2)) {
        LOGGER.warn("Column has already a color handler attached, ignoring new handler.");
    }
    // check for redundant shape handler
    ShapeHandler shapeHandler2 = cspec2.getShapeHandler();
    if (!Objects.equals(m_shapeHandler, shapeHandler2)) {
        LOGGER.warn("Column has already a shape handler attached, ignoring new handler.");
    }
    // check for redundant size handler
    SizeHandler sizeHandler2 = cspec2.getSizeHandler();
    if (!Objects.equals(m_sizeHandler, sizeHandler2)) {
        LOGGER.warn("Column has already a size handler attached, ignoring new handler.");
    }
    // check for redundant filter handler
    FilterHandler filterHandler = cspec2.getFilterHandler().orElse(null);
    if (!Objects.equals(m_filterHandler, filterHandler)) {
        LOGGER.warn("Column has already a filter handler attached, ignoring new handler.");
    }
    // merge properties, take intersection
    DataColumnProperties prop2 = cspec2.getProperties();
    Map<String, String> mergedProps = new HashMap<String, String>();
    Enumeration<String> e = m_properties.properties();
    while (e.hasMoreElements()) {
        String key = e.nextElement();
        String value = m_properties.getProperty(key);
        if (prop2.getProperty(key) != null && prop2.getProperty(key).equals(value)) {
            mergedProps.put(key, value);
        }
    }
    if (mergedProps.size() != m_properties.size()) {
        setProperties(new DataColumnProperties(mergedProps));
    }
    List<String> elNames2 = cspec2.getElementNames();
    String[] elNames2Array = elNames2.toArray(new String[elNames2.size()]);
    String[] elNamesArray = m_elementNames == null ? new String[] { m_name } : m_elementNames;
    if (!Arrays.deepEquals(elNamesArray, elNames2Array)) {
        throw new IllegalArgumentException("Element names are not equal");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) ShapeHandler(org.knime.core.data.property.ShapeHandler) FilterHandler(org.knime.core.data.property.filter.FilterHandler) SizeHandler(org.knime.core.data.property.SizeHandler) ColorHandler(org.knime.core.data.property.ColorHandler)

Example 2 with FilterHandler

use of org.knime.core.data.property.filter.FilterHandler in project knime-core by knime.

the class DataColumnSpec method load.

/**
 * Reads name, type, domain, and properties from the given
 * <code>ConfigRO</code> and - if available - size, shape, and color
 * handler. Returns a new <code>DataColumnSpec</code> object initialized
 * with the information read.
 *
 * @param config to read properties from
 * @return a new column spec object
 * @throws InvalidSettingsException if one of the non-optional properties is
 *             not available or can't be initialized
 * @throws NullPointerException if the config object is <code>null</code>
 */
public static DataColumnSpec load(final ConfigRO config) throws InvalidSettingsException {
    String name = config.getString(CFG_COLUMN_NAME);
    String[] elNames = config.getStringArray(CFG_ELEMENT_NAMES, (String[]) null);
    if (elNames == null) {
        elNames = new String[] { name };
    }
    DataType type = DataType.load(config.getConfig(CFG_COLUMN_TYPE));
    DataColumnDomain domain = DataColumnDomain.load(config.getConfig(CFG_COLUMN_DOMAIN));
    DataColumnProperties properties = DataColumnProperties.load(config.getConfig(CFG_COLUMN_PROPS));
    ColorHandler color = null;
    if (config.containsKey(CFG_COLORS)) {
        color = ColorHandler.load(config.getConfig(CFG_COLORS));
    }
    SizeHandler size = null;
    if (config.containsKey(CFG_SIZES)) {
        size = SizeHandler.load(config.getConfig(CFG_SIZES));
    }
    ShapeHandler shape = null;
    if (config.containsKey(CFG_SHAPES)) {
        shape = ShapeHandler.load(config.getConfig(CFG_SHAPES));
    }
    FilterHandler filter = null;
    if (config.containsKey(CFG_FILTER)) {
        filter = FilterHandler.load(config.getConfig(CFG_FILTER));
    }
    return new DataColumnSpec(name, elNames, type, domain, properties, size, color, shape, filter);
}
Also used : SizeHandler(org.knime.core.data.property.SizeHandler) ShapeHandler(org.knime.core.data.property.ShapeHandler) ColorHandler(org.knime.core.data.property.ColorHandler) FilterHandler(org.knime.core.data.property.filter.FilterHandler)

Example 3 with FilterHandler

use of org.knime.core.data.property.filter.FilterHandler in project knime-core by knime.

the class FilterApplyRowSplitterNodeModel method execute.

/**
 * Helper method to compute output, used both in streaming and non-streaming context
 */
private void execute(final RowInput inData, final RowOutput output1, final RowOutput output2, final DataTableSpec filterSpec, final ExecutionContext exec, final long rowCount) throws Exception {
    DataRow row;
    DataTableSpec inSpec = inData.getDataTableSpec();
    FilterHandler[] filterHandlers = new FilterHandler[inData.getDataTableSpec().getNumColumns()];
    for (String colName : filterSpec.getColumnNames()) {
        int columnIndex = inSpec.findColumnIndex(colName);
        if (columnIndex < 0) {
            setWarningMessage("Filter for column \"" + colName + "\" could not be applied, because the column was not found in the input table.");
        } else {
            Optional<FilterHandler> filterHandler = filterSpec.getColumnSpec(colName).getFilterHandler();
            filterHandlers[columnIndex] = filterHandler.isPresent() ? filterHandler.get() : null;
        }
    }
    long currentRowIndex = 0;
    while ((row = inData.poll()) != null) {
        exec.checkCanceled();
        // set progress if not streaming
        if (rowCount >= 0) {
            exec.setProgress(currentRowIndex / (double) rowCount);
        }
        final long currentRowIndexFinal = currentRowIndex;
        exec.setMessage(() -> "Row " + currentRowIndexFinal + "/" + rowCount);
        // check if row is filtered or not
        boolean isInAllFilters = true;
        int j = 0;
        for (DataCell cell : row) {
            if (filterHandlers[j] != null && !filterHandlers[j].isInFilter(cell)) {
                isInAllFilters = false;
            }
            j++;
        }
        // push row to output if it should not be filtered
        if (isInAllFilters) {
            output1.push(row);
        } else {
            output2.push(row);
        }
        currentRowIndex += 1;
    }
    inData.close();
    output1.close();
    output2.close();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow) FilterHandler(org.knime.core.data.property.filter.FilterHandler)

Example 4 with FilterHandler

use of org.knime.core.data.property.filter.FilterHandler in project knime-core by knime.

the class FilterApplyNodeModel method execute.

/**
 * Helper method to compute output, used both in streaming and non-streaming context
 */
private void execute(final RowInput inData, final RowOutput output, final DataTableSpec filterSpec, final ExecutionContext exec, final long rowCount) throws Exception {
    DataRow row;
    DataTableSpec inSpec = inData.getDataTableSpec();
    FilterHandler[] filterHandlers = new FilterHandler[inData.getDataTableSpec().getNumColumns()];
    for (String colName : filterSpec.getColumnNames()) {
        int columnIndex = inSpec.findColumnIndex(colName);
        if (columnIndex < 0) {
            setWarningMessage("Filter for column \"" + colName + "\" could not be applied, because the column was not found in the input table.");
        } else {
            Optional<FilterHandler> filterHandler = filterSpec.getColumnSpec(colName).getFilterHandler();
            filterHandlers[columnIndex] = filterHandler.isPresent() ? filterHandler.get() : null;
        }
    }
    long currentRowIndex = 0;
    while ((row = inData.poll()) != null) {
        exec.checkCanceled();
        // set progress if not streaming
        if (rowCount >= 0) {
            exec.setProgress(currentRowIndex / (double) rowCount);
        }
        final long currentRowIndexFinal = currentRowIndex;
        exec.setMessage(() -> "Row " + currentRowIndexFinal + "/" + rowCount);
        // check if row is filtered or not
        boolean isInAllFilters = true;
        int j = 0;
        for (DataCell cell : row) {
            if (filterHandlers[j] != null && !filterHandlers[j].isInFilter(cell)) {
                isInAllFilters = false;
            }
            j++;
        }
        // push row to output if it should not be filtered
        if (isInAllFilters) {
            output.push(row);
        }
        currentRowIndex += 1;
    }
    inData.close();
    output.close();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow) FilterHandler(org.knime.core.data.property.filter.FilterHandler)

Example 5 with FilterHandler

use of org.knime.core.data.property.filter.FilterHandler in project knime-core by knime.

the class DataTableSpecExtractor method extract.

/**
 * Creates and returns a data table containing the meta information of the given spec. The meta information is
 * referred to as the table data specification and contains information such as column names, types, domain values
 * (list of possible values for categorical columns) and lower and upper bounds. It also contains the information
 * which of the columns have a view handler associated, as well the possible values, if specified. Each column in
 * the given table spec is represented as a row in the returned table.
 *
 * @param spec The spec to extract the meta information from.
 * @return The data table containing the meta information of the given spec.
 */
public DataTable extract(final DataTableSpec spec) {
    List<DataColumnSpec> colSpecs = new ArrayList<DataColumnSpec>();
    if (m_extractColumnNameAsColumn) {
        colSpecs.add(new DataColumnSpecCreator("Column Name", StringCell.TYPE).createSpec());
    }
    colSpecs.add(new DataColumnSpecCreator("Column Type", StringCell.TYPE).createSpec());
    colSpecs.add(new DataColumnSpecCreator("Column Index", IntCell.TYPE).createSpec());
    switch(m_propertyHandlerOutputFormat) {
        case Hide:
            break;
        case Boolean:
            colSpecs.add(new DataColumnSpecCreator("Color Handler", BooleanCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Size Handler", BooleanCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Shape Handler", BooleanCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Filter Handler", BooleanCell.TYPE).createSpec());
            break;
        default:
            colSpecs.add(new DataColumnSpecCreator("Color Handler", StringCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Size Handler", StringCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Shape Handler", StringCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Filter Handler", StringCell.TYPE).createSpec());
    }
    // likely number (important for sorting)
    DataType lowerBoundColType = null;
    DataType upperBoundColType = null;
    for (DataColumnSpec c : spec) {
        DataColumnDomain domain = c.getDomain();
        if (domain.hasLowerBound()) {
            DataType curLowerType = domain.getLowerBound().getType();
            if (lowerBoundColType == null) {
                lowerBoundColType = curLowerType;
            } else {
                lowerBoundColType = DataType.getCommonSuperType(lowerBoundColType, curLowerType);
            }
        }
        if (domain.hasUpperBound()) {
            DataType curUpperType = domain.getUpperBound().getType();
            if (upperBoundColType == null) {
                upperBoundColType = curUpperType;
            } else {
                upperBoundColType = DataType.getCommonSuperType(upperBoundColType, curUpperType);
            }
        }
    }
    lowerBoundColType = lowerBoundColType == null ? GENERIC_DATA_TYPE : lowerBoundColType;
    upperBoundColType = upperBoundColType == null ? GENERIC_DATA_TYPE : upperBoundColType;
    colSpecs.add(new DataColumnSpecCreator("Lower Bound", lowerBoundColType).createSpec());
    colSpecs.add(new DataColumnSpecCreator("Upper Bound", upperBoundColType).createSpec());
    int maxPossValues = 0;
    switch(m_possibleValueOutputFormat) {
        case Hide:
            break;
        case Collection:
            colSpecs.add(new DataColumnSpecCreator("Possible Values", SetCell.getCollectionType(GENERIC_DATA_TYPE)).createSpec());
            break;
        default:
            for (DataColumnSpec c : spec) {
                DataColumnDomain domain = c.getDomain();
                if (domain.hasValues()) {
                    maxPossValues = Math.max(domain.getValues().size(), maxPossValues);
                }
            }
            for (int i = 0; i < maxPossValues; i++) {
                colSpecs.add(new DataColumnSpecCreator("Value " + i, GENERIC_DATA_TYPE).createSpec());
            }
    }
    /* fill it */
    DataContainer dc = new DataContainer(new DataTableSpec(colSpecs.toArray(new DataColumnSpec[colSpecs.size()])));
    for (int i = 0; i < spec.getNumColumns(); i++) {
        DataColumnSpec colSpec = spec.getColumnSpec(i);
        List<DataCell> cells = new ArrayList<DataCell>();
        if (m_extractColumnNameAsColumn) {
            cells.add(new StringCell(colSpec.getName()));
        }
        cells.add(new StringCell(colSpec.getType().toString()));
        cells.add(new IntCell(i));
        ColorHandler colorHandler = colSpec.getColorHandler();
        SizeHandler sizeHandler = colSpec.getSizeHandler();
        ShapeHandler shapeHandler = colSpec.getShapeHandler();
        Optional<FilterHandler> filterHandler = colSpec.getFilterHandler();
        switch(m_propertyHandlerOutputFormat) {
            case Hide:
                break;
            case Boolean:
                cells.add(BooleanCellFactory.create(colorHandler != null));
                cells.add(BooleanCellFactory.create(sizeHandler != null));
                cells.add(BooleanCellFactory.create(shapeHandler != null));
                cells.add(BooleanCellFactory.create(filterHandler.isPresent()));
                break;
            default:
                cells.add(new StringCell(colorHandler == null ? "" : colorHandler.toString()));
                cells.add(new StringCell(sizeHandler == null ? "" : sizeHandler.toString()));
                cells.add(new StringCell(shapeHandler == null ? "" : shapeHandler.toString()));
                cells.add(new StringCell(filterHandler.map(f -> f.toString()).orElse("")));
        }
        DataColumnDomain domain = colSpec.getDomain();
        DataCell lb = domain.getLowerBound();
        if (lb != null) {
            cells.add(lb);
        } else {
            cells.add(DataType.getMissingCell());
        }
        DataCell ub = domain.getUpperBound();
        if (ub != null) {
            cells.add(ub);
        } else {
            cells.add(DataType.getMissingCell());
        }
        switch(m_possibleValueOutputFormat) {
            case Hide:
                break;
            case Collection:
                if (domain.hasValues()) {
                    cells.add(CollectionCellFactory.createSetCell(domain.getValues()));
                } else {
                    cells.add(DataType.getMissingCell());
                }
                break;
            default:
                Set<DataCell> set = domain.hasValues() ? domain.getValues() : Collections.EMPTY_SET;
                int nrColsToWrite = maxPossValues;
                for (DataCell c : set) {
                    cells.add(c);
                    nrColsToWrite -= 1;
                }
                while (nrColsToWrite > 0) {
                    cells.add(DataType.getMissingCell());
                    nrColsToWrite -= 1;
                }
        }
        dc.addRowToTable(new DefaultRow(new RowKey(colSpec.getName()), cells));
    }
    dc.close();
    return dc.getTable();
}
Also used : FilterHandler(org.knime.core.data.property.filter.FilterHandler) RowKey(org.knime.core.data.RowKey) DataTableSpec(org.knime.core.data.DataTableSpec) ArrayList(java.util.ArrayList) DataContainer(org.knime.core.data.container.DataContainer) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataCell(org.knime.core.data.DataCell) ColorHandler(org.knime.core.data.property.ColorHandler) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell) ShapeHandler(org.knime.core.data.property.ShapeHandler) SetCell(org.knime.core.data.collection.SetCell) Set(java.util.Set) DataTable(org.knime.core.data.DataTable) SizeHandler(org.knime.core.data.property.SizeHandler) CollectionCellFactory(org.knime.core.data.collection.CollectionCellFactory) DataColumnDomain(org.knime.core.data.DataColumnDomain) List(java.util.List) BooleanCell(org.knime.core.data.def.BooleanCell) BooleanCellFactory(org.knime.core.data.def.BooleanCell.BooleanCellFactory) Optional(java.util.Optional) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) Collections(java.util.Collections) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) RowKey(org.knime.core.data.RowKey) ArrayList(java.util.ArrayList) ShapeHandler(org.knime.core.data.property.ShapeHandler) FilterHandler(org.knime.core.data.property.filter.FilterHandler) IntCell(org.knime.core.data.def.IntCell) DataContainer(org.knime.core.data.container.DataContainer) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) StringCell(org.knime.core.data.def.StringCell) SizeHandler(org.knime.core.data.property.SizeHandler) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) ColorHandler(org.knime.core.data.property.ColorHandler)

Aggregations

FilterHandler (org.knime.core.data.property.filter.FilterHandler)5 DataCell (org.knime.core.data.DataCell)3 DataTableSpec (org.knime.core.data.DataTableSpec)3 ColorHandler (org.knime.core.data.property.ColorHandler)3 ShapeHandler (org.knime.core.data.property.ShapeHandler)3 SizeHandler (org.knime.core.data.property.SizeHandler)3 DataRow (org.knime.core.data.DataRow)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 DataColumnDomain (org.knime.core.data.DataColumnDomain)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)1 DataTable (org.knime.core.data.DataTable)1 DataType (org.knime.core.data.DataType)1 RowKey (org.knime.core.data.RowKey)1