use of org.knime.core.data.DataRow in project knime-core by knime.
the class ExpressionFactory method or.
/**
* {@inheritDoc}
*/
@Override
public Expression or(final List<Expression> boolExpressions) {
final boolean allIsConstant = checkBooleansAndConstant(boolExpressions);
return new Expression.Base(boolExpressions) {
/**
* {@inheritDoc}
*/
@Override
public DataType getOutputType() {
return BooleanCell.TYPE;
}
/**
* {@inheritDoc}
*/
@Override
public List<DataType> getInputArgs() {
return Collections.singletonList(BooleanCell.TYPE);
}
/**
* {@inheritDoc}
*/
@Override
public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
DataCell ret = BooleanCell.FALSE;
Map<String, Map<String, String>> matchedObjects = new HashMap<String, Map<String, String>>();
for (Expression boolExpression : boolExpressions) {
ExpressionValue v = boolExpression.evaluate(row, provider);
DataCell cell = v.getValue();
assert !cell.isMissing();
if (cell instanceof BooleanValue) {
BooleanValue bool = (BooleanValue) cell;
if (!bool.getBooleanValue()) {
matchedObjects = Util.mergeObjects(matchedObjects, v.getMatchedObjects());
} else {
return new ExpressionValue(BooleanCell.TRUE, matchedObjects);
}
} else if (cell.isMissing()) {
ret = DataType.getMissingCell();
} else {
throw new IllegalStateException("Not boolean: " + v.getValue());
}
}
return new ExpressionValue(ret, matchedObjects);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isConstant() {
return allIsConstant;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "or(" + boolExpressions + ")";
}
/**
* {@inheritDoc}
*/
@Override
public ASTType getTreeType() {
return ASTType.Or;
}
};
}
use of org.knime.core.data.DataRow in project knime-core by knime.
the class ExpressionFactory method xor.
/**
* {@inheritDoc}
*/
@Override
public Expression xor(final List<Expression> boolExpressions) {
if (boolExpressions.size() < 1) {
throw new IllegalStateException("xor requires at least one argument.");
}
final boolean allIsConstant = checkBooleansAndConstant(boolExpressions);
return new Expression.Base(boolExpressions) {
/**
* {@inheritDoc}
*/
@Override
public DataType getOutputType() {
return BooleanCell.TYPE;
}
/**
* {@inheritDoc}
*/
@Override
public List<DataType> getInputArgs() {
return Collections.singletonList(BooleanCell.TYPE);
}
/**
* {@inheritDoc}
*/
@Override
public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
ExpressionValue val = boolExpressions.get(0).evaluate(row, provider);
DataCell valCell = val.getValue();
assert !valCell.isMissing();
if (valCell.isMissing()) {
return new ExpressionValue(valCell, EMPTY_MAP);
} else if (!(valCell instanceof BooleanValue)) {
throw new IllegalStateException("Not a boolean value in row '" + row.getKey() + "': " + valCell.getType());
}
BooleanCell ret = (BooleanCell) BooleanCellFactory.create(((BooleanValue) valCell).getBooleanValue());
Map<String, Map<String, String>> matchedObjects = val.getMatchedObjects();
for (int i = 1; i < boolExpressions.size(); ++i) {
Expression boolExpression = boolExpressions.get(i);
ExpressionValue v = boolExpression.evaluate(row, provider);
DataCell cell = v.getValue();
if (cell.isMissing()) {
return new ExpressionValue(cell, EMPTY_MAP);
} else if (cell instanceof BooleanValue) {
BooleanValue bool = (BooleanValue) cell;
matchedObjects = Util.mergeObjects(matchedObjects, v.getMatchedObjects());
ret = (BooleanCell) BooleanCellFactory.create(ret.getBooleanValue() ^ bool.getBooleanValue());
} else {
throw new IllegalStateException("Not a boolean value: " + v.getValue());
}
}
return new ExpressionValue(ret, matchedObjects);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isConstant() {
return allIsConstant;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "xor(" + boolExpressions + ")";
}
/**
* {@inheritDoc}
*/
@Override
public ASTType getTreeType() {
return ASTType.Xor;
}
};
}
use of org.knime.core.data.DataRow in project knime-core by knime.
the class ExpressionFactory method missingBoolean.
/**
* {@inheritDoc}
*/
@Override
public Expression missingBoolean(final Expression reference) {
return new Expression.Base(reference) {
/**
* {@inheritDoc}
*/
@Override
public DataType getOutputType() {
return BooleanCell.TYPE;
}
/**
* {@inheritDoc}
*/
@Override
public List<DataType> getInputArgs() {
return Collections.singletonList(DataType.getMissingCell().getType());
}
/**
* {@inheritDoc}
*/
@Override
public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
ExpressionValue val = reference.evaluate(row, provider);
DataCell valCell = val.getValue();
return new ExpressionValue(BooleanCellFactory.create(valCell.isMissing()), EMPTY_MAP);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isConstant() {
return false;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "missing(" + reference + ")";
}
/**
* {@inheritDoc}
*/
@Override
public ASTType getTreeType() {
return ASTType.Missing;
}
};
}
use of org.knime.core.data.DataRow in project knime-core by knime.
the class ExpressionFactory method columnRefImpl.
/**
* @param spec The {@link DataTableSpec}.
* @param columnRef Name of the column.
* @param booleanArgumentOfMissing When this value is {@code true} and the value is a boolean missing value, the
* result is {@code false}, else missing.
* @return The {@link Expression} computing the value of the column.
* @see #columnRef(DataTableSpec, String)
* @see #columnRefForMissing(DataTableSpec, String)
*/
private Expression columnRefImpl(final DataTableSpec spec, final String columnRef, final boolean booleanArgumentOfMissing) {
if (!spec.containsName(columnRef)) {
throw new IllegalStateException("Not a column: " + columnRef);
}
final int position = spec.findColumnIndex(columnRef);
final DataType type = spec.getColumnSpec(position).getType();
final boolean isBoolean = type.isCompatible(BooleanValue.class);
assert (!booleanArgumentOfMissing || isBoolean) : type;
return new Expression.Base() {
/**
* {@inheritDoc}
*/
@Override
public List<DataType> getInputArgs() {
return Collections.emptyList();
}
/**
* {@inheritDoc}
*/
@Override
public DataType getOutputType() {
return spec.getColumnSpec(columnRef).getType();
}
/**
* {@inheritDoc}
*/
@Override
public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
if (booleanArgumentOfMissing) {
return new ExpressionValue(row.getCell(position), EMPTY_MAP);
}
final DataCell cell = row.getCell(position);
if (isBoolean && cell.isMissing()) {
return new ExpressionValue(BooleanCell.FALSE, EMPTY_MAP);
}
return new ExpressionValue(row.getCell(position), EMPTY_MAP);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isConstant() {
return false;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "$" + columnRef + "$";
}
/**
* {@inheritDoc}
*/
@Override
public ASTType getTreeType() {
return ASTType.ColRef;
}
};
}
use of org.knime.core.data.DataRow in project knime-core by knime.
the class RuleEngineNodeModel method createRearranger.
private ColumnRearranger createRearranger(final DataTableSpec inSpec, final List<Rule> rules, final long rowCount, final boolean updateColSpec) throws InvalidSettingsException {
if (m_settings.isAppendColumn() && m_settings.getNewColName().isEmpty()) {
throw new InvalidSettingsException("No name for prediction column provided");
}
ColumnRearranger crea = new ColumnRearranger(inSpec);
String newColName = m_settings.isAppendColumn() ? DataTableSpec.getUniqueColumnName(inSpec, m_settings.getNewColName()) : m_settings.getReplaceColumn();
final DataType outType = computeOutputType(rules, RuleNodeSettings.RuleEngine, m_settings.isDisallowLongOutputForCompatibility());
DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(newColName, outType);
if (updateColSpec) {
// only update in configure, execute will compute properly
updateColSpec(rules, outType, colSpecCreator, this);
}
DataColumnSpec cs = colSpecCreator.createSpec();
final boolean disallowLongOutputForCompatibility = m_settings.isDisallowLongOutputForCompatibility();
VariableProvider.SingleCellFactoryProto cellFactory = new VariableProvider.SingleCellFactoryProto(cs) {
private long m_rowIndex = -1L;
@Override
public DataCell getCell(final DataRow row) {
m_rowIndex++;
return getRulesOutcome(outType, row, rules, disallowLongOutputForCompatibility, this);
}
@Override
public Object readVariable(final String name, final Class<?> type) {
return RuleEngineNodeModel.this.readVariable(name, type);
}
@Deprecated
@Override
public int getRowIndex() {
return (int) m_rowIndex;
}
@Override
public long getRowIndexLong() {
return m_rowIndex;
}
@Deprecated
@Override
public int getRowCount() {
return (int) rowCount;
}
@Override
public long getRowCountLong() {
return rowCount;
}
};
if (m_settings.isAppendColumn()) {
crea.append(cellFactory);
} else {
crea.replace(cellFactory, m_settings.getReplaceColumn());
}
return crea;
}
Aggregations