Re: [abc-users] Tracematches local variable tracking

From: Pavel Avgustinov <pavel.avgustinov_at_magd.ox.ac.uk>
Date: Wed, 20 Feb 2008 15:24:24 +0000

Hi Alan,

Indeed I don't think doing exactly what you describe is possible. Essentially
you are asking for side effects while matching symbols to the program
trace -- whenever a matching event occurs, you would like to update the
internal 'account balance' counter.

Tracematches aim to make the matching process completely declarative, and so
this is not possible.

Note that if you were only interested in a fixed, finite number of events, you
could sort of emulate this: You could define, for your pattern,
symbols "wd", "dp1" and "dp2". Giving the tracematch three more int-typed
formal parameters, you could make each symbol bind the amount with an args()
pointcut to one of the variables, and then in the tracematch body add them
all up in the appropriate way.

Still, it is not possible to do this for arbitrary traces containing
withdrawals and deposits, as you don't know how many intermediate variables
you'll need. For such a side-effect dependent computation, you'd probably be
better off defining standard AspectJ after advice on the withdraw() and
deposit() methods, which can appropriately update (say) an intertype-declared
field on Account containing the balance of the transactions.

HTH,
- P

On Wednesday 20 February 2008 15:11:58 Alan Teoh wrote:
> Hi again,
>
> I was wondering if I can just use a tracematch alone to track the value
> of a local variable across throughout the trace itself To illustrate the
> problem, I have the following code:
>
> class Account {
> private int type; // Integer to determine account type
>
> public Account(int t) {
> this.type = t;
> }
>
> public void withdraw(double amount) {
> System.out.println("Withdraw");
> }
>
> public void deposit(double amount) {
> System.out.println("Deposit");
> }
>
> public static void main(String[] args) {
> Account a1 = new Account(1);
> Account a2 = new Account(1);
>
> a1.withdraw(5);
> a1.deposit(2);
> a2.withdraw(2);
> a1.deposit(6); // Match to occur here
> a1.deposit(1);
> }
> }
>
> public aspect AccountTM {
> tracematch (Account a) {
> sym wd after:
> call (* Account.withdraw(..)) && target(a);
> sym dp after:
> call (* Account.deposit(..)) && target(a);
>
> wd dp[2]
> {
> System.out.println("1 withdraw and 2 deposit method calls
> made by " + a);
> }
> }
> }
>
> As we can see the tracematch matches 1 withdraw() and 2 deposit() method
> calls on a given object. My question is am I able to use just one
> tracematch to track the value of "amount"? i.e. given the following calls
> a1.withdraw(5);
> a1.deposit(2);
> a2.withdraw(2);
> a1.deposit(6);
> a1.deposit(1);
>
> As the regular expression matches the executions made by a1, I would
> like to be able to keep track of the amount that was withdrawn and
> deposited. So for that example above, I would have -5 + 2 + 6 = 3 as the
> total amount withdrawn and deposited throughout the execution of a1.
>
> I have ideas over how to solve this, but I was wondering if I could do
> it all using tracematches only? I was more inclined towards thinking
> that it is not possible given the grammar specified for tracematches.
>
> Thanks.
Received on Wed Feb 20 2008 - 15:24:37 GMT

This archive was generated by hypermail 2.2.0 : Wed Feb 20 2008 - 16:20:11 GMT