https://mooseframework.inl.gov
MooseBase.C
Go to the documentation of this file.
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 #include "MooseBase.h"
11 
12 #include "MooseApp.h"
13 #include "Moose.h"
14 #include "Factory.h"
16 #include "AppFactory.h"
17 
18 const std::string MooseBase::type_param = "_type";
19 const std::string MooseBase::name_param = "_object_name";
20 const std::string MooseBase::unique_name_param = "_unique_name";
21 const std::string MooseBase::app_param = "_moose_app";
22 const std::string MooseBase::moose_base_param = "_moose_base";
23 
26 {
28  params.addPrivateParam<std::string>(MooseBase::type_param); // The name of the class being built
29  params.addPrivateParam<std::string>(MooseBase::name_param); // The name passed the factory
30  params.addPrivateParam<std::string>(MooseBase::unique_name_param); // The unique name
31  return params;
32 }
33 
36  _app(app),
37  _type(params.getObjectType()),
38  _name(params.getObjectName()),
39  _pars(params)
40 {
41  mooseAssert(_type.size(), "Type is empty");
42  mooseAssert(_name.size(), "Name is empty");
43 
44  // This enforces that we call finalizeParams (checkParams() and setting file paths)
45  mooseAssert(_pars.isFinalized(), "Params are not finalized");
46 }
47 
49  : MooseBase(*params.getCheckedPointerParam<MooseApp *>(MooseBase::app_param), params)
50 {
51 }
52 
53 std::string
55 {
56  return type() + std::string(" \"") + name() + std::string("\"");
57 }
58 
60 MooseBase::uniqueParameterName(const std::string & parameter_name) const
61 {
62  return MooseObjectParameterName(getBase(), name(), parameter_name);
63 }
64 
67 {
68  if (!_pars.have_parameter<std::string>(unique_name_param))
69  mooseError("uniqueName(): Object does not have a unique name");
70  return MooseObjectName(_pars.get<std::string>(unique_name_param));
71 }
72 
73 void
74 MooseBase::connectControllableParams(const std::string & parameter,
75  const std::string & object_type,
76  const std::string & object_name,
77  const std::string & object_parameter) const
78 {
79  auto & factory = _app.getFactory();
80  auto & ip_warehouse = _app.getInputParameterWarehouse();
81 
82  MooseObjectParameterName primary_name(uniqueName(), parameter);
83  const auto base_type = factory.getValidParams(object_type).getBase();
84  MooseObjectParameterName secondary_name(base_type, object_name, object_parameter);
85  ip_warehouse.addControllableParameterConnection(primary_name, secondary_name);
86 
87  const auto & tags = _pars.get<std::vector<std::string>>("control_tags");
88  for (const auto & tag : tags)
89  {
90  if (!tag.empty())
91  {
92  // Only adds the parameter with the different control tags if the derived class
93  // properly registers the parameter to its own syntax
94  MooseObjectParameterName tagged_name(tag, name(), parameter);
95  ip_warehouse.addControllableParameterConnection(
96  tagged_name, secondary_name, /*error_on_empty=*/false);
97  }
98  }
99 }
100 
101 [[noreturn]] void
103  const bool with_prefix,
104  const hit::Node * node /* = nullptr */) const
105 {
106  callMooseError(&_app, _pars, msg, with_prefix, node);
107 }
108 
109 [[noreturn]] void
111  const InputParameters & params,
112  std::string msg,
113  const bool with_prefix,
114  const hit::Node * node)
115 {
116  if (!node)
117  node = MooseBase::getHitNode(params);
118 
119  std::string multiapp_prefix = "";
120  if (app)
121  {
122  if (!app->isUltimateMaster())
123  multiapp_prefix = app->name();
125  }
126 
127  if (with_prefix)
128  // False here because the hit context will get processed by the node
129  msg = messagePrefix(params, false) + msg;
130 
131  moose::internal::mooseErrorRaw(msg, multiapp_prefix, node);
132 }
133 
134 std::string
135 MooseBase::messagePrefix(const InputParameters & params, const bool hit_prefix)
136 {
137  std::string prefix = "";
138 
139  if (hit_prefix)
140  if (const auto node = MooseBase::getHitNode(params))
141  prefix += Moose::hitMessagePrefix(*node);
142 
143  // Don't have context without type and name
144  if (!params.isMooseBaseObject())
145  return prefix;
146 
147  const auto & name = params.getObjectName();
148  const std::string base = params.hasBase() ? params.getBase() : "object";
149  const bool is_main_app = base == "Application" && name == AppFactory::main_app_name;
150  prefix += "The following occurred in the ";
151  if (is_main_app)
152  prefix += "main " + base;
153  else
154  prefix += base;
155  if (base != params.getObjectName() && name.size() && !is_main_app)
156  prefix += " '" + name + "'";
157  prefix += " of type " + params.getObjectType() + ".";
158  return prefix + "\n\n";
159 }
160 
161 const hit::Node *
163 {
164  if (const auto hit_node = params.getHitNode())
165  if (!hit_node->isRoot())
166  return hit_node;
167  return nullptr;
168 }
const hit::Node * getHitNode(const std::string &param) const
static const std::string name_param
The name of the parameter that contains the object name.
Definition: MooseBase.h:55
static const std::string app_param
The name of the parameter that contains the MooseApp.
Definition: MooseBase.h:59
bool isUltimateMaster() const
Whether or not this app is the ultimate master app.
Definition: MooseApp.h:813
const InputParameters & _pars
The object&#39;s parameters.
Definition: MooseBase.h:362
void callMooseError(std::string msg, const bool with_prefix, const hit::Node *node=nullptr) const
External method for calling moose error with added object context.
Definition: MooseBase.C:102
const std::string & _name
The name of this class.
Definition: MooseBase.h:359
Base class for everything in MOOSE with a name and a type.
Definition: MooseBase.h:49
static const std::string type_param
The name of the parameter that contains the object type.
Definition: MooseBase.h:53
void addPrivateParam(const std::string &name, const T &value)
These method add a parameter to the InputParameters object which can be retrieved like any other para...
const std::string & getObjectName() const
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
static const std::string main_app_name
The name for the "main" moose application.
Definition: AppFactory.h:69
InputParameterWarehouse & getInputParameterWarehouse()
Get the InputParameterWarehouse for MooseObjects.
Definition: MooseApp.C:2900
Base class for MOOSE-based applications.
Definition: MooseApp.h:96
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
MooseObjectName uniqueName() const
Definition: MooseBase.C:66
bool hasBase() const
const std::string & getBase() const
MooseObjectParameterName uniqueParameterName(const std::string &parameter_name) const
Definition: MooseBase.C:60
static const std::string unique_name_param
The name of the parameter that contains the unique object name.
Definition: MooseBase.h:57
InputParameters emptyInputParameters()
Factory & getFactory()
Retrieve a writable reference to the Factory associated with this App.
Definition: MooseApp.h:394
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:99
static const std::string moose_base_param
The name of the parameter that contains the moose system base.
Definition: MooseBase.h:61
An inteface for the _console for outputting to the Console object.
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:89
std::string typeAndName() const
Get the class&#39;s combined type and name; useful in error handling.
Definition: MooseBase.C:54
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:353
bool isMooseBaseObject() const
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.
void mooseErrorRaw(std::string msg, const std::string &prefix="", const hit::Node *node=nullptr)
Main callback for emitting a moose error.
Definition: MooseError.C:53
const std::string & getObjectType() const
bool isFinalized() const
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:267
void mooseConsole()
Send current output buffer to Console output objects.
A class for storing an input parameter name.
const std::string & getBase() const
Definition: MooseBase.h:143
const hit::Node * getHitNode() const
Definition: MooseBase.h:132
std::string hitMessagePrefix(const hit::Node &node)
Get the prefix to be associated with a hit node for a message.
Definition: Moose.C:767
A class for storing the names of MooseObject by tag and object name.
const std::string & _type
The type of this class.
Definition: MooseBase.h:356
OutputWarehouse & getOutputWarehouse()
Get the OutputWarehouse objects.
Definition: MooseApp.C:2442
static InputParameters validParams()
Definition: MooseBase.C:25
void connectControllableParams(const std::string &parameter, const std::string &object_type, const std::string &object_name, const std::string &object_parameter) const
Connect controllable parameter of this action with the controllable parameters of the objects added b...
Definition: MooseBase.C:74
MooseBase(const InputParameters &params)
Primary constructor for general objects.
Definition: MooseBase.C:48
std::string messagePrefix(const bool hit_prefix=true) const
Definition: MooseBase.h:252