Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add DeleteWorkspaces algorithm, use it in GUI
re #17729
- Loading branch information
1 parent
f413189
commit 9953f7c
Showing
6 changed files
with
269 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
Framework/Algorithms/inc/MantidAlgorithms/DeleteWorkspaces.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#ifndef MANTID_ALGORITHMS_DELETEWORKSPACES_H_ | ||
#define MANTID_ALGORITHMS_DELETEWORKSPACES_H_ | ||
|
||
//---------------------------------------------------------------------- | ||
// Includes | ||
//---------------------------------------------------------------------- | ||
#include "MantidAPI/Algorithm.h" | ||
|
||
namespace Mantid { | ||
namespace Algorithms { | ||
/** | ||
A simple algorithm to remove multiple workspaces from the ADS. | ||
@author Nick Draper, Tessella plc | ||
@date 2017-02-17 | ||
Copyright © 2017 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge | ||
National Laboratory & European Spallation Source | ||
This file is part of Mantid. | ||
Mantid is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
Mantid is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
File change history is stored at: <https://github.com/mantidproject/mantid> | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
class DLLExport DeleteWorkspaces : public API::Algorithm { | ||
public: | ||
/// Algorithm's name | ||
const std::string name() const override { return "DeleteWorkspaces"; } | ||
/// Summary of algorithms purpose | ||
const std::string summary() const override { | ||
return "Removes a list of workspaces from memory."; | ||
} | ||
|
||
/// Algorithm's category for identification | ||
const std::string category() const override { return "Utility\\Workspaces"; } | ||
/// Algorithm's version for identification overriding a virtual method | ||
int version() const override { return 1; } | ||
|
||
private: | ||
/// Overridden init | ||
void init() override; | ||
/// Overridden exec | ||
void exec() override; | ||
|
||
|
||
}; | ||
|
||
} // namespace Algorithm | ||
} // namespace Mantid | ||
|
||
#endif // MANTID_ALGORITHMS_DELETEWORKSPACES_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "MantidAlgorithms/DeleteWorkspaces.h" | ||
#include "MantidAPI/AnalysisDataService.h" | ||
#include "MantidAPI/ADSValidator.h" | ||
#include "MantidKernel/ArrayProperty.h" | ||
|
||
namespace Mantid { | ||
namespace Algorithms { | ||
|
||
// Register the algorithm | ||
DECLARE_ALGORITHM(DeleteWorkspaces) | ||
|
||
/// Initialize the algorithm properties | ||
void DeleteWorkspaces::init() { | ||
declareProperty(Kernel::make_unique<Kernel::ArrayProperty<std::string>>( | ||
"WorkspaceList", boost::make_shared<API::ADSValidator>()), | ||
"A list of the workspaces to delete."); | ||
} | ||
|
||
/// Execute the algorithm | ||
void DeleteWorkspaces::exec() { | ||
using API::AnalysisDataService; | ||
using API::AnalysisDataServiceImpl; | ||
AnalysisDataServiceImpl &dataStore = AnalysisDataService::Instance(); | ||
const std::vector<std::string> wsNames = getProperty("WorkspaceList"); | ||
|
||
// Set up progress reporting | ||
API::Progress prog(this, 0.0, 1.0, wsNames.size()); | ||
|
||
for (auto wsName : wsNames) { | ||
//run delete workspace as a child algorithm | ||
auto deleteAlg = createChildAlgorithm("DeleteWorkspace", -1, -1, false); | ||
deleteAlg->initialize(); | ||
|
||
deleteAlg->setPropertyValue("Workspace", wsName); | ||
bool success = deleteAlg->execute(); | ||
if (!deleteAlg->isExecuted() || !success) { | ||
g_log.error() << "Failed to delete wsName. \n"; | ||
} | ||
prog.report(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
//------------------------------------------------------------------------------ | ||
// Includes | ||
//------------------------------------------------------------------------------ | ||
#include "MantidAlgorithms/DeleteWorkspaces.h" | ||
#include "MantidTestHelpers/WorkspaceCreationHelper.h" | ||
#include "MantidAPI/WorkspaceGroup.h" | ||
|
||
#include <cxxtest/TestSuite.h> | ||
|
||
class DeleteWorkspacesTest : public CxxTest::TestSuite { | ||
|
||
public: | ||
void test_That_An_Existing_Workspace_Is_Deleted_After_Execution() { | ||
using namespace Mantid::API; | ||
using namespace Mantid::DataObjects; | ||
|
||
// Need a test workspace registered within the ADS | ||
const int yLength = 20; | ||
AnalysisDataServiceImpl &dataStore = AnalysisDataService::Instance(); | ||
const size_t storeSizeAtStart(dataStore.size()); | ||
const std::string testName1 = "DeleteWorkspaces_testWS1"; | ||
const std::string testName2 = "DeleteWorkspaces_testWS2"; | ||
const std::string testName3 = "DeleteWorkspaces_testWS3"; | ||
createAndStoreWorspace(testName1); | ||
createAndStoreWorspace(testName2); | ||
createAndStoreWorspace(testName3, yLength); | ||
TS_ASSERT_EQUALS(dataStore.size(), storeSizeAtStart + 3); | ||
|
||
Mantid::Algorithms::DeleteWorkspaces alg; | ||
alg.initialize(); | ||
alg.setPropertyValue("WorkspaceList", testName1 + ", " + testName2); | ||
alg.setRethrows(true); | ||
TS_ASSERT_THROWS_NOTHING(alg.execute()); | ||
TS_ASSERT(alg.isExecuted()); | ||
|
||
TS_ASSERT_EQUALS(dataStore.size(), storeSizeAtStart + 1); | ||
// Check that what is left is correct | ||
MatrixWorkspace_sptr wsRemain = boost::dynamic_pointer_cast<MatrixWorkspace>( | ||
dataStore.retrieve(testName3)); | ||
TS_ASSERT(wsRemain); | ||
if (!wsRemain) | ||
TS_FAIL("Unable to retrieve remaining workspace."); | ||
|
||
TS_ASSERT_EQUALS(wsRemain->getNumberHistograms(), yLength); | ||
// Tidy up after test | ||
dataStore.remove(testName3); | ||
} | ||
|
||
void test_deleting_group_deletes_its_members() { | ||
using namespace Mantid::API; | ||
using namespace Mantid::DataObjects; | ||
|
||
// Need a test workspace registered within the ADS | ||
AnalysisDataServiceImpl &dataStore = AnalysisDataService::Instance(); | ||
dataStore.clear(); | ||
|
||
const std::string testName1 = "DeleteWorkspaces_testWS1"; | ||
const std::string testName2 = "DeleteWorkspaces_testWS2"; | ||
|
||
createAndStoreWorspace(testName1); | ||
createAndStoreWorspace(testName2); | ||
|
||
auto group = WorkspaceGroup_sptr(new WorkspaceGroup); | ||
dataStore.add("group", group); | ||
group->add(testName1); | ||
group->add(testName2); | ||
|
||
TS_ASSERT_EQUALS(dataStore.size(), 3); | ||
|
||
Mantid::Algorithms::DeleteWorkspaces alg; | ||
alg.initialize(); | ||
alg.setPropertyValue("WorkspaceList", "group"); | ||
alg.setRethrows(true); | ||
TS_ASSERT_THROWS_NOTHING(alg.execute()); | ||
TS_ASSERT(alg.isExecuted()); | ||
|
||
TS_ASSERT_EQUALS(dataStore.size(), 0); | ||
|
||
dataStore.clear(); | ||
} | ||
|
||
void createAndStoreWorspace(std::string name, int ylength = 10) { | ||
using namespace Mantid::API; | ||
using namespace Mantid::DataObjects; | ||
|
||
// create a test workspace registered within the ADS | ||
Workspace2D_sptr testWS1 = | ||
WorkspaceCreationHelper::create2DWorkspace(ylength, 10); | ||
AnalysisDataServiceImpl &dataStore = AnalysisDataService::Instance(); | ||
dataStore.add(name, testWS1); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
.. algorithm:: | ||
|
||
.. summary:: | ||
|
||
.. alias:: | ||
|
||
.. properties:: | ||
|
||
Description | ||
----------- | ||
|
||
If workspaces in the WorkspaceList exist then it is removed from Mantid. | ||
|
||
Usage | ||
----- | ||
|
||
**Example - Delete using a list of objects** | ||
|
||
.. testcode:: | ||
|
||
# A small test workspace, with sample_ws as the handle | ||
sample_ws = CreateSingleValuedWorkspace(5.0) | ||
sample_ws2 = CreateSingleValuedWorkspace(5.0) | ||
|
||
DeleteWorkspaces([sample_ws,sample_ws2) | ||
|
||
print "sample_ws exists in mantid:",("sample_ws" in mtd) | ||
print "sample_ws2 exists in mantid:",("sample_ws2" in mtd) | ||
|
||
Output: | ||
|
||
.. testoutput:: | ||
|
||
sample_ws exists in mantid: False | ||
sample_ws2 exists in mantid: False | ||
|
||
**Example - Delete using a string list** | ||
|
||
.. testcode:: | ||
|
||
# A small test workspace, with sample_ws as the handle | ||
CreateSingleValuedWorkspace(5.0, OutputWorkspace="single_value") | ||
CreateSingleValuedWorkspace(5.0, OutputWorkspace="single_value2") | ||
|
||
DeleteWorkspaces("single_value, single_value2") | ||
|
||
print "single_value exists in mantid:",("single_value" in mtd) | ||
print "single_value2 exists in mantid:",("single_value2" in mtd) | ||
|
||
Output: | ||
|
||
.. testoutput:: | ||
|
||
single_value exists in mantid: False | ||
single_value2 exists in mantid: False | ||
|
||
.. categories:: | ||
|
||
.. sourcelink:: |