Create Physical Function with Python4Capella

Hi everyone,
I’m trying to create a physical function in Capella using Python4Capella, but it seems that the method get_owned_physical_functions does not exist to add a physical function to the Root Physical function.

Here’s an example of my code snippet:
pf_root = se.get_physical_architecture().get_physical_function_pkg().get_contents()[0]

print("pf_root: " + pf_root.get_name()) #get Root Physical Function

pfct = PhysicalFunction()
puid = uuid.uuid4()

pfct.set_id(str(puid))

date_heure = datetime.now().strftime("%Y%m%d_%H%M%S")
pfct.set_name(f"PF_{date_heure}")

pf_root.get_owned_physical_functions.add(pfct)
print("* Create a new Pf with name " + pfct.get_name() + "   under   " + pf_root.get_name())   

Does anyone know how to create a physical function ?
Thanks in advance for your help!

Thierry

Hi Thierry,

You need to use get_owned_functions() which is the generic getter for all functions (logical, system, physical):

pf_root.get_owned_functions().add(pfct)

And don’t forget the parenthesis.

Thanks @YvanLussaud , it works.

Now I’m trying to create a functional exchange using get_incoming and get outgoing but I get an error. I also tried with get_inputs and get_outputs but i have the same error:

java.lang.UnsupportedOperationException
at org.eclipse.emf.common.util.BasicEList$UnmodifiableEList.add(BasicEList.java:908)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

The source code is hereunder:

Pf1 = find_physical_function_by_name('PhysicalFunction 1',pf_root)

    if not Pf1:
        print ( 'NOT FOUND FROM: ' + str(Pf1))
    else:
        Pf2 = find_physical_function_by_name('PhysicalFunction 2',pf_root)         
        if not Pf2:
            print ( 'NOT FOUND TO: ' + str(Pf2))
        else: 
        
            pfip = FunctionInputPort()
            pfip.set_name(f"PFIp_{date_heure}")
            PhysicalFunction(Pf1).get_incoming().add(pfip)
        
            pfop = FunctionOutputPort()
            pfop.set_name(f"PFop_{date_heure}")

           PhysicalFunction(Pf2).get_outgoing().add(pfop)
        
            
            le = FunctionalExchange()
            date_heure = datetime.now().strftime("%Y%m%d_%H%M%S")

            le.set_name(f"FE_{date_heure}")
            le.set_source_port(pfop)
            le.set_target_port(pfip)      

Do you know what methods I should use?

Thanks in advance,

Thierry

1 Like

I think incoming and outgoing ERefrences are derived (computed) and can’t be modified. You need to use inputs and outputs instead.

# you can also get an existing FunctionInputPort
pfip = FunctionInputPort()
f1.get_inputs().add(pfip)

# you can also get an existing FunctionOutputPort
pfop = FunctionOutputPort()
f2.get_outputs().add(pfop)

fe = FunctionalExchange()
fe.set_target_port(pfip)
fe.set_source_port(pfop)

commonFunctionAncestor.get_java_object().getOwnedFunctionalExchanges().add(fe.get_java_object())

It works perfectly.
Thank you very much @YvanLussaud and have a great holiday season.

1 Like