reuseLogostLogotudLogorewerseLogo modelplexLogo
gearsBG
Reuseware Composition Framework
Components, Modules, Aspects or something new?
Introduce new Composition Techniques into your Language of Choice with Reuseware!
TIP
This site refers to version 0.5.x of the Reuseware Composition Framework.

Contents

Java- Tutorial

This tutorial works with a subset of the Java programming language, here referred to as Java-. In order to create a composition environment for the Java- language, we first need a description of the Java- language itself, i.e. a grammar description of the language. We aim at having a clear separation between the abstract and concrete syntax of any language. In that way, several concrete syntaxes can be defined for a single abstract syntax. Thus, first we need to define the abstract syntax of Java- in a way that is readable by the tool. The Reuseware Framework understands two input formats: an EBNF-like syntax, but also a description in a more modeling-like language, Ecore. Here we provide the abstract syntax description of Java- in the EBNF-like syntax

Core language

See also: Language Grammers

CompilationUnit       = packageDeclaration:PackageDeclaration?,
                        importDeclarations:ImportDeclaration*, 
                        classDeclarations:ClassDeclaration*; 
 
PackageDeclaration    = name:QualifiedName; 
 
ImportDeclaration     = name:QualifiedName; 
 
ClassDeclaration      = modifier:Modifier, 
                        name:Identifier, 
                        extends:QualifiedName?, 
                        implements:QualifiedName*, 
                        classBody:MemberDeclaration*; 
 
MemberDeclaration     = AttributeDeclaration | MethodDeclaration; 
 
AttributeDeclaration  = modifier:Modifier, 
                        type:QualifiedName, 
                        name:Identifier, 
                        value:Value?; 
 
MethodDeclaration     = modifier:Modifier, 
                        type:QualifiedName, 
                        name:Identifier, 
                        arguments:VariableDeclaration*, 
                        statements:Statement*; 
 
Statement             = MethodCall | VariableDeclaration | VariableAssignment; 
 
VariableDeclaration   = type:QualifiedName, name:Identifier, value:Value?; 
 
VariableAssignment    = name:Identifier, 
                        value:Value; 
 
MethodCall            = method:QualifiedName, 
                        arguments:Value*, 
                        succeedingCall:MethodCall?;
 
Value                 = StringValue | IntegerValue | Identifier | MethodCall; 
 
Modifier              = PrivateModifier | PublicModifier | ProtectedModifier; 
 
QualifiedName         = elements:Identifier+; 
 
PrivateModifier;
PublicModifier;
ProtectedModifier;
 
StringValue           = value:S;
IntegerValue          = value:S;
Identifier            = value:S;

A valid Java- program is an instance of the CompilationUnit description in the grammar above. The first production rule of the grammar says that a CompilationUnit optionally (?) has a PackageDeclaration followed by any number (*) of ImportDeclarations and then any number (*) of ClassDeclarations.

The EBNF syntax for describing a language is mostly a shorthand for the Ecore variant. Thus, it is possible to automatically generate an Ecore model from any valid description in the EBNF syntax.

In order to use the grammar above, create a folder in your Eclipse workspace and copy the grammar description into a file called java.as ('as' for abstract syntax). Right-click on the grammar file (java.as) and click the Map to Ecore option. A new file will be created called java.ecore and its contents can be browsed, as shown below.



The next task is to define the concrete syntax for the Java- language.

The abstract syntax grammar above is accompanied by a concrete syntax, referring to the abstract syntax.

CONCRETESYNTAX java FOR java 
 
CompilationUnit      ::= packageDeclaration? ";" (importDeclarations ";")* classDeclarations*; 
 
PackageDeclaration   ::= "package" name; 
 
ImportDeclaration    ::= "import" name; 
 
ClassDeclaration     ::= modifier "class" name ("extends" extends)? ("implements" implements+)? 
                         "{" classBody* "}"; 
 
AttributeDeclaration ::= modifier type name ("=" value)? ";"; 
 
MethodDeclaration    ::= modifier type name "(" (arguments ("," arguments)*)? ")" 
                         "{" (statements ";")* "}";
 
VariableDeclaration  ::= type name ("=" value)?; 
 
VariableAssignment   ::= name "=" value; 
 
MethodCall           ::= method "(" (arguments ("," arguments)*)? ")" ("." succeedingCall)?; 
 
QualifiedName        ::= elements ("." elements)*; 
 
PrivateModifier      ::= "private"; 
PublicModifier       ::= "public"; 
ProtectedModifier    ::= "protected"; 
 
StringValue          ::= value['\"' (~('\"'|'\\') | '\\' . )*  '\"']; 
IntegerValue         ::= value[('-')? ('0'..'9')+]; 
 
Identifier           ::= value[('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')*]; 

For example, the first rule says that a CompilationUnit consists of first its optional package declaration followed by a semicolon (";"), a list of import declarations separated by semicolons (";") and finally any number of class declarations.

TODO: explain the use of roles.

The concrete syntax above can be put in the file java.cs ('cs' for concrete syntax) in the same folder as the abstract syntax description.


Defining components

We will now write some example components in Java- in order see what we would like to achieve with respect to composition. In particular, in what way we should extend Java- in order to cover the introduced examples.

