2

I have some problem with object type in JADE. In order to send object message to another agent I am using a method ACLMessage.setContentObject. However when I want to extract particular values/fields from the received message problem is arising.

Additionaly in sending agent I have created class to make possible sending message with object content:

class BusData implements java.io.Serializable
{
public Double current;
public Double power1;
public Double power2;
public Double voltage;
}

Receiving agent has following code (part):

data = MessageTemplate.MatchConversationId("measure");
ACLMessage data1 = receive(data); //receiving data with defined template

    if (data1!=null) {      

            for (int i = 0; i < Agents.length; ++i) {           

                try {
                    Data1 received_data = (Data1)data1.getContentObject();  
                    } catch (UnreadableException e) {
                        e.printStackTrace();}

                      //Serializable so = (Serializable)data1;
                      //System.out.println(Agents[i].getName()); //from whom received             

                      if (data1!=null) { 
                          System.out.println(getLocalName()+ " Info from " + data1.getSender().getName()); //from whom received   
                      }
            }                     
    }

Should I add in receiving agent similar class, e.g. BusData1 with similar variables, like in sending agent in order to extract message content? I am quite new in Java so I am asking for understanding.

Every hint will be helpful. Regards

Przem
  • 75
  • 1
  • 6

1 Answers1

0

Its too late for you but someone might be looking for it :)

In your receiving agent, instead of: Data1 received_data = (Data1)data1.getContentObject();

Just do : BusData received_data = (BusData)data1.getContentObject();

You can then access your received_data public variables (The cast allows the system to understand what is the serialized object's class to trigger the unserialization).

Tip : You should use variables names that correspond to their content.

  • data --> messageTemplate
  • data1 --> receivedMessage
Hc.
  • 86
  • 4
  • Hey, thank you for response and sorry for my late response. I am not using JADE since couple of years but who knows... maybe one day I will use that again. – Przem Mar 14 '23 at 20:05