From 83ecd5ed5d71c46c75762e02ad22dfb2f92a62ef Mon Sep 17 00:00:00 2001 From: Jose Borreguero <borreguero@gmail.com> Date: Mon, 2 May 2016 16:29:41 -0400 Subject: [PATCH] Refs #16037 Sorting method created --- .../API/inc/MantidAPI/AnalysisDataService.h | 1 + Framework/API/inc/MantidAPI/WorkspaceGroup.h | 7 +++++++ Framework/API/src/AnalysisDataService.cpp | 14 +++++++++++++ Framework/API/src/WorkspaceGroup.cpp | 12 +++++++++++ Framework/API/test/WorkspaceGroupTest.h | 20 +++++++++++++++++++ .../mantid/api/src/Exports/WorkspaceGroup.cpp | 2 ++ .../python/mantid/api/WorkspaceGroupTest.py | 13 ++++++++++++ docs/source/release/v3.7.0/framework.rst | 8 ++++++++ 8 files changed, 77 insertions(+) diff --git a/Framework/API/inc/MantidAPI/AnalysisDataService.h b/Framework/API/inc/MantidAPI/AnalysisDataService.h index f71b192afbb..b99dfb0f5dd 100644 --- a/Framework/API/inc/MantidAPI/AnalysisDataService.h +++ b/Framework/API/inc/MantidAPI/AnalysisDataService.h @@ -150,6 +150,7 @@ public: /** @name Methods to work with workspace groups */ //@{ + void sortGroupByName(const std::string &groupName); void addToGroup(const std::string &groupName, const std::string &wsName); void deepRemoveGroup(const std::string &name); void removeFromGroup(const std::string &groupName, const std::string &wsName); diff --git a/Framework/API/inc/MantidAPI/WorkspaceGroup.h b/Framework/API/inc/MantidAPI/WorkspaceGroup.h index 74f107a4456..235df052054 100644 --- a/Framework/API/inc/MantidAPI/WorkspaceGroup.h +++ b/Framework/API/inc/MantidAPI/WorkspaceGroup.h @@ -62,6 +62,8 @@ public: /// The collection itself is considered to take up no space size_t getMemorySize() const override { return 0; } + /// Sort the internal data structure according to member name + void sortMembersByName(); /// Adds a workspace to the group. void addWorkspace(Workspace_sptr workspace); /// Return the number of entries within the group @@ -89,6 +91,11 @@ public: /// @name Wrapped ADS calls //@{ + /// Invokes the ADS to sort group members by orkspace name + void sortByName() { + AnalysisDataService::Instance().sortGroupByName(this->name()); + } + /// Adds a workspace to the group. void add(const std::string &wsName) { AnalysisDataService::Instance().addToGroup(this->name(), wsName); diff --git a/Framework/API/src/AnalysisDataService.cpp b/Framework/API/src/AnalysisDataService.cpp index b33377998f7..44f38d03114 100644 --- a/Framework/API/src/AnalysisDataService.cpp +++ b/Framework/API/src/AnalysisDataService.cpp @@ -163,6 +163,20 @@ void AnalysisDataServiceImpl::remove(const std::string &name) { } } +/** + * Sort members by Workspace name. The group must be in the ADS. + * @param groupName :: A group name. + */ +void AnalysisDataServiceImpl::sortGroupByName(const std::string &groupName) { + WorkspaceGroup_sptr group = retrieveWS<WorkspaceGroup>(groupName); + if (!group) { + throw std::runtime_error("Workspace " + groupName + + " is not a workspace group."); + } + group->sortMembersByName(); + notificationCenter.postNotification(new GroupUpdatedNotification(groupName)); +} + /** * Add a workspace to a group. The group and the workspace must be in the ADS. * @param groupName :: A group name. diff --git a/Framework/API/src/WorkspaceGroup.cpp b/Framework/API/src/WorkspaceGroup.cpp index 86d7afd2c04..29f7e7475a5 100644 --- a/Framework/API/src/WorkspaceGroup.cpp +++ b/Framework/API/src/WorkspaceGroup.cpp @@ -85,6 +85,18 @@ bool WorkspaceGroup::isInChildGroup(const Workspace &workspaceToCheck) const { return false; } +/** + * Sort members by Workspace name + */ +void WorkspaceGroup::sortMembersByName(){ + if( this->size()==0){ + return; + } + std::sort(m_workspaces.begin(), m_workspaces.end(), + [](const Workspace_sptr &w1, const Workspace_sptr &w2) { + return (w1->name() < w2->name());} ); +} + /** * 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 diff --git a/Framework/API/test/WorkspaceGroupTest.h b/Framework/API/test/WorkspaceGroupTest.h index 54abf1ff38a..ca2e5e5b4b2 100644 --- a/Framework/API/test/WorkspaceGroupTest.h +++ b/Framework/API/test/WorkspaceGroupTest.h @@ -84,6 +84,7 @@ private: } public: + void test_toString_Produces_Expected_String() { WorkspaceGroup_sptr group = makeGroup(); @@ -94,6 +95,25 @@ public: TS_ASSERT_EQUALS(expected, group->toString()); } + void test_sortByName() { + WorkspaceGroup_sptr group = makeGroup(); + AnalysisDataService::Instance().rename("ws0", "ws3"); + AnalysisDataService::Instance().sortGroupByName("group"); + const std::string expected = "WorkspaceGroup\n" + " -- ws1\n" + " -- ws2\n" + " -- ws3\n"; + TS_ASSERT_EQUALS(expected, group->toString()); + AnalysisDataService::Instance().rename("ws1", "ws5"); + const std::string expected2 = "WorkspaceGroup\n" + " -- ws2\n" + " -- ws3\n" + " -- ws5\n"; + group->sortMembersByName(); + TS_ASSERT_EQUALS(expected2, group->toString()); + AnalysisDataService::Instance().clear(); + } + void test_add() { WorkspaceGroup_sptr group = makeGroup(); TS_ASSERT_EQUALS(group->size(), 3); diff --git a/Framework/PythonInterface/mantid/api/src/Exports/WorkspaceGroup.cpp b/Framework/PythonInterface/mantid/api/src/Exports/WorkspaceGroup.cpp index 8c15d0331d1..bfc39d40ff7 100644 --- a/Framework/PythonInterface/mantid/api/src/Exports/WorkspaceGroup.cpp +++ b/Framework/PythonInterface/mantid/api/src/Exports/WorkspaceGroup.cpp @@ -21,6 +21,8 @@ void export_WorkspaceGroup() { WorkspaceGroup::contains, (arg("self"), arg("workspace")), "Returns true if the given name is in the group") + .def("sortByName", &WorkspaceGroup::sortByName, (arg("self")), + "Sort members by name") .def("add", &WorkspaceGroup::add, (arg("self"), arg("workspace_name")), "Add a name to the group") .def("size", &WorkspaceGroup::size, arg("self"), diff --git a/Framework/PythonInterface/test/python/mantid/api/WorkspaceGroupTest.py b/Framework/PythonInterface/test/python/mantid/api/WorkspaceGroupTest.py index 92af5872f62..11cc951ff06 100644 --- a/Framework/PythonInterface/test/python/mantid/api/WorkspaceGroupTest.py +++ b/Framework/PythonInterface/test/python/mantid/api/WorkspaceGroupTest.py @@ -87,5 +87,18 @@ class WorkspaceGroupTest(unittest.TestCase): mtd.remove('grouped_1') mtd.remove('grouped_2') + def test_sortByName(self): + run_algorithm('CreateSingleValuedWorkspace', OutputWorkspace="w1") + run_algorithm('CreateSingleValuedWorkspace', OutputWorkspace="w4") + run_algorithm('GroupWorkspaces',InputWorkspaces='w4,w1', + OutputWorkspace='group') + group = mtd['group'] + names = ' '.join(list(group.getNames())) + self.assertTrue("w4 w1"==names) + group.sortByName() + names = ' '.join(list(group.getNames())) + self.assertTrue("w1 w4"==names) + + if __name__ == '__main__': unittest.main() diff --git a/docs/source/release/v3.7.0/framework.rst b/docs/source/release/v3.7.0/framework.rst index f60e3bf48df..a06cdaecf0f 100644 --- a/docs/source/release/v3.7.0/framework.rst +++ b/docs/source/release/v3.7.0/framework.rst @@ -5,6 +5,14 @@ Framework Changes .. contents:: Table of Contents :local: +API +--- + +Improved +######## + +- A sorting members by name method was added to :ref:`WorkspaceGroup <WorkspaceGroup>`. + Algorithms ---------- -- GitLab