LifeV
Parser.cpp
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 #include <lifev/core/util/Parser.hpp>
39 
40 namespace LifeV
41 {
42 
43 // ===================================================
44 // Constructors & Destructor
45 // ===================================================
47  M_strings (),
48  M_results (),
49  M_calculator (),
50  M_evaluate ( true )
51 {
52 
53 #ifdef HAVE_LIFEV_DEBUG
54  debugStream ( 5030 ) << "Parser::Parser" << "\n";
55 #endif
56 
57  M_calculator.setDefaultVariables();
58 }
59 
60 Parser::Parser ( const std::string& string ) :
61  M_strings (),
62  M_results (),
63  M_calculator (),
64  M_evaluate ( true )
65 {
66 
67 #ifdef HAVE_LIFEV_DEBUG
68  debugStream ( 5030 ) << "Parser::Parser( string )" << "\n";
69 #endif
70 
71  M_calculator.setDefaultVariables();
72  setString ( string );
73 }
74 
75 Parser::Parser ( const Parser& parser ) :
76  M_strings ( parser.M_strings ),
77  M_results ( parser.M_results ),
78  M_calculator ( parser.M_calculator ),
79  M_evaluate ( parser.M_evaluate )
80 {
81 }
82 
83 // ===================================================
84 // Operators
85 // ===================================================
86 Parser&
87 Parser::operator= ( const Parser& parser )
88 {
89  if ( this != &parser )
90  {
91  std::cerr << "!!! ERROR: Operator= not working !!!" << std::endl;
92  std::exit ( EXIT_FAILURE );
93 
94  M_strings = parser.M_strings;
95  M_results = parser.M_results;
96  //M_calculator = parser.M_calculator; //NOT WORKING!!!
97  M_evaluate = parser.M_evaluate;
98  }
99 
100  return *this;
101 }
102 
103 // ===================================================
104 // Methods
105 // ===================================================
106 const Real&
107 Parser::evaluate ( const ID& id )
108 {
109  if ( M_evaluate )
110  {
111  M_results.clear();
112  stringIterator_Type start, end;
113 
114  for ( UInt i (0); i < M_strings.size(); ++i )
115  {
116  start = M_strings[i].begin();
117  end = M_strings[i].end();
118 #ifdef HAVE_BOOST_SPIRIT_QI
119 #ifdef ENABLE_SPIRIT_PARSER
120  qi::phrase_parse ( start, end, M_calculator, ascii::space, M_results );
121 #else
122  std::cerr << "!!! ERROR: The Boost Spirit parser has been disabled !!!" << std::endl;
123  std::exit ( EXIT_FAILURE );
124 #endif /* ENABLE_SPIRIT_PARSER */
125 #else
126  std::cerr << "!!! ERROR: Boost version < 1.41 !!!" << std::endl;
127  std::exit ( EXIT_FAILURE );
128 #endif
129  }
130  M_evaluate = false;
131  }
132 
133 #ifdef HAVE_LIFEV_DEBUG
134  debugStream ( 5030 ) << "Parser::evaluate results[ " << id << "]: " << M_results[id] << "\n";
135 #endif
136 
137  return M_results[id];
138 }
139 
140 UInt
141 Parser::countSubstring ( const std::string& substring ) const
142 {
143  UInt counter ( 0 );
144  std::string::size_type position ( 0 );
145 
146  for ( ;; )
147  {
148  position = M_strings.back().find ( substring, position );
149 
150  if ( position == std::string::npos )
151  {
152  break;
153  }
154 
155  ++counter;
156  position += substring.length(); // start next search after this substring
157  }
158 
159  return counter;
160 }
161 
162 void
164 {
165  M_calculator.clearVariables();
166  M_evaluate = true;
167 }
168 
169 // ===================================================
170 // Set Methods
171 // ===================================================
172 void
173 Parser::setString ( const std::string& string, const std::string& stringSeparator )
174 {
175 
176 #ifdef HAVE_LIFEV_DEBUG
177  debugStream ( 5030 ) << "Parser::setString strings: " << string << "\n";
178 #endif
179 
180  M_strings.clear();
181  boost::split ( M_strings, string, boost::is_any_of ( stringSeparator ) );
182 
183  //Remove white space to speed up the parser
184  for ( UInt i = 0; i < M_strings.size(); ++i )
185  {
186  boost::replace_all ( M_strings[i], " ", "" );
187  }
188 
189  //Reserve the space for results
190  M_results.clear();
191  M_results.reserve ( countSubstring ( "," ) + 1 );
192 
193  M_evaluate = true;
194 }
195 
196 void
197 Parser::setVariable ( const std::string& name, const Real& value )
198 {
199 
200 #ifdef HAVE_LIFEV_DEBUG
201  debugStream ( 5030 ) << "Parser::setVariable variables[" << name << "]: " << value << "\n";
202 #endif
203 
204  M_calculator.setVariable ( name, value);
205 
206  M_evaluate = true;
207 }
208 
209 // ===================================================
210 // Get Methods
211 // ===================================================
212 const Real&
213 Parser::variable ( const std::string& name )
214 {
215 
216 #ifdef HAVE_LIFEV_DEBUG
217  debugStream ( 5030 ) << "Parser::variable variables[" << name << "]: " << M_calculator.variable ( name ) << "\n";
218 #endif
219 
220  return M_calculator.variable ( name );
221 }
222 
223 } // Namespace LifeV
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
Parser - A string parser for algebraic expressions.
Definition: Parser.hpp:77
uint32_type ID
IDs.
Definition: LifeV.hpp:194
double Real
Generic real data.
Definition: LifeV.hpp:175
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