Mantid
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Mantid::API::Expression Class Reference

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 ExpressionbracketsRemoved () 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...
 
Expressionoperator= (const Expression &expr)
 Assignment operator. More...
 
const Expressionoperator[] (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< Operatorsm_operators
 pointer ot the operators More...
 
std::vector< Expressionm_terms
 Child expressions (function arguments) More...
 
Tokens m_tokens
 The container for the token markers. More...
 

Detailed Description

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.

Author
Roman Tolchenov, Tessella plc
Date
2/02/2010

Definition at line 36 of file Expression.h.

Member Typedef Documentation

◆ iterator

using Mantid::API::Expression::iterator = std::vector<Expression>::const_iterator

Const Iterator tpyedef.

Definition at line 81 of file Expression.h.

◆ Tokens

using Mantid::API::Expression::Tokens = std::vector<Token>
private

The container type.

Definition at line 149 of file Expression.h.

Constructor & Destructor Documentation

◆ Expression() [1/5]

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().

◆ Expression() [2/5]

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.

◆ Expression() [3/5]

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.

◆ Expression() [4/5]

Mantid::API::Expression::Expression ( const Expression expr)

copy contructor

Definition at line 99 of file Expression.cpp.

◆ Expression() [5/5]

Mantid::API::Expression::Expression ( const Expression pexpr)
private

copy contructor

Definition at line 103 of file Expression.cpp.

Member Function Documentation

◆ add_operators()

void Mantid::API::Expression::add_operators ( const std::vector< std::string > &  ops)
private

Adds new binary operators to the expression.

Parameters
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().

◆ add_unary()

void Mantid::API::Expression::add_unary ( const std::unordered_set< std::string > &  ops)
private

Adds new unary operators to the expression.

Parameters
ops:: A vector with unary operators

Definition at line 129 of file Expression.cpp.

References m_operators.

Referenced by Expression().

◆ begin()

iterator Mantid::API::Expression::begin ( ) const
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().

◆ bracketsRemoved()

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().

◆ end()

iterator Mantid::API::Expression::end ( ) const
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().

◆ GetOp()

std::string Mantid::API::Expression::GetOp ( size_t  i)
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().

◆ GetToken()

std::string Mantid::API::Expression::GetToken ( size_t  i)
private

◆ getVariables()

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().

◆ is_op_symbol()

bool Mantid::API::Expression::is_op_symbol ( const char  c) const
private

Check if a character is a part of an operator.

Parameters
c:: The character to check
Returns
True if it is true

Definition at line 147 of file Expression.cpp.

References m_operators.

Referenced by setFunct(), and tokenize().

◆ is_unary()

bool Mantid::API::Expression::is_unary ( const std::string &  op) const
private

Check if a string is a unary operator.

Parameters
op:: The string to check
Returns
True if the argument is a unary operator

Definition at line 143 of file Expression.cpp.

References m_operators.

Referenced by setFunct(), str(), and tokenize().

◆ isFunct()

bool Mantid::API::Expression::isFunct ( ) const
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().

◆ logPrint()

void Mantid::API::Expression::logPrint ( const std::string &  pads = "") const

Print the expression into std::cerr to show its structure.

Parameters
pads:: Padding to make indentation

Definition at line 385 of file Expression.cpp.

References m_funct, m_op, and m_terms.

◆ name()

std::string Mantid::API::Expression::name ( ) const
inline

◆ op_prec()

size_t Mantid::API::Expression::op_prec ( const std::string &  op) const
private

Returns the precedence of operator op.

Parameters
op:: The operator
Returns
The precedence

Definition at line 136 of file Expression.cpp.

References m_operators.

Referenced by parse(), setFunct(), and str().

◆ operator=()

Expression & Mantid::API::Expression::operator= ( const Expression expr)
default

Assignment operator.

◆ operator[]()

const Expression & Mantid::API::Expression::operator[] ( size_t  i) const
inline

Gets the Expression at the specified index.

Parameters
i:: the index
Returns
Expression at the given index

Definition at line 92 of file Expression.h.

◆ operator_name()

std::string Mantid::API::Expression::operator_name ( ) const
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().

◆ parse()

void Mantid::API::Expression::parse ( const std::string &  str)

◆ rename()

void Mantid::API::Expression::rename ( const std::string &  newName)

Rename this expression.

Parameters
newName:: The new name

Definition at line 547 of file Expression.cpp.

References m_funct.

Referenced by renameAll().

◆ renameAll()

void Mantid::API::Expression::renameAll ( const std::string &  oldName,
const std::string &  newName 
)

Rename all variables with a given name.

Parameters
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().

◆ setFunct()

void Mantid::API::Expression::setFunct ( const std::string &  name)
private

Set the function name of this expression.

It is also called when name cannot be split into tokens.

Parameters
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().

Referenced by parse(), and toList().

◆ size()

size_t Mantid::API::Expression::size ( ) const
inline

◆ str()

std::string Mantid::API::Expression::str ( ) const

◆ terms()

const std::vector< Expression > & Mantid::API::Expression::terms ( ) const
inline

◆ tokenize()

void Mantid::API::Expression::tokenize ( )
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().

◆ toList()

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,".

Parameters
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().

◆ trim()

void Mantid::API::Expression::trim ( std::string &  str)
staticprivate

Remove leading and ending empty spaces from a string.

Parameters
str:: The string.

Definition at line 149 of file Expression.cpp.

References str().

Referenced by parse(), and setFunct().

Member Data Documentation

◆ DEFAULT_OPS_STR

const std::vector< std::string > Mantid::API::Expression::DEFAULT_OPS_STR
static
Initial value:
= {
";", ",", "=", "== != > < <= >=", "&& || ^^", "+ -", "* /", "^"}

Definition at line 119 of file Expression.h.

Referenced by Expression(), and Mantid::CurveFitting::Algorithms::Fit::validateInputs().

◆ m_expr

std::string Mantid::API::Expression::m_expr
private

Saved expression string.

Definition at line 170 of file Expression.h.

Referenced by GetOp(), GetToken(), parse(), and tokenize().

◆ m_funct

std::string Mantid::API::Expression::m_funct
private

Function name.

Definition at line 172 of file Expression.h.

Referenced by logPrint(), rename(), setFunct(), and str().

◆ m_op

std::string Mantid::API::Expression::m_op
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().

◆ m_operators

std::shared_ptr<Operators> Mantid::API::Expression::m_operators
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().

◆ m_terms

std::vector<Expression> Mantid::API::Expression::m_terms
private

Child expressions (function arguments)

Definition at line 174 of file Expression.h.

Referenced by bracketsRemoved(), logPrint(), parse(), renameAll(), setFunct(), str(), and toList().

◆ m_tokens

Tokens Mantid::API::Expression::m_tokens
private

The container for the token markers.

Definition at line 169 of file Expression.h.

Referenced by GetOp(), GetToken(), parse(), and tokenize().


The documentation for this class was generated from the following files: