Add value type RAW_STRING
When parsing RAW_STRING items the following applies: - Quotes are retained - as in 'P20' - Asteriks are not replaced with defaults
This commit is contained in:
@@ -64,6 +64,8 @@ namespace Opm {
|
||||
template< typename T > void setDefault( T );
|
||||
/* set type without a default value. will reset dimension etc. */
|
||||
template< typename T > void setType( T );
|
||||
template< typename T > void setType( T , bool raw);
|
||||
bool parseRaw() const;
|
||||
bool hasDefault() const;
|
||||
template< typename T > const T& getDefault() const;
|
||||
|
||||
@@ -81,6 +83,7 @@ namespace Opm {
|
||||
double dval;
|
||||
int ival;
|
||||
std::string sval;
|
||||
bool raw_string = false;
|
||||
std::vector< std::string > dimensions;
|
||||
|
||||
std::string m_name;
|
||||
|
||||
@@ -51,9 +51,10 @@ template<> const std::string& default_value< std::string >() {
|
||||
}
|
||||
|
||||
type_tag get_type_json( const std::string& str ) {
|
||||
if( str == "INT" ) return type_tag::integer;
|
||||
if( str == "DOUBLE" ) return type_tag::fdouble;
|
||||
if( str == "STRING" ) return type_tag::string;
|
||||
if( str == "INT" ) return type_tag::integer;
|
||||
if( str == "DOUBLE" ) return type_tag::fdouble;
|
||||
if( str == "STRING" ) return type_tag::string;
|
||||
if( str == "RAW_STRING") return type_tag::string;
|
||||
throw std::invalid_argument( str + " cannot be converted to enum 'tag'" );
|
||||
}
|
||||
|
||||
@@ -167,6 +168,8 @@ ParserItem::ParserItem( const Json::JsonObject& json ) :
|
||||
);
|
||||
}
|
||||
}
|
||||
if (json.get_string("value_type") == "RAW_STRING")
|
||||
this->raw_string = true;
|
||||
|
||||
if( !json.has_item( "default" ) ) return;
|
||||
|
||||
@@ -199,10 +202,17 @@ void ParserItem::setDefault( T val ) {
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
void ParserItem::setType( T ) {
|
||||
void ParserItem::setType( T) {
|
||||
this->type = get_type< T >();
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
void ParserItem::setType( T, bool raw ) {
|
||||
this->type = get_type< T >();
|
||||
this->raw_string = raw;
|
||||
}
|
||||
|
||||
|
||||
bool ParserItem::hasDefault() const {
|
||||
return this->m_defaultSet;
|
||||
}
|
||||
@@ -348,7 +358,11 @@ std::string ParserItem::createCode() const {
|
||||
}
|
||||
}
|
||||
|
||||
stream << " ); item.setType( " << tag_name( this->type ) << "() );";
|
||||
if (raw_string)
|
||||
stream << " ); item.setType( " << tag_name( this->type ) << "(), true );";
|
||||
else
|
||||
stream << " ); item.setType( " << tag_name( this->type ) << "() );";
|
||||
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
@@ -357,8 +371,17 @@ namespace {
|
||||
template< typename T >
|
||||
DeckItem scan_item( const ParserItem& p, RawRecord& record ) {
|
||||
DeckItem item( p.name(), T(), record.size() );
|
||||
bool parse_raw = p.parseRaw();
|
||||
|
||||
if( p.sizeType() == ParserItem::item_size::ALL ) {
|
||||
if (parse_raw) {
|
||||
while (record.size()) {
|
||||
auto token = record.pop_front();
|
||||
item.push_back( token.string() );
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
while( record.size() > 0 ) {
|
||||
auto token = record.pop_front();
|
||||
|
||||
@@ -399,20 +422,25 @@ DeckItem scan_item( const ParserItem& p, RawRecord& record ) {
|
||||
return item;
|
||||
}
|
||||
|
||||
if (parse_raw) {
|
||||
item.push_back( record.pop_front().string());
|
||||
return item;
|
||||
}
|
||||
|
||||
// The '*' should be interpreted as a repetition indicator, but it must
|
||||
// be preceeded by an integer...
|
||||
auto token = record.pop_front();
|
||||
std::string countString;
|
||||
std::string valueString;
|
||||
if( !isStarToken(token, countString, valueString) ) {
|
||||
item.push_back( readValueToken<T>( token ) );
|
||||
item.push_back( readValueToken<T>( token) );
|
||||
return item;
|
||||
}
|
||||
|
||||
StarToken st(token, countString, valueString);
|
||||
|
||||
if( st.hasValue() )
|
||||
item.push_back(readValueToken< T >( st.valueString() ) );
|
||||
item.push_back(readValueToken< T >( st.valueString()) );
|
||||
else if( p.hasDefault() )
|
||||
item.push_backDefault( p.getDefault< T >() );
|
||||
else
|
||||
@@ -549,6 +577,10 @@ std::ostream& operator<<( std::ostream& stream, const ParserItem& item ) {
|
||||
return stream << " }";
|
||||
}
|
||||
|
||||
bool ParserItem::parseRaw( ) const {
|
||||
return this->raw_string;
|
||||
}
|
||||
|
||||
template void ParserItem::setDefault( int );
|
||||
template void ParserItem::setDefault( double );
|
||||
template void ParserItem::setDefault( std::string );
|
||||
@@ -556,6 +588,7 @@ template void ParserItem::setDefault( std::string );
|
||||
template void ParserItem::setType( int );
|
||||
template void ParserItem::setType( double );
|
||||
template void ParserItem::setType( std::string );
|
||||
template void ParserItem::setType( std::string , bool);
|
||||
|
||||
template const int& ParserItem::getDefault() const;
|
||||
template const double& ParserItem::getDefault() const;
|
||||
|
||||
@@ -370,8 +370,12 @@ void set_dimensions( ParserItem& item,
|
||||
}
|
||||
|
||||
|
||||
if (value_type == "STRING") {
|
||||
item.setType( std::string() );
|
||||
if (value_type == "STRING" || value_type == "RAW_STRING") {
|
||||
if (value_type == "RAW_STRING")
|
||||
item.setType( std::string(), true );
|
||||
else
|
||||
item.setType( std::string() );
|
||||
|
||||
if (hasDefault) {
|
||||
std::string defaultValue = dataConfig.get_string("default");
|
||||
item.setDefault(defaultValue);
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
{"name" : "UDQ" , "sections" : ["SCHEDULE"],
|
||||
"items" : [
|
||||
{"name" : "ACTION", "value_type" : "STRING"},
|
||||
{"name" : "QUANTITY", "value_type" : "STRING"},
|
||||
{"name" : "DATA", "value_type" : "RAW_STRING" , "size_type" : "ALL"}]}
|
||||
@@ -331,6 +331,7 @@ set( keywords
|
||||
000_Eclipse100/T/TUNING
|
||||
000_Eclipse100/T/TUNINGDP
|
||||
000_Eclipse100/T/TVDP
|
||||
000_Eclipse100/U/UDQ
|
||||
000_Eclipse100/U/UDADIMS
|
||||
000_Eclipse100/U/UDQDIMS
|
||||
000_Eclipse100/U/UNIFIN
|
||||
|
||||
@@ -1838,3 +1838,21 @@ AQUTAB
|
||||
BOOST_CHECK_EQUAL( 1, aqutab.size());
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParseRAW_STRING) {
|
||||
const std::string deck_string = R"(
|
||||
UDQ
|
||||
DEFINE WUBHP 'P*X*' /
|
||||
DEFINE WUBHP 'P*X*' 5*(1 + LOG(WBHP)) /
|
||||
/
|
||||
)";
|
||||
Parser parser;
|
||||
const auto deck = parser.parseString( deck_string, ParseContext());
|
||||
const auto& udq = deck.getKeyword("UDQ");
|
||||
const auto& data0 = udq.getRecord(0).getItem("DATA").getData<std::string>();
|
||||
const auto& data1 = udq.getRecord(1).getItem("DATA").getData<std::string>();
|
||||
const std::vector<std::string> expected0 = {"'P*X*'"};
|
||||
const std::vector<std::string> expected1 = {"'P*X*'", "5*(1", "+", "LOG(WBHP))"};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS( data0.begin(), data0.end(), expected0.begin(), expected0.end());
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS( data1.begin(), data1.end(), expected1.begin(), expected1.end());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user