2013-03-21 15:37:40 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2013 Statoil ASA.
|
|
|
|
|
|
|
|
|
|
This file is part of the Open Porous Media project (OPM).
|
|
|
|
|
|
|
|
|
|
OPM is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
OPM is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
2013-04-08 10:36:14 +02:00
|
|
|
*/
|
2013-03-21 15:37:40 +01:00
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
2013-07-30 14:10:07 +02:00
|
|
|
|
2013-08-06 16:26:49 +02:00
|
|
|
|
2013-07-30 14:10:07 +02:00
|
|
|
#include <opm/json/JsonObject.hpp>
|
|
|
|
|
|
2013-04-02 15:19:32 +02:00
|
|
|
#include <opm/parser/eclipse/Parser/ParserConst.hpp>
|
2013-06-20 15:30:37 +02:00
|
|
|
#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>
|
2013-07-31 17:58:05 +02:00
|
|
|
#include <opm/parser/eclipse/Parser/ParserIntItem.hpp>
|
2013-08-01 09:31:27 +02:00
|
|
|
#include <opm/parser/eclipse/Parser/ParserDoubleItem.hpp>
|
2013-07-31 17:58:05 +02:00
|
|
|
#include <opm/parser/eclipse/Parser/ParserStringItem.hpp>
|
2013-03-21 15:37:40 +01:00
|
|
|
|
2013-08-01 09:31:27 +02:00
|
|
|
|
2013-03-21 15:37:40 +01:00
|
|
|
namespace Opm {
|
2013-04-08 10:36:14 +02:00
|
|
|
|
2013-07-30 14:10:07 +02:00
|
|
|
|
2013-06-20 15:30:37 +02:00
|
|
|
ParserKeyword::ParserKeyword(const std::string& name) {
|
2013-07-31 15:02:00 +02:00
|
|
|
commonInit(name);
|
2013-06-24 14:08:53 +02:00
|
|
|
m_keywordSizeType = UNDEFINED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-07-30 14:10:07 +02:00
|
|
|
ParserKeyword::ParserKeyword(const char * name) {
|
2013-07-31 15:02:00 +02:00
|
|
|
commonInit(name);
|
2013-07-30 14:10:07 +02:00
|
|
|
m_keywordSizeType = UNDEFINED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ParserKeyword::ParserKeyword(const std::string& name, size_t fixedKeywordSize) {
|
2013-07-31 15:02:00 +02:00
|
|
|
commonInit(name);
|
2013-06-24 14:08:53 +02:00
|
|
|
m_keywordSizeType = FIXED;
|
|
|
|
|
m_fixedSize = fixedKeywordSize;
|
2013-05-06 12:13:49 +02:00
|
|
|
}
|
2013-07-30 14:10:07 +02:00
|
|
|
|
|
|
|
|
|
2013-08-06 16:26:49 +02:00
|
|
|
ParserKeyword::ParserKeyword(const std::string& name , const std::string& sizeKeyword , const std::string& sizeItem) {
|
|
|
|
|
commonInit(name);
|
|
|
|
|
initSizeKeyword( sizeKeyword , sizeItem );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-07-30 14:10:07 +02:00
|
|
|
ParserKeyword::ParserKeyword(const Json::JsonObject& jsonConfig) {
|
2013-07-31 11:29:16 +02:00
|
|
|
if (jsonConfig.has_item("name")) {
|
2013-07-31 15:02:00 +02:00
|
|
|
commonInit(jsonConfig.get_string("name"));
|
2013-07-30 14:10:07 +02:00
|
|
|
} else
|
2013-07-31 11:29:16 +02:00
|
|
|
throw std::invalid_argument("Json object is missing name: property");
|
2013-07-31 17:58:05 +02:00
|
|
|
|
|
|
|
|
if (jsonConfig.has_item("size")) {
|
2013-08-06 16:26:49 +02:00
|
|
|
Json::JsonObject sizeObject = jsonConfig.get_item("size");
|
|
|
|
|
|
|
|
|
|
if (sizeObject.is_number()) {
|
|
|
|
|
m_fixedSize = (size_t) sizeObject.as_int( );
|
|
|
|
|
m_keywordSizeType = FIXED;
|
|
|
|
|
} else {
|
|
|
|
|
std::string sizeKeyword = sizeObject.get_string("keyword");
|
|
|
|
|
std::string sizeItem = sizeObject.get_string("item");
|
|
|
|
|
initSizeKeyword( sizeKeyword , sizeItem);
|
|
|
|
|
}
|
2013-07-31 17:58:05 +02:00
|
|
|
} else
|
|
|
|
|
m_keywordSizeType = UNDEFINED;
|
|
|
|
|
|
|
|
|
|
if (jsonConfig.has_item("items"))
|
|
|
|
|
addItems( jsonConfig );
|
2013-07-30 14:10:07 +02:00
|
|
|
}
|
2013-07-31 17:58:05 +02:00
|
|
|
|
2013-07-31 15:02:00 +02:00
|
|
|
|
2013-08-06 16:26:49 +02:00
|
|
|
void ParserKeyword::initSizeKeyword( const std::string& sizeKeyword, const std::string& sizeItem) {
|
|
|
|
|
m_keywordSizeType = OTHER;
|
|
|
|
|
m_sizeKeyword = std::pair<std::string , std::string>(sizeKeyword , sizeItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-07-31 15:02:00 +02:00
|
|
|
void ParserKeyword::commonInit(const std::string& name) {
|
2013-06-20 15:40:45 +02:00
|
|
|
if (name.length() > ParserConst::maxKeywordLength)
|
2013-05-06 12:13:49 +02:00
|
|
|
throw std::invalid_argument("Given keyword name is too long - max 8 characters.");
|
2013-07-31 15:02:00 +02:00
|
|
|
|
2013-05-06 12:13:49 +02:00
|
|
|
for (unsigned int i = 0; i < name.length(); i++)
|
|
|
|
|
if (islower(name[i]))
|
|
|
|
|
throw std::invalid_argument("Keyword must be all upper case - mixed case not allowed:" + name);
|
2013-07-31 15:02:00 +02:00
|
|
|
|
2013-05-27 14:28:23 +02:00
|
|
|
m_name = name;
|
2013-07-31 15:02:00 +02:00
|
|
|
m_record = ParserRecordPtr(new ParserRecord);
|
2013-05-06 12:13:49 +02:00
|
|
|
}
|
2013-03-21 15:37:40 +01:00
|
|
|
|
|
|
|
|
|
2013-07-31 17:58:05 +02:00
|
|
|
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") );
|
2013-08-01 09:31:27 +02:00
|
|
|
std::cout << "ValueType : " << itemConfig.get_string("value_type") << "Numeric: " << valueType << std::endl;
|
2013-07-31 17:58:05 +02:00
|
|
|
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;
|
2013-08-01 09:31:27 +02:00
|
|
|
case FLOAT:
|
|
|
|
|
{
|
|
|
|
|
ParserDoubleItemConstPtr item = ParserDoubleItemConstPtr(new ParserDoubleItem( itemConfig ));
|
|
|
|
|
m_record->addItem( item );
|
|
|
|
|
}
|
|
|
|
|
break;
|
2013-07-31 17:58:05 +02:00
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 15:02:00 +02:00
|
|
|
ParserRecordPtr ParserKeyword::getRecord() {
|
2013-05-27 14:28:23 +02:00
|
|
|
return m_record;
|
|
|
|
|
}
|
2013-06-21 15:34:06 +02:00
|
|
|
|
2013-07-31 15:02:00 +02:00
|
|
|
|
2013-06-20 15:30:37 +02:00
|
|
|
const std::string& ParserKeyword::getName() const {
|
2013-05-06 12:13:49 +02:00
|
|
|
return m_name;
|
|
|
|
|
}
|
2013-03-21 15:37:40 +01:00
|
|
|
|
2013-06-20 15:30:37 +02:00
|
|
|
DeckKeywordPtr ParserKeyword::parse(RawKeywordConstPtr rawKeyword) const {
|
|
|
|
|
DeckKeywordPtr keyword(new DeckKeyword(getName()));
|
2013-06-21 15:34:06 +02:00
|
|
|
for (size_t i = 0; i < rawKeyword->size(); i++) {
|
|
|
|
|
DeckRecordConstPtr deckRecord = m_record->parse(rawKeyword->getRecord(i));
|
|
|
|
|
keyword->addRecord(deckRecord);
|
2013-06-03 15:54:16 +02:00
|
|
|
}
|
2013-06-21 15:34:06 +02:00
|
|
|
|
2013-06-03 15:54:16 +02:00
|
|
|
return keyword;
|
2013-05-27 14:28:23 +02:00
|
|
|
}
|
2013-06-24 14:08:53 +02:00
|
|
|
|
2013-06-21 15:34:06 +02:00
|
|
|
size_t ParserKeyword::getFixedSize() const {
|
2013-06-24 14:08:53 +02:00
|
|
|
if (!hasFixedSize()) {
|
|
|
|
|
throw std::logic_error("This parser keyword does not have a fixed size!");
|
|
|
|
|
}
|
|
|
|
|
return m_fixedSize;
|
2013-06-21 15:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-24 14:08:53 +02:00
|
|
|
bool ParserKeyword::hasFixedSize() const {
|
|
|
|
|
return m_keywordSizeType == FIXED;
|
|
|
|
|
}
|
2013-08-06 16:26:49 +02:00
|
|
|
|
|
|
|
|
enum ParserKeywordSizeEnum ParserKeyword::getSizeType() const {
|
|
|
|
|
return m_keywordSizeType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::pair<std::string,std::string>& ParserKeyword::getSizePair() const {
|
|
|
|
|
return m_sizeKeyword;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-21 15:37:40 +01:00
|
|
|
}
|