This example is designed to show the interaction between two communities. The example is limited to showing the exchange of messages between two agents that operate under two different laws. In order to run this example, two laws are available, one for each agent (both in prolog and java). The laws show how the communication between two communities is disabled unless the law allows the exchange for speciffic messages.
The laws in Prolog follow:
law(exim1,language(prolog)). portal(exim2,lawURL(http://www.moses.rutgers.edu/examples/exim/exim2.law)) sent(X,foreign(M),Y) :- do(forward(X,M,[Y,exim2])). sent(X,M,Y) :- do(forward). arrived(X,M,Y) :- do(deliver). arrived([X,exim2],M,Y) :- do(deliver(X,foreign(M,PeerHash),Y)). disconnected :- do(quit). exception(E,Fc) :- do(deliver(Self,exc(E,Fc),Self)).
law(exim2,language(prolog)). portal(exim1,lawURL(http://www.moses.rutgers.edu/examples/exim/exim1.law)) sent(X,foreign(M),Y) :- do(forward(X,M,[Y,exim1])). sent(X,M,Y) :- do(forward). arrived(X,M,Y) :- do(deliver). arrived([X,exim1],M,Y) :- do(deliver(X,foreign(M,PeerHash),Y)). disconnected :- do(quit). exception(E,Fc) :- do(deliver(Self,exc(E,Fc),Self)).
The laws in Java follow:
law(exim1,language(java))
portal(exim2,lawURL(http://www.moses.rutgers.edu/examples/exim/exim2.java1))
/*the portal of the law defines other laws to interact with*/
import java.util.*;
import java.io.*;
public class exim1 extends Law{
public void sent(String source, String message, String dest, String destlaw) {
if(message.startsWith("foreign")){
//messages starting with foreign are forwarded using the 4 argument doForward()
String msg = getContentFromMessage(message);
doForward(source,msg,dest,"exim2");
} else //every other message is a community message
doForward();
}
public void arrived(String source, String sourcelaw, String message, String dest) {
//if the messages are coming from a different law, display
//them along with the hash of the foreign law
if(sourcelaw.equals("exim2"))
doDeliver(source,"foreign("+message+","+PeerHash+")",dest);
else //all other messages are delivered
doDeliver();
}
public void disconnected() {
doQuit();
}
public void exception(Message m, String failurecause) {
doDeliver("controller","exception("+failurecause+")",Self);
}
/*helper method that parses the argument of a regular expression*/
public String getContentFromMessage(String anyMessage) {
int index = anyMessage.indexOf("(");
if (index == -1)
return "";
else
return anyMessage.substring(index+1, anyMessage.length()-1);
}
}
law(exim2,language(java))
portal(exim1,lawURL(http://www.moses.rutgers.edu/examples/exim/exim1.java1))
/*the portal of the law defines other laws to interact with*/
import java.util.*;
import java.io.*;
public class exim2 extends Law{
public void sent(String source, String message, String dest, String destlaw) {
if(message.startsWith("foreign")){
//messages starting with foreign are forwarded using the 4 argument doForward()
String msg = getContentFromMessage(message);
doForward(source,msg,dest,"exim1");
} else //every other message is a community message
doForward();
}
public void arrived(String source, String sourcelaw, String message, String dest) {
//if the messages are coming from a different law, display
//them along with the hash of the foreign law
if(sourcelaw.equals("exim1"))
doDeliver(source,"foreign("+message+","+PeerHash+")",dest);
else //all other messages are delivered
doDeliver();
}
public void disconnected() {
doQuit();
}
public void exception(Message m, String failurecause) {
doDeliver("controller","exception("+failurecause+")",Self);
}
/*helper method that parses the argument of a regular expression*/
public String getContentFromMessage(String anyMessage) {
int index = anyMessage.indexOf("(");
if (index == -1)
return "";
else
return anyMessage.substring(index+1, anyMessage.length()-1);
}
}
In order to run this example, follow the steps below:
x,y under law exim1, andz under law exim2somemessage between x and
z. Observe the message exception(destinationLawMismatch)foreign(somemessage) between
x and z. Observe that the message is delivered at the destination
asforeign(some,F13FEC0E252AB6AA282EE4CD75D91828)