• Documentation
 
Fractal Tutorial : Helloworld reconfiguration with Fscript

Fractal Tutorial : Helloworld reconfiguration with Fscript


Chapter 1. Introduction

We have seen in the previous tutorial, how to reconfigure Fractal application. The problem is that the Fractal API is verbose, and it can become fastidious to reconfigure, or navigate throw the architecture to find a component. Fscript helps us, its syntax is much more concise than calls to the APIs.

We will perform the same reconfiguration than in the previous tutorial (replace the current client component by another client component), but this time with Fscript.

Chapter 2. Fscript

FScript can be used programmatically with APIs, or with fconsole, which is just a shell for FScript. In this tutorial we will use fconsole.

2.1. Start the FScript console

We first need to run the FScript console.

with Ant :

We create a jar containing our Java and Fractal ADL files.

ant dist

We launch FScript console with Fractal default libraries and our helloworld.jar in the classpath.

Linux :

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

Windows :

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

With Maven:

mvn -Prun

2.2. Reconfiguration

To reconfigure our helloworld application, we first need to create it. The ClientServerExplorer.fractal contains the definition of a Fractal explorer component which encapsulates our client-server component, in order to explore graphically our helloworld application.

Example 2.1. FScript, reconfiguration script

froot = adl-new("org.objectweb.fractal.explorer.BasicFractalExplorer(client-server,helloworld.ClientServerImpl)");

cs = $froot/descendant::client-server;
client = $froot/descendant::client;
server = $froot/descendant::server;
intf-cs-r = $cs/interface::r;
unbind(bindings-to($client));
unbind(bindings-to($server));
        
remove($cs, $client);

client2 = adl-new('helloworld.ClientImpl2');
set-name($client2,"client2");
add($cs,$client2);
intf-client2-r = $client2/interface::r;
intf-client2-s = $client2/interface::s;

bind($intf-client2-s,$froot/descendant::server/interface::s);
bind($intf-cs-r,$intf-client2-r);

start($cs);

:run $froot/descendant::client-server/interface::r


On the fconsole type:

FScript> froot = adl-new("org.objectweb.fractal.explorer.BasicFractalExplorer(client-server,helloworld.ClientServerImpl)");

We enclapsulate our component in a BasicFractalExplorer in order to launch our application in Fractal Explorer.

The 'client-server' parameter is the component name given to our helloworld.ClientServerImpl component.

Now the component is created we can start the reconfiguration.

First we get a reference to our root composite component ClientServerImpl:

FScript> cs = $froot/descendant::client-server;

This command means: "from all descendant components of the component referenced by froot select the one with the name client-server".

'$' is used to get the value of a variable. The '::' is an operator for the selection. Statements end with semi-colon ';'.

$froot/descendant gets all direct and indirect children of the component referenced by froot (helloworld.ClientServerExplorer).

In order to reconfigure the helloworld application, we first need to get the current client component unbind its client interface, and remove it from its parent.

Get a reference to the 'client' component

FScript> client = $froot/descendant::client;

Get a reference to the interface 'm' of the ClientServerImpl component.

FScript> intf-cs-m = $cs/interface::m;

Unbind the client and server component bindings and remove the client component from the root client-server component.

FScript>  unbind(bindings-to($client));
FScript>  unbind(bindings-to($server));
FScript> remove($cs, $client);

Create the new client component. Give it a name, and add it to the client-server component.

FScript> client2 = new('helloworld.ClientImpl2');
FScript> set-name($client2,"client2");
FScript> add($cs,$client2);

Get references to the interface of the new client component in order to manipulate them easily.

FScript> intf-client2-m = $client2/interface::m;
FScript> intf-client2-s = $client2/interface::s;

Bind 's' interface to the server component, the 'm' interface to the client-server component.

FScript> bind($intf-client2-s,$froot/descendant::server/interface::s);
FScript> bind($intf-cs-m,$intf-client2-m);

Start the client-server component

FScript> start($cs);

FScript provides a ':run' command which allows to launch Runnable interface. Don't forget the colon ':'. To see all available commands you can type

:help

. We can try our new client component by calling the 'm' interface of the client-server component:

FScript> :run $froot/descendant::client-server/interface::r

As the 'r' interface of the client-server calls the 'r' interface of our new client component, we can see the new nessage displayed :

->Hello I'm another Client

2.3. FScript script files

You can put all fscript reconfiguration commands, in a file, and load it in the FScript console.

We define a reconfigure method in a helloworld.fscript file:

Example 2.2. helloworld.fscript

action create(){
	froot = adl-new("org.objectweb.fractal.explorer.BasicFractalExplorer(client-server,helloworld.ClientServerImpl)");
	return $froot;
}

function getItfToRun(froot){
	return $froot/descendant::client-server/interface::r;
}

action reconfigure(froot) {
        cs = $froot/descendant::client-server;
        client = $froot/descendant::client;
        server = $froot/descendant::server;
        intf-cs-r = $cs/interface::r;
        unbind(bindings-to($client));
        unbind(bindings-to($server));
        
        remove($cs, $client);

        client2 = adl-new('helloworld.ClientImpl2');
        set-name($client2,"client2");
        add($cs,$client2);
        intf-client2-r = $client2/interface::r;
        intf-client2-s = $client2/interface::s;

        bind($intf-client2-s,$froot/descendant::server/interface::s);
        bind($intf-cs-r,$intf-client2-r);

        start($cs);
}
		


We can launch the FScript console, create our component, load the FScript file, and call the reconfiguration method on our component :

FScript> :load helloworld.fscript
FScript> froot = create();
CLIENT created
SERVER created
Success (took 11244 ms).

FScript> reconfigure($froot);
CLIENT created
Success (took 300 ms).

FScript>:run getItfToRun($froot);

Launching interface [#<interface: client-server.r>].
FScript>->Hello I'm another Client

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