22#include <boost/algorithm/string/classification.hpp>
23#include <boost/range/algorithm/remove_if.hpp>
24#include <boost/utility.hpp>
40 bool appendTimestamp, std::vector<std::string> ignoreTheseAlgs,
41 std::vector<std::vector<std::string>> ignoreTheseAlgProperties,
bool appendExecCount)
42 : m_historyItems(view->getAlgorithmsList()), m_output(), m_versionSpecificity(
std::move(versionSpecificity)),
43 m_timestampCommands(appendTimestamp), m_algsToIgnore(
std::move(ignoreTheseAlgs)),
44 m_propertiesToIgnore(
std::move(ignoreTheseAlgProperties)), m_execCount(appendExecCount) {}
52 std::ostringstream os;
53 os <<
"from mantid.simpleapi import *\n\n";
74 auto algHistory = iter->getAlgorithmHistory();
75 if (iter->isUnrolled()) {
77 os << std::string(depth,
'#');
78 os <<
" Child algorithms of " << algHistory->name() <<
"\n";
83 os << std::string(depth,
'#');
84 os <<
" End of child algorithms of " << algHistory->name() <<
"\n";
86 if (boost::next(iter) ==
m_historyItems.end() || !boost::next(iter)->isUnrolled()) {
89 os <<
" # " << algHistory->executionDate().toISO8601String();
104 std::shared_ptr<const Mantid::API::AlgorithmHistory> &algHistory) {
107 os <<
" # " << algHistory->executionDate().toISO8601String();
112 os <<
" execCount: " << algHistory->execCount();
114 os <<
" # execCount: " << algHistory->execCount();
134 size_t numChildren = iter->numberOfChildren();
136 for (
size_t i = 0; i < numChildren && iter !=
m_historyItems.end(); ++i, ++iter) {
149 std::ostringstream comment;
150 const std::string
name = algHistory.
name();
153 for (
auto &prop : props) {
154 if (prop->name() ==
"Text") {
155 comment <<
"# " << prop->value();
159 return comment.str();
169 std::ostringstream properties;
170 const std::string
name = algHistory.
name();
180 if (
name ==
"Load") {
182 algFresh = AlgorithmManager::Instance().createUnmanaged(algHistory.
getPropertyValue(
"LoaderName"), version);
184 algFresh = AlgorithmManager::Instance().createUnmanaged(
name, algHistory.
version());
186 algFresh->initialize();
188 const auto &propsFresh = algFresh->getProperties();
190 std::set<std::string> freshPropNames;
191 for (
const auto &propFresh : propsFresh) {
192 freshPropNames.insert(propFresh->name());
197 for (
auto prop_iter = props.begin(); prop_iter != props.end();) {
198 if (freshPropNames.find((*prop_iter)->name()) == freshPropNames.end() &&
200 prop_iter = props.erase(prop_iter);
206 }
catch (std::exception &) {
207 g_log.
error() <<
"Could not create a fresh version of " <<
name <<
" version " << algHistory.
version() <<
"\n";
212 for (
const auto &propIter : props) {
213 if (!storeInADS && propIter->name() ==
"OutputWorkspace") {
217 if (prop.length() > 0) {
218 properties << prop <<
", ";
224 properties <<
"Version=" << algHistory.
version() <<
", ";
227 const auto &algName = algHistory.
name();
228 auto &algFactory = API::AlgorithmFactory::Instance();
229 int latestVersion = 0;
231 if (algFactory.exists(algName)) {
232 latestVersion = AlgorithmFactory::Instance().highestVersion(algName);
236 if (latestVersion > algHistory.
version()) {
237 properties <<
"Version=" << algHistory.
version() <<
", ";
242 std::string assignmentStatement(
"");
244 properties <<
"StoreInADS=False, ";
246 std::find_if(props.cbegin(), props.cend(), [](
const auto &prop) { return prop->name() ==
"OutputWorkspace"; });
247 if (it != props.cend()) {
248 assignmentStatement = (*it)->value() +
" = ";
252 std::string propStr = properties.str();
253 if (propStr.length() > 0) {
255 propStr.erase(propStr.size() - 1);
256 propStr.erase(propStr.size() - 1);
259 std::string historyEntry = assignmentStatement +
name +
"(" + propStr +
")";
260 historyEntry.erase(boost::remove_if(historyEntry, boost::is_any_of(
"\n\r")), historyEntry.end());
274 const
std::
string &algName) {
278 if (std::find_if(m_propertiesToIgnore.begin(), m_propertiesToIgnore.end(),
279 [&propHistory, algName](std::vector<std::string>
const &c) ->
bool {
280 return algName == c[0] && propHistory.name() == c[1];
281 }) != m_propertiesToIgnore.end()) {
287 if (!propHistory.isDefault()) {
289 std::vector<std::string> nonWorkspaceTypes{
"number",
"boolean",
"string"};
291 if (find(nonWorkspaceTypes.begin(), nonWorkspaceTypes.end(), propHistory.type()) != nonWorkspaceTypes.end() &&
294 if (m_algsToIgnore.size() == 0) {
295 g_log.
debug() <<
"Ignoring property " << propHistory.name() <<
" of type " << propHistory.type() <<
'\n';
298 }
else if (propHistory.type() ==
"number") {
299 prop = propHistory.name() +
"=" + propHistory.value();
301 }
else if (propHistory.type() ==
"boolean") {
302 std::string
value = (propHistory.value() ==
"1" ?
"True" :
"False");
303 prop = propHistory.name() +
"=" +
value;
306 std::string opener =
"='";
307 std::string closer =
"'";
308 if (propHistory.value().find(
'\\') != std::string::npos) {
310 }
else if (propHistory.pythonVariable()) {
315 prop = propHistory.name() + opener + propHistory.value() + closer;
double value
The value of the point.
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
This class stores information about the Command History used by algorithms on a workspace.
const bool & getStoreInADS() const
get storeInADS
const std::string & getPropertyValue(const std::string &name) const
get the string representation of a specified property
const std::string & name() const
get name of algorithm in history const
const int & version() const
get version number of algorithm in history const
const Mantid::Kernel::PropertyHistories & getProperties() const
get parameter list of algorithm in history const
This class build a sttring which cana be executed as a python script.
void createStringForAlg(std::ostringstream &os, std::shared_ptr< const Mantid::API::AlgorithmHistory > &algHistory)
const std::string buildCommentString(const AlgorithmHistory &algHistory)
Build the script output for a single comment.
void writeHistoryToStream(std::ostringstream &os, std::vector< HistoryItem >::const_iterator &iter, int depth=1)
Write out an algorithm to the script.
std::vector< std::string > m_algsToIgnore
const std::vector< HistoryItem > m_historyItems
ScriptBuilder(const std::shared_ptr< HistoryView > &view, std::string versionSpecificity="old", bool appendTimestamp=false, std::vector< std::string > ignoreTheseAlgs={}, std::vector< std::vector< std::string > > ignoreTheseAlgProperties={}, bool appendExecCount=false)
void buildChildren(std::ostringstream &os, std::vector< HistoryItem >::const_iterator &iter, int depth=1)
Iterate over each of the items children and output them to the script.
const std::string buildAlgorithmString(const AlgorithmHistory &algHistory)
Build the script output for a single algorithm.
const std::string buildPropertyString(const Mantid::Kernel::PropertyHistory &propHistory, const std::string &algName)
Build the script output for a single property.
std::string m_versionSpecificity
const std::string build()
build a python script from the history view
The Logger class is in charge of the publishing messages from the framework through various channels.
void debug(const std::string &msg)
Logs at debug level.
void error(const std::string &msg)
Logs at error level.
This class stores information about the parameters used by an algorithm.
Kernel::Logger g_log("ExperimentInfo")
static logger object
const std::string COMMENT_ALG
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
std::shared_ptr< const PropertyHistory > PropertyHistory_const_sptr
std::shared_ptr< PropertyHistory > PropertyHistory_sptr
Helper class which provides the Collimation Length for SANS instruments.
Describes the direction (within an algorithm) of a Property.
@ Output
An output workspace.