Suppose we want to define the following fragment component, being a valid Java- CompilationUnit.

package org.foo.vehicles; 
 
import java.util.Collection; 
 
public class Car implements Comparable { 
       private String name; 
 
       private Engine engine; 
 
       <<myMethod : java.MemberDeclaration>> 
 
       public void startEngine() { 
             engine.start(); 
       } 
 
       public void stopEngine() { 
             engine.stop(); 
       } 
}

The fragment components consists of one package declaration, one package import statement and one class definition defining the class Car. The class in turn has two member attributes and two methods specified. Further more, the fragment components is declared to be underspecified by the use of the statement:

<<myMethod : java.MemberDeclaration>> 

The intention of this statement is to allow a yet unknown method definition to be bound in its place at a later time. The statement declares a slot, which can be bound to some suitable fragment component at composition time. The slot has the name myMethod and its type is declared to be java.MemberDeclaration. The type MemberDeclaration is reused from the java language (Java-). The typing ensures that the fragment component bound in its place is of a valid construct to replace the slot, in this case a member declaration. Possibly, the following fragment component could be bound there during composition:

public void drive(int direction) { 
      turn(direction); 
}

We will now see how to extend the Java- language to support this simple kind of composition.


Reuse language

Now that the core language Java- has been is given, and we have seen a simple example we will now need to extend it to support composition. This step can also be automated using the CoMoGen subsystem of the composition framework. Here, however, we show how to do this manually.

For this example we will extend Java- by allowing the specification of Java- components of the type MemberDeclaration, i.e. components containing method declarations.

java.MemberDeclaration  = MemberDeclarationSlot; 
 
MemberDeclarationSlot; 
MemberDeclarationSlot   ==> componentmodel.slot; 

This little language extension is the extension of the Java- abstract syntax and will be called a Java- reuse language. The extension definition can be put in the file reusejava.as in the same directory as the other Java- grammar files.

The description above says (line 1) that what previously (in Java-, hence the "java." usage) could be MemberDeclarations can now also be a new construct called MemberDeclarationSlot, introduced to describe the construct used for declaring slots (in this case as representatives for member declarations). The two remaining statements in the description above introduces the construct as a concept in the language description and says that the new construct is a subclass (==>) of the concept slot in a common component model.

This description can also be mapped to an Ecore model by right-clicking on the description and selecting Map to Ecore.



For the language extension above we also need to define a concrete syntax.

CONCRETESYNTAX rjava FOR reusejava EXTENDS java 
 
java.CompilationUnit              ::= packageDeclaration? ";" (importDeclarations ";")*
                                      classDeclarations*; 
 
MemberDeclarationSlot             ::= "<<" name ":" type ">>"; 
 
componentmodel.VariationPointName ::= name[('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')*]; 
componentmodel.FragmentType       ::= language[('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')*] "." 
                                      construct[('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')*];

The most interesting definition in the concrete syntax is the third statement saying that the newly introduced construct MemberDeclarationSlot is written using first "<<" token, then a name for the slot followed by a colon (":") and a type of the slot and finally the closing ">>" token.

The definition above can be put in the file rjava.cs ('r' for reuse) in the same folder as the other language descriptions.

It is now time to generate our composition environment for our extended Java- language. This involves two steps, first, generating the language plug-in for the reuse language (on the abstract grammar or the Ecore model) and generating a parser for the reuse language. These steps are shown below.



Once this is done, we must launch a reuse language composition environment. This is done by selecting the Eclipse menu option Run -> Run... and creating a new Eclipse Application like shown below and then clicking Run to launch it.



Execution composition

See also: Composition Language

Once our composition environment is launched, we need to create a Reuseware Project, into which our components and composition programs will go. A new Reuseware project can be created by the following Eclipse menu selection File -> New -> Project... -> Reuseware -> Reuseware Project -> Next



Once this is done we need to write a composition program which describes how our components written above will be put together.

The following composition program could be used to execute the composition:

composition program 
 
fragmentlist java.CompilationUnit    myCar        = /Car.rjava; 
fragmentlist java.MethodDeclaration  driveMethod  = /DriveMethod.rjava; 
 
bind myMethod on myCar with driveMethod; 
 
print myCar to /CoolCar.java;  

The composition program is put in the composition program folder in the file CarComposition.bcbc (basic composition) is the extension for composition programs. The components are put in the components folder in the files Car.rjava and DriveMethod.rjava



Tip: use the Reuseware Text Editor inside Eclipse to obtain code highlighting for components and composition programs:

If Project -> Build Automatically is selected, the file CoolCar.java should appear in the out folder of your newly created Reuseware project. As expected, the result looks like this:

package org.foo.vehicles; 
 
import java.util.Collection; 
 
public class Car { 
        private String name; 
 
        private Engine engine; 
 
        public void drive(int direction) 
              turn(direction); 
        } 
 
        public void startEngine() { 
              engine.start(); 
        } 
 
        public void stopEngine() { 
              engine.stop(); 
        } 
}

You have now successfully completed the first Reuseware Composition Framework tutorial. Please give your feedback for possible improvements.

Retrieved from "http://reuseware.org/index.php/Java_Tutorial"

This page has been accessed 1,660 times. This page was last modified 07:38, 29 May 2009.