Building Applications

 

In this section we build a very simple application that uses the coordination contracts API to dynamically reconfigure the interactions between customers and accounts.

The API Definition

The API of a contract consists in a set of methods to create and remove contract instances, set and get the attributes' values, and to get the participant objects. Each contract is compiled into a class with the same name in a package cde.contracts. We now list the available methods, showing the concrete API for the Credit contract.

Notice that the names of attributes and participants are exactly as written in the contract definition. No changes in capitalization are made by the CDE. This means, in our example, that the method to get the credit limit is not called getLimit

The CDE editor allows you to browse through the generated files. To see the Traditional API, just double-click on the File:Traditional.java node in the project tree (under the Generated Files of the Traditional contract).

Using the API

The toy application built to illustrate the Credit and Traditional APIs is a single class (to keep things simple) that manages the customers and accounts belonging to a bank agency. The complete application would allow the creation and deletion of customers and accounts, and changes of ownership, but as we are interested only in the operations on contracts, the interaction with the user is limited to setting the credit limit between a given customer and a given account, and to withdrawing and depositing money. 

The application is in file Agency.java in the source directory. It is written in a very straightforward, unsophisticated way, to keep it very simple and focus only on the important issues. The noteworthy methods of the class are the following:

To compile and run the application, do as follows:

  1. Open file Agency.java in the src/Bank directory by pressing Ctrl-O or selecting File->Open File... .

  2. Select Project->Compile->Java Compiler.

  3. In Windows, open a Command prompt, go to the application directory (i.e., demos/tour) and execute the command
    java -cp "classes;CDE" Bank.Agency
    where CDE is the CDE runtime library that you have indicated in the project options.

The application comes with two pre-defined customers, named Mary and John, and accounts, numbered 1 and 2. Mary owns both accounts, John only owns account 2. All contracts are of Traditional type, and the accounts have zero balance. After this initialization, main enters a loop asking the user to withdraw or deposit money, or to change the credit limit of one customer/account relation. In both cases, the user has to enter the customer and the account numbers. 

You can now play around with the application, seeing how it keeps the following invariants:

  1. There is at most one contract between each customer/account pair.

  2. The limit of Credit contracts is always positive.

Here is a sample scenario for you to try out. List the database after each operation to see how it changes.

  1. Set the limit of the contract between Mary and account 1 to 200 Euro.

  2. Let John withdraw 100 Euro from account 1.

  3. Let Mary withdraw 100 Euro from account 1.

  4. Let Mary withdraw 100 Euro from account 2.

  5. Let John withdraw 100 Euro from account 2.

  6. Repeat step 3 two more times.

  7. Let John deposit -100 Euro in account 1.

  8. Set the credit between John and account 1 to -50 Euro.

Congratulations! You have completed the Guided Tour, and we hope you feel confident in using contracts and the CDE for your own applications.  

Exercises

Here are some exercises for you to further use the tool and to deepen your understanding of coordination contracts. 

Top    CDE Documentation Home