Merge pull request #1380 from joakim-hove/split-codegen

Split codegen
This commit is contained in:
Joakim Hove
2020-01-06 10:27:14 +01:00
committed by GitHub
11 changed files with 243 additions and 296 deletions

View File

@@ -115,7 +115,10 @@ macro (sources_hook)
include(GenerateKeywords.cmake)
# Append generated sources
list(INSERT opm-common_SOURCES 0 ${PROJECT_BINARY_DIR}/ParserKeywords.cpp)
list(INSERT opm-common_SOURCES 0 ${PROJECT_BINARY_DIR}/ParserInit.cpp)
foreach (name A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
list(INSERT opm-common_SOURCES 0 ${PROJECT_BINARY_DIR}/ParserKeywords/${name}.cpp)
endforeach()
endif()
set_source_files_properties(src/opm/parser/eclipse/Python/Python.cpp
PROPERTIES COMPILE_FLAGS -Wno-shadow)

View File

@@ -1,14 +1,10 @@
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${BASE_DIR}/tmp_gen/ParserKeywords.cpp
${BASE_DIR}/ParserKeywords.cpp)
${BASE_DIR}/tmp_gen/ParserInit.cpp
${BASE_DIR}/ParserInit.cpp)
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${BASE_DIR}/tmp_gen/inlinekw.cpp
${BASE_DIR}/inlinekw.cpp)
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${BASE_DIR}/tmp_gen/include/opm/parser/eclipse/Parser/ParserKeywords.hpp
${BASE_DIR}/include/opm/parser/eclipse/Parser/ParserKeywords.hpp)
${BASE_DIR}/tmp_gen/TestKeywords.cpp
${BASE_DIR}/TestKeywords.cpp)
file(GLOB HDRS ${BASE_DIR}/tmp_gen/include/opm/parser/eclipse/Parser/ParserKeywords/*.hpp)
@@ -18,4 +14,11 @@ foreach(HDR ${HDRS})
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${HDR}
${BASE_DIR}/include/opm/parser/eclipse/Parser/ParserKeywords/${hdr})
endforeach()
foreach (name A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${BASE_DIR}/tmp_gen/ParserKeywords/${name}.cpp
${BASE_DIR}/ParserKeywords/${name}.cpp)
endforeach()

View File

@@ -4,22 +4,15 @@ set(EXTRA_TESTS)
# Generated source, needs to be here
opm_add_test(InlineKeywordTest
EXE_NAME inlinekw
SOURCES ${PROJECT_BINARY_DIR}/inlinekw.cpp
EXE_NAME TestKeywords
SOURCES ${PROJECT_BINARY_DIR}/TestKeywords.cpp
LIBRARIES ${TEST_LIBS})
list(APPEND EXTRA_TESTS inlinekw)
list(APPEND EXTRA_TESTS TestKeywords)
# Extra compile definitions and extra parameters
include(cmake/Modules/CheckCaseSensitiveFileSystem.cmake)
set(_testdir ${PROJECT_SOURCE_DIR}/tests/parser/data)
opm_add_test(LoaderTest
SOURCES tests/parser/KeywordLoaderTests.cpp
src/opm/parser/eclipse/Generator/KeywordLoader.cpp
LIBRARIES ${TEST_LIBS}
TEST_ARGS ${_testdir}/parser/keyword-generator/)
list(APPEND EXTRA_TESTS LoaderTest)
opm_add_test(ParserTests
SOURCES tests/parser/ParserTests.cpp
LIBRARIES ${TEST_LIBS}

View File

@@ -40,18 +40,74 @@ string(REGEX REPLACE "([^;]+)" "${PROJECT_SOURCE_DIR}/src/opm/parser/eclipse/sha
configure_file(src/opm/parser/eclipse/keyword_list.argv.in keyword_list.argv)
# Generate keyword source
add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords.cpp ${PROJECT_BINARY_DIR}/tmp_gen/inlinekw.cpp
COMMAND genkw keyword_list.argv
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords.cpp
add_custom_command( OUTPUT
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/A.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/B.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/C.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/D.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/E.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/F.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/G.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/H.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/I.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/J.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/K.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/L.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/M.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/N.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/O.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/P.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/Q.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/R.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/S.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/T.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/U.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/V.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/W.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/X.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/Y.cpp
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/Z.cpp
${PROJECT_BINARY_DIR}/tmp_gen/inlinekw.cpp
COMMAND genkw keyword_list.argv
${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords
${PROJECT_BINARY_DIR}/tmp_gen/ParserInit.cpp
${PROJECT_BINARY_DIR}/tmp_gen/include/
opm/parser/eclipse/Parser/ParserKeywords
${PROJECT_BINARY_DIR}/tmp_gen/inlinekw.cpp
${PROJECT_BINARY_DIR}/tmp_gen/TestKeywords.cpp
DEPENDS genkw ${keyword_files} src/opm/parser/eclipse/share/keywords/keyword_list.cmake
)
# To avoid some rebuilds
add_custom_command(OUTPUT ParserKeywords.cpp inlinekw.cpp
DEPENDS ${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords.cpp
add_custom_command(OUTPUT
${PROJECT_BINARY_DIR}/ParserKeywords/A.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/B.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/C.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/D.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/E.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/F.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/G.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/H.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/I.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/J.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/K.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/L.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/M.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/N.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/O.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/P.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/Q.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/R.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/S.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/T.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/U.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/V.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/W.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/X.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/Y.cpp
${PROJECT_BINARY_DIR}/ParserKeywords/Z.cpp
${PROJECT_BINARY_DIR}/TestKeywords.cpp
${PROJECT_BINARY_DIR}/ParserInit.cpp
DEPENDS ${PROJECT_BINARY_DIR}/tmp_gen/ParserKeywords/A.cpp
COMMAND ${CMAKE_COMMAND} -DBASE_DIR=${PROJECT_BINARY_DIR}
-P ${PROJECT_SOURCE_DIR}/CopyHeaders.cmake)

View File

@@ -36,12 +36,12 @@ namespace Opm {
static std::string endTest();
static std::string startTest(const std::string& test_name);
static std::string headerHeader( const std::string& );
static bool updateFile(const std::stringstream& newContent, const std::string& filename);
bool updateSource(const KeywordLoader& loader, const std::string& sourceFile ) const;
bool updateHeader(const KeywordLoader& loader, const std::string& headerBuildPath, const std::string& headerFile) const;
bool updateTest(const KeywordLoader& loader , const std::string& testFile) const;
static void updateFile(const std::stringstream& newContent, const std::string& filename);
void updateInitSource(const KeywordLoader& loader, const std::string& sourceFile ) const;
void updateKeywordSource(const KeywordLoader& loader, const std::string& sourceFile ) const;
void updateHeader(const KeywordLoader& loader, const std::string& headerBuildPath, const std::string& headerPath) const;
void updateTest(const KeywordLoader& loader , const std::string& testFile) const;
private:
bool m_verbose;
};

View File

@@ -22,34 +22,25 @@
#define KEYWORD_LOADER_HPP
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <boost/filesystem/path.hpp>
#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>
namespace Opm {
class ParserKeyword;
class KeywordLoader {
public:
KeywordLoader(bool verbose);
size_t size() const;
bool hasKeyword(const std::string& keyword) const;
std::shared_ptr<const ParserKeyword> getKeyword(const std::string& keyword) const;
KeywordLoader(const std::vector<std::string>& keyword_files, bool verbose);
std::string getJsonFile(const std::string& keyword) const;
void loadKeyword(const std::string& filename);
void loadKeyword(boost::filesystem::path& path);
std::map<std::string , std::shared_ptr<ParserKeyword> >::const_iterator keyword_begin( ) const;
std::map<std::string , std::shared_ptr<ParserKeyword> >::const_iterator keyword_end( ) const;
std::map<char , std::vector<ParserKeyword> >::const_iterator begin( ) const;
std::map<char , std::vector<ParserKeyword> >::const_iterator end( ) const;
private:
void addKeyword(std::shared_ptr<ParserKeyword> keyword , const std::string& jsonFile);
bool m_verbose;
std::map<std::string , std::shared_ptr<ParserKeyword> > m_keywords;
std::map<char, std::vector<ParserKeyword>> keywords;
std::map<std::string , std::string > m_jsonFile;
};
}

View File

@@ -34,29 +34,12 @@
namespace {
const std::string testHeader =
"#define BOOST_TEST_MODULE ParserRecordTests\n"
"#include <boost/filesystem.hpp>\n"
"#include <boost/test/unit_test.hpp>\n"
"#include <memory>\n"
"#include <opm/json/JsonObject.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserKeywords.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserItem.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserRecord.hpp>\n"
"#include <opm/parser/eclipse/Units/UnitSystem.hpp>\n"
"using namespace Opm;\n"
"auto unitSystem = UnitSystem::newMETRIC();\n";
const std::string sourceHeader =
"#include <opm/parser/eclipse/Deck/UDAValue.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserItem.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserRecord.hpp>\n"
"#include <opm/parser/eclipse/Parser/Parser.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserKeywords.hpp>\n\n\n"
"namespace Opm {\n"
"namespace ParserKeywords {\n\n";
"#include <opm/parser/eclipse/Parser/Parser.hpp>\n\n\n";
}
namespace Opm {
@@ -82,91 +65,79 @@ namespace Opm {
boost::filesystem::create_directories( file.parent_path());
}
bool KeywordGenerator::updateFile(const std::stringstream& newContent , const std::string& filename) {
void KeywordGenerator::updateFile(const std::stringstream& newContent , const std::string& filename) {
ensurePath(filename);
std::ofstream outputStream(filename);
outputStream << newContent.str();
return true;
}
static bool write_file( const std::stringstream& stream, const std::string& file, bool verbose, std::string desc = "source" ) {
auto update = KeywordGenerator::updateFile( stream, file );
if( !verbose ) return update;
if( update )
static void write_file( const std::stringstream& stream, const std::string& file, bool verbose, std::string desc = "source" ) {
KeywordGenerator::updateFile( stream, file );
if( verbose )
std::cout << "Updated " << desc << " file written to: " << file << std::endl;
else
std::cout << "No changes to " << desc << " file: " << file << std::endl;
return update;
}
bool KeywordGenerator::updateSource(const KeywordLoader& loader , const std::string& sourceFile ) const {
void KeywordGenerator::updateInitSource(const KeywordLoader& loader , const std::string& sourceFile ) const {
std::stringstream newSource;
newSource << sourceHeader << std::endl;
newSource << "#include <opm/parser/eclipse/Parser/Parser.hpp>" << std::endl;
for(const auto& kw_pair : loader) {
const auto& first_char = kw_pair.first;
newSource << "#include <opm/parser/eclipse/Parser/ParserKeywords/" << first_char << ".hpp>" << std::endl;
}
newSource << "namespace Opm {" << std::endl;
newSource << "namespace ParserKeywords {" << std::endl;
newSource << "void addDefaultKeywords(Parser& p);" << std::endl
<< "void addDefaultKeywords(Parser& p) {" << std::endl;
for( auto iter = loader.keyword_begin(); iter != loader.keyword_end(); ++iter ) {
newSource << "p.addKeyword< ParserKeywords::"
<< iter->second->className()
<< " >();" << std::endl;
}
for(const auto& kw_pair : loader) {
const auto& keywords = kw_pair.second;
for (const auto& kw: keywords)
newSource << " p.addKeyword< ParserKeywords::"
<< kw.className()
<< " >();" << std::endl;
}
newSource << "}" << std::endl;
newSource << "}" << std::endl;
for (auto iter = loader.keyword_begin(); iter != loader.keyword_end(); ++iter) {
std::shared_ptr<ParserKeyword> keyword = (*iter).second;
newSource << keyword->createCode() << std::endl;
}
newSource << "void Parser::addDefaultKeywords() {\n ParserKeywords::addDefaultKeywords(*this);\n}" << std::endl;
newSource << "}" << std::endl;
newSource << "void Parser::addDefaultKeywords() {" << std::endl
<< " Opm::ParserKeywords::addDefaultKeywords(*this);" << std::endl
<< "}}" << std::endl;
return write_file( newSource, sourceFile, m_verbose, "source" );
write_file( newSource, sourceFile, m_verbose, "init" );
}
bool KeywordGenerator::updateHeader(const KeywordLoader& loader, const std::string& headerBuildPath, const std::string& headerFile) const {
bool update = false;
void KeywordGenerator::updateKeywordSource(const KeywordLoader& loader , const std::string& sourcePath ) const {
std::map< char, std::vector< const ParserKeyword* > > keywords;
for( auto iter = loader.keyword_begin(); iter != loader.keyword_end(); ++iter )
keywords[ std::toupper( iter->second->className().at(0) ) ].push_back( iter->second.get() );
for(const auto& kw_pair : loader) {
const auto& first_char = kw_pair.first;
const auto& keywords = kw_pair.second;
std::stringstream newSource;
newSource << sourceHeader << std::endl;
newSource << std::endl << std::endl << "#include <opm/parser/eclipse/Parser/ParserKeywords/" << first_char << ".hpp>" << std::endl;
newSource << "namespace Opm {" << std::endl;
newSource << "namespace ParserKeywords {" << std::endl;
for (const auto& kw: keywords)
newSource << kw.createCode() << std::endl;
newSource << "}\n}" << std::endl;
write_file( newSource, sourcePath + "/" + first_char + ".cpp", m_verbose, "source" );
}
for( const auto& iter : keywords ) {
}
void KeywordGenerator::updateHeader(const KeywordLoader& loader, const std::string& headerBuildPath, const std::string& headerPath) const {
for( const auto& kw_pair : loader) {
std::stringstream stream;
const auto& first_char = kw_pair.first;
const auto& keywords = kw_pair.second;
stream << headerHeader( std::string( 1, std::toupper( iter.first ) ) );
for( auto& kw : iter.second )
stream << kw->createDeclaration(" ") << std::endl;
stream << headerHeader( std::string( 1, std::toupper( first_char ) ) );
for( auto& kw : keywords )
stream << kw.createDeclaration(" ") << std::endl;
stream << "}" << std::endl << "}" << std::endl;
stream << "#endif" << std::endl;
const auto final_path = headerBuildPath + headerFile + "/" + std::string( 1, iter.first ) + ".hpp";
if( write_file( stream, final_path, m_verbose, "header" ) )
update = true;
const auto final_path = headerBuildPath + headerPath+ "/" + std::string( 1, first_char ) + ".hpp";
write_file( stream, final_path, m_verbose, "header" );
}
std::stringstream stream;
stream << headerHeader("");
stream << "}}" << std::endl;
for( const auto& iter : keywords )
stream << "#include <"
<< headerFile + "/"
<< std::string( 1, std::toupper( iter.first ) ) + ".hpp>"
<< std::endl;
stream << "#endif" << std::endl;
const auto final_path = headerBuildPath + headerFile + ".hpp";
return write_file( stream, final_path, m_verbose, "header" ) || update;
}
@@ -181,32 +152,55 @@ namespace Opm {
bool KeywordGenerator::updateTest(const KeywordLoader& loader , const std::string& testFile) const {
void KeywordGenerator::updateTest(const KeywordLoader& loader , const std::string& testFile) const {
std::stringstream stream;
stream << testHeader;
for (auto iter = loader.keyword_begin(); iter != loader.keyword_end(); ++iter) {
const std::string& keywordName = (*iter).first;
std::shared_ptr<ParserKeyword> keyword = (*iter).second;
stream << startTest(keywordName);
stream << " std::string jsonFile = \"" << loader.getJsonFile( keywordName) << "\";" << std::endl;
stream << " boost::filesystem::path jsonPath( jsonFile );" << std::endl;
stream << " Json::JsonObject jsonConfig( jsonPath );" << std::endl;
stream << " ParserKeyword jsonKeyword(jsonConfig);" << std::endl;
stream << " ParserKeywords::" << keywordName << " inlineKeyword;" << std::endl;
stream << " BOOST_CHECK_EQUAL( jsonKeyword, inlineKeyword );" << std::endl;
stream << " if (jsonKeyword.hasDimension()) {" <<std::endl;
stream << " const auto& parserRecord = jsonKeyword.getRecord(0);" << std::endl;
stream << " for (size_t i=0; i < parserRecord.size(); i++){" << std::endl;
stream << " const auto& item = parserRecord.get( i );" << std::endl;
stream << " for (const auto& dim : item.dimensions())" << std::endl;
stream << " BOOST_CHECK_NO_THROW( unitSystem.getNewDimension( dim ));" << std::endl;
stream << " }" << std::endl;
stream << " }" << std::endl;
stream << endTest( );
for(const auto& kw_pair : loader) {
const auto& first_char = kw_pair.first;
stream << "#include <opm/parser/eclipse/Parser/ParserKeywords/" << first_char << ".hpp>" << std::endl;
}
return updateFile( stream , testFile );
stream << R"(
#define BOOST_TEST_MODULE GeneratedKeywordTest
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <memory>
#include <opm/json/JsonObject.hpp>
#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>
#include <opm/parser/eclipse/Parser/ParserItem.hpp>
#include <opm/parser/eclipse/Parser/ParserRecord.hpp>
#include <opm/parser/eclipse/Units/UnitSystem.hpp>
auto unitSystem = Opm::UnitSystem::newMETRIC();
namespace Opm {
void test_keyword(const ParserKeyword& inline_keyword, const std::string& json_file) {
boost::filesystem::path jsonPath( json_file );
Json::JsonObject jsonConfig( jsonPath );
ParserKeyword json_keyword(jsonConfig);
BOOST_CHECK_EQUAL( json_keyword, inline_keyword);
if (json_keyword.hasDimension()) {
const auto& parserRecord = json_keyword.getRecord(0);
for (size_t i=0; i < parserRecord.size(); i++){
const auto& item = parserRecord.get( i );
for (const auto& dim : item.dimensions())
BOOST_CHECK_NO_THROW( unitSystem.getNewDimension( dim ));
}
}
}
)";
for(const auto& kw_pair : loader) {
stream << std::endl << "BOOST_AUTO_TEST_CASE(TestKeywords" << kw_pair.first << ") {" << std::endl;
const auto& keywords = kw_pair.second;
for (const auto& kw: keywords)
stream << " test_keyword( ParserKeywords::" << kw.getName() << "(),\"" << loader.getJsonFile( kw.getName()) << "\");" << std::endl;
stream << "}" << std::endl;
}
stream << "}" << std::endl;
updateFile( stream , testFile );
}
}

View File

@@ -30,31 +30,46 @@
namespace Opm {
KeywordLoader::KeywordLoader(bool verbose)
: m_verbose(verbose)
KeywordLoader::KeywordLoader(const std::vector<std::string>& keyword_files, bool verbose)
{
}
std::map<std::string, ParserKeyword> keyword_map;
for (const auto& keyword_file : keyword_files) {
if (verbose)
std::cout << "Loading keyword from file: " << keyword_file << std::endl;
boost::filesystem::path path( keyword_file );
std::unique_ptr<ParserKeyword> parserKeyword;
try {
Json::JsonObject jsonConfig = Json::JsonObject( path );
parserKeyword.reset( new ParserKeyword(jsonConfig) );
boost::filesystem::path abs_path = boost::filesystem::absolute( path );
} catch (const std::exception& exc) {
std::cerr << std::endl;
std::cerr << "Failed to create parserkeyword from: " << path.string() << std::endl;
std::cerr << exc.what() << std::endl;
std::cerr << std::endl;
throw;
}
const std::string name = parserKeyword->getName();
if (keyword_map.find(name) != keyword_map.end()) {
this->m_jsonFile.erase(name);
keyword_map.erase(name);
}
size_t KeywordLoader::size() const {
return m_keywords.size();
}
this->m_jsonFile.insert( std::pair<std::string , std::string> ( name , keyword_file));
keyword_map.insert( std::pair<std::string , ParserKeyword> ( name , std::move(*parserKeyword)));
}
bool KeywordLoader::hasKeyword(const std::string& keyword) const {
if (m_keywords.find( keyword ) == m_keywords.end())
return false;
else
return true;
}
std::shared_ptr<const ParserKeyword> KeywordLoader::getKeyword(const std::string& keyword) const {
auto iter = m_keywords.find( keyword );
if (iter == m_keywords.end())
throw std::invalid_argument("Keyword " + keyword + " not loaded");
else
return (*iter).second;
this->keywords['Y'] = {};
this->keywords['X'] = {};
for (const auto& kw_pair : keyword_map) {
const auto& name = kw_pair.first;
const auto& parserKeyword = kw_pair.second;
this->keywords[name[0]].push_back( std::move(parserKeyword) );
}
}
@@ -67,53 +82,14 @@ namespace Opm {
}
void KeywordLoader::loadKeyword(boost::filesystem::path& path) {
std::shared_ptr<Json::JsonObject> jsonConfig = std::make_shared<Json::JsonObject>( path );
try {
std::shared_ptr<ParserKeyword> parserKeyword = std::make_shared<ParserKeyword>(*jsonConfig);
boost::filesystem::path abs_path = boost::filesystem::absolute( path );
addKeyword( parserKeyword , abs_path.generic_string() );
} catch (const std::exception& exc) {
std::cerr << std::endl;
std::cerr << "Failed to create parserkeyword from: " << path.string() << std::endl;
std::cerr << exc.what() << std::endl;
std::cerr << std::endl;
throw;
}
std::map<char, std::vector<ParserKeyword >>::const_iterator KeywordLoader::begin( ) const {
return this->keywords.begin( );
}
void KeywordLoader::loadKeyword(const std::string& filename) {
boost::filesystem::path path( filename );
if (m_verbose)
std::cout << "Loading keyword from file: " << filename << std::endl;
return loadKeyword( path );
std::map<char , std::vector<ParserKeyword >>::const_iterator KeywordLoader::end( ) const {
return this->keywords.end( );
}
void KeywordLoader::addKeyword(std::shared_ptr<ParserKeyword> keyword , const std::string& jsonFile) {
const std::string& name = keyword->getName();
if (hasKeyword(name)) {
m_keywords[name] = keyword;
m_jsonFile[name] = jsonFile;
} else {
m_keywords.insert( std::pair<std::string , std::shared_ptr<ParserKeyword> > (name , keyword) );
m_jsonFile.insert( std::pair<std::string , std::string> ( name , jsonFile));
}
}
std::map<std::string , std::shared_ptr<ParserKeyword> >::const_iterator KeywordLoader::keyword_begin( ) const {
return m_keywords.begin( );
}
std::map<std::string , std::shared_ptr<ParserKeyword> >::const_iterator KeywordLoader::keyword_end( ) const {
return m_keywords.end( );
}
}

View File

@@ -60,38 +60,37 @@ int main(int , char ** argv) {
<< std::endl;
}
const char * keyword_list_file = argv[1];
const char * source_file_name = argv[2];
const char * header_file_base_path = argv[3];
const char * header_file_name = argv[4];
const char * test_file_name = argv[5];
const char * source_file_path = argv[2];
const char * init_file_name = argv[3];
const char * header_file_base_path = argv[4];
const char * header_file_path = argv[5];
const char * test_file_name = argv[6];
Opm::KeywordGenerator generator( true );
Opm::KeywordLoader loader( false );
std::vector<std::string> keyword_list;
{
std::string buffer;
std::ifstream is(keyword_list_file);
std::getline( is , buffer );
is.close();
size_t start = 0;
std::string keyword_list;
{
std::ifstream is(keyword_list_file);
std::getline( is , keyword_list );
is.close();
}
while (true) {
size_t end = keyword_list.find( ";" , start);
size_t end = buffer.find( ";" , start);
if (end == std::string::npos) {
loader.loadKeyword( keyword_list.substr( start ));
keyword_list.push_back( buffer.substr(start) );
break;
}
loader.loadKeyword( keyword_list.substr( start , end - start ));
keyword_list.push_back( buffer.substr(start, end - start ));
start = end + 1;
}
}
Opm::KeywordLoader loader( keyword_list, false );
Opm::KeywordGenerator generator( true );
generator.updateSource(loader , source_file_name );
generator.updateHeader(loader, header_file_base_path, header_file_name );
generator.updateKeywordSource(loader , source_file_path );
generator.updateInitSource(loader , init_file_name );
generator.updateHeader(loader, header_file_base_path, header_file_path );
generator.updateTest( loader , test_file_name );
}

View File

@@ -1,66 +0,0 @@
/*
Copyright 2015 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 <stdexcept>
#include <iostream>
#include <cstdio>
#define BOOST_TEST_MODULE InputKeywordTests
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <opm/parser/eclipse/Generator/KeywordLoader.hpp>
#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>
#include <opm/parser/eclipse/Parser/ParserRecord.hpp>
inline std::string prefix() {
return boost::unit_test::framework::master_test_suite().argv[1];
}
BOOST_AUTO_TEST_CASE(EmptyKeywordLoader) {
Opm::KeywordLoader loader(false);
BOOST_CHECK_EQUAL( false , loader.hasKeyword("NO"));
BOOST_CHECK_EQUAL( 0U , loader.size() );
BOOST_CHECK_THROW( loader.getKeyword("NO") , std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(LoadKeyword) {
Opm::KeywordLoader loader(false);
BOOST_CHECK_THROW( loader.loadKeyword("does/not/exists") , std::invalid_argument );
BOOST_CHECK_THROW( loader.loadKeyword(prefix() + "invalid.json") , std::invalid_argument);
BOOST_CHECK_THROW( loader.loadKeyword(prefix() + "PORO-invalid") , std::invalid_argument);
loader.loadKeyword(prefix() + "PORO.json");
loader.loadKeyword(prefix() + "PORO.json");
BOOST_CHECK_EQUAL( true , loader.hasKeyword("PORO"));
BOOST_CHECK_EQUAL( 1U , loader.size() );
loader.getKeyword("PORO");
}

View File

@@ -46,8 +46,6 @@
#include <opm/parser/eclipse/EclipseState/Schedule/VFPProdTable.hpp>
#include <opm/parser/eclipse/EclipseState/Schedule/VFPInjTable.hpp>
#include <opm/parser/eclipse/Parser/ParserKeywords.hpp>
#include <opm/parser/eclipse/Units/UnitSystem.hpp>
#include <stdexcept>