From af054fb95ef62e88865de35cf9d792388943c823 Mon Sep 17 00:00:00 2001 From: Gaute Lindkvist Date: Fri, 24 May 2019 12:19:44 +0200 Subject: [PATCH] #4428 Implement support for repeated fields as parameters and add exportWellPaths to client library --- .../GrpcInterface/RiaGrpcCommandService.cpp | 118 ++++++++++++------ .../GrpcInterface/RiaGrpcCommandService.h | 6 + Python/api/ResInsight.py | 23 ++-- Python/examples/CommandExample.py | 3 +- 4 files changed, 106 insertions(+), 44 deletions(-) diff --git a/ApplicationCode/GrpcInterface/RiaGrpcCommandService.cpp b/ApplicationCode/GrpcInterface/RiaGrpcCommandService.cpp index 09d2b5851a..683187c066 100644 --- a/ApplicationCode/GrpcInterface/RiaGrpcCommandService.cpp +++ b/ApplicationCode/GrpcInterface/RiaGrpcCommandService.cpp @@ -25,7 +25,9 @@ #include "cafAssert.h" #include "cafPdmDefaultObjectFactory.h" +#include "cafPdmDataValueField.h" #include "cafPdmValueField.h" +#include using namespace rips; using namespace google::protobuf; @@ -101,6 +103,29 @@ std::vector RiaGrpcCommandService::createCallbacks() return {new RiaGrpcCallback(this, &Self::Execute, &Self::RequestExecute)}; } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +template +caf::PdmField* RiaGrpcCommandService::dataValueField(caf::PdmValueField* valueField) +{ + caf::PdmField* dataValField = dynamic_cast*>(valueField); + CAF_ASSERT(dataValField); + return dataValField; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +template +const caf::PdmField* RiaGrpcCommandService::constDataValueField(const caf::PdmValueField* valueField) +{ + const caf::PdmField* dataValField = dynamic_cast*>(valueField); + CAF_ASSERT(dataValField); + return dataValField; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -109,56 +134,77 @@ void RiaGrpcCommandService::assignPdmFieldValue(caf::PdmValueField* pdmValueF const FieldDescriptor* paramDescriptor) { FieldDescriptor::Type fieldDataType = paramDescriptor->type(); - QVariant qValue; + const Reflection* reflection = params.GetReflection(); + switch (fieldDataType) { case FieldDescriptor::TYPE_BOOL: { - auto value = params.GetReflection()->GetBool(params, paramDescriptor); - qValue = QVariant(value); + auto value = reflection->GetBool(params, paramDescriptor); + auto dataField = dataValueField(pdmValueField); + dataField->setValue(value); break; } case FieldDescriptor::TYPE_INT32: { - int value = params.GetReflection()->GetInt32(params, paramDescriptor); - qValue = QVariant(value); + if (paramDescriptor->is_repeated()) + { + RepeatedFieldRef repeatedField = + reflection->GetRepeatedFieldRef(params, paramDescriptor); + auto dataField = dataValueField>(pdmValueField); + dataField->setValue(std::vector(repeatedField.begin(), repeatedField.end())); + } + else + { + int value = reflection->GetInt32(params, paramDescriptor); + auto dataField = dataValueField(pdmValueField); + dataField->setValue(value); + } break; } case FieldDescriptor::TYPE_UINT32: { - uint value = params.GetReflection()->GetUInt32(params, paramDescriptor); - qValue = QVariant(value); - break; - } - case FieldDescriptor::TYPE_INT64: { - int64_t value = params.GetReflection()->GetInt64(params, paramDescriptor); - qValue = QVariant((qlonglong)value); - break; - } - case FieldDescriptor::TYPE_UINT64: { - uint64_t value = params.GetReflection()->GetUInt64(params, paramDescriptor); - qValue = QVariant((qulonglong)value); + uint value = reflection->GetUInt32(params, paramDescriptor); + auto dataField = dataValueField(pdmValueField); + dataField->setValue(value); break; } case FieldDescriptor::TYPE_STRING: { - auto value = params.GetReflection()->GetString(params, paramDescriptor); - qValue = QVariant(QString::fromStdString(value)); + if (paramDescriptor->is_repeated()) + { + RepeatedFieldRef repeatedField = + reflection->GetRepeatedFieldRef(params, paramDescriptor); + std::vector stringVector; + for (const std::string& string : repeatedField) + { + stringVector.push_back(QString::fromStdString(string)); + } + auto dataField = dataValueField>(pdmValueField); + dataField->setValue(stringVector); + } + else + { + auto value = QString::fromStdString(reflection->GetString(params, paramDescriptor)); + auto dataField = dataValueField(pdmValueField); + dataField->setValue(value); + } break; } case FieldDescriptor::TYPE_FLOAT: { - auto value = params.GetReflection()->GetFloat(params, paramDescriptor); - qValue = QVariant(value); + auto value = reflection->GetFloat(params, paramDescriptor); + auto dataField = dataValueField(pdmValueField); + dataField->setValue(value); break; } case FieldDescriptor::TYPE_DOUBLE: { - auto value = params.GetReflection()->GetDouble(params, paramDescriptor); - qValue = QVariant(value); + auto value = reflection->GetDouble(params, paramDescriptor); + auto dataField = dataValueField(pdmValueField); + dataField->setValue(value); break; } case FieldDescriptor::TYPE_ENUM: { - auto value = params.GetReflection()->GetEnumValue(params, paramDescriptor); - qValue = QVariant(value); + auto value = reflection->GetEnumValue(params, paramDescriptor); + pdmValueField->setFromQVariant(QVariant(value)); break; } } - pdmValueField->setFromQVariant(qValue); } //-------------------------------------------------------------------------------------------------- @@ -170,42 +216,44 @@ void RiaGrpcCommandService::assignGrpcFieldValue(google::protobuf::Message* { FieldDescriptor::Type fieldDataType = fieldDescriptor->type(); QVariant qValue = pdmValueField->toQVariant(); + + auto reflection = reply->GetReflection(); switch (fieldDataType) { case FieldDescriptor::TYPE_BOOL: { - reply->GetReflection()->SetBool(reply, fieldDescriptor, qValue.toBool()); + reflection->SetBool(reply, fieldDescriptor, qValue.toBool()); break; } case FieldDescriptor::TYPE_INT32: { - reply->GetReflection()->SetInt32(reply, fieldDescriptor, qValue.toInt()); + reflection->SetInt32(reply, fieldDescriptor, qValue.toInt()); break; } case FieldDescriptor::TYPE_UINT32: { - reply->GetReflection()->SetUInt32(reply, fieldDescriptor, qValue.toUInt()); + reflection->SetUInt32(reply, fieldDescriptor, qValue.toUInt()); break; } case FieldDescriptor::TYPE_INT64: { - reply->GetReflection()->SetInt64(reply, fieldDescriptor, qValue.toLongLong()); + reflection->SetInt64(reply, fieldDescriptor, qValue.toLongLong()); break; } case FieldDescriptor::TYPE_UINT64: { - reply->GetReflection()->SetUInt64(reply, fieldDescriptor, qValue.toULongLong()); + reflection->SetUInt64(reply, fieldDescriptor, qValue.toULongLong()); break; } case FieldDescriptor::TYPE_STRING: { - reply->GetReflection()->SetString(reply, fieldDescriptor, qValue.toString().toStdString()); + reflection->SetString(reply, fieldDescriptor, qValue.toString().toStdString()); break; } case FieldDescriptor::TYPE_FLOAT: { - reply->GetReflection()->SetFloat(reply, fieldDescriptor, qValue.toFloat()); + reflection->SetFloat(reply, fieldDescriptor, qValue.toFloat()); break; } case FieldDescriptor::TYPE_DOUBLE: { - reply->GetReflection()->SetDouble(reply, fieldDescriptor, qValue.toDouble()); + reflection->SetDouble(reply, fieldDescriptor, qValue.toDouble()); break; } case FieldDescriptor::TYPE_ENUM: { - reply->GetReflection()->SetEnumValue(reply, fieldDescriptor, qValue.toInt()); + reflection->SetEnumValue(reply, fieldDescriptor, qValue.toInt()); break; } } diff --git a/ApplicationCode/GrpcInterface/RiaGrpcCommandService.h b/ApplicationCode/GrpcInterface/RiaGrpcCommandService.h index 81914b2d1d..d763f5b751 100644 --- a/ApplicationCode/GrpcInterface/RiaGrpcCommandService.h +++ b/ApplicationCode/GrpcInterface/RiaGrpcCommandService.h @@ -28,6 +28,8 @@ namespace caf { class PdmValueField; class PdmObject; +template +class PdmField; } namespace google @@ -48,6 +50,10 @@ public: std::vector createCallbacks() override; private: + template + static caf::PdmField* dataValueField(caf::PdmValueField* valueField); + template + static const caf::PdmField* constDataValueField(const caf::PdmValueField* valueField); void assignPdmFieldValue(caf::PdmValueField* pdmValueField, const google::protobuf::Message& params, const google::protobuf::FieldDescriptor* paramDescriptor); diff --git a/Python/api/ResInsight.py b/Python/api/ResInsight.py index 02d401dc9e..5ae13daec2 100644 --- a/Python/api/ResInsight.py +++ b/Python/api/ResInsight.py @@ -12,8 +12,8 @@ sys.path.insert(1, os.path.join(sys.path[0], '../generated')) from Empty_pb2 import Empty import CaseInfo_pb2 import CaseInfo_pb2_grpc -import Commands_pb2 -import Commands_pb2_grpc +import Commands_pb2 as Cmd +import Commands_pb2_grpc as CmdRpc import GridInfo_pb2 import GridInfo_pb2_grpc import ProjectInfo_pb2 @@ -40,7 +40,7 @@ class ResInfo: class CommandExecutor: def __init__(self, channel): - self.commands = Commands_pb2_grpc.CommandsStub(channel) + self.commands = CmdRpc.CommandsStub(channel) def execute(self, commandParams): try: @@ -52,21 +52,28 @@ class CommandExecutor: print("Other error") def setTimeStep(self, caseId, timeStep): - return self.execute(Commands_pb2.CommandParams(setTimeStep=Commands_pb2.SetTimeStepParams(caseId=caseId, timeStep=timeStep))) + return self.execute(Cmd.CommandParams(setTimeStep=Cmd.SetTimeStepParams(caseId=caseId, timeStep=timeStep))) def setMainWindowSize(self, width, height): - return self.execute(Commands_pb2.CommandParams(setMainWindowSize=Commands_pb2.SetMainWindowSizeParams(width=width, height=height))) + return self.execute(Cmd.CommandParams(setMainWindowSize=Cmd.SetMainWindowSizeParams(width=width, height=height))) def openProject(self, path): - return self.execute(Commands_pb2.CommandParams(openProject=Commands_pb2.FilePathRequest(path=path))) + return self.execute(Cmd.CommandParams(openProject=Cmd.FilePathRequest(path=path))) def loadCase(self, path): - commandReply = self.execute(Commands_pb2.CommandParams(loadCase=Commands_pb2.FilePathRequest(path=path))) + commandReply = self.execute(Cmd.CommandParams(loadCase=Cmd.FilePathRequest(path=path))) assert commandReply.HasField("loadCaseResult") return commandReply.loadCaseResult.id def closeProject(self): - return self.execute(Commands_pb2.CommandParams(closeProject=Empty())) + return self.execute(Cmd.CommandParams(closeProject=Empty())) + + def exportWellPaths(self, wellPaths=[], mdStepSize=5.0): + if isinstance(wellPaths, str): + wellPathArray = [str] + elif isinstance(wellPaths, list): + wellPathArray = wellPaths + return self.execute(Cmd.CommandParams(exportWellPaths=Cmd.ExportWellPathRequest(wellPathNames=wellPathArray, mdStepSize=mdStepSize))) class GridInfo: def __init__(self, channel): diff --git a/Python/examples/CommandExample.py b/Python/examples/CommandExample.py index cca0159775..f34fdd8a9d 100644 --- a/Python/examples/CommandExample.py +++ b/Python/examples/CommandExample.py @@ -8,4 +8,5 @@ resInsight = ResInsight.Instance.find() # Run a couple of commands resInsight.commands.setTimeStep(caseId=0, timeStep=3) -resInsight.commands.setMainWindowSize(width=800, height=500) \ No newline at end of file +resInsight.commands.setMainWindowSize(width=800, height=500) +resInsight.commands.exportWellPaths() \ No newline at end of file