Mantid
Loading...
Searching...
No Matches
FuncMinimizerFactory.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
11
12#include <stdexcept>
13
14namespace Mantid::API {
15
17 // we need to make sure the library manager has been loaded before we
18 // are constructed so that it is destroyed after us and thus does
19 // not close any loaded DLLs with loaded algorithms in them
21}
22
29std::shared_ptr<IFuncMinimizer> FuncMinimizerFactoryImpl::createMinimizer(const std::string &str) const {
30 // check if there are any properties defined - look for a comma
31 if (str.find(',') == std::string::npos) { // no properties - create minimizer and return
32 return create(str);
33 }
34
35 // parse the string
36 Expression parser;
37 parser.parse(str);
38 parser.toList(); // make sure it is a list
39 const size_t n = parser.size();
40 if (n == 0) {
41 std::string mess = "Found empty initialization string";
42 throw std::invalid_argument(mess);
43 }
44
45 // create the minimizer from the type which is
46 const std::string type = parser[0].str();
47 auto minimizer = create(type);
48
49 // set the properties
50 for (size_t i = 1; i < n; ++i) {
51 auto &param = parser[i];
52 if (param.size() == 2 && param.name() == "=") {
53 const std::string parName = param[0].str();
54 if (minimizer->existsProperty(parName)) {
55 minimizer->setPropertyValue(parName, param[1].str());
56 }
57 }
58 }
59
60 return minimizer;
61}
62
63} // namespace Mantid::API
This class represents an expression made up of names, binary operators and brackets.
Definition: Expression.h:36
void parse(const std::string &str)
Parse a string and create an expression.
Definition: Expression.cpp:159
std::string str() const
Returns this expression as a string.
Definition: Expression.cpp:469
void toList(const std::string &sep=",")
Make sure the expression is a list of expression separated by sep, eg "term1,term2,...
Definition: Expression.cpp:559
size_t size() const
Returns the number of argumens.
Definition: Expression.h:79
FuncMinimizerFactoryImpl()
Private Constructor for singleton class.
std::shared_ptr< IFuncMinimizer > createMinimizer(const std::string &str) const
Creates an instance of a minimizer.
An interface for function minimizers.
The dynamic factory is a base dynamic factory for serving up objects in response to requests from oth...
virtual std::shared_ptr< IFuncMinimizer > create(const std::string &className) const
Creates a new instance of the class with the given name.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...