Skip to content

Commit 7b33c37

Browse files
feat: add any_operator support (#1901)
* feat: add any_operator support * feat: add test for Operator copy construction
1 parent afa88c8 commit 7b33c37

File tree

7 files changed

+62
-6
lines changed

7 files changed

+62
-6
lines changed

src/ansys/dpf/core/any.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def _type_to_new_from_get_as_method(self, obj):
123123
custom_type_field,
124124
collection,
125125
workflow,
126+
dpf_operator,
126127
)
127128

128129
if issubclass(obj, int):
@@ -196,6 +197,11 @@ def _type_to_new_from_get_as_method(self, obj):
196197
self._api.any_new_from_int_collection,
197198
self._api.any_get_as_int_collection,
198199
)
200+
elif issubclass(obj, dpf_operator.Operator):
201+
return (
202+
self._api.any_new_from_operator,
203+
self._api.any_get_as_operator,
204+
)
199205

200206
@staticmethod
201207
def new_from(obj, server=None):

src/ansys/dpf/core/dpf_operator.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Operator:
121121
122122
"""
123123

124-
def __init__(self, name, config=None, server=None):
124+
def __init__(self, name=None, config=None, server=None, operator=None):
125125
"""Initialize the operator with its name by connecting to a stub."""
126126
self.name = name
127127
self._internal_obj = None
@@ -136,14 +136,29 @@ def __init__(self, name, config=None, server=None):
136136
# step 2: get api
137137
self._api_instance = None # see _api property
138138

139-
# step3: init environment
139+
# step 3: init environment
140140
self._api.init_operator_environment(self) # creates stub when gRPC
141141

142-
# step4: if object exists: take instance, else create it (server)
143-
if self._server.has_client():
144-
self._internal_obj = self._api.operator_new_on_client(self.name, self._server.client)
142+
# step 4: if object exists, take the instance, else create it
143+
if operator is not None:
144+
if isinstance(operator, Operator):
145+
core_api = self._server.get_api_for_type(
146+
capi=data_processing_capi.DataProcessingCAPI,
147+
grpcapi=data_processing_grpcapi.DataProcessingGRPCAPI,
148+
)
149+
core_api.init_data_processing_environment(self)
150+
self._internal_obj = core_api.data_processing_duplicate_object_reference(operator)
151+
self.name = operator.name
152+
else:
153+
self._internal_obj = operator
154+
self.name = self._api.operator_name(self)
145155
else:
146-
self._internal_obj = self._api.operator_new(self.name)
156+
if self._server.has_client():
157+
self._internal_obj = self._api.operator_new_on_client(
158+
self.name, self._server.client
159+
)
160+
else:
161+
self._internal_obj = self._api.operator_new(self.name)
147162

148163
if self._internal_obj is None:
149164
raise KeyError(

src/ansys/dpf/gate/any_grpcapi.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def _type_to_message_type():
4242
custom_type_field,
4343
collection_base,
4444
workflow,
45+
dpf_operator,
4546
)
4647

4748
return [(int, base_pb2.Type.INT),
@@ -58,6 +59,7 @@ def _type_to_message_type():
5859
(workflow.Workflow, base_pb2.Type.WORKFLOW),
5960
(collection_base.CollectionBase, base_pb2.Type.COLLECTION, base_pb2.Type.ANY),
6061
(dpf_vector.DPFVectorInt, base_pb2.Type.COLLECTION, base_pb2.Type.INT),
62+
(dpf_operator.Operator, base_pb2.Type.OPERATOR),
6163
]
6264

6365
@staticmethod
@@ -145,6 +147,10 @@ def any_get_as_int_collection(any):
145147
def any_get_as_workflow(any):
146148
return AnyGRPCAPI._get_as(any).workflow
147149

150+
@staticmethod
151+
def any_get_as_operator(any):
152+
return AnyGRPCAPI._get_as(any).operator
153+
148154
@staticmethod
149155
def _new_from(any, client=None):
150156
from ansys.grpc.dpf import dpf_any_pb2
@@ -230,3 +236,7 @@ def any_new_from_data_tree(any):
230236
@staticmethod
231237
def any_new_from_workflow(any):
232238
return AnyGRPCAPI._new_from(any, any._server)
239+
240+
@staticmethod
241+
def any_new_from_operator(any):
242+
return AnyGRPCAPI._new_from(any, any._server)

src/ansys/dpf/gate/operator_grpcapi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def get_list(op):
5151
def operator_get_config(op):
5252
return OperatorGRPCAPI.get_list(op).config
5353

54+
@staticmethod
55+
def operator_name(op):
56+
return OperatorGRPCAPI.get_list(op).op_name
57+
5458
@staticmethod
5559
def update_init(op, pin):
5660
from ansys.grpc.dpf import operator_pb2

tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ def return_ds(server=None):
328328
return return_ds
329329

330330

331+
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_10_0 = meets_version(
332+
get_server_version(core._global_server()), "10.0"
333+
)
331334
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_9_1 = meets_version(
332335
get_server_version(core._global_server()), "9.1"
333336
)

tests/test_any.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,15 @@ def test_cast_workflow_any(server_type):
132132
new_entity = any_dpf.cast()
133133

134134
assert new_entity.input_names == []
135+
136+
137+
@pytest.mark.skipif(
138+
not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_10_0,
139+
reason="any does not support operator below 8.0",
140+
)
141+
def test_cast_operator_any(server_type):
142+
entity = dpf.Operator(server=server_type, name="U")
143+
any_dpf = dpf.Any.new_from(entity)
144+
new_entity = any_dpf.cast()
145+
146+
assert entity.name == new_entity.name

tests/test_operator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ def test_create_operator(server_type):
5454
assert op._internal_obj
5555

5656

57+
def test_create_operator_from_operator(server_type):
58+
op = dpf.core.Operator("min_max", server=server_type)
59+
op2 = dpf.core.Operator(operator=op, server=server_type)
60+
assert op2._internal_obj
61+
62+
5763
def test_invalid_operator_name(server_type):
5864
# with pytest.raises(errors.DPFServerException):
5965
with pytest.raises(Exception):

0 commit comments

Comments
 (0)