diff --git a/GrpcInterface/GrpcProtos/PdmObject.proto b/GrpcInterface/GrpcProtos/PdmObject.proto index cff7e5db9b..d55aa11499 100644 --- a/GrpcInterface/GrpcProtos/PdmObject.proto +++ b/GrpcInterface/GrpcProtos/PdmObject.proto @@ -14,6 +14,7 @@ service PdmObjectService rpc CallPdmObjectGetter(PdmObjectGetterRequest) returns (stream PdmObjectGetterReply) {} rpc CallPdmObjectSetter(stream PdmObjectSetterChunk) returns (ClientToServerStreamReply) {} rpc CallPdmObjectMethod(PdmObjectMethodRequest) returns (PdmObject) {} + rpc DeleteExistingPdmObject(PdmObject) returns (Empty) {} } message PdmDescendantObjectRequest diff --git a/GrpcInterface/Python/rips/pdmobject.py b/GrpcInterface/Python/rips/pdmobject.py index 877a2ccb4b..73b2724b07 100644 --- a/GrpcInterface/Python/rips/pdmobject.py +++ b/GrpcInterface/Python/rips/pdmobject.py @@ -511,3 +511,13 @@ class PdmObjectBase: raise Exception( "Object is not connected to GRPC service so cannot update ResInsight" ) + + def delete(self) -> None: + """Delete object in ResInsight""" + self.__copy_to_pb2() + if self._pdm_object_stub is not None: + self._pdm_object_stub.DeleteExistingPdmObject(self._pb2_object) + else: + raise Exception( + "Object is not connected to GRPC service so cannot update ResInsight" + ) diff --git a/GrpcInterface/Python/rips/tests/test_object_lifetime.py b/GrpcInterface/Python/rips/tests/test_object_lifetime.py new file mode 100644 index 0000000000..1175a002b8 --- /dev/null +++ b/GrpcInterface/Python/rips/tests/test_object_lifetime.py @@ -0,0 +1,27 @@ +import sys +import os + +sys.path.insert(1, os.path.join(sys.path[0], "../../")) +import rips + + +def test_well_path(rips_instance, initialize_test): + well_path_coll = rips_instance.project.descendants(rips.WellPathCollection)[0] + assert len(well_path_coll.well_paths()) is 0 + + well_path = well_path_coll.add_new_object(rips.ModeledWellPath) + well_path2 = well_path_coll.add_new_object(rips.ModeledWellPath) + assert len(well_path_coll.well_paths()) is 2 + + well_path.delete() + assert len(well_path_coll.well_paths()) is 1 + + try: + # Delete again should throw exception + well_path.delete() + assert False + except Exception: + assert True + + well_path2.delete() + assert len(well_path_coll.well_paths()) is 0 diff --git a/GrpcInterface/RiaGrpcCallbacks.h b/GrpcInterface/RiaGrpcCallbacks.h index faebbb56cf..f778d7e785 100644 --- a/GrpcInterface/RiaGrpcCallbacks.h +++ b/GrpcInterface/RiaGrpcCallbacks.h @@ -33,8 +33,6 @@ using grpc::ServerCompletionQueue; using grpc::ServerContext; using grpc::Status; -class RiaGrpcServiceInterface; - //================================================================================================== // // Base class for all gRPC-callbacks diff --git a/GrpcInterface/RiaGrpcCaseService.cpp b/GrpcInterface/RiaGrpcCaseService.cpp index 05b8c14999..b671c91ce2 100644 --- a/GrpcInterface/RiaGrpcCaseService.cpp +++ b/GrpcInterface/RiaGrpcCaseService.cpp @@ -54,7 +54,7 @@ grpc::Status RiaActiveCellInfoStateHandler::init( const rips::CellInfoRequest* r m_request = request; m_porosityModel = RiaDefines::PorosityModelType( m_request->porosity_model() ); - RimCase* rimCase = RiaGrpcServiceInterface::findCase( m_request->case_request().id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( m_request->case_request().id() ); m_eclipseCase = dynamic_cast( rimCase ); if ( !m_eclipseCase ) @@ -191,7 +191,7 @@ const std::vector& RiaActiveCellInfoStateHandler::reservoirCells() cons //-------------------------------------------------------------------------------------------------- grpc::Status RiaActiveCellInfoStateHandler::assignReply( rips::CellInfoArray* reply ) { - const size_t packageSize = RiaGrpcServiceInterface::numberOfDataUnitsInPackage( sizeof( rips::CellInfo ) ); + const size_t packageSize = RiaGrpcHelper::numberOfDataUnitsInPackage( sizeof( rips::CellInfo ) ); size_t indexInPackage = 0u; reply->mutable_data()->Reserve( (int)packageSize ); @@ -259,7 +259,7 @@ void RiaActiveCellInfoStateHandler::assignCellCenter( rips::Vec3d* //-------------------------------------------------------------------------------------------------- grpc::Status RiaActiveCellInfoStateHandler::assignCellCentersReply( rips::CellCenters* reply ) { - const size_t packageSize = RiaGrpcServiceInterface::numberOfDataUnitsInPackage( sizeof( rips::Vec3d ) ); + const size_t packageSize = RiaGrpcHelper::numberOfDataUnitsInPackage( sizeof( rips::Vec3d ) ); size_t indexInPackage = 0u; reply->mutable_centers()->Reserve( (int)packageSize ); for ( ; indexInPackage < packageSize && m_currentCellIdx < m_activeCellInfo->reservoirCellCount(); ++indexInPackage ) @@ -332,7 +332,7 @@ void RiaActiveCellInfoStateHandler::assignCellCorners( rips::CellCorners* //-------------------------------------------------------------------------------------------------- Status RiaActiveCellInfoStateHandler::assignCellCornersReply( rips::CellCornersArray* reply ) { - const size_t packageSize = RiaGrpcServiceInterface::numberOfDataUnitsInPackage( sizeof( rips::CellCorners ) ); + const size_t packageSize = RiaGrpcHelper::numberOfDataUnitsInPackage( sizeof( rips::CellCorners ) ); size_t indexInPackage = 0u; reply->mutable_cells()->Reserve( (int)packageSize ); for ( ; indexInPackage < packageSize && m_currentCellIdx < m_activeCellInfo->reservoirCellCount(); ++indexInPackage ) @@ -363,7 +363,7 @@ grpc::Status RiaGrpcCaseService::GetGridCount( grpc::ServerContext* context, const rips::CaseRequest* request, rips::GridCount* reply ) { - RimCase* rimCase = findCase( request->id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( request->id() ); RimEclipseCase* eclipseCase = dynamic_cast( rimCase ); if ( eclipseCase ) @@ -382,7 +382,7 @@ grpc::Status RiaGrpcCaseService::GetCellCount( grpc::ServerContext* cont const rips::CellInfoRequest* request, rips::CellCount* reply ) { - RimCase* rimCase = findCase( request->case_request().id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( request->case_request().id() ); RimEclipseCase* eclipseCase = dynamic_cast( rimCase ); if ( eclipseCase ) @@ -403,7 +403,7 @@ grpc::Status RiaGrpcCaseService::GetTimeSteps( grpc::ServerContext* context, const rips::CaseRequest* request, rips::TimeStepDates* reply ) { - RimCase* rimCase = findCase( request->id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( request->id() ); if ( rimCase ) { @@ -430,7 +430,7 @@ grpc::Status RiaGrpcCaseService::GetDaysSinceStart( grpc::ServerContext* con const rips::CaseRequest* request, rips::DaysSinceStart* reply ) { - RimCase* rimCase = findCase( request->id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( request->id() ); RimEclipseCase* eclipseCase = dynamic_cast( rimCase ); if ( eclipseCase ) @@ -466,7 +466,7 @@ grpc::Status RiaGrpcCaseService::GetDaysSinceStart( grpc::ServerContext* con grpc::Status RiaGrpcCaseService::GetCaseInfo( grpc::ServerContext* context, const rips::CaseRequest* request, rips::CaseInfo* reply ) { - RimCase* rimCase = findCase( request->id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( request->id() ); if ( rimCase ) { qint64 caseId = rimCase->caseId(); @@ -490,7 +490,7 @@ grpc::Status RiaGrpcCaseService::GetPdmObject( grpc::ServerContext* context, const rips::CaseRequest* request, rips::PdmObject* reply ) { - RimCase* rimCase = findCase( request->id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( request->id() ); if ( rimCase ) { copyPdmObjectFromCafToRips( rimCase, reply ); @@ -549,7 +549,7 @@ Status RiaSelectedCellsStateHandler::init( const rips::CaseRequest* request ) CAF_ASSERT( request ); m_request = request; - RimCase* rimCase = RiaGrpcServiceInterface::findCase( m_request->id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( m_request->id() ); m_eclipseCase = dynamic_cast( rimCase ); if ( !m_eclipseCase ) @@ -624,7 +624,7 @@ grpc::Status RiaSelectedCellsStateHandler::assignReply( rips::SelectedCells* rep } } - const size_t packageSize = RiaGrpcServiceInterface::numberOfDataUnitsInPackage( sizeof( rips::SelectedCell ) ); + const size_t packageSize = RiaGrpcHelper::numberOfDataUnitsInPackage( sizeof( rips::SelectedCell ) ); size_t indexInPackage = 0u; reply->mutable_cells()->Reserve( (int)packageSize ); for ( ; indexInPackage < packageSize && m_currentItem < eclipseItems.size(); ++indexInPackage ) @@ -667,7 +667,7 @@ grpc::Status RiaGrpcCaseService::GetReservoirBoundingBox( grpc::ServerContext* const rips::CaseRequest* request, rips::BoundingBox* reply ) { - RimCase* rimCase = findCase( request->id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( request->id() ); if ( rimCase ) { cvf::BoundingBox boundingBox = rimCase->reservoirBoundingBox(); @@ -689,7 +689,7 @@ grpc::Status RiaGrpcCaseService::GetCoarseningInfoArray( grpc::ServerContext* const rips::CaseRequest* request, rips::CoarseningInfoArray* reply ) { - RimEclipseCase* rimCase = dynamic_cast( findCase( request->id() ) ); + RimEclipseCase* rimCase = dynamic_cast( RiaGrpcHelper::findCase( request->id() ) ); if ( rimCase && rimCase->eclipseCaseData() && rimCase->eclipseCaseData()->mainGrid() ) { for ( size_t gridIdx = 0; gridIdx < rimCase->eclipseCaseData()->gridCount(); gridIdx++ ) diff --git a/GrpcInterface/RiaGrpcGridService.cpp b/GrpcInterface/RiaGrpcGridService.cpp index 9855ea9ece..8601428591 100644 --- a/GrpcInterface/RiaGrpcGridService.cpp +++ b/GrpcInterface/RiaGrpcGridService.cpp @@ -47,7 +47,7 @@ grpc::Status RiaCellCenterStateHandler::init( const rips::GridRequest* request ) CAF_ASSERT( request ); m_request = request; - RimCase* rimCase = RiaGrpcServiceInterface::findCase( m_request->case_request().id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( m_request->case_request().id() ); m_eclipseCase = dynamic_cast( rimCase ); if ( !m_eclipseCase ) @@ -71,7 +71,7 @@ grpc::Status RiaCellCenterStateHandler::init( const rips::GridRequest* request ) //-------------------------------------------------------------------------------------------------- grpc::Status RiaCellCenterStateHandler::assignReply( rips::CellCenters* reply ) { - const size_t packageSize = RiaGrpcServiceInterface::numberOfDataUnitsInPackage( sizeof( rips::Vec3d ) ); + const size_t packageSize = RiaGrpcHelper::numberOfDataUnitsInPackage( sizeof( rips::Vec3d ) ); size_t indexInPackage = 0u; reply->mutable_centers()->Reserve( (int)packageSize ); for ( ; indexInPackage < packageSize && m_currentCellIdx < m_grid->cellCount(); ++indexInPackage ) @@ -98,7 +98,7 @@ grpc::Status RiaCellCenterStateHandler::assignReply( rips::CellCenters* reply ) //-------------------------------------------------------------------------------------------------- grpc::Status RiaCellCenterStateHandler::assignCornersReply( rips::CellCornersArray* reply ) { - const size_t packageSize = RiaGrpcServiceInterface::numberOfDataUnitsInPackage( sizeof( rips::CellCorners ) ); + const size_t packageSize = RiaGrpcHelper::numberOfDataUnitsInPackage( sizeof( rips::CellCorners ) ); size_t indexInPackage = 0u; reply->mutable_cells()->Reserve( (int)packageSize ); @@ -137,7 +137,7 @@ grpc::Status RiaGrpcGridService::GetDimensions( grpc::ServerContext* context const rips::GridRequest* request, rips::GridDimensions* reply ) { - RimCase* rimCase = findCase( request->case_request().id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( request->case_request().id() ); RimEclipseCase* eclipseCase = dynamic_cast( rimCase ); if ( eclipseCase ) diff --git a/GrpcInterface/RiaGrpcHelper.cpp b/GrpcInterface/RiaGrpcHelper.cpp index 8de881cd04..2775706663 100644 --- a/GrpcInterface/RiaGrpcHelper.cpp +++ b/GrpcInterface/RiaGrpcHelper.cpp @@ -15,8 +15,18 @@ // for more details. // ////////////////////////////////////////////////////////////////////////////////// + #include "RiaGrpcHelper.h" +#include "RiaApplication.h" +#include "RimCase.h" +#include "RimCommandRouter.h" +#include "RimProject.h" + +#include "PdmObject.pb.h" + +#include "cafPdmObjectScriptingCapabilityRegister.h" + //-------------------------------------------------------------------------------------------------- /// Convert internal ResInsight representation of cells with negative depth to positive depth. //-------------------------------------------------------------------------------------------------- @@ -35,3 +45,63 @@ void RiaGrpcHelper::setCornerValues( rips::Vec3d* out, const cvf::Vec3d& in ) out->set_y( in.y() ); out->set_z( in.z() ); } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +caf::PdmObject* RiaGrpcHelper::findCafObjectFromRipsObject( const rips::PdmObject& ripsObject ) +{ + QString scriptClassName = QString::fromStdString( ripsObject.class_keyword() ); + uint64_t address = ripsObject.address(); + return findCafObjectFromScriptNameAndAddress( scriptClassName, address ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +caf::PdmObject* RiaGrpcHelper::findCafObjectFromScriptNameAndAddress( const QString& scriptClassName, uint64_t address ) +{ + QString classKeyword = caf::PdmObjectScriptingCapabilityRegister::classKeywordFromScriptClassName( scriptClassName ); + + if ( classKeyword == RimCommandRouter::classKeywordStatic() ) return RiaApplication::instance()->commandRouter(); + + RimProject* project = RimProject::current(); + std::vector objectsOfCurrentClass; + + project->descendantsIncludingThisFromClassKeyword( classKeyword, objectsOfCurrentClass ); + + caf::PdmObject* matchingObject = nullptr; + for ( caf::PdmObject* testObject : objectsOfCurrentClass ) + { + if ( reinterpret_cast( testObject ) == address ) + { + matchingObject = testObject; + } + } + return matchingObject; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +size_t RiaGrpcHelper::numberOfDataUnitsInPackage( size_t dataUnitSize, size_t packageByteCount /*= 64 * 1024u */ ) +{ + size_t dataUnitCount = packageByteCount / dataUnitSize; + return dataUnitCount; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +RimCase* RiaGrpcHelper::findCase( int caseId ) +{ + std::vector cases = RimProject::current()->allGridCases(); + for ( RimCase* rimCase : cases ) + { + if ( caseId == rimCase->caseId() ) + { + return rimCase; + } + } + return nullptr; +} diff --git a/GrpcInterface/RiaGrpcHelper.h b/GrpcInterface/RiaGrpcHelper.h index 4f29389fef..b027876fd6 100644 --- a/GrpcInterface/RiaGrpcHelper.h +++ b/GrpcInterface/RiaGrpcHelper.h @@ -21,6 +21,20 @@ #include "cvfVector3.h" +#include + +class RimCase; + +namespace caf +{ +class PdmObject; +} + +namespace rips +{ +class PdmObject; +} + //================================================================================================== // // Various gRPC helper methods @@ -31,4 +45,10 @@ class RiaGrpcHelper public: static void convertVec3dToPositiveDepth( cvf::Vec3d* vec ); static void setCornerValues( rips::Vec3d* out, const cvf::Vec3d& in ); + + static caf::PdmObject* findCafObjectFromRipsObject( const rips::PdmObject& ripsObject ); + static caf::PdmObject* findCafObjectFromScriptNameAndAddress( const QString& scriptClassName, uint64_t address ); + + static size_t numberOfDataUnitsInPackage( size_t dataUnitSize, size_t packageByteCount = 64 * 1024u ); + static RimCase* findCase( int caseId ); }; diff --git a/GrpcInterface/RiaGrpcNNCPropertiesService.cpp b/GrpcInterface/RiaGrpcNNCPropertiesService.cpp index 7554d42052..4ffcf58c52 100644 --- a/GrpcInterface/RiaGrpcNNCPropertiesService.cpp +++ b/GrpcInterface/RiaGrpcNNCPropertiesService.cpp @@ -19,6 +19,7 @@ #include "RiaGrpcCallbacks.h" #include "RiaGrpcCaseService.h" +#include "RiaGrpcHelper.h" #include "RigCaseCellResultsData.h" #include "RigEclipseCaseData.h" @@ -55,7 +56,7 @@ grpc::Status RiaNNCConnectionsStateHandler::init( const rips::CaseRequest* reque CAF_ASSERT( request ); m_request = request; - RimCase* rimCase = RiaGrpcServiceInterface::findCase( m_request->id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( m_request->id() ); m_eclipseCase = dynamic_cast( rimCase ); if ( !( m_eclipseCase && m_eclipseCase->eclipseCaseData() && m_eclipseCase->eclipseCaseData()->mainGrid() ) ) @@ -93,7 +94,7 @@ grpc::Status RiaNNCConnectionsStateHandler::assignReply( rips::NNCConnections* r const RigConnectionContainer& connections = mainGrid->nncData()->allConnections(); size_t connectionCount = connections.size(); - const size_t packageSize = RiaGrpcServiceInterface::numberOfDataUnitsInPackage( sizeof( rips::NNCConnection ) ); + const size_t packageSize = RiaGrpcHelper::numberOfDataUnitsInPackage( sizeof( rips::NNCConnection ) ); size_t indexInPackage = 0u; reply->mutable_connections()->Reserve( (int)packageSize ); for ( ; indexInPackage < packageSize && m_currentIdx < connectionCount; ++indexInPackage ) @@ -147,7 +148,7 @@ grpc::Status RiaNNCValuesStateHandler::init( const rips::NNCValuesRequest* reque CAF_ASSERT( request ); m_request = request; - RimCase* rimCase = RiaGrpcServiceInterface::findCase( m_request->case_id() ); + RimCase* rimCase = RiaGrpcHelper::findCase( m_request->case_id() ); m_eclipseCase = dynamic_cast( rimCase ); if ( !( m_eclipseCase && m_eclipseCase->eclipseCaseData() && m_eclipseCase->eclipseCaseData()->mainGrid() ) ) @@ -204,7 +205,7 @@ grpc::Status RiaNNCValuesStateHandler::assignReply( rips::NNCValues* reply ) } size_t connectionCount = connections.size(); - const size_t packageSize = RiaGrpcServiceInterface::numberOfDataUnitsInPackage( sizeof( double ) ); + const size_t packageSize = RiaGrpcHelper::numberOfDataUnitsInPackage( sizeof( double ) ); size_t indexInPackage = 0u; reply->mutable_values()->Reserve( (int)packageSize ); for ( ; indexInPackage < packageSize && m_currentIdx < connectionCount; ++indexInPackage ) @@ -238,7 +239,7 @@ grpc::Status RiaGrpcNNCPropertiesService::GetAvailableNNCProperties( grpc::Serve const CaseRequest* request, AvailableNNCProperties* reply ) { - RimEclipseCase* eclipseCase = dynamic_cast( RiaGrpcServiceInterface::findCase( request->id() ) ); + RimEclipseCase* eclipseCase = dynamic_cast( RiaGrpcHelper::findCase( request->id() ) ); if ( eclipseCase && eclipseCase->eclipseCaseData() && eclipseCase->eclipseCaseData()->mainGrid() ) { RigNNCData* nncData = eclipseCase->eclipseCaseData()->mainGrid()->nncData(); @@ -332,7 +333,7 @@ std::vector* getOrCreateConnectionScalarResultByName( RigNNCData* nncDat grpc::Status RiaNNCInputValuesStateHandler::init( const NNCValuesInputRequest* request ) { int caseId = request->case_id(); - m_eclipseCase = dynamic_cast( RiaGrpcServiceInterface::findCase( caseId ) ); + m_eclipseCase = dynamic_cast( RiaGrpcHelper::findCase( caseId ) ); if ( m_eclipseCase && m_eclipseCase->eclipseCaseData() && m_eclipseCase->eclipseCaseData()->mainGrid() ) { @@ -341,7 +342,7 @@ grpc::Status RiaNNCInputValuesStateHandler::init( const NNCValuesInputRequest* r m_timeStep = request->time_step(); m_propertyName = QString::fromStdString( request->property_name() ); - RigNNCData* nncData = m_eclipseCase->eclipseCaseData()->mainGrid()->nncData(); + RigNNCData* nncData = m_eclipseCase->eclipseCaseData()->mainGrid()->nncData(); std::vector* resultsToAdd = getOrCreateConnectionScalarResultByName( nncData, m_propertyName, m_timeStep ); if ( !resultsToAdd ) { diff --git a/GrpcInterface/RiaGrpcPdmObjectService.cpp b/GrpcInterface/RiaGrpcPdmObjectService.cpp index 84d4478084..de2a0aff45 100644 --- a/GrpcInterface/RiaGrpcPdmObjectService.cpp +++ b/GrpcInterface/RiaGrpcPdmObjectService.cpp @@ -15,12 +15,13 @@ // for more details. // ////////////////////////////////////////////////////////////////////////////////// + #include "RiaGrpcPdmObjectService.h" #include "RiaApplication.h" #include "RiaGrpcCallbacks.h" +#include "RiaGrpcHelper.h" -#include "ProjectDataModelCommands/CommandRouter/RimCommandRouter.h" #include "Rim3dView.h" #include "RimEclipseResultDefinition.h" #include "RimProject.h" @@ -168,7 +169,7 @@ RiaPdmObjectMethodStateHandler::RiaPdmObjectMethodStateHandler( bool clientToSer Status RiaPdmObjectMethodStateHandler::init( const rips::PdmObjectGetterRequest* request ) { CAF_ASSERT( !m_clientToServerStreamer ); - m_fieldOwner = RiaGrpcPdmObjectService::findCafObjectFromRipsObject( request->object() ); + m_fieldOwner = RiaGrpcHelper::findCafObjectFromRipsObject( request->object() ); QString fieldName = QString::fromStdString( request->method() ); std::vector fields = m_fieldOwner->fields(); @@ -220,7 +221,7 @@ Status RiaPdmObjectMethodStateHandler::init( const rips::PdmObjectSetterChunk* c CAF_ASSERT( chunk->has_set_request() ); auto setRequest = chunk->set_request(); auto methodRequest = setRequest.request(); - m_fieldOwner = RiaGrpcPdmObjectService::findCafObjectFromRipsObject( methodRequest.object() ); + m_fieldOwner = RiaGrpcHelper::findCafObjectFromRipsObject( methodRequest.object() ); QString fieldName = QString::fromStdString( methodRequest.method() ); int valueCount = setRequest.data_count(); @@ -230,30 +231,28 @@ Status RiaPdmObjectMethodStateHandler::init( const rips::PdmObjectSetterChunk* c auto scriptability = field->capability(); if ( scriptability && scriptability->scriptFieldName() == fieldName ) { - caf::PdmProxyFieldHandle* proxyField = dynamic_cast( field ); + auto* proxyField = dynamic_cast( field ); if ( proxyField ) { m_proxyField = proxyField; if ( dynamic_cast>*>( field ) ) { - m_dataHolder.reset( new DataHolder>( std::vector( valueCount ) ) ); + m_dataHolder = std::make_unique>>( std::vector( valueCount ) ); return grpc::Status::OK; } - else if ( dynamic_cast>*>( field ) ) + if ( dynamic_cast>*>( field ) ) { - m_dataHolder.reset( new DataHolder>( std::vector( valueCount ) ) ); + m_dataHolder = std::make_unique>>( std::vector( valueCount ) ); return grpc::Status::OK; } - else if ( dynamic_cast>*>( field ) ) + if ( dynamic_cast>*>( field ) ) { - m_dataHolder.reset( new DataHolder>( std::vector( valueCount ) ) ); + m_dataHolder = std::make_unique>>( std::vector( valueCount ) ); return grpc::Status::OK; } - else - { - CAF_ASSERT( false && "The proxy field data type is not yet supported for streaming fields" ); - } + + CAF_ASSERT( false && "The proxy field data type is not yet supported for streaming fields" ); } } } @@ -266,7 +265,7 @@ Status RiaPdmObjectMethodStateHandler::init( const rips::PdmObjectSetterChunk* c Status RiaPdmObjectMethodStateHandler::assignReply( rips::PdmObjectGetterReply* reply ) { CAF_ASSERT( m_dataHolder ); - const size_t packageSize = RiaGrpcServiceInterface::numberOfDataUnitsInPackage( m_dataHolder->dataSizeOf() ); + const size_t packageSize = RiaGrpcHelper::numberOfDataUnitsInPackage( m_dataHolder->dataSizeOf() ); size_t indexInPackage = 0u; m_dataHolder->reserveReplyStorage( reply ); @@ -377,7 +376,7 @@ grpc::Status RiaGrpcPdmObjectService::GetDescendantPdmObjects( grpc::ServerConte const rips::PdmDescendantObjectRequest* request, rips::PdmObjectArray* reply ) { - auto matchingObject = findCafObjectFromRipsObject( request->object() ); + auto matchingObject = RiaGrpcHelper::findCafObjectFromRipsObject( request->object() ); if ( matchingObject ) { @@ -402,7 +401,7 @@ grpc::Status RiaGrpcPdmObjectService::GetChildPdmObjects( grpc::ServerContext* const rips::PdmChildObjectRequest* request, rips::PdmObjectArray* reply ) { - auto matchingObject = findCafObjectFromRipsObject( request->object() ); + auto matchingObject = RiaGrpcHelper::findCafObjectFromRipsObject( request->object() ); if ( matchingObject ) { QString fieldName = QString::fromStdString( request->child_field() ); @@ -433,7 +432,7 @@ grpc::Status RiaGrpcPdmObjectService::UpdateExistingPdmObject( grpc::ServerConte const rips::PdmObject* request, rips::Empty* response ) { - auto matchingObject = findCafObjectFromRipsObject( *request ); + auto matchingObject = RiaGrpcHelper::findCafObjectFromRipsObject( *request ); if ( matchingObject ) { @@ -459,6 +458,36 @@ grpc::Status RiaGrpcPdmObjectService::UpdateExistingPdmObject( grpc::ServerConte return grpc::Status( grpc::NOT_FOUND, "PdmObject not found" ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +grpc::Status RiaGrpcPdmObjectService::DeleteExistingPdmObject( grpc::ServerContext* context, + const rips::PdmObject* request, + rips::Empty* response ) +{ + auto matchingObject = RiaGrpcHelper::findCafObjectFromRipsObject( *request ); + + if ( matchingObject && matchingObject->parentField() ) + { + auto parentField = matchingObject->parentField(); + parentField->removeChild( matchingObject ); + + auto obj = parentField->ownerObject(); + if ( obj && obj->uiCapability() ) + { + std::vector referringObjects; + + obj->onChildDeleted( nullptr, referringObjects ); + obj->uiCapability()->updateAllRequiredEditors(); + } + + delete matchingObject; + + return grpc::Status::OK; + } + return grpc::Status( grpc::NOT_FOUND, "PdmObject not found" ); +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -466,7 +495,7 @@ grpc::Status RiaGrpcPdmObjectService::CreateChildPdmObject( grpc::ServerContext* const rips::CreatePdmChildObjectRequest* request, rips::PdmObject* reply ) { - auto matchingObject = findCafObjectFromRipsObject( request->object() ); + auto matchingObject = RiaGrpcHelper::findCafObjectFromRipsObject( request->object() ); if ( matchingObject ) { @@ -517,7 +546,7 @@ grpc::Status RiaGrpcPdmObjectService::CallPdmObjectMethod( grpc::ServerContext* const rips::PdmObjectMethodRequest* request, rips::PdmObject* reply ) { - auto matchingObject = findCafObjectFromRipsObject( request->object() ); + auto matchingObject = RiaGrpcHelper::findCafObjectFromRipsObject( request->object() ); if ( matchingObject ) { QString methodKeyword = QString::fromStdString( request->method() ); @@ -538,15 +567,13 @@ grpc::Status RiaGrpcPdmObjectService::CallPdmObjectMethod( grpc::ServerContext* } return grpc::Status::OK; } - else - { - if ( method->isNullptrValidResult() ) - { - return grpc::Status::OK; - } - return grpc::Status( grpc::NOT_FOUND, "No result returned from Method" ); + if ( method->isNullptrValidResult() ) + { + return grpc::Status::OK; } + + return grpc::Status( grpc::NOT_FOUND, "No result returned from Method" ); } return grpc::Status( grpc::NOT_FOUND, "Could not find Method" ); } @@ -571,6 +598,9 @@ std::vector RiaGrpcPdmObjectService::createCallbacks( new RiaGrpcUnaryCallback( this, &Self::UpdateExistingPdmObject, &Self::RequestUpdateExistingPdmObject ), + new RiaGrpcUnaryCallback( this, + &Self::DeleteExistingPdmObject, + &Self::RequestDeleteExistingPdmObject ), new RiaGrpcUnaryCallback( this, &Self::CreateChildPdmObject, &Self::RequestCreateChildPdmObject ), @@ -596,41 +626,5 @@ std::vector RiaGrpcPdmObjectService::createCallbacks( }; } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -caf::PdmObject* RiaGrpcPdmObjectService::findCafObjectFromRipsObject( const rips::PdmObject& ripsObject ) -{ - QString scriptClassName = QString::fromStdString( ripsObject.class_keyword() ); - uint64_t address = ripsObject.address(); - return findCafObjectFromScriptNameAndAddress( scriptClassName, address ); -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -caf::PdmObject* RiaGrpcPdmObjectService::findCafObjectFromScriptNameAndAddress( const QString& scriptClassName, - uint64_t address ) -{ - QString classKeyword = caf::PdmObjectScriptingCapabilityRegister::classKeywordFromScriptClassName( scriptClassName ); - - if ( classKeyword == RimCommandRouter::classKeywordStatic() ) return RiaApplication::instance()->commandRouter(); - - RimProject* project = RimProject::current(); - std::vector objectsOfCurrentClass; - - project->descendantsIncludingThisFromClassKeyword( classKeyword, objectsOfCurrentClass ); - - caf::PdmObject* matchingObject = nullptr; - for ( caf::PdmObject* testObject : objectsOfCurrentClass ) - { - if ( reinterpret_cast( testObject ) == address ) - { - matchingObject = testObject; - } - } - return matchingObject; -} - static bool RiaGrpcPdmObjectService_init = RiaGrpcServiceFactory::instance()->registerCreator( typeid( RiaGrpcPdmObjectService ).hash_code() ); diff --git a/GrpcInterface/RiaGrpcPdmObjectService.h b/GrpcInterface/RiaGrpcPdmObjectService.h index 2b41195f39..3fae39cb4c 100644 --- a/GrpcInterface/RiaGrpcPdmObjectService.h +++ b/GrpcInterface/RiaGrpcPdmObjectService.h @@ -17,10 +17,10 @@ ////////////////////////////////////////////////////////////////////////////////// #pragma once -#include "PdmObject.grpc.pb.h" #include "RiaGrpcServiceInterface.h" -#include +#include "PdmObject.grpc.pb.h" + #include namespace caf @@ -30,7 +30,7 @@ class PdmProxyFieldHandle; struct AbstractDataHolder { - virtual ~AbstractDataHolder() = default; + virtual ~AbstractDataHolder() = default; virtual size_t dataCount() const = 0; virtual size_t dataSizeOf() const = 0; virtual void reserveReplyStorage( rips::PdmObjectGetterReply* reply ) const = 0; @@ -47,7 +47,7 @@ struct AbstractDataHolder //================================================================================================== class RiaPdmObjectMethodStateHandler { - typedef grpc::Status Status; + using Status = grpc::Status; public: RiaPdmObjectMethodStateHandler( bool clientToServerStreamer = false ); @@ -79,9 +79,11 @@ public: grpc::Status GetAncestorPdmObject( grpc::ServerContext* context, const rips::PdmParentObjectRequest* request, rips::PdmObject* reply ) override; + grpc::Status GetDescendantPdmObjects( grpc::ServerContext* context, const rips::PdmDescendantObjectRequest* request, rips::PdmObjectArray* reply ) override; + grpc::Status GetChildPdmObjects( grpc::ServerContext* context, const rips::PdmChildObjectRequest* request, rips::PdmObjectArray* reply ) override; @@ -94,20 +96,23 @@ public: const rips::PdmObject* request, rips::Empty* response ) override; + grpc::Status DeleteExistingPdmObject( grpc::ServerContext* context, + const rips::PdmObject* request, + rips::Empty* response ) override; + grpc::Status CallPdmObjectGetter( grpc::ServerContext* context, const rips::PdmObjectGetterRequest* request, rips::PdmObjectGetterReply* reply, RiaPdmObjectMethodStateHandler* stateHandler ); + grpc::Status CallPdmObjectSetter( grpc::ServerContext* context, const rips::PdmObjectSetterChunk* chunk, rips::ClientToServerStreamReply* reply, RiaPdmObjectMethodStateHandler* stateHandler ); + grpc::Status CallPdmObjectMethod( grpc::ServerContext* context, const rips::PdmObjectMethodRequest* request, rips::PdmObject* reply ) override; std::vector createCallbacks() override; - - static caf::PdmObject* findCafObjectFromRipsObject( const rips::PdmObject& ripsObject ); - static caf::PdmObject* findCafObjectFromScriptNameAndAddress( const QString& scriptClassName, uint64_t address ); }; diff --git a/GrpcInterface/RiaGrpcPropertiesService.cpp b/GrpcInterface/RiaGrpcPropertiesService.cpp index 2236a5bd21..a2ae751ff8 100644 --- a/GrpcInterface/RiaGrpcPropertiesService.cpp +++ b/GrpcInterface/RiaGrpcPropertiesService.cpp @@ -19,6 +19,7 @@ #include "RiaGrpcCallbacks.h" #include "RiaGrpcCaseService.h" +#include "RiaGrpcHelper.h" #include "RigActiveCellInfo.h" #include "RigActiveCellsResultAccessor.h" @@ -83,7 +84,7 @@ public: Status init( const PropertyRequest* request ) { int caseId = request->case_request().id(); - m_eclipseCase = dynamic_cast( RiaGrpcServiceInterface::findCase( caseId ) ); + m_eclipseCase = dynamic_cast( RiaGrpcHelper::findCase( caseId ) ); if ( m_eclipseCase ) { @@ -147,7 +148,7 @@ public: Status assignStreamReply( PropertyChunk* reply ) { // How many data units will fit into one stream package? - const size_t packageSize = RiaGrpcServiceInterface::numberOfDataUnitsInPackage( sizeof( double ) ); + const size_t packageSize = RiaGrpcHelper::numberOfDataUnitsInPackage( sizeof( double ) ); size_t indexInPackage = 0u; reply->mutable_values()->Reserve( (int)packageSize ); @@ -405,7 +406,7 @@ grpc::Status RiaGrpcPropertiesService::GetAvailableProperties( grpc::ServerConte AvailableProperties* reply ) { int caseId = request->case_request().id(); - RimEclipseCase* eclipseCase = dynamic_cast( RiaGrpcServiceInterface::findCase( caseId ) ); + RimEclipseCase* eclipseCase = dynamic_cast( RiaGrpcHelper::findCase( caseId ) ); if ( eclipseCase ) { auto porosityModel = static_cast( request->porosity_model() ); diff --git a/GrpcInterface/RiaGrpcServiceInterface.cpp b/GrpcInterface/RiaGrpcServiceInterface.cpp index 76c4bddddf..5b87695944 100644 --- a/GrpcInterface/RiaGrpcServiceInterface.cpp +++ b/GrpcInterface/RiaGrpcServiceInterface.cpp @@ -19,7 +19,6 @@ #include "RiaGrpcServiceInterface.h" #include "RiaLogging.h" -#include "RimCase.h" #include "RimProject.h" #include "cafPdmAbstractFieldScriptingCapability.h" @@ -39,32 +38,6 @@ #include #include -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -RimCase* RiaGrpcServiceInterface::findCase( int caseId ) -{ - std::vector cases = RimProject::current()->allGridCases(); - for ( RimCase* rimCase : cases ) - { - if ( caseId == rimCase->caseId() ) - { - return rimCase; - } - } - return nullptr; -} - -//-------------------------------------------------------------------------------------------------- -/// Find the number of data items that will fit in the given bytes. -/// The default argument for numBytesWantedInPackage is meant to be a sensible size for GRPC. -//-------------------------------------------------------------------------------------------------- -size_t RiaGrpcServiceInterface::numberOfDataUnitsInPackage( size_t dataUnitSize, size_t packageByteCount /*= 64 * 1024u*/ ) -{ - size_t dataUnitCount = packageByteCount / dataUnitSize; - return dataUnitCount; -} - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/GrpcInterface/RiaGrpcServiceInterface.h b/GrpcInterface/RiaGrpcServiceInterface.h index 465733f86c..579fc65b0d 100644 --- a/GrpcInterface/RiaGrpcServiceInterface.h +++ b/GrpcInterface/RiaGrpcServiceInterface.h @@ -22,7 +22,6 @@ #include class RiaGrpcCallbackInterface; -class RimCase; namespace caf { @@ -52,18 +51,11 @@ class RiaGrpcServiceInterface public: virtual std::vector createCallbacks() = 0; virtual ~RiaGrpcServiceInterface() = default; - static RimCase* findCase( int caseId ); - static size_t numberOfDataUnitsInPackage( size_t dataUnitSize, size_t packageByteCount = 64 * 1024u ); +protected: static void copyPdmObjectFromCafToRips( const caf::PdmObjectHandle* source, rips::PdmObject* destination ); static void copyPdmObjectFromRipsToCaf( const rips::PdmObject* source, caf::PdmObjectHandle* destination ); - static bool assignFieldValue( const QString& stringValue, - caf::PdmFieldHandle* field, - QVariant* oldValue, - QVariant* newValue, - caf::PdmScriptIOMessages* messages ); - static caf::PdmObjectHandle* emplaceChildField( caf::PdmObject* parent, const QString& fieldKeyword, const QString& keywordForClassToCreate ); @@ -71,7 +63,13 @@ public: const QString& keywordForClassToCreate ); static caf::PdmObjectHandle* emplaceChildArrayField( caf::PdmChildArrayFieldHandle* childArrayField, const QString& keywordForClassToCreate ); + + static bool assignFieldValue( const QString& stringValue, + caf::PdmFieldHandle* field, + QVariant* oldValue, + QVariant* newValue, + caf::PdmScriptIOMessages* messages ); }; #include "cafFactory.h" -typedef caf::Factory RiaGrpcServiceFactory; +using RiaGrpcServiceFactory = caf::Factory; diff --git a/GrpcInterface/RiaGrpcSimulationWellService.cpp b/GrpcInterface/RiaGrpcSimulationWellService.cpp index 180cefda6e..761a38d46a 100644 --- a/GrpcInterface/RiaGrpcSimulationWellService.cpp +++ b/GrpcInterface/RiaGrpcSimulationWellService.cpp @@ -18,6 +18,7 @@ #include "RiaGrpcSimulationWellService.h" #include "RiaGrpcCallbacks.h" +#include "RiaGrpcHelper.h" #include "RigEclipseCaseData.h" #include "RigGridBase.h" @@ -40,7 +41,7 @@ grpc::Status RiaGrpcSimulationWellService::GetSimulationWellStatus( grpc::Server rips::SimulationWellStatus* reply ) { - RimEclipseCase* eclipseCase = dynamic_cast( findCase( request->case_id() ) ); + RimEclipseCase* eclipseCase = dynamic_cast( RiaGrpcHelper::findCase( request->case_id() ) ); if ( !eclipseCase ) { return grpc::Status( grpc::NOT_FOUND, "Case not found" ); @@ -91,7 +92,7 @@ grpc::Status RiaGrpcSimulationWellService::GetSimulationWellCells( grpc::ServerC const rips::SimulationWellRequest* request, rips::SimulationWellCellInfoArray* reply ) { - RimEclipseCase* eclipseCase = dynamic_cast( findCase( request->case_id() ) ); + RimEclipseCase* eclipseCase = dynamic_cast( RiaGrpcHelper::findCase( request->case_id() ) ); if ( !eclipseCase ) { return grpc::Status( grpc::NOT_FOUND, "Case not found" );