/** Creates an instance of an algorithm and sets the properties provided * * @param algName :: The name of the algorithm required * @param propertiesArray :: A single string containing properties in the * form "Property1=Value1;Property2=Value2;..." * @param version :: The version of the algorithm * @return A pointer to the created algorithm * WARNING! DO NOT DELETE THIS POINTER, because it is owned * by a shared pointer in the AlgorithmManager. * * @throw NotFoundError Thrown if algorithm requested is not registered * @throw std::invalid_argument Thrown if properties string is ill-formed */ IAlgorithm* FrameworkManagerImpl::createAlgorithm(const std::string& algName,const std::string& propertiesArray, const int& version) { // Use the previous method to create the algorithm IAlgorithm *alg = AlgorithmManager::Instance().create(algName,version).get();//createAlgorithm(algName); alg->setProperties(propertiesArray); return alg; }
/// Check if a newer release of Mantid is available void FrameworkManagerImpl::CheckIfNewerVersionIsAvailable() { try { IAlgorithm *algCheckVersion = this->createAlgorithm("CheckMantidVersion"); algCheckVersion->setAlgStartupLogging(false); Poco::ActiveResult<bool> result = algCheckVersion->executeAsync(); } catch (Kernel::Exception::NotFoundError &) { g_log.debug() << "CheckMantidVersion algorithm is not available - cannot " "check if a newer version is available." << std::endl; } }
/** Creates an instance of an algorithm, sets the properties provided and * then executes it. * * @param algName :: The name of the algorithm required * @param propertiesArray :: A single string containing properties in the * form "Property1=Value1;Property2=Value2;..." * @param version :: The version of the algorithm * @return A pointer to the executed algorithm * WARNING! DO NOT DELETE THIS POINTER, because it is owned * by a shared pointer in the AlgorithmManager. * * @throw NotFoundError Thrown if algorithm requested is not registered * @throw std::invalid_argument Thrown if properties string is ill-formed * @throw runtime_error Thrown if algorithm cannot be executed */ IAlgorithm* FrameworkManagerImpl::exec(const std::string& algName, const std::string& propertiesArray, const int& version) { // Make use of the previous method for algorithm creation and property setting IAlgorithm *alg = createAlgorithm(algName, propertiesArray,version); // Now execute the algorithm alg->execute(); return alg; }
/// Update instrument definitions from github void FrameworkManagerImpl::UpdateInstrumentDefinitions() { try { IAlgorithm *algDownloadInstrument = this->createAlgorithm("DownloadInstrument"); algDownloadInstrument->setAlgStartupLogging(false); Poco::ActiveResult<bool> result = algDownloadInstrument->executeAsync(); } catch (Kernel::Exception::NotFoundError &) { g_log.debug() << "DowndloadInstrument algorithm is not available - cannot " "update instrument definitions." << std::endl; } }
MDHistoWorkspace_sptr doTest(std::string algoName, std::string inName, std::string outName, bool succeeds, std::string otherProp, std::string otherPropValue) { MDHistoWorkspace_sptr histo = MDEventsTestHelper::makeFakeMDHistoWorkspace(2.0, 2, 5, 10.0, 2.0); IMDEventWorkspace_sptr event = MDEventsTestHelper::makeMDEW<2>(3, 0.0, 10.0, 1); WorkspaceSingleValue_sptr scalar = WorkspaceCreationHelper::CreateWorkspaceSingleValue(2.5); AnalysisDataService::Instance().addOrReplace("histo", histo); AnalysisDataService::Instance().addOrReplace("event", event); AnalysisDataService::Instance().addOrReplace("scalar", scalar); IAlgorithm* alg = FrameworkManager::Instance().createAlgorithm(algoName); alg->initialize(); alg->setPropertyValue("InputWorkspace", inName ); alg->setPropertyValue("OutputWorkspace", outName ); if (!otherProp.empty()) alg->setPropertyValue(otherProp, otherPropValue); alg->execute(); if (succeeds) { if (!alg->isExecuted()) throw std::runtime_error("Algorithm " + algoName + " did not succeed."); IMDWorkspace_sptr out = boost::dynamic_pointer_cast<IMDWorkspace>( AnalysisDataService::Instance().retrieve(outName)); if (!out) throw std::runtime_error("Algorithm " + algoName + " did not create the output workspace."); return boost::dynamic_pointer_cast<MDHistoWorkspace>(out); } else { if (alg->isExecuted()) throw std::runtime_error("Algorithm " + algoName + " did not fail as expected."); return (MDHistoWorkspace_sptr()); } }
/// Run a binary algorithm. MDHistoWorkspace_sptr doTest(std::string algoName, std::string lhs, std::string rhs, std::string outName, bool succeeds, std::string otherProp, std::string otherPropValue) { setUpBinaryOperationMDTestHelper(); IAlgorithm* alg = FrameworkManager::Instance().createAlgorithm(algoName); alg->initialize(); alg->setPropertyValue("LHSWorkspace", lhs ); alg->setPropertyValue("RHSWorkspace", rhs ); alg->setPropertyValue("OutputWorkspace", outName ); if (!otherProp.empty()) alg->setPropertyValue(otherProp, otherPropValue); alg->execute(); if (succeeds) { if (!alg->isExecuted()) throw std::runtime_error("Algorithm " + algoName + " did not succeed."); IMDWorkspace_sptr out = boost::dynamic_pointer_cast<IMDWorkspace>( AnalysisDataService::Instance().retrieve(outName)); if (!out) throw std::runtime_error("Algorithm " + algoName + " did not create the output workspace."); return boost::dynamic_pointer_cast<MDHistoWorkspace>(out); } else { if (alg->isExecuted()) throw std::runtime_error("Algorithm " + algoName + " did not fail as expected."); return (MDHistoWorkspace_sptr()); } }
/** * Execute an algorithm * @param nlhs :: The number of parameters on the left-hand side of the equals * @param plhs :: The data on the left-hand side of the equals * @param nrhs :: The number of parameters in the Matlab function call * @param prhs :: The data from the Matlab function call * @returns An integer indicating success/failure */ int RunAlgorithm(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { try { char buffer[256]; uint64_t *data = (uint64_t *)mxGetData(prhs[0]); IAlgorithm *alg = (IAlgorithm *)data[0]; mxGetString(prhs[1], buffer, sizeof(buffer)); alg->setProperties(buffer); alg->execute(); plhs[0] = mxCreateString(""); return 0; } catch (std::exception &e) { mexErrMsgTxt(e.what()); return 1; } }
/** * A slot to handle the help button click */ void AlgorithmDialog::helpClicked() { // Default help URL QString url = QString("http://www.mantidproject.org/") + m_algName; if (m_algorithm) { // Find the latest version IAlgorithm* alg = Mantid::API::FrameworkManager::Instance().createAlgorithm(m_algName.toStdString(), -1); int latest_version = alg->version(); // Adjust the link if you're NOT looking at the latest version of the algo int this_version = m_algorithm->version(); if ((this_version != latest_version)) url += "_v." + QString::number(this_version); } // Open the URL QDesktopServices::openUrl(QUrl(url)); }
/** * Execute an algorithm with a property list * @param nlhs :: The number of parameters on the left-hand side of the equals * @param plhs :: The data on the left-hand side of the equals * @param nrhs :: The number of parameters in the Matlab function call * @param prhs :: The data from the Matlab function call * @returns An integer indicating success/failure */ int RunAlgorithmPV(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { try { mxArray *marray; char buffer[256]; std::string property_name; uint64_t *data = (uint64_t *)mxGetData(prhs[0]); IAlgorithm *alg = (IAlgorithm *)data[0]; int i = 1; while (i < nrhs) { if (mxGetClassID(prhs[i]) != mxCHAR_CLASS) { mexErrMsgTxt("Algorithm property name must be a string"); } mxGetString(prhs[i], buffer, sizeof(buffer)); property_name = buffer; i++; strcpy(buffer, mxGetClassName(prhs[i])); if (!strcmp(buffer, "char")) { mxGetString(prhs[i], buffer, sizeof(buffer)); alg->setPropertyValue(property_name, buffer); } else if (!strcmp(buffer, "MantidWorkspace")) { marray = mxGetField(prhs[i], 0, "name"); mxGetString(marray, buffer, sizeof(buffer)); alg->setPropertyValue(property_name, buffer); } else { mexErrMsgTxt("Algorithm property value must be a string"); } i++; } alg->execute(); plhs[0] = mxCreateString(""); return 0; } catch (std::exception &e) { mexErrMsgTxt(e.what()); return 1; } }
//Initialization of all algorithms available within the client void AlgorithmManager::initAlgorithms() { IAlgorithm *tmp = NULL; tmp = new StorageTTL; this->_algorithms[tmp->getName()] = tmp; tmp = new Checksum; this->_algorithms[tmp->getName()] = tmp; tmp = new TypeOfService; this->_algorithms[tmp->getName()] = tmp; tmp = new Timestamp; this->_algorithms[tmp->getName()] = tmp; tmp = new IPCoverTiming; this->_algorithms[tmp->getName()] = tmp; }
/** * A helper function to create the simple API * @param algName :: A string giving the name of the algorithm * @param path :: The path to the .m file that we should create */ void CreateSimpleAPIHelper(const std::string &algName, const std::string &path) { IAlgorithm *alg; try { alg = FrameworkManager::Instance().createAlgorithm(algName); } catch (std::exception &) { std::string err = "An error occurred while writing the "; err += algName + " function definition.\n"; mexErrMsgTxt(err.c_str()); return; } std::string fullpath(path + algName + ".m"); std::ofstream mfile(fullpath.c_str()); typedef std::vector<Mantid::Kernel::Property *> PropertyVector; // parameter list mfile << "function res = " << algName << "(varargin)\n"; // help string PropertyVector orderedProperties(alg->getProperties()); std::sort(orderedProperties.begin(), orderedProperties.end(), PropertyOrdering()); PropertyVector::const_iterator pIter = orderedProperties.begin(); PropertyVector::const_iterator pEnd = orderedProperties.end(); mfile << "%\t" << algName << "("; for (; pIter != pEnd;) { mfile << (*pIter)->name(); if (++pIter != pEnd) mfile << ", "; } mfile << ")\n"; mfile << "%\t\tArgument description:\n"; pIter = orderedProperties.begin(); unsigned int iOpt(0); for (; pIter != pEnd; ++pIter) { Mantid::Kernel::Property *prop = *pIter; mfile << "%\t\tName: " << prop->name() << ", Optional: "; if (prop->isValid() == "") { ++iOpt; mfile << "Yes, Default value: " << santizePropertyValue(prop->value()); } else mfile << "No"; mfile << ", Direction: " << Mantid::Kernel::Direction::asText(prop->direction()); // << ", "; auto allowed = prop->allowedValues(); if (!allowed.empty()) { mfile << ", Allowed values: "; auto sIter = allowed.begin(); auto sEnd = allowed.end(); for (; sIter != sEnd;) { mfile << (*sIter); if (++sIter != sEnd) mfile << ", "; } } mfile << "\n"; } mfile << "%\n%\tNote: All string arguments must be wrapped in single quotes " "''.\n"; // The function definition mfile << "if nargin < " << (orderedProperties.size() - iOpt) << "\n" << "\tfprintf('All mandatory arguments have not been supplied, type " "\"help " << algName << "\" for more information\\n');\n" << "\treturn\n" << "end\n"; mfile << "alg = MantidAlgorithm('" << algName << "');\n" << "argstring = '';\n"; // Build arguments list mfile << "for i = 1:nargin\n" << "\targstring = strcat(argstring,varargin{i});\n" << "\tif i < nargin\n" << "\t\targstring = strcat(argstring, ';');\n" << "\tend\n" << "end\n"; // Run the algorithm mfile << "res = run(alg, argstring);\n"; mfile.close(); }
/** * Create the dynamic widgets for the concrete loader */ void LoadDialog::createDynamicLayout() { // Disable the layout so that a widget cannot be interacted with while it may be being deleted m_form.propertyLayout->setEnabled(false); using namespace Mantid::API; using namespace Mantid::Kernel; if( !m_form.fileWidget->isValid() ) return; // First step is the get the specific loader that is responsible IAlgorithm *loadAlg = getAlgorithm(); const QString filenames = m_form.fileWidget->getUserInput().asString(); if( filenames == m_currentFiles ) return; m_currentFiles = filenames; removeOldInputWidgets(m_form.propertyLayout); // The new file might be invalid try { loadAlg->setPropertyValue("Filename", filenames.toStdString()); } catch(std::exception & exc) { m_form.fileWidget->setFileProblem(QString::fromStdString(exc.what())); m_form.propertyLayout->setEnabled(true); m_form.propertyLayout->activate(); this->resize(this->width(), m_initialHeight + 15); // Reset the algorithm pointer so that the base class re-reads the properties and drops links from // old widgets meaning they are safe to remove setAlgorithm(loadAlg); tieStaticWidgets(false); //The ties are cleared when resetting the algorithm return; } // Reset the algorithm pointer so that the base class re-reads the properties and drops links from // old widgets meaning they are safe to remove setAlgorithm(loadAlg); tieStaticWidgets(false); //The ties are cleared when resetting the algorithm // Add the new ones const std::vector<Property*> & inputProps = loadAlg->getProperties(); int dialogHeight = m_initialHeight; for( size_t i = 0; i < inputProps.size(); ++i ) { const Property* prop = inputProps[i]; const QString propName = QString::fromStdString(prop->name()); if( propName == "OutputWorkspace" || propName == "Filename" ) continue; if( requiresUserInput(propName) ) { dialogHeight += createWidgetsForProperty(prop, m_form.propertyLayout, m_form.scrollAreaWidgetContents); } } // Re-enable and recompute the size of the layout m_form.propertyLayout->setEnabled(true); m_form.propertyLayout->activate(); const int screenHeight = QApplication::desktop()->height(); // If the thing won't end up too big compared to the screen height, // resize the scroll area so we don't get a scroll bar if ( dialogHeight < 0.8*screenHeight ) this->resize(this->width(),dialogHeight + 20); // Make sure the OutputWorkspace value has been stored so that the validator is cleared appropriately QString wsName(m_form.workspaceEdit->text()); if(!wsName.isEmpty()) storePropertyValue("OutputWorkspace",wsName); setPropertyValues(QStringList("Filename")); }