CSC/ECE 517 Fall 2010/ch3 3d AI: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(39 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Introduction==
==Introduction==
Aspect Oriented Programming is a programming style which isolates the business logic of the program from its secondary or supporting functions. The basis of the aspect oriented programming is to add a third dimension to pre-existing dimensions of the Procedural Programming and Object Oriented Programming, by describing aspects that cross-cut the structure of the program.
Aspect Oriented Programming [http://en.wikipedia.org/wiki/Aspect-oriented_programming 1] is a programming style which isolates the business logic of the program from its secondary or supporting functions. The basis of the aspect oriented programming is to add a third dimension to pre-existing dimensions of the Procedural Programming and Object Oriented Programming, by describing aspects that cross-cut the structure of the program.


==Overview==
==Overview==
Line 11: Line 11:
describing so called aspects that cross-cut the structure of the program.  
describing so called aspects that cross-cut the structure of the program.  


==Motivation and Terminologies==
==Motivation ==
Traditional programming designs had poor modularity due to crosscutting dependencies. Especially in OOP each aspect is scattered by virtue of the function being spread over  numerous unrelated functions or modules which maybe using its functionality, this means changing logging in one function requires modifying all the modules using this functionality, this works on the logic that the users knows/maintains a log of all the functions that use the functionality of the aspect
Traditional programming designs had poor modularity due to crosscutting dependencies. Especially in OOP each aspect is scattered by virtue of the function being spread over  numerous unrelated functions or modules which maybe using its functionality, this means changing logging in one function requires modifying all the modules using this functionality, this works on the logic that the users knows/maintains a log of all the functions that use the functionality of the aspect
Example.  
Example.  
Line 18: Line 18:


Let us consider the example all know and love, Twitter. The model for which is shown in figure 1. If this model were to be implemented in an Object Oriented manner then the security aspect like authentication would have been scattered by virtue of a function called a logging, which is used by both the Users and Posts, a database transaction should encapsulate the operation in order to prevent accidental data loss Also, this operation should be logged onto the system log. We already see that authentication together with logging and transactions are now exhibiting cross-cutting concerns. Now consider the scenario in which the security specifications change , then in order to complete these changes this would require a major effort as the security-related operations are scattered across numerous methods.
Let us consider the example all know and love, Twitter. The model for which is shown in figure 1. If this model were to be implemented in an Object Oriented manner then the security aspect like authentication would have been scattered by virtue of a function called a logging, which is used by both the Users and Posts, a database transaction should encapsulate the operation in order to prevent accidental data loss Also, this operation should be logged onto the system log. We already see that authentication together with logging and transactions are now exhibiting cross-cutting concerns. Now consider the scenario in which the security specifications change , then in order to complete these changes this would require a major effort as the security-related operations are scattered across numerous methods.
==Aspect Oriented Programming ==
Aspect Oriented Programming aims to solve this problem by including the concept called as aspects [http://paginas.fe.up.pt/~ei01036/artigos/aop.pdf 3]. Aspects are stand-alone modules used to express cross-cutting concerns [http://www.marvin-steinberg.de/alternative-implementations-for-aspect-oriented-programming-language-constructs-a-case-study-in-ruby/ 4]. Figure demonstrates how our twitter module now looks with the inclusion of aspects.
In Java, the aspects contain advice and intertype declarations [http://www.javaworld.com/javaworld/jw-01-2002/jw-0118-aspect.html 9].


                                                 [[Image:Figure2.png|frame|center]]         
                                                 [[Image:Figure2.png|frame|center]]         
Aspect Oriented Programming aims to solve this problem by including the concept called as aspects. Aspects are stand-alone modules used to express cross-cutting concerns. Figure demonstrates how our twitter module now looks with the inclusion of aspects.
In our example our authentication module can include a security check for the users before he/she can access the posts posted by himself and his/her friends.  
In Java, the aspects contain advice and intertype declarations. In our example our authentication module can include a security check for the users before he/she can access the posts posted by himself and his/her friends. The pointcuts in JAVA defines the times when one can access the user account, and the code in the advice body defines how the authentication implementation takes place.  
 
Join points in Aspect Oriented Programming
Key Concepts
 
Join Points: A well defined location at a point in the execution of a program.
 
PointCut: A set of join points
 
Advice: Code designed to run automatically at all join points in a particular pointcut. It can be marked as before, after or around i.e. in place of the join points in the pointcut.
 
Lexical Introduction: Adding functionality to a class in place as opposed to extending it.


==AspectR==
==AspectR==
Aspects such loggin, security login, database session, transactions need to be done at the start of each method. If for example an application starts a session at the beginning of the application start and releases the resource only at the termination. A lot of resources are wasted and also may cause starvation of other applications. Hence we need frequent access to resources every time a needy method is called. Implementing this logic inside the method makes the aspect code repeated and cluttered all over the application source. To avoid this problem Aspect oriented programming can be adopted into the Ruby applications. Where all the aspect oriented functionality can be placed inside one behaviour. This behaviour can then be called before and after each method invocation. Thus making changes in aspect behaviour easy.
Aspect Oriented Programming in Ruby is known as AspectR. [http://www.objectmentor.com/resources/articles/AOP_in_Ruby.pdf 2]
Aspects such login, security login, database session, transactions need to be done at the start of each method. If for example an application starts a session at the beginning of the application start and releases the resource only at the termination. A lot of resources are wasted and also may cause starvation of other applications. Hence we need frequent access to resources every time a needy method is called. Implementing this logic inside the method makes the aspect code repeated and cluttered all over the application source. To avoid this problem Aspect oriented programming can be adopted into the Ruby applications[http://aspectr.sourceforge.net/ 6]. Where all the aspect oriented functionality can be placed inside one behaviour. This behaviour can then be called before and after each method invocation. Thus making changes in aspect behaviour easy [http://www.ironicwolf.com/2009/04/aspect-the-ruby-way/ 7].
Aspect oriented programming in Ruby can be implemented using Aquarium framework.  
Aspect oriented programming in Ruby can be implemented using Aquarium framework.  


==Creating a Simple Aspect==
===Creating a Simple Aspect in Ruby===
Let us understand how easily an aspect behavior can be setup.
Let us understand how easily an aspect behavior can be setup.


===Setup===
===Setup===
Install Aquarium gem from http://rubyforge.org/frs/?group_id=4281
Install Aquarium gem from rubyForge website or alternatively use "gem install aquarium".
alternatively use gem install aquarium


===Example===
===Example===
Depending on the requirement of the application the aspect behavior may be changed from the one below. The snippet below shows the skeleton of a typical Aspect definition.  
Depending on the requirement of the application the aspect behavior may be changed from the one below. The snippet below shows the skeleton of a typical Aspect definition [http://rubyforge.org/frs/?group_id=4281 8].
 
class Aspect_Name
    include Aquarium::DSL
    before :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
    #Do Aspect Work Like Creating Session, loggin, transaction, security etc     
    end


    after :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
class Aspect_Name
    #Do Aspect Work Like Ending Session, loggin, transaction, security etc   
include Aquarium::DSL
    end
before :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
end
#Do Aspect Work Like Creating Session, loggin, transaction, security etc     
end
after :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
#Do Aspect Work Like Ending Session, loggin, transaction, security etc   
end
end




Line 54: Line 65:


Another way of implementing is using Around Advice.
Another way of implementing is using Around Advice.
class Aspect_Name
class Aspect_Name
    include Aquarium::DSL
include Aquarium::DSL
    around :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
around :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
     
#Do Aspect Work Like Creating Session, loggin, transaction, security etc
        #Do Aspect Work Like Creating Session, loggin, transaction, security etc
result = join_point.proceed
#Do Aspect Work Like Ending Session, loggin, transaction, security etc
result  # block returns the result of the proceed.
end
end
 
==Aspect Oriented programming vs Object Oriented Programming==
Aspect oriented programming is not a complement to Object Oriented programming. It coexists together [http://paginas.fe.up.pt/~ei01036/artigos/aop.pdf 3]. AOP tries to lessen the limitations of traditional object oriented programming like cross-cutting in code, dependency in code change in repetitive methods which deal with aspects like logging, authentication and sessions.
 
==Summary==
Aspect oriented Programming is a programming technique which has added a third dimension to programming languages, which is a significant improvement over Object Oriented Programming as it lets the programmer focus on working on the business logic in times when he has to change his line of thinking mid-way. All this without having to to be worried about his aspect being scattered through the code as all his new code can be incorporated in aspects.
 
==References==
*[1]: [http://en.wikipedia.org/wiki/Aspect-oriented_programming Aspect Oriented Programming]
 
*[2]: [http://www.objectmentor.com/resources/articles/AOP_in_Ruby.pdf Aspect Oriented Programming in Ruby]
 
*[3]: [http://paginas.fe.up.pt/~ei01036/artigos/aop.pdf Aspect Oriented Programming, Comparison of Languages]
 
*[4]: [http://www.marvin-steinberg.de/alternative-implementations-for-aspect-oriented-programming-language-constructs-a-case-study-in-ruby/ Alternative Implementations for Aspect-Oriented Programming Language Constructs: A Case Study in Ruby]
 
*[5]: [http://www.theserverside.com/news/1364528/Implementing-Object-Caching-with-AOP Implementing Object Caching with AOP]
 
*[6]: [http://aspectr.sourceforge.net/ AspectR sourceforge]


      result = join_point.proceed
*[7]: [http://www.ironicwolf.com/2009/04/aspect-the-ruby-way/ Aspect the Ruby way]


          #Do Aspect Work Like Ending Session, loggin, transaction, security etc
*[8]: [http://rubyforge.org/frs/?group_id=4281 Ruby Forge]


      result  # block returns the result of the proceed.
*[9]: [http://www.javaworld.com/javaworld/jw-01-2002/jw-0118-aspect.html A detailed series of articles about the basics of aspect-oriented programming and AspectJ]
    end
end

Latest revision as of 03:53, 16 October 2010

Introduction

Aspect Oriented Programming 1 is a programming style which isolates the business logic of the program from its secondary or supporting functions. The basis of the aspect oriented programming is to add a third dimension to pre-existing dimensions of the Procedural Programming and Object Oriented Programming, by describing aspects that cross-cut the structure of the program.

Overview

If we look at the programming field from a distance we’ll generally divide programming languages into two main camps: procedural programming(POP) and object-oriented programming (OOP). Like all programming techniques these were invented as ways to structure programming to achieve conceptual separation of components. This separation is obtained by dividing programs into sub-structures of some kind so that we can think and program each one as a reasonably separate and autonomous entity.

When using POP we divide our program into separate functions or procedures and compose the program flow by redirecting it from one to another in what is essentially a linear structure. With OOP a second dimension is added as we structure our code into encapsulated units linked to each other. The control flow is now defined dynamically by the object structure and polymorphism. Aspect Oriented programming tries to add a third dimension to this structure by describing so called aspects that cross-cut the structure of the program.

Motivation

Traditional programming designs had poor modularity due to crosscutting dependencies. Especially in OOP each aspect is scattered by virtue of the function being spread over numerous unrelated functions or modules which maybe using its functionality, this means changing logging in one function requires modifying all the modules using this functionality, this works on the logic that the users knows/maintains a log of all the functions that use the functionality of the aspect Example.

Let us consider the example all know and love, Twitter. The model for which is shown in figure 1. If this model were to be implemented in an Object Oriented manner then the security aspect like authentication would have been scattered by virtue of a function called a logging, which is used by both the Users and Posts, a database transaction should encapsulate the operation in order to prevent accidental data loss Also, this operation should be logged onto the system log. We already see that authentication together with logging and transactions are now exhibiting cross-cutting concerns. Now consider the scenario in which the security specifications change , then in order to complete these changes this would require a major effort as the security-related operations are scattered across numerous methods.

Aspect Oriented Programming

Aspect Oriented Programming aims to solve this problem by including the concept called as aspects 3. Aspects are stand-alone modules used to express cross-cutting concerns 4. Figure demonstrates how our twitter module now looks with the inclusion of aspects. In Java, the aspects contain advice and intertype declarations 9.

In our example our authentication module can include a security check for the users before he/she can access the posts posted by himself and his/her friends.

Key Concepts

Join Points: A well defined location at a point in the execution of a program.

PointCut: A set of join points

Advice: Code designed to run automatically at all join points in a particular pointcut. It can be marked as before, after or around i.e. in place of the join points in the pointcut.

Lexical Introduction: Adding functionality to a class in place as opposed to extending it.

AspectR

Aspect Oriented Programming in Ruby is known as AspectR. 2 Aspects such login, security login, database session, transactions need to be done at the start of each method. If for example an application starts a session at the beginning of the application start and releases the resource only at the termination. A lot of resources are wasted and also may cause starvation of other applications. Hence we need frequent access to resources every time a needy method is called. Implementing this logic inside the method makes the aspect code repeated and cluttered all over the application source. To avoid this problem Aspect oriented programming can be adopted into the Ruby applications6. Where all the aspect oriented functionality can be placed inside one behaviour. This behaviour can then be called before and after each method invocation. Thus making changes in aspect behaviour easy 7. Aspect oriented programming in Ruby can be implemented using Aquarium framework.

Creating a Simple Aspect in Ruby

Let us understand how easily an aspect behavior can be setup.

Setup

Install Aquarium gem from rubyForge website or alternatively use "gem install aquarium".

Example

Depending on the requirement of the application the aspect behavior may be changed from the one below. The snippet below shows the skeleton of a typical Aspect definition 8.

class Aspect_Name
include Aquarium::DSL
before :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
#Do Aspect Work Like Creating Session, loggin, transaction, security etc      
end
after :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
#Do Aspect Work Like Ending Session, loggin, transaction, security etc  
end
end


Before advice tells what work needs to be done before the method is called. After advice tells what work needs to be done after the method ends.

Another way of implementing is using Around Advice.

class Aspect_Name
include Aquarium::DSL
around :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
#Do Aspect Work Like Creating Session, loggin, transaction, security etc
result = join_point.proceed
#Do Aspect Work Like Ending Session, loggin, transaction, security etc
result  # block returns the result of the proceed.
end
end

Aspect Oriented programming vs Object Oriented Programming

Aspect oriented programming is not a complement to Object Oriented programming. It coexists together 3. AOP tries to lessen the limitations of traditional object oriented programming like cross-cutting in code, dependency in code change in repetitive methods which deal with aspects like logging, authentication and sessions.

Summary

Aspect oriented Programming is a programming technique which has added a third dimension to programming languages, which is a significant improvement over Object Oriented Programming as it lets the programmer focus on working on the business logic in times when he has to change his line of thinking mid-way. All this without having to to be worried about his aspect being scattered through the code as all his new code can be incorporated in aspects.

References