Skip to content

Commit

Permalink
Assignment 4 schedule and aggregation of order #12
Browse files Browse the repository at this point in the history
reading client file properly in agent
  • Loading branch information
Dr4gonbl4de committed Nov 17, 2018
1 parent d964d03 commit 5c08a25
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 70 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ repositories {

dependencies {
compile group: 'com.tilab.jade', name: 'jade', version: '4.5.+'
compile group: 'org.json', name: 'json', version: '20180813'
compile group: 'org.json', name: 'json', version: '20180813+'
compile group: 'org.openjfx', name: 'javafx', version: '12-ea+2'
//testCompile 'junit:junit:4.12.+'
//testCompile 'org.mockito:mockito-core:1.10.+'
}
94 changes: 25 additions & 69 deletions src/main/java/org/team_pjt/agents/ClientDummy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,92 +15,48 @@
import org.json.JSONObject;
import org.team_pjt.behaviours.receiveKillMessage;
import org.team_pjt.behaviours.shutdown;
import org.team_pjt.objects.Location;
import org.team_pjt.objects.Order;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ClientDummy extends Agent {
public class ClientDummy extends BaseAgent {
private AID[] aidSchedulerAgents;
private List<Order> orders;
private String guid;
private String name;
private int type;
private Location location;

protected void setup(){
Object[] oArguments = getArguments();
if(!readArgs(oArguments)){
System.out.println("No parameters given for ClientDummy " + getName());
}
registerAgent();
addBehaviour(new OneShotBehaviour() {
@Override
public void action() {
DFAgentDescription template = new DFAgentDescription();
ServiceDescription sd = new ServiceDescription();
sd.setType("OrderProcessing");
template.addServices(sd);
try {
DFAgentDescription[] result = DFService.search(myAgent, template);
aidSchedulerAgents = new AID[result.length];
for (int i = 0; i < result.length; ++i){
aidSchedulerAgents[i] = result[i].getName();
}
} catch (FIPAException e) {
e.printStackTrace();
}
ACLMessage aclMessage = new ACLMessage(ACLMessage.QUERY_IF);
for(int i = 0; i < aidSchedulerAgents.length; i++){
aclMessage.addReceiver(aidSchedulerAgents[i]);
}
myAgent.send(aclMessage);
}
});
addBehaviour(new WakerBehaviour(this, 1000) {
@Override
protected void onWake() {
ACLMessage aclReceiveOffer = myAgent.receive(MessageTemplate.MatchPerformative(ACLMessage.PROPOSE));
if(aclReceiveOffer != null && aclReceiveOffer.getPerformative() == ACLMessage.PROPOSE){
String sContent = aclReceiveOffer.getContent();
JSONObject jsonObjectResponse = new JSONObject(sContent);
JSONObject jsoProducts = jsonObjectResponse.getJSONObject("products");
Iterator<String> iKeys = jsonObjectResponse.keys();
while (iKeys.hasNext()){
System.out.println(iKeys.next());;
}
// Iterator<Object> iJsonArrayResponse = jsonArrayResponse.iterator();
// while(iJsonArrayResponse.hasNext()){
// JSONObject jsonObject = (JSONObject) iJsonArrayResponse.next();
// if(jsonObject.isEmpty()){
// System.out.println("No demanded products are available");
// } else{
// JSONObject joProducts = jsonObject.getJSONObject("products");
// Iterator<String> iProductKeys = joProducts.keys();
// while (iProductKeys.hasNext()){
// System.out.println(iProductKeys.next() + "is available");
// }
// }
// }
}
}
});
addBehaviour(new receiveKillMessage());
register("customer", guid);
addBehaviour(new shutdown());

}

private void registerAgent() {
DFAgentDescription dfd = new DFAgentDescription();
dfd.setName(getAID());
ServiceDescription sd = new ServiceDescription();
sd.setType("dummyCustomer");
sd.setName(getName());
dfd.addServices(sd);
try {
DFService.register(this, dfd);
} catch (FIPAException e) {
e.printStackTrace();
}
}

private boolean readArgs(Object[] oArguments) {
JSONObject joClient;
if(oArguments != null && oArguments.length > 0){
String client_string = ((String)oArguments[0]).replaceAll("###", ",");
joClient = new JSONObject(client_string);
this.guid = joClient.getString("guid");
this.name = joClient.getString("name");
this.type = joClient.getInt("type");
this.location = new Location(joClient.getJSONObject("location").getDouble("y"),joClient.getJSONObject("location").getDouble("x"));

orders = new LinkedList<>();

Iterator<Object> order_iterator = joClient.getJSONArray("orders").iterator();
while(order_iterator.hasNext()) {
JSONObject joOrder = (JSONObject)order_iterator.next();
orders.add(new Order(joOrder.toString()));
}

return true;
}
return false;
Expand Down

0 comments on commit 5c08a25

Please sign in to comment.