I try to write code in jade (Java Agent DEvelopment Framework) to print the binary tree of agents in sma. I want to print tree inorder traversal with origin algorithm:
- Traverse the left subtree, i.e., call Inorder(left-subtree)
- Visit the root.
- Traverse the right subtree, i.e., call Inorder(right-subtree)
And my node agent is:
class Noeud {
private Integer value = null;
private AID gauche = null;
private AID droit = null;
private AID pere = null;
}
In my agent to print, I use CyclicBehaviour
as extensions. With my code:
public class ReceiveAffichageBehaviour extends CyclicBehaviour{
@Override
public void action() {
MessValueGauche();
MessValue();
MessValueDroit();
}
private void MessValueGauche() {
MessageTemplate messageTemplate = MessageTemplate.and(
MessageTemplate.MatchOntology(Constants.AFFICHAGE_VALUE_GAUCHE),
MessageTemplate.MatchPerformative(ACLMessage.REQUEST)
);
ACLMessage receiveMsg = myAgent.receive(messageTemplate);
if (receiveMsg != null) {
String contenu = receiveMsg.getContent() + "(";
ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
if (noeud.getGauche() == null) {
message.setOntology(Constants.AFFICHAGE_VALUE);
message.addReceiver(myAgent.getAID());
} else {
message.setOntology(Constants.AFFICHAGE_VALUE_GAUCHE);
message.addReceiver(noeud.getGauche());
}
message.setContent(contenu);
myAgent.send(message);
} else {
block();
}
}
private void MessValue() {
MessageTemplate messageTemplate = MessageTemplate.and(
MessageTemplate.MatchOntology(Constants.AFFICHAGE_VALUE),
MessageTemplate.MatchPerformative(ACLMessage.REQUEST)
);
ACLMessage receiveMsg = myAgent.receive(messageTemplate);
if (receiveMsg != null) {
String contenu = receiveMsg.getContent() + (noeud.getValue() == null ? "" : noeud.getValue().toString());
ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
if (noeud.getDroit() == null){
message.setOntology(Constants.AFFICHAGE_VALUE_DROIT);
message.addReceiver(myAgent.getAID());
} else if (noeud.getDroit().equals(receiveMsg.getSender())){
message.setOntology(Constants.AFFICHAGE_VALUE_DROIT);
message.addReceiver(noeud.getPere());
} else {
message.setOntology(Constants.AFFICHAGE_VALUE_GAUCHE);
message.addReceiver(noeud.getDroit());
}
message.setContent(contenu);
myAgent.send(message);
} else {
block();
}
}
private void MessValueDroit() {
MessageTemplate messageTemplate = MessageTemplate.and(
MessageTemplate.MatchOntology(Constants.AFFICHAGE_VALUE_DROIT),
MessageTemplate.MatchPerformative(ACLMessage.REQUEST)
);
ACLMessage receiveMsg = myAgent.receive(messageTemplate);
if (receiveMsg != null) {
String contenu = receiveMsg.getContent() + ")";
ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
message.setContent(contenu);
if (noeud.getDroit() == null && noeud.getGauche() == null ){
if (noeud.getPere().equals(Constants.AID_RACINE)){
message.setPerformative(ACLMessage.CONFIRM);
} else {
message.setOntology(Constants.AFFICHAGE_VALUE_GAUCHE);
}
message.addReceiver(noeud.getPere());
}
myAgent.send(message);
} else {
block();
}
}
I don't know to fix the infini loop in my code. Who can help me the detailed algorithm instead the general? Or could fix my bug in code? Thank you!
A cyclicBehaviour is infinite by nature. Do not use a cyclic if you want the behaviour to stop. Just use a simpleBehaviour and set the finished boolean at true when you want to remove it from the agent.