The Object Constraint Language: Getting Your Models Ready for MDA (2nd Edition)
Implementing UML model elements means implementing user -defined classes, datatypes, and so on. About the implementation of these types we can be short: Use the manner of implementation you prefer. The only thing you need to take care with, is maintaining the connection with the implementation of the OCL expressions in step 3. For instance, if an association is implemented by two class members , each placed in one of the classes at both sides of the association, the implementation of an OCL expression that refers to the association must refer to the right class member. In this chapter, we assume that the following rules are used to implement user-defined types. Our R&L example has no components or datatypes; therefore, no rule for implementing them is given. In practice, they follow rules similar to the ones given here: Classes Every user-defined class is implemented in one Java class. Operations Every operation on the class in the model is implemented by one operation in the Java class. Attributes Every attribute (private, protected, or public) of the class in the model is implemented by a private class member and a get and set operation in the Java class. The visibility of the operations is identical to the visibility of the corresponding attribute in the model. If the attribute is marked read-only , the set operation is omitted. The get operation always has the form AttributeType getAttributename() . The set operation always has the form void setAttributename (AttributeType newValue). Associations Every association end of the class in the model is implemented by a private class member and a get and set operation in the Java class. The name of the class member is the name of the role at that end. Again, the visibility of the operations is identical to the visibility of the corresponding association end in the model. If the multiplicity of the association end is greater than one, the type of the class member is either the Java implementation of the OCL Set or OrderedSet , depending on the ordering of the association. In this case, an add and remove operation are added to the implementation of the contextual type, which can add or remove an element from the class member. States Every state defined in a statechart for a class is implemented by a boolean class member. Additional invariants are added to ensure that an instance of the class can be in only one state at a time. When a state has substates, the invariants are adjusted to accommodate them. The class member representing the parent state and one of the class members representing the substates may be true at the same time. Events Every event defined in a statechart for that class is implemented by an operation with the same name. This operation implements the reaction of the instance on the event. The reaction must take into account the state of the instance, any conditions on transitions that are triggered by the event, and any actions connected to the transitions or to the begin and end state of the transitions. In other words, we assume that a statechart has a protocol interpretation. Pre- and postconditions of the event operations are inferred from the statechart in terms of the state members of the class. Enumerations Ever y enumeration type is implemented by one Java class, holding static public class members for each value. Interfaces Every interface is implemented by one Java interface. As an example, the implementation of the CustomerCard class from the R&L system is provided in Appendix D. |