Changed parser to support parsing of keywords with wildcard like

"TVDP*". An importtant change is that we now query the parser
canParseKeyword() instead of hasKeyword().
This commit is contained in:
Joakim Hove
2013-12-01 09:28:14 +01:00
parent 390d17aa3e
commit abd0905171
6 changed files with 131 additions and 52 deletions

View File

@@ -50,8 +50,8 @@ ParserPtr createWWCTParser() {
BOOST_AUTO_TEST_CASE(parse_fileWithWWCTKeyword_deckReturned) {
boost::filesystem::path singleKeywordFile("testdata/integration_tests/wwct.data");
ParserPtr parser = createWWCTParser();
BOOST_CHECK( parser->hasKeyword("WWCT"));
BOOST_CHECK( parser->hasKeyword("SUMMARY"));
BOOST_CHECK( parser->canParseKeyword("WWCT"));
BOOST_CHECK( parser->canParseKeyword("SUMMARY"));
BOOST_CHECK_NO_THROW(DeckPtr deck = parser->parse(singleKeywordFile.string()));
}

View File

@@ -46,9 +46,9 @@ BOOST_AUTO_TEST_CASE( parse_ACTION_OK ) {
parser->addKeyword( DIMENS );
parser->addKeyword( THROW );
BOOST_REQUIRE( parser->hasKeyword( "DIMENS" ));
BOOST_REQUIRE( parser->hasKeyword( "WCONHIST" ));
BOOST_REQUIRE( parser->hasKeyword( "THROW" ));
BOOST_REQUIRE( parser->canParseKeyword( "DIMENS" ));
BOOST_REQUIRE( parser->canParseKeyword( "WCONHIST" ));
BOOST_REQUIRE( parser->canParseKeyword( "THROW" ));
BOOST_REQUIRE_THROW( parser->parse( actionFile2.string() , false) , std::invalid_argument );

View File

@@ -63,31 +63,82 @@ namespace Opm {
size_t Parser::size() const {
return m_parserKeywords.size();
}
void Parser::addKeyword(ParserKeywordConstPtr parserKeyword) {
if (hasKeyword(parserKeyword->getName()))
m_parserKeywords.erase(parserKeyword->getName());
m_parserKeywords.insert(std::make_pair(parserKeyword->getName(), parserKeyword));
const std::string& name = parserKeyword->getName();
dropKeyword( name );
m_parserKeywords.insert(std::make_pair(name, parserKeyword));
if (ParserKeyword::wildCardName(name))
m_wildCardKeywords.insert( std::make_pair(name , parserKeyword ));
}
ParserKeywordConstPtr Parser::matchingKeyword(const std::string& name) const {
std::map<std::string, ParserKeywordConstPtr>::const_iterator iter = m_wildCardKeywords.begin();
ParserKeywordConstPtr keyword;
while (true) {
if (iter == m_wildCardKeywords.end())
break;
if ((*iter).second->matches(name)) {
keyword = (*iter).second;
break;
}
++iter;
}
return keyword;
}
bool Parser::hasKeyword(const std::string& keyword) const {
return m_parserKeywords.find(keyword) != m_parserKeywords.end();
return (m_parserKeywords.find(keyword) != m_parserKeywords.end());
}
bool Parser::dropKeyword(const std::string& keyword) {
if (m_parserKeywords.erase( keyword ) == 1)
return true;
else
return false;
bool Parser::hasWildCardKeyword(const std::string& keyword) const {
return (m_wildCardKeywords.find(keyword) != m_parserKeywords.end());
}
bool Parser::canParseKeyword( const std::string& keyword) const {
if (hasKeyword(keyword))
return true;
else {
ParserKeywordConstPtr wildCardKeyword = matchingKeyword( keyword );
if (wildCardKeyword)
return true;
else
return false;
}
}
bool Parser::dropKeyword(const std::string& keyword) {
bool erase = (m_parserKeywords.erase( keyword ) == 1);
if (erase)
m_wildCardKeywords.erase( keyword );
return erase;
}
ParserKeywordConstPtr Parser::getKeyword(const std::string& keyword) const {
if (hasKeyword(keyword)) {
return m_parserKeywords.at(keyword);
} else {
ParserKeywordConstPtr wildCardKeyword = matchingKeyword( keyword );
if (wildCardKeyword)
return wildCardKeyword;
else
throw std::invalid_argument("Do not have parser keyword for parsing: " + keyword);
}
else
throw std::invalid_argument("Keyword: " + keyword + " does not exist");
}
@@ -117,9 +168,9 @@ namespace Opm {
} else {
if (verbose)
std::cout << rawKeyword->getKeywordName() << std::endl;
if (hasKeyword(rawKeyword->getKeywordName())) {
ParserKeywordConstPtr parserKeyword = m_parserKeywords.at(rawKeyword->getKeywordName());
if (canParseKeyword(rawKeyword->getKeywordName())) {
ParserKeywordConstPtr parserKeyword = getKeyword(rawKeyword->getKeywordName());
ParserKeywordActionEnum action = parserKeyword->getAction();
if (action == INTERNALIZE) {
DeckKeywordPtr deckKeyword = parserKeyword->parse(rawKeyword);
@@ -154,8 +205,8 @@ namespace Opm {
}
RawKeywordPtr Parser::createRawKeyword(const DeckConstPtr deck, const std::string& filename , size_t lineNR , const std::string& keywordString, bool strictParsing) const {
if (hasKeyword(keywordString)) {
ParserKeywordConstPtr parserKeyword = m_parserKeywords.find(keywordString)->second;
if (canParseKeyword(keywordString)) {
ParserKeywordConstPtr parserKeyword = getKeyword( keywordString );
ParserKeywordActionEnum action = parserKeyword->getAction();
if (action == THROW_EXCEPTION)
@@ -192,7 +243,7 @@ namespace Opm {
bool Parser::tryParseKeyword(const DeckConstPtr deck, const std::string& filename , size_t& lineNR , std::ifstream& inputstream, RawKeywordPtr& rawKeyword, bool strictParsing) const {
std::string line;
std::cout << "tryParse " << std::endl;
while (std::getline(inputstream, line)) {
std::string keywordString;
lineNR++;

View File

@@ -46,8 +46,8 @@ namespace Opm {
/// Method to add ParserKeyword instances, these holding type and size information about the keywords and their data.
void addKeyword(ParserKeywordConstPtr parserKeyword);
bool hasKeyword(const std::string& keyword) const;
bool dropKeyword(const std::string& keyword);
bool canParseKeyword( const std::string& keyword) const;
ParserKeywordConstPtr getKeyword(const std::string& keyword) const;
void loadKeywords(const Json::JsonObject& jsonKeywords);
@@ -57,6 +57,12 @@ namespace Opm {
size_t size() const;
private:
std::map<std::string, ParserKeywordConstPtr> m_parserKeywords;
std::map<std::string, ParserKeywordConstPtr> m_wildCardKeywords;
bool hasKeyword(const std::string& keyword) const;
bool hasWildCardKeyword(const std::string& keyword) const;
ParserKeywordConstPtr matchingKeyword(const std::string& keyword) const;
bool tryParseKeyword(const DeckConstPtr deck , const std::string& filename , size_t& lineNR , std::ifstream& inputstream , RawKeywordPtr& rawKeyword, bool strictParsing) const;
void parseFile(DeckPtr deck , const boost::filesystem::path& file, const boost::filesystem::path& rootPath, bool strictParsing) const;
RawKeywordPtr createRawKeyword(const DeckConstPtr deck , const std::string& filename , size_t lineNR , const std::string& keywordString, bool strictParsing) const;

View File

@@ -308,7 +308,7 @@ namespace Opm {
}
DeckKeywordPtr ParserKeyword::parse(RawKeywordConstPtr rawKeyword) const {
DeckKeywordPtr keyword(new DeckKeyword(getName()));
DeckKeywordPtr keyword(new DeckKeyword(rawKeyword->getKeywordName()));
for (size_t i = 0; i < rawKeyword->size(); i++) {
DeckRecordConstPtr deckRecord = m_record->parse(rawKeyword->getRecord(i));
keyword->addRecord(deckRecord);

View File

@@ -52,10 +52,10 @@ BOOST_AUTO_TEST_CASE(addKeyword_keyword_doesntfail) {
}
BOOST_AUTO_TEST_CASE(hasKeyword_hasKeyword_returnstrue) {
BOOST_AUTO_TEST_CASE(canParseKeyword_canParseKeyword_returnstrue) {
ParserPtr parser(new Parser());
parser->addKeyword(ParserKeywordConstPtr(new ParserKeyword("FJAS")));
BOOST_CHECK(parser->hasKeyword("FJAS"));
BOOST_CHECK(parser->canParseKeyword("FJAS"));
}
@@ -77,11 +77,11 @@ BOOST_AUTO_TEST_CASE(getKeyword_hasnotkeyword_throws) {
/************************ JSON config related tests **********************'*/
BOOST_AUTO_TEST_CASE(addKeywordJSON_hasKeyword_returnstrue) {
BOOST_AUTO_TEST_CASE(addKeywordJSON_canParseKeyword_returnstrue) {
ParserPtr parser(new Parser());
Json::JsonObject jsonConfig("{\"name\": \"BPR\", \"size\" : 100 , \"items\" :[{\"name\":\"ItemX\" , \"size_type\":\"SINGLE\" , \"value_type\" : \"FLOAT\"}]}");
parser->addKeyword(ParserKeywordConstPtr(new ParserKeyword( jsonConfig )));
BOOST_CHECK(parser->hasKeyword("BPR"));
BOOST_CHECK(parser->canParseKeyword("BPR"));
}
@@ -89,7 +89,7 @@ BOOST_AUTO_TEST_CASE(addKeywordJSON_size_isObject_allGood) {
ParserPtr parser(new Parser());
Json::JsonObject jsonConfig("{\"name\": \"EQUIXL\", \"size\" : {\"keyword\":\"EQLDIMS\" , \"item\" : \"NTEQUL\"}, \"items\" :[{\"name\":\"ItemX\" , \"size_type\":\"SINGLE\" , \"value_type\" : \"FLOAT\"}]}");
parser->addKeyword(ParserKeywordConstPtr(new ParserKeyword( jsonConfig )));
BOOST_CHECK(parser->hasKeyword("EQUIXL"));
BOOST_CHECK(parser->canParseKeyword("EQUIXL"));
}
@@ -103,12 +103,12 @@ BOOST_AUTO_TEST_CASE(loadKeywordsJSON_notArray_throw) {
BOOST_AUTO_TEST_CASE(loadKeywordsJSON_hasKeyword_returnstrue) {
BOOST_AUTO_TEST_CASE(loadKeywordsJSON_canParseKeyword_returnstrue) {
ParserPtr parser(new Parser());
Json::JsonObject jsonConfig( "[{\"name\" : \"BPR\" , \"size\" : 100, \"items\" :[{\"name\":\"ItemX\" , \"size_type\":\"SINGLE\" , \"value_type\" : \"FLOAT\"}]}]");
parser->loadKeywords( jsonConfig );
BOOST_CHECK(parser->hasKeyword("BPR"));
BOOST_CHECK(parser->canParseKeyword("BPR"));
}
@@ -124,9 +124,9 @@ BOOST_AUTO_TEST_CASE(loadKeywordsJSON_manyKeywords_returnstrue) {
Json::JsonObject jsonConfig( "[{\"name\" : \"BPR\" , \"size\" : 100 , \"items\" :[{\"name\":\"ItemX\" , \"size_type\":\"SINGLE\" , \"value_type\" : \"FLOAT\"}]}, {\"name\" : \"WWCT\", \"size\" : 0} , {\"name\" : \"EQUIL\" , \"size\" : 0}]");
parser->loadKeywords( jsonConfig );
BOOST_CHECK(parser->hasKeyword("BPR"));
BOOST_CHECK(parser->hasKeyword("WWCT"));
BOOST_CHECK(parser->hasKeyword("EQUIL"));
BOOST_CHECK(parser->canParseKeyword("BPR"));
BOOST_CHECK(parser->canParseKeyword("WWCT"));
BOOST_CHECK(parser->canParseKeyword("EQUIL"));
BOOST_CHECK_EQUAL( 3U , parser->size() );
}
@@ -162,7 +162,7 @@ BOOST_AUTO_TEST_CASE(loadKeywordFromFile_validKeyword_returnsTrueHasKeyword) {
boost::filesystem::path configFile("testdata/json/BPR");
BOOST_CHECK_EQUAL( true , parser->loadKeywordFromFile( configFile ));
BOOST_CHECK_EQUAL( 1U , parser->size() );
BOOST_CHECK_EQUAL( true , parser->hasKeyword("BPR") );
BOOST_CHECK_EQUAL( true , parser->canParseKeyword("BPR") );
}
@@ -175,12 +175,12 @@ BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_directoryDoesNotexist_throws) {
BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_notRecursive_allNames) {
ParserPtr parser(new Parser(false));
BOOST_CHECK_EQUAL(false , parser->hasKeyword("BPR"));
BOOST_CHECK_EQUAL(false , parser->canParseKeyword("BPR"));
boost::filesystem::path configPath("testdata/config/directory1");
BOOST_CHECK_NO_THROW(parser->loadKeywordsFromDirectory( configPath, false , false));
BOOST_CHECK(parser->hasKeyword("WWCT"));
BOOST_CHECK_EQUAL(true , parser->hasKeyword("BPR"));
BOOST_CHECK_EQUAL(false , parser->hasKeyword("DIMENS"));
BOOST_CHECK(parser->canParseKeyword("WWCT"));
BOOST_CHECK_EQUAL(true , parser->canParseKeyword("BPR"));
BOOST_CHECK_EQUAL(false , parser->canParseKeyword("DIMENS"));
}
@@ -188,31 +188,31 @@ BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_notRecursive_strictNames) {
ParserPtr parser(new Parser(false));
boost::filesystem::path configPath("testdata/config/directory1");
BOOST_CHECK_NO_THROW(parser->loadKeywordsFromDirectory( configPath, false , true ));
BOOST_CHECK(parser->hasKeyword("WWCT"));
BOOST_CHECK_EQUAL(false , parser->hasKeyword("BPR"));
BOOST_CHECK_EQUAL(false , parser->hasKeyword("DIMENS"));
BOOST_CHECK(parser->canParseKeyword("WWCT"));
BOOST_CHECK_EQUAL(false , parser->canParseKeyword("BPR"));
BOOST_CHECK_EQUAL(false , parser->canParseKeyword("DIMENS"));
}
BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_Recursive_allNames) {
ParserPtr parser(new Parser(false));
BOOST_CHECK_EQUAL(false , parser->hasKeyword("BPR"));
BOOST_CHECK_EQUAL(false , parser->canParseKeyword("BPR"));
boost::filesystem::path configPath("testdata/config/directory1");
BOOST_CHECK_NO_THROW(parser->loadKeywordsFromDirectory( configPath, true, false));
BOOST_CHECK(parser->hasKeyword("WWCT"));
BOOST_CHECK_EQUAL(true , parser->hasKeyword("BPR"));
BOOST_CHECK_EQUAL(true , parser->hasKeyword("DIMENS"));
BOOST_CHECK(parser->canParseKeyword("WWCT"));
BOOST_CHECK_EQUAL(true , parser->canParseKeyword("BPR"));
BOOST_CHECK_EQUAL(true , parser->canParseKeyword("DIMENS"));
}
BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_default) {
ParserPtr parser(new Parser(false));
BOOST_CHECK_EQUAL(false , parser->hasKeyword("BPR"));
BOOST_CHECK_EQUAL(false , parser->canParseKeyword("BPR"));
boost::filesystem::path configPath("testdata/config/directory1");
BOOST_CHECK_NO_THROW(parser->loadKeywordsFromDirectory( configPath ));
BOOST_CHECK(parser->hasKeyword("WWCT"));
BOOST_CHECK_EQUAL(false , parser->hasKeyword("BPR"));
BOOST_CHECK_EQUAL(true , parser->hasKeyword("DIMENS"));
BOOST_CHECK(parser->canParseKeyword("WWCT"));
BOOST_CHECK_EQUAL(false , parser->canParseKeyword("BPR"));
BOOST_CHECK_EQUAL(true , parser->canParseKeyword("DIMENS"));
}
@@ -221,6 +221,10 @@ BOOST_AUTO_TEST_CASE(DropKeyword) {
BOOST_CHECK_EQUAL(false , parser->dropKeyword("DoesNotHaveThis"));
BOOST_CHECK_EQUAL(true , parser->dropKeyword("BPR"));
BOOST_CHECK_EQUAL(false , parser->dropKeyword("BPR"));
BOOST_CHECK_EQUAL(true , parser->canParseKeyword("TVDPX"));
BOOST_CHECK_EQUAL(true , parser->dropKeyword("TVDP*"));
BOOST_CHECK_EQUAL(false , parser->canParseKeyword("TVDPX"));
}
@@ -237,6 +241,24 @@ BOOST_AUTO_TEST_CASE(ReplaceKeyword) {
}
BOOST_AUTO_TEST_CASE(WildCardTest) {
ParserPtr parser(new Parser());
BOOST_CHECK_EQUAL(true , parser->canParseKeyword("TVDP*"));
BOOST_CHECK_EQUAL(true , parser->canParseKeyword("TVDP"));
BOOST_CHECK_EQUAL(true , parser->canParseKeyword("TVDPXXX"));
BOOST_CHECK_EQUAL(false , parser->canParseKeyword("TVD"));
ParserKeywordConstPtr keyword1 = parser->getKeyword("TVDP*");
ParserKeywordConstPtr keyword2 = parser->getKeyword("TVDP");
ParserKeywordConstPtr keyword3 = parser->getKeyword("TVDPXXX");
BOOST_CHECK_EQUAL( keyword1 , keyword2 );
BOOST_CHECK_EQUAL( keyword1 , keyword3 );
}
/***************** Simple Int parsing ********************************/
ParserKeywordPtr setupParserKeywordInt(std::string name, int numberOfItems) {