Search in sources :

Example 21 with CanceledExecutionException

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

the class PieNodeModel method loadInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws CanceledExecutionException {
    try {
        final File dataDir = new File(nodeInternDir, CFG_DATA_DIR_NAME);
        loadPieInternals(dataDir, exec);
    } catch (final CanceledExecutionException e) {
        throw e;
    } catch (final Exception e) {
        LOGGER.debug("Error while loading internals: " + e.getMessage());
    }
}
Also used : CanceledExecutionException(org.knime.core.node.CanceledExecutionException) File(java.io.File) TooManySectionsException(org.knime.base.node.viz.pie.util.TooManySectionsException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 22 with CanceledExecutionException

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

the class AppendedRowsIterator method initNextRow.

/**
 * Get next row internally.
 */
private void initNextRow() {
    // reached end of table's iterator - take next
    if (!m_curIterator.hasNext()) {
        do {
            if (m_curItIndex < m_iteratorSuppliers.length - 1) {
                initNextTable();
            } else {
                // final end
                m_nextRow = null;
                // reached end of this table
                return;
            }
        } while (!m_curIterator.hasNext());
    }
    // row from table
    DataRow baseRow = m_curIterator.next();
    m_curRowIndex++;
    boolean keyHasChanged = false;
    RowKey origKey = baseRow.getKey();
    RowKey key = origKey;
    while (m_duplicateMap.containsKey(key)) {
        if (m_exec != null) {
            try {
                m_exec.checkCanceled();
            } catch (CanceledExecutionException cee) {
                throw new RuntimeCanceledExecutionException(cee);
            }
        }
        switch(m_duplPolicy) {
            case Fail:
                assert false : "Duplicate checking is done in the BDT";
                throw new RuntimeException("Duplicate key \"" + key + "\"");
            case Skip:
                if (!m_hasPrintedError) {
                    LOGGER.warn("Table contains duplicate entry \"" + key.toString() + "\", skipping this row. " + "Suppress further warnings.");
                    m_hasPrintedError = true;
                }
                if (!m_curIterator.hasNext()) {
                    // end of one table reached
                    // note, this causes one more call on the stack
                    // (but who wants to concatenate 60000 tables...)
                    initNextRow();
                    return;
                }
                if (m_exec != null) {
                    m_nrRowsSkipped++;
                    String message = "Skipping row " + m_curRowIndex + " (\"" + key.toString() + "\")";
                    if (m_totalRowCount > 0L) {
                        m_exec.setProgress(m_curRowIndex / (double) m_totalRowCount, message);
                    } else {
                        m_exec.setMessage(message);
                    }
                }
                // row from table
                baseRow = m_curIterator.next();
                m_curRowIndex++;
                // stays false! rows have been skipped.
                keyHasChanged = false;
                origKey = baseRow.getKey();
                key = origKey;
                break;
            case AppendSuffix:
                // first time we come here
                if (!keyHasChanged && m_exec != null) {
                    String message = "Unifying row " + m_curRowIndex + " (\"" + key.toString() + "\")";
                    if (m_totalRowCount > 0L) {
                        m_exec.setProgress(m_curRowIndex / (double) m_totalRowCount, message);
                    } else {
                        m_exec.setMessage(message);
                    }
                }
                keyHasChanged = true;
                String newId = key.toString() + m_suffix;
                key = new RowKey(newId);
                // to do duplicate handling.
                break;
            default:
                throw new RuntimeException("Unknown policy: " + m_duplPolicy);
        }
    }
    switch(m_duplPolicy) {
        case Fail:
            // to do a efficient duplicate checking
            break;
        default:
            m_duplicateMap.put(key, origKey);
    }
    if (m_exec != null) {
        try {
            m_exec.checkCanceled();
        } catch (CanceledExecutionException cee) {
            throw new RuntimeCanceledExecutionException(cee);
        }
        String message = "Adding row " + m_curRowIndex + " (\"" + key.toString() + "\"" + (keyHasChanged ? " uniquified)" : ")");
        if (m_totalRowCount > 0L) {
            m_exec.setProgress(m_curRowIndex / (double) m_totalRowCount, message);
        } else {
            m_exec.setMessage(message);
        }
    }
    DataRow nextRow;
    if (m_curMissingCells != null) {
        // no missing cells implies the base row is complete
        assert (m_curMissingCells.length + baseRow.getNumCells() == m_spec.getNumColumns());
        // row enlarged by "missing" columns
        DataRow filledBaseRow = new AppendedColumnRow(baseRow, m_curMissingCells);
        nextRow = new ResortedCellsRow(filledBaseRow, m_curMapping);
    } else {
        nextRow = baseRow;
    }
    if (keyHasChanged) {
        final boolean blobRow = (nextRow instanceof BlobSupportDataRow);
        DataCell[] cells = new DataCell[nextRow.getNumCells()];
        for (int i = 0; i < cells.length; i++) {
            cells[i] = blobRow ? ((BlobSupportDataRow) nextRow).getRawCell(i) : nextRow.getCell(i);
        }
        m_nextRow = new BlobSupportDataRow(key, cells);
    } else {
        m_nextRow = nextRow;
    }
}
Also used : RowKey(org.knime.core.data.RowKey) BlobSupportDataRow(org.knime.core.data.container.BlobSupportDataRow) DataRow(org.knime.core.data.DataRow) BlobSupportDataRow(org.knime.core.data.container.BlobSupportDataRow) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) DataCell(org.knime.core.data.DataCell)

