diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h b/Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h index 2c6f0d192d7122b5dda8a5e08d11341d180dfd81..c09915d8c3884bec033fab2ddf63f49f489d88db 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h @@ -180,6 +180,11 @@ public: virtual const std::string categorySeparator() const {return ";";} /// function to return any aliases to the algorithm; A default implementation is provided virtual const std::string alias() const {return "";} + + const std::string workspaceMethodName() const; + const std::vector<std::string> workspaceMethodOn() const; + const std::string workspaceMethodInputProperty() const; + /// Algorithm ID. Unmanaged algorithms return 0 (or NULL?) values. Managed ones have non-zero. AlgorithmID getAlgorithmID()const{return m_algorithmID;} @@ -267,6 +272,9 @@ protected: /// Method defining summary, optional virtual void initDocs() {}; + /// Returns a semi-colon separated list of workspace types to attach this algorithm + virtual const std::string workspaceMethodOnTypes() const { return ""; } + void cacheWorkspaceProperties(); friend class AlgorithmProxy; diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmProxy.h b/Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmProxy.h index d7895483f326fe92f90e3f005c6f5f9550496178..528133cd96b45b6065d244e7282daaa63ff4455d 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmProxy.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmProxy.h @@ -101,6 +101,10 @@ namespace Mantid void enableHistoryRecordingForChild(const bool) {}; void setRethrows(const bool rethrow); + const std::string workspaceMethodName() const; + const std::vector<std::string> workspaceMethodOn() const; + const std::string workspaceMethodInputProperty() const; + /** @name PropertyManager methods */ //@{ /// Set the property value diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/IAlgorithm.h b/Code/Mantid/Framework/API/inc/MantidAPI/IAlgorithm.h index f8adc0531933d3b1d541542c673087bdd3a30deb..3959aacc1e37ef6ad9f38778f90202514014af8d 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/IAlgorithm.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/IAlgorithm.h @@ -79,6 +79,16 @@ public: /// function to return any aliases of the algorithm. virtual const std::string alias() const = 0; + /** @name Algorithms As Methods */ + ///@{ + /// Returns a name that will be used when attached as a workspace method. Empty string indicates do not attach + virtual const std::string workspaceMethodName() const = 0; + /// Returns a set of class names that will have the method attached. Empty list indicates all types + virtual const std::vector<std::string> workspaceMethodOn() const = 0; + /// Returns the name of the input workspace property used by the calling object + virtual const std::string workspaceMethodInputProperty() const = 0; + ///@} + /// Algorithm ID. Unmanaged algorithms return 0 (or NULL?) values. Managed ones have non-zero. virtual AlgorithmID getAlgorithmID()const = 0; diff --git a/Code/Mantid/Framework/API/src/Algorithm.cpp b/Code/Mantid/Framework/API/src/Algorithm.cpp index a2afdcbfece1ce5fcad25866f96a9e4a0aae97da..eb2721317f86936bfdab5e9ad5842e37ae0d4120 100644 --- a/Code/Mantid/Framework/API/src/Algorithm.cpp +++ b/Code/Mantid/Framework/API/src/Algorithm.cpp @@ -36,6 +36,12 @@ namespace Mantid { namespace API { + namespace + { + /// Separator for workspace types in workspaceMethodOnTypes member + const std::string WORKSPACE_TYPES_SEPARATOR = ";"; + } + // Doxygen can't handle member specialization at the moment: https://bugzilla.gnome.org/show_bug.cgi?id=406027 // so we have to ignore them ///@cond @@ -213,7 +219,7 @@ namespace Mantid Poco::StringTokenizer tokenizer(category(), categorySeparator(), Poco::StringTokenizer::TOK_TRIM | Poco::StringTokenizer::TOK_IGNORE_EMPTY); Poco::StringTokenizer::Iterator h = tokenizer.begin(); - + for (; h != tokenizer.end(); ++h) { res.push_back(*h); @@ -227,6 +233,40 @@ namespace Mantid return res; } + /** + * @return A string giving the method name that should be attached to a workspace + */ + const std::string Algorithm::workspaceMethodName() const + { + return ""; + } + + /** + * + * @return A list of workspace class names that should have the workspaceMethodName attached + */ + const std::vector<std::string> Algorithm::workspaceMethodOn() const + { + Poco::StringTokenizer tokenizer(this->workspaceMethodOnTypes(), WORKSPACE_TYPES_SEPARATOR, + Poco::StringTokenizer::TOK_TRIM | Poco::StringTokenizer::TOK_IGNORE_EMPTY); + std::vector<std::string> res; + res.reserve(tokenizer.count()); + for( auto iter = tokenizer.begin(); iter != tokenizer.end(); ++iter ) + { + res.push_back(*iter); + } + + return res; + } + + /** + * @return The name of the property that the calling object will be passed to. + */ + const std::string Algorithm::workspaceMethodInputProperty() const + { + return ""; + } + //============================================================================================= //================================== Initialization =========================================== diff --git a/Code/Mantid/Framework/API/src/AlgorithmProxy.cpp b/Code/Mantid/Framework/API/src/AlgorithmProxy.cpp index 7185b13eba117d619b2089f7599d3ad7a126fba3..0a1d216aa5620cb6231d27d242ce1efdad9248c4 100644 --- a/Code/Mantid/Framework/API/src/AlgorithmProxy.cpp +++ b/Code/Mantid/Framework/API/src/AlgorithmProxy.cpp @@ -183,6 +183,33 @@ namespace Mantid if(m_alg) m_alg->setRethrows(rethrow); } + /** + * @return A string giving the method name that should be attached to a workspace + */ + const std::string AlgorithmProxy::workspaceMethodName() const + { + if(m_alg) return m_alg->workspaceMethodName(); + else return ""; + } + + /** + * @return A set of workspace class names that should have the workspaceMethodName attached + */ + const std::vector<std::string> AlgorithmProxy::workspaceMethodOn() const + { + if(m_alg) return m_alg->workspaceMethodOn(); + else return std::vector<std::string>(); + } + + /** + * @return The name of the property that the calling object will be passed to + */ + const std::string AlgorithmProxy::workspaceMethodInputProperty() const + { + if(m_alg) return m_alg->workspaceMethodInputProperty(); + else return ""; + } + /** * Override setPropertyValue * @param name The name of the property diff --git a/Code/Mantid/Framework/API/test/AlgorithmProxyTest.h b/Code/Mantid/Framework/API/test/AlgorithmProxyTest.h index 9b1f30fda3567ca607e9bca002266610055461f0..9922212e882cb34df68acdc4540a33b1bbc1f614 100644 --- a/Code/Mantid/Framework/API/test/AlgorithmProxyTest.h +++ b/Code/Mantid/Framework/API/test/AlgorithmProxyTest.h @@ -21,6 +21,9 @@ public: int version() const { return 1;} ///< Algorithm's version for identification const std::string category() const { return "ProxyCat";} ///< Algorithm's category for identification const std::string alias() const { return "Dog";} ///< Algorithm's alias + const std::string workspaceMethodName() const { return "toyalgorithm"; } + const std::string workspaceMethodOnTypes() const { return "MatrixWorkspace;ITableWorkspace"; } + const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; } void init() { @@ -192,6 +195,23 @@ public: TS_ASSERT( obs.progress ); TS_ASSERT( obs.finish ); } + + void test_WorkspaceMethodFunctionsReturnProxiedContent() + { + IAlgorithm_sptr alg = AlgorithmManager::Instance().create("ToyAlgorithmProxy"); + + TS_ASSERT_EQUALS("toyalgorithm", alg->workspaceMethodName()); + + auto types = alg->workspaceMethodOn(); + TS_ASSERT_EQUALS(2, types.size()); + if(types.size() == 2) + { + TS_ASSERT_EQUALS("MatrixWorkspace", types[0]); + TS_ASSERT_EQUALS("ITableWorkspace", types[1]); + } + TS_ASSERT_EQUALS("InputWorkspace", alg->workspaceMethodInputProperty()); + } + }; #endif /*ALGORITHMPROXYTEST_H_*/ diff --git a/Code/Mantid/Framework/API/test/AlgorithmTest.h b/Code/Mantid/Framework/API/test/AlgorithmTest.h index 943f512c2904725c0c69971d17e992599f103a42..46e04b6f9ab1fd61943b5b240bf1b8fab9caa971 100644 --- a/Code/Mantid/Framework/API/test/AlgorithmTest.h +++ b/Code/Mantid/Framework/API/test/AlgorithmTest.h @@ -86,6 +86,10 @@ public: const std::string name() const { return "StubbedWorkspaceAlgorithm2";} int version() const { return 1;} const std::string category() const { return "Cat;Leopard;Mink";} + const std::string workspaceMethodName() const { return "methodname"; } + const std::string workspaceMethodOnTypes() const { return "MatrixWorkspace;ITableWorkspace"; } + const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; } + void init() { declareProperty("PropertyA", 12); @@ -242,6 +246,30 @@ public: TS_ASSERT( alg.isExecuted() ); } + void test_WorkspaceMethodFunctionsReturnEmptyByDefault() + { + StubbedWorkspaceAlgorithm alg; + + TS_ASSERT_EQUALS("", alg.workspaceMethodName()); + TS_ASSERT_EQUALS(std::vector<std::string>(), alg.workspaceMethodOn()); + TS_ASSERT_EQUALS("", alg.workspaceMethodInputProperty()); + } + + void test_WorkspaceMethodsReturnTypesCorrectly() + { + AlgorithmWithValidateInputs alg; + + TS_ASSERT_EQUALS("methodname", alg.workspaceMethodName()); + auto types = alg.workspaceMethodOn(); + TS_ASSERT_EQUALS(2, types.size()); + if(types.size() == 2) + { + TS_ASSERT_EQUALS("MatrixWorkspace", types[0]); + TS_ASSERT_EQUALS("ITableWorkspace", types[1]); + } + TS_ASSERT_EQUALS("InputWorkspace", alg.workspaceMethodInputProperty()); + } + void testStringization() { //Set the properties so that we know what they are