/** * Return the keys used for identifying algorithms. This includes those within the Factory itself and * any cleanly constructed algorithms stored here. * @param includeHidden true includes the hidden algorithm names and is faster, the default is false * @returns The strings used to identify individual algorithms */ const std::vector<std::string> AlgorithmFactoryImpl::getKeys(bool includeHidden) const { //Start with those subscribed with the factory and add the cleanly constructed algorithm keys std::vector<std::string> names = Kernel::DynamicFactory<Algorithm>::getKeys(); names.reserve(names.size() + m_cloneable_algs.size()); std::map<std::string, API::CloneableAlgorithm*>::const_iterator itr_end = m_cloneable_algs.end(); for(std::map<std::string, API::CloneableAlgorithm*>::const_iterator itr = m_cloneable_algs.begin(); itr!= itr_end; ++itr ) { names.push_back(itr->first); } if (includeHidden) { return names; } else { //hidden categories std::set<std::string> hiddenCategories; fillHiddenCategories(&hiddenCategories); //strip out any algorithms names where all of the categories are hidden std::vector<std::string> validNames; std::vector<std::string>::const_iterator itr_end = names.end(); for(std::vector<std::string>::const_iterator itr = names.begin(); itr!= itr_end; ++itr ) { std::string name = *itr; //check the categories std::pair<std::string,int> namePair = decodeName(name); boost::shared_ptr<IAlgorithm> alg = create(namePair.first,namePair.second); std::vector<std::string> categories = alg->categories(); bool toBeRemoved=true; //for each category std::vector<std::string>::const_iterator itCategoriesEnd = categories.end(); for(std::vector<std::string>::const_iterator itCategories = categories.begin(); itCategories!=itCategoriesEnd; ++itCategories) { //if the entry is not in the set of hidden categories if (hiddenCategories.find(*itCategories) == hiddenCategories.end()) { toBeRemoved=false; } } if (!toBeRemoved) { //just mark them to be removed as we are iterating around the vector at the moment validNames.push_back(name); } } return validNames; } }
/** * Get a list of descriptor objects used to order the algorithms in the stored map * where an algorithm has multiple categories it will be represented using multiple descriptors. * Hidden categories will not be included. * @returns A vector of descriptor objects */ std::vector<Algorithm_descriptor> AlgorithmFactoryImpl::getDescriptors() const { //algorithm names std::vector<std::string> sv; sv = getKeys(true); //hidden categories std::set<std::string> hiddenCategories; fillHiddenCategories(&hiddenCategories); //results vector std::vector<Algorithm_descriptor> res; for(std::vector<std::string>::const_iterator s=sv.begin();s!=sv.end();++s) { if (s->empty()) continue; Algorithm_descriptor desc; size_t i = s->find('|'); if (i == std::string::npos) { desc.name = *s; desc.version = 1; } else if (i > 0) { desc.name = s->substr(0,i); std::string vers = s->substr(i+1); desc.version = vers.empty()? 1 : atoi(vers.c_str()); } else continue; boost::shared_ptr<IAlgorithm> alg = create(desc.name,desc.version); std::vector<std::string> categories = alg->categories(); //for each category std::vector<std::string>::const_iterator itCategoriesEnd = categories.end(); for(std::vector<std::string>::const_iterator itCategories = categories.begin(); itCategories!=itCategoriesEnd; ++itCategories) { desc.category = *itCategories; //if the entry is not in the set of hidden categories if (hiddenCategories.find(desc.category) == hiddenCategories.end()) { res.push_back(desc); } } } return res; }
/** * Return the categories of the algorithms. This includes those within the Factory itself and * any cleanly constructed algorithms stored here. * @returns The map of the categories, together with a true false value difining if they are hidden */ const std::map<std::string,bool> AlgorithmFactoryImpl::getCategoriesWithState() const { std::map<std::string,bool> resultCategories; //hidden categories - empty initially std::set<std::string> hiddenCategories; fillHiddenCategories(&hiddenCategories); //get all of the alroithm keys, including the hidden ones for speed purposes we will filter later if required std::vector<std::string> names = getKeys(true); std::vector<std::string>::const_iterator itr_end = names.end(); //for each algorithm for(std::vector<std::string>::const_iterator itr = names.begin(); itr!= itr_end; ++itr ) { std::string name = *itr; //decode the name and create an instance std::pair<std::string,int> namePair = decodeName(name); boost::shared_ptr<IAlgorithm> alg = create(namePair.first,namePair.second); //extract out the categories std::vector<std::string> categories = alg->categories(); //for each category of the algorithm std::vector<std::string>::const_iterator itCategoriesEnd = categories.end(); for(std::vector<std::string>::const_iterator itCategories = categories.begin(); itCategories!=itCategoriesEnd; ++itCategories) { bool isHidden = true; //check if the category is hidden if (hiddenCategories.find(*itCategories) == hiddenCategories.end()) { isHidden = false; } resultCategories[*itCategories] = isHidden; } } return resultCategories; }
/** * Get a list of descriptor objects used to order the algorithms in the stored * map * where an algorithm has multiple categories it will be represented using * multiple descriptors. * @param includeHidden true includes the hidden algorithm names and is faster, * the default is false * @returns A vector of descriptor objects */ std::vector<Algorithm_descriptor> AlgorithmFactoryImpl::getDescriptors(bool includeHidden) const { // algorithm names std::vector<std::string> sv; sv = getKeys(true); // hidden categories std::set<std::string> hiddenCategories; if (includeHidden == false) { fillHiddenCategories(&hiddenCategories); } // results vector std::vector<Algorithm_descriptor> res; for (std::vector<std::string>::const_iterator s = sv.begin(); s != sv.end(); ++s) { if (s->empty()) continue; Algorithm_descriptor desc; size_t i = s->find('|'); if (i == std::string::npos) { desc.name = *s; desc.version = 1; } else if (i > 0) { desc.name = s->substr(0, i); std::string vers = s->substr(i + 1); desc.version = vers.empty() ? 1 : atoi(vers.c_str()); } else continue; boost::shared_ptr<IAlgorithm> alg = create(desc.name, desc.version); std::vector<std::string> categories = alg->categories(); desc.alias = alg->alias(); // For each category auto itCategoriesEnd = categories.end(); for (auto itCategories = categories.begin(); itCategories != itCategoriesEnd; ++itCategories) { desc.category = *itCategories; // Let's check if this category or any of its parents are hidden. bool categoryIsHidden = false; // Split the category into its components. std::vector<std::string> categoryLayers; boost::split(categoryLayers, desc.category, boost::is_any_of("\\")); // Traverse each parent category, working our way from the top down. std::string currentLayer = ""; for (auto &categoryLayer : categoryLayers) { currentLayer.append(categoryLayer); if (hiddenCategories.find(currentLayer) != hiddenCategories.end()) { // Category is hidden, no need to check any others. categoryIsHidden = true; break; } // Add a separator in case we're going down another layer. currentLayer.append("\\"); } if (!categoryIsHidden) res.push_back(desc); } } return res; }