Search in sources :

Example 36 with CanceledExecutionException

use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.

the class WorkflowManager method loadContent.

/**
 * {@inheritDoc}
 */
@Override
WorkflowCopyContent loadContent(final NodeContainerPersistor nodePersistor, final Map<Integer, BufferedDataTable> tblRep, final FlowObjectStack ignoredStack, final ExecutionMonitor exec, final LoadResult loadResult, final boolean preserveNodeMessage) throws CanceledExecutionException {
    exec.checkCanceled();
    if (!(nodePersistor instanceof WorkflowPersistor)) {
        throw new IllegalStateException("Expected " + WorkflowPersistor.class.getSimpleName() + " persistor object, got " + nodePersistor.getClass().getSimpleName());
    }
    WorkflowPersistor persistor = (WorkflowPersistor) nodePersistor;
    assert this != ROOT || persistor.getConnectionSet().isEmpty() : "ROOT workflow has no connections: " + persistor.getConnectionSet();
    LinkedHashMap<NodeID, NodeContainerPersistor> persistorMap = new LinkedHashMap<NodeID, NodeContainerPersistor>();
    Map<Integer, ? extends NodeContainerPersistor> nodeLoaderMap = persistor.getNodeLoaderMap();
    exec.setMessage("annotations");
    List<WorkflowAnnotation> annos = persistor.getWorkflowAnnotations();
    for (WorkflowAnnotation w : annos) {
        addWorkflowAnnotationInternal(w);
    }
    exec.setMessage("node & connection information");
    Map<Integer, NodeID> translationMap = loadNodesAndConnections(nodeLoaderMap, persistor.getConnectionSet(), loadResult);
    for (Map.Entry<Integer, NodeID> e : translationMap.entrySet()) {
        NodeID id = e.getValue();
        NodeContainerPersistor p = nodeLoaderMap.get(e.getKey());
        assert p != null : "Deficient translation map";
        persistorMap.put(id, p);
    }
    persistor.postLoad(this, loadResult);
    try {
        postLoad(persistorMap, tblRep, persistor.mustWarnOnDataLoadError(), exec, loadResult, preserveNodeMessage);
    } catch (CanceledExecutionException cee) {
        for (NodeID insertedNodeID : translationMap.values()) {
            removeNode(insertedNodeID);
        }
        throw cee;
    }
    NodeSettingsRO wizardState = persistor.getWizardExecutionControllerState();
    if (wizardState != null) {
        try {
            m_executionController = new WizardExecutionController(this, wizardState);
        } catch (InvalidSettingsException e1) {
            String msg = "Failed to restore wizard controller from file: " + e1.getMessage();
            LOGGER.debug(msg, e1);
            loadResult.addError(msg);
        }
    }
    // of the workflow can't be properly read from the workflow.knime)
    if (persistor.needsResetAfterLoad() || persistor.isDirtyAfterLoad()) {
        setDirty();
    }
    ReferencedFile ncDirectory = getNodeContainerDirectory();
    if (ncDirectory != null) {
        ncDirectory.getDeletedNodesFileLocations().addAll(persistor.getObsoleteNodeDirectories());
    }
    ReferencedFile autoSaveDirectory = getAutoSaveDirectory();
    if (autoSaveDirectory != null) {
        autoSaveDirectory.getDeletedNodesFileLocations().addAll(persistor.getObsoleteNodeDirectories());
    }
    Collection<NodeID> resultColl = persistorMap.keySet();
    NodeID[] newIDs = resultColl.toArray(new NodeID[resultColl.size()]);
    WorkflowAnnotation[] newAnnotations = annos.toArray(new WorkflowAnnotation[annos.size()]);
    addConnectionsFromTemplates(persistor.getAdditionalConnectionSet(), loadResult, translationMap, false);
    WorkflowCopyContent.Builder result = WorkflowCopyContent.builder();
    result.setAnnotation(newAnnotations);
    result.setNodeIDs(newIDs);
    return result.build();
}
Also used : ReferencedFile(org.knime.core.internal.ReferencedFile) LinkedHashMap(java.util.LinkedHashMap) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 37 with CanceledExecutionException

use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.

the class HiliteScorerNodeModel method execute.

/**
 * Starts the scoring in the scorer.
 *
 * @param data
 *            the input data of length one
 * @param exec
 *            the execution monitor
 * @return the confusion matrix
 * @throws CanceledExecutionException
 *             if user canceled execution
 *
 * @see NodeModel#execute(BufferedDataTable[],ExecutionContext)
 */
