EFRAFA

From Agent Factory

Jump to: navigation, search

Contents

EFRAFA: Efficient File Reading Agent Factory Add-on

Introduction

The EFRAFA plugin allows agents to read files located on the same machine as any agent platform. Files are given a URI in the following format:

efrafa://hostname//path/to/file.txt

If the supplied hostname relates to the local host, the file is read directly from the local filesystem, rather than being transmitted via the network. If it is a remote host, the file is sent over the network. The EFRAFA service handles the resolution of host names, so agents binding to it do not require knowledge of hostnames.

Usage

Using the EFRAFA service is done as follows:

1. Initialise the service in the platform configuration file in the same way as any other service

// add a service with the following format
// SERVICE service_name service_class port
SERVICE efrafa com.agentfactory.service.efrafa.EFRAFAService 4448

2. The agent must then be made to bind with the service. The following code is an example for an AFAPL2 agent:

COMMIT(?self, ?now, BELIEF(true), bindToService(efrafa));

3. Finally, actuators may now use the service to get a java.io.InputStream from which they can read the file contents

String requestedFile = "efrafa://hostname//path/to/file.txt";
EfrafaService service = (EfrafaService) getService( "efrafa" );
InputStream in = service.getInputStream( new URI( requestedFile ) );
// read from in
in.close();

Known Limitations

At present, it is assumed that all instances of EFRAFA servers listen on the same port as each other

Example

The following is a simple example to read the contents of a file and adopt a belief about its size.

1. Initialise the service in the platform configuration file in the same way as any other service

// add a service with the following format
// SERVICE service_name service_class port
SERVICE efrafa com.agentfactory.service.efrafa.EFRAFAService 4448

2. Write the AFAPL2 agent:

IMPORT com.agentfactory.afapl2.core.agent.BasicAgent;

// bind to the service
COMMIT(?self, ?now, BELIEF(true), bindToService(efrafa));

// define an action to request a file using EFRAFA
ACTION requestFile(?uri) {
    PRECONDITION BELIEF(true);
    POSTCONDITION BELIEF(true);

    CLASS filetransfertest.RequestFile;
}

// commit to requesting a specific file
COMMIT(?self, ?now, BELIEF(true), requestFile(efrafa://hostname.example.com//path/to/testfile.txt));

3. Write the actuator

package filetransfertest;

import com.agentfactory.logic.agent.Actuator;
import com.agentfactory.logic.lang.FOS; 

import com.agentfactory.service.efrafa.EfrafaService;

import java.io.InputStream;

import java.net.URI;

public class RequestFile extends Actuator {

    public boolean act( FOS action ) {
        String requestedFile = action.argAt( 0 ).toString(  );

        // get the service
        EfrafaService service = ( EfrafaService ) getService( "efrafa" );

        try {
            // get the input stream from which to read the data
            InputStream in = service.getInputStream( new URI( requestedFile ) );

            // count the number of bytes read and print it out
            int counter = 0;
            while( in.read(  ) != -1 ) {
                counter++;
            }
            System.out.println( "Bytes read: " + counter );

            // close the stream
            in.close(  );
        }
        // handle exceptions
        catch( Exception e ) {
            System.err.println( "File Transfer FAILED: " + e );
            e.printStackTrace(  );
            return false;
        }

        return true;
    }
}

Contact

EFRAFA is written and maintained by David Lillis. Please direct any comments or reports to the following email address: david /dot/ lillis /at/ ucd /dot/ ie