Aspects

In AspectMatlab, aspects are defined using a syntax similar to MATLAB classes. A MATLAB class typically contains properties, methods and events. As in other object-oriented languages, properties in MATLAB class encapsulate the data that belongs to instances of classes, which can be assigned default values, initialized in class constructors, and used throughout the class. Data contained in properties can be declared public, protected, or private. This data can be a fixed set of constant values, or it can be dependent on other values and calculated only when queried. Different attributes can be applied over a block of properties and property-specific access methods can be specified.

Encapsulation using methods is also a familiar concept in an object-oriented systems. MATLAB class methods are a little different as they act as an enclosing block, which can host a variety of functions. Common types of methods are ordinary functions, class constructors, class destructors and property access functions. Method blocks can be configured with different attributes, including access specifiers.

Listing 1 shows a typical MATLAB class, myClass, which can be used as a simple counter. This class declares a property, count, which has default scalar value 0. The counting functionality is provided through two functions. incCount increments the counter and getCount can be used to query the current value of count, which is returned through variable out. One important point to notice here is that MATLAB class methods always have the calling object automatically passed as the first argument.


\begin{lstlisting}[language=MATLAB, frame=htbp, caption={A typical {\sc Matlab}\...
...s)
out = this.count;
end
\par
end %methods
\par
end %classdef
\end{lstlisting}

In this section, we outline the grammar of AspectMatlab in pieces as we go through related concepts and constructs. If you have a coloured version of this document, you will see that all references to productions in the McLab implementaion of the base MATLAB grammar, are given in red.

As shown in Figure 1, the base McLab program rule is extended to include aspects, along with functions, scripts and classes, as a program entity. Just like a MATLAB class structure, an aspect is named and contains a body. An aspect retains the properties and methods constructs, while adding two aspect-related constructs: patterns and actions. Patterns are formally known as pointcuts in AspectJ and are used as picking out certain join points in the program flow. AspectMatlab actions correspond to AspectJ advice, which essentially is a block of code intended to be executed at certain points in the program. This choice of terminology was intended to convey that patterns specify where to match and actions specify what to do.

Figure 1: Syntax of an Aspect
\begin{figure}\begin{grammar}[(colon){ ::$\Rightarrow$}]
[(semicolon){\\ \hspac...
... <actions\_block>,<\textcolor{red}{stmt\_separator}>.
\end{grammar}
\end{figure}

Moreover, it is important to explain the stmt_separator non-terminal, imported from McLab. Unlike other high level languages, a MATLAB statement can be terminated in multiple ways. These statement separators include the new-line, a comma or a semi-colon.

With the addition of patterns and actions, Listing 2 shows an extension to the class presented in Listing 1. myAspect counts how many times a function named foo is invoked. To achieve this functionality, we first define a pattern. Pattern callFoo provides us the way to specify the target join points. Once we match such join points in the source code, then we can call the corresponding action, actCall. This action triggers before the call to function foo and increments the counter.


\begin{lstlisting}[language=MATLAB, frame=htbp, caption={A typical aspect exampl...
...s
actCall : before callFoo
this.incCount();
end
end
\par
end
\end{lstlisting}

In the compiled code, an aspect is transformed into a class and the actions are translated into corresponding methods of the resulting class. As described earlier, MATLAB class methods have the invoking class object as an argument. So the methods created out of actions are also provided that object, which we named this for the purposes of clarity and consistency. Inside an action body, this should be used to interact with the properties and methods for the specific object.

We present a detailed discussion on patterns and actions in the following sections.

Toheed ASLAM 2010-04-24