use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.
the class JSnippetPanel method update.
/**
* Updates the contents of the panel with new values.
*
* @param expression the expression in the editor
* @param spec the data table spec of the input table; used for filling the
* list of available columns
* @param flowVariables a map with all available flow variables; used to
* fill the list of available flow variables
* @param expressions {@link Expression}s' constants to add to the columns list.
* @since 2.8
*/
public void update(final String expression, final DataTableSpec spec, final Map<String, FlowVariable> flowVariables, final String[] expressions) {
m_expEdit.setText(expression);
m_expEdit.requestFocus();
DefaultListModel listModel = (DefaultListModel) m_colList.getModel();
listModel.removeAllElements();
// we have Expression.VERSION_2X
for (String exp : expressions) {
listModel.addElement(exp);
}
for (int i = 0; i < spec.getNumColumns(); i++) {
DataColumnSpec colSpec = spec.getColumnSpec(i);
listModel.addElement(colSpec);
}
m_completionProvider.setColumns(spec);
DefaultListModel fvListModel = (DefaultListModel) m_flowVarsList.getModel();
fvListModel.removeAllElements();
for (FlowVariable v : flowVariables.values()) {
fvListModel.addElement(v);
}
m_completionProvider.setFlowVariables(flowVariables.values());
}
use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.
the class ExpressionFactory method flowVarRef.
/**
* {@inheritDoc}
*/
@Override
public Expression flowVarRef(final Map<String, FlowVariable> flowVariables, final String flowVarRef) {
if (!flowVariables.containsKey(flowVarRef) || flowVariables.get(flowVarRef) == null) {
throw new IllegalStateException("Not a valid flow variable: " + flowVarRef + " (" + flowVariables + ")");
}
final FlowVariable var = flowVariables.get(flowVarRef);
final boolean isConstant = var.getScope() != Scope.Local;
final ExpressionValue constant = isConstant ? Util.readFlowVarToExpressionValue(var) : null;
return new Expression.Base() {
/**
* {@inheritDoc}
*/
@Override
public List<DataType> getInputArgs() {
return Collections.emptyList();
}
/**
* {@inheritDoc}
*/
@Override
public DataType getOutputType() {
return Util.toDataType(var.getType());
}
/**
* {@inheritDoc}
*/
@Override
public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
if (constant != null) {
return constant;
}
Object variable = provider.readVariable(flowVarRef, Util.flowVarTypeToClass(var.getType()));
switch(var.getType()) {
case DOUBLE:
return new ExpressionValue(new DoubleCell(((Double) variable).doubleValue()), EMPTY_MAP);
case INTEGER:
return new ExpressionValue(new IntCell(((Integer) variable).intValue()), EMPTY_MAP);
case STRING:
return new ExpressionValue(new StringCell((String) variable), EMPTY_MAP);
default:
throw new IllegalStateException("Not supported flow variable type: " + var.getType());
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean isConstant() {
return isConstant;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "$$" + flowVarRef + "$$";
}
/**
* {@inheritDoc}
*/
@Override
public ASTType getTreeType() {
return ASTType.FlowVarRef;
}
};
}
use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.
the class RuleEngineVariableNodeModel method parseRules.
/**
* Parses all rules in the settings object.
*
* @return a list of parsed rules
* @throws ParseException if a rule cannot be parsed
* @since 2.8
*/
protected List<Rule> parseRules() throws ParseException {
ArrayList<Rule> rules = new ArrayList<Rule>();
final Map<String, FlowVariable> availableFlowVariables = getAvailableFlowVariables();
final RuleFactory factory = RuleFactory.getInstance(RuleNodeSettings.VariableRule).cloned();
factory.disableNaNComparisons();
final DataTableSpec spec = new DataTableSpec();
int line = 0;
for (String s : m_settings.rules()) {
++line;
try {
final Rule rule = factory.parse(s, spec, availableFlowVariables);
if (rule.getCondition().isEnabled()) {
rules.add(rule);
}
} catch (ParseException e) {
throw Util.addContext(e, s, line);
}
}
return rules;
}
use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.
the class RulePanel method computeRules.
/**
* Computes the rules.
*
* @param includingComments Also keeps the comments.
* @param continueOnError Do not stop processing on errors.
* @return The parsed {@link Rule}s.
* @throws ParseException If we stop processing on errors this is the cause.
*/
protected List<Rule> computeRules(final boolean includingComments, final boolean continueOnError) throws ParseException {
List<Rule> ret = new ArrayList<Rule>();
String text = m_mainPanel.getExpression();
if (text == null) {
return ret;
}
final Map<String, FlowVariable> availableFlowVariables = m_nodeType.allowFlowVariables() ? m_flowVariables : Collections.<String, FlowVariable>emptyMap();
int lineNo = 0;
final RuleFactory factory = RuleFactory.getInstance(m_nodeType);
for (String line : text.split("\n", -1)) {
lineNo++;
// Skip empty lines
if (line.trim().isEmpty()) {
if (continueOnError) {
ret.add(null);
}
continue;
}
try {
final Rule r = factory.parse(line, m_spec, availableFlowVariables);
if (r.getCondition().isEnabled() || includingComments) {
ret.add(r);
}
} catch (ParseException e) {
if (continueOnError) {
ret.add(null);
} else {
ParseException error = new ParseException("line " + lineNo + ", col " + e.getErrorOffset() + ": " + e.getMessage(), e.getErrorOffset());
error.setStackTrace(e.getStackTrace());
throw error;
}
}
}
return ret;
}
use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.
the class PMMLRuleEditorNodeModel method validateSettings.
/**
* {@inheritDoc}
*/
@Override
protected void validateSettings(final NodeSettingsRO settings) throws InvalidSettingsException {
RuleEngineSettings res = new RuleEngineSettings();
res.loadSettings(settings);
RuleFactory ruleFactory = RuleFactory.getInstance(RuleNodeSettings.PMMLRule).cloned();
ruleFactory.disableColumnChecks();
ruleFactory.disableFlowVariableChecks();
Map<String, FlowVariable> flowVars = getAvailableInputFlowVariables();
for (String rule : res.rules()) {
try {
ruleFactory.parse(rule, null, flowVars);
} catch (ParseException e) {
throw new InvalidSettingsException(e.getMessage(), e);
}
}
}
Aggregations