Skip to content

Commit

Permalink
Moved deep group removing to the ADS class. Re #7253.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Jun 13, 2013
1 parent 3aec29e commit 2b0087c
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 23 deletions.
6 changes: 6 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/AnalysisDataService.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ class DLLExport AnalysisDataServiceImpl : public Kernel::DataService<API::Worksp
return boost::dynamic_pointer_cast<WSTYPE>(workspace);
}

/** @name Methods to work with workspace groups */
//@{

void deepRemoveGroup(const std::string& name);

//@}

private:
/// Checks the name is valid, throwing if not
Expand Down
2 changes: 0 additions & 2 deletions Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ class MANTID_API_DLL WorkspaceGroup : public Workspace
void remove(const std::string& name);
/// Remove all names from the group but do not touch the ADS
void removeAll();
/// Remove all names from the group and also from the ADS
void deepRemoveAll();
/// This method returns true if the group is empty (no member workspace)
bool isEmpty() const;
bool areNamesSimilar() const;
Expand Down
29 changes: 29 additions & 0 deletions Code/Mantid/Framework/API/src/AnalysisDataService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,35 @@ namespace Mantid
ws->setName( newName );
}

/**
* Remove a workspace group and all its members from the ADS.
* @param name :: A group to remove.
*/
void AnalysisDataServiceImpl::deepRemoveGroup(const std::string &name)
{
WorkspaceGroup_sptr group = retrieveWS<WorkspaceGroup>( name );
if ( !group )
{
throw std::runtime_error("Workspace " + name + " is not a workspace group.");
}
group->observeADSNotifications( false );
for(size_t i = 0; i < group->size(); ++i)
{
auto ws = group->getItem(i);
WorkspaceGroup_sptr gws = boost::dynamic_pointer_cast<WorkspaceGroup>( ws );
if ( gws )
{
// if a member is a group remove its items as well
deepRemoveGroup( gws->name() );
}
else
{
remove( ws->name() );
}
}
remove( name );
}

//-------------------------------------------------------------------------
// Private methods
//-------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/API/src/FrameworkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ bool FrameworkManagerImpl::deleteWorkspace(const std::string& wsName)
if(ws_grpsptr)
{
// selected workspace is a group workspace
ws_grpsptr->deepRemoveAll();
AnalysisDataService::Instance().deepRemoveGroup( wsName );
}
// Make sure we drop the references so the memory will get freed when we expect it to
ws_sptr.reset();
Expand Down
13 changes: 0 additions & 13 deletions Code/Mantid/Framework/API/src/WorkspaceGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,6 @@ void WorkspaceGroup::remove(const std::string& wsName)
updated();
}

/// Removes all members of the group from the group AND from the AnalysisDataService
void WorkspaceGroup::deepRemoveAll()
{
Poco::Mutex::ScopedLock _lock(m_mutex);
if( m_observingADS ) AnalysisDataService::Instance().notificationCenter.removeObserver(m_deleteObserver);
while (!m_workspaces.empty())
{
AnalysisDataService::Instance().remove(m_workspaces.back()->name());
m_workspaces.pop_back();
}
if( m_observingADS ) AnalysisDataService::Instance().notificationCenter.addObserver(m_deleteObserver);
}

/// Print the names of all the workspaces in this group to the logger (at debug level)
void WorkspaceGroup::print() const
{
Expand Down
44 changes: 44 additions & 0 deletions Code/Mantid/Framework/API/test/AnalysisDataServiceTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,27 @@ class AnalysisDataServiceTest : public CxxTest::TestSuite
ConfigService::Instance().setString("MantidOptions.InvisibleWorkspaces","0");
}

void test_deepRemoveGroup()
{
addToADS("some_workspace");
auto group = addGroupToADS("group");
TS_ASSERT_EQUALS( ads.size(), 4);

// name doesn't exist
TS_ASSERT_THROWS( ads.deepRemoveGroup("abc"), std::runtime_error );
// workspace isn't a group
TS_ASSERT_THROWS( ads.deepRemoveGroup("group_1"), std::runtime_error );
TS_ASSERT_THROWS_NOTHING( ads.deepRemoveGroup("group") );
TS_ASSERT_EQUALS( ads.size(), 1);

// check a group containing another group
group = addGroupWithGroupToADS("group");
TS_ASSERT_EQUALS( ads.size(), 6);
TS_ASSERT_THROWS_NOTHING( ads.deepRemoveGroup("group") );
TS_ASSERT_EQUALS( ads.size(), 1);
ads.clear();
}

