LCOV - code coverage report
Current view: top level - src/actions - ActionFactory.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 64 78 82.1 %
Date: 2025-07-17 01:28:37 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             : 
      16       62755 : ActionFactory::ActionFactory(MooseApp & app) : _app(app) {}
      17             : 
      18       57254 : ActionFactory::~ActionFactory() {}
      19             : 
      20             : void
      21    13097524 : ActionFactory::reg(std::shared_ptr<RegistryEntryBase> obj)
      22             : {
      23    13097524 :   const std::string & name = obj->_classname;
      24    13097524 :   const std::string & task = obj->_name;
      25             : 
      26    13097524 :   auto key = std::make_pair(name, task);
      27    13097524 :   if (_current_objs.count(key) > 0)
      28       62540 :     return;
      29    13034984 :   _current_objs.insert(key);
      30             : 
      31    13034984 :   BuildInfo build_info{obj, task};
      32    13034984 :   _name_to_build_info.insert(std::make_pair(name, build_info));
      33    13034984 :   _task_to_action_map.insert(std::make_pair(task, name));
      34    13034984 :   _tasks.insert(task);
      35    13034984 :   _name_to_line.addInfo(name, task, obj->_file, obj->_line);
      36    13097524 : }
      37             : 
      38             : std::shared_ptr<Action>
      39     3008925 : ActionFactory::create(const std::string & action,
      40             :                       const std::string & full_action_name,
      41             :                       InputParameters & incoming_parser_params)
      42             : {
      43     3008925 :   std::string action_name = MooseUtils::shortName(full_action_name);
      44     3008925 :   incoming_parser_params.addPrivateParam("_moose_app", &_app);
      45     3008925 :   incoming_parser_params.addPrivateParam("action_type", action);
      46     3008925 :   std::pair<ActionFactory::iterator, ActionFactory::iterator> iters;
      47             : 
      48             :   std::string unique_action_name =
      49     3008925 :       action + incoming_parser_params.get<std::string>("task") + full_action_name;
      50             :   // Create the actual parameters object that the object will reference
      51     3008925 :   InputParameters & action_params = _app.getInputParameterWarehouse().addInputParameters(
      52             :       unique_action_name, incoming_parser_params, 0, {});
      53             : 
      54     3008925 :   if (!action_params.getHitNode())
      55             :   {
      56             :     // If we currently are in an action, that means that we're creating an
      57             :     // action from within an action. Associate the action creating this one
      58             :     // with the new action's parameters so that errors can be associated with it
      59     1595740 :     if (const auto hit_node = _app.getCurrentActionHitNode())
      60      225791 :       action_params.setHitNode(*hit_node, {});
      61             :     // Don't have one, so just use the root
      62             :     else
      63     1369949 :       action_params.setHitNode(*_app.parser().root(), {});
      64             :   }
      65             : 
      66             :   // Check and finalize the parameters
      67     3008925 :   action_params.finalize(action_name);
      68             : 
      69     3008917 :   iters = _name_to_build_info.equal_range(action);
      70     3008917 :   BuildInfo * build_info = &(iters.first->second);
      71     3008917 :   if (!build_info)
      72           0 :     mooseError(
      73           0 :         std::string("Unable to find buildable Action from supplied InputParameters Object for ") +
      74             :         action_name);
      75             : 
      76             :   // Add the name to the parameters
      77     3008917 :   action_params.set<std::string>("_action_name") = action_name;
      78     3008917 :   action_params.set<std::string>("_unique_action_name") = unique_action_name;
      79             : 
      80             :   // Create the object
      81     3008917 :   _currently_constructing.push_back(&action_params);
      82     3008917 :   std::shared_ptr<Action> action_obj = build_info->_obj_pointer->buildAction(action_params);
      83     3008807 :   _currently_constructing.pop_back();
      84             : 
      85     3008807 :   if (action_params.get<std::string>("task") == "")
      86     2584167 :     action_obj->appendTask(build_info->_task);
      87             : 
      88     6017614 :   return action_obj;
      89     3008807 : }
      90             : 
      91             : InputParameters
      92     2971933 : ActionFactory::getValidParams(const std::string & name)
      93             : {
      94             :   /**
      95             :    * If an Action is registered more than once, it'll appear in the _name_to_build_info data
      96             :    * structure multiple times.  The actual parameters function remains the same however
      97             :    * so we can safely use the first instance
      98             :    */
      99     2971933 :   ActionFactory::iterator iter = _name_to_build_info.find(name);
     100             : 
     101     2971933 :   if (iter == _name_to_build_info.end())
     102           0 :     mooseError(std::string("A '") + name + "' is not a registered Action\n\n");
     103             : 
     104     2971933 :   InputParameters params = iter->second._obj_pointer->buildParameters();
     105     2971933 :   params.addPrivateParam("_moose_app", &_app);
     106     2971933 :   params.addPrivateParam<ActionWarehouse *>("awh", &_app.actionWarehouse());
     107             : 
     108     5943866 :   return params;
     109           0 : }
     110             : 
     111             : std::string
     112       18991 : ActionFactory::getTaskName(const std::string & action)
     113             : {
     114             :   // We are returning only the first found instance here
     115       18991 :   std::multimap<std::string, BuildInfo>::iterator iter = _name_to_build_info.find(action);
     116             : 
     117       18991 :   if (iter != _name_to_build_info.end())
     118       18991 :     return iter->second._task;
     119             :   else
     120           0 :     return "";
     121             : }
     122             : 
     123             : ActionFactory::iterator
     124           0 : ActionFactory::begin()
     125             : {
     126           0 :   return _name_to_build_info.begin();
     127             : }
     128             : 
     129             : ActionFactory::const_iterator
     130           0 : ActionFactory::begin() const
     131             : {
     132           0 :   return _name_to_build_info.begin();
     133             : }
     134             : 
     135             : ActionFactory::iterator
     136           0 : ActionFactory::end()
     137             : {
     138           0 :   return _name_to_build_info.end();
     139             : }
     140             : 
     141             : ActionFactory::const_iterator
     142           0 : ActionFactory::end() const
     143             : {
     144           0 :   return _name_to_build_info.end();
     145             : }
     146             : 
     147             : std::pair<std::multimap<std::string, std::string>::const_iterator,
     148             :           std::multimap<std::string, std::string>::const_iterator>
     149     1409067 : ActionFactory::getActionsByTask(const std::string & task) const
     150             : {
     151     1409067 :   return _task_to_action_map.equal_range(task);
     152             : }
     153             : 
     154             : std::set<std::string>
     155     2604541 : ActionFactory::getTasksByAction(const std::string & action) const
     156             : {
     157     2604541 :   std::set<std::string> tasks;
     158             : 
     159             :   std::pair<std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator,
     160             :             std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator>
     161     2604541 :       iters = _name_to_build_info.equal_range(action);
     162     2604541 :   for (std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator it = iters.first;
     163     6875493 :        it != iters.second;
     164     4270952 :        ++it)
     165     4270952 :     tasks.insert(it->second._task);
     166             : 
     167     5209082 :   return tasks;
     168           0 : }
     169             : 
     170             : const InputParameters *
     171     3008917 : ActionFactory::currentlyConstructing() const
     172             : {
     173     3008917 :   return _currently_constructing.size() ? _currently_constructing.back() : nullptr;
     174             : }
     175             : 
     176             : FileLineInfo
     177       30655 : ActionFactory::getLineInfo(const std::string & name, const std::string & task) const
     178             : {
     179       30655 :   return _name_to_line.getInfo(name, task);
     180             : }

Generated by: LCOV version 1.14