-
Notifications
You must be signed in to change notification settings - Fork 122
/
WorkspaceGroup.cpp
478 lines (438 loc) · 13.9 KB
/
WorkspaceGroup.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/IPropertyManager.h"
#include <Poco/ScopedLock.h>
namespace Mantid
{
namespace API
{
Kernel::Logger& WorkspaceGroup::g_log = Kernel::Logger::get("WorkspaceGroup");
size_t WorkspaceGroup::g_maximum_depth = 100;
WorkspaceGroup::WorkspaceGroup() :
Workspace(),
m_deleteObserver(*this, &WorkspaceGroup::workspaceDeleteHandle),
m_replaceObserver(*this, &WorkspaceGroup::workspaceReplaceHandle),
m_workspaces(), m_observingADS(false)
{
}
WorkspaceGroup::~WorkspaceGroup()
{
observeADSNotifications(false);
}
/**
* The format is:
* ID
* -- Name1
* -- Name2
* @returns A formatted human-readable string specifying the contents of the group
*/
const std::string WorkspaceGroup::toString() const
{
std::string descr = this->id() + "\n";
Poco::Mutex::ScopedLock _lock(m_mutex);
for(auto it = m_workspaces.begin(); it != m_workspaces.end(); ++it)
{
descr += " -- " + (*it)->name() + "\n";
}
return descr;
}
/**
* Turn on/off observing delete and rename notifications to update the group accordingly
* It can be useful to turn them off when constructing the group.
* @param observeADS :: If true observe the ADS notifications, otherwise disable them
*/
void WorkspaceGroup::observeADSNotifications(const bool observeADS)
{
if( observeADS )
{
if(!m_observingADS)
{
AnalysisDataService::Instance().notificationCenter.addObserver(m_deleteObserver);
AnalysisDataService::Instance().notificationCenter.addObserver(m_replaceObserver);
m_observingADS = true;
}
}
else
{
if(m_observingADS)
{
AnalysisDataService::Instance().notificationCenter.removeObserver(m_deleteObserver);
AnalysisDataService::Instance().notificationCenter.removeObserver(m_replaceObserver);
m_observingADS = false;
}
}
}
/**
* @param workspace :: A workspace to check.
* @return :: True if the workspace is found.
*/
bool WorkspaceGroup::isInChildGroup(const Workspace &workspace) const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
for(auto ws = m_workspaces.begin(); ws != m_workspaces.end(); ++ws)
{
// check child groups only
WorkspaceGroup *group = dynamic_cast<WorkspaceGroup*>( ws->get() );
if ( group )
{
if ( group->isInGroup( workspace ) ) return true;
}
}
return false;
}
/**
* Adds a workspace to the group. The workspace does not have to be in the ADS
* @param workspace :: A shared pointer to a workspace to add. If the workspace already exists give a warning.
*/
void WorkspaceGroup::addWorkspace(Workspace_sptr workspace)
{
Poco::Mutex::ScopedLock _lock(m_mutex);
// check it's not there already
auto it = std::find(m_workspaces.begin(), m_workspaces.end(), workspace);
if ( it == m_workspaces.end() )
{
m_workspaces.push_back( workspace );
}
else
{
g_log.warning() << "Workspace already exists in a WorkspaceGroup" << std::endl;;
}
}
/**
* Does this group contain the named workspace?
* @param wsName :: A string to compare
* @returns True if the name is part of this group, false otherwise
*/
bool WorkspaceGroup::contains(const std::string & wsName) const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
for(auto it = m_workspaces.begin(); it != m_workspaces.end(); ++it)
{
if ( (**it).name() == wsName ) return true;
}
return false;
}
/**
* @param workspace A pointer to a workspace
* @returns True if the workspace exists in the group, false otherwise
*/
bool WorkspaceGroup::contains(const Workspace_sptr & workspace) const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
auto iend = m_workspaces.end();
auto it = std::find(m_workspaces.begin(),iend, workspace);
return (it != iend);
}
/**
* Adds the current workspace members to the given list
* @param memberList
*/
void WorkspaceGroup::reportMembers(std::set<Workspace_sptr> & memberList) const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
memberList.insert(m_workspaces.begin(), m_workspaces.end());
}
/**
* Returns the names of workspaces that make up this group.
* Note that this returns a copy as the internal vector can mutate while the vector is being iterated over.
*/
std::vector<std::string> WorkspaceGroup::getNames() const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
std::vector<std::string> out;
for(auto it = m_workspaces.begin(); it != m_workspaces.end(); ++it)
{
out.push_back( (**it).name() );
}
return out;
}
/**
* Return the ith workspace
* @param index The index within the group
* @throws an out_of_range error if the index is invalid
*/
Workspace_sptr WorkspaceGroup::getItem(const size_t index) const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
if( index >= this->size() )
{
std::ostringstream os;
os << "WorkspaceGroup - index out of range. Requested=" << index << ", current size=" << this->size();
throw std::out_of_range(os.str());
}
return m_workspaces[index];
}
/**
* Return the workspace by name
* @param wsName The name of the workspace
* @throws an out_of_range error if the workspace'sname not contained in the group's list of workspace names
*/
Workspace_sptr WorkspaceGroup::getItem(const std::string wsName) const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
for(auto it = m_workspaces.begin(); it != m_workspaces.end(); ++it)
{
if ( (**it).name() == wsName ) return *it;
}
throw std::out_of_range("Workspace "+wsName+" not contained in the group");
}
/// Empty all the entries out of the workspace group. Does not remove the workspaces from the ADS.
void WorkspaceGroup::removeAll()
{
m_workspaces.clear();
}
/** Remove the named workspace from the group. Does not delete the workspace from the AnalysisDataService.
* @param wsName :: The name of the workspace to be removed from the group.
*/
void WorkspaceGroup::removeByADS(const std::string& wsName)
{
Poco::Mutex::ScopedLock _lock(m_mutex);
auto it = m_workspaces.begin();
for(; it != m_workspaces.end(); ++it)
{
if ( (**it).name() == wsName )
{
m_workspaces.erase(it);
break;
}
}
}
/// Print the names of all the workspaces in this group to the logger (at debug level)
void WorkspaceGroup::print() const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
for (auto itr = m_workspaces.begin(); itr != m_workspaces.end(); ++itr)
{
g_log.debug() << "Workspace name in group vector = " << (**itr).name() << std::endl;
}
}
/**
* Remove a workspace pointed to by an index. The workspace remains in the ADS if it was there
*
* @param index :: Index of a workspace to delete.
*/
void WorkspaceGroup::removeItem(const size_t index)
{
Poco::Mutex::ScopedLock _lock(m_mutex);
// do not allow this way of removing for groups in the ADS
if ( ! name().empty() )
{
throw std::runtime_error("AnalysisDataService must be used to remove a workspace from group.");
}
if( index >= this->size() )
{
std::ostringstream os;
os << "WorkspaceGroup - index out of range. Requested=" << index << ", current size=" << this->size();
throw std::out_of_range(os.str());
}
auto it = m_workspaces.begin() + index;
m_workspaces.erase( it );
}
/** Callback for a workspace delete notification
*
* Removes any deleted entries from the group.
* This also deletes the workspace group when the last member of it gets deteleted.
*
* @param notice :: A pointer to a workspace delete notificiation object
*/
void WorkspaceGroup::workspaceDeleteHandle(Mantid::API::WorkspacePostDeleteNotification_ptr notice)
{
Poco::Mutex::ScopedLock _lock(m_mutex);
const std::string deletedName = notice->object_name();
if( !this->contains(deletedName)) return;
if( deletedName != this->getName() )
{
this->removeByADS(deletedName);
if(isEmpty())
{
//We are about to get deleted so we don't want to recieve any notifications
observeADSNotifications(false);
AnalysisDataService::Instance().remove(this->getName());
}
}
}
/**
* Callback when a after-replace notification is received
* Replaces a member if it was replaced in the ADS.
* @param notice :: A pointer to a workspace after-replace notificiation object
*/
void WorkspaceGroup::workspaceReplaceHandle(Mantid::API::WorkspaceBeforeReplaceNotification_ptr notice)
{
Poco::Mutex::ScopedLock _lock(m_mutex);
std::string newName = notice->object_name();
if(this->contains(newName))
{
bool isObserving = m_observingADS;
if ( isObserving )
observeADSNotifications( false );
//remove any workspace that already exists with the name
if(this->name() != newName)
{
//remove workspace from group
std::vector<Workspace_sptr>::iterator iter = this->m_workspaces.begin();
for(; iter != m_workspaces.end(); ++iter)
{
if((**iter).name() == newName)
{
m_workspaces.erase(iter);
break;
}
}
//replace the object in the group
this->addWorkspace(notice->new_object());
}
if ( isObserving )
observeADSNotifications( true );
}
}
/**
* This method returns true if the workspace group is empty
* @return true if workspace is empty
*/
bool WorkspaceGroup::isEmpty() const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
return m_workspaces.empty();
}
//------------------------------------------------------------------------------
/** Are the members of this group of similar names,
* e.g. for a WorkspaceGroup names "groupname",
* the members are "groupname_1", "groupname_2", etc.
*
* @return true if the names match this pattern.
*/
bool WorkspaceGroup::areNamesSimilar() const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
if(m_workspaces.empty()) return false;
//Check all the members are of similar names
for(auto citr=m_workspaces.begin(); citr!=m_workspaces.end(); ++citr)
{
const std::string wsName = (**citr).name();
// Find the last underscore _
std::size_t pos = wsName.find_last_of("_");
// No underscore = not similar
if(pos==std::string::npos)
return false;
// The part before the underscore has to be the same
// as the group name to be similar
std::string commonpart(wsName.substr(0,pos));
if (this->name() != commonpart)
return false;
}
return true;
}
//------------------------------------------------------------------------------
/**
Determine in the WorkspaceGroup is multiperiod.
* @return True if the WorkspaceGroup instance is multiperiod.
*/
bool WorkspaceGroup::isMultiperiod() const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
if(m_workspaces.size() < 1)
{
g_log.debug("Not a multiperiod-group with < 1 nested workspace.");
return false;
}
std::vector<Workspace_sptr>::const_iterator iterator = m_workspaces.begin();
// Loop through all inner workspaces, checking each one in turn.
while(iterator != m_workspaces.end())
{
if(MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(*iterator))
{
try
{
Kernel::Property* nPeriodsProp = ws->run().getLogData("nperiods");
int num = -1;
Kernel::Strings::convert(nPeriodsProp->value(), num);
if(num < 1)
{
g_log.debug("Not a multiperiod-group with nperiods log < 1.");
return false;
}
}
catch(Kernel::Exception::NotFoundError&)
{
g_log.debug("Not a multiperiod-group without nperiods log on all nested workspaces.");
return false;
}
}
else
{
g_log.debug("Not a multiperiod-group unless all inner workspaces are Matrix Workspaces.");
return false;
}
++iterator;
}
return true;
}
/**
* @param workspace :: A workspace to check.
* @param level :: The current nesting level. Intended for internal use only by WorkspaceGroup.
* @return :: True if the worspace is found in any of the nested groups in this group.
*/
bool WorkspaceGroup::isInGroup(const Workspace &workspace, size_t level) const
{
// Check for a cycle.
if ( level > g_maximum_depth )
{
throw std::runtime_error("WorkspaceGroup nesting level is too deep.");
}
Poco::Mutex::ScopedLock _lock(m_mutex);
for(auto ws = m_workspaces.begin(); ws != m_workspaces.end(); ++ws)
{
if ( ws->get() == &workspace ) return true;
WorkspaceGroup *group = dynamic_cast<WorkspaceGroup*>( ws->get() );
if ( group )
{
if ( group->isInGroup( workspace, level + 1 ) ) return true;
}
}
return false;
}
} // namespace API
} // namespace Mantid
/// @cond TEMPLATE
namespace Mantid
{
namespace Kernel
{
template<> MANTID_API_DLL
Mantid::API::WorkspaceGroup_sptr IPropertyManager::getValue<Mantid::API::WorkspaceGroup_sptr>(const std::string &name) const
{
PropertyWithValue<Mantid::API::WorkspaceGroup_sptr>* prop =
dynamic_cast<PropertyWithValue<Mantid::API::WorkspaceGroup_sptr>*>(getPointerToProperty(name));
if (prop)
{
return *prop;
}
else
{
std::string message = "Attempt to assign property "+ name +" to incorrect type. Expected WorkspaceGroup.";
throw std::runtime_error(message);
}
}
template<> MANTID_API_DLL
Mantid::API::WorkspaceGroup_const_sptr IPropertyManager::getValue<Mantid::API::WorkspaceGroup_const_sptr>(const std::string &name) const
{
PropertyWithValue<Mantid::API::WorkspaceGroup_sptr>* prop =
dynamic_cast<PropertyWithValue<Mantid::API::WorkspaceGroup_sptr>*>(getPointerToProperty(name));
if (prop)
{
return prop->operator()();
}
else
{
std::string message = "Attempt to assign property "+ name +" to incorrect type. Expected const WorkspaceGroup.";
throw std::runtime_error(message);
}
}
} // namespace Kernel
} // namespace Mantid
/// @endcond TEMPLATE