How can I set a feature reference expression?

Hi,

I want to reference to an element with a feature value expression. I have a model like this:

package Structure {
  attribute attribute1;
  attribute attribute2;
}

and want to get a model like this:

package Structure {
  attribute attribute1;
  attribute attribute2 = Structure::attribute1;
}

I also want to do this for other element types. How can I do this? Thanks for your help!

Hi, Klara!

To get started you will need to have these elements in your python environment - I will refer to them as attribute_1 and attribute_2:

feature_expr = attribute_2.feature_value_member
_, member_element = feature_expr.set_member_element(
    syside.FeatureReferenceExpression
)

reference_value = member_element.referent_member
_, referent = reference_value.set_member_element(
    attribute_1
)

with open("file_name.sysml", "w") as f:
    f.write(syside.pprint(package_object))

This should also work for other element types as well, but if there is an issue - please let me know.

1 Like

Hi Martynas,

thanks a lot, this is working!

Related to this topic, I tried to insert a set of attributes as feature reference expression. With the code you provided, I was only able to insert one expression. Assuming, there is also an attribute3, how can I produce a model like this:

attribute attribute2 = (Structure::attribute1, Structure::attribute3);

You proceed much the same way, except instead of starting with a FeatureReferenceExpression you start with an OperatorExpression with the operator set to Comma.

Say attribute_1, attribute_2, and attribute_3 are analogous to @Martynas_Strazdas 's example, meaning we basically start with

package Structure {
  attribute attribute_1;
  attribute attribute_2;
  attribute attribute_3;
}

Then to set attribute_2 = (attribute_1, attribute_2) you do

# attribute_1 = ... get the attribute element ...
# attribute_2 = ... get the attribute element ...
# attribute_3 = ... get the attribute element ...

# Proceed as before, but create an appropriate OperatorExpression
feature_expr = attribute_2.feature_value_member
_, member_element = feature_expr.set_member_element(
    syside.OperatorExpression
)
member_element.operator = syside.ExplicitOperator.Comma

If you pprint this you get (the equivalent of)

package Structure {
  attribute attribute_1;
  attribute attribute_2 = ();
  attribute attribute_3;
}

Finally add the two features to the OperatorExpressionsimilarly to how you set the single feature in @Martynas_Strazdas 's example:

_, arg_1 = member_element.arguments.append(
    syside.FeatureReferenceExpression
)

arg_1.referent_member.set_member_element(attribute_1)

to add attribute_1 and

_, arg_2 = member_element.arguments.append(
    syside.FeatureReferenceExpression
)

arg_2.referent_member.set_member_element(attribute_3)

to add a second one.

1 Like

Thank you, Tilo!

1 Like