Pet Store Example
From textuml
See also: TextUML Notation Guide, TextUML Tutorial.
This document presents an example of how to use the TextUML Toolkit and Acceleo for creating UML models and generating code from those models.
Contents |
Setup
Installing the TextUML Toolkit and Acceleo
- Follow the Install Instructions for the TextUML Toolkit (and optionally EclipseGraphviz).
- still using the Software Updates dialog, install the Acceleo 2.3 feature from http://acceleo.org/update/
- again using the Software Updates dialog, install the custom "Acceleo Modules Feature" from http://abstratt.com/update/sandbox/acceleo-modules/
Accept to restart Eclipse to make the changes effective.
Getting the model and generator projects
Download the model and generator project archives and import them into your workspace.
The petstore_models project
The petstore_models project contains all the models shown in this article (including the source in TextUML) for the PetStore application.
The petstore_generator project
The petstore_generator project contains Acceleo code generation chains for the Pet Store application. It relies on the models defined in the petstore_models project.
Following is an explanation of the files in this project:
- generic.chain - the generic chain. Do NOT try to launch this chain directly, it will not work. Please use one of the concrete chains instead (see below). .
- petstore.chain - the chain that applies all templates to all UML models in the petstore_generator project.
- cart.chain - applies the generic.chain to the cart.uml model only
- order.chain - applies the generic.chain to the order.uml model only
- clear-src.xml - an Ant script that deletes the contents of the src folder. Be careful when using this script as it will delete any changes you made manually to the source directory. Run it in the same JRE as the workspace so the project contents are refreshed.
- custom_gen.properties - a properties file that customizes the code generation.
Models
Domain model
Product catalog
package catalog;
apply extensions;
import datatypes;
[Entity]
class Product
attribute mnemonic : String;
attribute name : String;
attribute description : String;
[Factory]
static operation newProduct(mnemonic : String, name : String) : Product;
end;
end.
Inventory
package inventory;
apply extensions;
import datatypes;
import catalog;
[Entity]
class Item
attribute mnemonic : String;
attribute listPrice : Decimal;
attribute unitCost : Decimal;
attribute stock : Integer;
reference product : Product[1];
[Factory]
static operation newItem(product : Product) : Item;
end;
end.
Customer
package customer;
apply extensions;
import datatypes;
[Entity]
class Customer
attribute firstName : String;
attribute lastName : String;
reference addresses : Address[*];
end;
[Entity]
class Address
attribute addressLine1 : String;
attribute addressLine2 : String;
attribute city : String;
attribute province : String;
attribute country : String;
end;
end.
Cart
package cart;
apply extensions;
import inventory;
[Entity]
class Cart
[Factory]
operation newItem(catalogItem : Item, quantity : Integer) : CartItem;
operation removeItem(cartItem : CartItem);
operation getItems() : CartItem[*];
operation getItemCount() : Integer;
operation hasItems() : Boolean;
operation getTotal() : Decimal;
operation checkOut() : order::Order;
end;
composition CartHasItem
role items : CartItem[*];
navigable role cart : Cart[1];
end;
[Entity]
class CartItem
attribute quantity : Integer;
[Factory]
static operation newCartItem(catalogItem : Item, quantity : Integer) : CartItem;
operation getUnitCost() : Decimal;
operation getTotalCost() : Decimal;
operation getCatalogItem() : Item;
end;
association CartItemToCatalogItem
navigable role cartItem : CartItem[*];
role catalogItem : Item[1];
end;
end.
Payment
package payment;
apply extensions;
import customer;
[Entity]
class Title
end;
association TitlePayment
navigable role payment : Payment[*];
role title : Title;
end;
[Entity]
class Payment
attribute amount : Decimal;
attribute paymentDate : DateTime;
reference method : PaymentMethod;
reference payer : Customer;
end;
[Entity]
class PaymentMethod
operation performPayment();
end;
[Entity]
class Cheque specializes PaymentMethod
end;
[Entity]
class Paypal specializes PaymentMethod
end;
[Entity]
class CreditCard specializes PaymentMethod
attribute cardType : CreditCardType[1];
attribute cardNumber : String;
attribute expiryDate : DateTime;
end;
enumeration CreditCardType
Visa, AmericanExpress, Diners, MasterCard
end;
end.
Order
package order;
apply extensions;
import customer;
import datatypes;
import payment;
[Entity]
class Order specializes Title
attribute orderNumber : String;
attribute orderDate : DateTime;
attribute items : OrderItem[*];
reference customer : Customer;
reference paymentAddress : Address;
reference deliveryAddress : Address;
end;
composition
role Order.items;
role order : Order;
end;
[Entity]
class OrderItem
reference product : inventory::Product;
attribute quantity : Integer;
attribute unitPrice : Integer;
end;
end.
Profiles and basic data types
Basic data types
(* Basic data types. *) package datatypes; apply javaProfile; [javaType(name="java.lang.Double")] class Decimal end; [javaType(name="java.lang.Integer")] class Integer end; [javaType(name="java.util.Date")] class DateTime end; [javaType(name="java.lang.Boolean")] class Boolean end; [javaType(name="java.lang.String")] class String end; end.
Java Type Profile
profile javaProfile;
import UMLPrimitiveTypes;
stereotype javaPackage extends uml::Package
property base : String;
end;
stereotype javaType extends uml::Class
property name : String;
end;
end.
Extensions
profile extensions; import uml; (* Marks operations that create objects. *) stereotype Factory extends Operation end; stereotype Entity extends Class end; end.
Generating code
To generate code, right-click the chain of interest (for instance, petstore.chain) and select the Launch action from the context menu. Any code generated should appear under the src folder. Code generation should create Java model classes and Hibernate mapping files under the petstore_generator project's src folder. After making changes to the models, you can run the chain again.
Documentation
See the TextUML Notation Guide and TextUML Tutorial for information on how to create UML models using the TextUML notation.
See the Acceleo documentation for information on how to use Acceleo's code generation capabilities.
You can also get help on the TextUML Toolkit support forum.
Notes
- Acceleo is an Obeo registered trademark
- The Acceleo modules used for generating code are customizations of work originally published at the Acceleo.org module repository.
- This Pet Store sample application is inspired by Sun's Java Pet Store demo application









