Search in sources :

Example 26 with FlowVariable

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

the class SandboxedNodeCreator method createSandbox.

/**
 * Creates that temporary mini workflow that is executed remotely on the cluster/stream executor.
 * The returned value should be {@link SandboxedNode#close()} when done (using try-with-resources). After this
 * method is called no other set-method should be called.
 *
 * @param exec for progress/cancelation
 * @return the index of the node that represents this node (the node to execute) in the temporary mini workflow
 * @throws InvalidSettingsException
 * @throws IOException
 * @throws CanceledExecutionException
 * @throws LockFailedException
 * @throws InterruptedException
 */
public SandboxedNode createSandbox(final ExecutionMonitor exec) throws InvalidSettingsException, IOException, CanceledExecutionException, LockFailedException, InterruptedException {
    exec.setMessage("Creating virtual workflow");
    final WorkflowManager parent = m_nc.getParent();
    // derive workflow context via NodeContext as the parent could only a be a metanode in a metanode...
    final WorkflowContext origContext = NodeContext.getContext().getWorkflowManager().getContext();
    WorkflowContext.Factory ctxFactory;
    // (specifically reading knime://knime.workflow files)
    if (!m_copyDataIntoNewContext) {
        ctxFactory = new WorkflowContext.Factory(origContext);
        if (m_localWorkflowDir != null) {
            ctxFactory.setOriginalLocation(origContext.getCurrentLocation()).setCurrentLocation(m_localWorkflowDir);
        }
    } else if (m_localWorkflowDir != null) {
        ctxFactory = new WorkflowContext.Factory(m_localWorkflowDir);
    } else {
        ctxFactory = new WorkflowContext.Factory(FileUtil.createTempDir("sandbox-" + m_nc.getNameWithID()));
    }
    // We have to use the same location for the temporary files
    ctxFactory.setTempLocation(origContext.getTempLocation());
    origContext.getMountpointURI().ifPresent(u -> ctxFactory.setMountpointURI(u));
    WorkflowCreationHelper creationHelper = new WorkflowCreationHelper();
    creationHelper.setWorkflowContext(ctxFactory.createContext());
    if (!m_copyDataIntoNewContext) {
        creationHelper.setDataHandlers(parent.getGlobalTableRepository(), parent.getFileStoreHandlerRepository());
    }
    WorkflowManager tempWFM = m_rootWFM.createAndAddProject("Sandbox Exec on " + m_nc.getNameWithID(), creationHelper);
    // Add the workflow variables
    List<FlowVariable> workflowVariables = parent.getProjectWFM().getWorkflowVariables();
    tempWFM.addWorkflowVariables(true, workflowVariables.toArray(new FlowVariable[workflowVariables.size()]));
    // update credentials store of the workflow
    CredentialsStore cs = tempWFM.getCredentialsStore();
    workflowVariables.stream().filter(f -> f.getType().equals(FlowVariable.Type.CREDENTIALS)).filter(f -> !cs.contains(f.getName())).forEach(cs::addFromFlowVariable);
    final int inCnt = m_inData.length;
    // port object IDs in static port object map, one entry for
    // each connected input (no value for unconnected optional inputs)
    List<Integer> portObjectRepositoryIDs = new ArrayList<Integer>(inCnt);
    try {
        NodeID[] ins = new NodeID[inCnt];
        for (int i = 0; i < inCnt; i++) {
            final PortObject in = m_inData[i];
            final NodeInPort inPort = m_nc.getInPort(i);
            final PortType portType = inPort.getPortType();
            if (in == null) {
                // unconnected optional input
                CheckUtils.checkState(portType.isOptional(), "No data at port %d, although port is mandatory (port type %s)", i, portType.getName());
                continue;
            }
            int portObjectRepositoryID = PortObjectRepository.add(in);
            portObjectRepositoryIDs.add(portObjectRepositoryID);
            boolean isTable = BufferedDataTable.TYPE.equals(portType);
            NodeID inID = tempWFM.createAndAddNode(isTable ? TABLE_READ_NODE_FACTORY : OBJECT_READ_NODE_FACTORY);
            NodeSettings s = new NodeSettings("temp_data_in");
            tempWFM.saveNodeSettings(inID, s);
            List<FlowVariable> flowVars = getFlowVariablesOnPort(i);
            PortObjectInNodeModel.setInputNodeSettings(s, portObjectRepositoryID, flowVars, m_copyDataIntoNewContext);
            // update credentials store of the workflow
            flowVars.stream().filter(f -> f.getType().equals(FlowVariable.Type.CREDENTIALS)).filter(f -> !cs.contains(f.getName())).forEach(cs::addFromFlowVariable);
            tempWFM.loadNodeSettings(inID, s);
            ins[i] = inID;
        }
        // execute inPort object nodes to store the input data in them
        if (ins.length > 0 && !tempWFM.executeAllAndWaitUntilDoneInterruptibly()) {
            String error = "Unable to execute virtual workflow, status sent to log facilities";
            LOGGER.debug(error + ":");
            LOGGER.debug(tempWFM.toString());
            throw new RuntimeException(error);
        }
        // add the target node to the workflow
        WorkflowCopyContent.Builder content = WorkflowCopyContent.builder();
        content.setNodeIDs(m_nc.getID());
        final NodeID targetNodeID = tempWFM.copyFromAndPasteHere(parent, content.build()).getNodeIDs()[0];
        NodeContainer targetNode = tempWFM.getNodeContainer(targetNodeID);
        // connect target node to inPort object nodes, skipping unconnected (optional) inputs
        IntStream.range(0, inCnt).filter(i -> ins[i] != null).forEach(i -> tempWFM.addConnection(ins[i], 1, targetNodeID, i));
        if (m_forwardConnectionProgressEvents) {
            setupConnectionProgressEventListeners(m_nc, targetNode);
        }
        // copy the existing tables into the (meta) node (e.g. an executed file reader that's necessary
        // for other nodes to execute)
        exec.setMessage("Copying tables into temp flow");
        NodeContainerExecutionResult origResult = m_nc.createExecutionResult(exec);
        ExecutionMonitor copyExec = exec.createSubProgress(0.0);
        copyExistingTablesIntoSandboxContainer(origResult, m_nc, targetNode, copyExec, m_copyDataIntoNewContext);
        CopyContentIntoTempFlowNodeExecutionJobManager copyDataIntoTmpFlow = new CopyContentIntoTempFlowNodeExecutionJobManager(origResult);
        NodeExecutionJobManager oldJobManager = targetNode.getJobManager();
        tempWFM.setJobManager(targetNodeID, copyDataIntoTmpFlow);
        tempWFM.executeAllAndWaitUntilDoneInterruptibly();
        tempWFM.setJobManager(targetNodeID, oldJobManager);
        // do not use the cluster executor on the cluster...
        tempWFM.setJobManager(targetNodeID, NodeExecutionJobManagerPool.getDefaultJobManagerFactory().getInstance());
        if (!m_copyDataIntoNewContext) {
            copyFileStoreHandlerReference(targetNode, parent, false);
        }
        // save workflow in the local job dir
        if (m_localWorkflowDir != null) {
            tempWFM.save(m_localWorkflowDir, exec, true);
            deepCopyFilesInWorkflowDir(m_nc, tempWFM);
        }
        return new SandboxedNode(tempWFM, targetNodeID);
    } finally {
        portObjectRepositoryIDs.stream().forEach(PortObjectRepository::remove);
    }
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ReferencedFile(org.knime.core.internal.ReferencedFile) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) WorkflowContext(org.knime.core.node.workflow.WorkflowContext) NativeNodeContainerExecutionResult(org.knime.core.node.workflow.execresult.NativeNodeContainerExecutionResult) NodeContainer(org.knime.core.node.workflow.NodeContainer) Map(java.util.Map) PortObjectInNodeModel(org.knime.core.node.exec.dataexchange.in.PortObjectInNodeModel) PortObjectInNodeFactory(org.knime.core.node.exec.dataexchange.in.PortObjectInNodeFactory) PortType(org.knime.core.node.port.PortType) LockFailedException(org.knime.core.util.LockFailedException) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) Collection(java.util.Collection) ConnectionID(org.knime.core.node.workflow.ConnectionID) WorkflowCreationHelper(org.knime.core.node.workflow.WorkflowCreationHelper) Collectors(java.util.stream.Collectors) ConnectionContainer(org.knime.core.node.workflow.ConnectionContainer) List(java.util.List) BufferedDataTable(org.knime.core.node.BufferedDataTable) Stream(java.util.stream.Stream) Optional(java.util.Optional) CredentialsStore(org.knime.core.node.workflow.CredentialsStore) CheckUtils(org.knime.core.node.util.CheckUtils) PortObject(org.knime.core.node.port.PortObject) NodeContainerExecutionResult(org.knime.core.node.workflow.execresult.NodeContainerExecutionResult) IntStream(java.util.stream.IntStream) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) NodeExecutionJobManagerPool(org.knime.core.node.util.NodeExecutionJobManagerPool) FlowVariable(org.knime.core.node.workflow.FlowVariable) NodeSettings(org.knime.core.node.NodeSettings) BDTInNodeFactory(org.knime.core.node.exec.dataexchange.in.BDTInNodeFactory) ArrayList(java.util.ArrayList) SubNodeContainer(org.knime.core.node.workflow.SubNodeContainer) ExecutionContext(org.knime.core.node.ExecutionContext) NodeExecutionJobManager(org.knime.core.node.workflow.NodeExecutionJobManager) NodeLogger(org.knime.core.node.NodeLogger) WorkflowExecutionResult(org.knime.core.node.workflow.execresult.WorkflowExecutionResult) NodeFactory(org.knime.core.node.NodeFactory) NodeInPort(org.knime.core.node.workflow.NodeInPort) WorkflowManager(org.knime.core.node.workflow.WorkflowManager) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) NodeContext(org.knime.core.node.workflow.NodeContext) WorkflowCopyContent(org.knime.core.node.workflow.WorkflowCopyContent) File(java.io.File) PortObjectRepository(org.knime.core.node.exec.dataexchange.PortObjectRepository) NodeExecutionResult(org.knime.core.node.workflow.execresult.NodeExecutionResult) NodeID(org.knime.core.node.workflow.NodeID) FileUtil(org.knime.core.util.FileUtil) IFileStoreHandler(org.knime.core.data.filestore.internal.IFileStoreHandler) SubnodeContainerExecutionResult(org.knime.core.node.workflow.execresult.SubnodeContainerExecutionResult) Collections(java.util.Collections) SingleNodeContainer(org.knime.core.node.workflow.SingleNodeContainer) WorkflowCopyContent(org.knime.core.node.workflow.WorkflowCopyContent) WorkflowManager(org.knime.core.node.workflow.WorkflowManager) ArrayList(java.util.ArrayList) PortObjectInNodeFactory(org.knime.core.node.exec.dataexchange.in.PortObjectInNodeFactory) BDTInNodeFactory(org.knime.core.node.exec.dataexchange.in.BDTInNodeFactory) NodeFactory(org.knime.core.node.NodeFactory) NodeContainer(org.knime.core.node.workflow.NodeContainer) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) SubNodeContainer(org.knime.core.node.workflow.SubNodeContainer) SingleNodeContainer(org.knime.core.node.workflow.SingleNodeContainer) WorkflowCreationHelper(org.knime.core.node.workflow.WorkflowCreationHelper) NodeInPort(org.knime.core.node.workflow.NodeInPort) CredentialsStore(org.knime.core.node.workflow.CredentialsStore) NodeID(org.knime.core.node.workflow.NodeID) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) PortObject(org.knime.core.node.port.PortObject) NativeNodeContainerExecutionResult(org.knime.core.node.workflow.execresult.NativeNodeContainerExecutionResult) NodeContainerExecutionResult(org.knime.core.node.workflow.execresult.NodeContainerExecutionResult) WorkflowContext(org.knime.core.node.workflow.WorkflowContext) NodeSettings(org.knime.core.node.NodeSettings) PortObjectRepository(org.knime.core.node.exec.dataexchange.PortObjectRepository) FlowVariable(org.knime.core.node.workflow.FlowVariable) PortType(org.knime.core.node.port.PortType) NodeExecutionJobManager(org.knime.core.node.workflow.NodeExecutionJobManager)

