Loop Patterns

The original AspectJ language definition did not contain any loop-related pointcuts. In MATLAB, loops are extensively used and having the ability to cross-cut the loops is equally important in such a language. AspectMatlab provides a range of poincuts for loops: $loop$, $loopbody$ and $loophead$.

As shown in Figure 6, the $loop$ join point presents only an outside view of the loop; because the points before and after the loop are not within the loop itself. For some applications it might be desirable to advise the loop body. Also, the loop iterators are good candidates to be advised. Because in MATLAB, loop headers are evaluated completely before the loop itself. So the $loophead$ join point is not contained inside the $loop$ join point.

Figure 6: Loop Join Points
\includegraphics[width=3in]{images/loop.eps}

In aspect-oriented systems, the means of selection for a join point is, in most cases, ultimately based on the naming of some source element characterising the join point, possibly using a regular expression. For example, to advise a method call or a group of methods, the pointcut has to contain an explicit reference to some names characterising the method signatures, for instance, a pattern matching the name of the methods. Since loops can not be named in MATLAB, a name-based pattern to write a pointcut that would select a particular loop will not work.

If it is known for certain that all the loops within a function are to be advised, it would be possible in AspectMatlab to use certain scope-related pattern to restrict the loop pattern to all the loops contained in the functions picked up in the restricted scope. However, selecting only one of several loops within the same function turns out to be impossible without any further mechanism. So for the sake of loops identification, we decided to use the loop iterator variables to match a loop pattern.

Examples of simple loop patterns are given in Listing 6. All three patterns will match on all the loops, either for or while, which iterate on variable i.


\begin{lstlisting}[language=MATLAB, frame=htbp, caption={Loop Patterns}, label=e...
...p(i);
pLoopHeadI : loophead(i);
pLoopBodyI : loopbody(i);
end
\end{lstlisting}

For example, consider the two loops shown in Listing 7, where both display the numbers from 1 to 10. Both loops match the patterns given in Listing 6.


\begin{lstlisting}[language=MATLAB, frame=htbp, caption={Example of loop pattern...
...0
disp(i);
end
\par
i=1;
while (i<=10)
disp(i);
i = i+1;
end
\end{lstlisting}

Toheed ASLAM 2010-04-24