Merge pull request #1250 from stefoss23/action_token_func

added enum class FuncType for ASTNode.
This commit is contained in:
Joakim Hove
2019-12-05 08:18:08 +01:00
committed by GitHub
8 changed files with 78 additions and 23 deletions

View File

@@ -76,6 +76,8 @@ namespace Opm {
bool userDefined_{false};
};
SummaryNode::Category parseKeywordCategory(const std::string& keyword);
bool operator==(const SummaryNode& lhs, const SummaryNode& rhs);
bool operator<(const SummaryNode& lhs, const SummaryNode& rhs);

View File

@@ -62,8 +62,9 @@ ASTNode::ASTNode(double value) :
{}
ASTNode::ASTNode(TokenType type_arg, const std::string& func_arg, const std::vector<std::string>& arg_list_arg):
ASTNode::ASTNode(TokenType type_arg, FuncType func_type_arg, const std::string& func_arg, const std::vector<std::string>& arg_list_arg):
type(type_arg),
func_type(func_type_arg),
func(func_arg),
arg_list(strip_quotes(arg_list_arg))
{}
@@ -92,6 +93,9 @@ Action::Value ASTNode::value(const Action::Context& context) const {
well patterns like 'P*'.
*/
if ((this->arg_list.size() == 1) && (arg_list[0].find("*") != std::string::npos)) {
if (this->func_type != FuncType::well)
throw std::logic_error(": attempted to action-evaluate list not of type well.");
Action::Value well_values;
int fnmatch_flags = 0;
for (const auto& well : context.wells(this->func)) {

View File

@@ -16,11 +16,12 @@ public:
ASTNode();
ASTNode(TokenType type_arg);
ASTNode(double value);
ASTNode(TokenType type_arg, const std::string& func_arg, const std::vector<std::string>& arg_list_arg);
ASTNode(TokenType type_arg, FuncType func_type_arg, const std::string& func_arg, const std::vector<std::string>& arg_list_arg);
Action::Result eval(const Action::Context& context) const;
Action::Value value(const Action::Context& context) const;
TokenType type;
FuncType func_type;
void add_child(const ASTNode& child);
size_t size() const;
std::string func;

View File

@@ -21,6 +21,8 @@
#include <cstring>
#include <cstdlib>
#include <opm/parser/eclipse/EclipseState/SummaryConfig/SummaryConfig.hpp>
#include "ActionParser.hpp"
namespace Opm {
@@ -82,6 +84,25 @@ TokenType Parser::get_type(const std::string& arg) {
return TokenType::ecl_expr;
}
FuncType Parser::get_func(const std::string& arg) {
if (arg == "YEAR") return FuncType::time;
if (arg == "MNTH") return FuncType::time;
if (arg == "DAY") return FuncType::time;
using Cat = SummaryNode::Category;
SummaryNode::Category cat = parseKeywordCategory(arg);
switch (cat) {
case Cat::Well: return FuncType::well;
case Cat::Group: return FuncType::group;
case Cat::Connection: return FuncType::well_connection;
case Cat::Region: return FuncType::region;
case Cat::Block: return FuncType::block;
case Cat::Segment: return FuncType::well_segment;
}
return FuncType::none;
}
ParseNode Parser::next() {
this->current_pos++;
@@ -108,6 +129,7 @@ Action::ASTNode Parser::parse_left() {
return TokenType::error;
std::string func = current.value;
FuncType func_type = get_func(current.value);
std::vector<std::string> arg_list;
current = this->next();
while (current.type == TokenType::ecl_expr || current.type == TokenType::number) {
@@ -115,7 +137,7 @@ Action::ASTNode Parser::parse_left() {
current = this->next();
}
return Action::ASTNode(TokenType::ecl_expr, func, arg_list);
return Action::ASTNode(TokenType::ecl_expr, func_type, func, arg_list);
}
Action::ASTNode Parser::parse_op() {
@@ -145,13 +167,14 @@ Action::ASTNode Parser::parse_right() {
return TokenType::error;
std::string func = current.value;
FuncType func_type = FuncType::none;
std::vector<std::string> arg_list;
current = this->next();
while (current.type == TokenType::ecl_expr || current.type == TokenType::number) {
arg_list.push_back(current.value);
current = this->next();
}
return Action::ASTNode(TokenType::ecl_expr, func, arg_list);
return Action::ASTNode(TokenType::ecl_expr, func_type, func, arg_list);
}

View File

@@ -51,6 +51,7 @@ class Parser {
public:
static Action::ASTNode parse(const std::vector<std::string>& tokens);
static TokenType get_type(const std::string& arg);
static FuncType get_func(const std::string& arg);
private:
explicit Parser(const std::vector<std::string>& tokens);

View File

@@ -20,6 +20,20 @@ enum TokenType {
error // 13
};
enum class FuncType {
none,
time,
region,
field,
group,
well,
well_segment,
well_connection,
Well_lgr,
aquifer,
block
};
namespace Opm {

View File

@@ -683,25 +683,6 @@ inline void keywordMISC( SummaryConfig::keyword_list& list,
}
}
SummaryNode::Category parseKeywordCategory(const std::string& keyword) {
using Cat = SummaryNode::Category;
if (is_special(keyword)) { return Cat::Miscellaneous; }
switch (keyword[0]) {
case 'W': return Cat::Well;
case 'G': return Cat::Group;
case 'F': return Cat::Field;
case 'C': return Cat::Connection;
case 'R': return Cat::Region;
case 'B': return Cat::Block;
case 'S': return Cat::Segment;
}
// TCPU, MLINEARS, NEWTON, &c
return Cat::Miscellaneous;
}
std::string to_string(const SummaryNode::Category cat) {
switch( cat ) {
case SummaryNode::Category::Well: return "Well";
@@ -814,6 +795,27 @@ inline void handleKW( SummaryConfig::keyword_list& list,
// =====================================================================
SummaryNode::Category parseKeywordCategory(const std::string& keyword) {
using Cat = SummaryNode::Category;
if (is_special(keyword)) { return Cat::Miscellaneous; }
switch (keyword[0]) {
case 'W': return Cat::Well;
case 'G': return Cat::Group;
case 'F': return Cat::Field;
case 'C': return Cat::Connection;
case 'R': return Cat::Region;
case 'B': return Cat::Block;
case 'S': return Cat::Segment;
}
// TCPU, MLINEARS, NEWTON, &c
return Cat::Miscellaneous;
}
SummaryNode::SummaryNode(std::string keyword, const Category cat, Location loc_arg) :
keyword_(std::move(keyword)),
category_(cat),

View File

@@ -478,7 +478,15 @@ BOOST_AUTO_TEST_CASE(Action_ContextTest) {
BOOST_REQUIRE_THROW(context.get("WGOR", "B37"), std::out_of_range);
}
//Note: that this is only temporary test.
//Groupnames w/ astirisks wil eventually work with ACTIONX
BOOST_AUTO_TEST_CASE(TestGroupList) {
Action::AST ast({"GWPR", "*", ">", "1.0"});
SummaryState st(std::chrono::system_clock::now());
Action::Context context(st);
BOOST_CHECK_THROW( ast.eval(context), std::logic_error );
}
BOOST_AUTO_TEST_CASE(TestMatchingWells) {
Action::AST ast({"WOPR", "*", ">", "1.0"});