exprDriver.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2010-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "exprDriver.H"
30 #include "expressionEntry.H"
31 #include "stringOps.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace expressions
38 {
39 
40  defineTypeNameAndDebug(exprDriver, 0);
41 
42 } // End namespace expressions
43 } // End namespace Foam
44 
45 
46 
47 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 #if 0
52 static string getEntryString
53 (
54  const dictionary& dict,
55  const string& key
56 )
57 {
58  const entry* eptr = dict.findEntry(key, keyType::REGEX_RECURSIVE);
59 
60  if (!eptr)
61  {
63  << "Entry " << key << " not found in "
64  << dict.name() << nl
65  << exit(FatalError);
66  }
67  else if (eptr->isDict())
68  {
70  << "Entry " << key << " found in "
71  << dict.name() << " but is a dictionary" << nl
72  << exit(FatalError);
73  }
74 
76 }
77 #endif
78 } // End namespace Foam
79 
80 
81 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
82 
84 (
85  bool cacheReadFields,
86  bool searchInMemory,
87  bool searchFiles,
88  const dictionary& dict
89 )
90 :
91  dict_(dict),
92  result_(),
93  variableStrings_(),
94  variables_(),
95  stashedTokenId_(0),
96 
97  // Controls
98  debugScanner_(dict.lookupOrDefault("debugScanner", false)),
99  debugParser_(dict.lookupOrDefault("debugParser", false)),
100  allowShadowing_
101  (
102  dict.lookupOrDefault("allowShadowing", false)
103  ),
104  prevIterIsOldTime_
105  (
106  dict.lookupOrDefault("prevIterIsOldTime", false)
107  ),
108  cacheReadFields_(cacheReadFields),
109  searchInMemory_(searchInMemory || cacheReadFields),
110  searchFiles_(searchFiles)
111 {}
112 
113 
115 (
116  const exprDriver& rhs
117 )
118 :
119  dict_(rhs.dict_),
120  result_(rhs.result_),
121  variableStrings_(rhs.variableStrings_),
122  variables_(rhs.variables_),
123  stashedTokenId_(0),
124 
125  debugScanner_(rhs.debugScanner_),
126  debugParser_(rhs.debugParser_),
127  allowShadowing_(rhs.allowShadowing_),
128  prevIterIsOldTime_(rhs.prevIterIsOldTime_),
129 
130  cacheReadFields_(rhs.cacheReadFields_),
131  searchInMemory_(rhs.searchInMemory_),
132  searchFiles_(rhs.searchFiles_)
133 {}
134 
135 
137 (
138  const dictionary& dict
139 )
140 :
141  exprDriver
142  (
143  dict.lookupOrDefault("cacheReadFields", false),
144  dict.lookupOrDefault("searchInMemory", true),
145  dict.lookupOrDefault("searchFiles", false),
146  dict
147  )
148 {
149  readDict(dict);
150 }
151 
152 
153 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
154 
156 (
157  const dictionary& dict
158 )
159 {
160  dict.readIfPresent("debugBaseDriver", debug);
161 
162  // Regular variables
163  variableStrings_ = readVariableStrings(dict);
164 
165  // Other tables?
166  // readTable("timelines", dict, lines_);
167  // readTable("lookuptables", dict, lookup_);
168  // readTable("lookuptables2D", dict, lookup2D_);
169 
170  return true;
171 }
172 
173 
175 {
176  result_.clear();
177 }
178 
179 
181 {
182  return true;
183 }
184 
185 
187 {}
188 
189 
191 {
192  variables_.clear();
193  addVariables(variableStrings_, false);
194 }
195 
196 
198 (
199  const word& varName,
200  const expressions::exprString& expr
201 )
202 {
203  parse(expr);
204  result_.testIfSingleValue();
205 
206  DebugInfo
207  << "Evaluating: " << expr << " -> " << varName << endl
208  << result_;
209 
210  // Overwrite with a copy
211  variables_.set(varName, exprResult(result_));
212 }
213 
214 
216 (
217  string remote,
218  const word& varName,
219  const expressions::exprString& expr
220 )
221 {
223 }
224 
225 
228 (
229  const exprDriver& other
230 ) const
231 {
232  // With warnings (noWarn = false)
233  return other.result().getUniform(this->size(), false);
234 }
235 
236 
238 (
239  const expressions::exprString& expr,
240  bool clear
241 )
242 {
243  if (clear)
244  {
245  clearVariables();
246  }
247 
248  // Allow inline list of semicolon-separated variables
249  const auto varExpressions =
250  stringOps::split<expressions::exprString>(expr, ';');
251 
252  for (const auto& subMatch : varExpressions)
253  {
254  string varExpr(stringOps::trim(subMatch.str()));
255  if (varExpr.empty())
256  {
257  continue;
258  }
259 
260  // Split on '=' for lhsExpr = rhsExpr
261  //
262  // varName = rhsExpr
263  // varName{where} = rhsExpr
264 
265  const auto eqPos = varExpr.find('=');
266 
267  if (eqPos == std::string::npos)
268  {
270  << "No '=' found in expression " << varExpr << nl << nl
271  << exit(FatalIOError);
272  }
273 
274  // The RHS
276  (
278  (
279  stringOps::trim(varExpr.substr(eqPos+1))
280  )
281  );
282 
283  // The LHS
284  varExpr.resize(eqPos);
285  stringOps::inplaceTrim(varExpr);
286 
287  // Check for varName{where}
288  const auto lbrace = varExpr.find('{');
289 
290  if (lbrace != std::string::npos)
291  {
292  const auto rbrace = varExpr.find('}');
293 
294  if (rbrace == std::string::npos || rbrace < lbrace)
295  {
297  // << "Context: " << driverContext_ << nl
298  << "No closing '}' found in " << varExpr << nl
299  << exit(FatalError);
300  }
301  else if (lbrace+1 == rbrace)
302  {
304  // << "Context: " << driverContext_ << nl
305  << "Empty '{}' location in " << varExpr << nl
306  << exit(FatalError);
307  }
308 
309  const word varName(word::validate(varExpr.substr(0, lbrace)));
310 
311  const expressions::exprString remoteExpr
312  (
314  (
315  varExpr.substr(lbrace+1, rbrace-lbrace-1)
316  )
317  );
318 
319  // Fails if derived class does not implement!
320 
321  evaluateVariableRemote(remoteExpr, varName, rhsExpr);
322  }
323  else
324  {
325  const word varName(word::validate(varExpr));
326 
327  evaluateVariable(varName, rhsExpr);
328  }
329  }
330 }
331 
332 
334 (
335  const UList<expressions::exprString>& list,
336  bool clear
337 )
338 {
339  if (clear)
340  {
341  clearVariables();
342  }
343 
344  for (const auto& expr : list)
345  {
346  addVariables(expr, false); // No clear (already done)
347  }
348 }
349 
350 
352 (
353  bool scannerDebug,
354  bool parserDebug
355 )
356 {
357  debugScanner_ = scannerDebug;
358  debugParser_ = parserDebug;
359 }
360 
361 
363 (
364  const exprDriver& rhs
365 )
366 {
367  debugScanner_ = rhs.debugScanner_;
368  debugParser_ = rhs.debugParser_;
369 }
370 
371 
373 (
374  bool cacheReadFields,
375  bool searchInMemory,
376  bool searchFiles
377 )
378 {
379  searchInMemory_ = searchInMemory_ || cacheReadFields_;
380 
381  #ifdef FULLDEBUG
382  Info<< "Searching "
383  << " registry:" << searchInMemory_
384  << " disk:" << searchFiles_
385  << " cache-read:" << cacheReadFields_ << nl;
386  #endif
387 }
388 
389 
391 (
392  const exprDriver& rhs
393 )
394 {
395  setSearchBehaviour
396  (
397  rhs.cacheReadFields_,
398  rhs.searchInMemory_,
399  rhs.searchFiles_
400  );
401 }
402 
403 
404 // ************************************************************************* //
Foam::expressions::exprDriver::updateSpecialVariables
virtual void updateSpecialVariables(bool force=false)
Examine current variable values and update stored variables.
Definition: exprDriver.C:186
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::exprTools::expressionEntry::evaluate
static string evaluate(const entry &e)
Generic concatenate tokens to space-separated string.
Definition: expressionEntryI.H:34
Foam::expressions::exprDriver::prevIterIsOldTime_
bool prevIterIsOldTime_
Use value of previous iteration when oldTime is requested.
Definition: exprDriver.H:148
Foam::expressions::exprDriver::debugParser_
bool debugParser_
Request debugging for parser.
Definition: exprDriver.H:142
Foam::expressions::exprDriver::addVariables
void addVariables(const expressions::exprString &expr, bool clear=true)
Add/set string expressions for variables.
Definition: exprDriver.C:238
Foam::expressions::exprDriver::searchInMemory_
bool searchInMemory_
Search in registry before looking on disk.
Definition: exprDriver.H:154
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::stringOps::inplaceTrim
void inplaceTrim(std::string &s)
Trim leading and trailing whitespace inplace.
Definition: stringOps.C:1088
Foam::expressions::exprDriver::readDict
virtual bool readDict(const dictionary &dict)
Read variables, tables etc.
Definition: exprDriver.C:156
Foam::expressions::exprDriver::setSearchBehaviour
void setSearchBehaviour(bool cacheReadFields, bool searchInMemory, bool searchFiles)
Set search behaviour.
Definition: exprDriver.C:373
Foam::expressions::exprDriver::searchFiles_
bool searchFiles_
Search on disk (eg, for a standalone application)
Definition: exprDriver.H:157
Foam::expressions::exprDriver::evaluateVariable
void evaluateVariable(const word &varName, const expressions::exprString &expr)
Definition: exprDriver.C:198
Foam::expressions::exprString::toExpr
static exprString toExpr(const std::string &str)
Copy convert string to exprString.
Definition: exprStringI.H:143
Foam::word::validate
static word validate(const std::string &s, const bool prefix=false)
Construct validated word (no invalid characters).
Definition: word.C:45
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::expressions::exprDriver::debugScanner_
bool debugScanner_
Request debugging for scanner.
Definition: exprDriver.H:139
Foam::expressions::exprDriver::dict_
const dictionary & dict_
The dictionary with all input data/specification.
Definition: exprDriver.H:121
Foam::expressions::exprDriver::setDebugging
void setDebugging(bool scannerDebug, bool parserDebug)
Set the scanner/parser debug.
Definition: exprDriver.C:352
Foam::expressions::exprDriver::allowShadowing_
bool allowShadowing_
Allow variable names to mask field names.
Definition: exprDriver.H:145
exprDriver.H
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:419
Foam::dictionary::name
const fileName & name() const
The dictionary name.
Definition: dictionary.H:446
Foam::expressions::exprDriver::getRemoteResult
virtual exprResult getRemoteResult(const exprDriver &other) const
Get the result from another driver.
Definition: exprDriver.C:228
Foam::expressions::exprResult
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:128
expressionEntry.H
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::expressions::exprDriver::variables_
HashTable< exprResult > variables_
The variables table.
Definition: exprDriver.H:130
Foam::expressions::exprDriver
Base driver for parsing (field) values.
Definition: exprDriver.H:112
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::expressions::exprDriver::clearVariables
virtual void clearVariables()
Clear temporary variables and resets from expression strings.
Definition: exprDriver.C:190
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::expressions::exprDriver::exprDriver
exprDriver(bool cacheReadFields=false, bool searchInMemory=true, bool searchFiles=false, const dictionary &dict=dictionary::null)
Null constructor, and null construct with search preferences.
Definition: exprDriver.C:84
Foam::expressions::exprDriver::cacheReadFields_
bool cacheReadFields_
Keep fields read from disc in memory.
Definition: exprDriver.H:151
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::expressions::exprDriver::variableStrings_
List< expressions::exprString > variableStrings_
Variable definitions, as read from a dictionary.
Definition: exprDriver.H:127
Foam::expressions::exprResult::getUniform
exprResult getUniform(const label size, const bool noWarn, const bool parRun=Pstream::parRun()) const
Construct a uniform field from the current results.
Definition: exprResult.C:424
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:350
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::expressions::exprResult::clear
void clear()
Clear (zero) the result.
Definition: exprResult.C:383
Foam::expressions::exprDriver::result
virtual const exprResult & result() const
Const access to expression result.
Definition: exprDriver.H:304
Foam::expressions::exprString
Definition: exprString.H:60
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::dictionary::findEntry
entry * findEntry(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find for an entry (non-const access) with the given keyword.
Definition: dictionary.C:369
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
Foam::expressions::exprDriver::evaluateVariableRemote
virtual void evaluateVariableRemote(string remote, const word &varName, const expressions::exprString &expr)
Definition: exprDriver.C:216
Foam::expressions::exprDriver::clearResult
void clearResult()
Clear the result.
Definition: exprDriver.C:174
Foam::expressions::defineTypeNameAndDebug
defineTypeNameAndDebug(fvExprDriver, 0)
stringOps.H
Foam::expressions::exprDriver::update
virtual bool update()
Update things.
Definition: exprDriver.C:180
Foam::expressions::exprDriver::result_
exprResult result_
The result.
Definition: exprDriver.H:124
Foam::keyType::REGEX_RECURSIVE
Definition: keyType.H:79
Foam::stringOps::trim
string trim(const std::string &str)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:1067