use of org.knime.core.data.DataCell 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.DataCell in project knime-core by knime.
the class ExpressionFactory method constant.
/**
* {@inheritDoc}
*/
@Override
public Expression constant(final String text, final DataType type) {
try {
final Class<? extends DataCell> cellClass = type.getCellClass();
if (cellClass == null) {
throw new IllegalStateException("Cannot construct cell with type: " + type);
}
final DataCell cell = cellClass.getConstructor(String.class).newInstance(text);
final ExpressionValue value = new ExpressionValue(cell, EMPTY_MAP);
return new ConstantExpression(value);
} catch (InstantiationException e) {
throw new IllegalStateException(e);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
} catch (IllegalArgumentException e) {
throw new IllegalStateException(e);
} catch (InvocationTargetException e) {
throw new IllegalStateException(e);
} catch (NoSuchMethodException e) {
throw new IllegalStateException(e);
} catch (SecurityException e) {
throw new IllegalStateException(e);
}
}
use of org.knime.core.data.DataCell 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.DataCell 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.DataCell in project knime-core by knime.
the class RuleEngineVariableNodeModel method performExecute.
/**
* Creates the flow variable according to the computed value.
*
* @param rules The rules to check for match.
* @throws InvalidSettingsException When there is an error in the settings.
*/
private void performExecute(final List<Rule> rules) throws InvalidSettingsException {
String newFlowVar = m_settings.getNewColName();
final DataType outType = computeOutputType(rules);
final VariableProvider provider = new VariableProvider() {
@Override
public Object readVariable(final String name, final Class<?> type) {
return RuleEngineVariableNodeModel.this.readVariable(name, type);
}
@Override
@Deprecated
public int getRowCount() {
throw new IllegalStateException("Row count is not available.");
}
@Override
@Deprecated
public int getRowIndex() {
throw new IllegalStateException("Row index is not available.");
}
@Override
public long getRowCountLong() {
throw new IllegalStateException("Row count is not available.");
}
@Override
public long getRowIndexLong() {
throw new IllegalStateException("Row index is not available.");
}
};
boolean wasMatch = false;
for (Rule r : rules) {
if (r.getCondition().matches(null, provider).getOutcome() == MatchState.matchedAndStop) {
Outcome outcome2 = r.getOutcome();
// r.getSideEffect().perform(row, this);
final DataCell cell = (DataCell) outcome2.getComputedResult(null, provider);
wasMatch = true;
if (outType.equals(StringCell.TYPE) && !cell.isMissing() && !cell.getType().equals(StringCell.TYPE)) {
pushFlowVariableString(newFlowVar, cell.toString());
break;
} else {
if (cell.isMissing()) {
throw new UnsupportedOperationException("Missing result, TODO");
}
if (outType.equals(IntCell.TYPE)) {
pushFlowVariableInt(newFlowVar, ((IntValue) cell).getIntValue());
break;
} else if (outType.equals(DoubleCell.TYPE)) {
pushFlowVariableDouble(newFlowVar, ((DoubleValue) cell).getDoubleValue());
break;
} else if (outType.equals(StringCell.TYPE)) {
pushFlowVariableString(newFlowVar, ((StringValue) cell).getStringValue());
break;
} else {
// TODO
throw new UnsupportedOperationException("Wrong type: " + cell.getClass());
}
}
}
}
if (!wasMatch) {
if (outType.equals(StringCell.TYPE)) {
pushFlowVariableString(newFlowVar, "");
} else if (outType.equals(IntCell.TYPE)) {
pushFlowVariableInt(newFlowVar, 0);
} else {
pushFlowVariableDouble(newFlowVar, 0.0);
}
}
}
Aggregations