private:

/// If replace=true then usea addOrReplace
Expand Down Expand Up @@ -373,6 +394,29 @@ class AnalysisDataServiceTest : public CxxTest::TestSuite
return space;
}

/// Add a group with 2 simple workspaces to the ADS
Workspace_sptr addGroupToADS(const std::string & name)
{
WorkspaceGroup_sptr group( new WorkspaceGroup );
group->addWorkspace( MockWorkspace_sptr(new MockWorkspace) );
group->addWorkspace( MockWorkspace_sptr(new MockWorkspace) );
ads.add(name, group);
return group;
}

/// Add a group with 1 simple workspace and 1 group with 2 simple ws to the ADS
Workspace_sptr addGroupWithGroupToADS(const std::string & name)
{
WorkspaceGroup_sptr group( new WorkspaceGroup );
group->addWorkspace( MockWorkspace_sptr(new MockWorkspace) );
WorkspaceGroup_sptr group1( new WorkspaceGroup );
group1->addWorkspace( MockWorkspace_sptr(new MockWorkspace) );
group1->addWorkspace( MockWorkspace_sptr(new MockWorkspace) );
group->addWorkspace( group1 );
ads.add(name, group);
return group;
}

/// Add or replace the given name
void addOrReplaceToADS(const std::string & name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ class CheckWorkspacesMatchTest : public CxxTest::TestSuite

void test_Input_With_Two_Groups_When_Single_Item_Checking_Is_Disabled()
{
Mantid::API::AnalysisDataService::Instance().clear();
// Create a group
const std::string groupOneName("TestGroupOne");
WorkspaceGroup_sptr groupOne = WorkspaceCreationHelper::CreateWorkspaceGroup(2, 2, 2, groupOneName);
Expand Down Expand Up @@ -804,9 +805,9 @@ class CheckWorkspacesMatchTest : public CxxTest::TestSuite

void cleanupGroup(const WorkspaceGroup_sptr group)
{
group->deepRemoveAll();
//group->deepRemoveAll();
const std::string name = group->getName();
Mantid::API::AnalysisDataService::Instance().remove(name);
Mantid::API::AnalysisDataService::Instance().deepRemoveGroup(name);
}

private:
Expand Down
3 changes: 1 addition & 2 deletions Code/Mantid/Framework/Algorithms/test/ChopDataTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ class ChopDataTest : public CxxTest::TestSuite

// Cleanup
AnalysisDataService::Instance().remove("chopdatatest_input");
wsgroup->deepRemoveAll();
AnalysisDataService::Instance().remove("chopdatatest_output");
AnalysisDataService::Instance().deepRemoveGroup("chopdatatest_output");
}

private:
Expand Down
7 changes: 5 additions & 2 deletions Code/Mantid/Framework/DataHandling/src/LoadSassena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Dataset '''fqt''' is split into two workspaces, one for the real part and the ot
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/Unit.h"
Expand Down Expand Up @@ -338,10 +339,12 @@ void LoadSassena::exec()
//auto gws=boost::dynamic_pointer_cast<API::WorkspaceGroup>(getProperty("OutputWorkspace"));
//API::WorkspaceGroup_sptr gws=getProperty("OutputWorkspace");
API::Workspace_sptr ows=getProperty("OutputWorkspace");

API::WorkspaceGroup_sptr gws=boost::dynamic_pointer_cast<API::WorkspaceGroup>(ows);
if(gws)
if(gws && API::AnalysisDataService::Instance().doesExist( gws->name() ) )
{
gws->deepRemoveAll(); // remove workspace members
//gws->deepRemoveAll(); // remove workspace members
API::AnalysisDataService::Instance().deepRemoveGroup( gws->name() );
}
else
{
Expand Down
1 change: 0 additions & 1 deletion Code/Mantid/MantidQt/CustomInterfaces/src/MuonAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,6 @@ void MuonAnalysis::getDeadTimeFromFile(const QString & fileName)
QMessageBox::information(this, "Mantid - Muon Analysis", "This kind of workspace is not compatible with applying dead times");
return;
}
deadTimeTables->deepRemoveAll();
Mantid::API::AnalysisDataService::Instance().remove("tempMuonDeadTime123qwe");
}
}
Expand Down

0 comments on commit 2b0087c

Please sign in to comment.