22 list.insert(list.end(), rargs.begin(), rargs.end());
37 if (term.
_root != NULL)
38 return os << *term.
_root;
40 return os <<
"[NULL]";
53 s <<
'[' << _id <<
']';
60 const char *
name[] = {
61 "sin",
"cos",
"tan",
"abs",
"log",
"log2",
"log10",
"exp",
"sinh",
"cosh",
"tanh"};
63 s <<
name[_type] <<
'(' << *_subnode <<
')';
70 const char *
name[] = {
"-",
"!"};
75 if (_subnode->precedence() > precedence())
76 s <<
'(' << *_subnode <<
')';
86 const char *
name[] = {
"min",
"max",
"atan2",
"hypot",
"plog"};
88 s <<
name[_type] <<
'(' << *_left <<
',' << *_right <<
')';
95 const char *
name[] = {
"+",
"-",
"*",
"/",
"%",
"^",
"<",
">",
"<=",
">=",
"=",
"!="};
98 if (_left->precedence() > precedence())
99 s <<
'(' << *_left <<
')';
107 if (_right->precedence() > precedence() ||
108 (_right->precedence() == precedence() &&
109 (_type == SUB || _type == DIV || _type == MOD || _type == POW)))
110 s <<
'(' << *_right <<
')';
147 const char *
name[] = {
"if"};
148 std::ostringstream s;
149 s <<
name[_type] <<
'(' << *_left <<
',' << *_middle <<
',' << *_right <<
')';
156 this->_eval_arguments = {arg};
170 this->_arguments = this->_eval_arguments;
178 this->_arguments = this->_eval_arguments;
179 this->_term =
EBTerm(func);
185 unsigned int narg = _arguments.size();
186 if (narg != _eval_arguments.size())
187 mooseError(
"EBFunction is used wth a different number of arguments than it was defined with.");
194 for (
unsigned i = 0; i < narg; ++i)
198 result.substitute(rules);
201 for (
unsigned i = 0; i < narg; ++i)
207ExpressionBuilder::EBFunction::operator std::string()
const
211 std::ostringstream s;
219 unsigned int narg = _arguments.size();
223 std::ostringstream s;
225 for (
unsigned int i = 1; i < narg; ++i)
226 s <<
',' << _arguments[i];
240 return _term.substitute(rules);
243#define UNARY_FUNC_IMPLEMENT(op, OP) \
244 ExpressionBuilder::EBTerm op(const ExpressionBuilder::EBTerm & term) \
246 mooseAssert(term._root != NULL, "Empty term provided as argument of function " #op "()"); \
247 return ExpressionBuilder::EBTerm(new ExpressionBuilder::EBUnaryFuncTermNode( \
248 term.cloneRoot(), ExpressionBuilder::EBUnaryFuncTermNode::OP)); \
262#define BINARY_FUNC_IMPLEMENT(op, OP) \
263 ExpressionBuilder::EBTerm op(const ExpressionBuilder::EBTerm & left, \
264 const ExpressionBuilder::EBTerm & right) \
266 mooseAssert(left._root != NULL, \
267 "Empty term provided as first argument of function " #op "()"); \
268 mooseAssert(right._root != NULL, \
269 "Empty term provided as second argument of function " #op "()"); \
270 return ExpressionBuilder::EBTerm(new ExpressionBuilder::EBBinaryFuncTermNode( \
271 left.cloneRoot(), right.cloneRoot(), ExpressionBuilder::EBBinaryFuncTermNode::OP)); \
283 mooseAssert(left.
_root != NULL,
"Empty term for base of pow()");
284 mooseAssert(right.
_root != NULL,
"Empty term for exponent of pow()");
289#define TERNARY_FUNC_IMPLEMENT(op, OP) \
290 ExpressionBuilder::EBTerm op(const ExpressionBuilder::EBTerm & left, \
291 const ExpressionBuilder::EBTerm & middle, \
292 const ExpressionBuilder::EBTerm & right) \
294 mooseAssert(left._root != NULL, \
295 "Empty term provided as first argument of the ternary function " #op "()"); \
296 mooseAssert(middle._root != NULL, \
297 "Empty term provided as second argument of the ternary function " #op "()"); \
298 mooseAssert(right._root != NULL, \
299 "Empty term provided as third argument of the ternary function " #op "()"); \
300 return ExpressionBuilder::EBTerm(new ExpressionBuilder::EBTernaryFuncTermNode( \
302 middle.cloneRoot(), \
304 ExpressionBuilder::EBTernaryFuncTermNode::OP)); \
311 unsigned int nrule = rules.size();
313 for (
unsigned int i = 0; i < nrule; ++i)
315 EBTermNode * replace = rules[i]->apply(_subnode);
324 return _subnode->substitute(rules);
330 unsigned int nrule = rules.size();
331 unsigned int success = 0;
333 for (
unsigned int i = 0; i < nrule; ++i)
335 EBTermNode * replace = rules[i]->apply(_left);
348 for (
unsigned int i = 0; i < nrule; ++i)
350 EBTermNode * replace = rules[i]->apply(_right);
365 unsigned int nrule = rules.size();
366 bool left_success =
false, middle_success =
false, right_success =
false;
369 for (
unsigned int i = 0; i < nrule; ++i)
371 replace = rules[i]->apply(_left);
381 for (
unsigned int i = 0; i < nrule; ++i)
383 replace = rules[i]->apply(_middle);
388 middle_success =
true;
393 for (
unsigned int i = 0; i < nrule; ++i)
395 replace = rules[i]->apply(_right);
400 right_success =
true;
408 middle_success = _middle->substitute(rules);
410 right_success = _right->substitute(rules);
412 return left_success + middle_success + right_success;
420 return substitute(rules);
426 unsigned int nrule = rules.size();
431 for (
unsigned int i = 0; i < nrule; ++i)
433 EBTermNode * replace = rules[i]->apply(_root);
450 if (find_root == NULL)
451 mooseError(
"Function arguments must be pure symbols.");
458 mooseError(
"Trying to substitute in an empty term for ", _find);
465 return _replace->
clone();
ExpressionBuilder::EBTermList operator,(const ExpressionBuilder::EBTerm &larg, const ExpressionBuilder::EBTerm &rarg)
UNARY_FUNC_IMPLEMENT(sin, SIN)
std::ostream & operator<<(std::ostream &os, const ExpressionBuilder::EBTerm &term)
TERNARY_FUNC_IMPLEMENT(conditional, CONDITIONAL)
BINARY_FUNC_IMPLEMENT(min, MIN)
ExpressionBuilder::EBTerm pow(const ExpressionBuilder::EBTerm &left, T exponent)
void mooseError(Args &&... args)
Node representing a function with two arguments.
virtual std::string stringify() const
Node representing a binary operator.
virtual int precedence() const
virtual std::string stringify() const
virtual unsigned int substitute(const EBSubstitutionRuleList &rule)
User facing host object for a function. This combines a term with an argument list.
std::string args()
get the list of arguments and check if they are all symbols
unsigned int substitute(const EBSubstitutionRule &rule)
EBFunction & operator=(const EBTerm &)
EBFunction & operator()(const EBTerm &arg)
virtual EBTermNode * substitute(const EBUnaryFuncTermNode &) const
Substitution rule functor base class to perform flexible term substitutions.
Template class for leaf nodes holding symbols (i.e. variables) in the expression tree.
virtual std::string stringify() const
virtual std::string stringify() const
Base class for nodes in the expression tree.
virtual EBTermNode * clone() const =0
virtual unsigned int substitute(const EBSubstitutionRuleList &)
Generic Substitution rule to replace all occurences of a given symbol node term with a user defined t...
virtual EBTermNode * substitute(const EBSymbolNode &) const
EBTermSubstitution(const EBTerm &find, const EBTerm &replace)
User facing host object for an expression tree.
unsigned int substitute(const EBSubstitutionRule &rule)
const EBTermNode * getRoot() const
EBTermNode * cloneRoot() const
virtual std::string stringify() const
virtual unsigned int substitute(const EBSubstitutionRuleList &rule)
Node representing a function with two arguments.
virtual std::string stringify() const
enum ExpressionBuilder::EBUnaryFuncTermNode::NodeType _type
virtual std::string stringify() const
const EBTermNode * getSubnode() const
virtual unsigned int substitute(const EBSubstitutionRuleList &rule)
std::vector< const EBSubstitutionRule * > EBSubstitutionRuleList
std::vector< EBTerm > EBTermList