Declarative Objectivity (DO) Language : Expressions : Expressions for Reachable Values
Expressions for Reachable Values
A reachable-value expression returns one or more values that can be reached directly or indirectly from a persistent object.
Syntax 
reachableValueExpression : (source)* (attribute | collectionElement);
source : (attribute '.') | (collectionElement '.')
Quick Look 
Return the reward points for the customer whose last name is Mertz:
FROM Customer WHERE lastName == 'Mertz' RETURN rewardPoints;
Clears just the first element of a particular customer’s collection of rental contracts:
UPDATE Customer WHERE lastName == 'Mertz' CLEAR rentals[0];
Find the vehicle associated with the rental contract whose tracking number is 782, and return the vehicle’s stall number and airport location:
FROM Vehicle WHERE currentRental.trackingNumber == 782 RETURN atStall.number, atStall.location.airportCode; 
Find the first and second pricing category associated with a particular vehicle’s model:
FROM Vehicle WHERE license == 'ABC1997' RETURN model.categories[0:2];
Return the make and model name of vehicle models whose primary category is “luxury”.
FROM VehicleModel WHERE categories[0].name == 'luxury' RETURN brand, modelName;
Return the license from each vehicle in the rental company’s collection of vehicles.
FROM RentalCompany RETURN vehicles.license;
Discussion 
A reachable-value expression returns one or more values that can be reached directly or indirectly from the current context object. You can use such an expression to return:
The value of a direct attribute of the current context object.
Element values of a collection held in a direct attribute of the current context object.
One or more indirect values that can be reached from the current context object, through a chain of referenced objects, embedded objects, or collections of referenced or embedded objects.
Note:A reachable-value expression uses a combination of Selection Operators (dot and subscript operators) to return indirect values and collection-element values.
A reachable-value expression may return a single value or it may return multiple values. Multiple values are typically returned when:
The expression ends in a collectionElement that specifies a range of elements in a collection. The expression returns the elements in the range.
The expression ends in a collectionElement that specifies a predicate for qualifying elements in a collection. The expression returns all elements that qualify.
The expression ends in an attribute, but the source evaluates to a collection of objects. The expression accesses the specified attribute in each object in the collection, and then returns a sequence of the accessed values.
Expressions That Return Direct and Indirect Values
When a reachable-value expression has no source expression, it returns a direct value of the current context object. Direct values are obtained from direct attributes, which defined by the current context object’s class (or a superclass of that class). If a direct attribute holds a collection, the collection’s values are accessed as direct values. For examples, see:
Value of a Direct Attribute
Elements of a Directly Held Collection
When a reachable-value expression includes a source expression, it returns an indirect value of the current context object. An indirect value belongs to an intermediate object that is related in some way to the current context object. The intermediate object could be an embedded object, a referenced object, or an element of a collection of embedded or referenced objects. Or, it could be more distantly related through a chain of embedded or referenced objects that are connected through attributes and collection elements. Just as the relationships among objects may be arbitrarily deep, there is no inherent limit to how indirect the value of interest may be. For examples, see:
Attribute Value of a Referenced Object
Attribute Value of an Embedded Object
Elements of an Indirectly Held Collection
Values from Collected Objects
Value of a Direct Attribute
A simple attribute expression returns the value of a direct attribute of the current context object.
Example. When the following statement processes each Customer, the expression in the WHERE clause evaluates to the string accessed through the Customer’s lastName attribute, and the expression in the RETURN clause returns the numeric value accessed through the Customer’s rewardPoints attribute:
FROM Customer WHERE lastName == 'Mertz' RETURN rewardPoints;
Example. When the following statement processes the RentalCompany, the expression in the CLEAR clause evaluates to the entire collection accessed through the RentalCompany’s highVolumeDates attribute; the statement replaces the entire collection with an empty list:
UPDATE RentalCompany CLEAR highVolumeDates;
Example. When the following statement processes the RentalCompany, the expression in the SET clause evaluates to the entire collection accessed through the RentalCompany’s highVolumeDates attribute; the statement replaces the entire collection with the specified list:
UPDATE RentalCompany SET highVolumeDates TO LIST{2017-01-31, 2017-02-28, 2017-03-31};
Elements of a Directly Held Collection
A simple collection-element expression returns one or more elements of a collection that is directly held by the current context object.
Example. When the following statement processes the qualifying Customer, the expression in the CLEAR clause evaluates to the first element of the list accessed through the Customer’s rentals attribute; the statement clears just that list element:
UPDATE Customer WHERE lastName == 'Mertz' CLEAR rentals[0];
Example. When the following statement processes the RentalCompany, the expression evaluates to a sequence of qualifying elements from the set accessed through the RentalCompany’s vehicles attribute; the statement returns the number of vehicles in the rental company that are currently in service:
FROM RentalCompany RETURN COUNT(vehicles[retired == false]) as NumberActive;
Attribute Value of a Referenced Object
A reachable-value expression may return an attribute value of an object that is referenced by the current context object.
Example. When the following statement processes a Vehicle, the expression in the WHERE clause evaluates to the tracking number of the RentalContract object that is referenced through the Vehicle’s currentRental attribute.
FROM Vehicle WHERE currentRental.trackingNumber == 782 RETURN atStall.number; 
Example. When the following statement processes the qualifying Vehicle, the expression in the RETURN clause digs another level deeper—it returns the airport code of the rental-company location of the stall that has been assigned to the Vehicle:
FROM Vehicle WHERE currentRental.trackingNumber == 782 RETURN atStall.location.airportCode; 
Attribute Value of an Embedded Object
A reachable-value expression may return an attribute value of an object that is embedded in the current context object.
Example. When the following statement processes each Location, the expression in the RETURN clause evaluates to the string accessed through the postalCode attribute of the Address object embedded in the Location. The statement returns the accessed string.
FROM Location RETURN address.postalCode;
Elements of an Indirectly Held Collection
A reachable-value expression may return elements of a collection held by an object that is referenced by or embedded in the current context object.
Example. When the following statement processes the qualifying Vehicle, the expression in the RETURN clause obtains the categories list from the VehicleModel that is referenced through the Vehicle’s model attribute, and then returns the first two elements of that list. The effect of the statement is to indicate the primary and secondary vehicle categories associated with the Vehicle’s model.
FROM Vehicle WHERE license == 'ABC1997' RETURN model.categories[0:2];
Example. When the following statement processes the qualifying Vehicle, the expression in the RETURN clause obtains the events collection from the RentalContract that is referenced through the Vehicle’s currentRental attribute, and then tests the elements of that collection. The effect of the statement is to return just the older rental events associated with the current rental contract of a particular Vehicle:
FROM Vehicle WHERE license == 'ABC1997' RETURN currentRental.events[timestamp < 2017-03-06 21:03:0.0];
Values from Collected Objects
A reachable-value expression may return attribute values from one or more elements of a collection of embedded objects or referenced objects. The collection may be directly or indirectly reachable from the current context object.
Example. When the following statement processes a RentalCompany, the expression in the RETURN clause obtains the vehicles collection from the RentalCompany, and returns a sequence consisting of the license attribute value from each of the Vehicles in the collection.
FROM RentalCompany RETURN vehicles.license;
Example. When the following statement processes each VehicleModel, the expression in the WHERE clause obtains the categories list from the VehicleModel, selects the first VehicleCategory in the list, and then tests the VehicleCategory’s name. The effect of the statement is to return the brand and model name of the vehicle models whose first category is “luxury”.
FROM VehicleModel WHERE categories[0].name == 'luxury' RETURN brand, modelName;
Example. When the following statement processes the RentalCompany, the expression in the RETURN clause obtains the vehicles collection from the RentalCompany, qualifies the elements to access just the Vehicles that are still in service, and then returns a sequence consisting of the pastRentals collection from each of those Vehicles.
FROM RentalCompany RETURN vehicles[retired == false].pastRentals;
Example. When the following statement processes the RentalCompany, the expression in the RETURN clause obtains the vehicles collection from the RentalCompany, and, for each Vehicle in the collection, returns the object identifier of the last RentalContract in Vehicle’s pastRentals collection. The effect of the statement is to identify the last completed rental contract associated with each vehicle in the rental company.
FROM RentalCompany RETURN vehicles.pastRentals[-1];
Example. The following statement is similar to the preceding statement, except that it identifies the last completed rental contract for just the qualifying vehicle in the rental company.
FROM RentalCompany RETURN vehicles[license == 'ABC1997'].pastRentals[-1];