mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Python adjustments (#7809)
* #7797 Well Targets: Add scripting capability * #7794 Python : Do not update childField or childFieldArray * #7797: Python - Add scripting to well path collection - Extend the pdmobject.py with method add_object() - allow objects to be created from Python in well path collections - add well targets to modelled well path * #7795 Python : Make sure referenced generated classes are defined * #7810 StimPlanModel: clean-up python generation * Python : Always use empty string as default value for ptrFieldValue It can happen that a ptrField is assigned to a pointer on object construction. (FaciesProperties) Make sure that constructor always assigns an empty string. Co-authored-by: magnesj <magnesj@users.noreply.github.com> Co-authored-by: Kristian Bendiksen <kristian.bendiksen@gmail.com>
This commit is contained in:
@@ -32,6 +32,7 @@ message CreatePdmChildObjectRequest
|
||||
{
|
||||
PdmObject object = 1;
|
||||
string child_field = 2;
|
||||
string class_keyword = 3;
|
||||
}
|
||||
|
||||
message PdmParentObjectRequest
|
||||
|
@@ -284,6 +284,41 @@ class PdmObjectBase:
|
||||
return []
|
||||
raise e
|
||||
|
||||
def add_new_object(self, class_definition, child_field=""):
|
||||
"""Create and add an object to the specified child field
|
||||
Arguments:
|
||||
class_definition[class]: Class definition of the object to create
|
||||
child_field[str]: The keyword for the field to create a new object in. If empty, the first child array field is used.
|
||||
Returns:
|
||||
The created PdmObject inside the child_field
|
||||
"""
|
||||
from .generated.generated_classes import class_from_keyword
|
||||
|
||||
assert inspect.isclass(class_definition)
|
||||
|
||||
class_keyword = class_definition.__name__
|
||||
|
||||
request = PdmObject_pb2.CreatePdmChildObjectRequest(
|
||||
object=self._pb2_object,
|
||||
child_field=child_field,
|
||||
class_keyword=class_keyword,
|
||||
)
|
||||
try:
|
||||
pb2_object = self._pdm_object_stub.CreateChildPdmObject(request)
|
||||
child_class_definition = class_from_keyword(pb2_object.class_keyword)
|
||||
|
||||
if child_class_definition is None:
|
||||
child_class_definition = class_keyword
|
||||
|
||||
pdm_object = child_class_definition(
|
||||
pb2_object=pb2_object, channel=self.channel()
|
||||
)
|
||||
return pdm_object
|
||||
except grpc.RpcError as e:
|
||||
if e.code() == grpc.StatusCode.NOT_FOUND:
|
||||
return None
|
||||
raise e
|
||||
|
||||
def ancestor(self, class_definition):
|
||||
"""Find the first ancestor that matches the provided class_keyword
|
||||
Arguments:
|
||||
|
35
GrpcInterface/Python/rips/tests/create_well_path.py
Normal file
35
GrpcInterface/Python/rips/tests/create_well_path.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from rips.generated.generated_classes import (
|
||||
ModeledWellPath,
|
||||
StimPlanModel,
|
||||
WellPathGeometry,
|
||||
WellPathTarget,
|
||||
)
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(1, os.path.join(sys.path[0], "../../"))
|
||||
import rips
|
||||
|
||||
|
||||
def test_well_path_target(rips_instance, initialize_test):
|
||||
well_path_coll = rips_instance.project.descendants(rips.WellPathCollection)[0]
|
||||
|
||||
my_well_path = well_path_coll.add_new_object(rips.ModeledWellPath)
|
||||
my_well_path.name = "test"
|
||||
my_well_path.update()
|
||||
|
||||
geometry = my_well_path.well_path_geometry()
|
||||
geometry.add_new_object(rips.WellPathTarget)
|
||||
geometry.add_new_object(rips.WellPathTarget)
|
||||
geometry.add_new_object(rips.WellPathTarget)
|
||||
assert len(geometry.well_path_targets()) == 3
|
||||
|
||||
assert len(well_path_coll.well_paths()) == 1
|
||||
my_well_path_duplicate = well_path_coll.well_paths()[0]
|
||||
assert my_well_path_duplicate.name == "test"
|
||||
geometry_duplicate = my_well_path_duplicate.well_path_geometry()
|
||||
assert len(geometry_duplicate.well_path_targets()) == 3
|
||||
|
||||
# Not allowed to add object of unrelated type
|
||||
invalid_object = geometry.add_new_object(rips.WellPath)
|
||||
assert invalid_object is None
|
@@ -269,7 +269,7 @@ void RiaGrpcCommandService::assignPdmObjectValues( caf::PdmObjectHandle*
|
||||
}
|
||||
else if ( childObjects.empty() )
|
||||
{
|
||||
childObject = emplaceChildField( pdmChildFieldHandle );
|
||||
childObject = emplaceChildField( pdmChildFieldHandle, "" );
|
||||
}
|
||||
CAF_ASSERT( childObject );
|
||||
if ( childObject )
|
||||
|
@@ -473,11 +473,15 @@ grpc::Status RiaGrpcPdmObjectService::CreateChildPdmObject( grpc::ServerContext*
|
||||
{
|
||||
CAF_ASSERT( request );
|
||||
|
||||
caf::PdmObjectHandle* pdmObject =
|
||||
emplaceChildField( matchingObject, QString::fromStdString( request->child_field() ) );
|
||||
if ( pdmObject )
|
||||
QString keywordClassToCreate = QString::fromStdString( request->class_keyword() );
|
||||
QString fieldKeyword = QString::fromStdString( request->child_field() );
|
||||
|
||||
caf::PdmObjectHandle* pdmObjectHandle = emplaceChildField( matchingObject, fieldKeyword, keywordClassToCreate );
|
||||
if ( pdmObjectHandle )
|
||||
{
|
||||
copyPdmObjectFromCafToRips( pdmObject, reply );
|
||||
copyPdmObjectFromCafToRips( pdmObjectHandle, reply );
|
||||
matchingObject->uiCapability()->updateConnectedEditors();
|
||||
|
||||
return grpc::Status::OK;
|
||||
}
|
||||
return grpc::Status( grpc::NOT_FOUND, "Could not create PdmObject" );
|
||||
|
@@ -142,6 +142,12 @@ void RiaGrpcServiceInterface::copyPdmObjectFromRipsToCaf( const rips::PdmObject*
|
||||
auto scriptability = field->template capability<caf::PdmAbstractFieldScriptingCapability>();
|
||||
if ( scriptability )
|
||||
{
|
||||
if ( !dynamic_cast<caf::PdmValueField*>( field ) )
|
||||
{
|
||||
// Recursive object update is not supported
|
||||
// https://github.com/OPM/ResInsight/issues/7794
|
||||
continue;
|
||||
}
|
||||
QString keyword = scriptability->scriptFieldName();
|
||||
QString value = QString::fromStdString( parametersMap[keyword.toStdString()] );
|
||||
|
||||
@@ -181,7 +187,9 @@ bool RiaGrpcServiceInterface::assignFieldValue( const QString& stringValue
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
caf::PdmObjectHandle* RiaGrpcServiceInterface::emplaceChildField( caf::PdmObject* parent, const QString& fieldLabel )
|
||||
caf::PdmObjectHandle* RiaGrpcServiceInterface::emplaceChildField( caf::PdmObject* parent,
|
||||
const QString& fieldKeyword,
|
||||
const QString& keywordForClassToCreate )
|
||||
{
|
||||
std::vector<caf::PdmFieldHandle*> fields;
|
||||
parent->fields( fields );
|
||||
@@ -190,13 +198,32 @@ caf::PdmObjectHandle* RiaGrpcServiceInterface::emplaceChildField( caf::PdmObject
|
||||
{
|
||||
auto pdmChildArrayField = dynamic_cast<caf::PdmChildArrayFieldHandle*>( field );
|
||||
auto pdmChildField = dynamic_cast<caf::PdmChildFieldHandle*>( field );
|
||||
if ( pdmChildArrayField && pdmChildArrayField->keyword() == fieldLabel )
|
||||
if ( pdmChildArrayField )
|
||||
{
|
||||
return emplaceChildArrayField( pdmChildArrayField );
|
||||
bool isMatching = false;
|
||||
if ( fieldKeyword.isEmpty() )
|
||||
{
|
||||
// Use first child array field if no fieldKeyword is specified
|
||||
isMatching = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
isMatching = ( pdmChildArrayField->keyword() == fieldKeyword );
|
||||
}
|
||||
|
||||
if ( isMatching )
|
||||
{
|
||||
auto objectCreated = emplaceChildArrayField( pdmChildArrayField, keywordForClassToCreate );
|
||||
|
||||
// Notify parent object that a new object has been created
|
||||
if ( objectCreated ) parent->onChildAdded( pdmChildArrayField );
|
||||
|
||||
return objectCreated;
|
||||
}
|
||||
}
|
||||
else if ( pdmChildField && pdmChildField->keyword() == fieldLabel )
|
||||
else if ( pdmChildField && pdmChildField->keyword() == fieldKeyword )
|
||||
{
|
||||
return emplaceChildField( pdmChildField );
|
||||
return emplaceChildField( pdmChildField, keywordForClassToCreate );
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
@@ -205,12 +232,33 @@ caf::PdmObjectHandle* RiaGrpcServiceInterface::emplaceChildField( caf::PdmObject
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
caf::PdmObjectHandle* RiaGrpcServiceInterface::emplaceChildField( caf::PdmChildFieldHandle* childField )
|
||||
caf::PdmObjectHandle* RiaGrpcServiceInterface::emplaceChildField( caf::PdmChildFieldHandle* childField,
|
||||
const QString& keywordForClassToCreate )
|
||||
{
|
||||
QString childClassKeyword = childField->xmlCapability()->dataTypeName();
|
||||
QString childClassKeyword;
|
||||
if ( keywordForClassToCreate.isEmpty() )
|
||||
{
|
||||
childClassKeyword = childField->xmlCapability()->dataTypeName();
|
||||
}
|
||||
else
|
||||
{
|
||||
childClassKeyword = keywordForClassToCreate;
|
||||
}
|
||||
|
||||
auto pdmObjectHandle = caf::PdmDefaultObjectFactory::instance()->create( childClassKeyword );
|
||||
CAF_ASSERT( pdmObjectHandle );
|
||||
|
||||
{
|
||||
auto childDataTypeName = childField->xmlCapability()->dataTypeName();
|
||||
|
||||
auto isInheritanceValid = pdmObjectHandle->xmlCapability()->inheritsClassWithKeyword( childDataTypeName );
|
||||
if ( !isInheritanceValid )
|
||||
{
|
||||
delete pdmObjectHandle;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
childField->setChildObject( pdmObjectHandle );
|
||||
return pdmObjectHandle;
|
||||
}
|
||||
@@ -218,13 +266,34 @@ caf::PdmObjectHandle* RiaGrpcServiceInterface::emplaceChildField( caf::PdmChildF
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
caf::PdmObjectHandle* RiaGrpcServiceInterface::emplaceChildArrayField( caf::PdmChildArrayFieldHandle* childArrayField )
|
||||
caf::PdmObjectHandle* RiaGrpcServiceInterface::emplaceChildArrayField( caf::PdmChildArrayFieldHandle* childArrayField,
|
||||
const QString& keywordForClassToCreate )
|
||||
{
|
||||
QString childClassKeyword = childArrayField->xmlCapability()->dataTypeName();
|
||||
QString childClassKeyword;
|
||||
if ( keywordForClassToCreate.isEmpty() )
|
||||
{
|
||||
childClassKeyword = childArrayField->xmlCapability()->dataTypeName();
|
||||
}
|
||||
else
|
||||
{
|
||||
childClassKeyword = keywordForClassToCreate;
|
||||
}
|
||||
|
||||
auto pdmObjectHandle = caf::PdmDefaultObjectFactory::instance()->create( childClassKeyword );
|
||||
CAF_ASSERT( pdmObjectHandle );
|
||||
if ( !pdmObjectHandle ) return nullptr;
|
||||
|
||||
{
|
||||
auto childDataTypeName = childArrayField->xmlCapability()->dataTypeName();
|
||||
|
||||
auto isInheritanceValid = pdmObjectHandle->xmlCapability()->inheritsClassWithKeyword( childDataTypeName );
|
||||
if ( !isInheritanceValid )
|
||||
{
|
||||
delete pdmObjectHandle;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
childArrayField->insertAt( -1, pdmObjectHandle );
|
||||
|
||||
return pdmObjectHandle;
|
||||
}
|
||||
|
@@ -60,10 +60,13 @@ public:
|
||||
static bool
|
||||
assignFieldValue( const QString& stringValue, caf::PdmFieldHandle* field, QVariant* oldValue, QVariant* newValue );
|
||||
|
||||
static caf::PdmObjectHandle* emplaceChildField( caf::PdmObject* parent, const QString& fieldLabel );
|
||||
static caf::PdmObjectHandle*
|
||||
emplaceChildField( caf::PdmObject* parent, const QString& fieldKeyword, const QString& keywordForClassToCreate );
|
||||
|
||||
static caf::PdmObjectHandle* emplaceChildField( caf::PdmChildFieldHandle* childField );
|
||||
static caf::PdmObjectHandle* emplaceChildArrayField( caf::PdmChildArrayFieldHandle* childArrayField );
|
||||
static caf::PdmObjectHandle* emplaceChildField( caf::PdmChildFieldHandle* childField,
|
||||
const QString& keywordForClassToCreate );
|
||||
static caf::PdmObjectHandle* emplaceChildArrayField( caf::PdmChildArrayFieldHandle* childArrayField,
|
||||
const QString& keywordForClassToCreate );
|
||||
};
|
||||
|
||||
#include "cafFactory.h"
|
||||
|
Reference in New Issue
Block a user