Skip to content

Commit

Permalink
Enable history for child alg in python. Refs #5158
Browse files Browse the repository at this point in the history
Setting them as parents causes issues with the workspace locking
and really isn't the right thing to do.
  • Loading branch information
martyngigg committed Apr 25, 2012
1 parent 63866b5 commit 7f8da44
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag

bool isChild() const;
void setChild(const bool isChild);
void enableHistoryRecordingForChild(const bool on);
void setAlwaysStoreInADS(const bool doStore);
void setRethrows(const bool rethrow);

Expand Down Expand Up @@ -337,6 +338,7 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag
bool m_isInitialized; ///< Algorithm has been initialized flag
bool m_isExecuted; ///< Algorithm is executed flag
bool m_isChildAlgorithm; ///< Algorithm is a child algorithm
bool m_recordHistoryForChild; ///< Flag to indicate whether history should be recorded. Applicable to child algs only
bool m_alwaysStoreInADS; ///< Always store in the ADS, even for child algos
bool m_runningAsync; ///< Algorithm is running asynchronously
bool m_running; ///< Algorithm is running
Expand Down
4 changes: 3 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ namespace Mantid
/// To query whether algorithm is a child. A proxy is always at top level, returns false
bool isChild() const {return m_isChild;}
void setAlwaysStoreInADS(const bool ) {}
void setChild(const bool val) {m_isChild = val;};
void setChild(const bool val) {m_isChild = val;}
/// Proxies only manage parent algorithms
void enableHistoryRecordingForChild(const bool) {};
void setRethrows(const bool rethrow);

/** @name PropertyManager methods */
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/IAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ class MANTID_API_DLL IAlgorithm : virtual public Kernel::IPropertyManager
*/
virtual void setChild(const bool isChild) = 0;

/// If true history will be recorded for a child
virtual void enableHistoryRecordingForChild(const bool on) = 0;

/// Set whether we always store the output in the analysis data service
virtual void setAlwaysStoreInADS(const bool doStore) = 0;

Expand Down
21 changes: 17 additions & 4 deletions Code/Mantid/Framework/API/src/Algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ namespace Mantid
PropertyManagerOwner(),m_progressObserver(*this, &Algorithm::handleChildProgressNotification),
m_cancel(false),m_parallelException(false),g_log(Kernel::Logger::get("Algorithm")),
m_executeAsync(this,&Algorithm::executeAsyncImpl),m_isInitialized(false),
m_isExecuted(false),m_isChildAlgorithm(false),m_alwaysStoreInADS(false),m_runningAsync(false),
m_running(false),m_rethrow(false),m_algorithmID(this)
m_isExecuted(false),m_isChildAlgorithm(false), m_recordHistoryForChild(false),
m_alwaysStoreInADS(false),m_runningAsync(false),
m_running(false),m_rethrow(false),m_algorithmID(this),
m_processGroups(false), m_singleGroup(-1), m_groupSize(0), m_groupsHaveSimilarNames(false)
{
}

Expand Down Expand Up @@ -133,6 +135,16 @@ namespace Mantid
m_isChildAlgorithm = isChild;
}

/**
* Change the state of the history recording flag. Only applicable for
* child algorithms.
* @param on :: The new state of the flag
*/
void Algorithm::enableHistoryRecordingForChild(const bool on)
{
m_recordHistoryForChild = on;
}

