<commit_before>package transportation;

import base.interfaces.Person;

import java.util.*;
import java.util.concurrent.Semaphore;

public class Bus {

	// For moving between stops (bus is always running)
	private Semaphore semAtStop = new Semaphore(0, true);

	// Reference to the GUI
	private BusGui mGui;

	// DATA

	private ArrayList<BusStop> mBusStops = new ArrayList<BusStop>();
	private ArrayList<Rider> mRiders = new ArrayList<Rider>();
	private int mBusCurrentStop;

	private class BusStop {
		List<Person> mWaitingPeople;
	}

	private class Rider {
		Person mPerson;
		int mDestination;
		
		public Rider(Person p, int dest) {
			mPerson = p;
			mDestination = dest;
		}
	}


	// MESSAGES

	// From GUI - bus arrived at stop
	public void msgGuiArrivedAtStop() {
		semAtStop.release();
	}

	// From Person who arrived at a bus stop
	public void msgNeedARide(Person p, int riderCurrentStop) {
		mBusStops.get(riderCurrentStop).mWaitingPeople.add(p);
	}

	// From Person who boarded the bus
	public void msgGoingTo(Person p, int riderDestination) {
		mRiders.add(new Rider(p, riderDestination));
		mBusStops.get(mBusCurrentStop).mWaitingPeople.remove(p);
	}

	// From Person who got off the bus
	public void msgImOff(Person p) {
		for (Rider r : mRiders) {
			if (r.mPerson.equals(p)) {
				mRiders.remove(r);
			}
		}

		int ridersStoppingHere = 0;
		for (Rider r : mRiders) {
			if (r.mDestination == mBusCurrentStop) {
				ridersStoppingHere++;
			}
		}
		
	}


	// SCHEDULER

	public void pickAndExecuteAnAction() {

		while (true) {
			// Instruct riders to get off if this is their stop
			if (mRiders.size() > 0) {
				// Pauses this thread until all riders (whose destination is here) are off
				TellRidersToGetOff();
			}
	
			// Instruct waiting customers at the current stop to board 
			if (mBusStops.get(mBusCurrentStop).mWaitingPeople.size() > 0) {
				TellRidersToBoard();
			}
			else {
				// Pauses this thread until GUI arrives at next stop
				AdvanceToNextStop();
			}
		}
	}


	// ACTIONS

	private void TellRidersToGetOff() {
		for (Rider r : mRiders) {
			if (r.mDestination == mBusCurrentStop) {
				// TODO r.mPerson.msgAtYourStop();
			}
		}
	}

	private void TellRidersToBoard() {
		for (Person p : mBusStops.get(mBusCurrentStop).mWaitingPeople) {
			// TODO p.msgBoardBus();
		}
	}

	private void AdvanceToNextStop() {
		// Gui has a list of bus stop coordinates
		mGui.DoAdvanceToNextStop();

		// Wait while GUI advances
		try { semAtStop.acquire(); } catch (Exception e) {}
		// semAtStop released when GUI calls msgGuiArrivedAtStop()

		mBusCurrentStop++;
	}


	// ACCESSORS/UTILITIES

	// TODO - setGui can (should?) be placed in base Agent
	public void setGui(BusGui bg) {
		mGui = bg;
	}
}<commit_msg>Semaphores for loading/unloading riders<commit_after>package transportation;

import base.interfaces.Person;

import java.util.*;
import java.util.concurrent.Semaphore;

public class Bus {

	// For moving between stops (bus is always running)
	private Semaphore semAtStop = new Semaphore(0, true);

	// For making sure all riders got off/boarded while at stop
	private Semaphore semRidersDone = new Semaphore(0, true);

	// Reference to the GUI
	private BusGui mGui;

	// DATA

	private ArrayList<BusStop> mBusStops = new ArrayList<BusStop>();
	private ArrayList<Rider> mRiders = new ArrayList<Rider>();
	private int mBusCurrentStop;

	private class BusStop {
		List<Person> mWaitingPeople;
	}

	private class Rider {
		Person mPerson;
		int mDestination;
		
		public Rider(Person p, int dest) {
			mPerson = p;
			mDestination = dest;
		}
	}


	// MESSAGES

	// From GUI - bus arrived at stop
	public void msgGuiArrivedAtStop() {
		semAtStop.release();
	}

	// From Person who arrived at a bus stop
	public void msgNeedARide(Person p, int riderCurrentStop) {
		mBusStops.get(riderCurrentStop).mWaitingPeople.add(p);
	}

	// From Person who boarded the bus
	public void msgGoingTo(Person p, int riderDestination) {
		mRiders.add(new Rider(p, riderDestination));
		mBusStops.get(mBusCurrentStop).mWaitingPeople.remove(p);
		
		if (mBusStops.get(mBusCurrentStop).mWaitingPeople.size() == 0) {
			semRidersDone.release();
		}
	}

	// From Person who got off the bus
	public void msgImOff(Person p) {
		for (Rider r : mRiders) {
			if (r.mPerson.equals(p)) {
				mRiders.remove(r);
			}
		}

		int ridersStoppingHere = 0;
		for (Rider r : mRiders) {
			if (r.mDestination == mBusCurrentStop) {
				ridersStoppingHere++;
			}
		}

		if (ridersStoppingHere == 0) {
			semRidersDone.release();
		}
	}


	// SCHEDULER

	public void pickAndExecuteAnAction() {

		while (true) {
			// Instruct riders to get off if this is their stop
			if (mRiders.size() > 0) {
				TellRidersToGetOff();
			}
	
			// Instruct waiting customers at the current stop to board 
			if (mBusStops.get(mBusCurrentStop).mWaitingPeople.size() > 0) {
				// Pauses this thread until all people waiting at this stop have boarded
				TellRidersToBoard();
			}
			else {
				// Pauses this thread until GUI arrives at next stop
				AdvanceToNextStop();
			}
		}
	}


	// ACTIONS

	// Pauses this thread until all riders (whose destination is here) are off
	private void TellRidersToGetOff() {
		int ridersStoppingHere = 0;

		for (Rider r : mRiders) {
			if (r.mDestination == mBusCurrentStop) {
				ridersStoppingHere++;
				// TODO r.mPerson.msgAtYourStop();
			}
		}

		if (ridersStoppingHere > 0) {
			try { semRidersDone.acquire(); } catch (Exception e) {}
		}
	}

	private void TellRidersToBoard() {
		for (Person p : mBusStops.get(mBusCurrentStop).mWaitingPeople) {
			// TODO p.msgBoardBus();
		}

		if (mBusStops.get(mBusCurrentStop).mWaitingPeople.size() > 0) {
			try { semRidersDone.acquire(); } catch (Exception e) {}
		}
	}

	private void AdvanceToNextStop() {
		// Gui has a list of bus stop coordinates
		mGui.DoAdvanceToNextStop();

		// Wait while GUI advances
		try { semAtStop.acquire(); } catch (Exception e) {}
		// semAtStop released when GUI calls msgGuiArrivedAtStop()

		mBusCurrentStop++;
	}


	// ACCESSORS/UTILITIES

	// TODO - setGui can (should?) be placed in base Agent
	public void setGui(BusGui bg) {
		mGui = bg;
	}
}
{'fraction_non_alphanumeric': 0.1087838934892028, 'fraction_numerical': 0.002273096281863939, 'mean_word_length': 3.3441466854724964, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2105599', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2088, 'n_words': 579}