-
Notifications
You must be signed in to change notification settings - Fork 122
/
GroupWorkspaces.cpp
73 lines (61 loc) · 2.24 KB
/
GroupWorkspaces.cpp
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
#include "MantidAlgorithms/GroupWorkspaces.h"
#include "MantidAPI/ADSValidator.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidKernel/ArrayProperty.h"
namespace Mantid {
namespace Algorithms {
DECLARE_ALGORITHM(GroupWorkspaces)
using namespace API;
using namespace Kernel;
/// Default constructor
GroupWorkspaces::GroupWorkspaces() : API::Algorithm(), m_group() {}
/// Initialisation method
void GroupWorkspaces::init() {
declareProperty(Kernel::make_unique<ArrayProperty<std::string>>(
"InputWorkspaces", boost::make_shared<ADSValidator>()),
"Names of the Input Workspaces to Group");
declareProperty(
make_unique<WorkspaceProperty<WorkspaceGroup>>("OutputWorkspace", "",
Direction::Output),
"Name of the workspace to be created as the output of grouping ");
}
/** Executes the algorithm
* @throw std::runtime_error If the selected workspaces are not of same types
*/
void GroupWorkspaces::exec() {
const std::vector<std::string> inputWorkspaces =
getProperty("InputWorkspaces");
m_group = boost::make_shared<WorkspaceGroup>();
addToGroup(inputWorkspaces);
setProperty("OutputWorkspace", m_group);
auto ¬ifier = API::AnalysisDataService::Instance().notificationCenter;
notifier.postNotification(new WorkspacesGroupedNotification(inputWorkspaces));
}
/**
* Add a list of names to the new group
* @param names The list of names to add from the ADS
*/
void GroupWorkspaces::addToGroup(const std::vector<std::string> &names) {
AnalysisDataServiceImpl &ads = AnalysisDataService::Instance();
for (const auto &name : names) {
auto workspace = ads.retrieve(name);
addToGroup(workspace);
}
}
/**
* If it is a group it is unrolled and each member added
* @param workspace A pointer to the workspace to add
*/
void GroupWorkspaces::addToGroup(const API::Workspace_sptr &workspace) {
auto localGroup = boost::dynamic_pointer_cast<WorkspaceGroup>(workspace);
if (localGroup) {
addToGroup(localGroup->getNames());
// Remove the group from the ADS
AnalysisDataService::Instance().remove(workspace->getName());
} else {
m_group->addWorkspace(workspace);
}
}
}
}