Editing allocations

Hi everyone,

I’m beginner with python4capella and I’m trying to set the allocated operational activity,so to allocate it requires the “ownedfunctionalallocation of the entity”, for that I have started to write these lines below

class AbstractFunctionalBlock(CapellaElement):
    """

    """
    def __init__(self, java_object = None):
        if java_object is None:
            JavaObject.__init__(self, create_e_object("http://www.polarsys.org/capella/core/fa/" + capella_version(), "AbstractFunctionalBlock"))
        elif isinstance(java_object, AbstractFunctionalBlock):
            JavaObject.__init__(self, java_object.get_java_object())
        elif get_e_classifier("http://www.polarsys.org/capella/core/fa/" + capella_version(), "AbstractFunctionalBlock").isInstance(java_object):
            JavaObject.__init__(self, java_object)
        else:
            raise AttributeError("Passed object is not compatible with " + self.__class__.__name__ + ": " + str(java_object))
    def get_owned_functional_allocation(self):
        """
        """
        value =  self.get_java_object().getOwnedFunctionalAllocation()
        if value is None:
            return value
        else:
            e_object_class = getattr(sys.modules["__main__"], "EObject")
            specific_cls = e_object_class.get_class(value)
            return specific_cls(value)
class Relationship(CapellaElement):
    """

    """
    def __init__(self, java_object = None):
        if java_object is None:
            JavaObject.__init__(self, create_e_object("http://www.polarsys.org/capella/core/core/" + capella_version(), "Relationship"))
        elif isinstance(java_object, Relationship):
            JavaObject.__init__(self, java_object.get_java_object())
        elif get_e_classifier("http://www.polarsys.org/capella/core/core/" + capella_version(), "Relationship").isInstance(java_object):
            JavaObject.__init__(self, java_object)
        else:
            raise AttributeError("Passed object is not compatible with " + self.__class__.__name__ + ": " + str(java_object))

class Allocation(Relationship):
    """

    """
    def __init__(self, java_object = None):
        if java_object is None:
            JavaObject.__init__(self, create_e_object("http://www.polarsys.org/capella/core/core/" + capella_version(), " Allocation"))
        elif isinstance(java_object, Allocation):
            JavaObject.__init__(self, java_object.get_java_object())
        elif get_e_classifier("http://www.polarsys.org/capella/core/core/" + capella_version(), " Allocation").isInstance(java_object):
            JavaObject.__init__(self, java_object)
        else:
            raise AttributeError("Passed object is not compatible with " + self.__class__.__name__ + ": " + str(java_object))
class AbstractFunctionAllocation(Allocation):
    """

    """
    def __init__(self, java_object = None):
        if java_object is None:
            JavaObject.__init__(self, create_e_object("http://www.polarsys.org/capella/core/fa/" + capella_version(), "AbstractFunctionAllocation"))
        elif isinstance(java_object, AbstractFunctionAllocation):
            JavaObject.__init__(self, java_object.get_java_object())
        elif get_e_classifier("http://www.polarsys.org/capella/core/fa/" + capella_version(), "AbstractFunctionAllocation").isInstance(java_object):
            JavaObject.__init__(self, java_object)
        else:
            raise AttributeError("Passed object is not compatible with " + self.__class__.__name__ + ": " + str(java_object))
class ComponentFunctionalAllocation(AbstractFunctionAllocation):
    """

    """
    def __init__(self, java_object = None):
        if java_object is None:
            JavaObject.__init__(self, create_e_object("http://www.polarsys.org/capella/core/fa/" + capella_version(), "ComponentFunctionalAllocation"))
        elif isinstance(java_object, ComponentFunctionalAllocation):
            JavaObject.__init__(self, java_object.get_java_object())
        elif get_e_classifier("http://www.polarsys.org/capella/core/fa/" + capella_version(), "ComponentFunctionalAllocation").isInstance(java_object):
            JavaObject.__init__(self, java_object)
        else:
            raise AttributeError("Passed object is not compatible with " + self.__class__.__name__ + ": " + str(java_object))
    def get_function(self):
        """
        """
        value =  self.get_java_object().getFunction()
        if value is None:
            return value
        else:
            e_object_class = getattr(sys.modules["__main__"], "EObject")
            specific_cls = e_object_class.get_class(value)
            return specific_cls(value)
    def get_block(self):
        """
        """
        value =  self.get_java_object().getBlock()
        if value is None:
            return value
        else:
            e_object_class = getattr(sys.modules["__main__"], "EObject")
            specific_cls = e_object_class.get_class(value)
            return specific_cls(value)

then to instantiate the ownedFunctionalAllocation I call inside OperationalActor class as:

    def get_owned_functional_allocation(self):
        """
        """
        return create_e_list(self.get_java_object().getOwnedFunctionalAllocation(), OperationalActor)

Then to set the allocatedOperationalActivies I added the method as following:

    def set_allocated_operational_activities(self, value):
        """
        """
        return self.get_java_object().setAllocatedOperationalActivities(value.get_java_object())

Then for the test I have done as follows:

# include needed for the Capella modeller API
include('workspace://Python4Capella/simplified_api/capella.py')
if False:
    from simplified_api.capella import *

# include needed for utilities
include('workspace://Python4Capella/utilities/CapellaPlatform.py')
if False:
    from utilities.CapellaPlatform import *

# include needed for the requirement API
include('workspace://Python4Capella/simplified_api/requirement.py')
if False:
    from simplified_api.requirement import *
    
