The Credit Contract

 

In this subsection we create from scratch the Credit contract, which allows the participating customer to overdraw the participating account up to a limit, given by a local variable of the contract.

Because this contract is very similar to Traditional, it is a good exercise to be the first contract written by you. The steps you will have to take are the following:

  1. Select New->Contract in the File menu and then, in the file dialog that appears, choose the source directory and provide the file name Credit.ctr. A new file edition window appears in the right pane with a coordination contract template.

  2. Write the contract using the template as a syntax guide. You should try to do this without looking at the Traditional contract. See below for some explanation on the template.

  3. Compile the contract as explained in the previous subsection, i.e., selecting Project->Compile->Contract Compiler.

  4. Repeat steps 2 and 3 until no errors remain.

The template that appears in step 1 shows all the syntax for coordination contracts. For the moment, we only need the constructs we used for the Traditional contract and attributes. The attributes section is necessary for contracts like Credit, which need to hold specific values, in this case the overdraft limit. The actual values for the attributes are provided when a contract instance between actual participant objects is created. The values may be changed at run-time as shown in Testing Contracts. Note that attributes are declared as in Java, and hence not checked by the CDE compiler, but participants are declared in a Pascal-like style. Also, make sure you do not forget the semicolon used to terminate the rule. Double-check that each identifier used in the Java expressions and code is either: 

When you don't remember the exact name of a variable or method of a component, you have two choices:

  1. Switch to the editor window of the component, by clicking on the corresponding tab in the right pane. For our example, it would be the Customer.java or Account.java windows, which we already opened in a previous subsection. Then scroll in the window until you find the intended method or variable, double-click the identifier, copy it, switch back to the contract window and paste it in the desired position. Copy and paste is done as in other Window applications, either by pressing Ctrl-C and Ctrl-V, or by clicking the familiar icons in the toolbar. The edit window context menu also contains the copy and paste commands.

  2. Expand the component nodes in the project tree on the left pane. Each component node has one child node for the attributes and one child for the operations. These two nodes further expand into one node for each attribute and operation. There is a further level for the operations, showing the names of the parameters, as illustrated next. Notice that only public fields are shown (except constructors). If a class has no public attributes or no public methods, the corresponding subtree does not exist.

The second method has the advantage of requiring less navigation through windows and code; one can see the contract being edited and the names of the desired attributes and operations of components at the same time. The disadvantage is that it is not possible to copy node identifiers from the tree and paste them into the contract file. However, if you click on a field node, the current edit window will be updated to show the corresponding line of the corresponding file. Now you can proceed as in the first method.

Now you have all information you need to write your Credit contract, by replacing the placeholders in the contract template. In case you're still stuck, here is some more help:

  1. Delete from the contract template the operations, before, do, and after parts, and the state condition rule template at the end of the contract.

  2. Declare two participants, an account and a customer, and one attribute of type double hold the credit limit.

  3. Replace TriggerRuleName by any name of your liking.

  4. Write the trigger (i.e., the conditions after the when keyword) such that it intercepts only calls from the participating customer to the withdrawal operation of the participating account.

  5. Replace JavaGuardConditions by a boolean expression that checks whether the amount to be withdrawn is less than the current balance plus the credit limit.

  6. In the failure section, throw the LIMIT_EXCEEDED exception. You might wish to open the AccountException.java file to see the exact arguments passed to the exception constructor. (Of course, you can also copy the failure part from the Traditional contract, but that would be cheating :) because you are not supposed to look at it while you write the Credit contract.)

Once you have written the Credit contract, check if it is similar to the one we provide. For the remaining of the guide, we will need the Credit contract to use the exact identifiers (for participants, attributes, and rules) we have used in our version. Instead of editing your contract to match ours, it is easier to replace it by the provided version (and along the way you will learn another CDE command).

  1. Make sure that the Credit.ctr window is the currently visible one. Select Save As... from the File menu and save the contract as MyCredit.ctr in the source directory, where it was (demos/tour/src). 

  2. Right-click on the Credit contract in the project view, and select Delete from the context menu to eliminate it from the project.

  3. Use File->Open File... to open file Credit.ctr in directory docs/Sources/tour.

  4. Use File->Save As... to save the file with the same name, but in the demos/tour/src directory.

  5. Close the Credit edit window. 

  6. Use File->Add->Add Contract... to add the Credit contract from the source directory to the project.

  7. Generate the Java files using any of the methods given in the previous section (e.g., click on the Credit node and then on the generation icon in the toolbar).

We now have all the components and contracts for the banking application. However, we will first create another contract just for pedagogical purposes.

Top    CDE Documentation Home    Next