Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Developing Compounds Behavior JADE Agents

Learn in this article how to develop compounds behaviors JADE agents

As described in the post entitled "Adding behaviors JADE agents" which gave an introduction on the development of behavior in JADE agents, the purpose of this post is to describe how to develop compounds behaviors JADE agents; a more complex way of working with behaviors JADE agents.

Compounds behaviors are more kinds of complex behaviors which are formed by sub-behavior; that is, a kind of behavior formed by the junction of several contained behaviors. In this new mode of developing behavior, the programmer develops a behavior that runs several other internal behaviors that can be the same or different types. Compounds behaviors are organized into three types of subclasses: SequentialBehaviour for running behaviors sequentially; ParallelBehaviour for running behaviors in parallel and FSMBehaviour for the implementation of behavior as a state machine. Thus, each of these classes of compounds behaviors may contain behaviors of type OneShotBehaviour, CyclicBehaviour, WakerBehaviour, TickerBehaviour and also developed by the programmer.

Sequential behavior

This behavior runs its sub-behaviors sequentially and ends when all its sub-behaviors are completed. The scheduling policy of the sub-behavior occurs as follows: the behavior starts running its first sub-behavior, when this sub-behavior is finalized (ie, his method done() returns true), the second sub-behavior runs and so on; when the last sub-behavior is finished, the sequence composed behavior is finalized. The sub-behaviors are added to the composite behavior with addSubBehaviour() method. Order as these sub-behaviors are added indicates the order of execution. To use SequentialBehaviour you must import the class "jade.core.behaviours.SequentialBehaviour". Listing 1 describs an agent that implements a type behavior SequentialBehaviour; They are added to this three sub-behavior behavior and they are WakerBehaviour, OneShotBehaviour and TickerBehaviour, respectively. Figure 1 highlights how the console window should appear when using the code in Listing 1.

Listing 1. agent code running behavior SequencialBehaviour compound

 import jade.core.Agent;
  import jade.core.behaviours.*;
      public class HelloWorldAgent extends Agent{
   
          protected void setup() {

              System.out.println("Hello! My name is " + getLocalName ( ));
              System.out.println("I'll execute three sequential behaviors:" );
              
              SequentialBehaviour behavior = new SequentialBehaviour(this) {

                  public int onEnd(){
                      myAgent.doDelete();
                      return 0;
                  }
              };
   
              behavior.addSubBehaviour(new WakerBehaviour(this,500) {
                  long t0 = System.currentTimeMillis();
                  protected void onWake() {
                      System.out.println((System.currentTimeMillis() - t0) + 
                              ":I've executed the first behavior of type WakeBehaviour!");
                 }
             });
   
              behavior.addSubBehaviour(new OneShotBehaviour(this) {
                  public void action() {
                      System.out.println("I've executed the second behavior of type OneShotBehaviour!");
                  }
              } );
   
              behavior.addSubBehaviour(new TickerBehaviour(this,700) {
                  
                  int exec = 0;
                  long t1 = System.currentTimeMillis();
                  
                  protected void onTick(){
                      
                      if(exec == 3){ 
                          stop();
                      } else {
                          System.out.println((System.currentTimeMillis() - t1) + 
                              ": I've executed the third behavior of type TickerBehaviour!");
                          exec++;
                      }
                  }
              });
   
              addBehaviour(behavior);
          }
          
          protected void takeDown() {
              System.out.println("Finalized with success!");
          }
          
      }

Figure 1. Implementation of SequencialBehaviour composite behavior.

Parallelbehaviour behavior

The ParallelBehaviours behavior implements a compound behavior that staggers its sub-behaviors in parallel. Every time the action() method of parallel behavior is executed, the action() method of its sub-behaviors will also be performed. Parallel behavior can be instructed to be finalized when all parallel conduct are complete, or when any of them is terminated. In addition, you can define the completion of the compound behavior for when a number n of sub-behaviors are finalized. These conditions are defined in the class constructor, passing as a parameter the when_all constant, when all completed behaviors when_any, when a finished behavior or an integer that specifies the number of sub-behaviors that are required to finalize the ParallelBehaviour. It is important to note that to use ParallelBehaviour you must import the class "import static jade.core.behaviours.ParallelBehaviour". You must also import the clauses that will be used; to WHEN_ALL clause, must be imported "import static jade.core.behaviours.ParallelBehaviour.WHEN_ALL"; to WHEN_ANY clause, must be imported "import static jade.core.behaviours.ParallelBehaviour.WHEN_ANY". Listing 2 illusrtates the description of an agent that runs parallel behaviors. Three behaviors type SimpleBehaviour are implemented; the first behavior ends after three interactions, the second behavior ends after seven interactions and the last behavior is finished after nine interactions. Behaviors SimpleBehaviour type are atomic behavior; a kind of abstract class of behavior that are made by a single task, monolithic and can not be stopped. The clause defines WHEN_ALL that parallel behavior will only be finalized when all sub-behaviors are executed. If you use the clause WHEN_ANY, parallel behavior will be finalized when the first sub-behavior is fully executed. Figure 2 illustrates the execution of a parallel behavior using the clause WHEN_ALL where all sub-behaviors are performed for parallel behavior is finalized; Figure 3 now illustrates the implementation of a parallel behavior using WHEN_ANY clause, where the parallel behavior is terminated when the first sub-end behavior to be performed in this example the behavior sub-one. ParallelBehaviour kind of behavior only allows you to use one clause at a time

