My Project
Loading...
Searching...
No Matches
KeywordValidation.hpp
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>
25#include <opm/simulators/flow/ValidationFunctions.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
93 const SupportedKeywordItems<std::string> string_items;
94 const SupportedKeywordItems<int> int_items;
95 const SupportedKeywordItems<double> double_items;
96};
97
99 {
100 public:
101 KeywordValidator(const UnsupportedKeywords& unsupported_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,
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 void validateKeywordItems(const DeckKeyword& keyword,
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 for (auto item : allowed_values) {
160 m_allowed_values.push_back(item);
161 }
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
Definition KeywordValidation.hpp:155
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilboundaryratevector.hh:37
constexpr auto getPropValue()
get the value data member of a property
Definition propertysystem.hh:242
Definition KeywordValidation.hpp:55
Definition KeywordValidation.hpp:92
Definition KeywordValidation.hpp:48
Definition KeywordValidation.hpp:75
Definition KeywordValidation.hpp:174