LCOV - code coverage report
Current view: top level - src/actions - ActionFactory.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 1f9d31 Lines: 64 78 82.1 %
Date: 2025-09-02 20:01:20 Functions: 10 15 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : // MOOSE includes
      11             : #include "ActionFactory.h"
      12             : #include "MooseApp.h"
      13             : #include "InputParameterWarehouse.h"
      14             : #include "MooseObjectAction.h"
      15             : #include "Parser.h"
      16             : 
      17       68460 : ActionFactory::ActionFactory(MooseApp & app) : _app(app) {}
      18             : 
      19       62854 : ActionFactory::~ActionFactory() {}
      20             : 
      21             : void
      22    14390843 : ActionFactory::reg(std::shared_ptr<RegistryEntryBase> obj)
      23             : {
      24    14390843 :   const std::string & name = obj->_classname;
      25    14390843 :   const std::string & task = obj->_name;
      26             : 
      27    14390843 :   auto key = std::make_pair(name, task);
      28    14390843 :   if (_current_objs.count(key) > 0)
      29       68246 :     return;
      30    14322597 :   _current_objs.insert(key);
      31             : 
      32    14322597 :   BuildInfo build_info{obj, task};
      33    14322597 :   _name_to_build_info.insert(std::make_pair(name, build_info));
      34    14322597 :   _task_to_action_map.insert(std::make_pair(task, name));
      35    14322597 :   _tasks.insert(task);
      36    14322597 :   _name_to_line.addInfo(name, task, obj->_file, obj->_line);
      37    14390843 : }
      38             : 
      39             : std::shared_ptr<Action>
      40     3316102 : ActionFactory::create(const std::string & action,
      41             :                       const std::string & full_action_name,
      42             :                       InputParameters & incoming_parser_params)
      43             : {
      44     3316102 :   std::string action_name = MooseUtils::shortName(full_action_name);
      45     3316102 :   incoming_parser_params.addPrivateParam(MooseBase::app_param, &_app);
      46     3316102 :   incoming_parser_params.set<std::string>(MooseBase::type_param) = action;
      47             : 
      48     3316102 :   std::pair<ActionFactory::iterator, ActionFactory::iterator> iters;
      49             : 
      50             :   const std::string unique_action_name =
      51     3316102 :       action + incoming_parser_params.get<std::string>("task") + full_action_name;
      52             : 
      53             :   // Create the actual parameters object that the object will reference
      54     3316102 :   InputParameters & action_params = _app.getInputParameterWarehouse().addInputParameters(
      55             :       unique_action_name, incoming_parser_params, 0, {});
      56             : 
      57     3316102 :   if (!action_params.getHitNode())
      58             :   {
      59             :     // If we currently are in an action, that means that we're creating an
      60             :     // action from within an action. Associate the action creating this one
      61             :     // with the new action's parameters so that errors can be associated with it
      62     1769273 :     if (const auto hit_node = _app.getCurrentActionHitNode())
      63      246528 :       action_params.setHitNode(*hit_node, {});
      64             :     // Don't have one, so just use the root
      65             :     else
      66     1522745 :       action_params.setHitNode(_app.parser().getRoot(), {});
      67             :   }
      68             : 
      69     3316102 :   action_params.set<std::string>(Action::unique_action_name_param) = unique_action_name;
      70     3316102 :   action_params.set<std::string>(MooseBase::name_param) = action_name;
      71             : 
      72             :   // Check and finalize the parameters
      73     3316102 :   action_params.finalize(action_name);
      74             : 
      75     3316094 :   iters = _name_to_build_info.equal_range(action);
      76     3316094 :   BuildInfo * build_info = &(iters.first->second);
      77     3316094 :   if (!build_info)
      78           0 :     mooseError(
      79           0 :         std::string("Unable to find buildable Action from supplied InputParameters Object for ") +
      80             :         action_name);
      81             : 
      82             :   // Create the object
      83     3316094 :   _currently_constructing.push_back(&action_params);
      84     3316094 :   std::shared_ptr<Action> action_obj = build_info->_obj_pointer->buildAction(action_params);
      85     3315984 :   _currently_constructing.pop_back();
      86             : 
      87     3315984 :   if (action_params.get<std::string>("task") == "")
      88     2851815 :     action_obj->appendTask(build_info->_task);
      89             : 
      90     6631968 :   return action_obj;
      91     3315984 : }
      92             : 
      93             : InputParameters
      94     3274985 : ActionFactory::getValidParams(const std::string & name)
      95             : {
      96             :   /**
      97             :    * If an Action is registered more than once, it'll appear in the _name_to_build_info data
      98             :    * structure multiple times.  The actual parameters function remains the same however
      99             :    * so we can safely use the first instance
     100             :    */
     101     3274985 :   ActionFactory::iterator iter = _name_to_build_info.find(name);
     102             : 
     103     3274985 :   if (iter == _name_to_build_info.end())
     104           0 :     mooseError(std::string("A '") + name + "' is not a registered Action\n\n");
     105             : 
     106     3274985 :   InputParameters params = iter->second._obj_pointer->buildParameters();
     107     3274985 :   params.addPrivateParam(MooseBase::app_param, &_app);
     108     6549970 :   params.addPrivateParam<ActionWarehouse *>("awh", &_app.actionWarehouse());
     109             : 
     110     6549970 :   return params;
     111           0 : }
     112             : 
     113             : std::string
     114       18991 : ActionFactory::getTaskName(const std::string & action)
     115             : {
     116             :   // We are returning only the first found instance here
     117       18991 :   std::multimap<std::string, BuildInfo>::iterator iter = _name_to_build_info.find(action);
     118             : 
     119       18991 :   if (iter != _name_to_build_info.end())
     120       18991 :     return iter->second._task;
     121             :   else
     122           0 :     return "";
     123             : }
     124             : 
     125             : ActionFactory::iterator
     126           0 : ActionFactory::begin()
     127             : {
     128           0 :   return _name_to_build_info.begin();
     129             : }
     130             : 
     131             : ActionFactory::const_iterator
     132           0 : ActionFactory::begin() const
     133             : {
     134           0 :   return _name_to_build_info.begin();
     135             : }
     136             : 
     137             : ActionFactory::iterator
     138           0 : ActionFactory::end()
     139             : {
     140           0 :   return _name_to_build_info.end();
     141             : }
     142             : 
     143             : ActionFactory::const_iterator
     144           0 : ActionFactory::end() const
     145             : {
     146           0 :   return _name_to_build_info.end();
     147             : }
     148             : 
     149             : std::pair<std::multimap<std::string, std::string>::const_iterator,
     150             :           std::multimap<std::string, std::string>::const_iterator>
     151     1611327 : ActionFactory::getActionsByTask(const std::string & task) const
     152             : {
     153     1611327 :   return _task_to_action_map.equal_range(task);
     154             : }
     155             : 
     156             : std::set<std::string>
     157     2872240 : ActionFactory::getTasksByAction(const std::string & action) const
     158             : {
     159     2872240 :   std::set<std::string> tasks;
     160             : 
     161             :   std::pair<std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator,
     162             :             std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator>
     163     2872240 :       iters = _name_to_build_info.equal_range(action);
     164     2872240 :   for (std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator it = iters.first;
     165     7630415 :        it != iters.second;
     166     4758175 :        ++it)
     167     4758175 :     tasks.insert(it->second._task);
     168             : 
     169     5744480 :   return tasks;
     170           0 : }
     171             : 
     172             : const InputParameters *
     173     3316094 : ActionFactory::currentlyConstructing() const
     174             : {
     175     3316094 :   return _currently_constructing.size() ? _currently_constructing.back() : nullptr;
     176             : }
     177             : 
     178             : FileLineInfo
     179       30655 : ActionFactory::getLineInfo(const std::string & name, const std::string & task) const
     180             : {
     181       30655 :   return _name_to_line.getInfo(name, task);
     182             : }

Generated by: LCOV version 1.14