Assemble DeckTree during parse process

This commit is contained in:
Joakim Hove
2021-09-22 08:18:57 +02:00
parent 6f94580ff1
commit a172879395
5 changed files with 47 additions and 45 deletions

View File

@@ -27,6 +27,7 @@
#include <vector>
#include <string>
#include <opm/parser/eclipse/Deck/DeckTree.hpp>
#include <opm/parser/eclipse/Deck/DeckKeyword.hpp>
#include <opm/parser/eclipse/Units/UnitSystem.hpp>
@@ -153,6 +154,8 @@ namespace Opm {
std::string getDataFile() const;
void setDataFile(const std::string& dataFile);
std::string makeDeckPath(const std::string& path) const;
DeckTree& tree();
DeckTree tree() const;
std::size_t size() const;
bool empty() const;
@@ -183,6 +186,7 @@ namespace Opm {
std::optional<std::string> m_dataFile;
std::string input_path;
DeckTree file_tree;
mutable std::size_t unit_system_access_count = 0;
};
}

View File

@@ -19,12 +19,15 @@
#include <algorithm>
#include <vector>
#include <opm/common/utility/FileSystem.hpp>
#include <opm/parser/eclipse/Deck/Deck.hpp>
#include <opm/parser/eclipse/Deck/DeckOutput.hpp>
#include <opm/parser/eclipse/Deck/DeckKeyword.hpp>
#include <opm/parser/eclipse/Deck/DeckSection.hpp>
#include <opm/parser/eclipse/Units/UnitSystem.hpp>
namespace fs = Opm::filesystem;
namespace Opm {
bool DeckView::hasKeyword( const DeckKeyword& keyword ) const {
@@ -156,7 +159,8 @@ namespace Opm {
keywordList( d.keywordList ),
defaultUnits( d.defaultUnits ),
m_dataFile( d.m_dataFile ),
input_path( d.input_path )
input_path( d.input_path ),
file_tree( d.file_tree )
{
this->init(this->keywordList.begin(), this->keywordList.end());
if (d.activeUnits)
@@ -169,7 +173,8 @@ namespace Opm {
keywordList( std::move(d.keywordList) ),
defaultUnits( d.defaultUnits ),
m_dataFile( d.m_dataFile ),
input_path( d.input_path )
input_path( d.input_path ),
file_tree( std::move(d.file_tree) )
{
this->init(this->keywordList.begin(), this->keywordList.end());
if (d.activeUnits)
@@ -280,8 +285,20 @@ namespace Opm {
this->input_path = "";
else
this->input_path = dataFile.substr(0, slash_pos);
this->file_tree.add_root(dataFile);
}
DeckTree& Deck::tree() {
return this->file_tree;
}
DeckTree Deck::tree() const {
return this->file_tree;
}
Deck::iterator Deck::begin() {
return this->keywordList.begin();
}

View File

@@ -1011,8 +1011,11 @@ bool parseState( ParserState& parserState, const Parser& parser ) {
std::string includeFileAsString = readValueToken<std::string>(firstRecord.getItem(0));
const auto& includeFile = parserState.getIncludeFilePath( includeFileAsString );
if (includeFile.has_value())
if (includeFile.has_value()) {
auto& deck_tree = parserState.deck.tree();
deck_tree.add_include(filesystem::absolute(parserState.current_path()), includeFile.value() );
parserState.loadFile( includeFile.value() );
}
continue;
}

View File

@@ -150,13 +150,6 @@ BOOST_AUTO_TEST_CASE(set_and_get_data_file) {
BOOST_CHECK_EQUAL("", deck.getInputPath());
BOOST_CHECK_EQUAL("some/path", deck.makeDeckPath("some/path"));
BOOST_CHECK_EQUAL("/abs/path", deck.makeDeckPath("/abs/path"));
std::string file("/path/to/file.DATA");
deck.setDataFile( file );
BOOST_CHECK_EQUAL(file, deck.getDataFile());
BOOST_CHECK_EQUAL("/path/to", deck.getInputPath());
BOOST_CHECK_EQUAL("/path/to/some/path", deck.makeDeckPath("some/path"));
BOOST_CHECK_EQUAL("/abs/path", deck.makeDeckPath("/abs/path"));
}
BOOST_AUTO_TEST_CASE(DummyDefaultsString) {
@@ -689,37 +682,3 @@ BOOST_AUTO_TEST_CASE(STRING_TO_BOOL) {
}
BOOST_AUTO_TEST_CASE(DeckTreeTest2) {
DeckTree dt;
dt.includes("no_such_file", "target");
dt.add_root("root");
dt.add_include("root", "include1");
dt.add_include("include1", "include2");
BOOST_CHECK( dt.includes("root", "include1") );
BOOST_CHECK( dt.includes("include1", "include2") );
BOOST_CHECK_THROW( dt.add_root("New_root"), std::exception);
}
BOOST_AUTO_TEST_CASE(DeckTreeTest) {
DeckTree dt("root");
BOOST_CHECK_THROW( dt.includes("no_such_file", "inc_file"), std::exception );
BOOST_CHECK_THROW( dt.add_include("no_such_file", "target"), std::exception);
dt.add_include("root", "include1");
dt.add_include("include1", "include2");
BOOST_CHECK( dt.includes("root", "include1") );
BOOST_CHECK( dt.includes("include1", "include2") );
BOOST_CHECK( !dt.includes("root", "include2") );
BOOST_CHECK_EQUAL( dt.parent("include2"), "include1" );
BOOST_CHECK_EQUAL( dt.parent("include1"), "root" );
BOOST_CHECK_THROW( dt.add_root("New_root"), std::exception);
}

View File

@@ -23,11 +23,16 @@
#include <boost/test/unit_test.hpp>
#include <fstream>
#include <iostream>
#include <opm/common/utility/FileSystem.hpp>
#include <opm/parser/eclipse/Deck/Deck.hpp>
#include <opm/parser/eclipse/Parser/Parser.hpp>
#include <opm/parser/eclipse/EclipseState/IOConfig/IOConfig.hpp>
#include "tests/WorkArea.cpp"
namespace fs = Opm::filesystem;
using namespace Opm;
const std::string& deckStr = R"(
@@ -258,12 +263,25 @@ GRIDFILE
BOOST_CHECK( !ioConfig.getWriteEGRIDFile() );
}
void touch_file(const fs::path& file) {
if (!fs::exists(file)) {
if (file.has_parent_path()) {
const auto& parent_path = file.parent_path();
if (!fs::is_directory(parent_path))
fs::create_directories(parent_path);
}
std::ofstream os{file};
}
}
BOOST_AUTO_TEST_CASE(OutputPaths) {
WorkArea wa;
IOConfig config1( "" );
BOOST_CHECK_EQUAL("", config1.getBaseName() );
Deck deck2;
touch_file("testString.DATA");
deck2.setDataFile( "testString.DATA" );
IOConfig config2( deck2 );
std::string output_dir2 = ".";
@@ -273,7 +291,8 @@ BOOST_AUTO_TEST_CASE(OutputPaths) {
namespace fs = Opm::filesystem;
Deck deck3;
deck3.setDataFile( "/path/to/testString.DATA" );
touch_file("path/to/testString.DATA");
deck3.setDataFile( "path/to/testString.DATA" );
IOConfig config3( deck3 );
std::string output_dir3 = "/path/to";
config3.setOutputDir( output_dir3 );