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-2020 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  arg1Value_(0),
96  stashedTokenId_(0),
97 
98  // Controls
99  debugScanner_(dict.getOrDefault("debugScanner", false)),
100  debugParser_(dict.getOrDefault("debugParser", false)),
101  allowShadowing_
102  (
103  dict.getOrDefault("allowShadowing", false)
104  ),
105  prevIterIsOldTime_
106  (
107  dict.getOrDefault("prevIterIsOldTime", false)
108  ),
109  cacheReadFields_(cacheReadFields),
110  searchInMemory_(searchInMemory || cacheReadFields),
111  searchFiles_(searchFiles)
112 {}
113 
114 
116 (
117  const exprDriver& rhs
118 )
119 :
120  dict_(rhs.dict_),
121  result_(rhs.result_),
122  variableStrings_(rhs.variableStrings_),
123  variables_(rhs.variables_),
124  arg1Value_(rhs.arg1Value_),
125  stashedTokenId_(0),
126 
127  debugScanner_(rhs.debugScanner_),
128  debugParser_(rhs.debugParser_),
129  allowShadowing_(rhs.allowShadowing_),
130  prevIterIsOldTime_(rhs.prevIterIsOldTime_),
131 
132  cacheReadFields_(rhs.cacheReadFields_),
133  searchInMemory_(rhs.searchInMemory_),
134  searchFiles_(rhs.searchFiles_)
135 {}
136 
137 
139 (
140  const dictionary& dict
141 )
142 :
143  exprDriver
144  (
145  dict.getOrDefault("cacheReadFields", false),
146  dict.getOrDefault("searchInMemory", true),
147  dict.getOrDefault("searchFiles", false),
148  dict
149  )
150 {
151  readDict(dict);
152 }
153 
154 
155 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
156 
158 (
159  const dictionary& dict
160 )
161 {
162  dict.readIfPresent("debugBaseDriver", debug);
163 
164  // Regular variables
165  variableStrings_ = readVariableStrings(dict);
166 
167  // Other tables?
168  // readTable("timelines", dict, lines_);
169  // readTable("lookuptables", dict, lookup_);
170  // readTable("lookuptables2D", dict, lookup2D_);
171 
172  return true;
173 }
174 
175 
177 {
178  result_.clear();
179 }
180 
181 
183 {
184  return true;
185 }
186 
187 
189 {}
190 
191 
193 {
194  variables_.clear();
195  addVariables(variableStrings_, false);
196 }
197 
198 
200 {
201  arg1Value_ = val;
202 }
203 
204 
206 {
207  return arg1Value_;
208 }
209 
210 
212 (
213  const word& varName,
214  const expressions::exprString& expr
215 )
216 {
217  parse(expr);
218  result_.testIfSingleValue();
219 
220  DebugInfo
221  << "Evaluating: " << expr << " -> " << varName << endl
222  << result_;
223 
224  // Overwrite with a copy
225  variables_.set(varName, exprResult(result_));
226 }
227 
228 
230 (
231  string remote,
232  const word& varName,
233  const expressions::exprString& expr
234 )
235 {
237 }
238 
239 
242 (
243  const exprDriver& other
244 ) const
245 {
246  // With warnings (noWarn = false)
247  return other.result().getUniform(this->size(), false);
248 }
249 
250 
252 (
253  const expressions::exprString& expr,
254  bool clear
255 )
256 {
257  if (clear)
258  {
259  clearVariables();
260  }
261 
262  // Allow inline list of semicolon-separated variables
263  const auto varExpressions =
264  stringOps::split<expressions::exprString>(expr, ';');
265 
266  for (const auto& subMatch : varExpressions)
267  {
268  string varExpr(stringOps::trim(subMatch.str()));
269  if (varExpr.empty())
270  {
271  continue;
272  }
273 
274  // Split on '=' for lhsExpr = rhsExpr
275  //
276  // varName = rhsExpr
277  // varName{where} = rhsExpr
278 
279  const auto eqPos = varExpr.find('=');
280 
281  if (eqPos == std::string::npos)
282  {
284  << "No '=' found in expression " << varExpr << nl << nl
285  << exit(FatalIOError);
286  }
287 
288  // The RHS
290  (
292  (
293  stringOps::trim(varExpr.substr(eqPos+1))
294  )
295  );
296 
297  // The LHS
298  varExpr.resize(eqPos);
299  stringOps::inplaceTrim(varExpr);
300 
301  // Check for varName{where}
302  const auto lbrace = varExpr.find('{');
303 
304  if (lbrace != std::string::npos)
305  {
306  const auto rbrace = varExpr.find('}');
307 
308  if (rbrace == std::string::npos || rbrace < lbrace)
309  {
311  // << "Context: " << driverContext_ << nl
312  << "No closing '}' found in " << varExpr << nl
313  << exit(FatalError);
314  }
315  else if (lbrace+1 == rbrace)
316  {
318  // << "Context: " << driverContext_ << nl
319  << "Empty '{}' location in " << varExpr << nl
320  << exit(FatalError);
321  }
322 
323  const word varName(word::validate(varExpr.substr(0, lbrace)));
324 
325  const expressions::exprString remoteExpr
326  (
328  (
329  varExpr.substr(lbrace+1, rbrace-lbrace-1)
330  )
331  );
332 
333  // Fails if derived class does not implement!
334 
335  evaluateVariableRemote(remoteExpr, varName, rhsExpr);
336  }
337  else
338  {
339  const word varName(word::validate(varExpr));
340 
341  evaluateVariable(varName, rhsExpr);
342  }
343  }
344 }
345 
346 
348 (
349  const UList<expressions::exprString>& list,
350  bool clear
351 )
352 {
353  if (clear)
354  {
355  clearVariables();
356  }
357 
358  for (const auto& expr : list)
359  {
360  addVariables(expr, false); // No clear (already done)
361  }
362 }
363 
364 
366 (
367  bool scannerDebug,
368  bool parserDebug
369 )
370 {
371  debugScanner_ = scannerDebug;
372  debugParser_ = parserDebug;
373 }
374 
375 
377 (
378  const exprDriver& rhs
379 )
380 {
381  debugScanner_ = rhs.debugScanner_;
382  debugParser_ = rhs.debugParser_;
383 }
384 
385 
387 (
388  bool cacheReadFields,
389  bool searchInMemory,
390  bool searchFiles
391 )
392 {
393  searchInMemory_ = searchInMemory_ || cacheReadFields_;
394 
395  #ifdef FULLDEBUG
396  Info<< "Searching "
397  << " registry:" << searchInMemory_
398  << " disk:" << searchFiles_
399  << " cache-read:" << cacheReadFields_ << nl;
400  #endif
401 }
402 
403 
405 (
406  const exprDriver& rhs
407 )
408 {
409  setSearchBehaviour
410  (
411  rhs.cacheReadFields_,
412  rhs.searchInMemory_,
413  rhs.searchFiles_
414  );
415 }
416 
417 
418 // ************************************************************************* //
Foam::expressions::exprDriver::updateSpecialVariables
virtual void updateSpecialVariables(bool force=false)
Examine current variable values and update stored variables.
Definition: exprDriver.C:188
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:151
Foam::expressions::exprDriver::debugParser_
bool debugParser_
Request debugging for parser.
Definition: exprDriver.H:145
Foam::expressions::exprDriver::addVariables
void addVariables(const expressions::exprString &expr, bool clear=true)
Add/set string expressions for variables.
Definition: exprDriver.C:252
Foam::expressions::exprDriver::searchInMemory_
bool searchInMemory_
Search in registry before looking on disk.
Definition: exprDriver.H:157
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:1069
Foam::expressions::exprDriver::readDict
virtual bool readDict(const dictionary &dict)
Read variables, tables etc.
Definition: exprDriver.C:158
Foam::expressions::exprDriver::setSearchBehaviour
void setSearchBehaviour(bool cacheReadFields, bool searchInMemory, bool searchFiles)
Set search behaviour.
Definition: exprDriver.C:387
Foam::expressions::exprDriver::searchFiles_
bool searchFiles_
Search on disk (eg, for a standalone application)
Definition: exprDriver.H:160
Foam::expressions::exprDriver::evaluateVariable
void evaluateVariable(const word &varName, const expressions::exprString &expr)
Definition: exprDriver.C:212
Foam::expressions::exprString::toExpr
static exprString toExpr(const std::string &str)
Copy convert string to exprString.
Definition: exprStringI.H:143
Foam::stringOps::trim
string trim(const std::string &s)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:1048
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:350
Foam::expressions::exprDriver::debugScanner_
bool debugScanner_
Request debugging for scanner.
Definition: exprDriver.H:142
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:366
Foam::expressions::exprDriver::allowShadowing_
bool allowShadowing_
Allow variable names to mask field names.
Definition: exprDriver.H:148
Foam::expressions::exprDriver::setArgument
virtual void setArgument(const scalar val)
Set special-purpose scalar reference argument.
Definition: exprDriver.C:199
exprDriver.H
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:445
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:242
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
Foam::expressions::exprDriver::arg1Value_
scalar arg1Value_
Special-purpose scalar reference argument.
Definition: exprDriver.H:133
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:192
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:154
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:427
Foam::expressions::exprDriver::argValue
scalar argValue() const
Set special-purpose scalar reference argument.
Definition: exprDriver.C:205
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:359
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::expressions::exprResult::clear
void clear()
Clear (zero) the result.
Definition: exprResult.C:386
Foam::expressions::exprDriver::result
virtual const exprResult & result() const
Const access to expression result.
Definition: exprDriver.H:305
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:374
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::expressions::exprDriver::evaluateVariableRemote
virtual void evaluateVariableRemote(string remote, const word &varName, const expressions::exprString &expr)
Definition: exprDriver.C:230
Foam::expressions::exprDriver::clearResult
void clearResult()
Clear the result.
Definition: exprDriver.C:176
Foam::expressions::defineTypeNameAndDebug
defineTypeNameAndDebug(fvExprDriver, 0)
stringOps.H
Foam::expressions::exprDriver::update
virtual bool update()
Update things.
Definition: exprDriver.C:182
Foam::expressions::exprDriver::result_
exprResult result_
The result.
Definition: exprDriver.H:124
Foam::keyType::REGEX_RECURSIVE
Definition: keyType.H:79