https://mooseframework.inl.gov
Capability.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 "Capability.h"
11 #include "CapabilityException.h"
12 
13 #include "MooseStringUtils.h"
14 
15 #include <regex>
16 
17 namespace Moose
18 {
19 Capability::Capability(const std::string_view name,
20  const Capability::Value & value,
21  const std::string_view doc)
22  : _name(name), _doc(doc), _value(value), _explicit(false)
23 {
24  // Check name validity
25  if (getName().empty())
26  throw CapabilityException("Capability has empty name");
27  if (!std::regex_match(getName(), std::regex("[a-z0-9_-]+")))
28  throw CapabilityException(
29  "Capability '" + getName() +
30  "': Name has unallowed characters; allowed characters = 'a-z, 0-9, _, -'");
31 
32  // String value validity
33  if (const auto string_ptr = queryStringValue())
34  {
35  const std::regex string_regex("[a-z0-9_.-]+");
36  if (!std::regex_match(*string_ptr, string_regex))
37  throw CapabilityException(
38  "String capability '" + getName() + "': value '" + *string_ptr +
39  "' has unallowed characters; allowed characters = 'a-z, 0-9, _, ., -'");
40  }
41 }
42 
43 bool
44 Capability::hasEnumeration(const std::string & value) const
45 {
46  return _enumeration ? _enumeration->count(value) : true;
47 }
48 
49 const std::set<std::string> &
51 {
52  if (!_enumeration)
53  throw CapabilityException("Capability::getEnumeration(): Capability '" + getName() +
54  "' does not have an enumeration");
55  return *_enumeration;
56 }
57 
58 Capability &
60 {
61  if (hasBoolValue())
62  throw CapabilityException("Capability::setExplicit(): Capability '" + getName() +
63  "' is bool-valued and cannot be set as explicit");
64  _explicit = true;
65  return *this;
66 }
67 
68 Capability &
69 Capability::setEnumeration(const std::set<std::string> & enumeration)
70 {
71  static const std::string error_prefix = "Capability::setEnumeration(): ";
72 
73  const auto string_ptr = queryStringValue();
74  if (!string_ptr)
75  throw CapabilityException(error_prefix + "Capability '" + getName() +
76  "' is not string-valued and cannot have an enumeration");
77 
78  if (_enumeration)
79  {
80  if (*_enumeration == enumeration)
81  return *this;
82  throw CapabilityException(error_prefix + "Capability '" + getName() +
83  "' already has an enumeration set");
84  }
85 
86  if (enumeration.empty())
87  throw CapabilityException(error_prefix + "Enumeration is empty for '" + getName() + "'");
88 
89  for (const auto & value : enumeration)
90  if (!std::regex_match(value, std::regex("[a-z0-9_-]+")))
91  throw CapabilityException(error_prefix + "Enumeration value '" + value +
92  "' for capability '" + getName() + "'" +
93  " has unallowed characters; allowed characters = 'a-z, 0-9, _, -'");
94 
95  _enumeration = enumeration;
96 
97  if (!hasEnumeration(*string_ptr))
98  throw CapabilityException(error_prefix + "Capability " + toString() +
99  " value not within enumeration");
100 
101  return *this;
102 }
103 
104 bool
106 {
107  if (const auto bool_ptr = queryBoolValue())
108  return *bool_ptr;
109  throw CapabilityException("Capability::getBoolValue(): Capability " + toString() +
110  " is not a bool");
111 }
112 
113 int
115 {
116  if (const auto int_ptr = queryIntValue())
117  return *int_ptr;
118  throw CapabilityException("Capability::getIntValue(): Capability " + toString() +
119  " is not an integer");
120 }
121 
122 const std::string &
124 {
125  if (const auto string_ptr = queryStringValue())
126  return *string_ptr;
127  throw CapabilityException("Capability::getStringValue(): Capability " + toString() +
128  " is not a string");
129 }
130 
131 std::string
133 {
134  if (const auto bool_ptr = queryBoolValue())
135  return *bool_ptr ? "true" : "false";
136  if (const auto string_ptr = queryStringValue())
137  return *string_ptr;
138  if (const auto int_ptr = queryIntValue())
139  return std::to_string(*int_ptr);
140  throw CapabilityException("Capability::valueToString(): Invalid type");
141 }
142 
143 std::string
145 {
146  return getName() + "=" + valueToString();
147 }
148 
149 std::string
151 {
152  if (_enumeration)
153  return MooseUtils::stringJoin(
154  std::vector<std::string>(_enumeration->begin(), _enumeration->end()), ", ");
155  throw CapabilityException("Capability::enumerationToString(): Capability '",
156  getName(),
157  "' does not have an enumeration");
158 }
159 } // namespace Moose
std::string name(const ElemQuality q)
bool getBoolValue() const
Definition: Capability.C:105
int getIntValue() const
Definition: Capability.C:114
const std::set< std::string > & getEnumeration() const
Definition: Capability.C:50
const std::string * queryStringValue() const
Definition: Capability.h:125
const std::string & getStringValue() const
Definition: Capability.C:123
std::string toString() const
Definition: Capability.C:144
const int * queryIntValue() const
Definition: Capability.h:121
bool _explicit
Whether or not this capability must be compared explicitly (not as a boolean check) ...
Definition: Capability.h:192
std::variant< bool, int, std::string > Value
A capability can have a bool, int, or string value.
Definition: Capability.h:33
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
An entry for a single capability.
Definition: Capability.h:29
std::string enumerationToString() const
Definition: Capability.C:150
bool hasBoolValue() const
Definition: Capability.h:130
Common execption to be thrown when interacting with capabilities.
bool hasEnumeration(const std::string &value) const
Definition: Capability.C:44
const std::string & getName() const
Definition: Capability.h:43
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
std::optional< std::set< std::string > > _enumeration
Possible enumeration for the capability, if any (string capabilities only)
Definition: Capability.h:194
const bool * queryBoolValue() const
Definition: Capability.h:117
Capability & setEnumeration(const std::set< std::string > &enumeration)
Set the enumeration (allowed values) for the capability.
Definition: Capability.C:69
Capability & setExplicit()
Set the capability to be explicit.
Definition: Capability.C:59
std::string stringJoin(const std::vector< std::string > &values, const std::string &separator=" ")
Concatenates value into a single string separated by separator.
std::string valueToString() const
Definition: Capability.C:132