Search in sources :

Example 1 with PortObjectHolder

use of org.knime.core.node.port.PortObjectHolder in project knime-core by knime.

the class Node method assignInternalHeldObjects.

// execute
/**
 * Called after execute to retrieve internal held objects from underlying NodeModel and to do some clean-up with
 * previous objects. Only relevant for {@link BufferedDataTableHolder} and {@link PortObjectHolder}.
 * @param rawInData Raw in data, potentially empty array for streaming executor
 * @param exEnv exec environment or null
 * @param exec Context - for wrapper table creation
 * @param newOutData the output tables - used for lookup
 * @noreference This method is not intended to be referenced by clients.
 * @since 3.1
 */
public void assignInternalHeldObjects(final PortObject[] rawInData, final ExecutionEnvironment exEnv, final ExecutionContext exec, final PortObject[] newOutData) {
    // there might be previous internal tables in a loop  (tables kept between loop iterations: eg group loop start)
    // or for interactive nodes that get re-executed (javascript table view)
    // they can be discarded if no longer needed; they are not part of the internal tables after this execution
    PortObject[] previousInternalHeldTables = m_internalHeldPortObjects;
    if (previousInternalHeldTables != null && !(this.isModelCompatibleTo(LoopStartNode.class) || this.isModelCompatibleTo(LoopEndNode.class)) && !((exEnv != null) && (exEnv.reExecute()))) {
        LOGGER.coding("Found internal tables for non loop node: " + getName());
    }
    if (m_model instanceof BufferedDataTableHolder || m_model instanceof PortObjectHolder) {
        // copy the table array to prevent later modification by the user
        PortObject[] internalObjects;
        if (m_model instanceof PortObjectHolder) {
            internalObjects = ((PortObjectHolder) m_model).getInternalPortObjects();
        } else {
            internalObjects = ((BufferedDataTableHolder) m_model).getInternalTables();
        }
        if (internalObjects != null) {
            m_internalHeldPortObjects = new PortObject[internalObjects.length];
            for (int i = 0; i < internalObjects.length; i++) {
                PortObject t = internalObjects[i];
                if (t instanceof BufferedDataTable) {
                    // if table is one of the input tables, wrap it in WrappedTable (otherwise table get's copied)
                    for (int in = 0; in < rawInData.length; in++) {
                        if (t == rawInData[in]) {
                            t = exec.createWrappedTable((BufferedDataTable) t);
                            break;
                        }
                    }
                }
                m_internalHeldPortObjects[i] = t;
            }
        } else {
            m_internalHeldPortObjects = null;
        }
    }
    // see comment at variable declaration on what is done here
    if (previousInternalHeldTables != null) {
        Set<BufferedDataTable> disposableTables = collectTableAndReferences(previousInternalHeldTables);
        disposableTables.removeAll(collectTableAndReferences(m_internalHeldPortObjects));
        disposableTables.removeAll(collectTableAndReferences(newOutData));
        for (BufferedDataTable t : disposableTables) {
            t.clearSingle(this);
        }
    }
}
Also used : PortObjectHolder(org.knime.core.node.port.PortObjectHolder) LoopStartNode(org.knime.core.node.workflow.LoopStartNode) PortObject(org.knime.core.node.port.PortObject) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) LoopEndNode(org.knime.core.node.workflow.LoopEndNode)

Example 2 with PortObjectHolder

use of org.knime.core.node.port.PortObjectHolder in project knime-core by knime.

the class Node method loadDataAndInternals.

/**
 * Loads data from an argument persistor.
 * @param loader To load from.
 * @param exec For progress.
 * @param loadResult to add errors and warnings to (if any)
 * @noreference This method is not intended to be referenced by clients.
 */
