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");
}
}
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);
}
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();
}
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();
}
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();
}
Aggregations