/** Do we ALWAYS store in the AnalysisDataService? This is set to true
* for python algorithms' child algorithms
*
Expand Down Expand Up @@ -525,7 +537,7 @@ namespace Mantid
const float duration = timer.elapsed();

// need it to throw before trying to run fillhistory() on an algorithm which has failed
if (!isChild())
if(!isChild() || m_recordHistoryForChild)
fillHistory(start_time,duration,Algorithm::g_execCount);

// Put any output workspaces into the AnalysisDataService - if this is not a child algorithm
Expand Down Expand Up @@ -690,9 +702,10 @@ namespace Mantid
{
alg->initialize();
}
catch (std::runtime_error&)
catch (std::runtime_error& exc)
{
g_log.error() << "Unable to initialise sub-algorithm " << name << std::endl;
g_log.error() << exc.what() << "\n";
}

// If output workspaces are nameless, give them a temporary name to satisfy validator
Expand Down
7 changes: 4 additions & 3 deletions Code/Mantid/Framework/PythonAPI/MantidFramework.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,9 @@ def _createAlgProxy(self, ialg, version=-1):

# Algorithm is being called as part of a Python algorithm.
ialg = parentAlg._createSubAlgorithm(ialg, version)
ialg.__async__ = False

# History is always active until the workflow algorithms are in place
ialg.enableHistoryRecordingForChild(True)

# Children do not log if the parent is not logging
ialg.setLogging( parentAlg.isLogging() )
Expand All @@ -1284,8 +1286,7 @@ def _createAlgProxy(self, ialg, version=-1):
ialg.setAlwaysStoreInADS(True)
else:
ialg = self.createManagedAlgorithm(ialg, version)
ialg.__async__ = HAVE_GUI
ialg.setRethrows(True) # Ensure the console rethrows. Async ones rethrow anyway
ialg.setRethrows(True) # Ensure the console rethrows.
return IAlgorithmProxy(ialg, self)

# make what comes out of C++ a little friendlier to use
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/Framework/PythonAPI/src/api_exports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ using namespace boost::python;
.def("isLogging", &API::IAlgorithm::isLogging)
.def("isExecuted", &API::IAlgorithm::isExecuted)
.def("setChild", &API::IAlgorithm::setChild)
.def("enableHistoryRecordingForChild", &IAlgorithm::enableHistoryRecordingForChild)
.def("setLogging", &API::IAlgorithm::setLogging)
.def("setAlwaysStoreInADS", &API::IAlgorithm::setAlwaysStoreInADS)
.def("setRethrows", &API::IAlgorithm::setRethrows)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,21 @@ namespace
{
UNUSED_ARG(self);
IAlgorithm_sptr alg;
int async(0);
if( Mantid::PythonInterface::Environment::isInCallStack("PyExec") )
{
alg = AlgorithmManager::Instance().createUnmanaged(name, version);
alg->initialize();
async = 0;
alg->setLogging(true);
alg->setAlwaysStoreInADS(true);
alg->enableHistoryRecordingForChild(true);
}
else
{
alg = AlgorithmManager::Instance().create(name, version); // This will be initialized already
async = 1;
}
alg->setRethrows(true);

PyObject * wrapped = converter::shared_ptr_to_python(alg);
// Add an attribute to indicate asynchronous execution
PyObject_SetAttrString(wrapped, "__async__", PyBool_FromLong(async));
return wrapped;
return converter::shared_ptr_to_python(alg);
}

//------------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def _is_managed_test(self, alg, version):
self.assertTrue(alg.isInitialized())
self.assertTrue(alg.version(), version)
self.assertTrue(isinstance(alg, AlgorithmProxy))
self.assertTrue(hasattr(alg, '__async__'))
self.assertTrue(alg.__async__)

def test_create_algorithm_produces_managed_alg_outside_PyExec_with_async_attr_and_is_true(self):
alg = FrameworkManager.createAlgorithm("Rebin")
Expand All @@ -37,8 +35,6 @@ def PyExec(self):
alg = FrameworkManager.createAlgorithm("Rebin")
self._test_obj.assertTrue(alg.isInitialized())
self._test_obj.assertFalse(isinstance(alg, AlgorithmProxy))
self._test_obj.assertTrue(hasattr(alg, '__async__'))
self._test_obj.assertFalse(alg.__async__)

top_level = TestAlg(self)
top_level.PyExec()
Expand Down

0 comments on commit 7f8da44

Please sign in to comment.