codeable_models.CAssociation

class codeable_models.CAssociation(source, target, descriptor=None, **kwargs)

CAssociation is used for representing associations. Usually associations are created using the association method of CClassifier which calls the constructor of CAssociation to actually create the association (i.e., CAssociation could also be used directly).

Superclasses: CClassifier

Parameters
  • source (CClassifier) – The source classifier of the association.

  • target (CClassifier) – The target classifier of the association.

  • descriptor – An optional descriptor making it easier to define associations with a simple string. The descriptor syntax is described below.

  • **kwargs

    Pass in any kwargs acceptable to superclasses. In addition, CAssociation accepts: multiplicity, role_name, source_multiplicity, source_role_name, aggregation, composition, stereotypes.

    • multiplicity:

      Used to provide the target multiplicity of the association. Defaults to *. See documentation of the property multiplicity for the accepted syntax.

    • role_name:

      Takes a string specifying the target role name and stores it in the same-named attribute. Defaults to None.

    • source_multiplicity:

      Used to provide the source multiplicity of the association. Defaults to 1. See documentation of the property multiplicity for the accepted syntax.

    • source_role_name:

      Takes a string specifying the source role name and stores it in the same-named attribute. Defaults to None.

    • aggregation:

      Takes a boolean argument. If set to True, this association is set to be an aggregation, else it is not. Also, if set to True, it sets the property composition to False.

    • composition:

      Takes a boolean argument. If set to True, this association is set to be a composition, else it is not. Also, if set to True, it sets the property aggregation to False.

    • stereotypes:

      Takes a single or a list of CStereotype objects which extend this association. Extension is only possible, if this association is an association between meta-classes.

role_name

A string specifying the target role name.

source_role_name

A string specifying the source role name.

Please note that the name property derived from CNamedElement is interpreted as the label of the association.

Descriptor Syntax:

The descriptor has the following syntax:

?<label>:? \
    ?[<source_role_name>]? <source_multiplicity> ->|<>-|<*>- \
    ?[<target_role_name>]? <target_multiplicity>

With it, first the label of the association can be optionally specified. Source and target multiplicity strings, in the same form as accepted by the multiplicity property, must be specified, too. They are divided by an arrow determining the type of association: -> for ordinary associations, <>- for aggregations, and <*>- for compositions. Optionally in square brackets the source and target role names can be specified right before the multiplicities.

Please note that first the keyword arguments are evaluated and then the descriptor. So the descriptor would override settings made in the keyword arguments.

Examples:

The following specifies a 0..1 to * association from a class cart to a class item labelled in cart with source role name cart and target role name item in cart:

cart.association(item, "in cart: [cart] 0..1 -> [item in cart] *")

The same can be specified more verbosely with kwargs:

cart.association(item, name="in cart", role_name="item in cart", multiplicity="*",
     source_role_name="cart", source_multiplicity="0..1")

Main Relations:

The main relations of CAssociation are shown in the figure below.

../_images/association_model.png

A CAssociation is need in order to derive CLink objects, which link objects in an object model. The CAssociation is the classifier of the CLink.

As can be seen, a CAssociation can be extended by stereotypes. A CLink can have stereotype instances of any CStereotype defined on the link’s association.

The CClassifier can be introspected for its associations. The association stores its source and target classifiers.

property aggregation

Takes a boolean argument. If set to True, this association is set to be an aggregation, else it is not. Also, if set to True, it sets the property composition to False.

Type

bool

property attributes

Overridden method from CClassifier. Attributes on associations are not supported.

property composition

Takes a boolean argument. If set to True, this association is set to be a composition, else it is not. Also, if set to True, it sets the property aggregation to False.

Type

bool

delete()

Deletes this association. Removes the association from all classifiers, links, and derived associations. Removes it from all stereotypes, too. Removes it from associations it is derived from. Calls delete() on superclass.

Returns

None.

delete_tagged_value(name, stereotype=None)

Delete tagged value of a stereotype attribute with the given name. Optionally the stereotype to consider can be specified. This is needed, if one or more attributes of the same name are defined on the inheritance hierarchy. Then a shadowed attribute can be accessed by specifying its stereotype.

Parameters
  • name – The name of the attribute.

  • stereotype – The optional stereotype on which the attribute is defined.

