Mantid
Loading...
Searching...
No Matches
RenameWorkspaces.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//----------------------------------------------------------------------
16
17namespace Mantid::Algorithms {
18
19// Register the class into the algorithm factory
20DECLARE_ALGORITHM(RenameWorkspaces)
21
22using namespace Kernel;
23using namespace API;
24
30 "InputWorkspaces", std::make_shared<MandatoryValidator<std::vector<std::string>>>()),
31 "Names of the Input Workspaces");
32 // WorkspaceNames - List of new names
33 declareProperty(std::make_unique<ArrayProperty<std::string>>("WorkspaceNames", Direction::Input),
34 "New Names of the Workspaces");
35 // --or--
36 // Prefix
37 declareProperty("Prefix", std::string(""), "Prefix to add to input workspace names", Direction::Input);
38 // Suffix
39 declareProperty("Suffix", std::string(""), "Suffix to add to input workspace names", Direction::Input);
40 // Set to default true to maintain compatibility with existing scripts
41 // as this just allowed overriding by default
42 declareProperty<bool>("OverwriteExisting", true,
43 "If true all existing workspaces with the output name will be"
44 " overwritten. Defaults to true to maintain backwards compatibility.",
46}
47
52std::map<std::string, std::string> RenameWorkspaces::validateInputs() {
53 using namespace std;
54 map<string, string> errorList;
55
56 // Get the workspace name list
57 std::vector<std::string> newWsName = getProperty("WorkspaceNames");
58 // Get the prefix and suffix
59 std::string prefix = getPropertyValue("Prefix");
60 std::string suffix = getPropertyValue("Suffix");
61
62 // Check properties
63 if (newWsName.empty() && prefix.empty() && suffix.empty()) {
64 errorList["WorkspaceNames"] = "No list of Workspace names, prefix or suffix has been supplied.";
65 }
66
67 if (!newWsName.empty() && (!prefix.empty() || !suffix.empty())) {
68 errorList["WorkspaceNames"] = "Both a list of workspace names and a prefix "
69 "or suffix has been supplied.";
70 if (!prefix.empty()) {
71 errorList["Prefix"] = "Both a list of workspace names and a prefix "
72 "or suffix has been supplied.";
73 } else {
74 errorList["Suffix"] = "Both a list of workspace names and a prefix "
75 "or suffix has been supplied.";
76 }
77 }
78
79 if (newWsName.size() > 1) {
80 for (size_t i = 0; i < newWsName.size() - 1; ++i) {
81 for (size_t j = i + 1; j < newWsName.size(); ++j) {
82 if (newWsName[i] == newWsName[j]) {
83 errorList["WorkspaceNames"] = "Duplicate '" + newWsName[i] + "' found in WorkspaceNames.";
84 }
85 }
86 }
87 }
88
89 return errorList;
90}
91
97 // Get the input workspace list
98 std::vector<std::string> inputWsName = getProperty("InputWorkspaces");
99
100 // Get the workspace name list
101 std::vector<std::string> newWsName = getProperty("WorkspaceNames");
102 // Get the prefix and suffix
103 std::string prefix = getPropertyValue("Prefix");
104 std::string suffix = getPropertyValue("Suffix");
105
106 bool overrideWorkspace = getProperty("OverwriteExisting");
107
108 // Check properties
109
110 size_t nWs = inputWsName.size();
111 if (!newWsName.empty()) {
112 // We are using a list of new names
113 if (nWs > newWsName.size()) {
114 nWs = newWsName.size();
115 // could issue warning here
116 }
117 } else { // We are using prefix and/or suffix
118 // Build new names.
119 for (size_t i = 0; i < nWs; ++i) {
120 newWsName.emplace_back(prefix + inputWsName[i] + suffix);
121 }
122 }
123
124 // Check all names are not used already before starting rename
125 for (const auto &name : newWsName) {
126 if (AnalysisDataService::Instance().doesExist(name)) {
127 // Name exists, stop if override if not set but let
128 // RenameWorkspace handle deleting if we are overriding
129 if (!overrideWorkspace) {
130 throw std::runtime_error("A workspace called " + name + " already exists");
131 }
132 }
133 }
134
135 // loop over array and rename each workspace
136 for (size_t i = 0; i < nWs; ++i) {
137 std::ostringstream os;
138 os << "OutputWorkspace_" << i + 1;
139 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>(os.str(), newWsName[i], Direction::Output));
140 auto alg = createChildAlgorithm("RenameWorkspace");
141 alg->setPropertyValue("InputWorkspace", inputWsName[i]);
142 alg->setPropertyValue("OutputWorkspace", newWsName[i]);
143 alg->setProperty("OverwriteExisting", overrideWorkspace);
144
145 alg->executeAsChildAlg();
146
147 Workspace_sptr renamedWs = alg->getProperty("OutputWorkspace");
148 setProperty(os.str(), renamedWs);
149 }
150}
151
152} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
A property class for workspaces.
void exec() override
Executes the algorithm.
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
std::map< std::string, std::string > validateInputs() override
Validator to check out name does not already exist.
void init() override
Initialisation method.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
Validator to check that a property is not left empty.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54