Merge branch '2018.01.01-patch' into dev. #2434

This commit is contained in:
Bjørn Erik Jensen 2018-02-05 14:15:50 +01:00
commit 0b0e60135c
6 changed files with 249 additions and 36 deletions

View File

@ -18,11 +18,14 @@
#include "RicfCommandFileExecutor.h" #include "RicfCommandFileExecutor.h"
#include "RifcCommandFileReader.h"
#include "RicfCommandObject.h"
#include "RiaLogging.h" #include "RiaLogging.h"
#include "RicfCloseProject.h"
#include "RicfCommandObject.h"
#include "RicfOpenProject.h"
#include "RicfReplaceCase.h"
#include "RifcCommandFileReader.h"
namespace caf { namespace caf {
template<> template<>
void RicfCommandFileExecutor::ExportTypeEnum::setUp() void RicfCommandFileExecutor::ExportTypeEnum::setUp()
@ -54,7 +57,9 @@ RicfCommandFileExecutor::~RicfCommandFileExecutor()
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void RicfCommandFileExecutor::executeCommands(QTextStream& stream) void RicfCommandFileExecutor::executeCommands(QTextStream& stream)
{ {
std::vector<RicfCommandObject*> commands = RicfCommandFileReader::readCommands(stream, caf::PdmDefaultObjectFactory::instance(), &m_messages); std::vector<RicfCommandObject*> executableCommands;
{
std::vector<RicfCommandObject*> fileCommands = RicfCommandFileReader::readCommands(stream, caf::PdmDefaultObjectFactory::instance(), &m_messages);
for (auto message : m_messages.m_messages) for (auto message : m_messages.m_messages)
{ {
if (message.first == RicfMessages::MESSAGE_WARNING) if (message.first == RicfMessages::MESSAGE_WARNING)
@ -64,12 +69,32 @@ void RicfCommandFileExecutor::executeCommands(QTextStream& stream)
else else
{ {
RiaLogging::error(QString("Command file parsing error: %1").arg(message.second)); RiaLogging::error(QString("Command file parsing error: %1").arg(message.second));
for (auto& command : fileCommands)
{
delete command;
command = nullptr;
}
return; return;
} }
} }
for (RicfCommandObject* command : commands)
executableCommands = RicfCommandFileExecutor::prepareFileCommandsForExecution(fileCommands);
}
for (auto& command : executableCommands)
{ {
command->execute(); command->execute();
delete command;
command = nullptr;
}
// All command objects should be deleted and grounded at this point
for (auto c : executableCommands)
{
CAF_ASSERT(c == nullptr);
} }
} }
@ -119,3 +144,66 @@ RicfCommandFileExecutor* RicfCommandFileExecutor::instance()
static RicfCommandFileExecutor* commandFileExecutorInstance = new RicfCommandFileExecutor(); static RicfCommandFileExecutor* commandFileExecutorInstance = new RicfCommandFileExecutor();
return commandFileExecutorInstance; return commandFileExecutorInstance;
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RicfCommandObject*> RicfCommandFileExecutor::prepareFileCommandsForExecution(const std::vector<RicfCommandObject*>& commandsReadFromFile)
{
// This function will merge multiple RicfSingleCaseReplace object by a single RicfMultiCaseReplace object
// A command file version for multi case replace was rejected by @hhgs 2018-02-02
//
// The reason for this is based on two requirements
// 1. Ability to aggregate info from multiple replaceCase() statements in a command file
// 2. Improve performance, as a replace case is implemented by reading a project file from XML and replace file paths
// during project loading
std::vector<RicfCommandObject*> executableCommands;
{
std::vector<RicfSingleCaseReplace*> objectsToBeDeleted;
std::vector<RicfSingleCaseReplace*> batchOfReplaceCaseFileObjects;
std::map<int, QString> aggregatedCasePathPairs;
for (RicfCommandObject* command : commandsReadFromFile)
{
RicfSingleCaseReplace* fileReplaceCase = dynamic_cast<RicfSingleCaseReplace*>(command);
if (fileReplaceCase)
{
aggregatedCasePathPairs[fileReplaceCase->caseId()] = fileReplaceCase->filePath();
batchOfReplaceCaseFileObjects.push_back(fileReplaceCase);
objectsToBeDeleted.push_back(fileReplaceCase);
}
else
{
if (!batchOfReplaceCaseFileObjects.empty())
{
RicfMultiCaseReplace* multiCaseReplace = new RicfMultiCaseReplace;
multiCaseReplace->setCaseReplacePairs(aggregatedCasePathPairs);
executableCommands.push_back(multiCaseReplace);
batchOfReplaceCaseFileObjects.clear();
}
if (dynamic_cast<RicfOpenProject*>(command) || dynamic_cast<RicfCloseProject*>(command))
{
// Reset aggregation when openProject or closeProject is issued
aggregatedCasePathPairs.clear();
}
executableCommands.push_back(command);
}
}
// Delete RicfSingleCaseReplace objects, as they are replaced by RicfMultiCaseReplace
for (auto objToDelete : objectsToBeDeleted)
{
delete objToDelete;
objToDelete = nullptr;
}
}
return executableCommands;
}

View File

@ -23,6 +23,9 @@
#include "cafAppEnum.h" #include "cafAppEnum.h"
#include <map> #include <map>
#include <vector>
class RicfCommandObject;
//================================================================================================== //==================================================================================================
// //
@ -53,6 +56,8 @@ public:
static RicfCommandFileExecutor* instance(); static RicfCommandFileExecutor* instance();
static std::vector<RicfCommandObject*> prepareFileCommandsForExecution(const std::vector<RicfCommandObject*>& commandsReadFromFile);
private: private:
RicfMessages m_messages; RicfMessages m_messages;

View File

@ -18,18 +18,20 @@
#include "RicfReplaceCase.h" #include "RicfReplaceCase.h"
#include "RicfCommandFileExecutor.h"
#include "RiaApplication.h" #include "RiaApplication.h"
#include "RiaLogging.h" #include "RiaLogging.h"
#include "RiaProjectModifier.h" #include "RiaProjectModifier.h"
CAF_PDM_SOURCE_INIT(RicfReplaceCase, "replaceCase"); #include "RicfCommandFileExecutor.h"
#include "RimProject.h"
CAF_PDM_SOURCE_INIT(RicfSingleCaseReplace, "replaceCase");
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
RicfReplaceCase::RicfReplaceCase() RicfSingleCaseReplace::RicfSingleCaseReplace()
{ {
RICF_InitField(&m_caseId, "caseId", -1, "Case ID", "", "", ""); RICF_InitField(&m_caseId, "caseId", -1, "Case ID", "", "", "");
RICF_InitField(&m_newGridFile, "newGridFile", QString(), "New Grid File", "", "", ""); RICF_InitField(&m_newGridFile, "newGridFile", QString(), "New Grid File", "", "", "");
@ -38,11 +40,56 @@ RicfReplaceCase::RicfReplaceCase()
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void RicfReplaceCase::execute() int RicfSingleCaseReplace::caseId() const
{ {
if (m_newGridFile().isNull()) return m_caseId;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RicfSingleCaseReplace::filePath() const
{
return m_newGridFile;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfSingleCaseReplace::execute()
{
// Never call execute on this object, information is aggregated into RicfMultiCaseReplace
CAF_ASSERT(false);
}
CAF_PDM_SOURCE_INIT(RicfMultiCaseReplace, "replaceCaseImpl_no_support_for_command_file_text_parsing");
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfMultiCaseReplace::RicfMultiCaseReplace()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfMultiCaseReplace::setCaseReplacePairs(const std::map<int, QString>& caseIdToGridFileNameMap)
{
m_caseIdToGridFileNameMap = caseIdToGridFileNameMap;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfMultiCaseReplace::execute()
{
if (m_caseIdToGridFileNameMap.empty())
{ {
RiaLogging::error("replaceCase: Required parameter newGridFile."); RiaLogging::error("replaceCaseImpl: No replacements available.");
return; return;
} }
@ -53,15 +100,19 @@ void RicfReplaceCase::execute()
return; return;
} }
cvf::ref<RiaProjectModifier> projectModifier = new RiaProjectModifier; cvf::ref<RiaProjectModifier> projectModifier = new RiaProjectModifier;
if (m_caseId() == -1) for (const auto& a : m_caseIdToGridFileNameMap)
{ {
projectModifier->setReplaceCaseFirstOccurrence(m_newGridFile()); const auto caseId = a.first;
const auto filePath = a.second;
if (caseId < 0)
{
projectModifier->setReplaceCaseFirstOccurrence(filePath);
} }
else else
{ {
projectModifier->setReplaceCase(m_caseId(), m_newGridFile()); projectModifier->setReplaceCase(caseId, filePath);
}
} }
RiaApplication::instance()->loadProject(lastProjectPath, RiaApplication::PLA_NONE, projectModifier.p()); RiaApplication::instance()->loadProject(lastProjectPath, RiaApplication::PLA_NONE, projectModifier.p());

View File

@ -23,16 +23,22 @@
#include "cafPdmField.h" #include "cafPdmField.h"
//================================================================================================== //==================================================================================================
// RicfSingleCaseReplace represents the parsed command from text file able to replace one case ID with
// a new file name
// //
// This is the preferred interface on file, based on discussion with @hhgs 2018-02-02
// //
// // Multiple objects of this type can be aggregated into RicfMultipleReplaceCase
//================================================================================================== //==================================================================================================
class RicfReplaceCase : public RicfCommandObject class RicfSingleCaseReplace : public RicfCommandObject
{ {
CAF_PDM_HEADER_INIT; CAF_PDM_HEADER_INIT;
public: public:
RicfReplaceCase(); RicfSingleCaseReplace();
int caseId() const;
QString filePath() const;
virtual void execute() override; virtual void execute() override;
@ -40,3 +46,25 @@ private:
caf::PdmField<QString> m_newGridFile; caf::PdmField<QString> m_newGridFile;
caf::PdmField<int> m_caseId; caf::PdmField<int> m_caseId;
}; };
//==================================================================================================
// RicfMultipleReplaceCase represents multiple caseId-gridFileName pairs
//
// NB! This object has no support for parsing a text command. This object is created by aggregating
// multiple RicfSingleCaseReplace objects
//
//==================================================================================================
class RicfMultiCaseReplace : public RicfCommandObject
{
CAF_PDM_HEADER_INIT;
public:
RicfMultiCaseReplace();
void setCaseReplacePairs(const std::map<int, QString>& caseIdToGridFileNameMap);
virtual void execute() override;
private:
std::map<int, QString> m_caseIdToGridFileNameMap;
};

View File

@ -440,6 +440,7 @@ const std::vector<QString>& RifEclipseInputFileTools::invalidPropertyDataKeyword
keywords.push_back("ZCORN"); keywords.push_back("ZCORN");
keywords.push_back("SPECGRID"); keywords.push_back("SPECGRID");
keywords.push_back("MAPAXES"); keywords.push_back("MAPAXES");
keywords.push_back("NOECHO");
keywords.push_back(faultsKeyword); keywords.push_back(faultsKeyword);

View File

@ -1,9 +1,11 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "RifcCommandFileReader.h" #include "RicfCommandFileExecutor.h"
#include "RicfCommandObject.h" #include "RicfCommandObject.h"
#include "cafPdmField.h"
#include "RicfMessages.h" #include "RicfMessages.h"
#include "RifcCommandFileReader.h"
#include "cafPdmField.h"
class TestCommand1: public RicfCommandObject class TestCommand1: public RicfCommandObject
{ {
@ -165,3 +167,41 @@ TEST(RicfCommands, EmptyArgumentList)
delete(obj); delete(obj);
} }
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST(RicfCommands, TransformFileCommandObjectsToExecutableCommandObjects)
{
QString commandString = R"(
replaceCase(newGridFile="/1.EGRID", caseId=1)
replaceCase(newGridFile="/2.EGRID", caseId=2)
openProject(path="/home/user/ResInsightProject.rsp")
replaceCase(newGridFile="/3.EGRID", caseId=3)
replaceCase(newGridFile="/4.EGRID", caseId=4)
exportSnapshots()
replaceCase(newGridFile="/6.EGRID", caseId=6)
replaceCase(newGridFile="/7.EGRID", caseId=7)
closeProject()
)";
QTextStream inputStream(&commandString);
RicfMessages errors;
auto objects = RicfCommandFileReader::readCommands(inputStream, caf::PdmDefaultObjectFactory::instance(), &errors);
EXPECT_TRUE(errors.m_messages.empty());
EXPECT_EQ((size_t)9, objects.size());
auto exeObjects = RicfCommandFileExecutor::prepareFileCommandsForExecution(objects);
EXPECT_EQ((size_t)6, exeObjects.size());
for (auto obj : exeObjects)
{
delete(obj);
}
}