#4516 Fix replaceCase in Python gRPC

This commit is contained in:
Gaute Lindkvist 2019-07-16 17:12:08 +02:00
parent f2c72a0e71
commit bbc8750d8f
5 changed files with 68 additions and 22 deletions

View File

@ -58,15 +58,37 @@ QString RicfSingleCaseReplace::filePath() const
//--------------------------------------------------------------------------------------------------
RicfCommandResponse RicfSingleCaseReplace::execute()
{
// Never call execute on this object, information is aggregated into RicfMultiCaseReplace
CAF_ASSERT(false);
return RicfCommandResponse(RicfCommandResponse::COMMAND_ERROR, "Never call execute on a RicfSingleCaseReplace object");
QString lastProjectPath = RicfCommandFileExecutor::instance()->getLastProjectPath();
if (lastProjectPath.isNull())
{
QString errMsg(
"replaceCase: 'openProject' must be called before 'replaceCase' to specify project file to replace case in.");
RiaLogging::error(errMsg);
return RicfCommandResponse(RicfCommandResponse::COMMAND_ERROR, errMsg);
}
cvf::ref<RiaProjectModifier> projectModifier = new RiaProjectModifier;
if (m_caseId() < 0)
{
projectModifier->setReplaceCaseFirstOccurrence(m_newGridFile());
}
else
{
projectModifier->setReplaceCase(m_caseId(), m_newGridFile());
}
if (!RiaApplication::instance()->loadProject(lastProjectPath, RiaApplication::PLA_NONE, projectModifier.p()))
{
QString errMsg("Could not reload project");
RiaLogging::error(errMsg);
return RicfCommandResponse(RicfCommandResponse::COMMAND_ERROR, errMsg);
}
return RicfCommandResponse();
}
CAF_PDM_SOURCE_INIT(RicfMultiCaseReplace, "replaceCaseImpl_no_support_for_command_file_text_parsing");
CAF_PDM_SOURCE_INIT(RicfMultiCaseReplace, "replaceMultipleCases");
//--------------------------------------------------------------------------------------------------
///

View File

@ -51,7 +51,7 @@ private:
// 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
// multiple RicfSingleCaseReplace objects, or through gRPC interface.
//
//==================================================================================================
class RicfMultiCaseReplace : public RicfCommandObject

View File

@ -25,6 +25,11 @@ message ReplaceCaseRequest
int32 caseId = 2;
}
message ReplaceCaseRequests
{
repeated ReplaceCaseRequest casePairs = 1;
}
message ReplaceSourceCasesRequest
{
string gridListFile = 1;
@ -264,6 +269,7 @@ message CommandParams
CreateMultipleFracRequest createMultipleFractures = 23;
CreateLgrForCompRequest createLgrForCompletions = 24;
CreateSatPressPlotRequest createSaturationPressurePlots = 25;
ReplaceCaseRequests replaceMultipleCases = 26;
}
}

View File

@ -76,7 +76,7 @@ def test_brugge_0010(rips_instance, initializeTest):
@pytest.mark.skipif(sys.platform.startswith('linux'), reason="Brugge is currently exceptionally slow on Linux")
def test_replaceCase(rips_instance, initializeTest):
project = rips_instance.project.open(dataroot.PATH + "/TEST10K_FLT_LGR_NNC/10KWithWellLog.rsp")
casePath = dataroot.PATH + "/Case_with_10_timesteps/Real10/BRUGGE_0010.EGRID"
casePath = dataroot.PATH + "/Case_with_10_timesteps/Real0/BRUGGE_0000.EGRID"
case = project.case(id=0)
assert(case is not None)
assert(case.name == "TEST10K_FLT_LGR_NNC")
@ -84,9 +84,9 @@ def test_replaceCase(rips_instance, initializeTest):
cases = rips_instance.project.cases()
assert(len(cases) is 1)
rips_instance.commands.replaceCase(newGridFile=casePath, caseId=case.id)
rips_instance.commands.replaceCase(newGridFile=os.path.abspath(casePath), caseId=case.id)
cases = rips_instance.project.cases()
assert(len(cases) is 1)
case = project.case(id=0)
assert(case.name == "BRUGGE_0000")
assert(case.name == "Real0--BRUGGE_0000.EGRID")
assert(case.id == 0)

View File

@ -19,6 +19,7 @@
#include "RiaGrpcCallbacks.h"
#include "RicfReplaceCase.h"
#include "RicfSetTimeStep.h"
#include "cafAssert.h"
@ -52,22 +53,39 @@ grpc::Status RiaGrpcCommandService::Execute(grpc::ServerContext* context, const
const Message& params = request->GetReflection()->GetMessage(*request, grpcOneOfMessage);
QString grpcOneOfMessageName = QString::fromStdString(grpcOneOfMessage->name());
auto pdmObjectHandle = caf::PdmDefaultObjectFactory::instance()->create(grpcOneOfMessageName);
auto commandHandle = dynamic_cast<RicfCommandObject*>(pdmObjectHandle);
auto pdmObjectHandle = caf::PdmDefaultObjectFactory::instance()->create(grpcOneOfMessageName);
auto commandHandle = dynamic_cast<RicfCommandObject*>(pdmObjectHandle);
if (commandHandle)
{
auto subMessageDescriptor = grpcOneOfMessage->message_type();
int numParameters = subMessageDescriptor->field_count();
for (int i = 0; i < numParameters; ++i)
RicfMultiCaseReplace* multiCaseReplaceCommand = dynamic_cast<RicfMultiCaseReplace*>(commandHandle);
if (multiCaseReplaceCommand)
{
auto parameter = subMessageDescriptor->field(i);
if (parameter)
CAF_ASSERT(request->has_replacemultiplecases());
auto replaceMultipleCasesRequest = request->replacemultiplecases();
std::map<int, QString> caseIdFileMap;
for (auto caseGridFilePair : replaceMultipleCasesRequest.casepairs())
{
QString parameterName = QString::fromStdString(parameter->name());
auto pdmValueFieldHandle = dynamic_cast<caf::PdmValueField*>(pdmObjectHandle->findField(parameterName));
if (pdmValueFieldHandle)
caseIdFileMap.insert(std::make_pair(caseGridFilePair.caseid(),
QString::fromStdString(caseGridFilePair.newgridfile())));
}
multiCaseReplaceCommand->setCaseReplacePairs(caseIdFileMap);
}
else
{
auto subMessageDescriptor = grpcOneOfMessage->message_type();
int numParameters = subMessageDescriptor->field_count();
for (int i = 0; i < numParameters; ++i)
{
auto parameter = subMessageDescriptor->field(i);
if (parameter)
{
assignPdmFieldValue(pdmValueFieldHandle, params, parameter);
QString parameterName = QString::fromStdString(parameter->name());
auto pdmValueFieldHandle = dynamic_cast<caf::PdmValueField*>(pdmObjectHandle->findField(parameterName));
if (pdmValueFieldHandle)
{
assignPdmFieldValue(pdmValueFieldHandle, params, parameter);
}
}
}
}