Description

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 law

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 Prolog laws can be found at:
http://www.moses.rutgers.edu/examples/exim/exim1.law
http://www.moses.rutgers.edu/examples/exim/exim2.law

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); 
	}
}
The Java laws can be found at:
http://www.moses.rutgers.edu/examples/exim/exim1.java1
http://www.moses.rutgers.edu/examples/exim/exim2.java1

Running the example

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

  • using the human (web-based) interface, start three agents:
    x,y under law exim1, and
    z under law exim2

    Note: if you are using Prolog laws, you need to have at least two controllers -- one for x and y and another one for z. This is due to the fact that currently our controller implementation supports a single Prolog law.
  • Send a message between x and y and observe how the message is delivered.
  • Try to send the message somemessage between x and z. Observe the message
    exception(destinationLawMismatch)
    displayed at the source. Observe the same message happening both ways. The exception is raised as a result of trying to send a message to an agent under a different law, without specifically directing so;
  • Try to send the message foreign(somemessage) between x and z. Observe that the message is delivered at the destination as
    foreign(some,F13FEC0E252AB6AA282EE4CD75D91828)
  • Try various combinations of messages between all three agents, and observe the effect;
  • Modify the laws provided above such that you can have communication between a Prolog law and a Java law. In order to do that, modify the preamble of both laws such that they reflect the appropriate law pair. Also, check the code of the law for references to the portal name and check it for consistency.