Starting to wrap up from the top, added the missing DeckKW, and refactored a bit in some of the Raw classes that returned the internal structure

This commit is contained in:
Kristian Flikka 2013-06-03 15:54:16 +02:00
parent f9ae0680eb
commit b43d95c5a2
31 changed files with 533 additions and 190 deletions

View File

@ -12,10 +12,12 @@ RawDeck/RawParserKWs.cpp )
set( deck_source
Deck/Deck.cpp
Deck/DeckKW.cpp
Deck/DeckRecord.cpp
Deck/DeckItem.cpp
Deck/DeckIntItem.cpp
Deck/DeckDoubleItem.cpp
Deck/DeckRecord.cpp
Deck/KeywordContainer.cpp
)
set( parser_source

View File

@ -22,10 +22,11 @@
namespace Opm {
Deck::Deck() {
m_keywords = KeywordContainerPtr(new KeywordContainer());
}
bool Deck::hasKeyword(const std::string& keyWord) const {
bool Deck::hasKeyword(const std::string& keyword) const {
return m_keywords->hasKeyword(keyword);
}

View File

@ -21,6 +21,7 @@
#define DECK_HPP
#include <boost/shared_ptr.hpp>
#include <opm/parser/eclipse/Deck/KeywordContainer.hpp>
namespace Opm {
@ -29,9 +30,9 @@ namespace Opm {
Deck();
virtual ~Deck();
bool hasKeyword( const std::string& keyWord ) const;
bool hasKeyword( const std::string& keyword ) const;
private:
KeywordContainerPtr m_keywords;
};
typedef boost::shared_ptr<Deck> DeckPtr;

View File

@ -0,0 +1,49 @@
/*
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/>.
*/
#include "DeckKW.hpp"
namespace Opm {
DeckKW::DeckKW(const std::string& keywordName) {
m_keywordName = keywordName;
}
std::string DeckKW::name() const {
return m_keywordName;
}
size_t DeckKW::size() const {
return m_recordList.size();
}
void DeckKW::addRecord(DeckRecordConstPtr record) {
m_recordList.push_back(record);
}
DeckRecordConstPtr DeckKW::getRecord(size_t index) const {
if (index < m_recordList.size()) {
return m_recordList[index];
}
else
throw std::range_error("Index out of range");
}
}

View File

@ -0,0 +1,37 @@
/*
* File: DeckKW.hpp
* Author: kflik
*
* Created on June 3, 2013, 12:55 PM
*/
#ifndef DECKKW_HPP
#define DECKKW_HPP
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <opm/parser/eclipse/Deck/DeckRecord.hpp>
namespace Opm {
class DeckKW {
public:
DeckKW(const std::string& keywordName);
std::string name() const;
size_t size() const;
void addRecord(DeckRecordConstPtr record);
DeckRecordConstPtr getRecord(size_t index) const;
private:
std::string m_keywordName;
std::vector<DeckRecordConstPtr> m_recordList;
};
typedef boost::shared_ptr<DeckKW> DeckKWPtr;
typedef boost::shared_ptr<const DeckKW> DeckKWConstPtr;
}
#endif /* DECKKW_HPP */

View File

@ -0,0 +1,50 @@
/*
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/>.
*/
#include <map>
#include <opm/parser/eclipse/Deck/KeywordContainer.hpp>
#include <opm/parser/eclipse/Deck/DeckKW.hpp>
namespace Opm {
KeywordContainer::KeywordContainer() {
}
bool KeywordContainer::hasKeyword(const std::string& keyword) const {
return (m_keywordMap.find(keyword) != m_keywordMap.end());
}
size_t KeywordContainer::size() const {
return m_keywordList.size();
}
void KeywordContainer::addKeyword(DeckKWConstPtr keyword) {
m_keywordList.push_back(keyword);
if (m_keywordMap.find(keyword->name()) == m_keywordMap.end()) {
m_keywordMap[keyword->name()] = std::vector<DeckKWConstPtr>();
}
{
std::vector<DeckKWConstPtr>& keywordList = m_keywordMap[keyword->name()];
keywordList.push_back(keyword);
}
}
}

View File

@ -0,0 +1,36 @@
/*
* File: KeywordContainer.hpp
* Author: kflik
*
* Created on June 3, 2013, 10:49 AM
*/
#ifndef KEYWORDCONTAINER_HPP
#define KEYWORDCONTAINER_HPP
#include <vector>
#include <map>
#include <boost/shared_ptr.hpp>
#include <opm/parser/eclipse/Deck/DeckKW.hpp>
namespace Opm {
class KeywordContainer {
public:
KeywordContainer();
bool hasKeyword(const std::string& keyword) const;
size_t size() const;
void addKeyword(DeckKWConstPtr keyword);
private:
std::vector<DeckKWConstPtr> m_keywordList;
std::map<std::string, std::vector<DeckKWConstPtr> > m_keywordMap;
};
typedef boost::shared_ptr<KeywordContainer> KeywordContainerPtr;
typedef boost::shared_ptr<const KeywordContainer> KeywordContainerConstPtr;
}
#endif /* KEYWORDCONTAINER_HPP */

View File

@ -15,6 +15,15 @@ target_link_libraries(runDeckTests Parser Logger ${Boost_LIBRARIES})
add_test(NAME runDeckTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runDeckTests )
set_property(SOURCE DeckTests.cpp PROPERTY COMPILE_FLAGS "-Wno-error")
add_executable(runKeywordContainerTests KeywordContainerTests.cpp)
target_link_libraries(runKeywordContainerTests Parser Logger ${Boost_LIBRARIES})
add_test(NAME runKeywordContainerTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runKeywordContainerTests )
set_property(SOURCE KeywordContainerTests.cpp PROPERTY COMPILE_FLAGS "-Wno-error")
add_executable(runDeckKWTests DeckKWTests.cpp)
target_link_libraries(runDeckKWTests Parser Logger ${Boost_LIBRARIES})
add_test(NAME runDeckKWTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runDeckKWTests )
set_property(SOURCE DeckKWTests.cpp PROPERTY COMPILE_FLAGS "-Wno-error")

View File

@ -0,0 +1,73 @@
/*
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/>.
*/
#define BOOST_TEST_MODULE DeckKWTests
#include <stdexcept>
#include <boost/test/unit_test.hpp>
#include <opm/parser/eclipse/Deck/DeckKW.hpp>
using namespace Opm;
BOOST_AUTO_TEST_CASE(Initialize) {
DeckKW deckKW1("KW");
DeckKWPtr deckKW2(new DeckKW("KW"));
DeckKWConstPtr deckKW3(new DeckKW("KW"));
}
BOOST_AUTO_TEST_CASE(name_nameSetInConstructor_nameReturned) {
DeckKWPtr deckKW(new DeckKW("KW"));
BOOST_CHECK_EQUAL("KW", deckKW->name());
}
BOOST_AUTO_TEST_CASE(size_noRecords_returnszero) {
DeckKWPtr deckKW(new DeckKW("KW"));
BOOST_CHECK_EQUAL(0U, deckKW->size());
}
BOOST_AUTO_TEST_CASE(addRecord_onerecord_recordadded) {
DeckKWPtr deckKW(new DeckKW("KW"));
deckKW->addRecord(DeckRecordConstPtr(new DeckRecord()));
BOOST_CHECK_EQUAL(1U, deckKW->size());
}
BOOST_AUTO_TEST_CASE(getRecord_onerecord_recordretured) {
DeckKWPtr deckKW(new DeckKW("KW"));
DeckRecordConstPtr deckRecord(new DeckRecord());
deckKW->addRecord(deckRecord);
BOOST_CHECK_EQUAL(deckRecord, deckKW->getRecord(0));
}
BOOST_AUTO_TEST_CASE(getRecord_outofrange_exceptionthrown) {
DeckKWPtr deckKW(new DeckKW("KW"));
DeckRecordConstPtr deckRecord(new DeckRecord());
deckKW->addRecord(deckRecord);
BOOST_CHECK_THROW(deckKW->getRecord(1), std::range_error);
}

View File

@ -27,14 +27,14 @@
using namespace Opm;
BOOST_AUTO_TEST_CASE(Initialize) {
BOOST_REQUIRE_NO_THROW(Deck deck());
BOOST_REQUIRE_NO_THROW(Deck deck);
BOOST_REQUIRE_NO_THROW(DeckPtr deckPtr(new Deck()));
BOOST_REQUIRE_NO_THROW(DeckConstPtr deckConstPtr(new Deck()));
}
BOOST_AUTO_TEST_CASE(hasKeyword_empty_returnFalse) {
Deck deck();
Deck deck;
BOOST_CHECK_EQUAL( false , deck.hasKeyword("Bjarne"));
}

View File

@ -0,0 +1,58 @@
/*
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/>.
*/
#define BOOST_TEST_MODULE KeywordContainerTests
#include <stdexcept>
#include <boost/test/unit_test.hpp>
#include <opm/parser/eclipse/Deck/KeywordContainer.hpp>
#include <opm/parser/eclipse/Deck/Deck.hpp>
#include <opm/parser/eclipse/Deck/DeckKW.hpp>
using namespace Opm;
BOOST_AUTO_TEST_CASE(Initialize) {
KeywordContainer container;
KeywordContainerPtr ptrContainer(new KeywordContainer());
KeywordContainerConstPtr constPtrContainer(new KeywordContainer());
}
BOOST_AUTO_TEST_CASE(hasKeyword_empty_returnsfalse) {
KeywordContainerPtr container(new KeywordContainer());
BOOST_CHECK_EQUAL(false, container->hasKeyword("Truls"));
}
BOOST_AUTO_TEST_CASE(addKeyword_keywordAdded_keywordAdded) {
KeywordContainerPtr container(new KeywordContainer());
DeckKWPtr keyword = DeckKWPtr(new DeckKW("Truls"));
container->addKeyword(keyword);
BOOST_CHECK_EQUAL(true, container->hasKeyword("Truls"));
BOOST_CHECK_EQUAL(1U, container->size());
}

View File

@ -60,5 +60,5 @@ BOOST_AUTO_TEST_CASE(parse_fileWithBPRKeyword_DeckhasBRP) {
ParserPtr parser = createBPRParser();
DeckPtr deck = parser->parse(singleKeywordFile.string());
BOOST_CHECK_EQUAL( true , deck.hasKeyword("BPR"));
BOOST_CHECK_EQUAL( true , deck->hasKeyword("BPR"));
}

View File

@ -31,16 +31,15 @@ namespace Opm {
DeckPtr Parser::parse(const std::string &path) {
Logger::initLogger();
Logger::info("Starting parsing of file: " + path);
DeckPtr deck(new Deck());
{
RawDeckPtr rawDeck(new RawDeck(RawParserKWsConstPtr(new RawParserKWs())));
rawDeck->parse(path);
RawDeckPtr rawDeck(new RawDeck(RawParserKWsConstPtr(new RawParserKWs())));
rawDeck->parse(path);
for (size_t i = rawDeck->getNumberOfKeywords(); i++) {
RawKeywordPtr rawKeyword = rawDeck->getKeyword(0);
}
Logger::info("Done parsing of file: " + path);
Logger::closeLogger();
return deck;
@ -53,7 +52,7 @@ namespace Opm {
}
void Parser::addKW(ParserKWConstPtr parserKW) {
keywords.insert(std::make_pair(parserKW->getName(), parserKW));
m_parserKeywords.insert(std::make_pair(parserKW->getName(), parserKW));
}
} // namespace Opm

View File

@ -48,7 +48,7 @@ namespace Opm {
void addKW(ParserKWConstPtr parserKW);
private:
std::map<std::string, ParserKWConstPtr> keywords;
std::map<std::string, ParserKWConstPtr> m_parserKeywords;
};
typedef boost::shared_ptr<Parser> ParserPtr;

View File

@ -54,9 +54,17 @@ namespace Opm {
return m_name;
}
ParserKW::~ParserKW() {
DeckKWPtr ParserKW::parse(RawKeywordPtr rawKeyword) {
DeckKWPtr keyword(new DeckKW(getName()));
if (m_record != NULL) {
for (size_t i=0; i<rawKeyword->size(); i++) {
DeckRecordConstPtr deckRecord = m_record->parse(rawKeyword->getRecord(i));
keyword->addRecord(deckRecord);
}
}
else
throw std::logic_error("Unable to parse rawKeyword, because the ParserKW's record is not set!");
return keyword;
}
}

View File

@ -24,6 +24,9 @@
#include <opm/parser/eclipse/Parser/ParserRecordSize.hpp>
#include <opm/parser/eclipse/Parser/ParserRecord.hpp>
#include <opm/parser/eclipse/Deck/DeckKW.hpp>
#include <opm/parser/eclipse/RawDeck/RawKeyword.hpp>
namespace Opm {
@ -33,9 +36,9 @@ namespace Opm {
ParserKW(const std::string& name, ParserRecordSizeConstPtr recordSize);
void setRecord(ParserRecordConstPtr record);
ParserRecordConstPtr getRecord();
~ParserKW();
const std::string& getName() const;
DeckKWPtr parse(RawKeywordPtr rawKeyword);
private:
std::string m_name;
ParserRecordConstPtr m_record;

View File

@ -26,7 +26,7 @@ namespace Opm {
}
size_t ParserRecord::size() {
size_t ParserRecord::size() const {
return m_items.size();
}
@ -39,7 +39,7 @@ namespace Opm {
}
ParserItemConstPtr ParserRecord::get(size_t index) {
ParserItemConstPtr ParserRecord::get(size_t index) const{
if (index < m_items.size())
return m_items[ index ];
else
@ -47,14 +47,17 @@ namespace Opm {
}
ParserItemConstPtr ParserRecord::get(const std::string& itemName) {
ParserItemConstPtr ParserRecord::get(const std::string& itemName) const {
if (m_itemMap.find( itemName ) == m_itemMap.end())
throw std::invalid_argument("Itemname: " + itemName + " does not exist.");
else
return m_itemMap[ itemName ];
{
std::map<std::string, ParserItemConstPtr>::const_iterator theItem = m_itemMap.find(itemName);
return (*theItem).second;
}
}
DeckRecordConstPtr ParserRecord::parse(RawRecordPtr rawRecord) {
DeckRecordConstPtr ParserRecord::parse(RawRecordPtr rawRecord) const {
DeckRecordPtr deckRecord(new DeckRecord());
for(size_t i=0; i<size(); i++) {

View File

@ -33,11 +33,11 @@ namespace Opm {
class ParserRecord {
public:
ParserRecord();
size_t size();
size_t size() const;
void addItem( ParserItemConstPtr item );
ParserItemConstPtr get(size_t index);
ParserItemConstPtr get(const std::string& itemName);
DeckRecordConstPtr parse(RawRecordPtr rawRecord);
ParserItemConstPtr get(size_t index) const;
ParserItemConstPtr get(const std::string& itemName) const;
DeckRecordConstPtr parse(RawRecordPtr rawRecord) const;
private:
std::vector<ParserItemConstPtr> m_items;

View File

@ -22,6 +22,9 @@
#include <boost/test/unit_test.hpp>
#include "opm/parser/eclipse/Parser/ParserKW.hpp"
#include "opm/parser/eclipse/Parser/ParserIntItem.hpp"
#include "opm/parser/eclipse/Parser/ParserItem.hpp"
using namespace Opm;
@ -58,3 +61,22 @@ BOOST_AUTO_TEST_CASE(MixedCase) {
ParserRecordSizeConstPtr recordSize(new ParserRecordSize(100));
BOOST_CHECK_THROW(ParserKW parserKW(keyword, recordSize), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(parse_rawKeyword_returnsDeckKW) {
RawKeywordPtr rawKeyword(new RawKeyword("TEST2"));
rawKeyword->addRawRecordString("2 3 5 /");
ParserKWPtr parserKW(new ParserKW("TEST2"));
ParserRecordPtr parserRecord = ParserRecordPtr(new ParserRecord());
ParserItemPtr item1(new ParserIntItem("I", SINGLE));
parserRecord->addItem(item1);
ParserItemPtr item2(new ParserIntItem("J", SINGLE));
parserRecord->addItem(item2);
ParserItemPtr item3(new ParserIntItem("K", SINGLE));
parserRecord->addItem(item3);
parserKW->setRecord(parserRecord);
DeckKWPtr deckKW = parserKW->parse(rawKeyword);
BOOST_CHECK_EQUAL(1U, deckKW->size());
}

View File

@ -63,7 +63,7 @@ BOOST_AUTO_TEST_CASE(Get_OneItem_Return1) {
}
BOOST_AUTO_TEST_CASE(Get_outOfRange_Throw) {
ParserRecordPtr record(new ParserRecord());
ParserRecordConstPtr record(new ParserRecord());
BOOST_CHECK_THROW(record->get(0), std::out_of_range);
}

View File

@ -65,13 +65,13 @@ BOOST_AUTO_TEST_CASE(ParseFullTestFile) {
RawKeywordConstPtr matchingKeyword = rawDeck->getKeyword("OIL");
std::list<RawRecordConstPtr> records = matchingKeyword->getRecords();
BOOST_CHECK_EQUAL("OIL", matchingKeyword->getKeyword());
BOOST_CHECK_EQUAL("OIL", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL((unsigned) 0, records.size());
matchingKeyword = rawDeck->getKeyword("VFPPDIMS");
records = matchingKeyword->getRecords();
BOOST_CHECK_EQUAL("VFPPDIMS", matchingKeyword->getKeyword());
BOOST_CHECK_EQUAL("VFPPDIMS", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL((unsigned) 1, records.size());
const std::string& recordString = records.front()->getRecordString();

View File

@ -28,13 +28,17 @@ namespace Opm {
m_rawParserKWs = rawParserKWs;
}
RawKeywordConstPtr RawDeck::getKeyword(size_t index) const {
}
/// Iterate through list of RawKeywords in search for the specified string.
/// O(n), not using map or hash because the keywords are not unique,
/// and the order matters. Returns first matching keyword.
RawKeywordConstPtr RawDeck::getKeyword(const std::string& keyword) const {
for (std::list<RawKeywordConstPtr>::const_iterator it = m_keywords.begin(); it != m_keywords.end(); it++) {
if ((*it)->getKeyword() == keyword) {
if ((*it)->getKeywordName() == keyword) {
return (*it);
}
}
@ -43,7 +47,7 @@ namespace Opm {
bool RawDeck::hasKeyword(const std::string& keyword) const {
for (std::list<RawKeywordConstPtr>::const_iterator it = m_keywords.begin(); it != m_keywords.end(); it++) {
if ((*it)->getKeyword() == keyword) {
if ((*it)->getKeywordName() == keyword) {
return true;
}
}
@ -95,8 +99,9 @@ namespace Opm {
}
void RawDeck::addKeyword(RawKeywordConstPtr keyword, const boost::filesystem::path& dataFolderPath) {
if (keyword->getKeyword() == Opm::RawConsts::include) {
std::string includeFileString = keyword->getRecords().front()->getItems().front();
if (keyword->getKeywordName() == Opm::RawConsts::include) {
RawRecordConstPtr firstRecord = keyword->getRecord(0);
std::string includeFileString = firstRecord->getItem(0);
boost::filesystem::path pathToIncludedFile(dataFolderPath);
pathToIncludedFile /= includeFileString;
@ -124,8 +129,8 @@ namespace Opm {
/// specified for the current keyword type in the RawParserKW class.
bool RawDeck::isKeywordFinished(RawKeywordConstPtr rawKeyword) {
if (m_rawParserKWs->keywordExists(rawKeyword->getKeyword())) {
return rawKeyword->getNumberOfRecords() == m_rawParserKWs->getFixedNumberOfRecords(rawKeyword->getKeyword());
if (m_rawParserKWs->keywordExists(rawKeyword->getKeywordName())) {
return rawKeyword->size() == m_rawParserKWs->getFixedNumberOfRecords(rawKeyword->getKeywordName());
}
return false;
}
@ -134,14 +139,12 @@ namespace Opm {
std::ostream& operator<<(std::ostream& os, const RawDeck& deck) {
for (std::list<RawKeywordConstPtr>::const_iterator keyword = deck.m_keywords.begin(); keyword != deck.m_keywords.end(); keyword++) {
os << (*keyword)->getKeyword() << " -- Keyword\n";
os << (*keyword)->getKeywordName() << " -- Keyword\n";
std::list<RawRecordConstPtr> records = (*keyword)->getRecords();
for (std::list<RawRecordConstPtr>::const_iterator record = records.begin(); record != records.end(); record++) {
std::deque<std::string> recordItems = (*record)->getItems();
for (size_t i=0; i<recordItems.size(); i++) {
os << recordItems[i] << " ";
for (size_t i = 0; i < (*keyword)->size(); i++) {
RawRecordConstPtr rawRecord = (*keyword)->getRecord(i);
for (size_t j = 0; j < rawRecord->size(); j++) {
os << rawRecord->getItem(j) << " ";
}
os << " / -- Data\n";
}

View File

@ -46,6 +46,8 @@ namespace Opm {
void parse(const std::string& path);
RawKeywordConstPtr getKeyword(const std::string& keyword) const;
RawKeywordConstPtr getKeyword(size_t index) const;
bool hasKeyword(const std::string& keyword) const;
unsigned int getNumberOfKeywords() const;
friend std::ostream& operator<<(std::ostream& os, const RawDeck& deck);
@ -56,7 +58,6 @@ namespace Opm {
std::list<RawKeywordConstPtr> m_keywords;
RawParserKWsConstPtr m_rawParserKWs;
//void readDataIntoDeck(const std::string& path, std::list<RawKeywordConstPtr>& keywordList);
void addKeyword(RawKeywordConstPtr keyword, const boost::filesystem::path& baseDataFolder);
bool isKeywordFinished(RawKeywordConstPtr rawKeyword);

View File

@ -25,11 +25,36 @@
namespace Opm {
RawKeyword::RawKeyword() {
RawKeyword::RawKeyword(const std::string& name) {
setKeywordName(name);
}
RawKeyword::RawKeyword(const std::string& keyword) {
setKeyword(keyword);
const std::string& RawKeyword::getKeywordName() const {
return m_name;
}
size_t RawKeyword::size() const {
return m_records.size();
}
/// Important method, being repeatedly called. When a record is terminated,
/// it is added to the list of records, and a new record is started.
void RawKeyword::addRawRecordString(const std::string& partialRecordString) {
m_partialRecordString += partialRecordString;
if (RawRecord::isTerminatedRecordString(partialRecordString)) {
RawRecordPtr record(new RawRecord(m_partialRecordString));
m_records.push_back(record);
m_partialRecordString.clear();
}
}
RawRecordPtr RawKeyword::getRecord(size_t index) const {
if (index < m_records.size()) {
return m_records[index];
}
else
throw std::range_error("Index out of range");
}
bool RawKeyword::tryParseKeyword(const std::string& keywordCandidate, std::string& result) {
@ -84,25 +109,14 @@ namespace Opm {
return false;
}
void RawKeyword::setKeyword(const std::string& keyword) {
m_keyword = boost::algorithm::trim_right_copy(keyword);
if (!isValidKeyword(m_keyword)) {
throw std::invalid_argument("Not a valid keyword:" + keyword);
} else if (m_keyword.size() > Opm::RawConsts::maxKeywordLength) {
throw std::invalid_argument("Too long keyword:" + keyword);
} else if (boost::algorithm::trim_left_copy(m_keyword) != m_keyword) {
throw std::invalid_argument("Illegal whitespace start of keyword:" + keyword);
}
}
/// Important method, being repeatedly called. When a record is terminated,
/// it is added to the list of records, and a new record is started.
void RawKeyword::addRawRecordString(const std::string& partialRecordString) {
m_partialRecordString += partialRecordString;
if (RawRecord::isTerminatedRecordString(partialRecordString)) {
RawRecordPtr record(new RawRecord(m_partialRecordString));
m_records.push_back(record);
m_partialRecordString.clear();
void RawKeyword::setKeywordName(const std::string& name) {
m_name = boost::algorithm::trim_right_copy(name);
if (!isValidKeyword(m_name)) {
throw std::invalid_argument("Not a valid keyword:" + name);
} else if (m_name.size() > Opm::RawConsts::maxKeywordLength) {
throw std::invalid_argument("Too long keyword:" + name);
} else if (boost::algorithm::trim_left_copy(m_name) != m_name) {
throw std::invalid_argument("Illegal whitespace start of keyword:" + name);
}
}
@ -110,19 +124,5 @@ namespace Opm {
return m_partialRecordString.size() == 0;
}
unsigned int RawKeyword::getNumberOfRecords() const {
return m_records.size();
}
const std::list<RawRecordConstPtr>& RawKeyword::getRecords() const {
return m_records;
}
const std::string& RawKeyword::getKeyword() const {
return m_keyword;
}
RawKeyword::~RawKeyword() {
}
}

View File

@ -22,7 +22,7 @@
#include <string>
#include <utility>
#include <list>
#include <vector>
#include <boost/shared_ptr.hpp>
#include "opm/parser/eclipse/Logger/Logger.hpp"
@ -33,28 +33,29 @@ namespace Opm {
/// Class representing a RawKeyword, meaning both the actual keyword phrase, and the records,
/// represented as a list of RawRecord objects.
/// The class also contains static functions to aid the parsing of the input file.
/// The creationg of an instance is performed by calling the addRawRecordString method repeatedly.
/// The creating of an instance is performed by calling the addRawRecordString method repeatedly.
class RawKeyword {
public:
RawKeyword();
RawKeyword(const std::string& keyword);
RawKeyword(const std::string& name);
const std::string& getKeywordName() const;
void addRawRecordString(const std::string& partialRecordString);
size_t size() const;
RawRecordPtr getRecord(size_t index) const;
static bool tryParseKeyword(const std::string& line, std::string& result);
static bool lineContainsData(const std::string& line);
static bool lineTerminatesKeyword(const std::string& line);
const std::string& getKeyword() const;
const std::list<RawRecordConstPtr>& getRecords() const;
unsigned int getNumberOfRecords() const;
void setKeyword(const std::string& keyword);
void addRawRecordString(const std::string& partialRecordString);
bool isPartialRecordStringEmpty() const;
virtual ~RawKeyword();
private:
std::string m_keyword;
std::list<RawRecordConstPtr> m_records;
std::string m_name;
std::vector<RawRecordPtr> m_records;
std::string m_partialRecordString;
void setKeywordName(const std::string& keyword);
static bool isValidKeyword(const std::string& keywordCandidate);
};
typedef boost::shared_ptr<RawKeyword> RawKeywordPtr;

View File

@ -67,18 +67,13 @@ namespace Opm {
}
const std::string& RawRecord::operator[](size_t index) const {
const std::string& RawRecord::getItem(size_t index) const {
if (index < m_recordItems.size())
return m_recordItems[index];
else
throw std::out_of_range("Lookup index out of range");
}
const std::deque<std::string>& RawRecord::getItems() const {
return m_recordItems;
}
const std::string& RawRecord::getRecordString() const {
return m_sanitizedRecordString;
}

View File

@ -40,8 +40,7 @@ namespace Opm {
size_t size() const;
const std::string& getRecordString() const;
const std::deque<std::string>& getItems() const;
const std::string& operator[](size_t index) const;
const std::string& getItem(size_t index) const;
static bool isTerminatedRecordString(const std::string& candidateRecordString);
virtual ~RawRecord();

View File

@ -59,18 +59,14 @@ BOOST_AUTO_TEST_CASE(ParseFullTestFile) {
// number of records
RawKeywordConstPtr matchingKeyword = rawDeck->getKeyword("OIL");
std::list<RawRecordConstPtr> records = matchingKeyword->getRecords();
BOOST_CHECK_EQUAL("OIL", matchingKeyword->getKeyword());
BOOST_CHECK_EQUAL((unsigned) 0, records.size());
BOOST_CHECK_EQUAL("OIL", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(0U, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword("VFPPDIMS");
records = matchingKeyword->getRecords();
BOOST_CHECK_EQUAL("VFPPDIMS", matchingKeyword->getKeyword());
BOOST_CHECK_EQUAL((unsigned) 1, records.size());
BOOST_CHECK_EQUAL("VFPPDIMS", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL((unsigned) 1, matchingKeyword->size());
const std::string& recordString = records.front()->getRecordString();
const std::string& recordString = matchingKeyword->getRecord(0)->getRecordString();
BOOST_CHECK_EQUAL("20 20 15 15 15 50", recordString);
std::deque<std::string> recordItems = records.front()->getItems();
BOOST_CHECK_EQUAL((unsigned) 6, recordItems.size());
BOOST_CHECK_EQUAL((unsigned) 6, matchingKeyword->getRecord(0)->size());
}

View File

@ -31,9 +31,16 @@ using namespace Opm;
BOOST_AUTO_TEST_CASE(Initialize_NoThrow) {
RawParserKWsConstPtr fixedKeywords(new RawParserKWs());
BOOST_CHECK_NO_THROW( RawDeck rawDeck( fixedKeywords ));
BOOST_CHECK_NO_THROW(RawDeck rawDeck(fixedKeywords));
}
BOOST_AUTO_TEST_CASE(getKeyword_byIndex_keywordReturned) {
RawParserKWsConstPtr fixedKeywords(new RawParserKWs());
RawDeck rawDeck(fixedKeywords);
rawDeck.parse("testdata/small.data");
RawKeywordConstPtr rawKeyword = rawDeck.getKeyword(0U);
}
}
BOOST_AUTO_TEST_CASE(GetNumberOfKeywords_EmptyDeck_RetunsZero) {
RawParserKWsConstPtr fixedKeywords(new RawParserKWs());
@ -53,8 +60,6 @@ BOOST_AUTO_TEST_CASE(GetKeyword_EmptyDeck_ThrowsExeption) {
BOOST_CHECK_THROW(rawDeck->getKeyword("TEST"), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(PrintToOStream_noThrow) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
@ -64,22 +69,17 @@ BOOST_AUTO_TEST_CASE(PrintToOStream_noThrow) {
std::cout << *rawDeck << "\n";
}
BOOST_AUTO_TEST_CASE(Parse_InvalidInputFile_Throws) {
RawDeckPtr rawDeck(new RawDeck(RawParserKWsConstPtr(new RawParserKWs())));
BOOST_CHECK_THROW(rawDeck->parse("nonexistingfile.asdf"), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(Parse_ValidInputFile_NoThrow) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
RawDeckPtr rawDeck(new RawDeck(RawParserKWsConstPtr(new RawParserKWs())));
BOOST_CHECK_NO_THROW(rawDeck->parse(singleKeywordFile.string()));
}
BOOST_AUTO_TEST_CASE(ParseFileWithOneKeyword) {
boost::filesystem::path singleKeywordFile("testdata/mini.data");
@ -88,20 +88,19 @@ BOOST_AUTO_TEST_CASE(ParseFileWithOneKeyword) {
BOOST_CHECK_EQUAL((unsigned) 1, rawDeck->getNumberOfKeywords());
RawKeywordConstPtr rawKeyword = rawDeck->getKeyword("ENDSCALE");
const std::list<RawRecordConstPtr>& records = rawKeyword->getRecords();
BOOST_CHECK_EQUAL((unsigned) 1, records.size());
RawRecordConstPtr record = records.back();
BOOST_CHECK_EQUAL(1U, rawKeyword->size());
RawRecordConstPtr record = rawKeyword->getRecord(rawKeyword->size() - 1);
const std::string& recordString = record->getRecordString();
BOOST_CHECK_EQUAL("'NODIR' 'REVERS' 1 20", recordString);
const std::deque<std::string>& recordElements = record->getItems();
BOOST_CHECK_EQUAL((unsigned) 4, recordElements.size());
BOOST_CHECK_EQUAL((unsigned) 4, record->size());
BOOST_CHECK_EQUAL("NODIR", recordElements[0]);
BOOST_CHECK_EQUAL("REVERS", recordElements[1]);
BOOST_CHECK_EQUAL("1", recordElements[2]);
BOOST_CHECK_EQUAL("20", recordElements[3]);
BOOST_CHECK_EQUAL("NODIR", record->getItem(0));
BOOST_CHECK_EQUAL("REVERS", record->getItem(1));
BOOST_CHECK_EQUAL("1", record->getItem(2));
BOOST_CHECK_EQUAL("20", record->getItem(3));
}
BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) {
@ -113,39 +112,32 @@ BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) {
BOOST_CHECK_EQUAL((unsigned) 7, rawDeck->getNumberOfKeywords());
RawKeywordConstPtr matchingKeyword = rawDeck->getKeyword("OIL");
std::list<RawRecordConstPtr> records = matchingKeyword->getRecords();
BOOST_CHECK_EQUAL("OIL", matchingKeyword->getKeyword());
BOOST_CHECK_EQUAL((unsigned) 0, records.size());
BOOST_CHECK_EQUAL("OIL", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(0U, matchingKeyword->size());
// The two next come in via the include of the include path/readthis.sch file
matchingKeyword = rawDeck->getKeyword("GRUPTREE");
BOOST_CHECK_EQUAL("GRUPTREE", matchingKeyword->getKeyword());
records = matchingKeyword->getRecords();
BOOST_CHECK_EQUAL((unsigned) 2, records.size());
BOOST_CHECK_EQUAL("GRUPTREE", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL((unsigned) 2, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword("WHISTCTL");
BOOST_CHECK_EQUAL("WHISTCTL", matchingKeyword->getKeyword());
records = matchingKeyword->getRecords();
BOOST_CHECK_EQUAL((unsigned) 1, records.size());
BOOST_CHECK_EQUAL("WHISTCTL", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL((unsigned) 1, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword("METRIC");
BOOST_CHECK_EQUAL("METRIC", matchingKeyword->getKeyword());
records = matchingKeyword->getRecords();
BOOST_CHECK_EQUAL((unsigned) 0, records.size());
BOOST_CHECK_EQUAL("METRIC", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL((unsigned) 0, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword("GRIDUNIT");
BOOST_CHECK_EQUAL("GRIDUNIT", matchingKeyword->getKeyword());
records = matchingKeyword->getRecords();
BOOST_CHECK_EQUAL((unsigned) 1, records.size());
BOOST_CHECK_EQUAL("GRIDUNIT", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL((unsigned) 1, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword("RADFIN4");
records = matchingKeyword->getRecords();
BOOST_CHECK_EQUAL("RADFIN4", matchingKeyword->getKeyword());
BOOST_CHECK_EQUAL((unsigned) 1, records.size());
BOOST_CHECK_EQUAL("RADFIN4", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL((unsigned) 1, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword("ABCDAD");
BOOST_CHECK_EQUAL("ABCDAD", matchingKeyword->getKeyword());
records = matchingKeyword->getRecords();
BOOST_CHECK_EQUAL("ABCDAD", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL((unsigned) 2, records.size());
BOOST_CHECK_EQUAL((unsigned) 2, matchingKeyword->size());
}

View File

@ -22,43 +22,49 @@
#include <boost/test/unit_test.hpp>
#include <opm/parser/eclipse/RawDeck/RawKeyword.hpp>
BOOST_AUTO_TEST_CASE(RawKeywordEmptyConstructorEmptyKeyword) {
Opm::RawKeyword keyword;
BOOST_CHECK(keyword.getKeyword() == "");
}
using namespace Opm;
BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorKeywordSet) {
Opm::RawKeyword keyword("KEYYWORD");
BOOST_CHECK(keyword.getKeyword() == "KEYYWORD");
RawKeyword keyword("KEYYWORD");
BOOST_CHECK(keyword.getKeywordName() == "KEYYWORD");
}
BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorTooLongThrows) {
BOOST_CHECK_THROW(Opm::RawKeyword keyword("KEYYYWORD"), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(RawKeywordSetTooLongKeywordThrows) {
Opm::RawKeyword keyword;
BOOST_CHECK_THROW(keyword.setKeyword("TESTTOOLONG"), std::invalid_argument);
BOOST_CHECK_THROW(RawKeyword keyword("KEYYYWORD"), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(RawKeywordSetKeywordInitialWhitespaceInKeywordThrows) {
Opm::RawKeyword keyword;
BOOST_CHECK_THROW(keyword.setKeyword(" TELONG"), std::invalid_argument);
BOOST_CHECK_THROW(RawKeyword(" TELONG"), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(constructor_mixedCaseName_throws) {
BOOST_CHECK_THROW(RawKeyword("Test"), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(RawKeywordSetKeywordInitialTabInKeywordThrows) {
Opm::RawKeyword keyword;
BOOST_CHECK_THROW(keyword.setKeyword("\tTELONG"), std::invalid_argument);
BOOST_CHECK_THROW( RawKeyword("\tTELONG"), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(RawKeywordSetCorrectLenghtKeywordNoError) {
Opm::RawKeyword keyword;
keyword.setKeyword("GOODONE");
BOOST_CHECK(keyword.getKeyword() == "GOODONE");
RawKeyword keyword("GOODONE");
BOOST_CHECK(keyword.getKeywordName() == "GOODONE");
}
BOOST_AUTO_TEST_CASE(RawKeywordSet8CharKeywordWithTrailingWhitespaceKeywordTrimmed) {
Opm::RawKeyword keyword;
keyword.setKeyword("GOODONEE ");
BOOST_CHECK(keyword.getKeyword() == "GOODONEE");
RawKeyword keyword("GOODONEE ");
BOOST_CHECK(keyword.getKeywordName() == "GOODONEE");
}
BOOST_AUTO_TEST_CASE(addRecord_singleRecord_recordAdded) {
RawKeyword keyword("TEST");
keyword.addRawRecordString("test 1 3 4 /");
BOOST_CHECK_EQUAL(1U, keyword.size());
}
BOOST_AUTO_TEST_CASE(getRecord_outOfRange_throws) {
RawKeyword keyword("TEST");
keyword.addRawRecordString("test 1 3 4 /");
BOOST_CHECK_THROW(keyword.getRecord(1), std::range_error);
}

View File

@ -31,13 +31,12 @@ BOOST_AUTO_TEST_CASE(RawRecordGetRecordStringReturnsTrimmedString) {
BOOST_AUTO_TEST_CASE(RawRecordGetRecordsCorrectElementsReturned) {
Opm::RawRecordPtr record(new Opm::RawRecord(" 'NODIR ' 'REVERS' 1 20 /"));
const std::deque<std::string>& recordElements = record->getItems();
BOOST_CHECK_EQUAL((unsigned) 4, recordElements.size());
BOOST_CHECK_EQUAL((unsigned) 4, record->size());
BOOST_CHECK_EQUAL("NODIR ", recordElements[0]);
BOOST_CHECK_EQUAL("REVERS", recordElements[1]);
BOOST_CHECK_EQUAL("1", recordElements[2]);
BOOST_CHECK_EQUAL("20", recordElements[3]);
BOOST_CHECK_EQUAL("NODIR ", record->getItem(0));
BOOST_CHECK_EQUAL("REVERS", record->getItem(1));
BOOST_CHECK_EQUAL("1", record->getItem(2));
BOOST_CHECK_EQUAL("20", record->getItem(3));
}
BOOST_AUTO_TEST_CASE(RawRecordIsCompleteRecordCompleteRecordReturnsTrue) {
@ -57,14 +56,14 @@ BOOST_AUTO_TEST_CASE(Rawrecord_OperatorThis_OK) {
Opm::RawRecord record(" 'NODIR ' 'REVERS' 1 20 /");
Opm::RawRecordPtr recordPtr(new Opm::RawRecord(" 'NODIR ' 'REVERS' 1 20 /"));
BOOST_CHECK_EQUAL( "NODIR " , record[0]);
BOOST_CHECK_EQUAL( "REVERS" , record[1]);
BOOST_CHECK_EQUAL( "1" , record[2]);
BOOST_CHECK_EQUAL( "20" , record[3]);
BOOST_CHECK_EQUAL( "NODIR " , record.getItem(0));
BOOST_CHECK_EQUAL( "REVERS" , record.getItem(1));
BOOST_CHECK_EQUAL( "1" , record.getItem(2));
BOOST_CHECK_EQUAL( "20" , record.getItem(3));
BOOST_CHECK_EQUAL( "20" , (*recordPtr)[3]);
BOOST_CHECK_EQUAL( "20" , recordPtr->getItem(3));
BOOST_CHECK_THROW( record[4] , std::out_of_range);
BOOST_CHECK_THROW( record.getItem(4) , std::out_of_range);
}
@ -74,8 +73,8 @@ BOOST_AUTO_TEST_CASE(Rawrecord_PushFront_OK) {
record->push_front( "String1" );
BOOST_CHECK_EQUAL( "String1" , (*record)[0]);
BOOST_CHECK_EQUAL( "String2" , (*record)[1]);
BOOST_CHECK_EQUAL( "String1" , record->getItem(0));
BOOST_CHECK_EQUAL( "String2" , record->getItem(1));
}