Skip to content

Commit

Permalink
Issue 10 create elementary agent (#35)
Browse files Browse the repository at this point in the history
* Create Elementary Agent (#10)

* Rename Agents Folder (#10)
  • Loading branch information
GNeves95 committed May 14, 2021
1 parent 76ae219 commit e20bebf
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/agents/Elementary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package agents;

import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.*;
import jade.domain.FIPAException;
import utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

public class Elementary extends Agent {
protected ArrayList<AID> logisticsAgents;
protected boolean registerServices(ArrayList<ServiceDescription> serviceDescriptions){
DFAgentDescription agentDescription = new DFAgentDescription();
agentDescription.setName(getAID());
for(ServiceDescription serviceDescription : serviceDescriptions){
agentDescription.addServices(serviceDescription);
}

try {
DFService.register(this, agentDescription );
}
catch (FIPAException fe) {
Utils.print(getAID().getName(), "Failed to register.");
Utils.print(getAID().getName(), fe.getMessage());
return false;
}

return true;
}

public ArrayList<AID> getAgencyAgents() {
return logisticsAgents;
}

public void setAgencyAgents(ArrayList<AID> logisticsAgents) {
this.logisticsAgents = logisticsAgents;
}

protected void takeDown(){
try {
DFService.deregister(this);
} catch (FIPAException fe) {
fe.printStackTrace();
}
}

public AID getAgentByID(String id){
for (AID agent : logisticsAgents){
if(id.equals(agent.getLocalName())){
return agent;
}
}
return null;
}

protected void updateAgents(ArrayList<AID> agents, String type){
DFAgentDescription agentDescription = new DFAgentDescription();
ServiceDescription serviceDescription = new ServiceDescription();
serviceDescription.setType(type);
agentDescription.addServices(serviceDescription);

try {
DFAgentDescription[] result = DFService.search(this, agentDescription);
agents.clear();
for(DFAgentDescription agent : result){
agents.add(agent.getName());
}
} catch (FIPAException e) {
e.printStackTrace();
}
}

protected boolean registerInYellowPages(String type, String name) {
ServiceDescription serviceDescription = new ServiceDescription();
serviceDescription.setType(type);
serviceDescription.setName(name);

return registerServices(new ArrayList<ServiceDescription>(Arrays.asList(serviceDescription)));
}

public synchronized void log(String message) {
Date a = new Date();
System.out.println(a.toString() + " -> " + getLocalName() + ": " + message);
}
}
4 changes: 4 additions & 0 deletions src/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ public static Document openAndParseXmlFile(String mapXmlFile) {
return null;
}
}

public static void print(String name, String s) {
System.err.println(name + ": " + s);
}
}

0 comments on commit e20bebf

Please sign in to comment.