• Documentation
Fractal Tutorial : Groovy + Fractal

Fractal Tutorial : Groovy + Fractal

Chapter 1. Introduction

In this tutorial we will show how we can manipulate easily our Fractal architecture at the runtime with Groovy. We'll be able to see graphically the evolution of our architecture with a tool named Fractal Explorer.

We will not describe the source code of the example it is quite the same than the one used in previous tutorials.

Chapter 2. The Groovy shell

The first thing we need to do to run and reconfigure our helloworld application in groovy, is to launch the groovy shell.

With ant, we first need to package the helloworld application in a jar :

ant dist

We can now run the groovy shell

Linux :

bash $FRACTAL_HOME/bin/fractal.sh -cp dist/helloworld.jar -groovy

Windows :

%FRACTAL_HOME%\bin\fractal.bat -cp dist/helloworld.jar -groovy

With Maven, just type :

mvn -Prun

Once the groovy shell is launched, we can instanciate our helloworld architecture by calling the Fractal Launcher on the ClientServerExplorer component.

groovy>  import org.objectweb.fractal.adl.*;
groovy>  import java.util.*;
groovy>  import helloworld.*;
groovy>  import org.objectweb.fractal.api.*;
groovy>  import org.objectweb.fractal.util.*;

Creation of the Fractal ADL factory.

groovy>  factoryfactory =  org.objectweb.fractal.adl.FactoryFactory;
groovy>  factory = factoryfactory.getFactory(factoryfactory.FRACTAL_BACKEND);

Fractal Explorer provides a Fractal component named 'BasicFractalExplorer' that allows to explore its subcomponents.

The creation of the BasicFractalExplorer component need 2 arguments the name of the component to explore "name" and the adl definition "definition".

groovy>  context = new HashMap();
groovy>  context.put("name", "ClientServerImpl");
groovy>  context.put("definition", "helloworld.ClientServerImpl");

We create a BasicFractalExplorer.

groovy>  root = factory.newComponent("org.objectweb.fractal.explorer.BasicFractalExplorer",context);

Once this code is entered or copied in the console, type :

groovy>  execute

If you've made a mistake during the copying execute may fails with a 'statement not complete' message. You can type discard on the console.

Normally the Fractal Explorer window will appear.

Now we can explore our architecture. Fractal Explorer provides also a graphical view, you can launch it with the "graph view" item of the "Roles" menu.

Now click on our root component ClientServerImpl, the architecture of the component is displayed.

2.1. Exploration

Scripting is really useful to introspect, explore the Fractal architecture, and play with the Fractal API.

groovy>  fractal = org.objectweb.fractal.util.Fractal;

get the subcomponents of the root component via the ContentController interface

groovy>  components = fractal.getContentController(root).getFcSubComponents();

get the name of the first subcomponent : explorer

groovy>  fractal.getNameController(components[0]).getFcName();

get the name of the second subcomponent : ClientServerImpl

groovy>  fractal.getNameController(components[1]).getFcName();

we create a reference to our root component ClientServerImpl

groovy>  cs = components[1];

start the component

groovy>  fractal.getLifeCycleController(cs).startFc();

call the run method of the interface named "r"

groovy>  cs.getFcInterface("r").run();

2.2. Reconfiguration

start the component

groovy>  fractal.getLifeCycleController(cs).stopFc();

unbind the binding between ClientServerImpl.m and client.m

groovy>  fractal.getBindingController(cs).unbindFc("m");

get the list of the components in the ClientServerImpl component

groovy>  subcomps = fractal.getContentController(cs).getFcSubComponents();

introspect to find the client

groovy>  fractal.getNameController(subcomps[0]).getFcName();
groovy>  client = subcomps[0];
groovy>  server = subcomps[1];

unbind the binding between client.s and server.s

groovy>  fractal.getBindingController(client).unbindFc("s");

We can update Fractal Explorer and see that the binding has been correctly removed:

Create a new client

groovy>  client2 = factory.newComponent("helloworld.ClientImpl2",null);

Add client2 to the ClientServerImpl

groovy>  fractal.getContentController(cs).addFcSubComponent(client2);

By default the new component is displayed at the top left corner you have to move it to display it correctly.

Remove the old client

groovy>  fractal.getContentController(cs).removeFcSubComponent(client);

rename the component

groovy>  fractal.getNameController(client2).setFcName("client2");

bind the client2 with the server

groovy>  fractal.getBindingController(client2).bindFc("s",server.getFcInterface("s"));

bind the ClientServerImpl with the client2

groovy>  fractal.getBindingController(cs).bindFc("r",client2.getFcInterface("r"));

start the ClientServerImpl component

groovy>  fractal.getLifeCycleController(cs).startFc();

call the run method of the interface named "r" so we now have the main method called from the client2 and a new message ->Hello I'm another Client

groovy>  cs.getFcInterface("r").run();

As you can see scripting languages like groovy is very interesting for prototyping and exploration of the recursive and reflexives capabilities of the fractal component model.

1999-2009 © OW2 Consortium  | Last Published: 2009-02-03 18:30  | Version: 1.1.2-SNAPSHOT