Mantid
Loading...
Searching...
No Matches
AlgorithmHistory.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
11#include "MantidAPI/Algorithm.h"
12
13#if BOOST_VERSION == 106900
14#ifndef BOOST_PENDING_INTEGER_LOG2_HPP
15#define BOOST_PENDING_INTEGER_LOG2_HPP
16#include <boost/integer/integer_log2.hpp>
17#endif /* BOOST_PENDING_INTEGER_LOG2_HPP */
18#endif /* BOOST_VERSION */
19
20#include <boost/uuid/uuid.hpp>
21#include <boost/uuid/uuid_generators.hpp>
22#include <boost/uuid/uuid_io.hpp>
23
24#include <algorithm>
25#include <iterator>
26#include <sstream>
27#include <utility>
28
29namespace Mantid::API {
30
31using Kernel::Property;
33using Kernel::PropertyHistory;
36using Types::Core::DateAndTime;
37
38namespace {
40static boost::uuids::random_generator uuidGen;
41} // namespace
42
51AlgorithmHistory::AlgorithmHistory(const Algorithm *const alg, const Types::Core::DateAndTime &start,
52 const double &duration, std::size_t uexeccount)
53 : m_name(alg->name()), m_version(alg->version()), m_executionDate(start), m_executionDuration(duration),
54 m_execCount(uexeccount), m_childHistories() {
55 // Now go through the algorithm's properties and create the PropertyHistory
56 // objects.
57 setProperties(alg);
58 m_uuid = boost::uuids::to_string(uuidGen());
59}
60
62AlgorithmHistory::AlgorithmHistory() : m_uuid(boost::uuids::to_string(uuidGen())) {}
63
76AlgorithmHistory::AlgorithmHistory(std::string name, int vers, std::string uuid, const Types::Core::DateAndTime &start,
77 const double &duration, std::size_t uexeccount)
78 : m_name(std::move(name)), m_version(vers), m_executionDate(start), m_executionDuration(duration),
79 m_execCount(uexeccount), m_childHistories(), m_uuid(std::move(uuid)) {}
80
87 // overwrite any existing properties
88 m_properties.clear();
89 // Now go through the algorithm's properties and create the PropertyHistory
90 // objects.
91 const std::vector<Property *> &properties = alg->getProperties();
92 std::transform(properties.cbegin(), properties.cend(), std::back_inserter(m_properties),
93 [](const auto &property) { return std::make_shared<PropertyHistory>(property->createHistory()); });
94}
95
105void AlgorithmHistory::fillAlgorithmHistory(const Algorithm *const alg, const Types::Core::DateAndTime &start,
106 const double &duration, std::size_t uexeccount) {
107 m_name = alg->name();
108 m_version = alg->version();
109 m_executionDate = start;
110 m_executionDuration = duration;
111 m_execCount = uexeccount;
112 setProperties(alg);
113}
114
119void AlgorithmHistory::addExecutionInfo(const DateAndTime &start, const double &duration) {
120 m_executionDate = start;
121 m_executionDuration = duration;
122}
123
130void AlgorithmHistory::addProperty(const std::string &name, const std::string &value, bool isdefault,
131 const unsigned int &direction) {
132 m_properties.emplace_back(std::make_shared<PropertyHistory>(name, value, "", isdefault, direction));
133}
134
139 // Don't copy one's own history onto oneself
140 if (this == &(*childHist)) {
141 return;
142 }
143
144 m_childHistories.emplace_back(childHist);
145}
146
147/*
148 Return the child history length
149 */
151
159 if (index >= this->getChildHistories().size()) {
160 throw std::out_of_range("AlgorithmHistory::getAlgorithmHistory() - Index out of range");
161 }
162 return *std::next(m_childHistories.cbegin(), index);
163}
164
172
179const std::string &AlgorithmHistory::getPropertyValue(const std::string &name) const {
180 const auto found = std::find_if(m_properties.cbegin(), m_properties.cend(),
181 [&name](const auto &history) { return history->name() == name; });
182 if (found == m_properties.cend()) {
183 throw Kernel::Exception::NotFoundError("Could not find the specified property", name);
184 }
185 return (*found)->value();
186}
187
193std::shared_ptr<IAlgorithm> AlgorithmHistory::getChildAlgorithm(const size_t index) const {
194 return Algorithm::fromHistory(*(this->getChildAlgorithmHistory(index)));
195}
196
204void AlgorithmHistory::printSelf(std::ostream &os, const int indent, const size_t maxPropertyLength) const {
205 auto execDate = m_executionDate.toISO8601String();
206 execDate.replace(execDate.find("T"), 1, " ");
207 os << std::string(indent, ' ') << "Algorithm: " << m_name;
208 os << std::string(indent, ' ') << " v" << m_version << '\n';
209
210 os << std::string(indent, ' ') << "Execution Date: " << execDate << '\n';
211 os << std::string(indent, ' ') << "Execution Duration: " << m_executionDuration << " seconds\n";
212 os << std::string(indent, ' ') << "UUID: " << m_uuid << '\n';
213 os << std::string(indent, ' ') << "Parameters:\n";
214
215 for (const auto &property : m_properties) {
216 property->printSelf(os, indent + 2, maxPropertyLength);
217 }
218}
219
224std::shared_ptr<IAlgorithm> AlgorithmHistory::createAlgorithm() const { return Algorithm::fromHistory(*this); }
225
231 if (this != &A) {
232 m_name = A.m_name;
237 // required to prevent destruction of descendant if assigning a descendant
238 // to an ancestor
239 auto temp = A.m_childHistories;
240 m_childHistories = temp;
241 m_uuid = A.m_uuid;
242 }
243 return *this;
244}
245
251std::ostream &operator<<(std::ostream &os, const AlgorithmHistory &AH) {
252 AH.printSelf(os);
253 return os;
254}
255
260void AlgorithmHistory::saveNexus(::NeXus::File *file, int &algCount) const {
261 std::stringstream algNumber;
262 ++algCount;
263 algNumber << "MantidAlgorithm_" << algCount; // history entry names start at 1 not 0
264
265 std::stringstream algData;
266 printSelf(algData);
267
268 file->makeGroup(algNumber.str(), "NXnote", true);
269 file->writeData("author", std::string("mantid"));
270 file->writeData("description", std::string("Mantid Algorithm data"));
271 file->writeData("data", algData.str());
272
273 // child algorithms
274 for (auto &history : m_childHistories) {
275 history->saveNexus(file, algCount);
276 }
277 file->closeGroup();
278}
279} // namespace Mantid::API
double value
The value of the point.
Definition: FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
std::vector< history_type > history
history information
This class stores information about the Command History used by algorithms on a workspace.
std::size_t m_execCount
count keeps track of execution order of an algorithm
double m_executionDuration
The execution duration of the algorithm.
Mantid::Kernel::PropertyHistories m_properties
The PropertyHistory's defined for the algorithm.
void addChildHistory(const AlgorithmHistory_sptr &childHist)
add a child algorithm history record to this history object
AlgorithmHistory & operator=(const AlgorithmHistory &)
Standard Assignment operator.
std::shared_ptr< IAlgorithm > getChildAlgorithm(const size_t index) const
Create an child algorithm from a history record at a given index.
const AlgorithmHistories & getChildHistories() const
get the child histories of this history object
void saveNexus(::NeXus::File *file, int &algCount) const
Write this history object to a nexus file.
AlgorithmHistories m_childHistories
set of child algorithm histories for this history record
void setProperties(const Algorithm *const alg)
Set the history properties for an algorithm pointer.
Mantid::Types::Core::DateAndTime m_executionDate
The execution date of the algorithm.
const std::string & getPropertyValue(const std::string &name) const
get the string representation of a specified property
std::string m_uuid
UUID for this algorithm history.
size_t childHistorySize() const
Retrieve the number of child algorithms.
AlgorithmHistory()
Default constructor.
int m_version
The version of the algorithm.
void addExecutionInfo(const Types::Core::DateAndTime &start, const double &duration)
Add details of an algorithm's execution to an existing history object.
const std::string & name() const
get name of algorithm in history const
std::string m_name
The name of the Algorithm.
void fillAlgorithmHistory(const Algorithm *const alg, const Types::Core::DateAndTime &start, const double &duration, std::size_t uexeccount)
Set data on history after it is created.
std::shared_ptr< IAlgorithm > createAlgorithm() const
Create a concrete algorithm based on a history record.
AlgorithmHistory_sptr operator[](const size_t index) const
Add operator[] access.
AlgorithmHistory_sptr getChildAlgorithmHistory(const size_t index) const
Retrieve a child algorithm history by index.
void printSelf(std::ostream &, const int indent=0, const size_t maxPropertyLength=0) const
print contents of object
void addProperty(const std::string &name, const std::string &value, bool isdefault, const unsigned int &direction=99)
Add a property to the history.
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:85
int version() const override=0
function to return a version of the algorithm, must be overridden in all algorithms
static IAlgorithm_sptr fromHistory(const AlgorithmHistory &history)
Construct an object from a history entry.
Definition: Algorithm.cpp:929
const std::vector< Kernel::Property * > & getProperties() const override
Get the list of managed properties.
Definition: Algorithm.cpp:2050
const std::string name() const override=0
function to return a name of the algorithm, must be overridden in all algorithms
Exception for when an item is not found in a collection.
Definition: Exception.h:145
MANTID_API_DLL std::ostream & operator<<(std::ostream &, const AlgorithmHistory &)
Prints a text representation.
std::shared_ptr< AlgorithmHistory > AlgorithmHistory_sptr
std::shared_ptr< const PropertyHistory > PropertyHistory_const_sptr
std::shared_ptr< PropertyHistory > PropertyHistory_sptr
std::vector< PropertyHistory_sptr > PropertyHistories
Definition: NDArray.h:49
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)