Общение агента Jade: ответ агента

Я делаю проект проекта MAS на нефрите. У меня есть 2 агента, один для отправки и один для получения. Как заставить агента 1 отправить сообщение агенту 2, например, «1000», и только когда агент 2 получит «1000», агент 2 ответит, например, «выключить»?


person newtojade    schedule 21.11.2017    source источник
comment
Второй тег - ДЖЕЙД, а не мопс. Pug — это механизм HTML-шаблонов node.js, который ранее назывался Jade.   -  person cdaiga    schedule 21.11.2017
comment
Я набрал jade, но получилось мопс..   -  person newtojade    schedule 21.11.2017
comment
Правильный тег agents-jade.   -  person cdaiga    schedule 21.11.2017


Ответы (3)


Я предлагаю создать агент 1 с поведением RequestPerformer и агент 2 с циклическим поведением для прослушивания сообщений.

Содержание поведения агента1 может быть таким:

ACLMessage cfp = new ACLMessage(ACLMessage.CFP);
cfp.addReceiver(/*agent2AID*/);

cfp.setContent("1000");
cfp.setConversationId(1000);
cfp.setReplyWith("cfp" + System.currentTimeMillis()); // Unique value
myAgent.send(cfp);
mt = MessageTemplate.and(MessageTemplate.MatchConversationId(targetProduct),
                    MessageTemplate.MatchInReplyTo(cfp.getReplyWith()));

поведение agent2 может быть таким:

частный класс CFPServer extends CyclicBehaviour { private static final long serialVersionUID = 1L;

public void action() {
    MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.CFP);
    ACLMessage msg = myAgent.receive(mt);
    if (msg != null) {
        // CFP Message received. Process it
        String title = msg.getContent();
        ACLMessage reply = msg.createReply();

        // The requested fruit is NOT available for sale.
        reply.setPerformative(ACLMessage.INFORM);
        reply.setContent("turn off");

        myAgent.send(reply);

    } else {
        block();
    }
}

}

Связанная проблема очень хорошо обсуждается и решается многими способами, вы можете проверить следующую реализацию простого протокола Contract Net, где вы можете найти функции отправки и ответа:

https://github.com/clebercbr/tp_cnp/blob/master/src/java/ Обратите внимание, что инициатор агента подобен агенту1, а участник и отклонитель — вашему агенту2.

person Cleber Jorge Amaral    schedule 21.11.2017

Я не рекомендую писать циклическое поведение для приема сообщений, это сожжет прок впустую. SimpleBehaviour с done() в false и block() в action() намного эффективнее.

Что касается вашего pb, что-то вроде этого должно работать:

public class ReceiveMessageBehaviour extends SimpleBehaviour{

       private boolean finished=false;

    /**
     * 
     * This behaviour is a one Shot.
     * It receives a message tagged with an inform performative, print the content in the console and destroy itself if its equal to 1000
     * @param myagent
     */
       public ReceiveMessageBehaviour(final Agent myagent) {
           super(myagent);
       }

       public void action() {
            //1) receive the message
            final MessageTemplate msg Template = MessageTemplate.MatchPerformative(ACLMessage.INFORM);          
            final ACLMessage msg = this.myAgent.receive(msgTemplate);
            //2) check its caracts
            if (msg != null && msg.getContent().equals("1000")) {       
                System.out.println(this.myAgent.getLocalName()+"<----Result received from "+msg.getSender().getLocalName()+" ,content= "+msg.getContent());
                this.finished=true;

                //3) answer
                final ACLMessage msg2 = new ACLMessage(ACLMessage.INFORM);
                msg2.setSender(this.myAgent.getAID());
                msg2.addReceiver(new AID(msg.getSender().getLocalName(), AID.ISLOCALNAME));     
                msg2.setContent("turn off");
                this.myAgent.send(msg2);

            }else{
                block();// the behaviour goes to sleep until the arrival of a new message in the agent's Inbox.
            }
      }

    public boolean done() { return finished;}
}
person Hc.    schedule 12.07.2019

Я новичок в этих компонентах, связанных с JADE. Но я хотел бы добавить самый простой код, который мы можем использовать для решения этой проблемы, используя oneShotBehaviour и Cyclicehaviour.

Следующий код обеспечивает простую связь между двумя клиентами.

Агент класса А:-

public class Agent_A extends Agent{
  
    protected void setup(){
    
    System.out.println(getAID().getName()+" is ready.");  
    addBehaviour(new AgentA_SendMessage());
    addBehaviour(new AgentA_ReceiveMessage()); 
    }
    
    public class AgentA_SendMessage extends OneShotBehaviour{
        @Override
        public void action(){
            ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
            msg.addReceiver(new AID("AgentB",AID.ISLOCALNAME));
            msg.setContent("1000");
            send(msg);
        }
    }
    
    public class AgentA_ReceiveMessage extends CyclicBehaviour{
        Scanner scn2 = new Scanner(System.in);
        @Override
        public void action(){
        ACLMessage remsg = myAgent.receive();
            if(remsg!=null){
                String reply = remsg.getContent();
                System.out.println("Reply From "+remsg.getSender()+" :- "+reply);
            }
            else{
            block();
            }
        }
    }
    
  
    
}

Агент класса B:-

public class Agent_B extends Agent{
    protected void setup(){
    System.out.println("Hello Seller Agent : "+getAID().getName()+" is ready.");
    addBehaviour(new AgentB_ReceiveMessage());
    }
    public class AgentB_ReceiveMessage extends CyclicBehaviour{
    
            @Override
            public void action(){
                ACLMessage remsg = myAgent.receive();
                
                    if(remsg!=null){
                        System.out.println(""+remsg);
                        ACLMessage reply = remsg.createReply();
                        reply.setPerformative(ACLMessage.INFORM);
                        String price = remsg.getContent();
                        reply.setContent("off");
                        send(reply);     
                    }else{
                        block();
                    }
            }
    }
}
person namal wijekoon    schedule 29.12.2020