Returns

Value of the attribute.

Return type

Supported Attribute Types

property derived_associations

Getter for the list of associations this association is derived from.

Type

list[CAssociation]

property derived_from

Getter to get and setter to set a metaclass association this class-level association is derived from.

Please note that parameter has to be a metaclass association, and this association needs to be a class-level association. An association can be derived from a metaclass association, if the association is of the same type as the metaclass association, i.e., both are either association, composition, or aggregation.

Both for source and target multiplicities,the lower multiplicity must be equal or higher than the metaclass association’s lower multiplicity, and the upper multiplicity must be equal or lower than the metaclass association’s upper multiplicity.

For the upper multiplicities (source and target), we need to consider other derived associations of the same kind (i.e., same classes or their superclasses are used, derived from the same metaclass association), as well. For example, an consider a meta-class association:

a = m1.association(m2, "a: [m1] * -> [m2] 1")

If we would derive two associations from it:

c1.association(c2, "a1: [c1] * -> [c2] 1", derived_from=a)
c1.association(c2, "a1: [c1] * -> [c2] 1", derived_from=a)

without adding up the multiplicities of all those associations, an upper (target) multiplicity of 2 could be reached, violating the meta-class target upper multiplicity of 1.

The stereotype instances must be stereotypes extending an association that this association is derived from.

The setter takes a CAssociation as argument. Can be None (the default value) to remove the derived_from relation. The getter always returns a CAssociation or None.

Type

CAssociation

get_opposite_classifier(classifier)

Given a classifier, this method returns the opposite in the association, i.e. the source if classifier is the target, and vice versa. Raises an exception if classifier is neither source nor target.

Parameters

classifier – The classifier from which we want to get the opposite in the association.

Returns

The opposite classifier.

Return type

CClassifier

get_tagged_value(name, stereotype=None)

Get the tagged value of a stereotype attribute with the given name. Optionally the stereotype to consider can be specified. This is needed, if one or more attributes of the same name are defined on the inheritance hierarchy. Then a shadowed attribute can be accessed by specifying its stereotype.

Parameters
  • name – The name of the attribute.

  • stereotype – The optional stereotype on which the attribute is defined.

Returns

Value of the attribute.

Return type

Supported Attribute Types

property multiplicity

Getter and setter for the target multiplicity of the association. The multiplicity string has the following syntax: <lower_multiplicity>..<higher_multiplicity>|<single_multiplicity>.

The multiplicity is either specified as a range or using a <single_multiplicity>. Accepted <single_multiplicity> values are any positive number, zero, or *. * denotes unbounded multiplicity.

For multiplicity ranges, <lower_multiplicity> is zero or any positive number specifying the lower end of the accepted multiplicity range. <higher_multiplicity> is a number specifying the higher end of the accepted multiplicity range. It must be higher than <lower_multiplicity>. It can also be * meaning unbounded higher multiplicity.

* multiplicity is equal to the range 0..*.

Type

str

set_tagged_value(name, value, stereotype=None)

Set the tagged value of a stereotype attribute with the given name to value. Optionally the stereotype to consider can be specified. This is needed, if one or more attributes of the same name are defined on the inheritance hierarchy. Then a shadowed attribute can be accessed by specifying its stereotype.

Parameters
  • name – The name of the attribute.

  • value – The new value.

  • stereotype – The optional stereotype on which the attribute is defined.

Returns

None

property source_multiplicity

Getter and setter for the source multiplicity of the association. Syntax is equal to the syntax defined in the documentation of the multiplicity property.

Type

str

property stereotype_instances

Getter to get and setter to set the stereotype instances of this association.

The stereotype instances must be stereotypes extending an association that this association is derived from.

The setter takes a list of stereotype instances or a single stereotype instance as argument. The getter always returns a list.

Type

list[CStereotype]|CStereotype

property stereotypes

The setter takes a single or a list of CStereotype objects which extend this association. Extension is only possible, if this association is an association between meta-classes. The getter returns the stereotypes that extend this association.

Type

CStereotype|list[CStereotype]

property tagged_values

Getter for getting all tagged values of the association using a dict, and setter of setting all tagged values of the association based on a dict. The dict uses key/value pairs. The value types must conform to the types defined for the attributes.

Type

dict[str, value]