Skip to content

Commit

Permalink
feat: add Operator.id property
Browse files Browse the repository at this point in the history
feat: add ability to connect operator as input
  • Loading branch information
Matteo-Baussart-ANSYS committed Nov 22, 2024
1 parent 849d50f commit 64a1890
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/ansys/dpf/core/dpf_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def __init__(self, name, config=None, server=None):
self._internal_obj = None
self._description = None
self._inputs = None
self._id = None

# step 1: get server
self._server = server_module.get_or_create_server(
Expand Down Expand Up @@ -226,7 +227,7 @@ def progress_bar(self) -> bool:
def progress_bar(self, value: bool) -> None:
self._progress_bar = value

def connect(self, pin, inpt, pin_out=0):
def connect(self, pin, inpt, pin_out=0, operator_as_input=False):
"""Connect an input on the operator using a pin number.
Parameters
Expand Down Expand Up @@ -260,7 +261,10 @@ def connect(self, pin, inpt, pin_out=0):
if inpt is self:
raise ValueError("Cannot connect to itself.")
elif isinstance(inpt, Operator):
self._api.operator_connect_operator_output(self, pin, inpt, pin_out)
if operator_as_input:
self._api.operator_connect_operator_as_input(self, pin, inpt)
else:
self._api.operator_connect_operator_output(self, pin, inpt, pin_out)
elif isinstance(inpt, Output):
self._api.operator_connect_operator_output(self, pin, inpt._operator, inpt._pin)
elif isinstance(inpt, list):
Expand Down Expand Up @@ -642,6 +646,15 @@ def config(self, value):
"""
self._api.operator_set_config(self, value)

@property
def id(self):
if self._id is None:
operator_id_op = Operator("operator_id")
operator_id_op.inputs.operator.connect(self, operator_as_input=True)
self._id = operator_id_op.outputs.id()

return self._id

@property
def inputs(self):
"""Inputs connected to the operator.
Expand Down
6 changes: 3 additions & 3 deletions src/ansys/dpf/core/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(self, spec, pin, operator, count_ellipsis=-1):
self.name += str(self._count_ellipsis + 1)
self._update_doc_str(docstr, self.name)

def connect(self, inpt):
def connect(self, inpt, operator_as_input=False):
"""Connect any input (entity or operator output) to a specified input pin of this operator.
Parameters
Expand All @@ -85,7 +85,7 @@ def connect(self, inpt):
# always convert ranges to lists
if isinstance(inpt, range):
inpt = list(inpt)
elif isinstance(inpt, core.Operator):
elif not operator_as_input and isinstance(inpt, core.Operator):
if hasattr(inpt, "outputs"):
inpt = inpt.outputs
else:
Expand Down Expand Up @@ -150,7 +150,7 @@ def connect(self, inpt):
corresponding_pins[0][1]: weakref.ref(inpt)
}
else:
self._operator().connect(self._pin, inpt)
self._operator().connect(self._pin, inpt, operator_as_input=operator_as_input)
self._operator().inputs._connected_inputs[self._pin] = (
weakref.ref(inpt) if hasattr(inpt, "__weakref__") else inpt
)
Expand Down

0 comments on commit 64a1890

Please sign in to comment.