diff --git a/Framework/API/inc/MantidAPI/AlgorithmFactory.h b/Framework/API/inc/MantidAPI/AlgorithmFactory.h index 6c4b4340f7521d555a294ab5bedbe94cf1f5a530..0cb94c003c6d60498784bd492a1e2d4d453c86c5 100644 --- a/Framework/API/inc/MantidAPI/AlgorithmFactory.h +++ b/Framework/API/inc/MantidAPI/AlgorithmFactory.h @@ -16,11 +16,14 @@ namespace API { /// Structure uniquely describing an algorithm with its name, category and /// version. -struct AlgorithmDescriptor { - std::string name; ///< name - std::string alias; ///< alias +struct AlgorithmKey { + std::string name; ///< Algorithm Name + int version; ///< version +}; + +struct AlgorithmDescriptor : public AlgorithmKey { std::string category; ///< category - int version; ///< version + std::string alias; ///< alias }; //---------------------------------------------------------------------- @@ -134,8 +137,7 @@ public: const std::map<std::string, bool> getCategoriesWithState() const; /// Returns a single algorithm descriptor - AlgorithmDescriptor getDescriptor(const std::string &algName, - int version = -1) const; + int getAlgLatestVersion(const std::string &algName) const; /// Returns algorithm descriptors. std::vector<AlgorithmDescriptor> diff --git a/Framework/API/src/AlgorithmFactory.cpp b/Framework/API/src/AlgorithmFactory.cpp index 155d405258d35f36400aa4f9a54ba30a6bbbe5e8..298a7b6c4c3c59cdd4668bbc5bc5f6f7afc1096a 100644 --- a/Framework/API/src/AlgorithmFactory.cpp +++ b/Framework/API/src/AlgorithmFactory.cpp @@ -35,37 +35,39 @@ AlgorithmFactoryImpl::~AlgorithmFactoryImpl() = default; */ boost::shared_ptr<Algorithm> AlgorithmFactoryImpl::create(const std::string &name, - const int &version) const { - int local_version = version; - if (version < 0) { - if (version == -1) // get latest version since not supplied - { - auto it = m_vmap.find(name); - if (!name.empty()) { - if (it == m_vmap.end()) - throw std::runtime_error("Algorithm not registered " + name); - else - local_version = it->second; - } else - throw std::runtime_error( - "Algorithm not registered (empty algorithm name)"); - } - } - try { - return this->createAlgorithm(name, local_version); - } catch (Kernel::Exception::NotFoundError &) { - auto it = m_vmap.find(name); - if (it == m_vmap.end()) - throw std::runtime_error("algorithm not registered " + name); - else { - g_log.error() << "algorithm " << name << " version " << version - << " is not registered \n"; - g_log.error() << "the latest registered version is " << it->second - << '\n'; - throw std::runtime_error("algorithm not registered " + - createName(name, local_version)); - } - } + const int &version) const { + int local_version = version; + if (version < 0) { + if (version == -1) // get latest version since not supplied + { + auto it = m_vmap.find(name); + if (!name.empty()) { + if (it == m_vmap.end()) + throw std::runtime_error("Algorithm not registered " + name); + else + local_version = it->second; + } + else + throw std::runtime_error( + "Algorithm not registered (empty algorithm name)"); + } + } + try { + return this->createAlgorithm(name, local_version); + } + catch (Kernel::Exception::NotFoundError &) { + auto it = m_vmap.find(name); + if (it == m_vmap.end()) + throw std::runtime_error("algorithm not registered " + name); + else { + g_log.error() << "algorithm " << name << " version " << version + << " is not registered \n"; + g_log.error() << "the latest registered version is " << it->second + << '\n'; + throw std::runtime_error("algorithm not registered " + + createName(name, local_version)); + } + } } /** @@ -307,27 +309,21 @@ AlgorithmFactoryImpl::getCategories(bool includeHidden) const { } /** - * Returns a single algorithm descriptor for the given algorithm name + * Returns a the latest version number of the given algorithm * - * @param algName The name of the algorithm to get a descriptor for - * @param version The version of the algorithm (default = latest) - * @return AlgorithmDescriptor object - * @throws std::runtime_error if algorithm does not exist + * @param algName The name of the algorithm to get a version for + * @param throwIfNotRegistered (Default true) Throw if the algorithm is not registered + * @return Integer of the latest algorithm version or -1 if it does not exist + * @throws std::runtime_error if algorithm does not exist and throw is true */ -AlgorithmDescriptor -AlgorithmFactoryImpl::getDescriptor(const std::string &algName, - int version) const { - AlgorithmDescriptor desc; - desc.name = algName; - - boost::shared_ptr<IAlgorithm> alg = create(algName, version); - auto categories = alg->categories(); - desc.version = alg->version(); - // Set the category if there are any - desc.category = categories.empty() ? "" : categories.back(); - desc.alias = alg->alias(); - - return desc; +int AlgorithmFactoryImpl::getAlgLatestVersion(const std::string &algName) const{ + auto it = m_vmap.find(algName); + + if (it == m_vmap.end()) { + throw std::runtime_error("Algorithm not registered " + algName); + } + + return it->second; } /** diff --git a/Framework/API/src/NotebookBuilder.cpp b/Framework/API/src/NotebookBuilder.cpp index 78a9233eabac763c694dd4c6152bc1b35daa9d4f..cd3cd1a380c6492e44ad51e7f5a8ed78dac892da 100644 --- a/Framework/API/src/NotebookBuilder.cpp +++ b/Framework/API/src/NotebookBuilder.cpp @@ -119,24 +119,17 @@ NotebookBuilder::buildAlgorithmString(AlgorithmHistory_const_sptr algHistory) { // Three cases, we can either specify the version of every algorithm... if (m_versionSpecificity == "all") { - properties << "Version=" << algHistory->version() << ", "; - } else if (m_versionSpecificity == "old") { - //...or only specify algorithm versions when they're not the newest version - bool oldVersion = false; - const auto &algName = algHistory->name(); - - auto descriptor = AlgorithmFactory::Instance().getDescriptor(algName); - - // If a newer version of this algorithm exists, then this must be an old - // version. - if (descriptor.name == algHistory->name() && - descriptor.version > algHistory->version()) { - oldVersion = true; - } - - if (oldVersion) { - properties << "Version=" << algHistory->version() << ", "; - } + properties << "Version=" << algHistory->version() << ", "; + } + else if (m_versionSpecificity == "old") { + //...or only specify algorithm versions when they're not the newest version + const auto &algName = algHistory->name(); + int latestVersion = AlgorithmFactory::Instance().getAlgLatestVersion(algName); + // If a newer version of this algorithm exists, then this must be an old + // version. + if (latestVersion > algHistory->version()) { + properties << "Version=" << algHistory->version() << ", "; + } } // Third case is we never specify the version, so do nothing. diff --git a/Framework/API/src/ScriptBuilder.cpp b/Framework/API/src/ScriptBuilder.cpp index 59317944869d293c0e11f1128297c7e36f8a83e9..e1f3e59a9cd04690e45b3449d85ca647c78ed07a 100644 --- a/Framework/API/src/ScriptBuilder.cpp +++ b/Framework/API/src/ScriptBuilder.cpp @@ -196,19 +196,11 @@ ScriptBuilder::buildAlgorithmString(AlgorithmHistory_const_sptr algHistory) { properties << "Version=" << algHistory->version() << ", "; } else if (m_versionSpecificity == "old") { //...or only specify algorithm versions when they're not the newest version - bool oldVersion = false; const auto &algName = algHistory->name(); - - auto descriptor = AlgorithmFactory::Instance().getDescriptor(algName); - + int latestVersion = AlgorithmFactory::Instance().getAlgLatestVersion(algName); // If a newer version of this algorithm exists, then this must be an old // version. - if (descriptor.name == algHistory->name() && - descriptor.version > algHistory->version()) { - oldVersion = true; - } - - if (oldVersion) { + if (latestVersion > algHistory->version()) { properties << "Version=" << algHistory->version() << ", "; } } diff --git a/Framework/API/test/AlgorithmFactoryTest.h b/Framework/API/test/AlgorithmFactoryTest.h index 4931ede3153e63f121996e348185d512b0b6f4eb..6d7b596099f92ead167b7ae3289baff969b889ac 100644 --- a/Framework/API/test/AlgorithmFactoryTest.h +++ b/Framework/API/test/AlgorithmFactoryTest.h @@ -25,6 +25,10 @@ public: auto &algFactory = AlgorithmFactory::Instance(); + // Ensure the algorithm factory does not already have this + algFactory.unsubscribe("ToyAlgorithm", 1); + algFactory.unsubscribe("ToyAlgorithm", 2); + // get the number of algorithms it already has std::vector<std::string> keys = algFactory.getKeys(); size_t noOfAlgs = keys.size(); @@ -183,24 +187,22 @@ public: TS_ASSERT_EQUALS(noOfAlgs - 1, descriptors.size()); } - void testGetDescriptor() { + void testGetLatestVersion() { auto &algFactory = AlgorithmFactory::Instance(); const std::string algName = "ToyAlgorithm"; algFactory.subscribe<ToyAlgorithm>(); - AlgorithmDescriptor descriptor; - - TS_ASSERT_THROWS_NOTHING(descriptor = algFactory.getDescriptor(algName)); + algFactory.subscribe<ToyAlgorithmTwo>(); - bool AlgCorrect = ("Cat" == descriptor.category) && - (algName == descriptor.name) && - ("Dog" == descriptor.alias) && (1 == descriptor.version); + int version = -1; + TS_ASSERT_THROWS_NOTHING(version = algFactory.getAlgLatestVersion(algName)); - TS_ASSERT(AlgCorrect); + TS_ASSERT_EQUALS(version, 2); - algFactory.unsubscribe("ToyAlgorithm", 1); + algFactory.unsubscribe(algName, 1); + algFactory.unsubscribe(algName, 2); - TS_ASSERT_THROWS(algFactory.getDescriptor(algName), std::runtime_error); + TS_ASSERT_THROWS(algFactory.getAlgLatestVersion(algName), std::runtime_error); } void testGetCategories() {