Because an Executable UML model captures the information inherent in a domain, redundant attributes are generally frowned upon. However, there are occasions when a clear understanding of a domain is best served by capturing the dependency between attributes explicitly. Definition: A derived attribute is an attribute whose value can be computed from other attributes already in the model. Figure 8.9 shows two derived attributes, each prefixed by a / (slash). Figure 8.9. Derived AttributesThe attribute ProductSelection.selectionValue can be defined in OCL like this:
or in action language this way:
Executable UML enforces this constraint by prohibiting writing to the attribute totalSelectionPrice and by requiring any model compiler to guarantee the constraint before the attribute can be read. Derived attribute dependencies can span multiple classes. Figure 8.9 also shows how the class Order allows a customer to purchase several different products in one transaction. Each product is represented as a distinct ProductSelection. In this example, Order.totalOrderAmount is derived from the sum of all the associated ProductSelection.totalSelectionPrice. Its definition in OCL looks like this:
This derived attribute definition is written in action language, as shown below.
Unlike the identifier idiom, which can be represented by tags, the derived attribute marker (/) indicates the presence of a constraint that must be specified.
|