Added full(?) Parser configuration from json

This commit is contained in:
Joakim Hove
2013-07-31 17:58:05 +02:00
parent 8dc8781c12
commit 8bc176d63e
6 changed files with 121 additions and 11 deletions

View File

@@ -187,6 +187,8 @@ BOOST_AUTO_TEST_CASE(ParseFileWithOneKeyword) {
BOOST_CHECK_EQUAL("20", record->getItem(3));
}
BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) {
boost::filesystem::path singleKeywordFile("testdata/small.data");

View File

@@ -22,6 +22,9 @@
#include <opm/parser/eclipse/Parser/ParserItem.hpp>
#include <opm/parser/eclipse/Parser/ParserEnums.hpp>
#include <opm/parser/eclipse/Parser/ParserIntItem.hpp>
#include <opm/parser/eclipse/Parser/ParserStringItem.hpp>
namespace Opm {
ParserItem::ParserItem(const std::string& itemName, ParserItemSizeEnum sizeType) {
@@ -43,6 +46,11 @@ namespace Opm {
throw std::invalid_argument("Json config object missing \"size_type\": ... item");
}
const std::string& ParserItem::name() const {
return m_name;

View File

@@ -25,6 +25,8 @@
#include <opm/parser/eclipse/Parser/ParserConst.hpp>
#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>
#include <opm/parser/eclipse/Parser/ParserIntItem.hpp>
#include <opm/parser/eclipse/Parser/ParserStringItem.hpp>
namespace Opm {
@@ -51,14 +53,19 @@ namespace Opm {
ParserKeyword::ParserKeyword(const Json::JsonObject& jsonConfig) {
if (jsonConfig.has_item("name")) {
commonInit(jsonConfig.get_string("name"));
if (jsonConfig.has_item("size")) {
m_fixedSize = (size_t) jsonConfig.get_int("size");
m_keywordSizeType = FIXED;
} else
m_keywordSizeType = UNDEFINED;
} else
throw std::invalid_argument("Json object is missing name: property");
if (jsonConfig.has_item("size")) {
m_fixedSize = (size_t) jsonConfig.get_int("size");
m_keywordSizeType = FIXED;
} else
m_keywordSizeType = UNDEFINED;
if (jsonConfig.has_item("items"))
addItems( jsonConfig );
}
void ParserKeyword::commonInit(const std::string& name) {
if (name.length() > ParserConst::maxKeywordLength)
@@ -73,6 +80,38 @@ namespace Opm {
}
void ParserKeyword::addItems( const Json::JsonObject& jsonConfig) {
const Json::JsonObject itemsConfig = jsonConfig.get_item("items");
if (itemsConfig.is_array()) {
size_t num_items = itemsConfig.size();
for (size_t i=0; i < num_items; i++) {
const Json::JsonObject itemConfig = itemsConfig.get_array_item( i );
if (itemConfig.has_item("value_type")) {
ParserValueTypeEnum valueType = ParserValueTypeEnumFromString( itemConfig.get_string("value_type") );
switch( valueType ) {
case INT:
{
ParserIntItemConstPtr item = ParserIntItemConstPtr(new ParserIntItem( itemConfig ));
m_record->addItem( item );
}
break;
case STRING:
{
ParserStringItemConstPtr item = ParserStringItemConstPtr(new ParserStringItem( itemConfig ));
m_record->addItem( item );
}
break;
default:
throw std::invalid_argument("Not implemented.");
}
} else
throw std::invalid_argument("Json config object missing \"value_type\": ... item");
}
} else
throw std::invalid_argument("The items: object must be an array");
}
ParserRecordPtr ParserKeyword::getRecord() {
return m_record;
}

View File

@@ -51,7 +51,7 @@ namespace Opm {
size_t m_fixedSize;
void commonInit(const std::string& name);
void addItems( const Json::JsonObject& jsonConfig);
};
typedef boost::shared_ptr<ParserKeyword> ParserKeywordPtr;
typedef boost::shared_ptr<const ParserKeyword> ParserKeywordConstPtr;

View File

@@ -42,6 +42,8 @@ BOOST_AUTO_TEST_CASE(NamedInit) {
BOOST_CHECK_EQUAL(parserKeyword.getName(), keyword);
}
/*****************************************************************/
/* json */
BOOST_AUTO_TEST_CASE(ConstructFromJsonObject) {
Json::JsonObject jsonObject("{\"name\": \"BPR\"}");
@@ -65,8 +67,58 @@ BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_missingName_throws) {
BOOST_CHECK_THROW(ParserKeyword parserKeyword(jsonObject) , std::invalid_argument);
}
/*
"items": [{"name" : "I" , "size_type" : "SINGLE" , "value_type" : "int"}]
*/
BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_invalidItems_throws) {
Json::JsonObject jsonObject("{\"name\": \"BPR\", \"size\" : 100 , \"items\" : 100}");
BOOST_CHECK_THROW(ParserKeyword parserKeyword(jsonObject) , std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_ItemMissingName_throws) {
Json::JsonObject jsonObject("{\"name\": \"BPR\", \"size\" : 100 , \"items\" : [{\"nameX\" : \"I\" , \"size_type\" : \"SINGLE\" , \"value_type\" : \"INT\"}]}");
BOOST_CHECK_THROW(ParserKeyword parserKeyword(jsonObject) , std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_ItemMissingSizeType_throws) {
Json::JsonObject jsonObject("{\"name\": \"BPR\", \"size\" : 100 , \"items\" : [{\"name\" : \"I\" , \"Xsize_type\" : \"SINGLE\" , \"value_type\" : \"INT\"}]}");
BOOST_CHECK_THROW(ParserKeyword parserKeyword(jsonObject) , std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_ItemMissingValueType_throws) {
Json::JsonObject jsonObject("{\"name\": \"BPR\", \"size\" : 100 , \"items\" : [{\"name\" : \"I\" , \"size_type\" : \"SINGLE\" , \"Xvalue_type\" : \"INT\"}]}");
BOOST_CHECK_THROW(ParserKeyword parserKeyword(jsonObject) , std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_ItemInvalidEnum_throws) {
Json::JsonObject jsonObject1("{\"name\": \"BPR\", \"size\" : 100 , \"items\" : [{\"name\" : \"I\" , \"size_type\" : \"XSINGLE\" , \"value_type\" : \"INT\"}]}");
Json::JsonObject jsonObject2("{\"name\": \"BPR\", \"size\" : 100 , \"items\" : [{\"name\" : \"I\" , \"size_type\" : \"SINGLE\" , \"value_type\" : \"INTX\"}]}");
BOOST_CHECK_THROW(ParserKeyword parserKeyword(jsonObject1) , std::invalid_argument);
BOOST_CHECK_THROW(ParserKeyword parserKeyword(jsonObject2) , std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(ConstructFromJsonObjectItemsOK) {
Json::JsonObject jsonObject("{\"name\": \"BPR\", \"size\" : 100 , \"items\" : [{\"name\" : \"I\" , \"size_type\" : \"SINGLE\" , \"value_type\" : \"INT\"}]}");
ParserKeyword parserKeyword(jsonObject);
ParserRecordConstPtr record = parserKeyword.getRecord();
ParserItemConstPtr item = record->get( 0 );
BOOST_CHECK_EQUAL( 1U , record->size( ) );
BOOST_CHECK_EQUAL( "I" , item->name( ) );
BOOST_CHECK_EQUAL( SINGLE , item->sizeType());
}
/* </Json> */
/*****************************************************************/
BOOST_AUTO_TEST_CASE(constructor_nametoolongwithfixedsize_exceptionthrown) {
std::string keyword("KEYWORDTOOLONG");
BOOST_CHECK_THROW(ParserKeyword parserKeyword(keyword, 100), std::invalid_argument);