Mantid
Loading...
Searching...
No Matches
GroupWorkspaces.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 +
12#include "MantidParallel/Communicator.h"
13
14#include "Poco/Glob.h"
15
16namespace Mantid::Algorithms {
17
18DECLARE_ALGORITHM(GroupWorkspaces)
19
20using namespace API;
21using namespace Kernel;
22
25
27 std::make_unique<ArrayProperty<std::string>>("InputWorkspaces", std::make_shared<ADSValidator>(true, true)),
28 "Names of the Input Workspaces to Group");
29 declareProperty(std::make_unique<PropertyWithValue<std::string>>("GlobExpression", ""),
30 "Add all Workspaces that match Glob expression to Group");
31 declareProperty(std::make_unique<WorkspaceProperty<WorkspaceGroup>>("OutputWorkspace", "", Direction::Output),
32 "Name of the workspace to be created as the output of grouping ");
33}
34
39 const std::vector<std::string> inputWorkspaces = getProperty("InputWorkspaces");
40
41 const std::string globExpression = getProperty("GlobExpression");
42
43 // Clear WorkspaceGroup in case algorithm instance is reused.
44 m_group = nullptr;
45
46 if (!inputWorkspaces.empty())
47 addToGroup(inputWorkspaces);
48 if (!globExpression.empty())
49 addToGroup(globExpression);
50 if ((m_group == nullptr) || m_group->isEmpty())
51 throw std::invalid_argument("Glob pattern " + globExpression + " does not match any workspace names in the ADS.");
52 setProperty("OutputWorkspace", m_group);
53 auto &notifier = API::AnalysisDataService::Instance().notificationCenter;
54 notifier.postNotification(new WorkspacesGroupedNotification(inputWorkspaces));
55}
56
57std::map<std::string, std::string> GroupWorkspaces::validateInputs() {
58 std::map<std::string, std::string> results;
59 const std::vector<std::string> inputWorkspaces = getProperty("InputWorkspaces");
60 std::string globExpression = getProperty("GlobExpression");
61 std::string outputWorkspace = getProperty("OutputWorkspace");
62
63 // Peform a check that inputworkspaces cannot contain a workspace with the
64 // same name as the group/output workspace
65 for (const auto &ws : inputWorkspaces) {
66 if (ws == outputWorkspace) {
67 if (!AnalysisDataService::Instance().retrieve(ws)->isGroup())
68 results["OutputWorkspace"] = "The output workspace has the same name as "
69 "one of the input workspaces";
70 }
71 }
72
73 for (auto it = globExpression.begin(); it < globExpression.end(); ++it) {
74 if (*it == '\\') {
75 it = globExpression.erase(it, it + 1);
76 }
77 }
78
79 if (inputWorkspaces.empty() && globExpression.empty()) {
80 results["InputWorkspaces"] = "No InputWorkspace names specified. Rerun with a list of workspaces "
81 "names or a glob expression";
82 return results;
83 }
84
85 // ADSValidator already checks names in inputWorkspaces
86
87 if (!globExpression.empty()) {
88 // This is only a sanity check. If may fail to detect subtle errors in
89 // complex expressions.
90 if (globExpression.find_first_of("*?[]") == std::string::npos) {
91 results["GlobExpression"] = "Expression is expected to contain one or "
92 "more of the following characters: *?[";
93 return results;
94 }
95 if (std::count(globExpression.cbegin(), globExpression.cend(), '[') !=
96 std::count(globExpression.cbegin(), globExpression.cend(), ']')) {
97 results["GlobExpression"] = "Expression has a mismatched number of []";
98 return results;
99 }
100 }
101 return results;
102}
103
108void GroupWorkspaces::addToGroup(const std::string &globExpression) {
109
110 Poco::Glob glob(globExpression);
111
113 const auto names = ads.topLevelItems();
114 for (const auto &name : names) {
115 if (glob.match(name.first)) {
116 addToGroup(name.second);
117 }
118 }
119}
120
125void GroupWorkspaces::addToGroup(const std::vector<std::string> &names) {
126
128 for (const auto &name : names) {
129 auto workspace = ads.retrieve(name);
131 }
132}
133
139 auto localGroup = std::dynamic_pointer_cast<WorkspaceGroup>(workspace);
140 if (localGroup) {
141 addToGroup(localGroup->getNames());
142 // Remove the group from the ADS
143 AnalysisDataService::Instance().remove(workspace->getName());
144 } else {
145 if (!m_group)
146 m_group = std::make_shared<WorkspaceGroup>(workspace->storageMode());
147 else if (communicator().size() != 1 && m_group->storageMode() != workspace->storageMode()) {
148 throw std::runtime_error("WorkspaceGroup with mixed Parallel::Storage mode is not supported.");
149 }
150 m_group->addWorkspace(workspace);
151 }
152}
153
154Parallel::ExecutionMode
155GroupWorkspaces::getParallelExecutionMode(const std::map<std::string, Parallel::StorageMode> &storageModes) const {
156 static_cast<void>(storageModes);
157 const std::vector<std::string> names = getProperty("InputWorkspaces");
158 const auto ws = AnalysisDataService::Instance().retrieve(names.front());
159 return Parallel::getCorrespondingExecutionMode(ws->storageMode());
160}
161} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
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
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
const Parallel::Communicator & communicator() const
Returns a const reference to the (MPI) communicator of the algorithm.
Definition: Algorithm.cpp:1870
The Analysis data service stores instances of the Workspace objects and anything that derives from te...
std::map< std::string, Workspace_sptr > topLevelItems() const
Return a lookup of the top level items.
A property class for workspaces.
void init() override
Overridden Init method.
void exec() override
overridden execute method
Parallel::ExecutionMode getParallelExecutionMode(const std::map< std::string, Parallel::StorageMode > &storageModes) const override
Get correct execution mode based on input storage modes for an MPI run.
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
API::WorkspaceGroup_sptr m_group
A pointer to the new group.
void addToGroup(const std::vector< std::string > &names)
Add a list of names to the new group.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
std::shared_ptr< T > retrieve(const std::string &name) const
Get a shared pointer to a stored data object.
Definition: DataService.h:350
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
The concrete, templated class for properties.
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
AnalysisDataServiceImpl::GroupWorkspacesNotification WorkspacesGroupedNotification
@ Output
An output workspace.
Definition: Property.h:54