19size_t MAXIMUM_DEPTH = 100;
21Kernel::Logger
g_log(
"WorkspaceGroup");
26 m_beforeReplaceObserver(*this, &
WorkspaceGroup::workspaceBeforeReplaceHandle), m_workspaces(),
27 m_observingADS(false) {}
40 const std::string firstLine = this->
id() +
"\n";
41 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
42 const auto descr = std::accumulate(
44 [](
const auto &
string,
const auto &
workspace) { return string +
" -- " + workspace->getName() +
'\n'; });
58 AnalysisDataService::Instance().notificationCenter.addObserver(
m_deleteObserver);
64 AnalysisDataService::Instance().notificationCenter.removeObserver(
m_deleteObserver);
76 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
81 if (
group->isInGroup(workspaceToCheck))
92 if (this->
size() == 0) {
103 if (indices.size() != this->size()) {
104 g_log.
warning(
"Number of indices must match the number of workspace members\n");
108 if (!std::all_of(indices.cbegin(), indices.cend(),
109 [&](
const int i) { return i >= 0 && i < static_cast<int>(m_workspaces.size()); })) {
110 g_log.
warning(
"All indices must be >= 0 and < the number of workspaces in the group\n");
115 if (std::unordered_set<int>(indices.cbegin(), indices.cend()).size() != indices.size()) {
120 std::vector<Mantid::API::Workspace_sptr> reordered;
121 std::transform(indices.cbegin(), indices.cend(), std::back_inserter(reordered),
122 [&](
const auto &i) { return m_workspaces[i]; });
133 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
135 g_log.
warning(
"Can't add a workspace as a child of itself!\n");
142 g_log.
warning() <<
"Workspace already exists in a WorkspaceGroup\n";
152 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
155 if (workspace->isGroup()) {
156 const auto group = std::dynamic_pointer_cast<WorkspaceGroup>(workspace);
157 return group->containsInChildren(wsName);
162 return it != m_workspaces.cend();
171 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
173 [&wsName](
const auto &
workspace) { return workspace->getName() == wsName; });
181 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
192 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
202 std::vector<std::string> out;
203 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
207 [](
const auto &ws) { return ws->getName(); });
218 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
231 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
233 [&wsName](
const auto &
workspace) { return workspace->getName() == wsName; });
235 throw std::out_of_range(
"Workspace " + wsName +
" not contained in the group");
244 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
257 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
260 if ((**it).getName() == wsName) {
270 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
282 std::ostringstream os;
283 os <<
"WorkspaceGroup - index out of range. Requested=" <<
index <<
", current size=" << this->
size();
284 throw std::out_of_range(os.str());
326 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
328 if (!this->
getName().empty()) {
329 throw std::runtime_error(
"AnalysisDataService must be used to remove a "
330 "workspace from group.");
333 std::ostringstream os;
334 os <<
"WorkspaceGroup - index out of range. Requested=" <<
index <<
", current size=" << this->
size();
335 throw std::out_of_range(os.str());
350 std::unique_lock<std::recursive_mutex> _lock(
m_mutex);
351 const std::string deletedName = notice->objectName();
355 if (deletedName != this->
getName()) {
365 AnalysisDataService::Instance().remove(this->
getName());
378 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
380 const auto oldObject = notice->oldObject();
381 const auto newObject = notice->newObject();
383 bool foundOld(
false);
384 bool foundDuplicate(
false);
396 foundDuplicate =
true;
399 if (foundOld && foundDuplicate) {
414 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
426 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
432 const std::string &wsName = workspace->getName();
434 std::size_t pos = wsName.find_last_of(
'_');
436 if (pos == std::string::npos)
440 std::string commonpart(wsName.substr(0, pos));
441 if (this->getName() != commonpart)
453 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
455 g_log.
debug(
"Not a multiperiod-group with < 1 nested workspace.");
466 g_log.
debug(
"Not a multiperiod-group with nperiods log < 1.");
470 g_log.
debug(
"Not a multiperiod-group without nperiods log on all "
471 "nested workspaces.");
475 g_log.
debug(
"Not a multiperiod-group unless all inner workspaces are "
476 "Matrix Workspaces.");
488 [](
auto ws) { return dynamic_cast<IPeaksWorkspace *>(ws.get()) != nullptr; });
500 if (level > MAXIMUM_DEPTH) {
501 throw std::runtime_error(
"WorkspaceGroup nesting level is too deep.");
503 std::lock_guard<std::recursive_mutex> _lock(
m_mutex);
505 if (
workspace.get() == &workspaceToCheck)
509 if (
group->isInGroup(workspaceToCheck, level + 1))
517 auto total = std::size_t(0);
522 total = total + std::dynamic_pointer_cast<WorkspaceGroup>(
workspace)->getMemorySize();
525 total = total +
workspace->getMemorySize();
538IPropertyManager::getValue<Mantid::API::WorkspaceGroup_sptr>(
const std::string &
name)
const {
543 std::string message =
544 "Attempt to assign property " +
name +
" to incorrect type. Expected shared_ptr<WorkspaceGroup>.";
545 throw std::runtime_error(message);
551IPropertyManager::getValue<Mantid::API::WorkspaceGroup_const_sptr>(
const std::string &
name)
const {
552 const auto *prop =
dynamic_cast<PropertyWithValue<Mantid::API::WorkspaceGroup_sptr> *
>(getPointerToProperty(
name));
554 return prop->operator()();
556 std::string message =
557 "Attempt to assign property " +
name +
" to incorrect type. Expected const shared_ptr<WorkspaceGroup>.";
558 throw std::runtime_error(message);
IPeaksWorkspace_sptr workspace
std::map< DeltaEMode::Type, std::string > index
Class to hold a set of workspaces.
std::vector< Workspace_sptr > getAllItems() const
Return all workspaces in the group as one call for thread safety.
void observeADSNotifications(const bool observeADS)
Turn ADS observations on/off.
void addWorkspace(const Workspace_sptr &workspace)
Adds a workspace to the group.
const std::string toString() const override
Returns a formatted string detailing the contents of the group.
std::vector< Workspace_sptr >::iterator end()
Returns a non-const iterator pointing at the last element in the workspace group.
Poco::NObserver< WorkspaceGroup, Mantid::API::WorkspaceBeforeReplaceNotification > m_beforeReplaceObserver
Observer for workspace before-replace notifications.
void removeAll()
Remove all names from the group but do not touch the ADS.
void reorderMembersWithIndices(const std::vector< int > &indices)
Reorder the group members using a list of indices (e.g the list 4,3,2,1 would reverse the order)
void workspaceDeleteHandle(Mantid::API::WorkspacePostDeleteNotification_ptr notice)
Callback when a delete notification is received.
const std::string id() const override
Return a string ID of the class.
Poco::NObserver< WorkspaceGroup, Mantid::API::WorkspacePostDeleteNotification > m_deleteObserver
Observer for workspace delete notifications.
WorkspaceGroup()
Default constructor.
~WorkspaceGroup() override
Destructor.
size_t getMemorySize() const override
Return the memory size of all workspaces in this group and subgroups.
bool m_observingADS
Flag as to whether the observers have been added to the ADS.
std::vector< Workspace_sptr >::iterator begin()
Returns a non-const iterator pointing at the first element in the workspace group.
void removeByADS(const std::string &wsName)
ADS removes a member of this group using this method.
void print() const
Prints the group to the screen using the logger at debug.
bool isEmpty() const
This method returns true if the group is empty (no member workspace)
void removeItem(const size_t index)
Remove a workspace from the group.
bool contains(const std::string &wsName) const
Does a workspace exist within the group.
Workspace_sptr getItem(const size_t index) const
Return the ith workspace.
bool containsInChildren(const std::string &wsName) const
Does a workspace exist within the group or any groups within this group.
void reportMembers(std::set< Workspace_sptr > &memberList) const
Add the members of the group to the given list.
std::vector< Workspace_sptr > m_workspaces
The list of workspace pointers in the group.
bool isInChildGroup(const Workspace &workspaceToCheck) const
Check if a workspace is included in any child groups and groups in them.
std::vector< std::string > getNames() const
Returns the names of workspaces that make up this group.
std::recursive_mutex m_mutex
Recursive mutex to avoid simultaneous access.
void sortMembersByName()
Sort the internal data structure according to member name.
void workspaceBeforeReplaceHandle(Mantid::API::WorkspaceBeforeReplaceNotification_ptr notice)
Callback when a before-replace notification is received.
void throwIndexOutOfRangeError(int index) const
Throws an out_of_range error for an invalid index.
bool isMultiperiod() const
Indicates that the workspace group can be treated as multiperiod.
bool areNamesSimilar() const
Are the members of this group of similar names, e.g.
bool isInGroup(const Workspace &workspaceToCheck, size_t level=0) const
Check if a workspace is included in this group or any nested groups.
bool isGroupPeaksWorkspaces() const
Check if the workspace group contains just peak workspaces.
size_t size() const
Return the size of the group, so it is more like a container.
Base Workspace Abstract Class.
const std::string & getName() const override
Get the workspace name.
Exception for when an item is not found in a collection.
void debug(const std::string &msg)
Logs at debug level.
void warning(const std::string &msg)
Logs at warning level.
The concrete, templated class for properties.
Base class for properties.
virtual std::string value() const =0
Returns the value of the property as a string.
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< const WorkspaceGroup > WorkspaceGroup_const_sptr
shared pointer to Mantid::API::WorkspaceGroup, pointer to const version
const Poco::AutoPtr< Mantid::Kernel::DataService< Mantid::API::Workspace >::BeforeReplaceNotification > & WorkspaceBeforeReplaceNotification_ptr
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
const Poco::AutoPtr< Mantid::Kernel::DataService< Mantid::API::Workspace >::PostDeleteNotification > & WorkspacePostDeleteNotification_ptr
int convert(const std::string &A, T &out)
Convert a string into a number.