英文:
Algorithm or code to print the binary tree of agents
问题
以下是您提供的代码的翻译部分:
我尝试在JADE(Java Agent DEvelopment Framework)中编写代码,以打印SMA中代理的二叉树。我希望使用原始算法按顺序遍历打印树:
- 遍历左子树,即调用Inorder(left-subtree)
- 访问根节点。
- 遍历右子树,即调用Inorder(right-subtree)
我的节点代理是:
class Noeud {
private Integer value = null;
private AID gauche = null;
private AID droit = null;
private AID pere = null;
}
在我的打印代理中,我使用CyclicBehaviour
作为扩展。以下是我的代码:
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 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!
答案1
得分: 1
循环行为天生是无限的。如果不希望行为停止,请勿使用循环行为。只需使用SimpleBehaviour,并在希望将其从代理中移除时将finished布尔值设置为true。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论