LifeV
Parser.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  * @file
29  * @brief File containing the Parser interface
30  *
31  * @date 07-04-2009
32  * @author Cristiano Malossi <cristiano.malossi@epfl.ch>
33  *
34  * @contributor Gilles Fourestey <gilles.fourestey@epfl.ch>
35  * @maintainer Cristiano Malossi <cristiano.malossi@epfl.ch>
36  */
37 
38 #ifndef Parser_H
39 #define Parser_H 1
40 
41 #include <lifev/core/util/LifeDebug.hpp>
42 #include <lifev/core/util/ParserSpiritGrammar.hpp>
43 //#include "muParser.h"
44 
45 namespace LifeV
46 {
47 
48 //! Parser - A string parser for algebraic expressions
49 /*!
50  * @author Cristiano Malossi
51  *
52  * \c Parser is a general interface class for \c LifeV algebraic parsers.
53  * At the present time it works only with \c boost::spirit::qi.
54  *
55  * <b>EXAMPLE - HOW TO USE</b>
56  *
57  * The syntax is very simple:
58  *
59  * <CODE>
60  * Parser parser;<BR>
61  * parser.setString( "-sqrt(4)+1*2" );<BR>
62  * Real result = parser.evaluate();<BR>
63  * </CODE>
64  *
65  * You can change the string at any time:
66  *
67  * <CODE>
68  * parser.setString( "c=2; [0., c, c*c, c*c*c]" );<BR>
69  * Real result0 = parser.evaluate(0); // 0<BR>
70  * Real result1 = parser.evaluate(1); // c<BR>
71  * Real result2 = parser.evaluate(2); // c*c<BR>
72  * Real result3 = parser.evaluate(3); // c*c*c<BR>
73  * </CODE>
74  *
75  * See \c ParserSpiritGrammar class for more details on the expression syntax.
76  */
77 class Parser
78 {
79 public:
80 
81  //! @name Public Types
82  //@{
83 
84  /*! @typedef stringsVector_Type */
85  //! Type definition for the vector containing the string segments
87 
88  /*! @typedef stringIterator_Type */
89  //! Type definition for the iterator over the strings
91 
92  /*! @typedef calculator_Type */
93  //!Type definition for the parser interpreter
95 
96  /*! @typedef results_Type */
97  //! Type definition for the results
99 
100  //@}
101 
102 
103  //! @name Constructors & Destructor
104  //@{
105 
106  //! Empty constructor (it needs a manual call to setString)
107  explicit Parser();
108 
109  //! Constructor
110  /*!
111  * @param string expression to parse
112  */
113  explicit Parser ( const std::string& string );
114 
115  //! Copy constructor
116  /*!
117  * @param parser Parser
118  */
119  explicit Parser ( const Parser& parser );
120 
121  //! Destructor
122  virtual ~Parser() {}
123 
124  //@}
125 
126 
127  //! @name Operators
128  //@{
129 
130  //! Operator =
131  /*!
132  * <b> Important note: this method is not working right now. </b>
133  *
134  * @param parser Parser
135  * @return reference to a copy of the class
136  */
137  Parser& operator= ( const Parser& parser );
138 
139  //@}
140 
141 
142  //! @name Methods
143  //@{
144 
145  //! Evaluate the expression
146  /*!
147  * @param id expression index (starting from 0)
148  * @return computed value
149  */
150  const Real& evaluate ( const ID& id = 0 );
151 
152  //! Count how many substrings are present in the string (utility for BCInterfaceFunctionParser)
153  /*!
154  * @param substring string to find
155  * @return number of substring
156  */
157  UInt countSubstring ( const std::string& substring ) const;
158 
159  //! Clear all the variables.
160  void clearVariables();
161 
162  //@}
163 
164 
165  //! @name Set Methods
166  //@{
167 
168  //! Set string function
169  /*!
170  * @param string Expression to evaluate
171  * @param stringSeparator Separator identifier (default -> ";")
172  */
173  void setString ( const std::string& string, const std::string& stringSeparator = ";" );
174 
175  //! Set/replace a variable
176  /*!
177  * @param name name of the parameter
178  * @param value value of the parameter
179  */
180  void setVariable ( const std::string& name, const Real& value );
181 
182  //@}
183 
184 
185  //! @name Get Methods
186  //@{
187 
188  //! Get variable
189  /*!
190  * @param name name of the parameter
191  * @return value of the variable
192  */
193  const Real& variable ( const std::string& name );
194 
195  //@}
196 
197 private:
198 
200 
202 
204 
206 
207  // mu::Parser M_parser;
208 };
209 
210 } // Namespace LifeV
211 
212 #endif /* Parser_H */
Parser(const Parser &parser)
Copy constructor.
Definition: Parser.cpp:75
void clearVariables()
Clear all the variables.
Definition: Parser.cpp:163
Parser & operator=(const Parser &parser)
Operator =.
Definition: Parser.cpp:87
calculator_Type M_calculator
Definition: Parser.hpp:205
stringsVector_Type M_strings
Definition: Parser.hpp:199
Parser()
Empty constructor (it needs a manual call to setString)
Definition: Parser.cpp:46
void updateInverseJacobian(const UInt &iQuadPt)
results_Type M_results
Definition: Parser.hpp:201
std::string::const_iterator stringIterator_Type
Type definition for the iterator over the strings.
Definition: Parser.hpp:90
bool M_evaluate
Definition: Parser.hpp:203
std::vector< std::string > stringsVector_Type
Type definition for the vector containing the string segments.
Definition: Parser.hpp:86
Parser - A string parser for algebraic expressions.
Definition: Parser.hpp:77
uint32_type ID
IDs.
Definition: LifeV.hpp:194
virtual ~Parser()
Destructor.
Definition: Parser.hpp:122
ParserSpiritGrammar< stringIterator_Type > calculator_Type
Type definition for the parser interpreter.
Definition: Parser.hpp:94
double Real
Generic real data.
Definition: LifeV.hpp:175
calculator_Type::results_Type results_Type
Type definition for the results.
Definition: Parser.hpp:98
const Real & evaluate(const ID &id=0)
Evaluate the expression.
Definition: Parser.cpp:107
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191