28 return std::dynamic_pointer_cast<const WorkspaceGroup>(this->
object());
46 const size_t length =
name.size();
47 for (
size_t i = 0; i < length; ++i) {
48 if (illegal.find_first_of(
name[i]) != std::string::npos) {
49 std::ostringstream strm;
50 strm <<
"Invalid object name '" <<
name <<
"'. Names cannot contain any of the following characters: " << illegal;
80 group->observeADSNotifications(
true);
81 for (
size_t i = 0; i <
group->size(); ++i) {
82 auto ws =
group->getItem(i);
83 std::string wsName = ws->getName();
91 if (!wsName.empty()) {
106 auto group = std::dynamic_pointer_cast<WorkspaceGroup>(
workspace);
116 group->observeADSNotifications(
true);
117 for (
size_t i = 0; i <
group->size(); ++i) {
118 auto ws =
group->getItem(i);
119 std::string wsName = ws->getName();
121 if (wsName.empty()) {
127 if (!wsName.empty()) {
141 auto oldWorkspace =
retrieve(oldName);
142 auto group = std::dynamic_pointer_cast<WorkspaceGroup>(oldWorkspace);
143 if (
group &&
group->containsInChildren(newName)) {
144 throw std::invalid_argument(
"Unable to rename group as the new name matches its members");
150 ws->setName(newName);
172 auto group = std::dynamic_pointer_cast<WorkspaceGroup>(ws);
174 group->observeADSNotifications(
false);
185 static const std::string alphabet =
"abcdefghijklmnopqrstuvwxyz";
186 static std::random_device rd;
187 static std::mt19937 gen(rd());
188 static std::uniform_int_distribution<> dis(0,
int(alphabet.size() - 1));
189 return alphabet[dis(gen)];
200 const std::string &suffix) {
202 throw std::invalid_argument(
"n must be a positive number");
204 auto randomNameGenerator = [
n]() {
206 for (
int i = 0; i <
n; ++i) {
213 for (
int i = 0; i < (
n * 260); i++) {
214 std::string wsName = prefix + randomNameGenerator() + suffix;
219 throw std::runtime_error(
"Unable to generate unique workspace of length " +
std::to_string(
n));
239 bool unrollGroups)
const {
240 using WorkspacesVector = std::vector<Workspace_sptr>;
241 WorkspacesVector workspaces;
242 workspaces.reserve(names.size());
243 std::transform(std::begin(names), std::end(names), std::back_inserter(workspaces),
244 [
this](
const std::string &
name) {
return this->
retrieve(name); });
245 assert(names.size() == workspaces.size());
247 using IteratorDifference = std::iterator_traits<WorkspacesVector::iterator>::difference_type;
249 bool done{workspaces.size() == 0};
252 if (
auto group = std::dynamic_pointer_cast<WorkspaceGroup>(workspaces.at(i))) {
253 const auto groupLength(
group->size());
254 workspaces.erase(std::next(std::begin(workspaces),
static_cast<IteratorDifference
>(i)));
255 for (
size_t j = 0; j < groupLength; ++j) {
256 workspaces.insert(std::next(std::begin(workspaces),
static_cast<IteratorDifference
>(i + j)),
264 if (i == workspaces.size()) {
279 throw std::runtime_error(
"Workspace " + groupName +
" is not a workspace group.");
281 group->sortMembersByName();
293 throw std::runtime_error(
"Workspace " + groupName +
" is not a workspace group.");
296 if (groupName == wsName) {
297 throw std::runtime_error(
"The group name and workspace name are the same");
301 group->addWorkspace(ws);
312 throw std::runtime_error(
"Workspace " +
name +
" is not a workspace group.");
314 group->observeADSNotifications(
false);
315 for (
size_t i = 0; i <
group->size(); ++i) {
316 auto ws =
group->getItem(i);
337 throw std::runtime_error(
"Workspace " + groupName +
" is not a workspace group.");
339 if (!
group->contains(wsName)) {
340 throw std::runtime_error(
"WorkspaceGroup " + groupName +
" does not containt workspace " + wsName);
342 group->removeByADS(wsName);
352 std::map<std::string, Workspace_sptr> topLevel;
354 std::set<Workspace_sptr> groupMembers;
356 for (
const auto &topLevelName : topLevelNames) {
358 const std::string &
name = topLevelName;
359 auto ws = this->
retrieve(topLevelName);
360 topLevel.emplace(
name, ws);
361 if (
auto group = std::dynamic_pointer_cast<WorkspaceGroup>(ws)) {
362 group->reportMembers(groupMembers);
364 }
catch (
const std::exception &) {
369 for (
auto it = topLevel.begin(); it != topLevel.end();) {
371 if (groupMembers.count(item) == 1) {
372 topLevel.erase(it++);
421 if (!
error.empty()) {
422 throw std::invalid_argument(
error);
426 throw std::invalid_argument(
"Unable to add group as name matches its members");
IPeaksWorkspace_sptr workspace
GroupWorkspaces notification is send when a group is updated by adding or removing members.
std::shared_ptr< const WorkspaceGroup > getWorkspaceGroup() const
Returns the workspace pointer cast to WorkspaceGroup.
GroupUpdatedNotification(const std::string &name)
Constructor.
const std::string uniqueName(const int n=5, const std::string &prefix="", const std::string &suffix="")
Random generated unique workspace name.
const std::string isValid(const std::string &name) const
Is the given name a valid name for an object in the ADS.
AnalysisDataServiceImpl()
Constructor.
void setIllegalCharacterList(const std::string &)
Set the list of illegal characters.
std::string m_illegalChars
The string of illegal characters.
const std::string & illegalCharacters() const
Return the list of illegal characters as one string.
virtual void rename(const std::string &oldName, const std::string &newName)
Overridden rename member to attach the new name to the workspace when a workspace object is renamed.
void add(const std::string &name, const std::shared_ptr< API::Workspace > &workspace) override
Overridden add member to attach the name to the workspace when a workspace object is added to the ser...
std::map< std::string, Workspace_sptr > topLevelItems() const
Return a lookup of the top level items.
void sortGroupByName(const std::string &groupName)
Sort members by Workspace name.
void deepRemoveGroup(const std::string &name)
Remove a workspace group and all its members from the ADS.
static char getRandomLowercaseLetter()
random lowercase letter used for generating workspace name in unique_name and unique_hidden_name
void addOrReplace(const std::string &name, const std::shared_ptr< API::Workspace > &workspace) override
Overridden addOrReplace member to attach the name to the workspace when a workspace object is added t...
void addToGroup(const std::string &groupName, const std::string &wsName)
Add a workspace to a group.
void verifyName(const std::string &name, const std::shared_ptr< API::WorkspaceGroup > &workspace)
Checks the name is valid, throwing if not.
std::vector< Workspace_sptr > retrieveWorkspaces(const std::vector< std::string > &names, bool unrollGroups=false) const
Given a list of names retrieve the corresponding workspace handles.
virtual Workspace_sptr remove(const std::string &name)
Overridden remove member to delete its name held by the workspace itself.
const std::string uniqueHiddenName()
Random generated unique hidden workspace name.
void removeFromGroup(const std::string &groupName, const std::string &wsName)
Remove a workspace from a group but not from the ADS.
Base Workspace Abstract Class.
DataService stores instances of a given type.
virtual void add(const std::string &name, const std::shared_ptr< T > &Tobject)
Add an object to the service.
bool doesExist(const std::string &name) const
Check to see if a data object exists in the store.
void rename(const std::string &oldName, const std::string &newName)
Rename an object within the service.
std::vector< std::string > getObjectNames(DataServiceSort sortState=DataServiceSort::Unsorted, DataServiceHidden hiddenState=DataServiceHidden::Auto, const std::string &contain="") const
Returns a vector of strings containing all object names in the ADS.
std::shared_ptr< API::Workspace > retrieve(const std::string &name) const
Get a shared pointer to a stored data object.
virtual void addOrReplace(const std::string &name, const std::shared_ptr< T > &Tobject)
Add or replace an object to the service.
Poco::NotificationCenter notificationCenter
Sends notifications to observers.
void remove(const std::string &name)
Remove an object from the service.
Exception for when an item is not found in a collection.
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Mantid::Kernel::SingletonHolder< AnalysisDataServiceImpl > AnalysisDataService
Helper class which provides the Collimation Length for SANS instruments.
std::string to_string(const wide_integer< Bits, Signed > &n)