public void loadDataAndInternals(final NodeContentPersistor loader, final ExecutionMonitor exec, final LoadResult loadResult) {
    LOGGER.assertLog(NodeContext.getContext() != null, "No node context available, please check call hierarchy and fix it");
    boolean hasContent = loader.hasContent();
    m_model.setHasContent(hasContent);
    for (int i = 0; i < getNrOutPorts(); i++) {
        PortObjectSpec spec = loader.getPortObjectSpec(i);
        if (checkPortObjectSpecClass(spec, i)) {
            m_outputs[i].spec = spec;
        } else {
            Class<? extends PortObjectSpec> specClass = m_outputs[i].type.getPortObjectSpecClass();
            loadResult.addError("Loaded PortObjectSpec of class \"" + spec.getClass().getSimpleName() + ", expected " + specClass.getSimpleName());
            loader.setNeedsResetAfterLoad();
        }
        PortObject obj = loader.getPortObject(i);
        if (checkPortObjectClass(obj, i)) {
            m_outputs[i].object = obj;
            m_outputs[i].summary = loader.getPortObjectSummary(i);
        } else {
            Class<? extends PortObject> objClass = m_outputs[i].type.getPortObjectClass();
            loadResult.addError("Loaded PortObject of class \"" + obj.getClass().getSimpleName() + ", expected " + objClass.getSimpleName());
            loader.setNeedsResetAfterLoad();
        }
        if (m_outputs[i].object != null) {
            // overwrites the spec that is read few rows above
            spec = m_outputs[i].object.getSpec();
            m_outputs[i].spec = spec;
            m_outputs[i].hiliteHdl = (i == 0) ? null : m_model.getOutHiLiteHandler(i - 1);
        }
    }
    m_model.restoreWarningMessage(loader.getWarningMessage());
    ReferencedFile internDirRef = loader.getNodeInternDirectory();
    if (internDirRef != null) {
        internDirRef.lock();
        try {
            exec.setMessage("Loading internals");
            m_model.loadInternals(internDirRef.getFile(), exec);
        } catch (Throwable e) {
            String error;
            if (e instanceof IOException) {
                error = "Loading model internals failed: " + e.getMessage();
                if (loader.mustWarnOnDataLoadError()) {
                    LOGGER.debug(error, e);
                } else {
                    LOGGER.debug(error);
                }
            } else {
                error = "Caught \"" + e.getClass().getSimpleName() + "\", " + "Loading model internals failed: " + e.getMessage();
                LOGGER.coding(error, e);
            }
            loadResult.addError(error, true);
        } finally {
            internDirRef.unlock();
        }
    }
    if (m_model instanceof BufferedDataTableHolder || m_model instanceof PortObjectHolder) {
        m_internalHeldPortObjects = loader.getInternalHeldPortObjects();
        if (m_internalHeldPortObjects != null) {
            if (m_model instanceof PortObjectHolder) {
                PortObject[] copy = Arrays.copyOf(m_internalHeldPortObjects, m_internalHeldPortObjects.length);
                ((PortObjectHolder) m_model).setInternalPortObjects(copy);
            } else {
                assert m_model instanceof BufferedDataTableHolder;
                BufferedDataTable[] copy;
                try {
                    copy = NodeModel.toBDTArray(m_internalHeldPortObjects, "Internal held objects array index", m_model.getClass().getSimpleName() + " should implement " + PortObjectHolder.class.getSimpleName() + " and not " + BufferedDataTableHolder.class.getSimpleName());
                    ((BufferedDataTableHolder) m_model).setInternalTables(copy);
                } catch (IOException e) {
                    loadResult.addError(e.getMessage(), true);
                }
            }
        }
    }
}
Also used : IOException(java.io.IOException) ReferencedFile(org.knime.core.internal.ReferencedFile) InactiveBranchPortObjectSpec(org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) FlowVariablePortObjectSpec(org.knime.core.node.port.flowvariable.FlowVariablePortObjectSpec) PortObjectHolder(org.knime.core.node.port.PortObjectHolder) PortObject(org.knime.core.node.port.PortObject) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject)

Aggregations

FileStorePortObject (org.knime.core.data.filestore.FileStorePortObject)2 PortObject (org.knime.core.node.port.PortObject)2 PortObjectHolder (org.knime.core.node.port.PortObjectHolder)2 FlowVariablePortObject (org.knime.core.node.port.flowvariable.FlowVariablePortObject)2 InactiveBranchPortObject (org.knime.core.node.port.inactive.InactiveBranchPortObject)2 IOException (java.io.IOException)1 ReferencedFile (org.knime.core.internal.ReferencedFile)1 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)1 FlowVariablePortObjectSpec (org.knime.core.node.port.flowvariable.FlowVariablePortObjectSpec)1 InactiveBranchPortObjectSpec (org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec)1 LoopEndNode (org.knime.core.node.workflow.LoopEndNode)1 LoopStartNode (org.knime.core.node.workflow.LoopStartNode)1