From 96c50a5f91cd076aac6fb059e06d4bd279780e05 Mon Sep 17 00:00:00 2001 From: Samuel Jackson Date: Thu, 24 Apr 2014 17:01:45 +0100 Subject: [PATCH] Refs #8913 Add new methods to Algorithm class for nested history. --- .../Framework/API/inc/MantidAPI/Algorithm.h | 12 +++- Code/Mantid/Framework/API/src/Algorithm.cpp | 59 ++++++++++++++----- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h b/Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h index 5ee253389dac..cd3b24d7eb5d 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h @@ -270,7 +270,7 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag static IAlgorithm_sptr fromHistory(const AlgorithmHistory & history); //@} - boost::shared_ptr createChildAlgorithm(const std::string& name, const double startProgress = -1., + virtual boost::shared_ptr createChildAlgorithm(const std::string& name, const double startProgress = -1., const double endProgress = -1., const bool enableLogging=true, const int& version = -1); protected: @@ -312,6 +312,11 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag ///checks the property is a workspace property bool isWorkspaceProperty(const Kernel::Property* const prop) const; + + /// set whether we wish to track the child algorithm's history and pass it the parent object to fill. + void trackAlgorithmHistory(AlgorithmHistory* parentHist); + /// get whether we are tracking the history for this algorithm, + bool trackingHistory(); /// Set to true to stop execution bool m_cancel; @@ -334,7 +339,7 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag /// All the WorkspaceProperties that are Input or InOut. Set in execute() std::vector m_inputWorkspaceProps; - + /// Logger for this algorithm Kernel::Logger m_log; Kernel::Logger &g_log; @@ -381,7 +386,6 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag std::string m_WikiDescription; ///< Description in the wiki page. std::vector> m_ChildAlgorithms; ///< A list of weak pointers to any child algorithms created - /// Vector of all the workspaces that have been read-locked WorkspaceVector m_readLockedWorkspaces; /// Vector of all the workspaces that have been write-locked @@ -402,6 +406,8 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag size_t m_groupSize; /// All the groups have similar names (group_1, group_2 etc.) bool m_groupsHaveSimilarNames; + /// Pointer to the parent history object (if set) + AlgorithmHistory* m_parentHistory; /// A non-recursive mutex for thread-safety mutable Kernel::Mutex m_mutex; }; diff --git a/Code/Mantid/Framework/API/src/Algorithm.cpp b/Code/Mantid/Framework/API/src/Algorithm.cpp index 110c5b37eaf9..5a085987628b 100644 --- a/Code/Mantid/Framework/API/src/Algorithm.cpp +++ b/Code/Mantid/Framework/API/src/Algorithm.cpp @@ -2,8 +2,8 @@ // Includes //---------------------------------------------------------------------- #include "MantidAPI/Algorithm.h" -#include "MantidAPI/AlgorithmProxy.h" #include "MantidAPI/AlgorithmHistory.h" +#include "MantidAPI/AlgorithmProxy.h" #include "MantidAPI/AnalysisDataService.h" #include "MantidAPI/DeprecatedAlgorithm.h" #include "MantidAPI/AlgorithmManager.h" @@ -84,7 +84,8 @@ namespace Mantid m_isInitialized(false), 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_singleGroup(-1), m_groupSize(0), m_groupsHaveSimilarNames(false) + m_singleGroup(-1), m_groupSize(0), m_groupsHaveSimilarNames(false), + m_parentHistory(NULL) { } @@ -596,7 +597,7 @@ namespace Mantid Poco::FastMutex::ScopedLock _lock(m_mutex); m_running = true; } - if(!isChild() || m_recordHistoryForChild) + if(trackingHistory()) { // count used for defining the algorithm execution order // If history is being recorded we need to count this as a separate algorithm @@ -615,7 +616,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() || m_recordHistoryForChild) + if(trackingHistory()) fillHistory(start_time,duration,Algorithm::g_execCount); // Put any output workspaces into the AnalysisDataService - if this is not a child algorithm @@ -1017,22 +1018,52 @@ namespace Mantid // Create the history object for this algorithm AlgorithmHistory algHistory(this,start,duration,uexecCount); - std::vector::iterator outWS; - std::vector::const_iterator inWS; - // Loop over the output workspaces - for (outWS = outputWorkspaces.begin(); outWS != outputWorkspaces.end(); ++outWS) + + //this is not a child algorithm. Add the history algorithm to the WorkspaceHistory object. + if (!isChild()) { - // Loop over the input workspaces, making the call that copies their history to the output ones - // (Protection against copy to self is in WorkspaceHistory::copyAlgorithmHistory) - for (inWS = inputWorkspaces.begin(); inWS != inputWorkspaces.end(); ++inWS) + std::vector::iterator outWS; + std::vector::const_iterator inWS; + + // Loop over the output workspaces + for (outWS = outputWorkspaces.begin(); outWS != outputWorkspaces.end(); ++outWS) { - (*outWS)->history().addHistory( (*inWS)->getHistory() ); + // Loop over the input workspaces, making the call that copies their history to the output ones + // (Protection against copy to self is in WorkspaceHistory::copyAlgorithmHistory) + for (inWS = inputWorkspaces.begin(); inWS != inputWorkspaces.end(); ++inWS) + { + (*outWS)->history().addHistory( (*inWS)->getHistory() ); + } + // Add the history for the current algorithm to all the output workspaces + (*outWS)->history().addHistory(algHistory); } - // Add the history for the current algorithm to all the output workspaces - (*outWS)->history().addHistory(algHistory); } + //this is a child algorithm, but we still want to keep the history. + else if (m_recordHistoryForChild && m_parentHistory) + { + m_parentHistory->addHistory(algHistory); + } + } + + /** Indicates that this algrithms history should be tracked regardless of if it is a child. + * @param state :: a boolean indicating whether to track the history or not + * @param parent :: the parent algorithm history object the history in. + */ + void Algorithm::trackAlgorithmHistory(AlgorithmHistory* parentHist) + { + m_recordHistoryForChild = true; + m_parentHistory = parentHist; + } + + /** Check if we are tracking history for thus algorithm + * @return if we are tracking the history of this algorithm + */ + bool Algorithm::trackingHistory() + { + return (!isChild() || m_recordHistoryForChild); } + /** Populate lists of the input & output workspace properties. * (InOut workspaces go in both lists) * @param inputWorkspaces :: A reference to a vector for the input workspaces