Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/HistoryItem.h"
#include "MantidAPI/ScriptBuilder.h"
#include <boost/utility.hpp>
namespace Mantid
{
namespace API
{
ScriptBuilder::ScriptBuilder(const HistoryView& view)
: m_historyItems(view.getAlgorithmsList()), m_output()
{
}
const std::string ScriptBuilder::build()
{
std::ostringstream os;
auto iter = m_historyItems.cbegin();
for( ; iter != m_historyItems.cend(); ++iter)
{
writeHistoryToStream(os, iter);
}
return os.str();
}
void ScriptBuilder::writeHistoryToStream(std::ostringstream& os, std::vector<HistoryItem>::const_iterator& iter)
{
auto algHistory = iter->getAlgorithmHistory();
if(iter->isUnrolled())
{
//don't create a line for the algorithm, just output its children
os << "# Child algorithms of " << algHistory->name() << "\n";
buildChildren(os, iter);
os << "# End of child algorithms of " << algHistory->name() << "\n";
}
else
{
//create the string for this algorithm
os << buildAlgorithmString(algHistory) << "\n";
}
}
void ScriptBuilder::buildChildren(std::ostringstream& os, std::vector<HistoryItem>::const_iterator& iter)
{
size_t numChildren = iter->numberOfChildren();
++iter; //move to first child
for(size_t i = 0; i < numChildren && iter != m_historyItems.cend(); ++i, ++iter)
{
writeHistoryToStream(os, iter);
}
--iter;
}
const std::string ScriptBuilder::buildAlgorithmString(AlgorithmHistory_const_sptr algHistory)
{
std::ostringstream properties;
const std::string name = algHistory->name();
std::string prop = "";
auto props = algHistory->getProperties();
for (auto propIter = props.cbegin(); propIter != props.cend(); ++propIter)
{
prop = buildPropertyString(*propIter);
if(prop.length() > 0)
{
properties << prop;
if(boost::next(propIter) != props.cend()
&& !(boost::next(propIter, 2) == props.cend() && boost::next(propIter)->isDefault()))
{
properties << ", ";
}
}
}
return name + "(" + properties.str() + ")";
}
const std::string ScriptBuilder::buildPropertyString(const Mantid::Kernel::PropertyHistory& propHistory)
{
std::string prop = "";
if (!propHistory.isDefault())
{
prop = propHistory.name() + "='" + propHistory.value() + "'";
}
return prop;
}
} // namespace API
} // namespace Mantid