Example 27 with FlowVariable

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

the class ConfigEditTreeNodePanel method setTreeNode.

/**
 * Set a new tree node to display.
 * @param treeNode the new node to represent (may be null).
 */
public void setTreeNode(final ConfigEditTreeNode treeNode) {
    m_treeNode = treeNode;
    boolean isEditable = m_treeNode != null && m_treeNode.isLeaf();
    Type selType;
    String usedVariable;
    m_valueField.setEnabled(isEditable);
    if (m_treeNode != null) {
        AbstractConfigEntry entry = treeNode.getConfigEntry();
        switch(entry.getType()) {
            case xbyte:
            case xlong:
            case xshort:
            case xint:
                selType = Type.INTEGER;
                break;
            case xdouble:
            case xfloat:
                selType = Type.DOUBLE;
                break;
            default:
                selType = Type.STRING;
        }
        Icon icon;
        switch(entry.getType()) {
            case xstring:
                icon = ICON_STRING;
                break;
            case xdouble:
                icon = ICON_DOUBLE;
                break;
            case xint:
                icon = ICON_INT;
                break;
            default:
                icon = ICON_UNKNOWN;
        }
        m_keyIcon = icon;
        m_keyLabel.setText(entry.getKey());
        m_keyLabel.setToolTipText(entry.getKey());
        usedVariable = m_treeNode.getUseVariableName();
        String exposeVariable = m_treeNode.getExposeVariableName();
        m_exposeAsVariableField.setText(exposeVariable);
    } else {
        selType = Type.STRING;
        m_keyLabel.setText("");
        m_keyLabel.setToolTipText(null);
        m_keyIcon = ICON_UNKNOWN;
        m_exposeAsVariableField.setText("");
        usedVariable = null;
    }
    m_keyLabel.setMinimumSize(LABEL_DIMENSION);
    m_keyLabel.setMaximumSize(LABEL_DIMENSION);
    m_keyLabel.setPreferredSize(LABEL_DIMENSION);
    m_keyLabel.setSize(LABEL_DIMENSION);
    DefaultComboBoxModel model = (DefaultComboBoxModel) m_valueField.getModel();
    model.removeAllElements();
    model.addElement(" ");
    @SuppressWarnings("unchecked") Collection<FlowVariable> allVars = getFlowObjectStack() != null ? getFlowObjectStack().getAvailableFlowVariables().values() : (Collection<FlowVariable>) Collections.EMPTY_LIST;
    ComboBoxElement match = null;
    for (FlowVariable v : allVars) {
        boolean isOk = ConfigEditTreeModel.doesTypeAccept(selType, v.getType());
        if (isOk) {
            ComboBoxElement cbe = new ComboBoxElement(v);
            model.addElement(cbe);
            if (v.getName().equals(usedVariable)) {
                match = cbe;
            }
        } else if (v.getName().equals(usedVariable)) {
            String error = "Variable \"" + usedVariable + "\" has wrong type (" + v.getType() + "), expected " + selType;
            ComboBoxElement cbe = new ComboBoxElement(v, error);
            model.addElement(cbe);
            match = cbe;
        }
    }
    if (match != null) {
        m_valueField.setSelectedItem(match);
    } else if (usedVariable != null) {
        // show name in variable in arrows; makes also sure to
        // not violate the namespace of the variable (could be
        // node-local variable, which can't be created outside
        // the workflow package)
        String errorName = "<" + usedVariable + ">";
        String error = "Invalid variable \"" + usedVariable + "\"";
        FlowVariable virtualVar;
        switch(selType) {
            case DOUBLE:
                virtualVar = new FlowVariable(errorName, 0.0);
                break;
            case INTEGER:
                virtualVar = new FlowVariable(errorName, 0);
                break;
            default:
                virtualVar = new FlowVariable(errorName, "");
                break;
        }
        ComboBoxElement cbe = new ComboBoxElement(virtualVar, error);
        model.addElement(cbe);
        m_valueField.setSelectedItem(cbe);
    }
    m_valueField.setEnabled(model.getSize() > 1);
}
Also used : AbstractConfigEntry(org.knime.core.node.config.base.AbstractConfigEntry) Type(org.knime.core.node.workflow.FlowVariable.Type) Icon(javax.swing.Icon) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 28 with FlowVariable

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

