opm-common
ASTNode.hpp
1 /*
2  Copyright 2019 Equinor ASA.
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 ASTNODE_HPP
21 #define ASTNODE_HPP
22 
23 #include <opm/input/eclipse/Schedule/Action/ActionValue.hpp>
24 
25 #include <cstddef>
26 #include <string>
27 #include <string_view>
28 #include <unordered_set>
29 #include <vector>
30 
31 namespace Opm::Action {
32  class Context;
33 } // namespace Opm::Action
34 
35 namespace Opm::Action {
36 
48 class ASTNode
49 {
50 public:
55  ASTNode();
56 
63  explicit ASTNode(TokenType type_arg);
64 
70  explicit ASTNode(double value);
71 
89  explicit ASTNode(TokenType type_arg,
90  FuncType func_type_arg,
91  std::string_view func_arg,
92  const std::vector<std::string>& arg_list_arg);
93 
95  TokenType type;
96 
98  FuncType func_type;
99 
102  std::string func;
103 
105  static ASTNode serializationTestObject();
106 
118  void add_child(ASTNode&& child);
119 
127  Result eval(const Context& context) const;
128 
135  void required_summary(std::unordered_set<std::string>& required_summary) const;
136 
143  bool operator==(const ASTNode& that) const;
144 
146  std::size_t size() const;
147 
149  bool empty() const;
150 
156  template<class Serializer>
157  void serializeOp(Serializer& serializer)
158  {
159  serializer(type);
160  serializer(func_type);
161  serializer(func);
162  serializer(arg_list);
163  serializer(number);
164  serializer(children);
165  }
166 
167 private:
168  // Note: data member order here is dictated by initialisation list in
169  // four-argument constructor.
170 
173  std::vector<std::string> arg_list{};
174 
176  double number {0.0};
177 
178  // Note: a data member of type std::vector<ASTNode> inside class ASTNode
179  // is well defined in C++17 or later, but may look surprising to the
180  // uninitiated.
181 
183  std::vector<ASTNode> children{};
184 
194  Result evalLogicalOperation(const Context& context) const;
195 
206  Result evalComparison(const Context& context) const;
207 
219  Value nodeValue(const Context& context) const;
220 
233  Value evalListExpression(const Context& context) const;
234 
247  Value evalScalarExpression(const Context& context) const;
248 
261  Value evalWellExpression(const Context& context) const;
262 
271  std::vector<std::string> getWellList(const Context& context) const;
272 
275  bool argListIsPattern() const;
276 
280  bool argListIsWellList() const;
281 };
282 
283 } // namespace Opm::Action
284 
285 #endif // ASTNODE_HPP
Definition: Python.hpp:35