@SuppressWarnings("unchecked")
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] data, final ExecutionContext exec) throws CanceledExecutionException {
    // check input data
    assert (data != null && data.length == 1 && data[INPORT] != null);
    // blow away result from last execute (should have been reset anyway)
    // first try to figure out what are the different class values
    // in the two respective columns
    BufferedDataTable in = data[INPORT];
    DataTableSpec inSpec = in.getDataTableSpec();
    final int index1 = inSpec.findColumnIndex(m_firstCompareColumn);
    final int index2 = inSpec.findColumnIndex(m_secondCompareColumn);
    // two elements, first is column names, second row names;
    // these arrays are ordered already, i.e. if both columns have
    // cells in common (e.g. both have Iris-Setosa), they get the same
    // index in the array. thus, the high numbers should appear
    // in the diagonal
    DataCell[] values = determineColValues(in, index1, index2, exec.createSubProgress(0.5));
    List<DataCell> valuesList = Arrays.asList(values);
    Set<DataCell> valuesInCol2 = new HashSet<DataCell>();
    m_correctCount = 0;
    m_falseCount = 0;
    // the key store remembers the row key for later hiliting
    List[][] keyStore = new List[values.length][values.length];
    // the scorerCount counts the confusions
    m_scorerCount = new int[values.length][values.length];
    // init the matrix
    for (int i = 0; i < keyStore.length; i++) {
        for (int j = 0; j < keyStore[i].length; j++) {
            keyStore[i][j] = new ArrayList<RowKey>();
        }
    }
    int rowCnt = in.getRowCount();
    int rowNr = 0;
    ExecutionMonitor subExec = exec.createSubProgress(0.5);
    for (Iterator<DataRow> it = in.iterator(); it.hasNext(); rowNr++) {
        DataRow row = it.next();
        subExec.setProgress((1.0 + rowNr) / rowCnt, "Computing score, row " + rowNr + " (\"" + row.getKey() + "\") of " + in.getRowCount());
        try {
            subExec.checkCanceled();
        } catch (CanceledExecutionException cee) {
            reset();
            throw cee;
        }
        DataCell cell1 = row.getCell(index1);
        DataCell cell2 = row.getCell(index2);
        valuesInCol2.add(cell2);
        if (cell1.isMissing() || cell2.isMissing()) {
            continue;
        }
        boolean areEqual = cell1.equals(cell2);
        int i1 = valuesList.indexOf(cell1);
        int i2 = areEqual ? i1 : valuesList.indexOf(cell2);
        assert i1 >= 0 : "column spec lacks possible value " + cell1;
        assert i2 >= 0 : "column spec lacks possible value " + cell2;
        // i2 must be equal to i1 if cells are equal (implication)
        assert (!areEqual || i1 == valuesList.indexOf(cell2));
        keyStore[i1][i2].add(row.getKey());
        m_scorerCount[i1][i2]++;
        if (areEqual) {
            m_correctCount++;
        } else {
            m_falseCount++;
        }
    }
    m_nrRows = rowNr;
    HashSet<String> valuesAsStringSet = new HashSet<String>();
    HashSet<String> duplicateValuesAsString = new HashSet<String>();
    for (DataCell c : values) {
        valuesAsStringSet.add(c.toString());
    }
    for (DataCell c : values) {
        String cAsString = c.toString();
        if (!valuesAsStringSet.remove(cAsString)) {
            duplicateValuesAsString.add(cAsString);
        }
    }
    boolean hasPrintedWarningOnAmbiguousValues = false;
    m_values = new String[values.length];
    for (int i = 0; i < m_values.length; i++) {
        DataCell c = values[i];
        String s = c.toString();
        if (duplicateValuesAsString.contains(s)) {
            boolean isInSecondColumn = valuesInCol2.contains(c);
            int uniquifier = 1;
            if (isInSecondColumn) {
                s = s.concat(" (" + m_secondCompareColumn + ")");
            } else {
                s = s.concat(" (" + m_firstCompareColumn + ")");
            }
            String newName = s;
            while (!valuesAsStringSet.add(newName)) {
                newName = s + "#" + (uniquifier++);
            }
            m_values[i] = newName;
            if (!hasPrintedWarningOnAmbiguousValues) {
                hasPrintedWarningOnAmbiguousValues = true;
                setWarningMessage("Ambiguous value \"" + c.toString() + "\" encountered. Preserving individual instances;" + " consider to convert input columns to string");
            }
        } else {
            int uniquifier = 1;
            String newName = s;
            while (!valuesAsStringSet.add(newName)) {
                newName = s + "#" + (uniquifier++);
            }
            m_values[i] = newName;
        }
    }
    DataType[] colTypes = new DataType[m_values.length];
    Arrays.fill(colTypes, IntCell.TYPE);
    BufferedDataContainer container = exec.createDataContainer(new DataTableSpec(m_values, colTypes));
    for (int i = 0; i < m_values.length; i++) {
        // need to make a datacell for the row key
        container.addRowToTable(new DefaultRow(m_values[i], m_scorerCount[i]));
    }
    container.close();
    // print info
    int correct = getCorrectCount();
    int incorrect = getFalseCount();
    double error = getError();
    int nrRows = getNrRows();
    int missing = nrRows - correct - incorrect;
    m_keyStore = keyStore;
    LOGGER.info("error=" + error + ", #correct=" + correct + ", #false=" + incorrect + ", #rows=" + nrRows + ", #missing=" + missing);
    // our view displays the table - we must keep a reference in the model.
    BufferedDataTable result = container.getTable();
    return new BufferedDataTable[] { result };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowKey(org.knime.core.data.RowKey) DataRow(org.knime.core.data.DataRow) Point(java.awt.Point) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DefaultRow(org.knime.core.data.def.DefaultRow) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 38 with CanceledExecutionException

use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.

the class MissingValueHandlingTableIterator method push.

/**
 * pushes the internal iterator forward to the next row to return.
 */
private void push() {
    DataRow row;
    boolean hasMissing;
    boolean skipRow;
    // containing missing values, you get a StackOverFlow, bug fix #350
    do {
        if (!m_internIt.hasNext()) {
            m_next = null;
            return;
        }
        row = m_internIt.next();
        if (m_finalCount > 0) {
            m_exec.setProgress(m_count / (double) m_finalCount);
        }
        try {
            m_exec.checkCanceled();
        } catch (CanceledExecutionException cee) {
            throw new RuntimeCanceledExecutionException(cee);
        }
        m_count++;
        // check once if we can get away easy
        hasMissing = false;
        skipRow = false;
        for (int i = 0; !skipRow && i < row.getNumCells(); i++) {
            if (row.getCell(i).isMissing()) {
                switch(m_table.getColSetting(i).getMethod()) {
                    case ColSetting.METHOD_NO_HANDLING:
                        break;
                    case ColSetting.METHOD_IGNORE_ROWS:
                        skipRow = true;
                        break;
                    default:
                        hasMissing = true;
                }
            }
        }
    } while (skipRow);
    if (hasMissing) {
        m_next = handleMissing(row);
    } else {
        m_next = row;
    }
}
Also used : CanceledExecutionException(org.knime.core.node.CanceledExecutionException) DataRow(org.knime.core.data.DataRow)

Example 39 with CanceledExecutionException

use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.

the class FileUtil method addZipEntry.

private static boolean addZipEntry(final byte[] buf, final ZipWrapper zout, final File f, final String entryName, final ExecutionMonitor exec, final long origSize) throws IOException, CanceledExecutionException {
    InputStream in = new FileInputStream(f);
    try {
        zout.putNextEntry(new ZipEntry(entryName));
        int read;
        while ((read = in.read(buf)) >= 0) {
            exec.checkCanceled();
            zout.write(buf, 0, read);
            exec.setProgress(zout.getBytesWritten() / (double) origSize);
        }
        exec.setProgress(zout.getBytesWritten() / (double) origSize, "Added file " + entryName);
    } catch (CanceledExecutionException cee) {
        throw cee;
    } catch (IOException ioe) {
        throw ioe;
    } catch (Throwable t) {
        LOGGER.debug("Error while adding file to zip archive (" + f.getAbsolutePath() + ")", t);
        return false;
    } finally {
        zout.closeEntry();
        in.close();
    }
    return true;
}
Also used : CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 40 with CanceledExecutionException

use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.

the class DecisionTreeLearnerNodeModel method loadInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    File internalsFile = new File(nodeInternDir, SAVE_INTERNALS_FILE_NAME);
    if (!internalsFile.exists()) {
        // file to load internals from not available
        return;
    }
    BufferedInputStream in = new BufferedInputStream(new GZIPInputStream(new FileInputStream(internalsFile)));
    ModelContentRO decisionTreeModel = ModelContent.loadFromXML(in);
    try {
        m_decisionTree = new DecisionTree(decisionTreeModel);
    } catch (Exception e) {
        // continue, but inform the user via a message
        setWarningMessage("Internal model could not be loaded: " + e.getMessage() + ". The view will not display properly.");
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) ModelContentRO(org.knime.core.node.ModelContentRO) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Aggregations

CanceledExecutionException (org.knime.core.node.CanceledExecutionException)82 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)34 IOException (java.io.IOException)32 File (java.io.File)21 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)21 DataRow (org.knime.core.data.DataRow)20 DataTableSpec (org.knime.core.data.DataTableSpec)20 BufferedDataTable (org.knime.core.node.BufferedDataTable)20 DataCell (org.knime.core.data.DataCell)19 ArrayList (java.util.ArrayList)11 DataColumnSpec (org.knime.core.data.DataColumnSpec)11 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)10 LinkedHashMap (java.util.LinkedHashMap)9 ExecutionException (java.util.concurrent.ExecutionException)9 DefaultRow (org.knime.core.data.def.DefaultRow)9 RowKey (org.knime.core.data.RowKey)8 BufferedWriter (java.io.BufferedWriter)7 FileInputStream (java.io.FileInputStream)7 Map (java.util.Map)7 Future (java.util.concurrent.Future)7