Now also reading unrecognized keywords, labeled such in DeckKeyword class
This commit is contained in:
parent
c7fa8a2e0a
commit
5f1c2722b5
@ -22,6 +22,7 @@
|
||||
namespace Opm {
|
||||
|
||||
DeckKeyword::DeckKeyword(const std::string& keywordName) {
|
||||
m_knownKeyword = true;
|
||||
m_keywordName = keywordName;
|
||||
}
|
||||
|
||||
@ -33,6 +34,15 @@ namespace Opm {
|
||||
return m_recordList.size();
|
||||
}
|
||||
|
||||
void DeckKeyword::setUnknown() {
|
||||
m_knownKeyword = false;
|
||||
}
|
||||
|
||||
|
||||
bool DeckKeyword::isKnown() const {
|
||||
return m_knownKeyword;
|
||||
}
|
||||
|
||||
void DeckKeyword::addRecord(DeckRecordConstPtr record) {
|
||||
m_recordList.push_back(record);
|
||||
}
|
||||
@ -43,6 +53,5 @@ namespace Opm {
|
||||
} else
|
||||
throw std::range_error("Index out of range");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -23,10 +23,13 @@ namespace Opm {
|
||||
size_t size() const;
|
||||
void addRecord(DeckRecordConstPtr record);
|
||||
DeckRecordConstPtr getRecord(size_t index) const;
|
||||
void setUnknown();
|
||||
bool isKnown() const;
|
||||
|
||||
private:
|
||||
std::string m_keywordName;
|
||||
std::vector<DeckRecordConstPtr> m_recordList;
|
||||
bool m_knownKeyword;
|
||||
|
||||
};
|
||||
typedef boost::shared_ptr<DeckKeyword> DeckKeywordPtr;
|
||||
|
@ -63,6 +63,14 @@ BOOST_AUTO_TEST_CASE(getRecord_outofrange_exceptionthrown) {
|
||||
BOOST_CHECK_THROW(deckKeyword->getRecord(1), std::range_error);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(setUnknown_wasknown_nowunknown) {
|
||||
DeckKeywordPtr deckKeyword(new DeckKeyword("KW"));
|
||||
BOOST_CHECK(deckKeyword->isKnown());
|
||||
deckKeyword->setUnknown();
|
||||
BOOST_CHECK(!deckKeyword->isKnown());
|
||||
deckKeyword->setUnknown();
|
||||
BOOST_CHECK(!deckKeyword->isKnown());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -141,7 +141,6 @@ BOOST_AUTO_TEST_CASE(parse_fileWithBPRKeyword_dataiscorrect) {
|
||||
BOOST_CHECK_EQUAL(3, K1->getInt(0));
|
||||
K1 = record2->getItem("K");
|
||||
BOOST_CHECK_EQUAL(3, K1->getInt(0));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -158,10 +157,10 @@ BOOST_AUTO_TEST_CASE(Parse_ValidInputFile_NoThrow) {
|
||||
}
|
||||
|
||||
/***************** Testing non-recognized keywords ********************/
|
||||
//BOOST_AUTO_TEST_CASE(loadKeywordsJSON_manyKeywords_returnstrue) {
|
||||
// ParserPtr parser(new Parser());
|
||||
// Json::JsonObject jsonConfig( "[{\"name\" : \"BPR\" , \"size\" : 100}, {\"name\" : \"WWCT\"} , {\"name\" : \"EQUIL\" , \"size\" : 100}]");
|
||||
// parser->loadKeywords( jsonConfig );
|
||||
//
|
||||
// parser->parse("testdata/someobscureelements.data");
|
||||
//}
|
||||
BOOST_AUTO_TEST_CASE(loadKeywordsJSON_manyKeywords_returnstrue) {
|
||||
ParserPtr parser(new Parser(JSON_CONFIG_FILE));
|
||||
DeckPtr deck = parser->parse("testdata/integration_tests/someobscureelements.data");
|
||||
BOOST_CHECK_EQUAL(4U, deck->size());
|
||||
DeckKeywordConstPtr unknown = deck->getKeyword("GRUDINT");
|
||||
BOOST_CHECK(!unknown->isKnown());
|
||||
}
|
||||
|
@ -33,96 +33,93 @@ namespace Opm {
|
||||
}
|
||||
|
||||
Parser::Parser(const boost::filesystem::path& jsonFile) {
|
||||
initializeFromJsonFile( jsonFile );
|
||||
initializeFromJsonFile(jsonFile);
|
||||
}
|
||||
|
||||
|
||||
DeckPtr Parser::parse(const std::string &dataFile) {
|
||||
DeckPtr deck(new Deck());
|
||||
|
||||
parseFile( deck , dataFile );
|
||||
parseFile(deck, dataFile);
|
||||
return deck;
|
||||
}
|
||||
|
||||
|
||||
void Parser::parseFile(DeckPtr deck , const std::string &file) {
|
||||
void Parser::parseFile(DeckPtr deck, const std::string &file) {
|
||||
std::ifstream inputstream;
|
||||
inputstream.open( file.c_str() );
|
||||
|
||||
inputstream.open(file.c_str());
|
||||
|
||||
if (inputstream) {
|
||||
RawKeywordPtr rawKeyword;
|
||||
|
||||
while (tryParseKeyword(deck , inputstream , rawKeyword)) {
|
||||
if (rawKeyword->getKeywordName() == Opm::RawConsts::include) {
|
||||
boost::filesystem::path dataFolderPath = verifyValidInputPath(file);
|
||||
RawRecordConstPtr firstRecord = rawKeyword->getRecord(0);
|
||||
std::string includeFileString = firstRecord->getItem(0);
|
||||
boost::filesystem::path pathToIncludedFile(dataFolderPath);
|
||||
pathToIncludedFile /= includeFileString;
|
||||
|
||||
parseFile( deck , pathToIncludedFile.string() );
|
||||
} else {
|
||||
ParserKeywordConstPtr parserKeyword = m_parserKeywords[rawKeyword->getKeywordName()];
|
||||
DeckKeywordConstPtr deckKeyword = parserKeyword->parse(rawKeyword);
|
||||
deck->addKeyword( deckKeyword );
|
||||
RawKeywordPtr rawKeyword;
|
||||
|
||||
while (tryParseKeyword(deck, inputstream, rawKeyword)) {
|
||||
if (rawKeyword->getKeywordName() == Opm::RawConsts::include) {
|
||||
boost::filesystem::path dataFolderPath = verifyValidInputPath(file);
|
||||
RawRecordConstPtr firstRecord = rawKeyword->getRecord(0);
|
||||
std::string includeFileString = firstRecord->getItem(0);
|
||||
boost::filesystem::path pathToIncludedFile(dataFolderPath);
|
||||
pathToIncludedFile /= includeFileString;
|
||||
|
||||
parseFile(deck, pathToIncludedFile.string());
|
||||
} else {
|
||||
if (m_parserKeywords.find(rawKeyword->getKeywordName()) == m_parserKeywords.end()) {
|
||||
DeckKeywordPtr deckKeyword(new DeckKeyword(rawKeyword->getKeywordName()));
|
||||
deckKeyword->setUnknown();
|
||||
deck->addKeyword(deckKeyword);
|
||||
|
||||
} else {
|
||||
ParserKeywordConstPtr parserKeyword = m_parserKeywords[rawKeyword->getKeywordName()];
|
||||
DeckKeywordConstPtr deckKeyword = parserKeyword->parse(rawKeyword);
|
||||
deck->addKeyword(deckKeyword);
|
||||
}
|
||||
}
|
||||
rawKeyword.reset();
|
||||
}
|
||||
rawKeyword.reset();
|
||||
}
|
||||
|
||||
inputstream.close();
|
||||
|
||||
inputstream.close();
|
||||
} else
|
||||
throw std::invalid_argument("Failed to open file: " + file);
|
||||
throw std::invalid_argument("Failed to open file: " + file);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Parser::addKeyword(ParserKeywordConstPtr parserKeyword) {
|
||||
m_parserKeywords.insert(std::make_pair(parserKeyword->getName(), parserKeyword));
|
||||
}
|
||||
|
||||
|
||||
void Parser::initializeFromJsonFile( const boost::filesystem::path& jsonFile ) {
|
||||
void Parser::initializeFromJsonFile(const boost::filesystem::path& jsonFile) {
|
||||
Json::JsonObject jsonConfig(jsonFile);
|
||||
if (jsonConfig.has_item("keywords")) {
|
||||
Json::JsonObject jsonKeywords = jsonConfig.get_item("keywords");
|
||||
loadKeywords( jsonKeywords );
|
||||
loadKeywords(jsonKeywords);
|
||||
} else
|
||||
throw std::invalid_argument("Missing \"keywords\" section in config file: " + jsonFile.string());
|
||||
}
|
||||
|
||||
|
||||
void Parser::loadKeywords(const Json::JsonObject& jsonKeywords) {
|
||||
if (jsonKeywords.is_array()) {
|
||||
for (size_t index = 0; index < jsonKeywords.size(); index++) {
|
||||
Json::JsonObject jsonKeyword = jsonKeywords.get_array_item( index );
|
||||
ParserKeywordConstPtr parserKeyword( new ParserKeyword( jsonKeyword ));
|
||||
|
||||
addKeyword( parserKeyword );
|
||||
Json::JsonObject jsonKeyword = jsonKeywords.get_array_item(index);
|
||||
ParserKeywordConstPtr parserKeyword(new ParserKeyword(jsonKeyword));
|
||||
|
||||
addKeyword(parserKeyword);
|
||||
}
|
||||
} else
|
||||
throw std::invalid_argument("Input JSON object is not an array");
|
||||
}
|
||||
|
||||
|
||||
bool Parser::hasKeyword(const std::string& keyword) const {
|
||||
return m_parserKeywords.find(keyword) != m_parserKeywords.end();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
RawKeywordPtr Parser::createRawKeyword(const DeckConstPtr deck , const std::string& keywordString) {
|
||||
RawKeywordPtr Parser::createRawKeyword(const DeckConstPtr deck, const std::string& keywordString) {
|
||||
if (hasKeyword(keywordString)) {
|
||||
ParserKeywordConstPtr parserKeyword = m_parserKeywords.find(keywordString)->second;
|
||||
if (parserKeyword->getSizeType() == UNDEFINED)
|
||||
if (parserKeyword->getSizeType() == UNDEFINED)
|
||||
return RawKeywordPtr(new RawKeyword(keywordString));
|
||||
else {
|
||||
size_t targetSize;
|
||||
|
||||
|
||||
if (parserKeyword->hasFixedSize())
|
||||
targetSize = parserKeyword->getFixedSize();
|
||||
else {
|
||||
const std::pair<std::string,std::string> sizeKeyword = parserKeyword->getSizeDefinitionPair();
|
||||
const std::pair<std::string, std::string> sizeKeyword = parserKeyword->getSizeDefinitionPair();
|
||||
DeckKeywordConstPtr sizeDefinitionKeyword = deck->getKeyword(sizeKeyword.first);
|
||||
DeckItemConstPtr sizeDefinitionItem;
|
||||
{
|
||||
@ -131,16 +128,19 @@ namespace Opm {
|
||||
}
|
||||
targetSize = sizeDefinitionItem->getInt(0);
|
||||
}
|
||||
return RawKeywordPtr(new RawKeyword(keywordString , targetSize));
|
||||
return RawKeywordPtr(new RawKeyword(keywordString, targetSize));
|
||||
}
|
||||
} else
|
||||
throw std::invalid_argument("Keyword " + keywordString + " not recognized ");
|
||||
} else {
|
||||
bool strict = false;
|
||||
if (strict) {
|
||||
throw std::invalid_argument("Keyword " + keywordString + " not recognized ");
|
||||
} else {
|
||||
return RawKeywordPtr(new RawKeyword(keywordString, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool Parser::tryParseKeyword(const DeckConstPtr deck , std::ifstream& inputstream , RawKeywordPtr& rawKeyword) {
|
||||
bool Parser::tryParseKeyword(const DeckConstPtr deck, std::ifstream& inputstream, RawKeywordPtr& rawKeyword) {
|
||||
std::string line;
|
||||
|
||||
while (std::getline(inputstream, line)) {
|
||||
@ -148,24 +148,21 @@ namespace Opm {
|
||||
|
||||
if (rawKeyword == NULL) {
|
||||
if (RawKeyword::tryParseKeyword(line, keywordString)) {
|
||||
rawKeyword = createRawKeyword( deck , keywordString );
|
||||
rawKeyword = createRawKeyword(deck, keywordString);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (RawKeyword::useLine(line)) {
|
||||
rawKeyword->addRawRecordString(line);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (rawKeyword != NULL && rawKeyword->isFinished())
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
boost::filesystem::path Parser::verifyValidInputPath(const std::string& inputPath) const {
|
||||
Logger::info("Verifying path: " + inputPath);
|
||||
boost::filesystem::path pathToInputFile(inputPath);
|
||||
|
@ -1,35 +1,17 @@
|
||||
-- Dette er en supertest - OK
|
||||
OIL
|
||||
|
||||
-- Saa bare en - OK
|
||||
--INCLUDE
|
||||
-- 'include path/readthis.sch' /
|
||||
---- Saa kommer en for lang en, skal bli "trunkert", slik at keywordet blir ABCXYZFF - OK
|
||||
GRIDUNIT
|
||||
METRES /
|
||||
|
||||
GRUDINT -- Denne skjønner vi ikke, men "tar den med" som en ukjent
|
||||
"knaskenspill" 3 5 /
|
||||
3 3 3 3 3 3 /
|
||||
/
|
||||
|
||||
GRIDUNIT Tull kommer her
|
||||
-- Saa kommer det data
|
||||
METRES /
|
||||
|
||||
-- Og en med tall - OK
|
||||
|
||||
RADFIN4
|
||||
213 123 123 /
|
||||
|
||||
-- Og en med tall først- Ikke OK
|
||||
--
|
||||
--5ABCADFC
|
||||
--
|
||||
--
|
||||
---- Og en med mellomrom etter, og et ubestemt antall, terminert av / - OK
|
||||
--
|
||||
--ABCDAD
|
||||
-- 4.5 5 5 /
|
||||
-- 3 3 'FISK_1' /
|
||||
--/
|
||||
--
|
||||
---- Og en med liten bokstav i IKKE OK
|
||||
--
|
||||
--ABCDAd
|
||||
--
|
||||
---- Og en kommentar med space foran, det er ok
|
||||
-- -- Hei her er jeg.
|
||||
|
Loading…
Reference in New Issue
Block a user