Search in sources :

Example 21 with ContainerTable

use of org.knime.core.data.container.ContainerTable in project knime-core by knime.

the class WorkflowLoadHelper method createTemplateLoadPersistor.

/**
 * Create persistor for a workflow or template.
 * @noreference Clients should only be required to load projects using
 * {@link WorkflowManager#loadProject(File, ExecutionMonitor, WorkflowLoadHelper)}
 * @param directory The directory to load from
 * @param templateSourceURI URI of the link to the template (will load a template and when the template is
 * instantiated and put into the workflow a link is created)
 * @return The persistor
 * @throws IOException If an IO error occured
 * @throws UnsupportedWorkflowVersionException If the workflow is of an unsupported version
 */
public final TemplateNodeContainerPersistor createTemplateLoadPersistor(final File directory, final URI templateSourceURI) throws IOException, UnsupportedWorkflowVersionException {
    if (directory == null) {
        throw new NullPointerException("Arguments must not be null.");
    }
    String fileName = getDotKNIMEFileName();
    if (!directory.isDirectory() || !directory.canRead()) {
        throw new IOException("Can't read metanode/template directory " + directory);
    }
    // template.knime or workflow.knime
    ReferencedFile dotKNIMERef = new ReferencedFile(new ReferencedFile(directory), fileName);
    File dotKNIME = dotKNIMERef.getFile();
    if (!dotKNIME.isFile()) {
        throw new IOException("No \"" + fileName + "\" file in directory \"" + directory.getAbsolutePath() + "\"");
    }
    NodeSettingsRO settings = NodeSettings.loadFromXML(new BufferedInputStream(new FileInputStream(dotKNIME)));
    // CeBIT 2006 version did not contain a version string.
    String versionString;
    if (settings.containsKey(WorkflowLoadHelper.CFG_VERSION)) {
        try {
            versionString = settings.getString(WorkflowLoadHelper.CFG_VERSION);
        } catch (InvalidSettingsException e) {
            throw new IOException("Can't read version number from \"" + dotKNIME.getAbsolutePath() + "\"", e);
        }
    } else {
        versionString = "0.9.0";
    }
    // might also be FUTURE
    LoadVersion version = FileWorkflowPersistor.parseVersion(versionString);
    boolean isSetDirtyAfterLoad = false;
    StringBuilder versionDetails = new StringBuilder(versionString);
    String createdBy = settings.getString(WorkflowLoadHelper.CFG_CREATED_BY, null);
    Version createdByVersion = null;
    if (createdBy != null) {
        versionDetails.append(" (created by KNIME ").append(createdBy).append(")");
        try {
            createdByVersion = new Version(createdBy);
        } catch (IllegalArgumentException e) {
            // ideally this goes into the 'LoadResult' but it's not instantiated yet
            LOGGER.warn(String.format("Unable to parse version string \"%s\" (file \"%s\"): %s", createdBy, dotKNIME.getAbsolutePath(), e.getMessage()), e);
        }
    }
    // added 3.5.0
    boolean isNightly = settings.getBoolean(CFG_NIGHTLY, false);
    boolean isRunningNightly = KNIMEConstants.isNightlyBuild();
    boolean isFutureWorkflow = createdByVersion != null && !new Version(KNIMEConstants.VERSION).isSameOrNewer(createdByVersion);
    if (version == LoadVersion.FUTURE || isFutureWorkflow || (!isRunningNightly && isNightly)) {
        switch(getUnknownKNIMEVersionLoadPolicy(version, createdByVersion, isNightly)) {
            case Abort:
                StringBuilder e = new StringBuilder("Unable to load ");
                e.append(isTemplateFlow() ? "template, " : "workflow, ");
                if (version == LoadVersion.FUTURE || isFutureWorkflow) {
                    e.append("it was created with a future version of KNIME (").append(createdBy).append("). ");
                    e.append("You are running ").append(KNIMEConstants.VERSION).append(".");
                } else {
                    e.append("it was created with a nightly build of KNIME (version ").append(createdBy).append("). ");
                    e.append("You are running ").append(KNIMEConstants.VERSION).append(".");
                }
                throw new UnsupportedWorkflowVersionException(e.toString());
            default:
                isSetDirtyAfterLoad = true;
        }
    } else if (version.isOlderThan(LoadVersion.V200)) {
        LOGGER.warn("The current KNIME version (" + KNIMEConstants.VERSION + ") is different from the one that " + "created the workflow (" + version + ") you are trying to load. In some rare cases, it  " + "might not be possible to load all data or some nodes can't be configured. " + "Please re-configure and/or re-execute these nodes.");
    }
    final MetaNodeTemplateInformation templateInfo;
    if (isTemplateFlow() && templateSourceURI != null) {
        try {
            templateInfo = MetaNodeTemplateInformation.load(settings, version);
            CheckUtils.checkSetting(Role.Template.equals(templateInfo.getRole()), "Role is not '%s' but '%s'", Role.Template, templateInfo.getRole());
        } catch (InvalidSettingsException e) {
            throw new IOException(String.format("Attempting to load template from \"%s\" but can't locate template information: %s", dotKNIME.getAbsolutePath(), e.getMessage()), e);
        }
    } else if (isTemplateFlow()) {
        // LOGGER.coding("Supposed to instantiate a template but the link URI is not set");
        // metanode template from node repository
        templateInfo = null;
    } else {
        templateInfo = null;
    }
    final TemplateNodeContainerPersistor persistor;
    // TODO only create new hash map if workflow is a project?
    HashMap<Integer, ContainerTable> tableRep = new GlobalTableRepository();
    WorkflowFileStoreHandlerRepository fileStoreHandlerRepository = new WorkflowFileStoreHandlerRepository();
    // ordinary workflow is loaded
    if (templateInfo == null) {
        persistor = new FileWorkflowPersistor(tableRep, fileStoreHandlerRepository, dotKNIMERef, this, version, !isTemplateFlow());
    } else {
        // some template is loaded
        switch(templateInfo.getNodeContainerTemplateType()) {
            case MetaNode:
                final ReferencedFile workflowDotKNIME;
                if (version.isOlderThan(LoadVersion.V2100)) {
                    // before 2.10 everything was stored in template.knime
                    workflowDotKNIME = dotKNIMERef;
                } else {
                    workflowDotKNIME = new ReferencedFile(dotKNIMERef.getParent(), WorkflowPersistor.WORKFLOW_FILE);
                }
                persistor = new FileWorkflowPersistor(tableRep, fileStoreHandlerRepository, workflowDotKNIME, this, version, !isTemplateFlow());
                break;
            case SubNode:
                final ReferencedFile settingsDotXML = new ReferencedFile(dotKNIMERef.getParent(), SingleNodeContainerPersistor.SETTINGS_FILE_NAME);
                persistor = new FileSubNodeContainerPersistor(settingsDotXML, this, version, tableRep, fileStoreHandlerRepository, true);
                break;
            default:
                throw new IllegalStateException("Unsupported template type");
        }
    }
    if (templateInfo != null) {
        persistor.setOverwriteTemplateInformation(templateInfo.createLink(templateSourceURI));
        persistor.setNameOverwrite(directory.getName());
    }
    if (isSetDirtyAfterLoad) {
        persistor.setDirtyAfterLoad();
    }
    return persistor;
}
Also used : WorkflowFileStoreHandlerRepository(org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository) ReferencedFile(org.knime.core.internal.ReferencedFile) ContainerTable(org.knime.core.data.container.ContainerTable) BufferedInputStream(java.io.BufferedInputStream) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) Version(org.knime.core.util.Version) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 22 with ContainerTable