Listing 2. Agent running behavior consisting ParallelBehaviour with clause WHEN_ALL

 import jade.core.Agent;
  import jade.core.behaviours.*;
  import static jade.core.behaviours.ParallelBehaviour.WHEN_ALL;
      public class HelloWorldAgent extends Agent{
   
          protected void setup() {
   
              System.out.println("Hi! I am the agent " + getLocalName());
              System.out.println("I'll execute three behaviors concurrently");
              
              ParallelBehaviour s = new ParallelBehaviour(this, WHEN_ALL){
                  
                  public int onEnd() {
                      System.out.println("Behavior finalized with success!");
                      return 0;
                  }
              };
   
              addBehaviour(s);
   
              s.addSubBehaviour(new SimpleBehaviour(this){
                  int quantity =1;
                  public void action(){
                      System.out.println("Behavior 1: Executing for the "+ quantity + " time");
                      quantity = quantity +1;
                  }
   
                  public boolean done(){
                      if(quantity==4) {
                          System.out.println("Behavior 1 - Finalized");
                          return true;
                      }else
                          return false;
                  }
              });
   
              s.addSubBehaviour(new SimpleBehaviour(this) {
                  int quantity =1;
                  public void action() {
                      System.out.println("Behavior 2: Executing for the " + quantity + " time");
                      quantity = quantity +1;
                  }
              
                  public boolean done() {
                      if(quantity==8) {
                          System.out.println("Behavior 2 - Finalized");
                          return true;
                      }else
                          return false;
                  }
              });
   
              s.addSubBehaviour(new SimpleBehaviour(this){
                  int quantity =1;
                  public void action() {
                      System.out.println("Behavior 3: Executing for the"+ quantity + " time");
                      quantity = quantity +1;
                  }
   
                  public boolean done() {
                      if( quantity==10) {
                          System.out.println(" Behavior 3 - Finalized");
                          return true;
                      }else
                              return false;
                  }
              });
          }        
  }    

Figure 2. Behavior parallel compound using the clause WHEN_ALL

Figure 3. Behavior parallel compound using the clause WHEN_ANY

One can also set a compound conduct finish after a certain number of completions of its sub-behaviors. Can be used as parameter an integer, for example: ParallelBehaviour s = new ParallelBehaviour(this, 2), where 2 indicates that after two sub-behaviors are finalized the compound behavior is terminated. It is extremely important that a value is used according to the number of implemented sub-behavior; if there are three sub-behaviors and be used as a numeric parameter that parallel behavior only finish after four sub-behaviors, its three sub-behaviors will be completed but the parallel behavior will be running indefinitely. Taking the code described in Listing 2 by changing the field of amount of sub-behaviors finalized for two, Figure 4 illustrates how it should appear in the console window. In this case the parallel conduct is terminated when a second sub-behavior is finalized.

Figure 4. Illustration composite behavior with stop condition equal to 2 completed sub-behaviors.

Fsmbehaviour behavior

This behavior is based on scheduling by a finite state machine (Finite State Machine). The FSMBehaviour executes each sub-behavior according to a finite state machine defined by the user. More specifically, each sub-behavior is a defined state in the finite state machine. It provides methods to register states (sub-behaviors) and transitions that define how it will be give-scaling of the sub-behaviors. The basic steps to set a FSMBehaviour are:

  • Register a unique behavior as initial state, passing as parameters the behavior and a string that names this state. For this one uses the registerFirstState() method;
  • Register one or more behaviors as final states using the registerLastState() method;
  • Register one or more behaviors as intermediate states using registerState() method;
  • For each state, record the transitions of this with the other states using the registerTransition() method. For example, suppose you have a state defined as X and Y as another state and you want to be informed that the transition will be made from X to Y state when the state X return the value 1. The method would be defined as registerTransition(X, Y, 1).

