Message Passing

Content = String

Content = Bean

Commands-As-Objects: Math Command

public class MathCommand implements java.io.Serializable {
�� private String operation; // +, *, -, /, %, <, etc.
�� private float arg1;
�� private float arg2;
�� public void setOperation(String op) { operation = op; }
�� public String getOperation() { return operation; }
�� public float getArg1() { return arg1; }
�� public void setArg1(float arg) { arg1 = arg; }
�� public float getArg2() { return arg2; }
�� public void setArg2(float arg) { arg2 = arg; }
}

Math Client

public class MathClient extends Agent {
�� protected void setup() {
����� MathCommand cmmd = new MathCommand();
����� cmmd.setOperation("*");
����� cmmd.setArg1(6);
����� cmmd.setArg2(7);
����� ACLMessage msg = new ACLMessage( ACLMessage.REQUEST );
�� �� msg.addReceiver(new AID( "dt", AID.ISLOCALNAME));
�� �� try {
����� �� msg.setContentObject(cmmd);
�� �� }
�� �� catch (Exception e) {
�������� System.err.println(e.getMessage());
����� }
�� �� send(msg);
�� �� ACLMessage result = blockingReceive();
�� �� System.out.println("result = " + result.getContent());
�� }
}

Math Server

A math server just adds a math behavior:

public class MathServer extends Agent {
�� protected void setup() {
����� addBehaviour(new MathBehavior(this));
�� }
}

Here's the server's behavior:

class MathBehavior extends CyclicBehaviour {
�� public MathBehavior(Agent a) { super(a); }

�� protected String execute(MathCommand cmmd) {
����� String op = cmmd.getOperation();
����� float arg1 = cmmd.getArg1();
����� float arg2 = cmmd.getArg2();
����� if (op.equals("+")) return "" + (arg1 + arg2);
����� if (op.equals("*")) return "" + (arg1 * arg2);
����� if (op.equals("-")) return "" + (arg1 - arg2);
����� if (op.equals("/")) return "" + (arg1 / arg2);
����� if (op.equals("%")) return "" + (arg1 % arg2);
����� if (op.equals("<")) return "" + (arg1 < arg2);
����� if (op.equals("=")) return "" + (arg1 = arg2);
����� return "unknown operator: " + op;
�� }

�� public void action() {
����� ACLMessage msg = myAgent.receive();
����� if (msg == null) { block(); return; } // cool!
����� try {
�������� MathCommand cmmd = (MathCommand)msg.getContentObject();
�� ����� String result = execute(cmmd);
�������� ACLMessage answer = msg.createReply();
�������� answer.setContent(result);
�������� myAgent.send(answer);
����� } catch(Exception e) {
�������� System.err.println(e.getMessage());
����� }
�� }
}

Content = ???

Useful Constants

public interface MathVocabulary {
�� public static final String MATH_COMMAND = "MathCommand";
�� public static final String MATH_COMMAND_ARG1 = "arg1";
�� public static final String MATH_COMMAND_ARG2 = "arg2";
�� public static final String MATH_COMMAND_OP = "operation";
}

The Math Ontology

public class MathOntology extends Ontology {
�� public static final String ONTOLOGY_NAME = "Math-Ontology";
�� // MathOntology is a singleton:
�� private static MathOntology instance = new MathOntology();
�� public static MathOntology getInstance() { return instance; }
�� private MathOntology() {
����� super(ONTOLOGY_NAME, BasicOntology.getInstance());
����� try {

�������� AgentActionSchema as =
����������� new AgentActionSchema(MathVocabulary.MATH_COMMAND);
�������� add(as, MathCommand.class);
�������� as.add(MathVocabulary.MATH_COMMAND_ARG1,
����������� (PrimitiveSchema) getSchema(BasicOntology.FLOAT),
����������� ObjectSchema.MANDATORY);
�������� as.add(MathVocabulary.MATH_COMMAND_ARG2,
����������� (PrimitiveSchema) getSchema(BasicOntology.FLOAT),
����������� ObjectSchema.MANDATORY);
�������� as.add(MathVocabulary.MATH_COMMAND_OP,
����������� (PrimitiveSchema) getSchema(BasicOntology.STRING),
����������� ObjectSchema.MANDATORY);
����� } catch(Exception e) {
�������� System.err.println(e.getMessage());
����� }
�� }
}

The Math Client

public class MathClient extends Agent {
�� private Codec codec = new SLCodec(); // ???
�� private Ontology ontology = MathOntology.getInstance();


�� protected void setup() {

����� // Register language and ontology
����� getContentManager().registerLanguage(codec);
����� getContentManager().registerOntology(ontology);

����� MathCommand cmmd = new MathCommand();
����� cmmd.setOperation("*");
����� cmmd.setArg1(6);
����� cmmd.setArg2(7);
����� sendMessage(
�������� ACLMessage.REQUEST, cmmd, new AID( "dt", AID.ISLOCALNAME));


�� �� ACLMessage result = blockingReceive();
�� �� System.out.println("result = " + result.getContent());
�� }



�� void sendMessage(
����� int performative, AgentAction action, AID server) {
����� ACLMessage msg = new ACLMessage(performative);
����� msg.setLanguage(codec.getName());
����� msg.setOntology(ontology.getName());
����� try {
�������� getContentManager().fillContent(
����������� msg, new Action(server, action));
�������� msg.addReceiver(server);
�������� send(msg);
����� } catch (Exception e) {
�������� System.err.println(e.getMessage());
����� }
�� }
}

The Math Server

public class MathServer extends Agent {
�� private Codec codec = new SLCodec();
��� private Ontology ontology = MathOntology.getInstance();
�� protected void setup() {
����� // Register language and ontology
����� getContentManager().registerLanguage(codec);
������� getContentManager().registerOntology(ontology);

����� addBehaviour(new MathBehavior(this));
�� }
}

Server behavior

class MathBehavior extends CyclicBehaviour {
�� // etc.
�� public void action() {
����� ACLMessage msg = myAgent.receive();
����� if (msg == null) { block(); return; } // cool!
����� try {
�������� ContentElement content =
����������� myAgent.getContentManager().extractContent(msg);
�������� Concept action = ((Action)content).getAction();
�������� MathCommand cmmd = (MathCommand)action;

�������� String result = execute(cmmd);
�������� ACLMessage answer = msg.createReply();
�������� answer.setContent(result);
�������� myAgent.send(answer);
����� } catch(Exception e) {
�������� System.err.println(e.getMessage());
����� }
�� }
}