Search in sources :

Example 1 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.

the class RuleEngineFilter2PortsNodeModel method parseRules.

/**
 * Parses all rules in the from the table (assuming {@link #rules()} is safe to call, like
 * {@link #createStreamableOperator(PartitionInfo, PortObjectSpec[])} or
 * {@link #execute(BufferedDataTable[], ExecutionContext)} was called before).
 *
 * @param spec the spec of the table on which the rules are applied.
 * @param nodeType The type of the node from this method is called.
 * @return a list of parsed rules
 * @throws ParseException if a rule cannot be parsed
 * @since 2.12
 */
@Override
protected List<Rule> parseRules(final DataTableSpec spec, final RuleNodeSettings nodeType) throws ParseException {
    ArrayList<Rule> rules = new ArrayList<Rule>();
    final Map<String, FlowVariable> availableFlowVariables = getAvailableFlowVariables();
    // SimpleRuleParser ruleParser = new SimpleRuleParser(spec, availableFlowVariables);
    RuleFactory factory = RuleFactory.getInstance(nodeType).cloned();
    factory.disableMissingComparisons();
    factory.disableNaNComparisons();
    int line = 0;
    for (String s : 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 : RuleFactory(org.knime.base.node.rules.engine.RuleFactory) ArrayList(java.util.ArrayList) Rule(org.knime.base.node.rules.engine.Rule) ParseException(java.text.ParseException) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 2 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.

the class KnimeCompletionProvider method setFlowVariables.

/**
 * Set flow variables that should be shown in the code completion box.
 *
 * @param variables flow variables
 */
public void setFlowVariables(final Iterable<FlowVariable> variables) {
    if (m_flowVariableCompletions.size() > 0) {
        for (Completion c : m_flowVariableCompletions) {
            removeCompletion(c);
        }
    }
    m_flowVariableCompletions.clear();
    for (FlowVariable var : variables) {
        String typeChar;
        switch(var.getType()) {
            case DOUBLE:
                typeChar = "D";
                break;
            case INTEGER:
                typeChar = "I";
                break;
            case STRING:
                typeChar = "S";
                break;
            default:
                return;
        }
        m_flowVariableCompletions.add(new BasicCompletion(this, "$${" + typeChar + var.getName() + /*.replace("\\", "\\\\").replace("}", "\\}")*/
        "}$$", var.getType().toString(), "The flow variable " + var.getName() + "."));
    }
    addCompletions(m_flowVariableCompletions);
}
Also used : Completion(org.fife.ui.autocomplete.Completion) BasicCompletion(org.fife.ui.autocomplete.BasicCompletion) BasicCompletion(org.fife.ui.autocomplete.BasicCompletion) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 3 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.

the class NodeOutputView method updateVariableTable.

/*
     *  Put info about workflow variables into table.
     */
private void updateVariableTable(final NodeContainer nc) {
    assert Display.getCurrent().getThread() == Thread.currentThread();
    // display swt table
    ((StackLayout) m_stackPanel.getLayout()).topControl = m_table;
    m_stackPanel.layout();
    // Initialize table
    m_table.removeAll();
    for (TableColumn tc : m_table.getColumns()) {
        tc.dispose();
    }
    String[] titles = { "Variable", "Value" };
    for (int i = 0; i < titles.length; i++) {
        TableColumn column = new TableColumn(m_table, SWT.NONE);
        column.setText(titles[i]);
    }
    // retrieve variables
    Collection<FlowVariable> fvs;
    if ((nc instanceof SingleNodeContainer) || nc.getNrOutPorts() > 0) {
        // for normal nodes port 0 is available (hidden variable OutPort!)
        FlowObjectStack fos = nc.getOutPort(0).getFlowObjectStack();
        if (fos != null) {
            fvs = fos.getAvailableFlowVariables(org.knime.core.node.workflow.FlowVariable.Type.values()).values();
        } else {
            fvs = null;
        }
        m_info.setText("Node Variables");
    } else {
        // no output port on metanode - display workflow variables
        fvs = ((WorkflowManager) nc).getWorkflowVariables();
        m_info.setText("Metanode Variables");
    }
    if (fvs != null) {
        // update content
        for (FlowVariable fv : fvs) {
            TableItem item = new TableItem(m_table, SWT.NONE);
            item.setText(0, fv.getName());
            item.setText(1, fv.getValueAsString());
        }
    }
    for (int i = 0; i < m_table.getColumnCount(); i++) {
        m_table.getColumn(i).pack();
    }
}
Also used : TableItem(org.eclipse.swt.widgets.TableItem) FlowObjectStack(org.knime.core.node.workflow.FlowObjectStack) TableColumn(org.eclipse.swt.widgets.TableColumn) Point(org.eclipse.swt.graphics.Point) FlowVariable(org.knime.core.node.workflow.FlowVariable) SingleNodeContainer(org.knime.core.node.workflow.SingleNodeContainer)

Example 4 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.

the class StringManipulationVariableNodeModel method calculate.

/**
 * @throws CompilationFailedException
 * @throws InstantiationException
 * @throws Exception
 */
private void calculate() throws InvalidSettingsException, CompilationFailedException, InstantiationException {
    if (m_settings == null || m_settings.getExpression() == null) {
        throw new InvalidSettingsException("No expression has been set.");
    }
    JavaScriptingSettings settings = m_settings.createJavaScriptingSettings();
    settings.setInputAndCompile(new DataTableSpec());
    // calculate the result
    ColumnCalculator cc = new ColumnCalculator(settings, this);
    DataCell calculate = null;
    try {
        calculate = cc.calculate(new DefaultRow(new RowKey(""), new DataCell[] {}));
    } catch (NoSuchElementException e) {
        throw new InvalidSettingsException(e.getMessage());
    }
    String newVariableName;
    Map<String, FlowVariable> inputFlowVariables = getAvailableInputFlowVariables();
    if (m_settings.isReplace()) {
        newVariableName = m_settings.getColName();
        CheckUtils.checkSettingNotNull(inputFlowVariables.get(newVariableName), "Can't replace input variable '%s' -- it does not exist in the input", newVariableName);
    } else {
        newVariableName = new UniqueNameGenerator(inputFlowVariables.keySet()).newName(m_settings.getColName());
    }
    // convert and push result as flow variable
    CheckUtils.checkSetting(!calculate.isMissing(), "Calculation returned missing value");
    Class<? extends DataCell> cellType = calculate.getClass();
    if (cellType.equals(IntCell.class)) {
        pushFlowVariableInt(newVariableName, ((IntCell) calculate).getIntValue());
    } else if (cellType.equals(DoubleCell.class)) {
        pushFlowVariableDouble(newVariableName, ((DoubleCell) calculate).getDoubleValue());
    } else if (cellType.equals(StringCell.class)) {
        pushFlowVariableString(newVariableName, ((StringCell) calculate).getStringValue());
    } else {
        throw new RuntimeException("Invalid variable class: " + cellType);
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) JavaScriptingSettings(org.knime.ext.sun.nodes.script.settings.JavaScriptingSettings) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ColumnCalculator(org.knime.ext.sun.nodes.script.calculator.ColumnCalculator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) NoSuchElementException(java.util.NoSuchElementException) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 5 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.

the class FlowVariableList method setFlowVariables.

/**
 * Set the flow variables to display.
 * @param flowVars the flow variables.
 */
public void setFlowVariables(final Collection<FlowVariable> flowVars) {
    Collection<FlowVariable> sortedFlowVars = new TreeSet<>(new Comparator<FlowVariable>() {

        @Override
        public int compare(final FlowVariable o1, final FlowVariable o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    sortedFlowVars.addAll(flowVars);
    DefaultListModel fvListModel = (DefaultListModel) getModel();
    fvListModel.removeAllElements();
    for (FlowVariable v : sortedFlowVars) {
        fvListModel.addElement(v);
    }
}
Also used : TreeSet(java.util.TreeSet) DefaultListModel(javax.swing.DefaultListModel) 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