Similar to a sequential behavior, a behavior FSMBehaviour maintains a pointer to the sub current behavior. When a sub-behavior is complete (the method done() returns true), the FSMBehaviour behavior checks on its internal transition table and, on that basis, selects the next sub-behavior to trigger the next time your action( ) method runs. Transitions in a FSMBehaviour behavior are marked with an entire label. When a current sub-behavior FSMBehaviour behavior is completed, the return value of your onEnd() method is used as an output value and compared to the labels of all transitions that come out of the current state of subehavior. The first transition which label corresponds to this output value is used as your target state, which becomes the current sub-behavior to run. The registerState() method is used to add members to a FSMBehaviour instance, accepts two arguments: a string that defines the status of the name being registered and the sub-behavior that will be executed in this state. The registerTransition() method is used to add transitions for a FSMBehaviour instance, accepts three arguments: two strings that define the home state and the transition destination state and an integer value defining the transition label. The registerFirstState() methods and registerLastState() allow you to record the input state and the completion FSMBehaviour states, respectively. It is worth noting that one FSMBehaviour behavior can only contain one sub-behavior as the initial state, but may contain several sub-behaviors as final states. The FSMBehaviour behavior ends when a termination condition is reached and fully implemented.

Consider an agent that performs a certain behavior x. At the end of this behavior is checked if its operation has been completed. If this operation is not completed, az behavior is made. At the end of the z behavior, behavior x runs again and the whole check occurs again. In implementing that behavior x is completed, the last agent behavior is invoked, the y behavior.

Listing 3, the following describes the code of an agent having three behaviors structured as a state machine, as illustrated in Figure 5. As can be observed, the X behavior is invoked 5 times; when X behavior is finalized, it is checked if it has already run 5 times. If not, the FSMBehaviour behavior passes the state transition to the Z behavior that runs until finished. After Z behavior is finalized, the FSMBehaviour behavior passes the next transition state for behavior X. If X behavior has run 5 times, the FSMBehaviour behavior moves the next state to the Y behavior which is the final state of the machine States.

Listing 3. Agent with composite behavior of the state machine type

 import jade.core.Agent;
  import jade.core.behaviours. *;
     public class HelloWorldAgent extends Agent {
   
          protected void setup () {
   
              FSMBehaviour compFSM = new FSMBehaviour (this) {
                  public int onEnd () {
                      System.out.println ( "FSM Behavior completed successfully!");
                      return 0;
                  }
              };
              
              // The first state registration - Behavior X
              compFSM.registerFirstState (new OneShotBehaviour (this) {
                  int c = 0;
                  public void action () {
                      System.out.println ( "complement all the X Behavior");
                      C ++;
                  }
              
                  public int onEnd () {
                      return (c> 1 4: 0);
                  }
              } "X");
              
              // The second state registration - Behavior Z
              compFSM.registerState (new OneShotBehaviour (this) {
                  
                  public void action () {
                      System.out.println ( "complement all of Z Behavior");
                  }
                  
                  public int onEnd () {
                      return 2;
                  }
              }, "Z");
   
              // The third and final state registration - Behavior Z
              compFSM.registerLastState (new OneShotBehaviour (this) {
             
                  public void action () {
                      System.out.println ( "Running my last behavior.");
                  }
              }, "Y");
   
              // Definition of transactions
              // Transaction behavior X to Z, if onEnd () X behavior returns 0
              compFSM.registerTransition ( "X", "Z", 0);
              
              // Transaction behavior X to Y if onEnd () X behavior returns 1
              compFSM.registerTransition ( "X", "Y", 1);
   
   
              // Default transition setting (no matter what type of return)
              // As the state machine is finite, need to reset the states of X and Z Sring [] { "X", "Z"}
              compFSM.registerDefaultTransition ( "Z", "X", new String [] { "X", "Z"});
              
              // Trigger the behavior as state machine
              addBehaviour (compFSM);
   
          }
  }

As can be seen in Figure 5, the behavior of X was run five times and the end of each run Z behavior was invoked. When c>4, the X implementation to be complete and with it, the last behavior is invoked. As can be seen in the code in Listing 3, the code of this agent that was set a state with registerState (behavior, behavior name) method. The transitions were defined as registerTransition(Source, Destination, Source of Return). The registerDefaultTransition() method defines a standard transition, that is, a transition that always occurs from one state to the other regardless of the return achieved in the implementation of state.

It is important to emphasize that the line compFSM.registerDefaultTransition code ("Z", "X", new String[]{"X","Z"}) records a default transition between Z and X, but as both states have been executed and how are OneShot behaviors could only run once. For this, we have the argument new String[]{"X","Z"} zeroing information about these states, allowing to be run again.

Figure 5. Illustration of composite behavior of the implementation of the state machine type

As may have been observed in this post, there are pre-defined behaviors that lead to building more complex behaviors. These behaviors can be performed in three ways: sequential behaviors, parallel conduct and behavior as a state machine. These three behavior classes may involve several types of sub-behaviors to run. In the development process the analysis of business rules you may find what or which classes are best solutions for your application. Such as in standalone sequential behaviors applications are feasible; for distributed applications are the best behaviors that run parallel and/or as a state machine way. Even an application can make use of various types of different classes simultaneously.



Web developer and passioned for web design, SEO and front end technologies.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login