Description

This example is designed to show the means of interaction between LGI agents and non-LGI agents (external agents). The example focuses on the usage of two constructs: the release primitive operation and submitted event, leaving aside the control logic of the law.

In order to run this example, it is necessary to run a programmed external agent (e-agent), that will serve as external server receiving messages released by LGI agents. The LGI agents used in this example can be created using the human interface.

The law

The law in Prolog follows:

law(external,language(prolog))

sent(X,release(M,H,P),Y):-do(release(X,M,[H,P])).
sent(X,M,Y):- do(forward).

arrived(X,M,Y):-do(deliver).

submitted([H,P],[M],D) :- do(deliver(H,M,D)).

disconnected :- do(quit).

exception(E,Fc) :- do(deliver(Self,exc(E,Fc),Self)).

The Prolog law can be found at:
http://www.moses.rutgers.edu/examples/external/external.law

This is the Java version of the same law:

law(external,language(java))

import java.util.*;
import java.io.*;

public class external extends Law{

        public void sent(String source, String message, String dest) {
	 if(message.startsWith("release")) { //release(msg,host,port)
		String msg=getFromMessage(message,0);
		String host=getFromMessage(message,1);
		int port = Integer.parseInt(getFromMessage(message,2));

		doRelease(Self, msg, host, port);
		return;
	 }
	 doForward();
        }

	public void arrived(String source, String message, String dest) {
	 doDeliver();
	}

        public void submitted(String source, int port, String message, String dest) {
	 doDeliver(source+":"+port,message,dest);
        }

        public void disconnected() {
	 doQuit();
        }

        /**
        Get the argument no index from the message argument, assuming that the message is 
        properly formed as a regular expression.
        */
        public String getFromMessage(String message, int index) {
           Term tmessage = Term.parse(message);
           if( !(tmessage.type == Term.CType || tmessage.type == Term.LType))
                return null;
           if(tmessage.getArity()<=index)
                return null;
           return tmessage.getSubTerm(index).toString();
        }
}
This law can be found at:
http://www.moses.rutgers.edu/examples/external/external.java1

This is the external agent:

import java.net.*;
import java.util.*;
import java.io.*;
import java.security.*; 

import moses.member.*;
import moses.util.*;
import moses.security.*;

public class EAgent{
    public static void  main (String[] args) throws Exception { 
	
      ExMember ex = new ExMember(9002,9000);
      Answer an;
      for (;;) {
	  an = ex.generic_receive_lg();
	  System.out.println(an.source+" "+an.s_payload);
    
	  ex.send("reply"+an.s_payload,an.source);
	  System.out.println( "reply"+an.s_payload +" sent back");
      }
    }
}
This source code can be found at:
http://www.moses.rutgers.edu/examples/external/EAgent.java

Running the example

In order to run this example, follow the steps below:

  • Download the EAgent.java file, and compile it on your local machine;
  • Launch the external agent by issuing:
    > java EAgent
    
    Note that EAgent's ExMember class creates an TCPIP server that waits for connections on port 9002. In order to have the example running, make sure that the port above is free and the firewall allows connections on this port. If not, change the port number in the code and the law accordingly.
  • Using the human interface create an agent to operate under the external law;
  • Using the human interface, send a message of the form release(somemessage,hostname,9002), where hostname stands for the hostname where the external agent runs (it can be the same host). The destination of this message can be "any".
  • Observe how the external agent has received the message, and how the agent received the reply. If an exception is delivered instead, check that the port number in the external agent's code matches the port number in the message.
  • In order to observe messages initiated by the external agent, change its code such that it sends messages first. Make sure that every message has a proper, existing destination.