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/common/OpmLog/KeywordLocation.hpp>
24
25#include <cstddef>
26#include <functional>
27#include <initializer_list>
28#include <map>
29#include <optional>
30#include <string>
31#include <unordered_map>
32#include <vector>
33
34namespace Opm
35{
36
37class Deck;
38class DeckKeyword;
39class ErrorGuard;
40class ParseContext;
41
42namespace KeywordValidation
43{
44 // Describe an unsupported keyword:
46 bool critical; // Set to true if presence of the keyword should be an error
47 std::optional<std::string> message; // An optional message to show if the keyword is present
48 };
49
50 // Describe a partially supported keyword item, by listing legal values:
51 template <typename T>
53 bool critical; // Set to true if the unsupported item value should be an error
54 std::function<bool(T)> validator; // Predicate function to test values
55 std::optional<std::string> message; // An optional message to show if an illegal item is encountered
56 };
57
58 // This is used to list unsupported kewyords.
59 using UnsupportedKeywords = std::map<std::string, UnsupportedKeywordProperties>;
60
61 // This is used to list the partially supported items of a keyword:
62 template <typename T>
63 using PartiallySupportedKeywordItems = std::map<std::size_t, PartiallySupportedKeywordProperties<T>>;
64
65 // This is used to list the keywords that have partially supported items:
66 template <typename T>
67 using PartiallySupportedKeywords = std::map<std::string, PartiallySupportedKeywordItems<T>>;
68
69 // This contains the information needed to report a single error occurence.
70 // The validator will construct a vector of these, copying the relevant
71 // information from the properties of the offending keywords and items.
73 bool critical; // Determines if the encountered problem should be an error or a warning
74 KeywordLocation location; // Location information (keyword name, file and line number)
75 std::size_t record_number; // Number of the offending record
76 std::optional<std::size_t> item_number; // Number of the offending item
77 std::optional<std::string> item_value; // The offending value of a problematic item
78 std::optional<std::string> user_message; // An optional message to show if a problem is encountered
79 };
80
81 // Get a formatted error report from a vector of validation errors. Set
82 // include_noncritical to true if the report should include noncritical errors, and
83 // include_critical to true if the report should include critical errors. These may
84 // be set independently. If no errors are included the result will be an empty string.
85 std::string get_error_report(const std::vector<ValidationError>& errors,
86 const bool include_noncritical,
87 const bool include_critical);
88
89
90
91 // These are special case validation functions for keyword which do not fit nicely into the general
92 // validation framework. The validation function itself is void, but error conditions are signalled by
93 // appending ValidationError instances to the @errors vector.
94 void validateBRINE(const DeckKeyword& keyword, std::vector<ValidationError>& errors);
95
97 {
98 public:
100 const PartiallySupportedKeywords<std::string>& string_items,
101 const PartiallySupportedKeywords<int>& int_items,
102 const PartiallySupportedKeywords<double>& double_items,
103 const std::unordered_map<std::string, std::function<void(const DeckKeyword& keyword, std::vector<ValidationError>& errors)>>& special_validation)
104 : m_keywords(keywords)
105 , m_string_items(string_items)
106 , m_int_items(int_items)
107 , m_double_items(double_items)
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,
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
137 template <typename T>
138 void validateKeywordItems(const DeckKeyword& keyword,
139 const PartiallySupportedKeywords<T>& partially_supported_options,
140 std::vector<ValidationError>& errors) const;
141
142 const UnsupportedKeywords m_keywords;
143 const PartiallySupportedKeywords<std::string> m_string_items;
144 const PartiallySupportedKeywords<int> m_int_items;
145 const PartiallySupportedKeywords<double> m_double_items;
146 const std::unordered_map<std::string, std::function<void(const DeckKeyword& keyword, std::vector<ValidationError>& errors)>> m_special_validation;
147 };
148
149
150 // Helper class to test if a given value is with a list of allowed values.
151 template <typename T>
153 {
154 public:
155 allow_values(const std::initializer_list<T>& allowed_values)
156 {
157 for (auto item : allowed_values) {
158 m_allowed_values.push_back(item);
159 }
160 }
161
162 bool operator()(const T& value) const
163 {
164 return std::find(m_allowed_values.begin(), m_allowed_values.end(), value) != m_allowed_values.end();
165 }
166
167 private:
168 std::vector<T> m_allowed_values;
169 };
170
171
172} // namespace KeywordValidation
173
174} // namespace Opm
175
176
177#endif
Definition: KeywordValidation.hpp:97
KeywordValidator(const UnsupportedKeywords &keywords, const PartiallySupportedKeywords< std::string > &string_items, const PartiallySupportedKeywords< int > &int_items, const PartiallySupportedKeywords< double > &double_items, const std::unordered_map< std::string, std::function< void(const DeckKeyword &keyword, std::vector< ValidationError > &errors)> > &special_validation)
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
Definition: KeywordValidation.hpp:153
bool operator()(const T &value) const
Definition: KeywordValidation.hpp:162
allow_values(const std::initializer_list< T > &allowed_values)
Definition: KeywordValidation.hpp:155
void validateBRINE(const DeckKeyword &keyword, std::vector< ValidationError > &errors)
std::string get_error_report(const std::vector< ValidationError > &errors, const bool include_noncritical, const bool include_critical)
std::map< std::size_t, PartiallySupportedKeywordProperties< T > > PartiallySupportedKeywordItems
Definition: KeywordValidation.hpp:63
std::map< std::string, UnsupportedKeywordProperties > UnsupportedKeywords
Definition: KeywordValidation.hpp:59
std::map< std::string, PartiallySupportedKeywordItems< T > > PartiallySupportedKeywords
Definition: KeywordValidation.hpp:67
Definition: BlackoilPhases.hpp:27
std::function< bool(T)> validator
Definition: KeywordValidation.hpp:54
bool critical
Definition: KeywordValidation.hpp:53
std::optional< std::string > message
Definition: KeywordValidation.hpp:55
Definition: KeywordValidation.hpp:45
bool critical
Definition: KeywordValidation.hpp:46
std::optional< std::string > message
Definition: KeywordValidation.hpp:47
Definition: KeywordValidation.hpp:72
KeywordLocation location
Definition: KeywordValidation.hpp:74
std::optional< std::string > user_message
Definition: KeywordValidation.hpp:78
bool critical
Definition: KeywordValidation.hpp:73
std::optional< std::size_t > item_number
Definition: KeywordValidation.hpp:76
std::optional< std::string > item_value
Definition: KeywordValidation.hpp:77
std::size_t record_number
Definition: KeywordValidation.hpp:75