/** Using the [Post]ProcessingAlgorithm and [Post]ProcessingProperties *properties, * create and initialize an algorithm for processing. * * @param postProcessing :: true to create the PostProcessingAlgorithm. * false to create the ProcessingAlgorithm * @return shared pointer to the algorithm, ready for execution. * Returns a NULL pointer if no algorithm was chosen. */ IAlgorithm_sptr LiveDataAlgorithm::makeAlgorithm(bool postProcessing) { std::string prefix; if (postProcessing) prefix = "Post"; // Get the name of the algorithm to run std::string algoName = this->getPropertyValue(prefix + "ProcessingAlgorithm"); algoName = Strings::strip(algoName); // Get the script to run. Ignored if algo is specified std::string script = this->getPropertyValue(prefix + "ProcessingScript"); script = Strings::strip(script); std::string scriptfile = this->getPropertyValue(prefix + "ProcessingScriptFilename"); if (!algoName.empty()) { g_log.information() << "Creating algorithm from name \'" << algoName << "\'\n"; // Properties to pass to algo std::string props = this->getPropertyValue(prefix + "ProcessingProperties"); // Create the UNMANAGED algorithm IAlgorithm_sptr alg = this->createChildAlgorithm(algoName); // Skip some of the properties when setting std::unordered_set<std::string> ignoreProps; ignoreProps.insert("InputWorkspace"); ignoreProps.insert("OutputWorkspace"); // ...and pass it the properties alg->setPropertiesWithString(props, ignoreProps); // Warn if someone put both values. if (!script.empty()) g_log.warning() << "Running algorithm " << algoName << " and ignoring the script code in " << prefix + "ProcessingScript\n"; return alg; } else if (!script.empty() || !scriptfile.empty()) { // Run a snippet of python IAlgorithm_sptr alg = this->createChildAlgorithm("RunPythonScript"); alg->setLogging(false); if (scriptfile.empty()) { g_log.information("Creating python algorithm from string"); alg->setPropertyValue("Code", script); } else { g_log.information() << "Creating python algorithm from file \'" << scriptfile << "\'\n"; alg->setPropertyValue("Filename", scriptfile); } g_log.information(" stack traces will be off by 5" " lines because of boiler-plate"); return alg; } else { return IAlgorithm_sptr(); } }
/** * Returns a shared pointer by algorithm id * @param id :: The ID of the algorithm * @returns A shared pointer tot eh algorithm */ IAlgorithm_sptr AlgorithmManagerImpl::getAlgorithm(AlgorithmID id) const { for( std::deque<IAlgorithm_sptr>::const_iterator a = m_managed_algs.begin(); a!=m_managed_algs.end(); ++a) { if ((**a).getAlgorithmID() == id) return *a; } return IAlgorithm_sptr(); }
/** * Returns a shared pointer by algorithm id * @param id :: The ID of the algorithm * @returns A shared pointer to the algorithm */ IAlgorithm_sptr AlgorithmManagerImpl::getAlgorithm(AlgorithmID id) const { std::lock_guard<std::mutex> _lock(this->m_managedMutex); for (const auto &managed_alg : m_managed_algs) { if ((*managed_alg).getAlgorithmID() == id) return managed_alg; } return IAlgorithm_sptr(); }
/// Returns the most recently created instance of the named algorithm (or null /// if not found) IAlgorithm_sptr AlgorithmManagerImpl::newestInstanceOf(const std::string &algorithmName) const { for (auto it = m_managed_algs.rbegin(); it != m_managed_algs.rend(); ++it) { if ((*it)->name() == algorithmName) return *it; } return IAlgorithm_sptr(); }
/** * Returns a shared pointer by algorithm id * @param id :: The ID of the algorithm * @returns A shared pointer to the algorithm */ IAlgorithm_sptr AlgorithmManagerImpl::getAlgorithm(AlgorithmID id) const { Mutex::ScopedLock _lock(this->m_managedMutex); for (std::deque<IAlgorithm_sptr>::const_iterator a = m_managed_algs.begin(); a != m_managed_algs.end(); ++a) { if ((**a).getAlgorithmID() == id) return *a; } return IAlgorithm_sptr(); }
/** Creates and initialises an instance of an algorithm. * * The algorithm gets tracked in the list of "managed" algorithms, * which is shown in GUI for cancelling, etc. * * @param algName :: The name of the algorithm required * @param version :: The version of the algorithm required, if not defined most *recent version is used -> version =-1 * @param makeProxy :: If true (default), create and return AlgorithmProxy of *the given algorithm. * DO NOT SET TO FALSE unless you are really sure of what you are doing! * @return A pointer to the created algorithm * @throw NotFoundError Thrown if algorithm requested is not registered * @throw std::runtime_error Thrown if properties string is ill-formed */ IAlgorithm_sptr AlgorithmManagerImpl::create(const std::string &algName, const int &version, bool makeProxy) { Mutex::ScopedLock _lock(this->m_managedMutex); IAlgorithm_sptr alg; try { Algorithm_sptr unmanagedAlg = AlgorithmFactory::Instance().create( algName, version); // Throws on fail: if (makeProxy) alg = IAlgorithm_sptr(new AlgorithmProxy(unmanagedAlg)); else alg = unmanagedAlg; // If this takes us beyond the maximum size, then remove the oldest one(s) while (m_managed_algs.size() >= static_cast<std::deque<IAlgorithm_sptr>::size_type>(m_max_no_algs)) { std::deque<IAlgorithm_sptr>::iterator it; it = m_managed_algs.begin(); // Look for the first (oldest) algo that is NOT running right now. while (it != m_managed_algs.end()) { if (!(*it)->isRunning()) break; ++it; } if (it == m_managed_algs.end()) { // Unusual case where ALL algorithms are running g_log.warning() << "All algorithms in the AlgorithmManager are running. " << "Cannot pop oldest algorithm. " << "You should increase your 'algorithms.retained' value. " << m_managed_algs.size() << " in queue." << std::endl; break; } else { // Normal; erase that algorithm g_log.debug() << "Popping out oldest algorithm " << (*it)->name() << std::endl; m_managed_algs.erase(it); } } // Add to list of managed ones m_managed_algs.push_back(alg); alg->initialize(); } catch (std::runtime_error &ex) { g_log.error() << "AlgorithmManager:: Unable to create algorithm " << algName << ' ' << ex.what() << std::endl; throw std::runtime_error("AlgorithmManager:: Unable to create algorithm " + algName + ' ' + ex.what()); } return alg; }
/** Creates and initialises an instance of an algorithm * * @param algName The name of the algorithm required * @param version The version of the algorithm required, if not defined most recent version is used -> version =-1 * @return A pointer to the created algorithm * @throw NotFoundError Thrown if algorithm requested is not registered * @throw std::runtime_error Thrown if properties string is ill-formed */ IAlgorithm_sptr AlgorithmManagerImpl::create(const std::string& algName, const int& version) { try { Algorithm_sptr alg = AlgorithmFactory::Instance().create(algName,version);// Throws on fail: m_managed_algs.push_back(IAlgorithm_sptr(new AlgorithmProxy(alg))); m_managed_algs.back()->initialize(); // If this takes us beyond the maximum size, then remove the oldest one if (m_managed_algs.size() > static_cast<std::deque<IAlgorithm_sptr>::size_type>(m_max_no_algs) ) m_managed_algs.pop_front(); } catch(std::runtime_error& ex) { g_log.error()<<"AlgorithmManager:: Unable to create algorithm "<< algName << ' ' << ex.what() << std::endl; throw std::runtime_error("AlgorithmManager:: Unable to create algorithm " + algName + ' ' + ex.what()); } return m_managed_algs.back(); }
/** Using the [Post]ProcessingAlgorithm and [Post]ProcessingProperties *properties, * create and initialize an algorithm for processing. * * @param postProcessing :: true to create the PostProcessingAlgorithm. * false to create the ProcessingAlgorithm * @return shared pointer to the algorithm, ready for execution. * Returns a NULL pointer if no algorithm was chosen. */ IAlgorithm_sptr LiveDataAlgorithm::makeAlgorithm(bool postProcessing) { std::string prefix = ""; if (postProcessing) prefix = "Post"; // Get the name of the algorithm to run std::string algoName = this->getPropertyValue(prefix + "ProcessingAlgorithm"); algoName = Strings::strip(algoName); // Get the script to run. Ignored if algo is specified std::string script = this->getPropertyValue(prefix + "ProcessingScript"); script = Strings::strip(script); if (!algoName.empty()) { // Properties to pass to algo std::string props = this->getPropertyValue(prefix + "ProcessingProperties"); // Create the UNMANAGED algorithm IAlgorithm_sptr alg = this->createChildAlgorithm(algoName); // Skip some of the properties when setting std::set<std::string> ignoreProps; ignoreProps.insert("InputWorkspace"); ignoreProps.insert("OutputWorkspace"); // ...and pass it the properties alg->setPropertiesWithSimpleString(props, ignoreProps); // Warn if someone put both values. if (!script.empty()) g_log.warning() << "Running algorithm " << algoName << " and ignoring the script code in " << prefix + "ProcessingScript" << std::endl; return alg; } else if (!script.empty()) { // Run a snippet of python IAlgorithm_sptr alg = this->createChildAlgorithm("RunPythonScript"); alg->setLogging(false); alg->setPropertyValue("Code", script); return alg; } else return IAlgorithm_sptr(); }
/** Using the [Post]ProcessingAlgorithm and [Post]ProcessingProperties properties, * create and initialize an algorithm for processing. * * @param postProcessing :: true to create the PostProcessingAlgorithm. * false to create the ProcessingAlgorithm * @return shared pointer to the algorithm, ready for execution. * Returns a NULL pointer if no algorithm was chosen. */ IAlgorithm_sptr LiveDataAlgorithm::makeAlgorithm(bool postProcessing) { std::string prefix = ""; if (postProcessing) prefix = "Post"; // Get the name of the algorithm to run std::string algoName = this->getPropertyValue(prefix+"ProcessingAlgorithm"); algoName = Strings::strip(algoName); // Get the script to run. Ignored if algo is specified std::string script = this->getPropertyValue(prefix+"ProcessingScript"); script = Strings::strip(script); if (!algoName.empty()) { // Properties to pass to algo std::string props = this->getPropertyValue(prefix+"ProcessingProperties"); // Create the UNMANAGED algorithm IAlgorithm_sptr alg = this->createChildAlgorithm(algoName); // ...and pass it the properties boost::char_separator<char> sep(";"); typedef boost::tokenizer<boost::char_separator<char> > tokenizer; tokenizer propPairs(props, sep); // Iterate over the properties for (tokenizer::iterator it = propPairs.begin(); it != propPairs.end(); ++it) { // Pair of the type " std::string pair = *it; size_t n = pair.find('='); if (n == std::string::npos) { // Do nothing } else { // Normal "PropertyName=value" string. std::string propName = ""; std::string value = ""; // Extract the value string if (n < pair.size()-1) { propName = pair.substr(0, n); value = pair.substr(n+1, pair.size()-n-1); } else { // String is "PropertyName=" propName = pair.substr(0, n); value = ""; } // Skip some of the properties when setting if ((propName != "InputWorkspace") && (propName != "OutputWorkspace")) alg->setPropertyValue(propName,value); } } // Warn if someone put both values. if (!script.empty()) g_log.warning() << "Running algorithm " << algoName << " and ignoring the script code in " << prefix+"ProcessingScript" << std::endl; return alg; } else if (!script.empty()) { // Run a snippet of python IAlgorithm_sptr alg = this->createChildAlgorithm("RunPythonScript"); alg->setLogging(false); alg->setPropertyValue("Code", script); return alg; } else return IAlgorithm_sptr(); }