15#include <boost/algorithm/string.hpp>
24Kernel::Logger
g_log(
"AlgorithmFactory");
31 Mantid::Kernel::LibraryManager::Instance();
32 g_log.
debug() <<
"Algorithm Factory created.\n";
43 int local_version = version;
60 auto alg = this->
createAlgorithm(realNameAndVersion->first, local_version);
61 alg->calledByAlias =
true;
68 g_log.
error() <<
"algorithm " <<
name <<
" version " << version <<
" is not registered \n";
69 g_log.
error() <<
"the latest registered version is " << hVersion <<
'\n';
70 throw std::runtime_error(
"algorithm not registered " +
createName(
name, local_version));
73 throw std::runtime_error(
"algorithm not registered " +
name);
84 std::string key = this->
createName(algorithmName, version);
88 auto it =
m_vmap.find(algorithmName);
90 int highest_version = it->second;
91 if (highest_version > 1 && version == highest_version)
95 m_vmap.erase(algorithmName);
98 g_log.
warning() <<
"Error unsubscribing algorithm " << algorithmName <<
" version " << version
99 <<
". Nothing registered with this name and version.";
114 std::string key = this->
createName(algorithmName, version);
125 std::ostringstream oss;
126 oss <<
name <<
"|" << version;
135 std::string::size_type seperatorPosition = mangledName.find(
'|');
136 if (seperatorPosition == std::string::npos) {
137 throw std::invalid_argument(
"Cannot decode a Name string without a \"|\" (bar) character ");
139 std::string
name = mangledName.substr(0, seperatorPosition);
141 std::istringstream ss(mangledName.substr(seperatorPosition + 1));
144 g_log.
debug() <<
"mangled string:" << mangledName <<
" name:" <<
name <<
" version:" << version <<
'\n';
145 return std::pair<std::string, int>(
name, version);
178 std::unordered_set<std::string> hiddenCategories;
182 std::vector<std::string> validNames;
183 std::vector<std::string>::const_iterator itr_end = names.end();
184 for (std::vector<std::string>::const_iterator itr = names.begin(); itr != itr_end; ++itr) {
185 std::string
name = *itr;
188 std::shared_ptr<IAlgorithm> alg =
create(namePair.first, namePair.second);
189 std::vector<std::string> categories = alg->categories();
190 bool toBeRemoved =
true;
193 std::vector<std::string>::const_iterator itCategoriesEnd = categories.end();
194 for (std::vector<std::string>::const_iterator itCategories = categories.begin(); itCategories != itCategoriesEnd;
197 if (hiddenCategories.find(*itCategories) == hiddenCategories.end()) {
205 validNames.emplace_back(
name);
216std::optional<std::pair<std::string, int>>
218 auto a_it = m_amap.find(alias);
219 if (a_it == m_amap.end())
231 auto viter =
m_vmap.find(algorithmName);
232 if (viter !=
m_vmap.end())
233 return viter->second;
238 return realNameAndVersion->second;
240 throw std::runtime_error(
"AlgorithmFactory::highestVersion() - Unknown algorithm '" + algorithmName +
"'");
253 std::map<std::string, bool> resultCategories;
256 std::unordered_set<std::string> hiddenCategories;
261 std::vector<std::string> names =
getKeys(
true);
263 std::vector<std::string>::const_iterator itr_end = names.end();
265 for (std::vector<std::string>::const_iterator itr = names.begin(); itr != itr_end; ++itr) {
266 std::string
name = *itr;
269 std::shared_ptr<IAlgorithm> alg =
create(namePair.first, namePair.second);
271 std::vector<std::string> categories = alg->categories();
274 std::vector<std::string>::const_iterator itCategoriesEnd = categories.end();
275 for (std::vector<std::string>::const_iterator itCategories = categories.begin(); itCategories != itCategoriesEnd;
277 bool isHidden =
true;
279 if (hiddenCategories.find(*itCategories) == hiddenCategories.end()) {
282 resultCategories[*itCategories] = isHidden;
285 return resultCategories;
297 std::unordered_set<std::string> validCategories;
303 for (
auto const &category : categoryMap) {
304 bool isHidden = (category).second;
305 if (includeHidden || (!isHidden)) {
306 validCategories.insert((category).first);
310 return validCategories;
329 std::unordered_set<std::string> hiddenCategories;
330 if (!includeHidden) {
335 std::vector<AlgorithmDescriptor> res;
337 for (
const auto &s : sv) {
341 size_t i = s.find(
'|');
342 if (i == std::string::npos) {
346 desc.
name = s.substr(0, i);
347 std::string vers = s.substr(i + 1);
348 desc.
version = vers.empty() ? 1 : std::stoi(vers);
353 auto categories = alg->categories();
354 desc.
alias = alg->alias();
357 auto itCategoriesEnd = categories.end();
358 for (
auto itCategories = categories.begin(); itCategories != itCategoriesEnd; ++itCategories) {
362 bool categoryIsHidden =
false;
365 std::vector<std::string> categoryLayers;
366 boost::split(categoryLayers, desc.
category, boost::is_any_of(
"\\"));
369 std::string currentLayer;
370 for (
auto &categoryLayer : categoryLayers) {
371 currentLayer.append(categoryLayer);
373 if (hiddenCategories.find(currentLayer) != hiddenCategories.end()) {
375 categoryIsHidden =
true;
380 currentLayer.append(
"\\");
383 if (!categoryIsHidden) {
384 res.emplace_back(desc);
386 if (!desc.
alias.empty() && includeAliases) {
388 std::string lowerCaseName = desc.
name;
389 std::transform(desc.
name.cbegin(), desc.
name.cend(), lowerCaseName.begin(),
390 [](
unsigned char c) { return std::tolower(c); });
391 if (lowerCaseName != desc.
alias) {
402 std::string categoryString = Kernel::ConfigService::Instance().getString(
"algorithms.categories.hidden");
VersionMap m_vmap
The map holding the registered class names and their highest versions.
bool exists(const std::string &algorithmName, const int version=-1)
Does an algorithm of the given name and version exist.
std::string createName(const std::string &, const int &) const
creates an algorithm name convolved from an name and version
std::shared_ptr< Algorithm > createAlgorithm(const std::string &name, const int version) const
Create an algorithm object with the specified name.
const std::unordered_set< std::string > getCategories(bool includeHidden=false) const
Get the algorithm categories.
void fillHiddenCategories(std::unordered_set< std::string > *categorySet) const
fills a set with the hidden categories
int highestVersion(const std::string &algorithmName) const
Returns the highest version of the algorithm currently registered.
std::vector< AlgorithmDescriptor > getDescriptors(bool includeHidden=false, bool includeAliases=false) const
Returns algorithm descriptors.
void unsubscribe(const std::string &algorithmName, const int version)
Unsubscribe the given algorithm.
AlgorithmFactoryImpl()
Private Constructor for singleton class.
const std::string extractAlgName(const std::shared_ptr< IAlgorithm > &alg) const
Extract the name of an algorithm.
std::pair< std::string, int > decodeName(const std::string &mangledName) const
unmangles the names used as keys into the name and version
int extractAlgVersion(const std::shared_ptr< IAlgorithm > &alg) const
Extract the version of an algorithm.
~AlgorithmFactoryImpl() override
Private Destructor.
const std::string extractAlgAlias(const std::shared_ptr< IAlgorithm > &alg) const
Extract the alias of an algorithm.
const std::vector< std::string > getKeys() const override
Get the algorithm names and version - mangled use decodeName to separate.
std::shared_ptr< Algorithm > create(const std::string &, const int &) const
Creates an instance of an algorithm.
std::optional< std::pair< std::string, int > > getRealNameFromAlias(const std::string &alias) const noexcept
Get an algorithms name and version from the alias map.
const std::map< std::string, bool > getCategoriesWithState() const
Get the algorithm categories.
Base class from which all concrete algorithm classes should be derived.
The dynamic factory is a base dynamic factory for serving up objects in response to requests from oth...
void unsubscribe(const std::string &className)
Unregisters the given class and deletes the instantiator for the class.
virtual std::shared_ptr< Base > create(const std::string &className) const
Creates a new instance of the class with the given name.
virtual const std::vector< std::string > getKeys() const
Returns the keys in the map.
bool exists(const std::string &className) const
Returns true if the given class is currently registered.
Exception for when an item is not found in a collection.
void debug(const std::string &msg)
Logs at debug level.
void error(const std::string &msg)
Logs at error level.
void warning(const std::string &msg)
Logs at warning level.
Iterator begin()
Iterator referring to first element in the container.
@ TOK_IGNORE_EMPTY
ignore empty tokens
@ TOK_TRIM
remove leading and trailing whitespace from tokens
Iterator end()
Iterator referring to the past-the-end element in the container.
Kernel::Logger g_log("ExperimentInfo")
static logger object
Mantid::Kernel::StringTokenizer tokenizer
Structure uniquely describing an algorithm with its name, category and version.
std::string category
category
std::string name
Algorithm Name.