Get physical components linked by a physical link

Hello,

I would like to find the physical components related to a physical link. To do this, I go through the list of my physical components, I get the physical ports, and I retrieve the physical links. But when I have a physical link, I would like to find the component at the other end. I made this code that is not very elegant, but it does not work. Cmp_a and cmp_b are always empty. Is there a way to get the physical components from a physical link? Maybe via the Java API?

I saw that it was possible to use something like this “for nodeSrc in capella_query_by_name(pl, “Physical Link Ends”):” but it no longer works.

def traverse_nodepc(npc):

for pp in npc.get_contained_physical_ports():
    for pl in pp.get_physical_links():
        if pl.get_id() in link_set:
            continue
        link_set.add(pl.get_id())
        ends = pl.get_connected_physical_ports()
        if len(ends) == 2:
            a, b = ends[0], ends[1]
            comp_a = comp_b = None
            for n in se.get_all_contents_by_type(NodePC):
                if a in n.get_contained_physical_ports():
                    comp_a = n
                if b in n.get_contained_physical_ports():
                    comp_b = n

for child in npc.get_owned_physical_components():
    if isinstance(child, NodePC):
        traverse_nodepc(child)

for npc in se.get_physical_architecture().get_physical_system().get_owned_physical_components():
if isinstance(npc, NodePC):
traverse_nodepc(npc)

Thanks in advance,

PhysicalPort are contained in the PhysicalComponent so when you have the port you should look for the container of the port:

for link in se.get_all_contents_by_type(PhysicalLink):
    ends = pl.get_connected_physical_ports()
    if len(ends) == 2:
        comp_a, comp_b = ends[0].e_container(), ends[1].e_container()
        ...

I’m not sure how to put this in your code… But your code is walking the all model multiple times and will most likely be slow on large models.

i have an error with e_container() which is not defined.

I though I could write those 4 lines without testing… big mistake.

for link in se.get_all_contents_by_type(PhysicalLink):
    ends = link.get_connected_physical_ports()
    if len(ends) == 2:
        comp_a, comp_b = ends[0].get_container(), ends[1].get_container()
        ...

This is the fixed version of the code.