#4429 Implement return values from the gRPC Command execution

This commit is contained in:
Gaute Lindkvist 2019-05-23 16:46:50 +02:00
parent 650af20e06
commit cc031e7895
10 changed files with 185 additions and 43 deletions

View File

@ -69,7 +69,7 @@
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaImportEclipseCaseTools::openEclipseCasesFromFile(const QStringList& fileNames, QStringList* openedFiles, bool noDialog)
bool RiaImportEclipseCaseTools::openEclipseCasesFromFile(const QStringList& fileNames, FileCaseIdMap* openedFilesOut, bool noDialog)
{
RiaApplication* app = RiaApplication::instance();
RimProject* project = app->project();
@ -80,12 +80,15 @@ bool RiaImportEclipseCaseTools::openEclipseCasesFromFile(const QStringList& file
selector.determineFilesToImportFromGridFiles(fileNames);
std::vector<RifSummaryCaseFileResultInfo> summaryFileInfos = selector.summaryFileInfos();
FileCaseIdMap openedFiles;
// Import eclipse case files
for (const QString& gridCaseFile : selector.gridCaseFiles())
{
if (RiaImportEclipseCaseTools::openEclipseCaseFromFile(gridCaseFile))
int caseId = RiaImportEclipseCaseTools::openEclipseCaseFromFile(gridCaseFile);
if (caseId >= 0)
{
if(openedFiles) openedFiles->push_back(gridCaseFile);
openedFiles.insert(std::make_pair(gridCaseFile, caseId));
}
}
@ -178,13 +181,18 @@ bool RiaImportEclipseCaseTools::openEclipseCasesFromFile(const QStringList& file
RiuPlotMainWindowTools::refreshToolbars();
return true;
if (openedFilesOut)
{
*openedFilesOut = openedFiles;
}
return !openedFiles.empty();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaImportEclipseCaseTools::openEclipseCaseFromFile(const QString& fileName)
int RiaImportEclipseCaseTools::openEclipseCaseFromFile(const QString& fileName)
{
if (!caf::Utils::fileExists(fileName)) return false;
@ -198,7 +206,7 @@ bool RiaImportEclipseCaseTools::openEclipseCaseShowTimeStepFilter(const QString&
{
if (!caf::Utils::fileExists(fileName)) return false;
return RiaImportEclipseCaseTools::openEclipseCaseShowTimeStepFilterImpl(fileName, true);
return RiaImportEclipseCaseTools::openEclipseCaseShowTimeStepFilterImpl(fileName, true) >= 0;
}
//--------------------------------------------------------------------------------------------------
@ -259,7 +267,7 @@ bool RiaImportEclipseCaseTools::openMockModel(const QString& name)
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaImportEclipseCaseTools::openEclipseCaseShowTimeStepFilterImpl(const QString& fileName, bool showTimeStepFilter)
int RiaImportEclipseCaseTools::openEclipseCaseShowTimeStepFilterImpl(const QString& fileName, bool showTimeStepFilter)
{
QFileInfo gridFileName(fileName);
QString caseName = gridFileName.completeBaseName();
@ -274,7 +282,7 @@ bool RiaImportEclipseCaseTools::openEclipseCaseShowTimeStepFilterImpl(const QStr
if (analysisModels == nullptr)
{
delete rimResultReservoir;
return false;
return -1;
}
if (RiaGuiApplication::isRunning())
@ -290,7 +298,7 @@ bool RiaImportEclipseCaseTools::openEclipseCaseShowTimeStepFilterImpl(const QStr
delete rimResultReservoir;
return false;
return -1;
}
RimEclipseView* riv = rimResultReservoir->createAndAddReservoirView();
@ -306,7 +314,7 @@ bool RiaImportEclipseCaseTools::openEclipseCaseShowTimeStepFilterImpl(const QStr
RiuMainWindow::instance()->selectAsCurrentItem(riv->cellResult());
return true;
return rimResultReservoir->caseId();
}
//--------------------------------------------------------------------------------------------------

View File

@ -18,6 +18,8 @@
#pragma once
#include <map>
class QString;
class QStringList;
@ -27,7 +29,9 @@ class QStringList;
class RiaImportEclipseCaseTools
{
public:
static bool openEclipseCasesFromFile(const QStringList& fileNames, QStringList* openedFiles = nullptr, bool noDialog = false);
typedef std::map<QString, int> FileCaseIdMap;
static bool openEclipseCasesFromFile(const QStringList& fileNames, FileCaseIdMap* openedFilesOut = nullptr, bool noDialog = false);
static bool openEclipseCaseShowTimeStepFilter(const QString& fileName);
static bool openEclipseInputCaseFromFileNames(const QStringList& fileNames, QString* fileContainingGrid = nullptr);
@ -36,7 +40,7 @@ public:
static bool addEclipseCases(const QStringList& fileNames);
private:
static bool openEclipseCaseFromFile(const QString& fileName);
static bool openEclipseCaseShowTimeStepFilterImpl(const QString& fileName, bool showTimeStepFilter);
static int openEclipseCaseFromFile(const QString& fileName);
static int openEclipseCaseShowTimeStepFilterImpl(const QString& fileName, bool showTimeStepFilter);
};

View File

@ -24,6 +24,17 @@
#include <QStringList>
CAF_PDM_SOURCE_INIT(RicfLoadCaseResult, "loadCaseResult");
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfLoadCaseResult::RicfLoadCaseResult(int caseId)
{
CAF_PDM_InitObject("case_result", "", "", "");
CAF_PDM_InitField(&this->caseId, "id", caseId, "", "", "", "");
}
CAF_PDM_SOURCE_INIT(RicfLoadCase, "loadCase");
//--------------------------------------------------------------------------------------------------
@ -39,12 +50,17 @@ RicfLoadCase::RicfLoadCase()
//--------------------------------------------------------------------------------------------------
RicfCommandResponse RicfLoadCase::execute()
{
bool ok = RiaImportEclipseCaseTools::openEclipseCasesFromFile(QStringList({m_path()}), nullptr, true);
RiaImportEclipseCaseTools::FileCaseIdMap fileCaseIdMap;
bool ok = RiaImportEclipseCaseTools::openEclipseCasesFromFile(QStringList({m_path()}), &fileCaseIdMap, true);
if (!ok)
{
QString error = QString("loadCase: Unable to load case from %1").arg(m_path());
RiaLogging::error(error);
return RicfCommandResponse(RicfCommandResponse::COMMAND_ERROR, error);
}
return RicfCommandResponse();
CAF_ASSERT(fileCaseIdMap.size() == 1u);
RicfLoadCaseResult result;
RicfCommandResponse response;
response.setResult(new RicfLoadCaseResult(fileCaseIdMap.begin()->second));
return response;
}

View File

@ -22,6 +22,15 @@
#include "cafPdmField.h"
class RicfLoadCaseResult : public caf::PdmObject
{
CAF_PDM_HEADER_INIT;
public:
RicfLoadCaseResult(int caseId = -1);
public:
caf::PdmField<int> caseId;
};
//==================================================================================================
//
//

View File

@ -68,12 +68,12 @@ void RicImportEclipseCasesFeature::onActionTriggered(bool isChecked)
// Remember the path to next time
app->setLastUsedDialogDirectory("BINARY_GRID", QFileInfo(result.rootDir).absoluteFilePath());
QStringList newCaseFiles;
RiaImportEclipseCaseTools::FileCaseIdMap newCaseFiles;
RiaImportEclipseCaseTools::openEclipseCasesFromFile(result.files, &newCaseFiles);
for (const auto newCaseFile : newCaseFiles)
for (const auto newCaseFileAndId : newCaseFiles)
{
RiaApplication::instance()->addToRecentFiles(newCaseFile);
RiaApplication::instance()->addToRecentFiles(newCaseFileAndId.first);
}
}

View File

@ -184,12 +184,12 @@ void RicImportGeneralDataFeature::openFileDialog(ImportFileType fileTypes)
//--------------------------------------------------------------------------------------------------
bool RicImportGeneralDataFeature::openEclipseCaseFromFileNames(const QStringList& fileNames)
{
QStringList newCaseFiles;
RiaImportEclipseCaseTools::FileCaseIdMap newCaseFiles;
if (RiaImportEclipseCaseTools::openEclipseCasesFromFile(fileNames, &newCaseFiles))
{
for (const auto newCaseFile : newCaseFiles)
for (const auto newCaseFileAndId : newCaseFiles)
{
RiaApplication::instance()->addToRecentFiles(newCaseFile);
RiaApplication::instance()->addToRecentFiles(newCaseFileAndId.first);
}
return true;
}

View File

@ -1,12 +1,13 @@
syntax = "proto3";
import "CaseInfo.proto";
import "Empty.proto";
package rips;
service Commands
{
rpc Execute(CommandParams) returns(Empty) {}
rpc Execute(CommandParams) returns(CommandReply) {}
}
message SetTimeStepParams
@ -58,3 +59,12 @@ message CommandParams
}
}
message CommandReply
{
oneof result
{
Empty emptyResult = 1;
Case loadCaseResult = 2;
}
}

View File

@ -39,7 +39,7 @@ using namespace google::protobuf;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
grpc::Status RiaGrpcCommandService::Execute(grpc::ServerContext* context, const CommandParams* request, Empty* reply)
grpc::Status RiaGrpcCommandService::Execute(grpc::ServerContext* context, const CommandParams* request, CommandReply* reply)
{
auto requestDescriptor = request->GetDescriptor();
RiaLogging::info(QString::fromStdString(requestDescriptor->name()));
@ -69,19 +69,22 @@ grpc::Status RiaGrpcCommandService::Execute(grpc::ServerContext* context, const
if (pdmValueFieldHandle)
{
RiaLogging::info(QString("Found Matching Parameter: %1").arg(parameterName));
assignFieldValue(pdmValueFieldHandle, params, parameter);
assignPdmFieldValue(pdmValueFieldHandle, params, parameter);
}
}
}
RicfCommandResponse response = commandHandle->execute();
if (response.status() == RicfCommandResponse::COMMAND_ERROR)
{
return grpc::Status(grpc::FAILED_PRECONDITION, response.message().toStdString());
return grpc::Status(grpc::FAILED_PRECONDITION, response.message().toStdString());
}
else if (response.status() == RicfCommandResponse::COMMAND_WARNING)
{
context->AddInitialMetadata("warning", response.message().toStdString());
}
assignResultToReply(response.result(), reply);
return Status::OK;
}
}
@ -95,15 +98,15 @@ std::vector<RiaAbstractGrpcCallback*> RiaGrpcCommandService::createCallbacks()
{
typedef RiaGrpcCommandService Self;
return {new RiaGrpcCallback<Self, CommandParams, Empty>(this, &Self::Execute, &Self::RequestExecute)};
return {new RiaGrpcCallback<Self, CommandParams, CommandReply>(this, &Self::Execute, &Self::RequestExecute)};
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaGrpcCommandService::assignFieldValue(caf::PdmValueField* pdmValueField,
const Message& params,
const FieldDescriptor* paramDescriptor)
void RiaGrpcCommandService::assignPdmFieldValue(caf::PdmValueField* pdmValueField,
const Message& params,
const FieldDescriptor* paramDescriptor)
{
FieldDescriptor::Type fieldDataType = paramDescriptor->type();
QVariant qValue;
@ -116,7 +119,7 @@ void RiaGrpcCommandService::assignFieldValue(caf::PdmValueField* pdmValueFiel
}
case FieldDescriptor::TYPE_INT32: {
int value = params.GetReflection()->GetInt32(params, paramDescriptor);
qValue = QVariant(value);
qValue = QVariant(value);
break;
}
case FieldDescriptor::TYPE_UINT32: {
@ -126,12 +129,12 @@ void RiaGrpcCommandService::assignFieldValue(caf::PdmValueField* pdmValueFiel
}
case FieldDescriptor::TYPE_INT64: {
int64_t value = params.GetReflection()->GetInt64(params, paramDescriptor);
qValue = QVariant((qlonglong) value);
qValue = QVariant((qlonglong)value);
break;
}
case FieldDescriptor::TYPE_UINT64: {
uint64_t value = params.GetReflection()->GetUInt64(params, paramDescriptor);
qValue = QVariant((qulonglong) value);
qValue = QVariant((qulonglong)value);
break;
}
case FieldDescriptor::TYPE_STRING: {
@ -158,5 +161,95 @@ void RiaGrpcCommandService::assignFieldValue(caf::PdmValueField* pdmValueFiel
pdmValueField->setFromQVariant(qValue);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaGrpcCommandService::assignGrpcFieldValue(google::protobuf::Message* reply,
const google::protobuf::FieldDescriptor* fieldDescriptor,
const caf::PdmValueField* pdmValueField)
{
FieldDescriptor::Type fieldDataType = fieldDescriptor->type();
QVariant qValue = pdmValueField->toQVariant();
switch (fieldDataType)
{
case FieldDescriptor::TYPE_BOOL: {
reply->GetReflection()->SetBool(reply, fieldDescriptor, qValue.toBool());
break;
}
case FieldDescriptor::TYPE_INT32: {
reply->GetReflection()->SetInt32(reply, fieldDescriptor, qValue.toInt());
break;
}
case FieldDescriptor::TYPE_UINT32: {
reply->GetReflection()->SetUInt32(reply, fieldDescriptor, qValue.toUInt());
break;
}
case FieldDescriptor::TYPE_INT64: {
reply->GetReflection()->SetInt64(reply, fieldDescriptor, qValue.toLongLong());
break;
}
case FieldDescriptor::TYPE_UINT64: {
reply->GetReflection()->SetUInt64(reply, fieldDescriptor, qValue.toULongLong());
break;
}
case FieldDescriptor::TYPE_STRING: {
reply->GetReflection()->SetString(reply, fieldDescriptor, qValue.toString().toStdString());
break;
}
case FieldDescriptor::TYPE_FLOAT: {
reply->GetReflection()->SetFloat(reply, fieldDescriptor, qValue.toFloat());
break;
}
case FieldDescriptor::TYPE_DOUBLE: {
reply->GetReflection()->SetDouble(reply, fieldDescriptor, qValue.toDouble());
break;
}
case FieldDescriptor::TYPE_ENUM: {
reply->GetReflection()->SetEnumValue(reply, fieldDescriptor, qValue.toInt());
break;
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaGrpcCommandService::assignResultToReply(const caf::PdmObject* result, CommandReply* reply)
{
if (!result)
{
reply->set_allocated_emptyresult(new Empty);
return;
}
QString resultType = result->classKeyword();
auto replyDescriptor = reply->GetDescriptor();
auto oneofDescriptor = replyDescriptor->FindOneofByName("result");
const FieldDescriptor* matchingOneOf = nullptr;
for (int fieldIndex = 0; fieldIndex < oneofDescriptor->field_count(); ++fieldIndex)
{
auto fieldDescriptor = oneofDescriptor->field(fieldIndex);
if (fieldDescriptor->name() == resultType.toStdString())
{
matchingOneOf = fieldDescriptor;
break;
}
}
CAF_ASSERT(matchingOneOf);
Message* message = reply->GetReflection()->MutableMessage(reply, matchingOneOf);
CAF_ASSERT(message);
auto resultDescriptor = message->GetDescriptor();
for (int fieldIndex = 0; fieldIndex < resultDescriptor->field_count(); ++fieldIndex)
{
auto fieldDescriptor = resultDescriptor->field(fieldIndex);
const auto pdmField =
dynamic_cast<const caf::PdmValueField*>(result->findField(QString::fromStdString(fieldDescriptor->name())));
assignGrpcFieldValue(message, fieldDescriptor, pdmField);
}
}
static bool RiaGrpcCommandService_init =
RiaGrpcServiceFactory::instance()->registerCreator<RiaGrpcCommandService>(typeid(RiaGrpcCommandService).hash_code());

View File

@ -24,14 +24,10 @@
#include <grpcpp/grpcpp.h>
#include <vector>
namespace rips
{
class Empty;
}
namespace caf
{
class PdmValueField;
class PdmObject;
}
namespace google
@ -48,11 +44,15 @@ class RiaAbstractGrpcCallback;
class RiaGrpcCommandService : public rips::Commands::AsyncService, public RiaGrpcServiceInterface
{
public:
grpc::Status Execute(grpc::ServerContext* context, const rips::CommandParams* request, rips::Empty* reply) override;
grpc::Status Execute(grpc::ServerContext* context, const rips::CommandParams* request, rips::CommandReply* reply) override;
std::vector<RiaAbstractGrpcCallback*> createCallbacks() override;
private:
void assignFieldValue(caf::PdmValueField* pdmValueField,
const google::protobuf::Message& params,
const google::protobuf::FieldDescriptor* paramDescriptor);
void assignPdmFieldValue(caf::PdmValueField* pdmValueField,
const google::protobuf::Message& params,
const google::protobuf::FieldDescriptor* paramDescriptor);
void assignGrpcFieldValue(google::protobuf::Message* reply,
const google::protobuf::FieldDescriptor* fieldDescriptor,
const caf::PdmValueField* pdmValueField);
void assignResultToReply(const caf::PdmObject* result, rips::CommandReply* reply);
};

View File

@ -44,7 +44,7 @@ class CommandExecutor:
def execute(self, commandParams):
try:
self.commands.Execute(commandParams)
return self.commands.Execute(commandParams)
except grpc.RpcError as e:
if e.code() == grpc.StatusCode.NOT_FOUND:
print("Command not found")
@ -61,7 +61,9 @@ class CommandExecutor:
return self.execute(Commands_pb2.CommandParams(openProject=Commands_pb2.FilePathRequest(path=path)))
def loadCase(self, path):
return self.execute(Commands_pb2.CommandParams(loadCase=Commands_pb2.FilePathRequest(path=path)))
commandReply = self.execute(Commands_pb2.CommandParams(loadCase=Commands_pb2.FilePathRequest(path=path)))
assert commandReply.HasField("loadCaseResult")
return commandReply.loadCaseResult.id
def closeProject(self):
return self.execute(Commands_pb2.CommandParams(closeProject=Empty()))