aird_path = '/In-Flight Entertainment System/In-Flight Entertainment System.aird'
model = CapellaModel()
model.open(aird_path)
se = model.get_system_engineering()

model.start_transaction()
try:
    for entity in se.get_all_contents_by_type(OperationalEntity):
        print(entity.get_name())
        for allocated in entity.get_allocated_operational_activities():
            print(allocated.get_name())
        for functional in entity.get_owned_functional_allocation():
            print(functional)
            entity.set_allocated_operational_activities(functional)
            print("end")
                   
except:
    model.rollback_transaction()
    raise
else:
    model.commit_transaction()

model.save(

But at the end I can’t allocate operational activity to the entity. There is something I am not doing correctly but I don’t know what it’s exactly. In my perception the source element and target element are missing but in the ecore “ComponentFunctionalAllocation” I can’t find that relationship. Any idea how to assign operationalActivity to the entity?

Thanks in advance

You need to add the function to the list of activities (multi valued EReference only have a getter to a list):

entity.get_allocated_operational_activities().add(functional)

I didn’t check myself but is should solve your issue.

1 Like

Thanks Yvan for your reply. I apologize for insisting but in order to set function

    def set_function(self, function):
        self.get_java_object().setFunction(function)

the value given for function is type ‘<main.OperationalActivity object at …’ , but in this case it return error

File “C:\Tools\Capella 5.2.0\plugins\py4j-python_0.10.9.5-bnd-2odeag\src\py4j\protocol.py”, line 298, in get_command_part
command_part = REFERENCE_TYPE + parameter._get_object_id()
AttributeError: ‘OperationalActivity’ object has no attribute ‘_get_object_id’

Any idea how to solve this?

You are passing a Python Object to the Java functionsetFunction(), you need to pass the Java Object:

def set_function(self, function):
    self.get_java_object().setFunction(function.get_java_object())

Thanks again Yvan for your reply. I’ve tested to set_function but still gives error

File “workspace://Python4Capella/simplified_api/capella.py”, line 1288, in set_function
File “C:\Tools\Capella 5.2.0\plugins\py4j-python_0.10.9.5-bnd-2odeag\src\py4j\java_gateway.py”, line 1321, in call
return_value = get_return_value(
File “C:\Tools\Capella 5.2.0\plugins\py4j-python_0.10.9.5-bnd-2odeag\src\py4j\protocol.py”, line 330, in get_return_value
raise Py4JError(
py4j.protocol.Py4JError: An error occurred while calling o282476.setFunction. Trace:
py4j.Py4JException: Method setFunction([class org.polarsys.capella.core.data.oa.impl.OperationalActivityImpl]) does not exist
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
at py4j.Gateway.invoke(Gateway.java:274)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.ClientServerConnection.sendCommand(ClientServerConnection.java:244)
at py4j.CallbackClient.sendCommand(CallbackClient.java:384)
at py4j.CallbackClient.sendCommand(CallbackClient.java:356)
at py4j.reflection.PythonProxyHandler.invoke(PythonProxyHandler.java:106)
at com.sun.proxy.$Proxy56.executeScript(Unknown Source)
at org.eclipse.ease.lang.python.py4j.internal.Py4jScriptEngine.internalExecute(Py4jScriptEngine.java:240)
at org.eclipse.ease.lang.python.py4j.internal.Py4jScriptEngine.execute(Py4jScriptEngine.java:227)
at org.eclipse.ease.AbstractScriptEngine.inject(AbstractScriptEngine.java:189)
at org.eclipse.ease.AbstractScriptEngine.run(AbstractScriptEngine.java:242)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

    for entity in se.get_all_contents_by_type(OperationalEntity):
        if entity.get_name() == "OE 5":
            for op in se.get_all_contents_by_type(OperationalActivity):
                if op.get_name()=="OperationalActivity 19":
                    cfa=ComponentFunctionalAllocation()
                    cfa.set_function(op)
                    cfa.set_block(entity)
                    entity.get_allocated_operational_activities().add(cfa)

After looking at the Java API you need to add a ComponentFunctionalAllocation to AbstractFunction.getComponentFunctionalAllocations(), it will set ComponentFunctionalAllocation.getFunction() since it’s the eOpposite relation. So you need to replace:

cfa.set_function(op)

by:

entity.get_java_object().getComponentFunctionalAllocations().add(cfa.get_java_object())

You also can wrap this line in your Python API.

class Function
    ...
def get_component_functional_allocations(self):
    return create_e_list(self.get_java_object().getComponentFunctionalAllocations(), ComponentFunctionalAllocation)

then use:

entity.get_component_functional_allocations().add(cfa)

I hope that will help. It’s not strait forward…

Thanks Yvan, it’s really useful all your replies in this learning process. It works finally !!, is created CFA, but the function is not specified, so any idea to do this ?
I’ve tried this code but it doesn’t work

cfa.get_java_object().getComponentFunctionalAllocations.add(op.get_java_object())

Thanks in advance

Ignore the last post, I have managed to get everything working. Thanks again for all.

Maybe you can post how you resolved your issue here for future readers ?

Sure !.To set function I’ve used the method set_source defined in AbstractTrace

cfa = ComponentFunctionalAllocation()
cfa.set_source(entity)
cfa.set_target(op)
entity.get_java_object().getComponentFunctionalAllocations().add(cfa.get_java_object())
1 Like