10.2 Simple ConstraintsSimple constraints that involve one object, or two objects and their link, provide the foundation for working with more complex constraints that involve multiple objects and their links. 10.2.1 AttributesTo specify a rule about the values of an object's attributes, you can refer to an object's attribute from the object itself by using the name of the attribute. Within the context of an organization, the following expression indicates that the name of an organization may not be an empty string: Name <> '' Using the keyword self, this rule may be described as follows: self.Name <> '' Notice that a literal string is enclosed in single quotes. Figure 10-2 updates Figure 10-1 to show how the rule is captured. Look specifically at the top compartment for Organization. Figure 10-2. Organizations with an OCL expression indicating that organizations must have namesWithin the context of a person, the following expression indicates that a person must work more than 10 hours and less than or equal to 40 hours: HoursAvailable > 10 and HoursAvailable <= 40 Using the keyword self, this rule may be described as follows: self.HoursAvailable > 10 and self.HoursAvailable <= 40 Notice that the keyword self is used at the beginning of each occurrence of the HoursAvailable attribute. Also notice that I can access this attribute even though it is private, because the expression is written within the context of a person. If the context of the rule is not a person object, I would not be able to access the attributes directly in the expression. 10.2.2 OperationsTo specify a rule that uses an object's operations, you can refer to an object's operation from the object itself by using the name of the operation followed by parentheses containing an optional comma-separated list that specifies the actual parameters passed to the operation. If the operation does not have any parameters, the parentheses are mandatory and left empty. Within the context of a project, the following expression uses the validStartEndDates operation to check that the start and end dates are valid: validStartEndDates ( ) Using the keyword self, this rule may be described as follows: self.validStartEndDates ( ) Notice that because the operation returns a Boolean value, I did not have to compare it to anything. If the operation returns a value of True, the rule is satisfied. If the operation returns a value of False, the rule is not satisfied. 10.2.3 Link EndsAs discussed in Chapter 3, a link end is an endpoint of a link that connects the link to an object. To refer to an object across a link, use the rolename attached to the link end. If the link end does not have a rolename, use the class name of the object of the link end, but start the class name with a lowercase character. 10.2.3.1 Link ends with a multiplicity of 1Within the context of a person, the following partial expression returns the organization related to the person using the rolename attached to the link end: self.employer Within the context of a project, the following partial expression returns the organization related to the project using the class name of the object of the link end, but starting with a lowercase character: self.organization In these two expressions referring to a person's organization and a team's organization, because the multiplicity on the association end related to the link end attached to the Organization class has a maximum of 1, the result is an object of the class connected to the link end. That is, each previous expression results in a single object of the Organization class. Also notice that I used the class name to refer to the organization from the project, but I used the rolename to refer to the organization from the person. You can use the class name if no rolename is specified; otherwise, you can use the rolename. To refer to a characteristic of a single object, use a dot. Within the context of a person, the following expression indicates that the name of the organization related to a person cannot be an empty string: self.employer.Name <> '' Within the context of a project, the following expression indicates that the name of the organization related to a project cannot be an empty string: self.organization.Name <> '' 10.2.3.2 Link ends with a multiplicity of more than 1Within the context of an organization, the following partial expression returns the collection of people related to an organization using the rolename attached to the link end: self.employee Within the context of an organization, the following partial expression returns the collection of projects related to an organization using the class name (starting with a lowercase character) of the objects at the link end: self.project In these two expressions referring to an organization's people and an organization's projects, because the multiplicity on the association ends related to the link ends attached to the People class and Project class have no maximum or have a maximum of more than 1, the result is a set collection of objects of the class connected to the link end. Such a set is a collection of unique and unordered objects. That is, the first expression above results in a set of objects of the Person class and the second expression results in a set of objects of the Project class. To refer to a characteristic of a collection, use an arrow (->). Within the context of an organization, the following expression indicates that the names of the people related to an organization may not be an empty string: self.employee->forAll (e : Person | e.Name <> '') The forAll in this expression indicates a rule that must be satisfied by each element of the set of employees. The first part of the expression enclosed in the parentheses indicates that each element of the set is a person that is referenced using the variable e. The second part of the expression, which is separated from the first part by a vertical bar (|), indicates the rule that each element of the set must satisfy. If any employee has an empty name, the result of the forAll is False; otherwise, the result is True. Within the context of an organization, the following expression indicates that the names of the projects related to an organization cannot be an empty string: self.project->forAll (p : Project | p.Name <> '') The forAll in this expression indicates a rule that must be satisfied by each element of the set of projects. The first part of the expression enclosed in the parentheses indicates that each element of the set is a project that is referenced using the variable p. The second part of the expression, which is separated from the first part by a vertical bar (|), indicates the rule that each element of the set must satisfy. If any project has an empty name, the result of the forAll is False; otherwise, the result is True. Within the context of an organization, the following expression indicates that each person who is employed by an organization must work more than 10 hours and less than or equal to 40 hours: self.employee->forAll (e : Person | e.isHoursAvailableInRange (10, 40)) Notice that I must use the isHoursAvailableInRange operation, and that I cannot use the HoursAvailable attribute because it is private to the person objects and not accessible from the context of an organization. 10.2.4 Link ObjectsAs discussed in Chapter 3, a link object is a specific instance of an association that links multiple objects. Given a link and the objects it relates, you can refer to a link object from the objects that are linked, and you can refer to the objects that are linked from the link object. 10.2.4.1 Referring to link objectsTo refer to link objects from an object on one end of a link, use the class name of the link object, but starting with a lowercase character, which results in a set collection of objects of the link's class. Within the context of a team or a person, the following expression indicates that the title of each role object of a team or a person may not be an empty string: self.role->forAll (r : Role | r.Title <> '') Notice that this expression may be used within the context of a team or a person, because it references the links between teams and people. 10.2.4.2 Referring to linked objectTo refer to the object of a link end from a link object, use the rolename attached to the link end. If the link end does not have a rolename, use the class name of the object of the link end, but starting with a lowercase character, which results in exactly one object. Within the context of a role, the following expression indicates that the name of the team to which a role applies may not be an empty string using the class name: self.team.Name <> '' Within the context of a role, the following expression indicates that the name of the person to which a role applies may not be an empty string using the rolename: self.member.Name <> '' Notice that these are two different expressions used within the context of a role, because each expression references a different link end. |