the class DialogComponentFlowVariableNameSelection method updateComponent.

/**
 * {@inheritDoc}
 */
@Override
protected void updateComponent() {
    final String strVal = ((SettingsModelString) getModel()).getStringValue();
    FlowVariable val = null;
    if (strVal != null) {
        for (int i = 0, length = m_jcombobox.getItemCount(); i < length; i++) {
            final FlowVariable curVal = (FlowVariable) m_jcombobox.getItemAt(i);
            if (curVal.getName().equals(strVal)) {
                val = curVal;
                break;
            }
        }
        if (val == null) {
            val = new FlowVariable("NONE", "");
        }
    }
    boolean update;
    if (val == null) {
        update = m_jcombobox.getSelectedItem() != null;
    } else {
        update = !val.equals(m_jcombobox.getSelectedItem());
    }
    if (update) {
        m_jcombobox.setSelectedItem(val);
    }
    // also update the enable status
    setEnabledComponents(getModel().isEnabled());
    // make sure the model is in sync (in case model value isn't selected)
    final FlowVariable selItem = (FlowVariable) m_jcombobox.getSelectedItem();
    try {
        if ((selItem == null && strVal != null) || (selItem != null && !selItem.getName().equals(strVal))) {
            // if the (initial) value in the model is not in the list
            updateModel();
        }
    } catch (InvalidSettingsException e) {
    // ignore invalid values here
    }
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 29 with FlowVariable

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

the class DialogComponentFlowVariableNameSelection method replaceListItems.

/**
 * Replaces the list of selectable flow variables in the component. If
 * <code>select</code> is specified (not null) and it exists in the
 * collection it will be selected. If <code>select</code> is null, the
 * previous value will stay selected (if it exists in the new list).
 *
 * @param newItems new flow variables for the combo box
 * @param select the item to select after the replace. Can be null, in which
 *            case the previous selection remains - if it exists in the new
 *            list.
 * @throws IllegalArgumentException if set of flow variables is null or empty
 */
public void replaceListItems(final Collection<FlowVariable> newItems, final String select) {
    final String sel;
    if (select == null) {
        sel = ((SettingsModelString) getModel()).getStringValue();
    } else {
        sel = select;
    }
    m_jcombobox.removeAllItems();
    if (m_hasNone) {
        m_jcombobox.addItem(new FlowVariable("NONE", " "));
    }
    if (newItems == null || newItems.size() < 1) {
        if (!m_hasNone) {
            throw new IllegalArgumentException("The container with the new items can't be null or empty.");
        }
    } else {
        Vector<FlowVariable> filteredItems = getFilteredFlowVariables(newItems);
        FlowVariable selOption = null;
        for (final FlowVariable option : filteredItems) {
            if (option == null) {
                throw new NullPointerException("Options in the selection list can't be null");
            }
            m_jcombobox.addItem(option);
            if (option.getName().equals(sel)) {
                selOption = option;
            }
        }
        if (selOption == null) {
            m_jcombobox.setSelectedIndex(0);
        } else {
            m_jcombobox.setSelectedItem(selOption);
        }
    }
    // update the size of the comboBox and force the repainting
    // of the whole panel
    m_jcombobox.setSize(m_jcombobox.getPreferredSize());
    getComponentPanel().validate();
}
Also used : FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 30 with FlowVariable

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

the class SendMailNodeDialog method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final PortObjectSpec[] specs) {
    SendMailConfiguration config = new SendMailConfiguration();
    config.loadConfigurationInDialog(settings);
    setValueInStringHistoryPanel(m_smtpHostPanel, config.getSmtpHost());
    setValueInStringHistoryPanel(m_smtpPortPanel, Integer.toString(config.getSmtpPort()));
    if (m_useAuthenticationChecker.isSelected() != config.isUseAuthentication()) {
        m_useAuthenticationChecker.doClick();
    }
    if (m_useCredentialsChecker.isSelected() != config.isUseCredentials()) {
        m_useCredentialsChecker.doClick();
    }
    DefaultComboBoxModel model = (DefaultComboBoxModel) m_credentialsCombo.getModel();
    model.removeAllElements();
    for (String s : getCredentialsNames()) {
        model.addElement(s);
    }
    m_credentialsCombo.setSelectedItem(config.getCredentialsId());
    setValueInStringHistoryPanel(m_smtpUserPanel, config.getSmtpUser());
    m_smtpPasswordField.setText(config.getSmtpPassword());
    m_connectionSecurityCombo.setSelectedItem(config.getConnectionSecurity());
    m_connectionPriorityCombo.setSelectedItem(config.getPriority());
    setValueInStringHistoryPanel(m_fromPanel, config.getFrom());
    setValueInStringHistoryPanel(m_toPanel, config.getTo());
    setValueInStringHistoryPanel(m_ccPanel, config.getCc());
    setValueInStringHistoryPanel(m_bccPanel, config.getBcc());
    setValueInStringHistoryPanel(m_subjectPanel, config.getSubject());
    DefaultListModel listModel = (DefaultListModel) m_flowVarList.getModel();
    listModel.removeAllElements();
    for (FlowVariable e : getAvailableFlowVariables().values()) {
        listModel.addElement(e);
    }
    m_textArea.setText(config.getText());
    switch(config.getFormat()) {
        case Html:
            m_formatHTMLButton.doClick();
            break;
        case Text:
            m_formatTextButton.doClick();
            break;
        default:
            throw new RuntimeException("Unsupported format");
    }
    URL[] attachedURLs = config.getAttachedURLs();
    m_attachmentList.setSelectedURLs(Arrays.asList(attachedURLs));
}
Also used : DefaultListModel(javax.swing.DefaultListModel) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) URL(java.net.URL) 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