As indicated in the previous section, the main difference between the standard syntax and the business modeling syntax lies in the predefined iterator expressions and the predefined collection operations. The following sections explain the alternative syntax for these two items. The syntax for the other language concepts remains largely the same as the standard syntax. The small number of differences are given in Section C.2.3. C.2.1 IteratorsEvery OCL iterator (loop expression) has the following format according to the standard syntax: <source> -> iterator ( <iters> <body> ) The terms between the < > brackets serve as placeholders, and iterator stands for the name of the iterator from the Standard Library. The placeholder <source> indicates the collection over which to iterate, <iters> stands for the iterator variables , and <body> stands for the body parameter of the iterator. In the BM syntax, each iterator has its own specialized format. Take for example the select iterator. Intuitively, one would say that one selects a thing or things from a set, taking into account any criteria. The BM syntax reflects this manner of speaking, as shown in the following expression, written in the context of LoyaltyProgram : select pp: ProgramPartner from partners where pp.numberOfCustomers > 1000 The corresponding standard syntax is partners->select(pp pp.numberOfCustomers > 1000) The reject and any iterators are written in a similar fashion, using the keywords reject and selectAny , respectively. The following expression, again in the context of LoyaltyProgram , is an example of the BM syntax for the collect iterator: collect p.deliveredServices using p: ProgramPartner from partners It is built according to the intuition that one collects a thing (or things) from a set. To indicate the item to be collected, you may use a reference to an iterator variable, whose name and type are given after the keyword using . The collectNested iterator is written in the same manner, using the keyword collectNested . The BM syntax for the exists and one iterator are built on the intuition that one asks whether an element exists in a set, given some criteria: context CustomerCard inv : exists t: Transaction in transactions where t.date.isBefore( Date::now ) context CustomerCard inv : existsOne t: Transaction in transactions where t.date.isBefore( Date::now ) There still remains the forAll , sortedBy, and isUnique iterators. Examples of the BM syntax for these iterators are as follows (all written in the context Customer ): forall c: CustomerCard in cards isTrue c.valid sort cards using c: CustomerCard by c.goodThru isUnique c.color using c: CustomerCard in cards In Table C-1, you can find the general description of the alternative syntax for each iterator using the placeholders <source>, <iters>, and <body>. As in the standard syntax, the iterator variables may be omitted. In that case, the corresponding keyword is omitted as well. For instance: forall cards isTrue valid sort cards by goodThru isUnique color in cards The more general iterate expression is defined in the standard syntax as: <source> ->iterate( <iters> ; <result> = <initialValue> <body> ) Again, the placeholder <source> indicates the collection over which to iterate; <iters> stands for the iterator varables; and <body> stands for the body of the iterator. Additionally, there are the placeholders <result> and <initialValue>. The first stands for the result variable name and type declaration. The second holds the initial value of the result variable. The general description of the alternative syntax for the iterate expression is included in Table C-1. An example is the following invariant, which is equal to the expression transactions.points->sum() > 0 : context CustomerCard inv : iterate t: Transaction over transactions result myResult : Integer initialValue 0 nextValue myResult + points > 0 Table C-1. Business modeling syntax for predefined iterators
C.2.2 Collection OperationsAccording to the standard syntax, collection operations have the following format: <source> -> operator ( <arg1>, <arg2> ) Again, we use placeholders to indicate parts of the expression. The placeholder <source> indicates the collection over which to iterate. The <arg1> and <arg2> stand for the arguments of the operation, which are both optional. The term operator stands for the name of the collection operation from the Standard Library. All collection operations with no arguments are written as a keyword followed by the collection to which they are applied. The keyword is usually the equivalent of the operation name. The following expressions in the context Customer are examples of the BM syntax for collection operations with no arguments: sizeOf cards isEmpty programs asSequence cards lastOf asSequence cards The BM syntax for collection operations with one argument uses an extra keyword next to the operation name. For example, the including operation is written using the keywords is and includedIn : is self includedIn collect c.owner using c: CustomerCard from cards The union operation uses the keywords unionOf and with : unionOf select c: CustomerCard from cards where c.valid with select c: CustomerCard from cards where not c.valid The BM syntax for collection operations with two arguments uses two extra keywords next to the operation name. One of the keywords separates the arguments. For example, the insertAt operation is written using the keywords insert, at, and in : insert self at 3 in asSequence collect c.owner using c: CustomerCard from cards In Table C-2, you can find the general description of the alternative syntax for each collection operation using the common placeholders. Table C-2. Business modeling syntax of collection operations
C.2.3 Other DifferencesThe local variable definition ( let expression) is one of the expressions that is written slightly differently in the BM syntax. As usual, the difference lies in the use of keywords. The equal sign used to provide the value of the variable is changed into the keyword be , as shown in the following example: context LoyaltyProgram inv: let noc: Integer be collect numberOfCustomers from partners in forall pp: ProgramPartner in partners isTrue noc >= 10.000 Another difference between the standard and BM syntax is the keyword indicating the value of a feature at precondition time. In the standard syntax, it is written as @pre ; in the BM syntax it is written as atPre , as shown here: context Company::hireEmployee(p : Person) post : employees = include p in employees atPre and stockprice() = stockprice atPre + 10 The last difference is the notation of the isSent and message operators. In the standard syntax they are written as ^ and ^^ respectively. In the BM syntax the keywords isSentTo and sentTo are used, and the order of target and message is switched, as shown in the following examples: context File::save() post : forAll b: Builder in self.project.builders isTrue incrementalBuild() isSentTo b context Subject::hasChanged() post : let messages : Sequence(OclMessage) be collect update(? : Integer, ? : Integer) sentTo obs using obs from observers in forAll m in messages isTrue m.i <= m.j |