Create a Union/Variant in P4C

Hello,

How do I create a Union of kind Variant using Python4Capella? How to set its Discriminant and name?

Thank you

There is a Union class in the simplified API:

union = Union()
union.set_kind("VARIANT")
union.set_name("MyUnion")
discriminant = UnionProperty()
union.set_discriminant(discriminant)
1 Like

Thank you.

I create an Enumeration as the discriminant of the union and use its enumeration literals as qualifiers.

However, I don’t see any method in the UnionProperty class to set the qualifier of a union property. Do you know how to do that?

Yes that API is missing from the Python simplified API, but you can a use the Java API:

discriminant.get_java_object().getQualifier().add(enumeration.get_java_object())

I opened the following issue to add this API:

1 Like

Thank you very much.

More generally, where can I find the Java API so that I know which methods can be used?

The Java API is generated from the Ecore metamodels and the Java code is here.

1 Like