Example 23 with CanceledExecutionException

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

the class Enh1536_CancelDuringLoad method testCancelWhileLoad.

@Test
public void testCancelWhileLoad() throws Exception {
    int previousChildCount = WorkflowManager.ROOT.getNodeContainers().size();
    LOAD_NODE_STATE_THREAD_LOCAL.set(LoadNodeState.Default);
    DefaultNodeProgressMonitor progMon = NODE_PROGRESS_THREAD_LOCAL.get();
    try {
        final WorkflowLoadHelper loadHelper = new WorkflowLoadHelper(m_workflowDirectory);
        WorkflowManager.ROOT.load(m_workflowDirectory, new ExecutionMonitor(progMon), loadHelper, true);
        fail("Workflow should not load as it's canceled");
    } catch (CanceledExecutionException e) {
        assertEquals(LoadNodeState.MiddleNodeCanceled, LOAD_NODE_STATE_THREAD_LOCAL.get());
    } finally {
        LOAD_NODE_STATE_THREAD_LOCAL.set(null);
    }
    assertEquals(previousChildCount, WorkflowManager.ROOT.getNodeContainers().size());
}
Also used : CanceledExecutionException(org.knime.core.node.CanceledExecutionException) DefaultNodeProgressMonitor(org.knime.core.node.DefaultNodeProgressMonitor) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) Test(org.junit.Test)

Example 24 with CanceledExecutionException

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

the class SVMLearnerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable inTable = (BufferedDataTable) inData[0];
    DataTableSpec inSpec = inTable.getDataTableSpec();
    LearnColumnsAndColumnRearrangerTuple tuple = createTrainTableColumnRearranger(inSpec);
    // no progress needed as constant operation (column removal only)
    BufferedDataTable trainTable = exec.createColumnRearrangeTable(inTable, tuple.getTrainingRearranger(), exec.createSubProgress(0.0));
    DataTableSpec trainSpec = trainTable.getDataTableSpec();
    int classpos = trainSpec.findColumnIndex(m_classcol.getStringValue());
    CheckUtils.checkArgument(classpos >= 0, "Selected class column not found: " + m_classcol.getStringValue());
    // convert input data
    ArrayList<DoubleVector> inputData = new ArrayList<DoubleVector>();
    List<String> categories = new ArrayList<String>();
    StringValue classvalue = null;
    for (DataRow row : trainTable) {
        exec.checkCanceled();
        ArrayList<Double> values = new ArrayList<Double>();
        boolean add = true;
        for (int i = 0; i < row.getNumCells(); i++) {
            if (row.getCell(i).isMissing()) {
                add = false;
                break;
            }
            if (i != classpos) {
                DoubleValue cell = (DoubleValue) row.getCell(i);
                values.add(cell.getDoubleValue());
            } else {
                classvalue = (StringValue) row.getCell(classpos);
                if (!categories.contains(classvalue.getStringValue())) {
                    categories.add(classvalue.getStringValue());
                }
            }
        }
        if (add) {
            @SuppressWarnings("null") final String nonNullClassValue = classvalue.getStringValue();
            inputData.add(new DoubleVector(row.getKey(), values, nonNullClassValue));
        }
    }
    if (categories.isEmpty()) {
        throw new Exception("No categories found to train SVM. " + "Possibly an empty input table was provided.");
    }
    DoubleVector[] inputDataArr = new DoubleVector[inputData.size()];
    inputDataArr = inputData.toArray(inputDataArr);
    Kernel kernel = KernelFactory.getKernel(m_kernelType);
    Vector<SettingsModelDouble> kernelparams = m_kernelParameters.get(m_kernelType);
    for (int i = 0; i < kernel.getNumberParameters(); ++i) {
        kernel.setParameter(i, kernelparams.get(i).getDoubleValue());
    }
    final Svm[] svms = new Svm[categories.size()];
    exec.setMessage("Training SVM");
    final BinarySvmRunnable[] bst = new BinarySvmRunnable[categories.size()];
    for (int i = 0; i < categories.size(); i++) {
        bst[i] = new BinarySvmRunnable(inputDataArr, categories.get(i), kernel, m_paramC.getDoubleValue(), exec.createSubProgress((1.0 / categories.size())));
    }
    ThreadPool pool = KNIMEConstants.GLOBAL_THREAD_POOL;
    final Future<?>[] fut = new Future<?>[bst.length];
    KNIMETimer timer = KNIMETimer.getInstance();
    TimerTask timerTask = new TimerTask() {

        @Override
        public void run() {
            try {
                exec.checkCanceled();
            } catch (final CanceledExecutionException ce) {
                for (int i = 0; i < fut.length; i++) {
                    if (fut[i] != null) {
                        fut[i].cancel(true);
                    }
                }
                super.cancel();
            }
        }
    };
    timer.scheduleAtFixedRate(timerTask, 0, 3000);
    for (int i = 0; i < bst.length; i++) {
        fut[i] = pool.enqueue(bst[i]);
    }
    try {
        pool.runInvisible(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                for (int i = 0; i < fut.length; ++i) {
                    fut[i].get();
                    bst[i].ok();
                    if (bst[i].getWarning() != null) {
                        setWarningMessage(bst[i].getWarning());
                    }
                    svms[i] = bst[i].getSvm();
                }
                return null;
            }
        });
    } catch (Exception ex) {
        exec.checkCanceled();
        Throwable t = ex;
        if (ex instanceof ExecutionException) {
            t = ex.getCause();
        }
        if (t instanceof Exception) {
            throw (Exception) t;
        } else {
            throw new Exception(t);
        }
    } finally {
        for (int i = 0; i < fut.length; i++) {
            fut[i].cancel(true);
        }
        timerTask.cancel();
    }
    // the optional PMML input (can be null)
    PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[1] : null;
    // create the outgoing PMML spec
    PMMLPortObjectSpecCreator specCreator = new PMMLPortObjectSpecCreator(inPMMLPort, inSpec);
    specCreator.setLearningCols(trainSpec);
    specCreator.setTargetCol(trainSpec.getColumnSpec(m_classcol.getStringValue()));
    // create the outgoing PMML port object
    PMMLPortObject outPMMLPort = new PMMLPortObject(specCreator.createSpec(), inPMMLPort, inSpec);
    outPMMLPort.addModelTranslater(new PMMLSVMTranslator(categories, Arrays.asList(svms), kernel));
    m_svms = svms;
    return new PortObject[] { outPMMLPort };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLSVMTranslator(org.knime.base.node.mine.svm.PMMLSVMTranslator) ArrayList(java.util.ArrayList) ThreadPool(org.knime.core.util.ThreadPool) SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) Svm(org.knime.base.node.mine.svm.Svm) DataRow(org.knime.core.data.DataRow) TimerTask(java.util.TimerTask) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedDataTable(org.knime.core.node.BufferedDataTable) StringValue(org.knime.core.data.StringValue) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Kernel(org.knime.base.node.mine.svm.kernel.Kernel) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) KNIMETimer(org.knime.core.util.KNIMETimer) BinarySvmRunnable(org.knime.base.node.mine.svm.util.BinarySvmRunnable) SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DoubleValue(org.knime.core.data.DoubleValue) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) Future(java.util.concurrent.Future) DoubleVector(org.knime.base.node.mine.svm.util.DoubleVector) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 25 with CanceledExecutionException

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

the class AbstractParallelNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected final BufferedDataTable[] execute(final BufferedDataTable[] data, final ExecutionContext exec) throws Exception {
    final DataTableSpec[] outSpecs = prepareExecute(data);
    final List<Future<BufferedDataContainer[]>> futures = new ArrayList<>();
    final BufferedDataTable[] additionalTables = new BufferedDataTable[Math.max(0, data.length - 1)];
    System.arraycopy(data, 1, additionalTables, 0, additionalTables.length);
    // do some consistency checks to bail out as early as possible
    if (outSpecs == null) {
        throw new NullPointerException("Implementation Error: The " + "array of generated output table specs can't be null.");
    }
    if (outSpecs.length != getNrOutPorts()) {
        throw new IllegalStateException("Implementation Error: Number of" + " provided DataTableSpecs doesn't match number of output" + " ports");
    }
    for (DataTableSpec outSpec : outSpecs) {
        if (outSpec == null) {
            throw new IllegalStateException("Implementation Error: The" + " generated output DataTableSpec is null.");
        }
    }
    final double max = data[0].size();
    final Callable<Void> submitter = new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            final RowIterator it = data[0].iterator();
            BufferedDataContainer container = null;
            int count = 0, chunks = 0;
            while (true) {
                if ((count++ % m_chunkSize == 0) || !it.hasNext()) {
                    exec.checkCanceled();
                    if (container != null) {
                        container.close();
                        final BufferedDataContainer temp = container;
                        chunks++;
                        final int temp2 = chunks;
                        futures.add(m_workers.submit(new Callable<BufferedDataContainer[]>() {

                            @Override
                            public BufferedDataContainer[] call() throws Exception {
                                ExecutionMonitor subProg = exec.createSilentSubProgress((m_chunkSize > max) ? 1 : m_chunkSize / max);
                                exec.setMessage("Processing chunk " + temp2);
                                BufferedDataContainer[] result = new BufferedDataContainer[outSpecs.length];
                                for (int i = 0; i < outSpecs.length; i++) {
                                    result[i] = exec.createDataContainer(outSpecs[i], true, 0);
                                }
                                executeByChunk(temp.getTable(), additionalTables, result, subProg);
                                for (DataContainer c : result) {
                                    c.close();
                                }
                                exec.setProgress(temp2 * m_chunkSize / max);
                                return result;
                            }
                        }));
                    }
                    if (!it.hasNext()) {
                        break;
                    }
                    container = exec.createDataContainer(data[0].getDataTableSpec());
                }
                container.addRowToTable(it.next());
            }
            return null;
        }
    };
    try {
        m_workers.runInvisible(submitter);
    } catch (IllegalThreadStateException ex) {
        // this node has not been started by a thread from a thread pool.
        // This is odd, but may happen
        submitter.call();
    }
    final BufferedDataTable[][] tempTables = new BufferedDataTable[outSpecs.length][futures.size()];
    int k = 0;
    for (Future<BufferedDataContainer[]> results : futures) {
        try {
            exec.checkCanceled();
        } catch (CanceledExecutionException ex) {
            for (Future<BufferedDataContainer[]> cancel : futures) {
                cancel.cancel(true);
            }
            throw ex;
        }
        final BufferedDataContainer[] temp = results.get();
        if ((temp == null) || (temp.length != getNrOutPorts())) {
            throw new IllegalStateException("Invalid result. Execution " + " failed, reason: data is null or number " + "of outputs wrong.");
        }
        for (int i = 0; i < temp.length; i++) {
            tempTables[i][k] = temp[i].getTable();
        }
        k++;
    }
    final BufferedDataTable[] resultTables = new BufferedDataTable[outSpecs.length];
    for (int i = 0; i < resultTables.length; i++) {
        resultTables[i] = exec.createConcatenateTable(exec, tempTables[i]);
    }
    return resultTables;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DataContainer(org.knime.core.data.container.DataContainer) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) RowIterator(org.knime.core.data.RowIterator) BufferedDataTable(org.knime.core.node.BufferedDataTable) Future(java.util.concurrent.Future) ExecutionMonitor(org.knime.core.node.ExecutionMonitor)

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