use of org.knime.core.data.container.ContainerTable in project knime-core by knime.

the class TableSorterWorker method doInBackground.

/**
 * {@inheritDoc}
 */
@Override
protected DataTable doInBackground() throws Exception {
    // passed to table sorter for progress
    long rowCount;
    if (m_inputTable instanceof BufferedDataTable) {
        rowCount = ((BufferedDataTable) m_inputTable).size();
    } else if (m_inputTable instanceof ContainerTable) {
        rowCount = ((ContainerTable) m_inputTable).size();
    } else {
        // unknown, no progress
        rowCount = -1;
    }
    publish(new NodeProgress(0.0, "Starting table sort..."));
    Collection<String> sortColNames = new ArrayList<String>(2);
    DataTableSpec spec = m_inputTable.getDataTableSpec();
    for (int i : m_sortOrder.getSortColumnIndices()) {
        String name;
        if (i < 0) {
            // row id
            name = DataTableSorter.ROWKEY_SORT_SPEC.getName();
        } else {
            name = spec.getColumnSpec(i).getName();
        }
        sortColNames.add(name);
    }
    long start = System.currentTimeMillis();
    LOGGER.debug("Starting interactive table sorting on column(s) " + sortColNames);
    boolean[] sortOrders = m_sortOrder.getSortColumnOrder();
    // it DOES NOT respect blobs -- they will be copied (expensive)
    DataTableSorter sorter = new DataTableSorter(m_inputTable, rowCount, sortColNames, sortOrders, false);
    NodeProgressListener progLis = new NodeProgressListener() {

        @Override
        public void progressChanged(final NodeProgressEvent pe) {
            publish(pe.getNodeProgress());
        }
    };
    m_nodeProgressMonitor = new DefaultNodeProgressMonitor();
    ExecutionMonitor exec = new ExecutionMonitor(m_nodeProgressMonitor);
    m_nodeProgressMonitor.addProgressListener(progLis);
    try {
        DataTable result = sorter.sort(exec);
        long elapsedMS = System.currentTimeMillis() - start;
        String time = StringFormat.formatElapsedTime(elapsedMS);
        LOGGER.debug("Interactive table sorting finished (" + time + ")");
        return result;
    } finally {
        m_nodeProgressMonitor.removeProgressListener(progLis);
    }
}
Also used : DataTable(org.knime.core.data.DataTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTableSpec(org.knime.core.data.DataTableSpec) NodeProgressListener(org.knime.core.node.workflow.NodeProgressListener) NodeProgress(org.knime.core.node.workflow.NodeProgress) ArrayList(java.util.ArrayList) ContainerTable(org.knime.core.data.container.ContainerTable) NodeProgressEvent(org.knime.core.node.workflow.NodeProgressEvent) DataTableSorter(org.knime.core.data.sort.DataTableSorter) DefaultNodeProgressMonitor(org.knime.core.node.DefaultNodeProgressMonitor) BufferedDataTable(org.knime.core.node.BufferedDataTable) ExecutionMonitor(org.knime.core.node.ExecutionMonitor)

Example 23 with ContainerTable

use of org.knime.core.data.container.ContainerTable in project knime-core by knime.

the class DataColumnPropertiesView method dispose.

/**
 * {@inheritDoc}
 * @since 3.6
 */
@Override
public void dispose() {
    TableContentModel contentModel = m_propsView.getContentModel();
    DataTable dataTable = contentModel.getDataTable();
    contentModel.setDataTable(null);
    if (dataTable instanceof ContainerTable) {
        ((ContainerTable) dataTable).clear();
    }
}
Also used : DataTable(org.knime.core.data.DataTable) TableContentModel(org.knime.core.node.tableview.TableContentModel) ContainerTable(org.knime.core.data.container.ContainerTable)

Example 24 with ContainerTable

use of org.knime.core.data.container.ContainerTable in project knime-core by knime.

the class LinRegLearnerNodeModel method loadInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void loadInternals(final File internDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    File inFile = new File(internDir, FILE_SAVE);
    ModelContentRO c = ModelContent.loadFromXML(new BufferedInputStream(new GZIPInputStream(new FileInputStream(inFile))));
    try {
        m_nrRows = c.getInt(CFG_NR_ROWS);
        m_nrRowsSkipped = c.getInt(CFG_NR_ROWS_SKIPPED);
        m_error = c.getDouble(CFG_ERROR);
        ModelContentRO specContent = c.getModelContent(CFG_SPEC);
        DataTableSpec outSpec = DataTableSpec.load(specContent);
        m_actualUsedColumns = specContent.getStringArray(CFG_USED_COLUMNS, (String[]) null);
        ModelContentRO parContent = c.getModelContent(CFG_PARAMS);
        m_params = LinearRegressionContent.instantiateAndLoad(parContent, outSpec);
    } catch (InvalidSettingsException ise) {
        IOException ioe = new IOException("Unable to restore state: " + ise.getMessage());
        ioe.initCause(ise);
        throw ioe;
    }
    File dataFile = new File(internDir, FILE_DATA);
    ContainerTable t = DataContainer.readFromZip(dataFile);
    int rowCount = t.getRowCount();
    m_rowContainer = new DefaultDataArray(t, 1, rowCount, exec);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) DataTableSpec(org.knime.core.data.DataTableSpec) ModelContentRO(org.knime.core.node.ModelContentRO) BufferedInputStream(java.io.BufferedInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) ContainerTable(org.knime.core.data.container.ContainerTable)

Example 25 with ContainerTable

use of org.knime.core.data.container.ContainerTable in project knime-core by knime.

the class PolyRegLearnerNodeModel method loadInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    File f = new File(nodeInternDir, "internals.xml");
    if (f.exists()) {
        NodeSettingsRO internals = NodeSettings.loadFromXML(new BufferedInputStream(new FileInputStream(f)));
        try {
            double[] betas = internals.getDoubleArray("betas");
            String[] columnNames = internals.getStringArray("columnNames");
            double squaredError = internals.getDouble("squaredError");
            double[] meanValues = internals.getDoubleArray("meanValues");
            m_viewData = new PolyRegViewData(meanValues, betas, squaredError, columnNames, m_settings.getDegree(), m_settings.getTargetColumn());
        } catch (InvalidSettingsException ex) {
            throw new IOException("Old or corrupt internals", ex);
        }
    } else {
        throw new FileNotFoundException("Internals do not exist");
    }
    f = new File(nodeInternDir, "data.zip");
    if (f.exists()) {
        ContainerTable t = DataContainer.readFromZip(f);
        int rowCount = t.getRowCount();
        m_rowContainer = new DefaultDataArray(t, 1, rowCount, exec);
    } else {
        throw new FileNotFoundException("Internals do not exist");
    }
}
Also used : DefaultDataArray(org.knime.base.node.util.DefaultDataArray) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ContainerTable(org.knime.core.data.container.ContainerTable) BufferedInputStream(java.io.BufferedInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) File(java.io.File)

Aggregations

ContainerTable (org.knime.core.data.container.ContainerTable)32 File (java.io.File)18 DefaultDataArray (org.knime.base.node.util.DefaultDataArray)13 FileInputStream (java.io.FileInputStream)12 IOException (java.io.IOException)11 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)9 BufferedInputStream (java.io.BufferedInputStream)7 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)7 DataTable (org.knime.core.data.DataTable)5 DataTableSpec (org.knime.core.data.DataTableSpec)5 Map (java.util.Map)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 ReferencedFile (org.knime.core.internal.ReferencedFile)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 Test (org.junit.Test)3 RowKey (org.knime.core.data.RowKey)3 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)3