Search code examples
agents-jade

Send objects between agents


I have two jade agents and i want to send an object from one agent to the other, i used setContentObject() and getContentObject() methods, it worked fine the second agent receives the object, but i can't seem to find a way to get the object's variables, for example:

public class Person implements Serializable{
  private String firstName;
  private String lastName;
  public String getFirstName(){
     return firstName;
  }
  public void setFirstName(String fName){
     this.firstName=fName;
  }
  public String getLastName(){
     return lastName;
  }
  public void setFirstName(String lName){
     this.lastName=lName;
  }
}

Agent1:

public class Agent1 extends Agent{
  @Override
    protected void setup() {
        addBehaviour(new OneShotBehaviour(this) {
          @Override
          public void action(){
          ACLMessage aclmsg = new ACLMessage(ACLMessage.REQUEST);
          aclmsg.addReceiver(new AID("Agent2", AID.ISLOCALNAME));
          aclmsg.setContentObject(Person);
          send(aclmsg);
          });
        }
    }
}

Agent2

public class Agent2 extends Agent{
      @Override
        protected void setup() {
            addBehaviour(new CyclicBehaviour(this) {
            @Override
            public void action(){
             ACLMessage msg = receive();
             if(msg!=null){
               //Here i want to get the first name of the object Person
             }             
            });

           }
        }
}

So my question is how can i retrieve the firstName of the sent object Person?


Solution

  • ((Person)msg.getContentObject()).getFirstName()
    

    Read this