Search in sources :

Example 76 with FlowVariable

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());
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DefaultListModel(javax.swing.DefaultListModel) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 77 with FlowVariable

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;
        }
    };
}
Also used : DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 78 with FlowVariable

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;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 79 with FlowVariable

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;
}
Also used : ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 80 with FlowVariable

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);
        }
    }
}
Also used : RuleFactory(org.knime.base.node.rules.engine.RuleFactory) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ParseException(java.text.ParseException) RuleEngineSettings(org.knime.base.node.rules.engine.RuleEngineSettings) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Aggregations

FlowVariable (org.knime.core.node.workflow.FlowVariable)93 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)24 DataColumnSpec (org.knime.core.data.DataColumnSpec)14 DataType (org.knime.core.data.DataType)13 DataTableSpec (org.knime.core.data.DataTableSpec)11 ArrayList (java.util.ArrayList)10 PortType (org.knime.core.node.port.PortType)8 DefaultListModel (javax.swing.DefaultListModel)7 Type (org.knime.core.node.workflow.FlowVariable.Type)7 IOException (java.io.IOException)6 Map (java.util.Map)6 PortObject (org.knime.core.node.port.PortObject)6 Optional (java.util.Optional)5 Collectors (java.util.stream.Collectors)5 OutVar (org.knime.base.node.jsnippet.util.field.OutVar)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)5 URL (java.net.URL)4 ParseException (java.text.ParseException)4 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4