Allocation Usage

How can I write an allocation usage with the Automator? I have a model like this:

attribute attribute1;
attribute attribute2;

and want to extend my model with an allocation like this:

allocate attribute1 to attribute2;

Hello, Klara!

To create a new allocation, you will need the elements you want to allocate to one another. I will refer to them as attribute_1 and attribute_2. Below is a code snippet that creates an allocation allocate attribute1 to attribute2 in an example namespace example_namespace (e.g. the package in which the attributes are defined):

_, new_allocation = example_namespace.children.append(
    syside.OwningMembership, syside.AllocationUsage
)

_, source = allocation.declared_ends.append()
source.heritage.append(syside.ReferenceSubsetting, attribute_1)

_, sink = allocation.declared_ends.append()
sink.heritage.append(
    syside.ReferenceSubsetting, attribute_2),
)

where _ is just a conventional name of a variable that is returned by the function, but not used. To save this new allocation into a SysML v2 file, you can use syside.pprint to either write over the old file or create a copy:

with open("sysml_file_name.sysml", "w") as f:
    f.write(syside.pprint(example_namespace)

Hopefully this is what you were asking for! If you have any more questions, please do let me know.

P.S.
We are also working on an API that would make adding SysML v2 elements more user friendly.

1 Like

Hello Martynas,
thank you, this is exactly what I need.
More user friendly APIs sound great. Is it then still possible to use the current functionalities to add SysML v2 elements?

If you are asking if the experimental API will break the above code - no, that is not the case. The current implementation is stable and the API changes will not affect existing functionality.

1 Like