Document Sample
Tutorial Powered By Docstoc
					 Aspect-Oriented Software
  Development (AOSD)
        Tutorial #7

Assume – guarantee specifications;
Today: Aspects Categories (contd.),
         LTL properties
• Assume – guarantee aspects specification
• Event-based Aspect Oriented Programming
• Examples

                Aspect-Oriented Software     2
                 Development (236608)
    Example Class: Point - reminder
class Point {
   private int x, y;

    public Point(int x, int y) { this.x = x; this.y = y; }

    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }
    public void MoveTo(Point p) {setX(p.x); setY(p.y); }

    public int getX() { return x; }
    public int getY() { return y; }

                              Aspect-Oriented Software       3
                               Development (236608)
      Assume-guarantee spec. 1
Aspect 1: Positive quarter check. After each
 change in points, check whether they are in
 the positive quarter of the space
pointcut movePoint… //calls to setX(), setY()…
pointcut createPoint …//calls to Point(..)

after(Point pt, int newArg) returning(): movePoint(pt, newArg) {
         if( newArg < 0) System.out.println(“…”);
  after(int x, int y) returning(): createPoint(x, y) {
         if( x < 0 || y < 0) System.out.println(“…”);
                        Aspect-Oriented Software                   4
                         Development (236608)
Assume-guarantee spec. 1 – contd.
The coordinates of a point can not be changed
 without calling the constructor or the setX(),
 setY() functions
Whenever a point appears outside the positive
 quarter of the space, a warning message is

                 Aspect-Oriented Software     5
                  Development (236608)
       Assume-guarantee spec. 2
Aspect 2: Positive quarter enforce. Make sure all the
  points are in the positive quarter of the space
void around(Point pt, int newArg): movePoint(pt, newArg) {
        if( newArg >= 0) proceed(pt, newArg);
        else System.out.println(“…”);

  Point around(int x, int y) : createPoint(x, y) {
        if( x < 0) { x=0; System.out.println(“…”);}
        if( y < 0) { y=0; System.out.println(“…”);}
                         Aspect-Oriented Software            6
                          Development (236608)
Assume-guarantee spec. 2 – contd.
The coordinates of a point can not be changed
  without calling the constructor or the setX(),
  setY() functions
Points never appear outside the positive
  quarter of the space

                  Aspect-Oriented Software     7
                   Development (236608)
       Assume-guarantee spec. 3
Aspect3: Adding names to points; tracing
private String Point.name = "";
public String Point.getName() {return name;}
public void Point.setName(String newName) {name = newName;}
 pointcut moved(Point pt): target(pt) &&
       (call(void setX(int)) || call(void setY(int)));

after(Point pt) returning: moved (pt) {
        System.out.println("Point "+pt.getName()+" moved to
                         Aspect-Oriented Software             8
                          Development (236608)
Assume-guarantee spec. 3 – contd.
• The coordinates of an existing point can not be
  changed without calling the setX(), setY()
• Class Point has no “name” field and no
  getName(), setName(String ...) functions
• Point has the “name” field and getName(),
  setName(String ...) functions
• Each change of existing points coordinates is
  reported to the user
                    Aspect-Oriented Software        9
                     Development (236608)
E-Commerce Example - Reminder
• Bingo (half-price for every 1000-th client)
• Discount (10% discount after accumulated
  purchase value > 30)
• Profiling (counts bingos / discounts given)
• Checkout (abstract, implemented by Bingo
  and Discount)

                 Aspect-Oriented Software       10
                  Development (236608)
       Example – Aspects of Aspects
    Profiling of discounts is an example of an aspect which is
    applied to other aspects. Have a look at the definition of the
    aspect Profiling, an excerpt of which is shown here:
while (true) {                                      where should
       Event e = nextComputeDiscount();          computeDiscount(..)
                                                         Bingo              Discount

       this.isCrosscutting = true;                  be defined?
       n++;                                                     Profiling
    Event nextComputeDiscount() { profile: "
        System.out.println("aspect                meaning: the aspect
         boolean ok = false;
            + "number of discounts and bingos = " + n);
        Event e = null;                            declares that is has
        while (!ok) {                              been applied to the
    In the = nextEvent(); of calls to method computeDiscount are
              loop, events                             current event
           ok = (e instanceof MethodCall) &&
    counted, a method which is defined in the abstract aspect
    Checkout and inherited by the Bingo and Discount aspects.
       this.isCrosscutting = true;
               return e;
                              Aspect-Oriented Software                       11
                               Development (236608)
Example – Dynamic Instantiation of
       Aspects and Events
  While we have already seen examples of dynamic event creation
  during runtime, and aspect creation at program start time, the
  Discount aspect provides a more interesting example of
  dynamic instantiation of aspects.
  Let's have another look at an excerpt of this aspect:

                          Aspect-Oriented Software             12
                           Development (236608)
        Example – Dynamic instantiation of aspects
  In order to express that discounts are applied
  on a per-customer basis, we use dynamic
                                                       We wait for the next
  instantiation of aspects:
                                                       customer creation,
 public void definition() {
      Customer cust = newCustomer();                         Dynamically create a new instance
                                                             of the discount aspect
      insert(new Discount());
      while (true) {                                         And insert it into the tree
            Order o = nextShip(cust);                        representing the collection of
            accPurchases += o.total();                       aspects associated to the monitor
            System.out.println("aspect discount: accumulated purchases of "
            + cust + " is " + accPurchases);
            if (accPurchases > Discount.discountThreshold) {
                    Event e2 = nextCheckout(cust);
                    System.out.print("aspect discount: ");
We have
already seen        float discount = computeDiscount(Discount.discountRate,cust);
event               Set tmp = new HashSet();
instantiation       tmp.add(new Product("discount", -discount));
                    accPurchases -= Discount.discountThreshold;
       }                   This new instance is then ready to manage the
                           new customer                                               13
             Example – Possible Output
Welcome to www.javazon.com
  shipping p2 to c2
           aspect discount: accumulated purchases of c1 is 40.0
  shipping p1 to c1
           aspect profile: number of discounts and bingos = 2
  apply discount of 10% to c2
           aspect profile: number of discounts and bingos = 3
  apply discount of 25% (i.e.5.0) to c1                           Discount before
  ...                                                              profiling? Or
                                                                  profiling before
  This demonstrates:                                                 discount?
         The different discounts crosscut the execution of the base
         The profiling aspect applies to the discounting aspects.
         The profiling action is performed before discounting.
                                       Aspect-Oriented Software              14
                                        Development (236608)
Adding Event-based Aspects to Point
• Which functions should be instrumented?
setX(..), setY(..), Point(..)
• Events in aspect pointcuts: “call” or “return”?
Positive quarter check:
  return of SetX, SetY, Point
Positive quarter enforce:
  call and return of SetX, SetY, Point
Name adding and movements tracking:
  return of SetX, SetY; call and return of Point

                  Aspect-Oriented Software          15
                   Development (236608)
  Aspect Composition Example
Aspects on class Point:
C: Positive quarter check;
E: Positive quarter enforce;
N: Name adding and movements tracking

                 Seq                          N

       E_m                    Cond

                 C                          E_c
                 Aspect-Oriented Software         16
                  Development (236608)

Shared By: