distributed_automated/multi_agent_system/src/com/mas/main/test/Conveyor.java
<filename>distributed_automated/multi_agent_system/src/com/mas/main/test/Conveyor.java<gh_stars>0package com.mas.main.test;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

public class Conveyor extends Agent{
	/**
	 * 
	 */
	enum STATES {
	    IDLE,
	    BUSY,
	    DOWN
	}
	private String name;
	private STATES state;
	//key is conveyor, value is state
	private Map<Integer, String> conveyorStates = new HashMap<Integer, String>(); // 
	private Map<Integer, Boolean> conveyorHasPallet = new HashMap<Integer, Boolean>(); // 
	private Graph conveyor = new Graph();
	private Long transferTime = 5000L;
	protected void setup() {
		for(int i=1;i<15;i++) {
			conveyorStates.put(i, "IDLE");
			conveyorHasPallet.put(i, false);
			conveyor.addVertex(Integer.toString(i), "IDLE");
		}
		conveyor.addEdge("1", "2", 1);
		conveyor.addEdge("2", "3", 1);
		conveyor.addEdge("3", "4", 1);
		conveyor.addEdge("3", "13", 1);
		conveyor.addEdge("4", "5", 1);
		conveyor.addEdge("5", "6", 1);
		conveyor.addEdge("6", "7", 1);
		conveyor.addEdge("7", "8", 1);
		conveyor.addEdge("8", "14", 1);
		conveyor.addEdge("8", "9", 1);
		conveyor.addEdge("9", "10", 1);
		conveyor.addEdge("10", "11", 1);
		conveyor.addEdge("11", "12", 1);
		conveyor.addEdge("12", "1", 1);
		conveyor.addEdge("13", "14", 1);
		conveyor.addEdge("14", "12",  1);
		//Vertex ver = conveyor.vertices.get("13");
		//ver.state = "DOWN";
		List<Vertex> list = conveyor.shortestPath("1", "14");
		for(Vertex v: list) {
			System.out.println(v.name);
		}

		addBehaviour(new ConveyorBehaviour());
		addBehaviour(new ConveyorTransferBehaviour(this, this.transferTime));
	}
	private class ConveyorTransferBehaviour extends TickerBehaviour  {
		public ConveyorTransferBehaviour(Agent a, long period) {
			super(a, period);
		}

		@Override
		protected void onTick() {
			System.out.println("ConveyorTransferBehaviour Started");

			for(int i=1;i<15;i++) {
				if(conveyorStates.get(i).contentEquals("IDLE")) {
					conveyorStates.put(i, "BUSY");
				}else {
					conveyorStates.put(i, "IDLE");
				}
			}
			for(Integer key:conveyorHasPallet.keySet()) {
				if(key<conveyorHasPallet.keySet().size()) {
					if(conveyorHasPallet.get(key)&&(conveyorHasPallet.get(key+1)==false)) {
						conveyorHasPallet.put(key, false);
						conveyorHasPallet.put(key, true);
					}
				}else {
					if(conveyorHasPallet.get(key)&&(conveyorHasPallet.get(1)==false)) {
						conveyorHasPallet.put(key, true);
						conveyorHasPallet.put(key, false);
					}
				}
			}
		}

	}
	private class ConveyorBehaviour extends CyclicBehaviour {

		public void action() {
			MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.REQUEST);

			ACLMessage msg = receive(mt);
			if (msg != null) {
				String content = msg.getContent();
				System.out.println("msg: "+content);


				//CHANGE_STATE:1:IDLE example message to change state of conveyor
				if(content!=null&&msg.getContent().contains("CHANGE_STATE")) {
					String[] contentParts = content.split(":");
					String conveyorNumber = contentParts[1].trim();
					String newState = contentParts[2];
					if(!(newState.contentEquals("IDLE")||newState.contentEquals("DOWN")||newState.contentEquals("BUSY"))) {
						System.out.println("Wrong State");
						return;
					}
					conveyor.vertices.get(conveyorNumber).state=newState;
					System.out.println("State Updated:"+newState+" for conveyor:"+conveyorNumber);
					return;
				}


				String[] contentParts = content.split(",");
				String src = contentParts[0];
				String dst = contentParts[1];
				System.out.println("Pallet Agent Reqeust src:"+src+" dst:"+dst);
				
				
				

				Integer conveyorNumReq = Integer.parseInt(content);
				if(conveyorNumReq!=null&&conveyorStates.get(conveyorNumReq).contentEquals("IDLE")) {
					ACLMessage notifyMsg = new ACLMessage(ACLMessage.INFORM);
					notifyMsg.addReceiver(new jade.core.AID("PalletAgent", jade.core.AID.ISLOCALNAME));
					if(conveyorHasPallet.get(conveyorNumReq)) {
						notifyMsg.setContent("PALLET IS ALREADY on Conveyor"+conveyorNumReq);
						send(notifyMsg);
					}else {
						conveyorHasPallet.put(conveyorNumReq, true);
						notifyMsg.setContent("PALLET PLACED on Conveyor"+conveyorNumReq);
						send(notifyMsg);
					}
				}else if(conveyorNumReq!=null&&conveyorStates.get(conveyorNumReq).contentEquals("BUSY")) {
					ACLMessage notifyMsg = new ACLMessage(ACLMessage.INFORM);
					notifyMsg.addReceiver(new jade.core.AID("PalletAgent", jade.core.AID.ISLOCALNAME));
					notifyMsg.setContent("BUSY Conveyor"+conveyorNumReq);
					send(notifyMsg);
				}else if(conveyorNumReq!=null&&conveyorStates.get(conveyorNumReq).contentEquals("DOWN")) {
					ACLMessage notifyMsg = new ACLMessage(ACLMessage.INFORM);
					notifyMsg.addReceiver(new jade.core.AID("PalletAgent", jade.core.AID.ISLOCALNAME));
					notifyMsg.setContent("DOWN Conveyor"+conveyorNumReq);
					send(notifyMsg);
				}
			} else {
				// Block until a message is received
				block();
			}

		}
	}
}
