Last active
October 7, 2015 10:30
-
-
Save prmichaelsen/0a1d16b80b9d407ffd51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.DecimalFormat; | |
import java.text.NumberFormat; | |
import java.util.AbstractCollection; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.Map; | |
import java.util.Queue; | |
import java.util.Random; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import java.util.concurrent.ArrayBlockingQueue; | |
import java.util.concurrent.ConcurrentSkipListMap; | |
import java.util.concurrent.LinkedBlockingQueue; | |
import java.util.concurrent.PriorityBlockingQueue; | |
import java.util.concurrent.Semaphore; | |
public class RequestGenerator{ | |
private Random rnd; | |
private int numFloors; | |
private int rate; //percent chance of generation | |
private Queue<Integer>[] floorMap; | |
private ConcurrentSkipListMap<Integer,Boolean> pickupFloors; | |
private int millisecondsPerFrame; | |
private static NumberFormat f; | |
private int capacity; | |
private int queueMax; | |
private int numDigits; | |
public static void main(String[] args) throws InterruptedException{ | |
RequestGenerator gen; | |
//try and build from the command line or use defaults | |
if(args.length==5){ | |
gen = new RequestGenerator( | |
Integer.parseInt(args[0]), | |
Integer.parseInt(args[1]), | |
Integer.parseInt(args[2]), | |
Integer.parseInt(args[3]), | |
Integer.parseInt(args[4])); | |
} | |
else | |
gen = new RequestGenerator(20,1,10,50,250); | |
Elevator lift = gen.new Elevator(); | |
} | |
public RequestGenerator(int numFloors, int capacity, int queueMax, int rate, int milliSecondsPerFrame){ | |
rnd = new Random(); | |
this.numFloors = numFloors; | |
this.capacity = capacity; | |
this.queueMax = queueMax; | |
this.rate = rate; | |
this.millisecondsPerFrame = milliSecondsPerFrame; | |
floorMap = new LinkedBlockingQueue[numFloors]; | |
pickupFloors = new ConcurrentSkipListMap<Integer,Boolean>(); | |
for(int i = 0; i < numFloors; i++) | |
floorMap[i] = new LinkedBlockingQueue<Integer>(); | |
//create a format for printing | |
String format = numFloors+""; | |
format = format.replaceAll(".","0"); | |
f = new DecimalFormat(format+" "); | |
numDigits = format.length(); | |
(new Timer()).scheduleAtFixedRate(new TimerTask(){ | |
@Override | |
public void run(){ | |
generate(); | |
} | |
}, millisecondsPerFrame, millisecondsPerFrame); | |
} | |
public void generate(){ | |
if(rnd.nextInt(100)<rate){ | |
int pickupFloor = rnd.nextInt(numFloors); | |
int destinationFloor = rnd.nextInt(numFloors); | |
//only allow people with valid destinations | |
//only allow a wait queue per floor to queueMax | |
if(pickupFloor!=destinationFloor && floorMap[pickupFloor].size() < queueMax){ | |
floorMap[pickupFloor].offer(destinationFloor); | |
pickupFloors.putIfAbsent(pickupFloor,true); | |
} | |
} | |
} | |
public String toString(){ | |
String result = ""; | |
for(int i=0;i<numFloors;i++) | |
result+=floorMap[i]+"\n"; | |
return result; | |
} | |
public class Elevator{ | |
private boolean goingUp; | |
private int currentFloor; | |
private int destinationFloor; | |
private int size=0; | |
private ArrayBlockingQueue<Integer> passengers; | |
public Elevator(){ | |
Elevator lift = this; | |
goingUp = true; | |
currentFloor = 0; | |
destinationFloor = 0; | |
passengers = new ArrayBlockingQueue<Integer>(capacity); | |
(new Timer()).scheduleAtFixedRate(new TimerTask(){ | |
@Override | |
public void run(){ | |
System.out.println(lift); | |
if(goingUp){ | |
pickupFloors.get(currentFloor); | |
//allow all exiting passengers on this floor to get off | |
while(passengers.remove(currentFloor)); | |
// pickupFloors.remove(currentFloor); | |
//allow passengers going up to board | |
//boolean clearBell = false; | |
for (Iterator<Integer> it = floorMap[currentFloor].iterator(); it.hasNext();){ | |
int passenger = it.next(); | |
if(passenger>currentFloor) | |
if(passengers.offer(passenger)) | |
it.remove(); | |
} | |
if(floorMap[currentFloor].isEmpty()) | |
pickupFloors.remove(currentFloor); | |
if(!pickupFloors.isEmpty()){ | |
destinationFloor = pickupFloors.lastKey(); | |
} | |
for (Iterator<Integer> it = passengers.iterator(); it.hasNext();){ | |
int passengerDestination = it.next(); | |
if(passengerDestination>destinationFloor) | |
destinationFloor=passengerDestination; | |
} | |
if(destinationFloor>currentFloor) | |
currentFloor++; | |
else | |
goingUp=false; | |
}else{ | |
//allow all exiting passengers on this floor to get off | |
while(passengers.remove(currentFloor)); | |
//pickupFloors.remove(currentFloor); | |
//allow passengers going down to board | |
boolean clearBell = false; | |
for (Iterator<Integer> it = floorMap[currentFloor].iterator(); it.hasNext();){ | |
int passenger = it.next(); | |
if(passenger<currentFloor) | |
if(passengers.offer(passenger)) | |
it.remove(); | |
} | |
if(floorMap[currentFloor].isEmpty()) | |
pickupFloors.remove(currentFloor); | |
if(!pickupFloors.isEmpty()){ | |
destinationFloor = pickupFloors.firstKey(); | |
} | |
for (Iterator<Integer> it = passengers.iterator(); it.hasNext();){ | |
int passengerDestination = it.next(); | |
if(passengerDestination<destinationFloor) | |
destinationFloor=passengerDestination; | |
} | |
if(destinationFloor<currentFloor) | |
currentFloor--; | |
else | |
goingUp=true; | |
} | |
} | |
}, millisecondsPerFrame, millisecondsPerFrame); | |
} | |
@Override | |
public String toString(){ | |
String result = ""; | |
//result+=pickupFloors + "\n"; | |
result+=String.format( | |
"On floor %d, going " | |
+ ((goingUp)? "up ":"down ") | |
+ "to floor %d\n",currentFloor,destinationFloor); | |
for(int i=numFloors-1;i>=0;i--){ | |
result+= f.format(i); | |
if(pickupFloors.containsKey(i)) | |
result+= "o "; | |
else | |
result+= "- "; | |
if(i == currentFloor){ | |
String floorResult = floorMap[i]+""; | |
while(floorResult.length()<numDigits + (numDigits+2)*queueMax) | |
floorResult+=" "; | |
result+=floorResult + "> " + passengers + "\n"; | |
}else | |
result+=floorMap[i]+"\n"; | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment