Skip to content

Commit

Permalink
Re #7843. Run file and live data loading algorithms asynchronously.
Browse files Browse the repository at this point in the history
Using the AlgorithmRunner class.
  • Loading branch information
RussellTaylor committed Sep 5, 2013
1 parent 9f99d4e commit 6a7f0b1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 30 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/LiveData/src/StartLiveData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ namespace LiveData
// Give the listener directly to LoadLiveData (don't re-create it)
monitorAlg->setLiveListener(listener);

// Check for possible cancellation
interruption_point();
// Launch asyncronously
monitorAlg->executeAsync();

Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/MantidQt/API/inc/MantidQtAPI/AlgorithmRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace API
void cancelRunningAlgorithm();

void startAlgorithm(Mantid::API::IAlgorithm_sptr alg);
Mantid::API::IAlgorithm_sptr getAlgorithm() const;

signals:
/// Signal emitted when the algorithm has completed execution/encountered an error
Expand Down
6 changes: 6 additions & 0 deletions Code/Mantid/MantidQt/API/src/AlgorithmRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ namespace API
alg->addObserver(m_progressObserver);
}

/// Get back a pointer to the running algorithm
Mantid::API::IAlgorithm_sptr AlgorithmRunner::getAlgorithm() const
{
return m_asyncAlg;
}

//--------------------------------------------------------------------------------------
/** Observer called when the asynchronous algorithm has completed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//----------------------
#include "ui_StepScan.h"
#include "MantidQtAPI/UserSubWindow.h"
#include "MantidQtAPI/AlgorithmRunner.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/IAlgorithm.h"

Expand All @@ -32,7 +33,9 @@ class StepScan : public API::UserSubWindow

private slots:
void triggerLiveListener(bool checked);
void startLiveListenerComplete(bool error);
void loadFile();
void loadFileComplete(bool error);
void launchInstrumentWindow();
void fillPlotVarCombobox(const Mantid::API::MatrixWorkspace_const_sptr& ws);
void expandPlotVarCombobox(const Mantid::API::MatrixWorkspace_const_sptr& ws);
Expand Down Expand Up @@ -67,6 +70,7 @@ private slots:
bool m_dataReloadNeeded;
const std::string m_instrument; ///< The default instrument (for live data)

API::AlgorithmRunner * m_algRunner; ///< Object for running algorithms asynchronously
Poco::NObserver<StepScan, Mantid::API::WorkspaceAddNotification> m_addObserver;
Poco::NObserver<StepScan, Mantid::API::WorkspaceAfterReplaceNotification> m_replObserver;
bool m_replaceObserverAdded;
Expand Down
75 changes: 45 additions & 30 deletions Code/Mantid/MantidQt/CustomInterfaces/src/StepScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ using namespace Mantid::API;
StepScan::StepScan(QWidget *parent)
: UserSubWindow(parent), m_dataReloadNeeded(false),
m_instrument(ConfigService::Instance().getInstrument().name()),
m_algRunner(new API::AlgorithmRunner(this)),
m_addObserver(*this, &StepScan::handleAddEvent),
m_replObserver(*this, &StepScan::handleReplEvent),
m_replaceObserverAdded(false)
Expand All @@ -35,6 +36,8 @@ StepScan::StepScan(QWidget *parent)

StepScan::~StepScan()
{
// Stop any async algorithm
m_algRunner->cancelRunningAlgorithm();
// Stop live data collection, if running
m_uiForm.mWRunFiles->stopLiveAlgorithm();
// Disconnect the observers for the mask workspace combobox
Expand Down Expand Up @@ -116,40 +119,48 @@ void StepScan::startLiveListener()
return;
}

QApplication::setOverrideCursor(QCursor(Qt::BusyCursor));

// Remove any previously-loaded workspaces
cleanupWorkspaces();

// TODO: Run entirely asynchronously (see AlgorithmRunner)
m_algRunner->cancelRunningAlgorithm();
connect(m_algRunner, SIGNAL(algorithmComplete(bool)), SLOT(startLiveListenerComplete(bool)));

IAlgorithm_sptr startLiveData = AlgorithmManager::Instance().create("StartLiveData");
startLiveData->setProperty("UpdateEvery",5.0);
startLiveData->setProperty("FromNow",false);
startLiveData->setProperty("FromStartOfRun",true);
startLiveData->setProperty("Instrument",m_instrument);
m_inputWSName = "__live";
startLiveData->setProperty("OutputWorkspace",m_inputWSName);
try {
const bool executed = startLiveData->execute();
if ( ! executed ) throw std::runtime_error("Unable to run StartLiveData");
} catch (std::runtime_error&)
if ( ! startLiveData->validateInputs().empty() )
{
QApplication::restoreOverrideCursor();
QMessageBox::critical(this,"StartLiveData failed","Unable to start live data collection");
m_uiForm.mWRunFiles->liveButtonSetChecked(false);
return;
}
m_uiForm.mWRunFiles->setLiveAlgorithm(startLiveData);
m_algRunner->startAlgorithm(startLiveData);
}

// Keep track of the algorithm that's pulling in the live data
m_uiForm.mWRunFiles->setLiveAlgorithm(startLiveData->getProperty("MonitorLiveData"));

setupOptionControls();
void StepScan::startLiveListenerComplete(bool error)
{
disconnect(m_algRunner, SIGNAL(algorithmComplete(bool)), this, SLOT(startLiveListenerComplete(bool)));
if ( ! error )
{
// Keep track of the algorithm that's pulling in the live data
m_uiForm.mWRunFiles->setLiveAlgorithm(m_algRunner->getAlgorithm()->getProperty("MonitorLiveData"));

addReplaceObserverOnce();
connect( this, SIGNAL(logsUpdated(const Mantid::API::MatrixWorkspace_const_sptr &)),
SLOT(expandPlotVarCombobox(const Mantid::API::MatrixWorkspace_const_sptr &)) );
setupOptionControls();

QApplication::restoreOverrideCursor();
addReplaceObserverOnce();
connect( this, SIGNAL(logsUpdated(const Mantid::API::MatrixWorkspace_const_sptr &)),
SLOT(expandPlotVarCombobox(const Mantid::API::MatrixWorkspace_const_sptr &)) );
}
else
{
QMessageBox::critical(this,"StartLiveData failed","Unable to start live data collection");
m_uiForm.mWRunFiles->liveButtonSetChecked(false);
}
}

void StepScan::loadFile()
Expand All @@ -161,28 +172,32 @@ void StepScan::loadFile()
{
m_inputFilename = filename;

QApplication::setOverrideCursor(QCursor(Qt::BusyCursor));

// Remove any previously-loaded workspaces
cleanupWorkspaces();

// TODO: Run entirely asynchronously (see AlgorithmRunner or AlgorithmObserver)
m_algRunner->cancelRunningAlgorithm();
connect(m_algRunner, SIGNAL(algorithmComplete(bool)), SLOT(loadFileComplete(bool)));

IAlgorithm_sptr alg = AlgorithmManager::Instance().create("LoadEventNexus");
alg->setPropertyValue("Filename", filename.toStdString());
m_inputWSName = "__" + QFileInfo(filename).baseName().toStdString();
alg->setPropertyValue("OutputWorkspace", m_inputWSName);
alg->setProperty("LoadMonitors", true);
if ( alg->execute() ) // executeAsync???
{
QApplication::restoreOverrideCursor();
m_dataReloadNeeded = false;
setupOptionControls();
}
else
{
QApplication::restoreOverrideCursor();
QMessageBox::warning(this,"File loading failed","Is this an event nexus file?");
}
m_algRunner->startAlgorithm(alg);
}
}

void StepScan::loadFileComplete(bool error)
{
disconnect(m_algRunner, SIGNAL(algorithmComplete(bool)), this, SLOT(loadFileComplete(bool)));
if ( ! error )
{
m_dataReloadNeeded = false;
setupOptionControls();
}
else
{
QMessageBox::warning(this,"File loading failed","Is this an event nexus file?");
}
}

Expand Down

0 comments on commit 6a7f0b1

Please sign in to comment.