Demo entry 6311009

AgentCreator

   

Submitted by anonymous on Oct 24, 2016 at 15:00
Language: Java. Code size: 1.8 kB.

/**
 * Created by Rafiul Sabbir on 24/10/16.
 */

import jade.core.Agent;
import jade.core.behaviours.*;
import jade.core.AID;
import jade.wrapper.AgentContainer;  //1- Ipmort Appropriate Jade’s Class
import jade.wrapper.AgentController; // 2- Import Appropriate Jade’s Class
import jade.lang.acl.*;

public class AgentCreator extends Agent { //3- Complete the Class definition
    String name = "Bender-The-Offender" ;
    AID Bender = new AID(name, AID.ISLOCALNAME); // 4-Complete the code to Instantiate this Object from AID class

    protected void setup() {
        AgentContainer c = getContainerController(); // 5-Get the name of current container and assign it to “c”
        try {
            AgentController a = c.createNewAgent(name, "Creature", null ); // 6-Create Bender-The-Offender in the current container
            // 7-Start your agent
            a.start();
        System.out.println("+++ Created: " + Bender);
        }
        catch (Exception e){

        }

        addBehaviour(new SimpleBehaviour() { // 8-Create a behavior from an appropriate Behaviour Type
            int n = 0;

            public void action() {
                ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
                msg.setContent("Hi");
                msg.addReceiver(Bender); //9- Add the Receiver
                System.out.println("+++ Sending: " + n);
                send(msg); //10- Send the Message
                block( 1000 );
            }

            public boolean done() {
                // 11-use variable ”n” to run this Behaviour 3 times
                if (n == 3) {
                    return true;
                } else {
                    n += 1;
                }
                return false;
            }
        });
    }

}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).