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 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)).
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 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");
}
}
}
In order to run this example, follow the steps below:
EAgent.java file, and compile it on
your local machine;
> java EAgent
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".