Added function to get keyword from deck based on index. Added looping over all keywords in application. This should have been several commits

This commit is contained in:
Kristian Flikka 2013-08-20 15:51:19 +02:00
parent 13ec4f2b38
commit b5dac4b5bd
8 changed files with 74 additions and 5 deletions

View File

@ -1,3 +1,4 @@
add_definitions( -DJSON_CONFIG_FILE="${PROJECT_SOURCE_DIR}/opm/parser/share/parser_config.json")
add_executable(eclipsedatadoctor EclipseDataDoctor.cpp)
target_link_libraries(eclipsedatadoctor Parser)

View File

@ -6,6 +6,8 @@
*/
#include <iostream>
#include <opm/parser/eclipse/Parser/Parser.hpp>
#include <opm/parser/eclipse/Deck/Deck.hpp>
/*
*
@ -13,9 +15,30 @@
int main(int argc, char** argv) {
if (argc <= 1)
{
std::cout << "Usage: " << argv[0] << " <Filename>" << std::endl;
std::cout << "Usage: " << argv[0] << " <Filename>" << "[-a] (list all keywords)" << std::endl;
exit(1);
}
bool showKeywords = false;
for (int i=1; i<argc; i++) {
std::string arg(argv[i]);
if (arg == "-a")
showKeywords = true;
}
std::string file = argv[1];
Opm::ParserPtr parser(new Opm::Parser(JSON_CONFIG_FILE));
try {
Opm::DeckConstPtr deck = parser->parse(file);
std::cout << "Number of keywords: " << deck->size() << std::endl;
if (showKeywords) {
for (size_t i=0; i < deck->size(); i++) {
std::cout << "Keyword" << ": " << deck->getKeyword(i)->name() << std::endl;
}
}
}
catch (std::invalid_argument exception) {
std::cout << "Unable to read file, error:" << std::endl << exception.what() << std::endl;
}
return 0;
}

View File

@ -43,6 +43,10 @@ namespace Opm {
return m_keywords->getKeyword(keyword);
}
DeckKeywordConstPtr Deck::getKeyword(size_t index) const {
return m_keywords->getKeyword(index);
}
size_t Deck::numKeywords(const std::string& keyword) {
return m_keywords->numKeywords( keyword );
}

View File

@ -35,6 +35,8 @@ namespace Opm {
void addKeyword( DeckKeywordConstPtr keyword);
DeckKeywordConstPtr getKeyword(const std::string& keyword , size_t index) const;
DeckKeywordConstPtr getKeyword(const std::string& keyword) const;
DeckKeywordConstPtr getKeyword(size_t index) const;
size_t numKeywords(const std::string& keyword);
const std::vector<DeckKeywordConstPtr>& getKeywordList(const std::string& keyword);
size_t size() const;

View File

@ -63,7 +63,7 @@ namespace Opm {
if (index < keywordList.size())
return keywordList[index];
else
throw std::invalid_argument("Keyword index is out of range.");
throw std::out_of_range("Keyword index is out of range.");
}
@ -72,6 +72,12 @@ namespace Opm {
return keywordList.back();
}
DeckKeywordConstPtr KeywordContainer::getKeyword(size_t index) const {
if (index < m_keywordList.size())
return m_keywordList[index];
else
throw std::out_of_range("Keyword index is out of range.");
}
size_t KeywordContainer::numKeywords(const std::string& keyword) const{
if (hasKeyword(keyword)) {

View File

@ -25,6 +25,8 @@ namespace Opm {
void addKeyword(DeckKeywordConstPtr keyword);
DeckKeywordConstPtr getKeyword(const std::string& keyword, size_t index) const;
DeckKeywordConstPtr getKeyword(const std::string& keyword) const;
DeckKeywordConstPtr getKeyword(size_t index) const;
const std::vector<DeckKeywordConstPtr>& getKeywordList(const std::string& keyword) const;
size_t numKeywords(const std::string& keyword) const;

View File

@ -65,7 +65,7 @@ BOOST_AUTO_TEST_CASE(getKeyword_singlekeyword_outRange_throws) {
Deck deck;
DeckKeywordConstPtr keyword(new DeckKeyword("BJARNE"));
deck.addKeyword(keyword);
BOOST_CHECK_THROW(deck.getKeyword("BJARNE" , 10) , std::invalid_argument);
BOOST_CHECK_THROW(deck.getKeyword("BJARNE" , 10) , std::out_of_range);
}
@ -77,6 +77,13 @@ BOOST_AUTO_TEST_CASE(getKeywordList_returnOK) {
}
BOOST_AUTO_TEST_CASE(getKeyword_indexok_returnskeyword) {
Deck deck;
DeckKeywordConstPtr keyword(new DeckKeyword("BJARNE"));
deck.addKeyword(keyword);
BOOST_CHECK_NO_THROW(deck.getKeyword(0));
}
BOOST_AUTO_TEST_CASE(numKeyword_singlekeyword_return1) {
Deck deck;
DeckKeywordConstPtr keyword(new DeckKeyword("BJARNE"));

View File

@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE(getKeyword_outOfRange_throws) {
KeywordContainerPtr container(new KeywordContainer());
DeckKeywordPtr keyword = DeckKeywordPtr(new DeckKeyword("TRULS"));
container->addKeyword(keyword);
BOOST_CHECK_THROW( container->getKeyword("TRULS" , 3) , std::invalid_argument)
BOOST_CHECK_THROW( container->getKeyword("TRULS" , 3) , std::out_of_range)
}
@ -126,7 +126,31 @@ BOOST_AUTO_TEST_CASE(keywordList_getnum_OK) {
}
BOOST_AUTO_TEST_CASE(keywordList_getbyindexoutofbounds_exceptionthrown) {
KeywordContainerPtr container(new KeywordContainer());
BOOST_CHECK_THROW(container->getKeyword(0), std::out_of_range);
DeckKeywordPtr keyword1 = DeckKeywordPtr(new DeckKeyword("TRULS"));
DeckKeywordPtr keyword2 = DeckKeywordPtr(new DeckKeyword("TRULS"));
DeckKeywordPtr keyword3 = DeckKeywordPtr(new DeckKeyword("TRULSX"));
container->addKeyword(keyword1);
container->addKeyword(keyword2);
container->addKeyword(keyword3);
BOOST_CHECK_NO_THROW(container->getKeyword(2));
BOOST_CHECK_THROW(container->getKeyword(3), std::out_of_range);
}
BOOST_AUTO_TEST_CASE(keywordList_getbyindex_correctkeywordreturned) {
KeywordContainerPtr container(new KeywordContainer());
DeckKeywordPtr keyword1 = DeckKeywordPtr(new DeckKeyword("TRULS"));
DeckKeywordPtr keyword2 = DeckKeywordPtr(new DeckKeyword("TRULS"));
DeckKeywordPtr keyword3 = DeckKeywordPtr(new DeckKeyword("TRULSX"));
container->addKeyword(keyword1);
container->addKeyword(keyword2);
container->addKeyword(keyword3);
BOOST_CHECK_EQUAL("TRULS", container->getKeyword(0)->name());
BOOST_CHECK_EQUAL("TRULS", container->getKeyword(1)->name());
BOOST_CHECK_EQUAL("TRULSX", container->getKeyword(2)->name());
}