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