Added method to load keyword configurations from directory

This commit is contained in:
Joakim Hove 2013-08-21 12:50:21 +02:00
parent 4202ad027d
commit dfca913462
6 changed files with 87 additions and 0 deletions

View File

@ -108,6 +108,9 @@ namespace Opm {
}
bool Parser::hasKeyword(const std::string& keyword) const {
return m_parserKeywords.find(keyword) != m_parserKeywords.end();
}
@ -191,6 +194,25 @@ namespace Opm {
}
void Parser::loadKeywordsFromDirectory(const boost::filesystem::path& directory, bool recursive, bool onlyALLCAPS8) {
if (!boost::filesystem::exists(directory))
throw std::invalid_argument("Directory: " + directory.string() + " does not exist.");
else {
boost::filesystem::directory_iterator end;
for (boost::filesystem::directory_iterator iter(directory) ; iter != end; iter++) {
if (is_directory(*iter)) {
if (recursive)
loadKeywordsFromDirectory(*iter , recursive , onlyALLCAPS8);
} else {
if (!onlyALLCAPS8 || ParserKeyword::validName( iter->path().filename())) {
if (!loadKeywordFromFile(*iter))
std::cerr << "** Warning: failed to load keyword from file:" << iter->path() << std::endl;
}
}
}
}
}
void Parser::populateDefaultKeywords() {
addKeyword(ParserKeywordConstPtr(new ParserKeyword("GRIDUNIT", 1)));

View File

@ -52,6 +52,8 @@ namespace Opm {
void initializeFromJsonFile( const boost::filesystem::path& jsonFile );
void loadKeywords(const Json::JsonObject& jsonKeywords);
bool loadKeywordFromFile(const boost::filesystem::path& configFile);
void loadKeywordsFromDirectory(const boost::filesystem::path& directory , bool recursive = true, bool onlyALLCAPS8Files = true);
size_t size() const;
private:
std::map<std::string, ParserKeywordConstPtr> m_parserKeywords;

View File

@ -188,6 +188,60 @@ BOOST_AUTO_TEST_CASE(loadKeywordFromFile_validKeyword_returnsTrueHasKeyword) {
BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_directoryDoesNotexist_throws) {
ParserPtr parser(new Parser());
boost::filesystem::path configPath("path/does/not/exist");
BOOST_CHECK_THROW(parser->loadKeywordsFromDirectory( configPath), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_notRecursive_allNames) {
ParserPtr parser(new Parser());
BOOST_CHECK_EQUAL(false , parser->hasKeyword("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_AUTO_TEST_CASE(loadConfigFromDirectory_notRecursive_strictNames) {
ParserPtr parser(new Parser());
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_AUTO_TEST_CASE(loadConfigFromDirectory_Recursive_allNames) {
ParserPtr parser(new Parser());
BOOST_CHECK_EQUAL(false , parser->hasKeyword("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_AUTO_TEST_CASE(loadConfigFromDirectory_default) {
ParserPtr parser(new Parser());
BOOST_CHECK_EQUAL(false , parser->hasKeyword("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"));
}
/***************** Simple Int parsing ********************************/
ParserKeywordPtr setupParserKeywordInt(std::string name, int numberOfItems) {

4
testdata/config/directory1/Bpr vendored Normal file
View File

@ -0,0 +1,4 @@
{"name" : "BPR" , "items" :
[{"name": "I" , "size_type" : "SINGLE" , "value_type" : "INT"},
{"name": "J" , "size_type" : "SINGLE" , "value_type" : "INT"},
{"name": "K" , "size_type" : "SINGLE" , "value_type" : "INT"}]}

1
testdata/config/directory1/WWCT vendored Normal file
View File

@ -0,0 +1 @@
{"name" : "WWCT", "items" : [{"name" : "wells" , "size_type" : "ALL" , "value_type" : "STRING"}]}

View File

@ -0,0 +1,4 @@
{"name" : "DIMENS", "size" : 1 , "items" :
[{"name": "NX" , "size_type" : "SINGLE" , "value_type" : "INT"},
{"name": "NY" , "size_type" : "SINGLE" , "value_type" : "INT"},
{"name": "NZ" , "size_type" : "SINGLE" , "value_type" : "INT"}]}