KeywordValidation.hpp
Go to the documentation of this file.
1/*
2 Copyright 2021 Equinor.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef OPM_KEYWORDVALIDATION_HEADER_INCLUDED
21#define OPM_KEYWORDVALIDATION_HEADER_INCLUDED
22
23#include <opm/input/eclipse/Deck/DeckItem.hpp>
24#include <opm/common/OpmLog/KeywordLocation.hpp>
26
27#include <algorithm>
28#include <cstddef>
29#include <functional>
30#include <initializer_list>
31#include <map>
32#include <optional>
33#include <string>
34#include <unordered_map>
35#include <vector>
36
37namespace Opm
38{
39
40class Deck;
41class DeckKeyword;
42class ErrorGuard;
43class ParseContext;
44
45namespace KeywordValidation
46{
47 // Describe an unsupported keyword:
49 bool critical; // Set to true if presence of the keyword should be an error
50 std::optional<std::string> message; // An optional message to show if the keyword is present
51 };
52
53 // Describe a partially or fully supported keyword item, by listing legal values:
54 template <typename T>
56 bool critical; // Set to true if an unsupported or invalid item value should be an error
57 std::function<bool(T)> validator; // Predicate function to test values
58 std::optional<std::string> message; // An optional message to show if an illegal item is encountered
59 };
60
61 // This is used to list unsupported kewyords.
62 using UnsupportedKeywords = std::map<std::string, UnsupportedKeywordProperties>;
63
64 // This is used to list the partially supported items of a keyword:
65 template <typename T>
66 using SupportedSingleKeywordItems = std::map<std::size_t, SupportedKeywordProperties<T>>;
67
68 // This is used to list the keywords that have partially supported items or items that benefit from early validation:
69 template <typename T>
70 using SupportedKeywordItems = std::map<std::string, SupportedSingleKeywordItems<T>>;
71
72 // This contains the information needed to report a single error occurence.
73 // The validator will construct a vector of these, copying the relevant
74 // information from the properties of the offending keywords and items.
76 bool critical; // Determines if the encountered problem should be an error or a warning
77 KeywordLocation location; // Location information (keyword name, file and line number)
78 std::size_t record_number; // Number of the offending record
79 std::optional<std::size_t> item_number; // Number of the offending item
80 std::optional<std::string> item_value; // The offending value of a problematic item
81 std::optional<std::string> user_message; // An optional message to show if a problem is encountered
82 };
83
84 // Get a formatted error report from a vector of validation errors. Set
85 // include_noncritical to true if the report should include noncritical errors, and
86 // include_critical to true if the report should include critical errors. These may
87 // be set independently. If no errors are included the result will be an empty string.
88 std::string get_error_report(const std::vector<ValidationError>& errors,
89 const bool include_noncritical,
90 const bool include_critical);
91
96};
97
99 {
100 public:
101 KeywordValidator(const UnsupportedKeywords& unsupported_keywords,
102 const SupportedKeywords& partially_supported_keywords,
103 const SupportedKeywords& fully_supported_keywords,
104 const std::unordered_map<std::string, ValidationFunction>& special_validation)
105 : m_unsupported_keywords(unsupported_keywords)
106 , m_partially_supported_keywords(partially_supported_keywords)
107 , m_fully_supported_keywords(fully_supported_keywords)
108 , m_special_validation(special_validation)
109 {
110 }
111
112 // Validate a deck, reporting warnings and errors. If there are only
113 // warnings, these will be reported. If there are errors, these are
114 // reported, and execution of the program is halted, unless the argument
115 // treat_critical_as_noncritical is true, then these also will only be
116 // reported and not cause termination.
117 void validateDeck(const Deck& deck,
118 const ParseContext& parse_context,
119 const bool treat_critical_as_noncritical,
120 ErrorGuard& error_guard) const;
121
122 // Validate a single deck keyword. If a problem is encountered, add the
123 // relevant information to the errors vector.
124 void validateDeckKeyword(const DeckKeyword& keyword, std::vector<ValidationError>& errors) const;
125
126 private:
127 template <typename T>
128 void validateKeywordItem(const DeckKeyword& keyword,
129 const SupportedKeywordProperties<T>& properties,
130 const bool multiple_records,
131 const std::size_t record_number,
132 const std::size_t item_number,
133 const T& item_value,
134 std::vector<ValidationError>& errors) const;
135
136 void validateKeywordItems(const DeckKeyword& keyword,
137 const SupportedKeywords& keyword_items,
138 std::vector<ValidationError>& errors) const;
139
140 template <typename T>
141 void validateKeywordItems(const DeckKeyword& keyword,
142 const SupportedKeywordItems<T>& supported_options,
143 std::vector<ValidationError>& errors) const;
144
145 const UnsupportedKeywords m_unsupported_keywords;
146 const SupportedKeywords m_partially_supported_keywords;
147 const SupportedKeywords m_fully_supported_keywords;
148 const std::unordered_map<std::string, ValidationFunction> m_special_validation;
149 };
150
151
152 // Helper class to test if a given value is with a list of allowed values.
153 template <typename T>
155 {
156 public:
157 allow_values(const std::initializer_list<T>& allowed_values)
158 {
159 std::copy(allowed_values.begin(),
160 allowed_values.end(),
161 std::back_inserter(m_allowed_values));
162 }
163
164 bool operator()(const T& value) const
165 {
166 return std::find(m_allowed_values.begin(), m_allowed_values.end(), value) != m_allowed_values.end();
167 }
168
169 private:
170 std::vector<T> m_allowed_values;
171 };
172
173 // Helper to test if given string value is convertible to bool (see DeckItem::to_bool)
176 bool operator()(const std::string& value) const {
177 try {
178 return DeckItem::to_bool(value) || true;
179 } catch (const std::invalid_argument& e) {
180 return false;
181 }
182 }
183 };
184
185} // namespace KeywordValidation
186
187} // namespace Opm
188
189
190#endif
Definition: KeywordValidation.hpp:99
void validateDeck(const Deck &deck, const ParseContext &parse_context, const bool treat_critical_as_noncritical, ErrorGuard &error_guard) const
void validateDeckKeyword(const DeckKeyword &keyword, std::vector< ValidationError > &errors) const
KeywordValidator(const UnsupportedKeywords &unsupported_keywords, const SupportedKeywords &partially_supported_keywords, const SupportedKeywords &fully_supported_keywords, const std::unordered_map< std::string, ValidationFunction > &special_validation)
Definition: KeywordValidation.hpp:101
Definition: KeywordValidation.hpp:155
bool operator()(const T &value) const
Definition: KeywordValidation.hpp:164
allow_values(const std::initializer_list< T > &allowed_values)
Definition: KeywordValidation.hpp:157
std::string get_error_report(const std::vector< ValidationError > &errors, const bool include_noncritical, const bool include_critical)
std::map< std::string, UnsupportedKeywordProperties > UnsupportedKeywords
Definition: KeywordValidation.hpp:62
std::map< std::string, SupportedSingleKeywordItems< T > > SupportedKeywordItems
Definition: KeywordValidation.hpp:70
std::map< std::size_t, SupportedKeywordProperties< T > > SupportedSingleKeywordItems
Definition: KeywordValidation.hpp:66
Definition: blackoilboundaryratevector.hh:39
Definition: KeywordValidation.hpp:55
bool critical
Definition: KeywordValidation.hpp:56
std::optional< std::string > message
Definition: KeywordValidation.hpp:58
std::function< bool(T)> validator
Definition: KeywordValidation.hpp:57
Definition: KeywordValidation.hpp:92
const SupportedKeywordItems< int > int_items
Definition: KeywordValidation.hpp:94
const SupportedKeywordItems< std::string > string_items
Definition: KeywordValidation.hpp:93
const SupportedKeywordItems< double > double_items
Definition: KeywordValidation.hpp:95
Definition: KeywordValidation.hpp:48
bool critical
Definition: KeywordValidation.hpp:49
std::optional< std::string > message
Definition: KeywordValidation.hpp:50
Definition: KeywordValidation.hpp:75
KeywordLocation location
Definition: KeywordValidation.hpp:77
std::optional< std::string > user_message
Definition: KeywordValidation.hpp:81
bool critical
Definition: KeywordValidation.hpp:76
std::optional< std::size_t > item_number
Definition: KeywordValidation.hpp:79
std::optional< std::string > item_value
Definition: KeywordValidation.hpp:80
std::size_t record_number
Definition: KeywordValidation.hpp:78
Definition: KeywordValidation.hpp:174
bool operator()(const std::string &value) const
Definition: KeywordValidation.hpp:176
is_bool_convertible()
Definition: KeywordValidation.hpp:175