Newer
Older
Peterson, Peter
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/GroupWorkspaces.h"
Russell Taylor
committed
namespace Mantid {
namespace Algorithms {
Russell Taylor
committed
Russell Taylor
committed
Peterson, Peter
committed
/// 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");
make_unique<WorkspaceProperty<WorkspaceGroup>>("OutputWorkspace", "",
Direction::Output),
"Name of the workspace to be created as the output of grouping ");
}
Peterson, Peter
committed
/** 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");
Russell Taylor
committed
m_group = boost::make_shared<WorkspaceGroup>();
addToGroup(inputWorkspaces);
Russell Taylor
committed
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) {
Russell Taylor
committed
AnalysisDataServiceImpl &ads = AnalysisDataService::Instance();
for (const auto &name : names) {
auto workspace = ads.retrieve(name);
Russell Taylor
committed
/**
* 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->name());
} else {
m_group->addWorkspace(workspace);
Russell Taylor
committed
}