use of org.knime.core.data.DataType in project knime-core by knime.
the class BaseRuleParser method parseColumnExpression.
/**
* Creates an {@link Expression} to compute a column.
*
* @param state The {@link ParseState}.
* @param fromMissing This {@link Expression} is created within a {@link Operators#MISSING}.
* @return The {@link Expression} parsed representing a column.
* @throws ParseException Problem during parsing.
*/
private Expression parseColumnExpression(final ParseState state, final boolean fromMissing) throws ParseException {
state.skipWS();
int startPos = state.getPosition();
if (state.isColumnRef()) {
String columnRef = state.readColumnRef();
if (!m_checkColumns) {
// Create a dummy expression
return new Expression() {
@Override
public boolean isConstant() {
return false;
}
@Override
public ASTType getTreeType() {
return ASTType.ColRef;
}
@Override
public DataType getOutputType() {
return DataType.getMissingCell().getType();
}
@Override
public List<DataType> getInputArgs() {
return Collections.emptyList();
}
@Override
public List<Expression> getChildren() {
return Collections.emptyList();
}
@Override
public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
throw new IllegalStateException("This expression is only for configuration.");
}
};
}
if (m_spec == null) {
throw new ParseException("No columns present.", startPos);
}
try {
Expression expr = m_factoryRef.columnRef(m_spec, columnRef);
if (BooleanCell.TYPE.isASuperTypeOf(expr.getOutputType()) && fromMissing) {
expr = m_factoryRef.columnRefForMissing(m_spec, columnRef);
}
return expr;
} catch (IllegalStateException e) {
throw new ParseException(e.getMessage(), startPos);
}
}
throw new ParseException("Expected a column reference", state.getPosition());
}
use of org.knime.core.data.DataType in project knime-core by knime.
the class BaseRuleParser method parseFlowVariableExpression.
/**
* Creates an {@link Expression} to compute a {@link FlowVariable}.
*
* @param state The {@link ParseState}.
* @return The {@link Expression} parsed representing a {@link FlowVariable}.
* @throws ParseException Problem during parsing.
*/
private Expression parseFlowVariableExpression(final ParseState state) throws ParseException {
state.skipWS();
int startPos = state.getPosition();
if (state.isFlowVariableRef()) {
String flowVarRef = state.readFlowVariable();
if (!m_checkFlowVars) {
// Create a dummy expression
return new Expression() {
@Override
public boolean isConstant() {
return false;
}
@Override
public ASTType getTreeType() {
return ASTType.FlowVarRef;
}
@Override
public DataType getOutputType() {
return DataType.getMissingCell().getType();
}
@Override
public List<DataType> getInputArgs() {
return Collections.emptyList();
}
@Override
public List<Expression> getChildren() {
return Collections.emptyList();
}
@Override
public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
throw new IllegalStateException("This expression is only for configuration.");
}
};
}
try {
return m_factoryRef.flowVarRef(m_flowVariables, flowVarRef);
} catch (IllegalStateException e) {
throw new ParseException(e.getMessage(), startPos);
}
}
throw new ParseException("Expected a flow variable reference", state.getPosition());
}
use of org.knime.core.data.DataType 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.DataType 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.DataType 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;
}
};
}
Aggregations