Mantid
|
This class represents an expression made up of names, binary operators and brackets. More...
#include <Expression.h>
Classes | |
struct | Operators |
Keeps operator that can be used in an expression. More... | |
class | ParsingError |
Specialised exception for parsing errors. More... | |
struct | Token |
This is a struct to mark a token in a string expression. More... | |
Public Types | |
using | iterator = std::vector< Expression >::const_iterator |
Const Iterator tpyedef. More... | |
Public Member Functions | |
iterator | begin () const |
An iterator pointing to the start of the expressions. More... | |
const Expression & | bracketsRemoved () const |
If the expression has 1 argument and empty function name it means it is wrapped in brackets This method returns first sub-expression without brackets. More... | |
iterator | end () const |
An iterator pointing to the end of the expressions. More... | |
Expression () | |
Default contructor. More... | |
Expression (const Expression &expr) | |
copy contructor More... | |
Expression (const std::vector< std::string > &binary, const std::unordered_set< std::string > &unary) | |
contructor More... | |
Expression (const std::vector< std::string > &ops) | |
contructor More... | |
std::unordered_set< std::string > | getVariables () const |
Return a list of all variable names in this expression. More... | |
bool | isFunct () const |
Returns true if the expression is a function (i.e. has arguments) More... | |
void | logPrint (const std::string &pads="") const |
Print the expression into std::cerr to show its structure. More... | |
std::string | name () const |
Returns the name of the expression which is a function or variable name. More... | |
Expression & | operator= (const Expression &expr) |
Assignment operator. More... | |
const Expression & | operator[] (size_t i) const |
Gets the Expression at the specified index. More... | |
std::string | operator_name () const |
Returns the the expression's binary operator on its left. More... | |
void | parse (const std::string &str) |
Parse a string and create an expression. More... | |
void | rename (const std::string &newName) |
Rename this expression. More... | |
void | renameAll (const std::string &oldName, const std::string &newName) |
Rename all variables with a given name. More... | |
size_t | size () const |
Returns the number of argumens. More... | |
std::string | str () const |
Returns this expression as a string. More... | |
const std::vector< Expression > & | terms () const |
Returns the top level terms of the expression (function arguments). More... | |
void | toList (const std::string &sep=",") |
Make sure the expression is a list of expression separated by sep, eg "term1,term2,..." If it's not a list turn it into one, eg "expr,". More... | |
Static Public Attributes | |
static const std::vector< std::string > | DEFAULT_OPS_STR |
Private Types | |
using | Tokens = std::vector< Token > |
The container type. More... | |
Private Member Functions | |
void | add_operators (const std::vector< std::string > &ops) |
Adds new binary operators to the expression. More... | |
void | add_unary (const std::unordered_set< std::string > &ops) |
Adds new unary operators to the expression. More... | |
Expression (const Expression *pexpr) | |
copy contructor More... | |
std::string | GetOp (size_t i) |
Get the operator connecting i-th token. More... | |
std::string | GetToken (size_t i) |
Get i-th token. More... | |
bool | is_op_symbol (const char c) const |
Check if a character is a part of an operator. More... | |
bool | is_unary (const std::string &op) const |
Check if a string is a unary operator. More... | |
size_t | op_prec (const std::string &op) const |
Returns the precedence of operator op. More... | |
void | setFunct (const std::string &name) |
Set the function name of this expression. More... | |
void | tokenize () |
Analyze the string in m_expr and find all top level tokens. More... | |
Static Private Member Functions | |
static void | trim (std::string &str) |
Remove leading and ending empty spaces from a string. More... | |
Private Attributes | |
std::string | m_expr |
Saved expression string. More... | |
std::string | m_funct |
Function name. More... | |
std::string | m_op |
Operator connecting this expression to its sibling on the left. More... | |
std::shared_ptr< Operators > | m_operators |
pointer ot the operators More... | |
std::vector< Expression > | m_terms |
Child expressions (function arguments) More... | |
Tokens | m_tokens |
The container for the token markers. More... | |
This class represents an expression made up of names, binary operators and brackets.
The input for an Expression is a text string. If an Expression is a function (sum, product, sine, etc) it has arguments. Each argument is an Expression itself. So Expression is a tree structure with functions in its nodes and the branches are the arguments.
Definition at line 36 of file Expression.h.
using Mantid::API::Expression::iterator = std::vector<Expression>::const_iterator |
Const Iterator tpyedef.
Definition at line 81 of file Expression.h.
|
private |
The container type.
Definition at line 149 of file Expression.h.
Mantid::API::Expression::Expression | ( | ) |
Default contructor.
Definition at line 72 of file Expression.cpp.
References add_operators(), add_unary(), DEFAULT_OPS_STR, and m_operators.
Referenced by parse().
Mantid::API::Expression::Expression | ( | const std::vector< std::string > & | ops | ) |
contructor
Definition at line 87 of file Expression.cpp.
References add_operators(), and m_operators.
Mantid::API::Expression::Expression | ( | const std::vector< std::string > & | binary, |
const std::unordered_set< std::string > & | unary | ||
) |
contructor
Definition at line 93 of file Expression.cpp.
References add_operators(), add_unary(), and m_operators.
Mantid::API::Expression::Expression | ( | const Expression & | expr | ) |
copy contructor
Definition at line 99 of file Expression.cpp.
|
private |
copy contructor
Definition at line 103 of file Expression.cpp.
|
private |
Adds new binary operators to the expression.
ops | :: A vector with operators in reverse order of precedence |
Definition at line 108 of file Expression.cpp.
References index, m_operators, str(), Mantid::Kernel::StringTokenizer::TOK_IGNORE_EMPTY, and Mantid::Kernel::StringTokenizer::TOK_TRIM.
Referenced by Expression().
|
private |
Adds new unary operators to the expression.
ops | :: A vector with unary operators |
Definition at line 129 of file Expression.cpp.
References m_operators.
Referenced by Expression().
|
inline |
An iterator pointing to the start of the expressions.
Definition at line 84 of file Expression.h.
Referenced by Mantid::API::FunctionFactoryImpl::addConstraints(), Mantid::API::IFunction::addConstraints(), and Mantid::API::MultiDomainFunction::setLocalAttribute().
const Expression & Mantid::API::Expression::bracketsRemoved | ( | ) | const |
If the expression has 1 argument and empty function name it means it is wrapped in brackets This method returns first sub-expression without brackets.
Definition at line 513 of file Expression.cpp.
References m_terms, name(), and size().
Referenced by Mantid::API::FunctionFactoryImpl::createComposite(), and Mantid::API::FunctionFactoryImpl::createInitialized().
|
inline |
An iterator pointing to the end of the expressions.
Definition at line 86 of file Expression.h.
Referenced by Mantid::API::FunctionFactoryImpl::addConstraints(), Mantid::API::IFunction::addConstraints(), and Mantid::API::MultiDomainFunction::setLocalAttribute().
|
private |
Get the operator connecting i-th token.
Definition at line 377 of file Expression.cpp.
References Mantid::API::Expression::Token::ie, Mantid::API::Expression::Token::is1, m_expr, and m_tokens.
Referenced by parse().
|
private |
Get i-th token.
Definition at line 360 of file Expression.cpp.
References Mantid::API::Expression::Token::ie, Mantid::API::Expression::Token::is, Mantid::API::Expression::Token::is1, m_expr, and m_tokens.
Referenced by parse().
std::unordered_set< std::string > Mantid::API::Expression::getVariables | ( | ) | const |
Return a list of all variable names in this expression.
Definition at line 524 of file Expression.cpp.
References isFunct(), and name().
Referenced by MantidQt::MantidWidgets::UserFunctionDialog::checkParameters(), and MantidQt::MantidWidgets::UserFunctionDialog::updateFunction().
|
private |
Check if a character is a part of an operator.
c | :: The character to check |
Definition at line 147 of file Expression.cpp.
References m_operators.
Referenced by setFunct(), and tokenize().
|
private |
Check if a string is a unary operator.
op | :: The string to check |
Definition at line 143 of file Expression.cpp.
References m_operators.
Referenced by setFunct(), str(), and tokenize().
|
inline |
Returns true if the expression is a function (i.e. has arguments)
Definition at line 69 of file Expression.h.
Referenced by getVariables(), and renameAll().
void Mantid::API::Expression::logPrint | ( | const std::string & | pads = "" | ) | const |
Print the expression into std::cerr to show its structure.
pads | :: Padding to make indentation |
Definition at line 385 of file Expression.cpp.
|
inline |
Returns the name of the expression which is a function or variable name.
Definition at line 71 of file Expression.h.
Referenced by Mantid::API::FunctionFactoryImpl::addConstraints(), Mantid::API::FunctionFactoryImpl::addTies(), bracketsRemoved(), Mantid::API::FunctionFactoryImpl::createComposite(), Mantid::API::ConstraintFactoryImpl::createInitialized(), Mantid::API::FunctionFactoryImpl::createSimple(), getVariables(), Mantid::CurveFitting::Constraints::BoundaryConstraint::initialize(), renameAll(), setFunct(), Mantid::API::MultiDomainFunction::setLocalAttribute(), MantidQt::MantidWidgets::splitConstraintString(), and toList().
|
private |
Returns the precedence of operator op.
op | :: The operator |
Definition at line 136 of file Expression.cpp.
References m_operators.
Referenced by parse(), setFunct(), and str().
|
default |
Assignment operator.
|
inline |
Gets the Expression at the specified index.
i | :: the index |
Definition at line 92 of file Expression.h.
|
inline |
Returns the the expression's binary operator on its left.
Can be an empty string.
Definition at line 74 of file Expression.h.
Referenced by Mantid::CurveFitting::Constraints::BoundaryConstraint::initialize(), and MantidQt::MantidWidgets::splitConstraintString().
void Mantid::API::Expression::parse | ( | const std::string & | str | ) |
Parse a string and create an expression.
str | :: The input string. |
Definition at line 159 of file Expression.cpp.
References Mantid::Kernel::StringTokenizer::begin(), Expression(), GetOp(), GetToken(), m_expr, m_op, m_operators, m_terms, m_tokens, op_prec(), parse(), setFunct(), str(), Mantid::Kernel::StringTokenizer::TOK_IGNORE_EMPTY, Mantid::Kernel::StringTokenizer::TOK_TRIM, tokenize(), and trim().
Referenced by Mantid::API::IFunction::addConstraints(), Mantid::API::IFunction::addTies(), MantidQt::MantidWidgets::UserFunctionDialog::checkParameters(), Mantid::API::FunctionFactoryImpl::createInitialized(), Mantid::API::ConstraintFactoryImpl::createInitialized(), Mantid::API::FuncMinimizerFactoryImpl::createMinimizer(), Mantid::CurveFitting::Algorithms::EstimateFitParameters::execConcrete(), parse(), Mantid::API::MultiDomainFunction::setLocalAttribute(), MantidQt::MantidWidgets::splitConstraintString(), and MantidQt::MantidWidgets::UserFunctionDialog::updateFunction().
void Mantid::API::Expression::rename | ( | const std::string & | newName | ) |
Rename this expression.
newName | :: The new name |
Definition at line 547 of file Expression.cpp.
References m_funct.
Referenced by renameAll().
void Mantid::API::Expression::renameAll | ( | const std::string & | oldName, |
const std::string & | newName | ||
) |
Rename all variables with a given name.
oldName | :: The old name |
newName | :: The new name |
Definition at line 549 of file Expression.cpp.
References isFunct(), m_terms, name(), and rename().
Referenced by MantidQt::MantidWidgets::UserFunctionDialog::checkParameters().
|
private |
Set the function name of this expression.
It is also called when name cannot be split into tokens.
name | :: The name of the function |
Definition at line 396 of file Expression.cpp.
References Mantid::API::EMPTY_EXPRESSION_NAME, is_op_symbol(), is_unary(), m_funct, m_op, m_terms, name(), op_prec(), tmp, and trim().
|
inline |
Returns the number of argumens.
Definition at line 79 of file Expression.h.
Referenced by Mantid::API::FunctionFactoryImpl::addTie(), bracketsRemoved(), Mantid::API::FunctionFactoryImpl::createComposite(), Mantid::API::FuncMinimizerFactoryImpl::createMinimizer(), Mantid::API::FunctionFactoryImpl::createSimple(), Mantid::CurveFitting::Constraints::BoundaryConstraint::initialize(), Mantid::API::MultiDomainFunction::setLocalAttribute(), MantidQt::MantidWidgets::splitConstraintString(), and str().
std::string Mantid::API::Expression::str | ( | ) | const |
Returns this expression as a string.
It does not simply returns the input string but recreates it.
Definition at line 469 of file Expression.cpp.
References is_unary(), m_funct, m_op, m_terms, op_prec(), and size().
Referenced by add_operators(), Mantid::API::FunctionFactoryImpl::addTie(), MantidQt::MantidWidgets::UserFunctionDialog::checkParameters(), Mantid::API::FunctionFactoryImpl::createComposite(), Mantid::API::FuncMinimizerFactoryImpl::createMinimizer(), Mantid::API::FunctionFactoryImpl::createSimple(), Mantid::CurveFitting::Constraints::BoundaryConstraint::initialize(), parse(), and trim().
|
inline |
Returns the top level terms of the expression (function arguments).
For a variable it empty.
Definition at line 77 of file Expression.h.
Referenced by Mantid::API::FunctionFactoryImpl::addConstraint(), Mantid::API::FunctionFactoryImpl::createComposite(), Mantid::API::FunctionFactoryImpl::createSimple(), Mantid::CurveFitting::Algorithms::EstimateFitParameters::execConcrete(), and Mantid::API::MultiDomainFunction::setLocalAttribute().
|
private |
Analyze the string in m_expr and find all top level tokens.
Result: filled in m_tokens structure.
Definition at line 196 of file Expression.cpp.
References error, Mantid::API::Expression::Token::ie, Mantid::API::Expression::Token::is1, is_op_symbol(), is_unary(), m_expr, m_operators, and m_tokens.
Referenced by parse().
void Mantid::API::Expression::toList | ( | const std::string & | sep = "," | ) |
Make sure the expression is a list of expression separated by sep, eg "term1,term2,..." If it's not a list turn it into one, eg "expr,".
sep | :: Separator |
Definition at line 559 of file Expression.cpp.
References m_terms, name(), and setFunct().
Referenced by Mantid::API::IFunction::addConstraints(), Mantid::API::IFunction::addTies(), Mantid::API::FuncMinimizerFactoryImpl::createMinimizer(), Mantid::CurveFitting::Algorithms::EstimateFitParameters::execConcrete(), and Mantid::API::MultiDomainFunction::setLocalAttribute().
|
staticprivate |
Remove leading and ending empty spaces from a string.
str | :: The string. |
Definition at line 149 of file Expression.cpp.
References str().
Referenced by parse(), and setFunct().
|
static |
Definition at line 119 of file Expression.h.
Referenced by Expression(), and Mantid::CurveFitting::Algorithms::Fit::validateInputs().
|
private |
Saved expression string.
Definition at line 170 of file Expression.h.
Referenced by GetOp(), GetToken(), parse(), and tokenize().
|
private |
Function name.
Definition at line 172 of file Expression.h.
Referenced by logPrint(), rename(), setFunct(), and str().
|
private |
Operator connecting this expression to its sibling on the left.
Definition at line 173 of file Expression.h.
Referenced by logPrint(), parse(), setFunct(), and str().
|
private |
pointer ot the operators
Definition at line 215 of file Expression.h.
Referenced by add_operators(), add_unary(), Expression(), is_op_symbol(), is_unary(), op_prec(), parse(), and tokenize().
|
private |
Child expressions (function arguments)
Definition at line 174 of file Expression.h.
Referenced by bracketsRemoved(), logPrint(), parse(), renameAll(), setFunct(), str(), and toList().
|
private |
The container for the token markers.
Definition at line 169 of file Expression.h.
Referenced by GetOp(), GetToken(), parse(), and tokenize().