31 #include "libmesh/parallel.h" 32 #include "libmesh/fparser.hh" 47 const hit::Node * n = &node;
50 const auto section = n->parent();
54 if (
const auto active = section->find(
"active");
55 active && active->type() == hit::NodeType::Field && active->parent() == section)
57 const auto vars = active->param<std::vector<std::string>>();
58 return std::find_if(
vars.begin(),
60 [&n](
const auto & var)
61 {
return n->path() == hit::pathNorm(var); }) !=
vars.end();
64 if (
const auto inactive = section->find(
"inactive");
65 inactive && inactive->type() == hit::NodeType::Field && inactive->parent() == section)
67 const auto vars = inactive->param<std::vector<std::string>>();
68 return std::find_if(
vars.begin(),
70 [&n](
const auto & var)
71 {
return n->path() == hit::pathNorm(var); }) ==
vars.end();
79 std::vector<std::string>
80 findSimilar(
const std::string & param,
const std::vector<std::string> & options)
82 std::vector<std::string> candidates;
83 if (options.size() == 0)
87 for (
const auto &
opt : options)
91 const int dist_cutoff = 1 + param.size() / 5;
92 if (dist > dist_cutoff || dist > mindist)
100 candidates.push_back(
opt);
108 _factory(app.getFactory()),
109 _action_wh(action_wh),
110 _action_factory(app.getActionFactory()),
111 _syntax(_action_wh.syntax()),
113 _root(_parser.getRoot()),
114 _syntax_formatter(nullptr)
129 params.
addParam<std::vector<std::string>>(
131 std::vector<std::string>({
"__all__"}),
132 "If specified only the blocks named will be visited and made active");
133 params.
addParam<std::vector<std::string>>(
135 std::vector<std::string>(),
136 "If specified blocks matching these identifiers will be skipped.");
141 std::vector<std::string>
148 std::vector<std::string> paramlist;
149 for (
auto it = iters.first; it != iters.second; ++it)
152 for (
const auto & it : params)
153 paramlist.push_back(it.first);
164 if (!
_used.count(fullpath) && nodename !=
"active" && nodename !=
"inactive" &&
167 auto section_name = fullpath.substr(0, fullpath.rfind(
"/"));
169 const auto candidates =
findSimilar(nodename, paramlist);
170 if (candidates.size() > 0)
172 "unused parameter '" + fullpath +
"'; did you mean '" + candidates[0] +
"'?", n);
174 errors.emplace_back(
"unused parameter '" + fullpath +
"'", n);
182 return (strip_leading_path ? path.filename() : std::filesystem::absolute(path)).
string();
191 const std::string section_name = n->fullpath();
192 const std::string curr_identifier = n->fullpath();
206 if (
const auto [begin, end] =
_syntax.
getActions(registered_identifier); begin == end)
209 "section '[" + curr_identifier +
210 "]' does not have an associated Action; you likely misspelled the Action/section name " 211 "or the app you are running does not support this Action/syntax",
218 std::set<const Syntax::ActionInfo *> processed_actions;
224 for (; it != end && processed_actions.count(&it->second); ++it)
232 processed_actions.insert(&it->second);
247 params.
set<std::string>(
"task") = it->second._task;
248 params.
set<std::string>(
"registered_identifier") = registered_identifier;
250 if (!(params.
have_parameter<
bool>(
"isObjectAction") && params.
get<
bool>(
"isObjectAction")))
251 params.
set<std::vector<std::string>>(
"control_tags")
255 std::shared_ptr<Action> action_obj =
260 std::shared_ptr<MooseObjectAction> object_action =
267 object_params.set<std::vector<std::string>>(
"control_tags")
271 std::shared_ptr<AddActionComponentAction> component_action =
273 if (component_action)
278 component_params.set<std::vector<std::string>>(
"control_tags")
289 Builder::walk(
const std::string & fullpath,
const std::string & nodepath, hit::Node * n)
296 walkRaw(fullpath, nodepath, n);
307 const auto need_action_syntax_first = [
this](
const auto & action_name)
310 mooseAssert(syntax.size(),
"Empty syntax");
311 std::copy(syntax.begin(), syntax.end(), std::back_inserter(
_secs_need_first));
317 need_action_syntax_first(
"SetupDebugAction");
322 need_action_syntax_first(
"GlobalParamsAction");
327 need_action_syntax_first(
"DynamicObjectRegistrationAction");
331 if (
auto n =
_root.find(sec))
332 walkRaw(n->parent()->fullpath(), n->path(), n);
335 _root.walk(
this, hit::NodeType::Section);
340 std::vector<std::string> messages;
342 messages.push_back(key_message_pair.second);
364 if (!warn_unused && !err_unused)
367 std::vector<hit::ErrorMessage> messages;
382 messages.emplace_back(
"unused command line parameter '" + arg +
"'");
396 "\n\nAppend --allow-unused (or -w) on the command line to ignore unused parameters.");
413 mooseError(
"Unrecognized Syntax Formatter requested");
421 std::vector<std::pair<std::string, Syntax::ActionInfo>> all_names;
435 if (act_info.
_task ==
"")
438 all_names.push_back(std::make_pair(iter.first, act_info));
442 for (
const auto & act_names : all_names)
444 const auto & act_info = act_names.second;
445 const std::string & action = act_info._action;
446 const std::string & task = act_info._task;
447 const std::string syntax = act_names.first;
461 for (
auto & t : tasks)
474 action_obj_params.
get<
bool>(
"isObjectAction"))
478 auto moose_obj_params = obj->buildParameters();
482 const std::vector<std::string> & buildable_types = action_obj_params.
getBuildableTypes();
485 if ((buildable_types.empty() ||
486 std::find(buildable_types.begin(), buildable_types.end(), moose_obj_name) !=
487 buildable_types.end()) &&
488 moose_obj_params.hasBase() &&
496 bool is_action_params =
false;
497 bool is_type =
false;
498 if (syntax[syntax.size() - 1] ==
'*')
503 name = syntax.substr(0, pos - 1) + moose_obj_name;
506 name = syntax.substr(0, pos - 1) +
"/<type>/" + moose_obj_name;
507 is_action_params =
true;
512 name = syntax +
"/<type>/" + moose_obj_name;
515 moose_obj_params.set<std::string>(
"type") = moose_obj_name;
532 if (syntax !=
"ActionComponents/*")
537 for (
auto it = iters.first; it != iters.second; ++it)
540 const auto component_name = it->second;
553 if (syntax[syntax.size() - 1] ==
'*')
555 size_t pos = syntax.size();
556 name = syntax.substr(0, pos - 1) + component_name;
558 component_params.set<std::string>(
"type") = component_name;
566 "AddActionComponentAction",
581 std::vector<std::pair<std::string, Syntax::ActionInfo>> all_names;
591 if (act_info.
_task ==
"")
594 all_names.push_back(std::pair<std::string, Syntax::ActionInfo>(iter.first, act_info));
597 for (
const auto & act_names : all_names)
601 act_names.first, act_names.second._action,
true, &action_obj_params);
603 const std::string & task = act_names.second._task;
604 std::string act_name = act_names.first;
612 action_obj_params.
get<
bool>(
"isObjectAction"))
616 auto moose_obj_params = obj->buildParameters();
621 const std::vector<std::string> & buildable_types = action_obj_params.
getBuildableTypes();
624 if ((buildable_types.empty() ||
625 std::find(buildable_types.begin(), buildable_types.end(), moose_obj_name) !=
626 buildable_types.end()) &&
627 moose_obj_params.hasBase() &&
635 bool is_action_params =
false;
636 if (act_name[act_name.size() - 1] ==
'*')
638 pos = act_name.size();
641 name = act_name.substr(0, pos - 1) + moose_obj_name;
644 name = act_name.substr(0, pos - 1) +
"/<type>/" + moose_obj_name;
645 is_action_params =
true;
650 name = act_name +
"/<type>/" + moose_obj_name;
653 moose_obj_params.set<std::string>(
"type") = moose_obj_name;
669 mooseAssert(section_node->type() == hit::NodeType::Section,
"Node type should be a section");
671 for (
const auto & [
name, par_unique_ptr] : p)
673 if (p.shouldIgnore(
name))
676 const hit::Node * param_node =
nullptr;
678 for (
const auto & param_name : p.paramAliases(
name))
684 if (
const auto section_param_node = section_node->find(param_name);
685 section_param_node && section_param_node->type() == hit::NodeType::Field &&
686 section_param_node->parent() == section_node)
687 param_node = section_param_node;
693 global_node && global_node->type() == hit::NodeType::Field &&
696 mooseAssert(
isGlobal(*global_node),
"Could not detect global-ness");
697 param_node = global_node;
704 const auto fullpath = param_node->fullpath();
705 p.setHitNode(param_name, *param_node, {});
706 p.set_attributes(param_name,
false);
709 const auto global =
isGlobal(*param_node);
713 if (
const auto deprecated_message = p.queryDeprecatedParamMessage(param_name))
715 std::string key =
"";
716 if (
const auto object_type_ptr = p.queryObjectType())
717 key += *object_type_ptr +
"_";
723 if (p.isPrivate(param_name))
726 if (!global && std::find_if(
_errors.begin(),
728 [¶m_node](
const auto &
err)
729 {
return err.node == param_node; }) ==
_errors.end())
730 _errors.emplace_back(
"parameter '" + fullpath +
"' is private and cannot be set",
736 const auto param_field =
dynamic_cast<const hit::Field *
>(param_node);
737 mooseAssert(param_field,
"Is not a field");
738 bool set_param =
false;
744 catch (hit::Error & e)
746 _errors.emplace_back(e.message, param_node);
748 catch (std::exception & e)
750 _errors.emplace_back(e.what(), param_node);
758 if (
auto cast_par =
dynamic_cast<InputParameters::Parameter<std::vector<VariableName>
> *>(
759 par_unique_ptr.get()))
760 if (
const auto error = p.setupVariableNames(cast_par->set(), *param_node, {}))
761 _errors.emplace_back(*error, param_node);
764 if (p.isRangeChecked(param_node->path()))
765 if (
const auto error = p.parameterRangeCheck(
766 *par_unique_ptr, param_node->fullpath(), param_node->path(),
true))
767 _errors.emplace_back(error->second, param_node);
780 if (
auto out_par_ptr =
781 dynamic_cast<InputParameters::Parameter<OutFileBase> *
>(par_unique_ptr.get()))
784 mooseAssert(input_file_name.size(),
"Input Filename is empty");
785 const auto pos = input_file_name.find_last_of(
'.');
786 mooseAssert(pos != std::string::npos,
"Unable to determine suffix of input file name");
787 out_par_ptr->set() = input_file_name.substr(0, pos) +
"_out";
788 p.set_attributes(
name,
false);
794 for (
const auto & [param_name, base_name_num_repeat_pair] : p.getAutoBuildVectors())
796 const auto & [base_name, num_repeat] = base_name_num_repeat_pair;
799 if (!p.isParamValid(param_name) && p.isParamValid(base_name) && p.isParamValid(num_repeat))
801 const auto vec_size = p.get<
unsigned int>(num_repeat);
802 const std::string &
name = p.get<std::string>(base_name);
804 std::vector<VariableName> variable_names(vec_size);
807 std::ostringstream oss;
809 variable_names[i] = oss.str();
813 p.set<std::vector<VariableName>>(param_name) = variable_names;
821 const auto node =
_root.find(prefix);
822 extractParams((node && node->type() == hit::NodeType::Section) ? node :
nullptr, p);
829 return global_params_node && node.parent() == global_params_node;
838 mooseAssert(syntax.size() == 1,
"Unexpected GlobalParamsAction syntax size");
std::string name(const ElemQuality q)
void build()
Parse an input file (or text string if provided) consisting of hit syntax and setup objects in the MO...
void addSyntaxType(const std::string &path, const std::string type)
Add an associated type to a block.
ActionFactory & _action_factory
The Factory that builds actions.
MooseApp & _app
The MooseApp this Parser is part of.
const std::multimap< std::string, std::string > & getAssociatedTypes() const
Get a multimap of registered associations of syntax with type.
void walk(const std::string &fullpath, const std::string &nodename, hit::Node *n) override
bool show_trace
Set to true (the default) to print the stack trace with error and warning messages - false to omit it...
std::pair< std::multimap< std::string, ActionInfo >::const_iterator, std::multimap< std::string, ActionInfo >::const_iterator > getActions(const std::string &syntax) const
Returns a pair of multimap iterators to all the ActionInfo objects associated with a given piece of s...
void walkRaw(std::string fullpath, std::string nodepath, hit::Node *n)
void parseError(std::vector< hit::ErrorMessage > messages) const
Helper for throwing an error with the given messages.
Parser & _parser
The front parser.
InputParameters & getComponentParams()
Return the parameters of the component.
const std::multimap< std::string, ActionInfo > & getAssociatedActions() const
Return all Syntax to Action associations.
void buildFullTree(const std::string &search_string)
Use MOOSE Factories to construct a full parse tree for documentation or echoing input.
void set(libMesh::Parameters::Value &value, const hit::Field &field) const
Sets a parameter value given a hit field.
InputParameters getValidParams(const std::string &name)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
void addActionTask(const std::string &path, const std::string &action, const std::string &task, const FileLineInfo &lineinfo)
Add a task to the tree.
std::shared_ptr< CommandLine > commandLine() const
Get the command line.
std::vector< std::string > _secs_need_first
The sections that we need to execute first (read during the final walk)
std::string associatedClassName(const std::string &name) const
Get the associated class name for an object name.
std::pair< std::multimap< std::string, std::string >::const_iterator, std::multimap< std::string, std::string >::const_iterator > getActionsByTask(const std::string &task) const
Returns begin and end iterators in a multimap from tasks to actions names.
std::set< std::string > getTasksByAction(const std::string &action) const
Builder(MooseApp &app, ActionWarehouse &action_wh, Parser &parser)
FileLineInfo getLineInfo(const std::string &syntax, const std::string &action, const std::string &task) const
Gets the file and line where the syntax/action/task combo was registered.
static InputParameters validParams()
Parameters that are processed directly by the Parser and are valid anywhere in the input...
Base class for MOOSE-based applications.
Storage for action instances.
void addActionBlock(std::shared_ptr< Action > blk)
This method add an Action instance to the warehouse.
FileLineInfo getLineInfo(const std::string &name, const std::string &task) const
Gets file and line information where an action was registered.
const std::set< std::string > & getExtractedVars() const
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
These are reworked from https://stackoverflow.com/a/11003103.
Action for creating component actions.
std::unique_ptr< SyntaxTree > _syntax_formatter
Object for holding the syntax parse tree.
Holds the syntax in a Json::Value tree.
FileLineInfo getLineInfo(const std::string &name) const
Gets file and line information where an object was initially registered.
std::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters ¶meters)
void extractParams(const hit::Node *const section_node, InputParameters &p)
Attempt to extract values from input starting with the section in input in section_node based on the ...
std::vector< std::string > getSyntaxByAction(const std::string &action, const std::string &task="")
Retrieve the syntax associated with the passed in action type string.
static void appendErrorMessages(std::vector< hit::ErrorMessage > &to, const std::vector< hit::ErrorMessage > &from)
Helper for accumulating errors from a walker into an accumulation of errors.
bool isDeprecatedSyntax(const std::string &syntax) const
Returns a Boolean indicating whether the syntax has been deprecated through a call to deprecateAction...
static InputParameters validParams()
InputParameters & getObjectParams()
Retrieve the parameters of the object to be created by this action.
hit::Node & _root
The root node from the Parser.
std::set< std::string > _extracted_vars
The set of all variables extracted from the input file.
bool addParameters(const std::string &parent_path, const std::string &path, bool is_type, const std::string &action, bool is_action, InputParameters *params, const FileLineInfo &lineinfo, const std::string &classname)
Add parameters to the tree.
static std::string joinErrorMessages(const std::vector< hit::ErrorMessage > &error_messages)
Helper for combining error messages into a single, newline separated message.
void mooseUnused(Args &&... args)
Warning message used to notify the users of unused parts of their input files Really used internally ...
void initSyntaxFormatter(SyntaxFormatterType type, bool dump_mode)
Creates a syntax formatter for printing.
bool getThrowOnError() const
static InputParameters validParams()
An inteface for the _console for outputting to the Console object.
void buildJsonSyntaxTree(JsonSyntaxTree &tree) const
Use MOOSE Factories to construct a parameter tree for documentation or echoing input.
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
void errorCheck(const libMesh::Parallel::Communicator &comm, bool warn_unused, bool err_unused)
const auto & registeredObjects() const
Returns a reference to the map from names to RegistryEntryBase pointers.
std::string getPrimaryFileName(bool stripLeadingPath=true) const
Return the primary (first) filename that was parsed.
std::vector< hit::ErrorMessage > errors
bool isGlobal(const hit::Node &node) const
void mooseDeprecatedStream(S &oss, const bool expired, const bool print_title, Args &&... args)
std::vector< hit::ErrorMessage > _errors
The errors accumulated during the walk.
std::string isAssociated(const std::string &real_id, bool *is_parent, const std::map< std::string, std::set< std::string >> &alt_map={}) const
Method for determining whether a piece of syntax is associated with an Action an optional syntax map ...
std::string getTaskName(const std::string &action)
std::map< std::string, std::string > _deprecated_params
Deprecation warnings (object type/param name) -> (message)
std::string baseName(const std::string &name)
Function for string the information before the final / in a parser block.
Syntax & _syntax
Reference to an object that defines input file syntax.
std::set< std::string > _used
const hit::Node & getCommandLineRoot() const
void addGlobal()
Add the global section to the output.
int levenshteinDist(const std::string &s1, const std::string &s2)
Computes and returns the Levenshtein distance between strings s1 and s2.
bool verifyMooseObjectTask(const std::string &base, const std::string &task) const
Returns a Boolean indicating whether a task is associated with on of the MOOSE pluggable systems (BAS...
std::vector< std::string > findSimilar(const std::string ¶m, const std::vector< std::string > &options)
Factory & _factory
The Factory associated with that MooseApp.
std::vector< std::string > listValidParams(std::string §ion_name)
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
std::filesystem::path getLastInputFilePath() const
ActionWarehouse & _action_wh
Action warehouse that will be filled by actions.
std::string deprecatedActionSyntaxMessage(const std::string syntax)
Returns the deprecation message for a given syntax that has been deprecated by deprecateActionSyntax...
const hit::Node * queryGlobalParamsNode() const
Get the [GlobalParams] section node if it exists.
std::string stringJoin(const std::vector< std::string > &values, const std::string &separator=" ")
Concatenates value into a single string separated by separator.
auto index_range(const T &sizable)
Class for parsing input files.
static ParameterRegistry & get()
Get the singleton registry.
std::optional< const hit::Node * > _global_params_node
The hit Node for the [GlobalParams] block, if any If set (could be null), it means we have searched f...
bool isSectionActive(const hit::Node &node)
void walk(const std::string &fullpath, const std::string &nodepath, hit::Node *n)