added trips instead of stop list
This commit is contained in:
parent
16fe398f5f
commit
dd474b686a
5 changed files with 96 additions and 66 deletions
|
@ -9,12 +9,14 @@ import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
static private int numBusses = 5;
|
static private int numBusPerTrip = 1;
|
||||||
|
static private int numTrips = 1;
|
||||||
|
static private int numBusses = numTrips * numBusPerTrip;
|
||||||
static private int numStopPerBus = 8;
|
static private int numStopPerBus = 8;
|
||||||
static private int numStops = numBusses * numStopPerBus;
|
static private int numStops = numBusses * numStopPerBus;
|
||||||
static private int numPassengersPerBus = 10;
|
static private int numPassengersPerBus = 10;
|
||||||
static private int numPassengersPerStop = 3;
|
static private int numPassengersPerStop = 5;
|
||||||
static private int numPassengers = numPassengersPerStop * numStops;
|
//static private int numPassengers = numPassengersPerStop * numStops;
|
||||||
static private int numThreadsBus = 5;
|
static private int numThreadsBus = 5;
|
||||||
static private int numThreadsStop = 2;
|
static private int numThreadsStop = 2;
|
||||||
static private int timeBetweenStops = 5;
|
static private int timeBetweenStops = 5;
|
||||||
|
@ -27,9 +29,15 @@ public class App {
|
||||||
// themselfes out.
|
// themselfes out.
|
||||||
ArrayList<Stop> stops = new ArrayList<Stop>();
|
ArrayList<Stop> stops = new ArrayList<Stop>();
|
||||||
ArrayList<Bus> busses = new ArrayList<Bus>();
|
ArrayList<Bus> busses = new ArrayList<Bus>();
|
||||||
|
ArrayList<Trip> trips = new ArrayList<Trip>();
|
||||||
ArrayList<Passenger> passengers = new ArrayList<Passenger>();
|
ArrayList<Passenger> passengers = new ArrayList<Passenger>();
|
||||||
BlockingQueue<BusInformationMessage> blockingQueue = new LinkedBlockingQueue<BusInformationMessage>();
|
BlockingQueue<BusInformationMessage> blockingQueue = new LinkedBlockingQueue<BusInformationMessage>();
|
||||||
|
|
||||||
|
if (numStopPerBus > numStops) {
|
||||||
|
System.out.println("More stops per bus than stops.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// make stop" + name + " reached stop " + nextStop.getStopName() + "!"s
|
// make stop" + name + " reached stop " + nextStop.getStopName() + "!"s
|
||||||
for (int k = 0; k < numStops; k++) {
|
for (int k = 0; k < numStops; k++) {
|
||||||
Stop s = new Stop(k, numPassengersPerStop);
|
Stop s = new Stop(k, numPassengersPerStop);
|
||||||
|
@ -40,16 +48,10 @@ public class App {
|
||||||
threadsStop.submit(s);
|
threadsStop.submit(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
// make busses
|
// make trpis
|
||||||
for (int k = 0; k < numBusses; k++) {
|
for (int k = 0 ; k < numTrips ; k++) {
|
||||||
if (numStopPerBus > numStops) {
|
|
||||||
System.out.println("More stops per bus than stops.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayList<Stop> s_list = new ArrayList<Stop>();
|
|
||||||
Stop s;
|
Stop s;
|
||||||
// add stop. Make a bus doesnt stop multiple time at the same stop
|
ArrayList<Stop> s_list = new ArrayList<Stop>();
|
||||||
while (s_list.size() < numStopPerBus) {
|
while (s_list.size() < numStopPerBus) {
|
||||||
do {
|
do {
|
||||||
s = stops.get((int) (Math.random() * numStops));
|
s = stops.get((int) (Math.random() * numStops));
|
||||||
|
@ -57,7 +59,13 @@ public class App {
|
||||||
s_list.add(s);
|
s_list.add(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
Bus b = new Bus(s_list, k, timeBetweenStops, timeEmbark, numPassengersPerBus);
|
Trip t = new Trip(k, s_list);
|
||||||
|
trips.add(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
// make busses
|
||||||
|
for (int k = 0; k < numBusses; k++) {
|
||||||
|
Bus b = new Bus(trips.get(k % trips.size()), k, timeBetweenStops, timeEmbark, numPassengersPerBus);
|
||||||
b.setBlockingQueue(blockingQueue);
|
b.setBlockingQueue(blockingQueue);
|
||||||
|
|
||||||
busses.add(b);
|
busses.add(b);
|
||||||
|
@ -82,7 +90,6 @@ public class App {
|
||||||
dest = b.getStops().get(idx + 1 + (int) Math.round(Math.random() * (numStopPerBus - 2 - idx)));
|
dest = b.getStops().get(idx + 1 + (int) Math.round(Math.random() * (numStopPerBus - 2 - idx)));
|
||||||
|
|
||||||
Passenger p = new Passenger(idPassenger, start, dest);
|
Passenger p = new Passenger(idPassenger, start, dest);
|
||||||
p.setThreadPool(threadsPassenger);
|
|
||||||
passengers.add(p);
|
passengers.add(p);
|
||||||
start.addPassenger(p);
|
start.addPassenger(p);
|
||||||
idPassenger++;
|
idPassenger++;
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
package usherbrooke.ift630;
|
package usherbrooke.ift630;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
|
|
||||||
public class Bus extends Thread {
|
public class Bus extends Thread {
|
||||||
private ArrayList<Passenger> passengers;
|
private ArrayList<Passenger> passengers;
|
||||||
private ArrayList<Stop> stops;
|
private Trip trip;
|
||||||
private String name;
|
private String name;
|
||||||
private int id;
|
private int id;
|
||||||
private int maxCapacity;
|
private int maxCapacity;
|
||||||
|
@ -14,6 +14,7 @@ public class Bus extends Thread {
|
||||||
private int timeBetweenStops;
|
private int timeBetweenStops;
|
||||||
private int timeToNextStop;
|
private int timeToNextStop;
|
||||||
private int timeEmbark;
|
private int timeEmbark;
|
||||||
|
private int nextStopIndex = 0;
|
||||||
private Stop nextStop;
|
private Stop nextStop;
|
||||||
private BlockingQueue<BusInformationMessage> blockingQueue;
|
private BlockingQueue<BusInformationMessage> blockingQueue;
|
||||||
|
|
||||||
|
@ -47,7 +48,7 @@ public class Bus extends Thread {
|
||||||
// if no reason to stop, skip current stop
|
// if no reason to stop, skip current stop
|
||||||
boolean nextStopEmpty, overMaxCapacity;
|
boolean nextStopEmpty, overMaxCapacity;
|
||||||
do {
|
do {
|
||||||
nextStop = stops.remove(0);
|
nextStop = trip.get(++nextStopIndex);
|
||||||
|
|
||||||
sendNextStopInfo();
|
sendNextStopInfo();
|
||||||
|
|
||||||
|
@ -79,22 +80,14 @@ public class Bus extends Thread {
|
||||||
ArrayList<Passenger> res = new ArrayList<Passenger>();
|
ArrayList<Passenger> res = new ArrayList<Passenger>();
|
||||||
|
|
||||||
if (nextStop != null)
|
if (nextStop != null)
|
||||||
for (Passenger p : passengers) {
|
res = nextStop.getPassengerByDest(trip, nextStopIndex);
|
||||||
if (p.getDest() == nextStop) {
|
|
||||||
res.add(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// lock until passenger got in
|
// lock until passenger got in
|
||||||
private void waitEmbark() throws InterruptedException {
|
private synchronized void waitEmbark() throws InterruptedException {
|
||||||
synchronized (this) {
|
|
||||||
sleep(timeEmbark());
|
sleep(timeEmbark());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// lock until passenger got out
|
// lock until passenger got out
|
||||||
private void waitStop() throws InterruptedException {
|
private void waitStop() throws InterruptedException {
|
||||||
|
@ -127,14 +120,14 @@ public class Bus extends Thread {
|
||||||
Logger.getInstance().print(id, "[BUS] " + name + " exiting!");
|
Logger.getInstance().print(id, "[BUS] " + name + " exiting!");
|
||||||
}
|
}
|
||||||
|
|
||||||
Bus(ArrayList<Stop> s, int id, int timeStop, int timeEmbark, int maxCapacity) {
|
Bus(Trip trajet, int id, int timeStop, int timeEmbark, int maxCapacity) {
|
||||||
this.timeBetweenStops = timeStop * 1000 + 1;
|
this.timeBetweenStops = timeStop * 1000 + 1;
|
||||||
this.timeEmbark = timeEmbark * 1000 + 1;
|
this.timeEmbark = timeEmbark * 1000 + 1;
|
||||||
this.name = "Bus n°" + id;
|
this.name = "Bus n°" + id;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.passengers = new ArrayList<Passenger>();
|
this.passengers = new ArrayList<Passenger>();
|
||||||
this.stops = s;
|
this.trip = trajet;
|
||||||
this.nextStop = stops.get(0);
|
this.nextStop = trajet.get(0);
|
||||||
this.currentCapacity = 0;
|
this.currentCapacity = 0;
|
||||||
this.maxCapacity = maxCapacity;
|
this.maxCapacity = maxCapacity;
|
||||||
}
|
}
|
||||||
|
@ -144,10 +137,9 @@ public class Bus extends Thread {
|
||||||
// lock on next stop
|
// lock on next stop
|
||||||
synchronized (nextStop) {
|
synchronized (nextStop) {
|
||||||
for (Passenger p : passengers) {
|
for (Passenger p : passengers) {
|
||||||
waitEmbark();
|
|
||||||
|
|
||||||
if (p.getDest() == nextStop) {
|
if (p.getDest() == nextStop) {
|
||||||
synchronized (p) {
|
synchronized (p) {
|
||||||
|
waitEmbark();
|
||||||
p.notify();
|
p.notify();
|
||||||
}
|
}
|
||||||
// already synced on this, func is synced
|
// already synced on this, func is synced
|
||||||
|
@ -162,11 +154,8 @@ public class Bus extends Thread {
|
||||||
public synchronized void embarkPassengers() throws InterruptedException {
|
public synchronized void embarkPassengers() throws InterruptedException {
|
||||||
synchronized (nextStop) {
|
synchronized (nextStop) {
|
||||||
try {
|
try {
|
||||||
ArrayList<Passenger> list = nextStop.getPassengerByDest(stops);
|
ArrayList<Passenger> list = nextStop.getPassengerByDest(trip, nextStopIndex);
|
||||||
for (Passenger p : list) {
|
for (Passenger p : list) {
|
||||||
synchronized (p) {
|
|
||||||
p.notify();
|
|
||||||
}
|
|
||||||
waitEmbark();
|
waitEmbark();
|
||||||
if (currentCapacity >= maxCapacity)
|
if (currentCapacity >= maxCapacity)
|
||||||
continue;
|
continue;
|
||||||
|
@ -189,9 +178,9 @@ public class Bus extends Thread {
|
||||||
Logger.getInstance().print(id, "\t".repeat(indent) + "current stop: " + nextStop.getStopName());
|
Logger.getInstance().print(id, "\t".repeat(indent) + "current stop: " + nextStop.getStopName());
|
||||||
Logger.getInstance().print(id, "\t".repeat(indent) + "stops: ");
|
Logger.getInstance().print(id, "\t".repeat(indent) + "stops: ");
|
||||||
|
|
||||||
if (nextStop != stops.get(0))
|
if (nextStop != trip.get(0))
|
||||||
nextStop.printDetails(id, indent + 1);
|
nextStop.printDetails(id, indent + 1);
|
||||||
for (Stop s : stops) {
|
for (Stop s : trip.getStops()) {
|
||||||
s.printDetails(id, indent + 1);
|
s.printDetails(id, indent + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -201,24 +190,19 @@ public class Bus extends Thread {
|
||||||
printDetails(0);
|
printDetails(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// return stop list
|
|
||||||
public ArrayList<Stop> getStops() {
|
|
||||||
ArrayList<Stop> res = new ArrayList<Stop>(stops);
|
|
||||||
if (nextStop != stops.get(0))
|
|
||||||
res.add(0, nextStop);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
// return last stop
|
// return last stop
|
||||||
public Stop getTerminus() {
|
public Stop getTerminus() {
|
||||||
return stops.get(stops.size() - 1);
|
return trip.getTerminus();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getNameBus() {
|
public String getNameBus() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Stop> getStops() {
|
||||||
|
return trip.getStops();
|
||||||
|
}
|
||||||
|
|
||||||
public void setBlockingQueue(BlockingQueue<BusInformationMessage> q) {
|
public void setBlockingQueue(BlockingQueue<BusInformationMessage> q) {
|
||||||
blockingQueue = q;
|
blockingQueue = q;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,16 +10,14 @@ public class Passenger extends Thread {
|
||||||
private int id;
|
private int id;
|
||||||
private Stop dest;
|
private Stop dest;
|
||||||
private Stop start;
|
private Stop start;
|
||||||
private ExecutorService threads;
|
|
||||||
private ExecutorService ex;
|
private ExecutorService ex;
|
||||||
private Future<Void> future;
|
|
||||||
|
|
||||||
Passenger(int id, Stop start, Stop dest) {
|
Passenger(int id, Stop start, Stop dest) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = "Passenger " + id;
|
this.name = "Passenger " + id;
|
||||||
this.dest = dest;
|
this.dest = dest;
|
||||||
this.start = start;
|
this.start = start;
|
||||||
this.ex = Executors.newFixedThreadPool(2); // one for each future.
|
this.ex = Executors.newFixedThreadPool(1); // one for each future.
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
@ -37,7 +35,7 @@ public class Passenger extends Thread {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
wait();
|
wait();
|
||||||
}
|
}
|
||||||
Logger.getInstance().print(id, "\t[PASSENGER] I'm embarking at " + start.getStopName());
|
Logger.getInstance().print(id, "\t[PASSENGER] " + name + " embarking at " + start.getStopName());
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -48,7 +46,7 @@ public class Passenger extends Thread {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
wait();
|
wait();
|
||||||
}
|
}
|
||||||
Logger.getInstance().print(id, "\t[PASSENGER] I'm leaving at " + dest.getStopName());
|
Logger.getInstance().print(id, "\t[PASSENGER] " + name + " leaving at " + dest.getStopName());
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -84,8 +82,4 @@ public class Passenger extends Thread {
|
||||||
public void printDetails() {
|
public void printDetails() {
|
||||||
printDetails(0);
|
printDetails(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setThreadPool(ExecutorService threads) {
|
|
||||||
this.threads = threads;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package usherbrooke.ift630;
|
package usherbrooke.ift630;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -75,10 +74,10 @@ public class Stop extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
// return all passenger that stops at a stop in the list
|
// return all passenger that stops at a stop in the list
|
||||||
public ArrayList<Passenger> getPassengerByDest(ArrayList<Stop> list) {
|
public ArrayList<Passenger> getPassengerByDest(Trip t, int indexStart) {
|
||||||
ArrayList<Passenger> res = new ArrayList<Passenger>();
|
ArrayList<Passenger> res = new ArrayList<Passenger>();
|
||||||
// for all given stop
|
// for all given stop
|
||||||
for (Stop s : list) {
|
for (Stop s : t.getStops(indexStart)) {
|
||||||
// for all passenger at this stop
|
// for all passenger at this stop
|
||||||
for (Passenger p : passengers) {
|
for (Passenger p : passengers) {
|
||||||
// if they stop at this stop, add to res
|
// if they stop at this stop, add to res
|
||||||
|
@ -126,10 +125,9 @@ public class Stop extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void removePassenger(Passenger p) {
|
public synchronized void removePassenger(Passenger p) {
|
||||||
try {
|
if(passengers.remove(p))
|
||||||
passengers.remove(p);
|
synchronized (p) {
|
||||||
} catch (Exception e) {
|
p.notify();
|
||||||
System.out.println("exception" + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
47
src/main/java/usherbrooke/ift630/Trip.java
Normal file
47
src/main/java/usherbrooke/ift630/Trip.java
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
package usherbrooke.ift630;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
class Trip {
|
||||||
|
private List<Stop> stops;
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
Trip(int id, ArrayList<Stop> stops) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = "Trip " + this.id;
|
||||||
|
this.stops = stops;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Stop> getStops(int index) {
|
||||||
|
return stops.subList(index, stops.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Stop> getStops() {
|
||||||
|
return stops;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printDetails() {
|
||||||
|
printDetails(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printDetails(int indent) {
|
||||||
|
Logger.getInstance().print(id, "\t.".repeat(indent) + "[TRIP] " + name);
|
||||||
|
for (Stop s : stops) {
|
||||||
|
s.printDetails(id, indent + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Stop get(int idx) {
|
||||||
|
return stops.get(idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Stop getTerminus() {
|
||||||
|
return stops.get(stops.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue