Skip to content

Commit

Permalink
Started changes to WorkspaceGroup with simple use cases.
Browse files Browse the repository at this point in the history
Re #6199.
  • Loading branch information
mantid-roman committed May 10, 2013
1 parent 6f503cd commit e37fdb5
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 75 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/AnalysisDataService.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class DLLExport AnalysisDataServiceImpl : public Kernel::DataService<API::Worksp
virtual void addOrReplace( const std::string& name, const boost::shared_ptr<API::Workspace>& workspace);
/// Overridden rename member to attach the new name to the workspace when a workspace object is renamed
virtual void rename( const std::string& oldName, const std::string& newName);
/// Get a shared pointer to a stored workspace
boost::shared_ptr<API::Workspace> retrieve( const std::string& name) const;

/** Retrieve a workspace and cast it to the given WSTYPE
*
Expand Down
22 changes: 13 additions & 9 deletions Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,12 @@ class MANTID_API_DLL WorkspaceGroup : public Workspace
~WorkspaceGroup();
/// Return a string ID of the class
virtual const std::string id() const { return "WorkspaceGroup"; }
/// The collection itself is considered to take up no space
virtual size_t getMemorySize() const { return 0; }
/// Turn ADS observations on/off
void observeADSNotifications(const bool observeADS);
/// The size of the group.
virtual size_t getMemorySize() const;
/// Adds a workspace to the group.
void add(const std::string& wsName);
/// Adds a workspace to the group.
void addWorkspace(Workspace_sptr workspace);
/// Does a workspace exist within the group
bool contains(const std::string & wsName) const;
/// 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> getNames() const;
/// Return the number of entries within the group
int getNumberOfEntries() const { return static_cast<int>(this->size()); }
/// Return the size of the group, so it is more like a container
Expand All @@ -81,6 +75,13 @@ class MANTID_API_DLL WorkspaceGroup : public Workspace
Workspace_sptr getItem(const size_t index) const;
/// Return the workspace by name
Workspace_sptr getItem(const std::string wsName) const;
/// Find a workspace by name
Workspace_sptr findItem(const std::string wsName) const;

/// Does a workspace exist within the group
bool contains(const std::string & wsName) const;
/// 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> getNames() const;
/// Prints the group to the screen using the logger at debug
void print() const;
/// Remove a name from the group
Expand All @@ -96,7 +97,9 @@ class MANTID_API_DLL WorkspaceGroup : public Workspace
void updated() const;
/// Inidicates that the workspace group can be treated as multiperiod.
bool isMultiperiod() const;

/// Turn ADS observations on/off
void observeADSNotifications(const bool observeADS);

private:
/// Private, unimplemented copy constructor
WorkspaceGroup(const WorkspaceGroup& ref);
Expand All @@ -110,6 +113,7 @@ class MANTID_API_DLL WorkspaceGroup : public Workspace
void workspaceReplaceHandle(Mantid::API::WorkspaceBeforeReplaceNotification_ptr notice);
/// Observer for workspace before-replace notfications
Poco::NObserver<WorkspaceGroup, Mantid::API::WorkspaceBeforeReplaceNotification> m_replaceObserver;

/// The list of workspace pointers in the group
std::vector<Workspace_sptr> m_workspaces;
/// Flag as to whether the observers have been added to the ADS
Expand Down
41 changes: 23 additions & 18 deletions Code/Mantid/Framework/API/src/AnalysisDataService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,7 @@ namespace Mantid
if ( wsName.empty() )
{
wsName = name + "_" + boost::lexical_cast<std::string>( i + 1 );
}
else if ( doesExist( wsName ) )
{// if ws is already there do nothing
wsName.clear();
}
// add member workspace if needed
if ( !wsName.empty() )
{
add( wsName, ws );
ws->setName( wsName );
}
}
}
Expand Down Expand Up @@ -118,15 +110,6 @@ namespace Mantid
{
wsName = name + "_" + boost::lexical_cast<std::string>( i + 1 );
}
else if ( doesExist( wsName ) )
{// if ws is already there do nothing
wsName.clear();
}
// add member workspace if needed
if ( !wsName.empty() )
{
addOrReplace( wsName, ws );
}
}
}

Expand All @@ -143,6 +126,28 @@ namespace Mantid
ws->setName( newName );
}

/**
* Extend the default behaviour by searching workspace groups recursively.
* @param name :: Name of the workspace.
*/
boost::shared_ptr<API::Workspace> AnalysisDataServiceImpl::retrieve(const std::string &name) const
{
std::vector<Workspace_sptr> workspaces = getObjects();
for(auto it = workspaces.begin(); it != workspaces.end(); ++it)
{
Workspace *ws = it->get();
if ( ws->name() == name ) return *it;
WorkspaceGroup* wsg = dynamic_cast<WorkspaceGroup*>(ws);
if ( wsg )
{
// look in member groups recursively
auto res = wsg->findItem(name);
if ( res ) return res;
}
}
throw Kernel::Exception::NotFoundError("Workspace",name);
}

//-------------------------------------------------------------------------
// Private methods
//-------------------------------------------------------------------------
Expand Down
39 changes: 38 additions & 1 deletion Code/Mantid/Framework/API/src/WorkspaceGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,20 @@ WorkspaceGroup::WorkspaceGroup(const bool observeADS) :

WorkspaceGroup::~WorkspaceGroup()
{
observeADSNotifications(false);
observeADSNotifications(false);
}

/**
* The size of the group is the sum of sizes of members.
*/
size_t WorkspaceGroup::getMemorySize() const
{
size_t n = 0;
for (auto it = m_workspaces.begin(); it != m_workspaces.end(); ++it)
{
n += (**it).getMemorySize();
}
return n;
}

/**
Expand Down Expand Up @@ -63,6 +76,7 @@ void WorkspaceGroup::observeADSNotifications(const bool observeADS)
void WorkspaceGroup::add(const std::string& name)
{
Workspace_sptr ws = AnalysisDataService::Instance().retrieve( name );
AnalysisDataService::Instance().remove( name );
addWorkspace( ws );
g_log.debug() << "workspacename added to group vector = " << name <<std::endl;
}
Expand Down Expand Up @@ -149,6 +163,29 @@ Workspace_sptr WorkspaceGroup::getItem(const std::string wsName) const
throw std::out_of_range("Workspace "+wsName+" not contained in the group");
}

/**
* Find a workspace by name. Search recursively in any nseted groups.
* Return an empty pointer if the workspace isn't found.
* @param wsName The name of the workspace.
*/
Workspace_sptr WorkspaceGroup::findItem(const std::string wsName) const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
for(auto it = m_workspaces.begin(); it != m_workspaces.end(); ++it)
{
Workspace *ws = it->get();
if ( ws->name() == wsName ) return *it;
WorkspaceGroup* wsg = dynamic_cast<WorkspaceGroup*>(ws);
if ( wsg )
{
// look in member groups recursively
auto res = wsg->findItem(wsName);
if ( res ) return res;
}
}
return Workspace_sptr();
}

/// Empty all the entries out of the workspace group. Does not remove the workspaces from the ADS.
void WorkspaceGroup::removeAll()
{
Expand Down

0 comments on commit e37fdb5

Please sign in to comment.