22 list.insert(list.end(), rargs.begin(), rargs.end());
37 if (term.
_root != NULL)
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)
207 ExpressionBuilder::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];
234 return _term.substitute(rule);
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();
User facing host object for an expression tree.
virtual std::string stringify() const
unsigned int substitute(const EBSubstitutionRule &rule)
void mooseError(Args &&... args)
virtual std::string stringify() const
BINARY_FUNC_IMPLEMENT(min, MIN)
virtual unsigned int substitute(const EBSubstitutionRuleList &rule)
Node representing a binary operator.
ExpressionBuilder::EBTermList operator,(const ExpressionBuilder::EBTerm &larg, const ExpressionBuilder::EBTerm &rarg)
virtual int precedence() const
const EBTermNode * getSubnode() const
Node representing a function with two arguments.
std::ostream & operator<<(std::ostream &os, const ExpressionBuilder::EBTerm &term)
virtual EBTermNode * clone() const =0
std::vector< EBTerm > EBTermList
std::basic_ostream< charT, traits > * os
EBFunction & operator()(const EBTerm &arg)
Base class for nodes in the expression tree.
Template class for leaf nodes holding symbols (i.e. variables) in the expression tree.
TERNARY_FUNC_IMPLEMENT(conditional, CONDITIONAL)
unsigned int substitute(const EBSubstitutionRule &rule)
virtual unsigned int substitute(const EBSubstitutionRuleList &rule)
virtual unsigned int substitute(const EBSubstitutionRuleList &)
Node representing a function with two arguments.
EBTermSubstitution(const EBTerm &find, const EBTerm &replace)
virtual EBTermNode * substitute(const EBUnaryFuncTermNode &) const
virtual unsigned int substitute(const EBSubstitutionRuleList &rule)
std::vector< const EBSubstitutionRule * > EBSubstitutionRuleList
UNARY_FUNC_IMPLEMENT(sin, SIN)
ExpressionBuilder::EBTerm pow(const ExpressionBuilder::EBTerm &left, T exponent)
const EBTermNode * getRoot() const
virtual std::string stringify() const
User facing host object for a function. This combines a term with an argument list.
virtual std::string stringify() const
virtual std::string stringify() const
std::string args()
get the list of arguments and check if they are all symbols
virtual std::string stringify() const
enum ExpressionBuilder::EBUnaryFuncTermNode::NodeType _type
virtual EBTermNode * substitute(const EBSymbolNode &) const
Generic Substitution rule to replace all occurences of a given symbol node term with a user defined t...
EBFunction & operator=(const EBTerm &)
Substitution rule functor base class to perform flexible term substitutions.
virtual std::string stringify() const
EBTermNode * cloneRoot() const