|
|||||||||
Consortium Activities Projects Forge Events |
Fractal |
About this document
Table of Contents
About SpoonIn addition, Spoon provides templates in pure Java, which are Java class that contains template parameters, which are defined as fields annotated with @Parameter. Template parameters can represent primitive values (such as literal values, program element's names, types), or actual CtElements (ASTs). In the template code, all the references to template parameters can be substituted by their actual values using the substitution engine API provided by Spoon. The templates are useful to express patterns that can be used for generating code, as exampled by Generative Programming. To implement Fraclet with have developed a set of Spoon processors and templates to process our annotations. This tutorial does not detail the implementation of the processors and templates but focus on the use of Fraclet annotations. Available AnnotationsA small set of annotations have been defined for easy programming of Fractal components. The table below summarises these annotations.
In addition to this small annotations set, additional annotations are also available for handling advanced concepts of Fractal.
Core Annotations@Component
Details:
Parameters:
PS. The attribute Examples: @Component public class MyComponent implements Runnable { (...) } Generate the following Fractal ADL definition: <definition name="MyComponent"> (...) <content class="MyComponent"/> </definition> @Component(name="MyComponentDefinition") public class MyComponent implements Runnable { (...) } Generate the following Fractal ADL definition: <definition name="MyComponentDefinition"> (...) <content class="MyComponent"/> </definition> @Component(name="MyComponentDefinition", provides=@Interface(name="r", signature=Runnable.class)) public class MyComponent implements Runnable { (...) } Generate the following Fractal ADL definition: <definition name="MyComponentDefinition"> <interface name="r" role="server" cardinality="singleton" contingency="mandatory" signature="java.lang.Runnable"/> (...) <content class="MyComponent"/> </definition> @Component(name="MyComponentDefinition", uses=@Definition(name="RunnableComponent")) public class MyComponent implements Runnable { (...) } Generate the following Fractal ADL definition: <definition name="MyComponentDefinition" extends="RunnableComponent"> (...) <content class="MyComponent"/> </definition> @Interface
Details:
Parameters:
Examples: @Interface public interface Service { (...) } Generate the following Fractal ADL abstract definition: <definition name="Service"> <interface name="service" signature="Service" role="server" cardinality="singleton" contingency="mandatory"/> </definition> @Interface(name="s") public interface Service { (...) } Generate the following Fractal ADL abstract definition: <definition name="Service"> <interface name="s" signature="Service" role="server" cardinality="singleton" contingency="mandatory"/> </definition> @Interface(name="s") public interface Service { (...) } @Component public class Server implements Service { (...) } Generate the following Fractal ADL definitions: <definition name="Service"> <interface name="service" signature="Service" role="server" cardinality="singleton" contingency="mandatory"/> </definition> <definition name="Server" extends="Service"> (...) <content class="Server"/> </definition> @Definition
Details:
Parameters:
Examples: @Component(uses=@Definition(name="MyDefinition",argument="runnableItf=>r")) public class Client implements Runnable { (...) } Generates the following Fractal ADL definition: <definition name="Client" argument="runnableItf=r" extends="MyDefinition(runnableItf)"> (...) </definition> @Attribute
Details:
Parameters:
Example: @Component public class Server implements Service { @Attribute(mode=Access.READ_ONLY) private int constantValue = 10 ; @Attribute(value=">>") private String header ; @Attribute(name="msg", set="setMessage", mode=Access.WRITE_ONLY) private String message ; (...) private void setMessage(String m) { System.out.println("Calling my custom attribute setter"); this.message = m; } } Generates the following Fractal ADL definitions: <definition name="Server" arguments="msg,header=>>"> <interface name="service" signature="Service" role="server" cardinality="singleton" contingency="mandatory"/> <content class="Server"/> <attributes signature="ServerAttributes"> <attribute name="message" value="${msg}"/> <attribute name="header" value="${header}"/> </attributes> </definition> @Requires
Details:
Parameters:
Example: @Component public class Client implements Runnable { @Requires(name = "default", bind="setDefault") private Service defaut; @Requires(cardinality = Cardinality.COLLECTION) private Map<String,Service> service = new HashMap<String,Service>(); (...) private void setDefault(Service d) { System.out.println("Calling my custom attribute setter"); this.defaut = d; } } Generates the following Fractal ADL definitions: <definition name="Client"> <interface name="default" signature="Service" role="client" cardinality="singleton" contingency="mandatory"/> <interface name="service" signature="Service" role="client" cardinality="collection" contingency="mandatory"/> <content class="Client"/> </definition> @Lifecycle
Details:
Parameters:
Example: @Component public class Client implements Runnable { @LifeCycle(step=Step.START, policy=Policy.ASYNCHRONOUS) private void setup() { System.out.println("Starting the component Client asynchronously..."); } @LifeCycle(step=Step.STOP) private void tearDown() { System.out.println("Stopping the component Client synchronously..."); } (...) } @ControllerDetails: This class annotation provides access to the controllers supported by the component. Parameters:
Examples: @Component public class Client implements Runnable { @Controller private org.objectweb.fractal.api.Component comp; (...) } @Component public class Client implements Runnable { @Controller("name-controller") private NameController nc; @Controller("super-controller") private SuperController sc; (...) } Additional Annotations@Data
Details:
Example: @Data public class Message { (...) } @Membrane
Details:
Parameters:
Example: @Component @Membrane(controller="myPrimitive") public class Client implements Runnable { (...) } Generates the following Fractal ADL definition: <definition name="Client"> (...) <controller desc="MyPrimitive"/> </definition> @Component @Membrane(template="myTemplate") public class Client implements Runnable { (...) } Generates the following Fractal ADL definition: <definition name="Client"> (...) <template-controller desc="MyTemplate"/> </definition> @Node
Details:
Parameters:
Example: @Component @Node("clientNode") public class Client implements Runnable { (...) } Generates the following Fractal ADL definition: <definition name="Client" extends="MyDefinition"> (...) <virtual-node name="clientNode"> </definition> ConfigurationSpoon ConfigurationYou should first describe the list of spoonlets that need to be
applied to your source code. Fraclet basically provides two
spoonlets for introducing the Fractal API callbacks and generating
the Fractal ADL definitions, respectively. To do so, you should
create a file named <?xml version="1.0" encoding="UTF-8"?> <spoon> <spoonlet groupId="org.objectweb.fractal.fraclet.java" artifactId="fractal-spoonlet" version="3.2" /> <spoonlet groupId="org.objectweb.fractal.fraclet.java" artifactId="fractaladl-spoonlet" version="3.2" /> </spoon> Maven2 ConfigurationThen, you should describe the list of spoonlets as Maven2 artefact dependencies. <project> (...) <dependencies> (...) <dependency> <groupId>org.objectweb.fractal.fraclet.java</groupId> <artifactId>fractal-spoonlet</artifactId> <version>3.2</version> </dependency> <dependency> <groupId>org.objectweb.fractal.fraclet.java</groupId> <artifactId>fractaladl-spoonlet</artifactId> <version>3.2</version> </dependency> </dependencies> (...) <project> Finally, you should extend the Maven2 build process by using the Spoon Maven2 plugin. <project> (...) <build> <plugins> <plugin> <groupId>net.sf.alchim</groupId> <artifactId>spoon-maven-plugin</artifactId> <version>0.7.1</version> <executions> <execution> <goals> <goal>recompile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> (...) <project> Revisiting HelloWorldThis section provides a quick overview of the benefits of Fraclet. In particular, it illustrates that, using annotations, component source code becomes more concise and easier to maintain. Moreover, using Fraclet, more than 60% of the source code (Java, FractalADL) can be saved. HelloWorld Architecture OverviewThe figure below introduces the well-known HelloWorld example implemented with Fractal components.
HelloWorld Components ImplementationThe source code below represents the Java code and the Fraclet
annotations written to implement the @Component(provides=@Interface(name="r",signature=Runnable.class)) public class Client implements Runnable { @Requires(name = "s") private Service defaut; @Attribute(value="Hello !") protected String message; public void run() { this.defaut.print(this.message); } } The @Interface(name="s") public interface Service { void print(String message); } The @Component public class Server implements Service { @Attribute private String header; @Attribute private int counter; public void print(final String message) { for (int i = 0; i < this.counter; i++) System.out.println(this.header + message); } } The HelloWorld Architecture DefinitionThe assembly definition below represents the FractalADL assembly
defined to describe the <definition name="HelloWorld" extends="ClientComposite(s=>server)"> <component name="server" definition="Server('>>',2)"/> </definition> This definition creates a composite component
Directory StructureThe directory structure below presents the list of compiled, generated and written files when using Fraclet to implement the HelloWorld example: %example% * src/main/java - Client.java - Server.java - Service.java * src/main/resources - HelloWorld.fractal * target/generated-sources/spoon - Client.java - ClientAttributes.java - Server.java - ServerAttributes.java - Service.java * target/classes - Client.class - Client.fractal - ClientAttributes.class - ClientComposite.fractal - Server.class - Server.fractal - ServerAttributes.class - Service.class - Service.fractal - HelloWorld.fractal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Copyright © 1999-2009, OW2 Consortium | contact | webmaster | Last modified at 2012-12-03 09:57 PM |