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