Download presentation
Presentation is loading. Please wait.
1
Aspect-Oriented Software Development (AOSD) Tutorial #7 Assume – guarantee specifications; EAOP
2
Aspect-Oriented Software Development (236608) 2 Today: Aspects Categories (contd.), LTL properties Assume – guarantee aspects specification Event-based Aspect Oriented Programming Examples
3
Aspect-Oriented Software Development (236608) 3 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; } }
4
Aspect-Oriented Software Development (236608) 4 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( “…” ); }
5
Aspect-Oriented Software Development (236608) 5 Assume-guarantee spec. 1 – contd. Assumption: The coordinates of a point can not be changed without calling the constructor or the setX(), setY() functions Guarantee: Whenever a point appears outside the positive quarter of the space, a warning message is printed
6
Aspect-Oriented Software Development (236608) 6 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( “…” );} proceed(x,y); }
7
Aspect-Oriented Software Development (236608) 7 Assume-guarantee spec. 2 – contd. Assumption: The coordinates of a point can not be changed without calling the constructor or the setX(), setY() functions Guarantee: Points never appear outside the positive quarter of the space
8
Aspect-Oriented Software Development (236608) 8 Assume-guarantee spec. 3 Aspect3: Adding names to points; tracing movements 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 ("+pt.getX()+","+pt.getY()+")"); }
9
Aspect-Oriented Software Development (236608) 9 Assume-guarantee spec. 3 – contd. Assumption: The coordinates of an existing point can not be changed without calling the setX(), setY() functions Class Point has no “name” field and no getName(), setName(String...) functions Guarantee: Point has the “name” field and getName(), setName(String...) functions Each change of existing points coordinates is reported to the user
10
Aspect-Oriented Software Development (236608) 10 E-Commerce Example - Reminder Aspects: 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)
11
Aspect-Oriented Software Development (236608) 11 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) { Event e = nextComputeDiscount(); this.isCrosscutting = true; n++; System.out.println("aspect profile: " + "number of discounts and bingos = " + n); } In the loop, events of calls to method computeDiscount are counted, a method which is defined in the abstract aspect Checkout and inherited by the Bingo and Discount aspects. Profiling BingoDiscount Event nextComputeDiscount() { boolean ok = false; Event e = null; while (!ok) { e = nextEvent(); ok = (e instanceof MethodCall) && ((MethodCall)e).method.getName().equals("computeDiscount"); } this.isCrosscutting = true; return e; } where should computeDiscount(..) be defined? meaning: the aspect declares that is has been applied to the current event
12
Aspect-Oriented Software Development (236608) 12 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:
13
13 Example – Dynamic instantiation of aspects public void definition() { Customer cust = newCustomer(); insert(new Discount()); while (true) { Order o = nextShip(cust); accPurchases += o.total(); System.out.println("aspect discount: accumulated purchases of " + cust + " is " + accPurchases); if (accPurchases > Discount.discountThreshold) { Event e2 = nextCheckout(cust); System.out.print("aspect discount: "); float discount = computeDiscount(Discount.discountRate,cust); Set tmp = new HashSet(); tmp.add(new Product("discount", -discount)); cust.shoppingCart.add(tmp); accPurchases -= Discount.discountThreshold; } In order to express that discounts are applied on a per-customer basis, we use dynamic instantiation of aspects: We wait for the next customer creation, Dynamically create a new instance of the discount aspect And insert it into the tree representing the collection of aspects associated to the monitor This new instance is then ready to manage the new customer We have already seen event instantiation
14
Aspect-Oriented Software Development (236608) 14 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... This demonstrates: The different discounts crosscut the execution of the base program. The profiling aspect applies to the discounting aspects. The profiling action is performed before discounting. Discount before profiling? Or profiling before discount?
15
Aspect-Oriented Software Development (236608) 15 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
16
Aspect-Oriented Software Development (236608) 16 Aspect Composition Example Aspects on class Point: C: Positive quarter check; E: Positive quarter enforce; N: Name adding and movements tracking Seq N Cond C E_c Seq E_m
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.