Files
opm-common/opm/parser/eclipse/Generator/KeywordGenerator.cpp
Joakim Hove 1161e1098c Rewritten code generation:
With this commit the generation of built in keywords is completely
changed. The most important changes include:

  1) We have autogenerated a class for each keyword in the new
     ParserKeywords { ... } namespace.

  2) The autogenerated classes derive from ParserKeyword, and the
     default constructor will build of a fully initialized
     ParserKeyword instance, i.e. the keyword used to parse the EQUIL
     keyword can be instantiated as simple as:

         ParserKeywords::EQUIL kw;

  3) The generated keywords have built in static constants for keyword
     and item names, and item default values. That way it should be
     possible for the compiler to catch trivial errors like trying to
     access the keyword "PoRO"; also the the access to default values
     means that properties can be initialized without actually
     insantiating a DeckKeyword.

  4) Two new classes Generator/KeywordLoader and
     Generator/KeywordGenerator have been created, with the help of
     these classes the keyword generation code is significantly
     simplified.
2015-06-08 11:29:18 +02:00

213 lines
8.3 KiB
C++

/*
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 <fstream>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <opm/json/JsonObject.hpp>
#include <opm/parser/eclipse/Generator/KeywordGenerator.hpp>
namespace Opm {
KeywordGenerator::KeywordGenerator(bool verbose)
: m_verbose( verbose )
{
}
std::string testHeader() {
std::string header = "#define BOOST_TEST_MODULE\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/ParserIntItem.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserStringItem.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserDoubleItem.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserFloatItem.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserRecord.hpp>\n"
"#include <opm/parser/eclipse/Units/UnitSystem.hpp>\n"
"using namespace Opm;\n"
"std::shared_ptr<UnitSystem> unitSystem( UnitSystem::newMETRIC() );\n";
return header;
}
std::string KeywordGenerator::sourceHeader() {
std::string header = "#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserItem.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserIntItem.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserStringItem.hpp>\n"
"#include <opm/parser/eclipse/Parser/ParserDoubleItem.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";
return header;
}
std::string KeywordGenerator::headerHeader() {
std::string header = "#ifndef PARSER_KEYWORDS_HPP\n"
"#define PARSER_KEYWORDS_HPP\n"
"#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>\n"
"namespace Opm {\n"
"namespace ParserKeywords {\n\n";
return header;
}
void KeywordGenerator::ensurePath( const std::string& file_name) {
boost::filesystem::path file(file_name);
if (!boost::filesystem::is_directory( file.parent_path()))
boost::filesystem::create_directories( file.parent_path());
}
bool KeywordGenerator::updateFile(std::stringstream& newContent , const std::string& filename) {
std::ifstream stream(filename.c_str());
bool update = true;
ensurePath( filename );
if (stream.is_open()) {
std::stringstream oldContent;
oldContent << stream.rdbuf( );
if (oldContent.str() == newContent.str())
update = false;
}
if (update) {
std::fstream stream(filename.c_str() , std::fstream::out);
stream << newContent.str();
stream.close();
}
return update;
}
bool KeywordGenerator::updateSource(const KeywordLoader& loader , const std::string& sourceFile) const {
std::stringstream newSource;
newSource << sourceHeader();
for (auto iter = loader.keyword_begin(); iter != loader.keyword_end(); ++iter) {
std::shared_ptr<ParserKeyword> keyword = (*iter).second;
newSource << keyword->createCode() << std::endl;
}
newSource << "}" << std::endl;
{
newSource << "void Parser::addDefaultKeywords() {" << std::endl;
for (auto iter = loader.keyword_begin(); iter != loader.keyword_end(); ++iter) {
std::shared_ptr<ParserKeyword> keyword = (*iter).second;
newSource << " addParserKeyword( std::shared_ptr<ParserKeyword>( new ParserKeywords::" << keyword->className() << "()));" << std::endl;
}
newSource << "}" << std::endl;
}
newSource << "}" << std::endl;
{
bool update = updateFile( newSource , sourceFile );
if (m_verbose) {
if (update)
std::cout << "Updated source file written to: " << sourceFile << std::endl;
else
std::cout << "No changes to source file: " << sourceFile << std::endl;
}
return update;
}
}
bool KeywordGenerator::updateHeader(const KeywordLoader& loader , const std::string& headerFile) const {
std::stringstream stream;
stream << headerHeader();
for (auto iter = loader.keyword_begin(); iter != loader.keyword_end(); ++iter) {
std::shared_ptr<ParserKeyword> keyword = (*iter).second;
stream << keyword->createDeclaration(" ") << std::endl;
}
stream << "}" << std::endl << "}" << std::endl;
stream << "#endif" << std::endl;
{
bool update = updateFile( stream , headerFile );
if (m_verbose) {
if (update)
std::cout << "Updated header file written to: " << headerFile << std::endl;
else
std::cout << "No changes to header file: " << headerFile << std::endl;
}
return update;
}
}
std::string KeywordGenerator::startTest(const std::string& keyword_name) {
return std::string("BOOST_AUTO_TEST_CASE(TEST") + keyword_name + std::string("Keyword) {\n");
}
std::string KeywordGenerator::endTest() {
return "}\n\n";
}
bool 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( jsonKeyword.equal( inlineKeyword ));" << std::endl;
stream << " if (jsonKeyword.hasDimension()) {" <<std::endl;
stream << " ParserRecordConstPtr parserRecord = jsonKeyword.getRecord(0);" << std::endl;
stream << " for (size_t i=0; i < parserRecord->size(); i++){ " << std::endl;
stream << " ParserItemConstPtr item = parserRecord->get( i );" << std::endl;
stream << " for (size_t j=0; j < item->numDimensions(); j++) {" << std::endl;
stream << " std::string dimString = item->getDimension(j);" << std::endl;
stream << " BOOST_CHECK_NO_THROW( unitSystem->getNewDimension( dimString ));" << std::endl;
stream << " }" << std::endl;
stream << " }" << std::endl;
stream << " }" << std::endl;
stream << endTest( );
}
return updateFile( stream , testFile );
}
}