<commit_before>package residence;

import Person.PersonAgent;
import Person.Role.Role;
import agent.Agent;
import residence.ApartmentManagerRole.AgentEvent;
import residence.gui.HomeRoleGui;
import residence.interfaces.*;
import trace.AlertLog;
import trace.AlertTag;

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

/**
 * Home Role
 */

public class HomeRole extends Role implements Home {
	private ApartmentManager landlord;
	private int rentOwed = 0;
	private int aptNumber = 0;
	private boolean leaveHome = false;
	private boolean enterHome = false;
	private List <Item> inventory = new ArrayList<Item>();
	private List <HomeFeature> features = new ArrayList<HomeFeature>(); //includes appliances, toilets, sinks, etc (anything that can break)
	private List <PersonAgent> partyAttendees = new ArrayList<PersonAgent>();
	private Semaphore atKitchen = new Semaphore(0, true);
	private Semaphore atBedroom = new Semaphore(0, true);
	private Semaphore atBed = new Semaphore(0, true);
	private Semaphore atFrontDoor = new Semaphore(0, true);
	private Semaphore atTable = new Semaphore(0, true);
	private Semaphore atCenter = new Semaphore(0, true);
	Timer timer = new Timer();
	
	private String name;
	
	public HomeRoleGui gui;

	public enum AgentState
	{DoingNothing, Cooking, Sleeping, Leaving};
	private AgentState state = AgentState.DoingNothing;//The start state

	public enum AgentEvent
	{none, cooking, eating, asleep, leaving};
	AgentEvent event = AgentEvent.none;

	public HomeRole(PersonAgent myPerson) {
		this.myPerson = myPerson;
		
		gui = new HomeRoleGui(this);
		myPerson.home.getPanel().addGui(gui);
		
		inventory.add(new Item("Cooking Ingredient",2));
		inventory.add(new Item("Cleaning supply", 2));
		
		features.add(new HomeFeature("Sink"));
	}
	
	public String getNameOfRole() {
		return "HomeRole";
	}
	
	public boolean canGoGetFood() {
		return true;
	}
	
	// Messages
	
	public void msgRentDue (int amount) {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "I just got charged rent.");
		rentOwed = amount;
		stateChanged();
	}
	public void msgTired() { //called by timer
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "I'm tired.");
		state = AgentState.Sleeping;
		stateChanged();
	}
	public void msgRestockItem (String itemName, int quantity) {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "Just received " + quantity + " " + itemName + "s.");
		event = AgentEvent.none;
		for(Item i : inventory) {
			if(i.name == itemName) {
				i.quantity = i.quantity + quantity;
			}
		}
		stateChanged();
	}
	public void msgFixedFeature (String name) {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), name + " fixed.");
		for (HomeFeature hf : features) {
			if (name == hf.name) {
				hf.working = true;
			}
		}
		stateChanged();
	}
	public void msgMakeFood() {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "I'm eating at home.");
		state = AgentState.Cooking;
		stateChanged();
	}
	public void msgLeaveBuilding() {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "Leaving home.");
		event = AgentEvent.leaving;
		leaveHome = true;
		stateChanged();
	}
	public void msgEnterBuilding() {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "Going home.");
		event = AgentEvent.none;
		enterHome = true;
		stateChanged();
	}
	
	public void msgAtKitchen() {
		atKitchen.release();
	}
	public void msgAtBedroom() {
		atBedroom.release();
	}
	public void msgAtBed() {
		atBed.release();
	}
	public void msgAtFrontDoor() {
		deactivate();
		event = AgentEvent.none;
		atFrontDoor.release();
	}
	public void msgAtTable() {
		atTable.release();
	}
	public void msgAtCenter() {
		atCenter.release();
	}
	
	/**
	 * Scheduler.  Determine what action is called for, and do it.
	 */
	public boolean pickAndExecuteAction() {
		if (leaveHome == true) {
			leaveHome();
			return true;
		}
		if (enterHome == true) {
			enterHome();
			return true;
		}
		if (rentOwed > 0) {
			payRent();
			return true;
		}
		if (state == AgentState.Cooking && event == AgentEvent.none) {
			cook();
			return true;
		}
		/*if (Person.stateOfNourishment == hungry) {
			eat()
			return true;
		}*/
		if (state == AgentState.Sleeping && event == AgentEvent.none) {
			goToSleep();
			return true;
		}
		for(Item i : inventory) {
			if(i.quantity < 2 && state == AgentState.DoingNothing && event == AgentEvent.none) {
				goToMarket(i);
			}
			return true;
		}
		for (HomeFeature hf : features) {
			if(!hf.working) {
				fileWorkOrder(hf);
				return true;
			}
		}
		return false;
	}
	

	// Actions

	private void cook() {
		event = AgentEvent.cooking;
		gui.DoGoToCenter();
		try {
			atCenter.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		gui.DoGoToKitchen();
		try {
			atKitchen.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		timer.schedule(new TimerTask() {
			public void run() {
				goEat();
				state = AgentState.DoingNothing;
				event = AgentEvent.eating;
			}
		},
		5000);
		for(Item i : inventory) {
			if(i.name == "Cooking Ingredient") {
				i.quantity--;
			}
		}
	}
	private void goEat() {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "Dinner's ready! Eating.");
		gui.DoGoToTable();
		try {
			atTable.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		timer.schedule(new TimerTask() {
			public void run() {
				AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "Done eating.");
				state = AgentState.DoingNothing;
				event = AgentEvent.none;
			}
		},
		5000);
	}
	private void goToMarket (Item item) {
		event = AgentEvent.leaving;
		gui.DoGoToCenter();
		try {
			atCenter.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "Low on " + item.name + ". I'm going to the market.");
		gui.DoGoToFrontDoor();
		try {
			atFrontDoor.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		myPerson.msgGoToMarket(item.name);
	}
	private void fileWorkOrder (HomeFeature brokenFeature) {
		landlord.msgBrokenFeature(brokenFeature.name, this);
	}<commit_msg>Another attempted bug fix for home role not leaving properly for market.<commit_after>package residence;

import Person.PersonAgent;
import Person.Role.Role;
import agent.Agent;
import residence.ApartmentManagerRole.AgentEvent;
import residence.gui.HomeRoleGui;
import residence.interfaces.*;
import trace.AlertLog;
import trace.AlertTag;

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

/**
 * Home Role
 */

public class HomeRole extends Role implements Home {
	private ApartmentManager landlord;
	private int rentOwed = 0;
	private int aptNumber = 0;
	private boolean leaveHome = false;
	private boolean enterHome = false;
	private List <Item> inventory = new ArrayList<Item>();
	private List <HomeFeature> features = new ArrayList<HomeFeature>(); //includes appliances, toilets, sinks, etc (anything that can break)
	private List <PersonAgent> partyAttendees = new ArrayList<PersonAgent>();
	private Semaphore atKitchen = new Semaphore(0, true);
	private Semaphore atBedroom = new Semaphore(0, true);
	private Semaphore atBed = new Semaphore(0, true);
	private Semaphore atFrontDoor = new Semaphore(0, true);
	private Semaphore atTable = new Semaphore(0, true);
	private Semaphore atCenter = new Semaphore(0, true);
	Timer timer = new Timer();
	
	private String name;
	
	public HomeRoleGui gui;

	public enum AgentState
	{DoingNothing, Cooking, Sleeping, Leaving};
	private AgentState state = AgentState.DoingNothing;//The start state

	public enum AgentEvent
	{none, cooking, eating, asleep, leaving};
	AgentEvent event = AgentEvent.none;

	public HomeRole(PersonAgent myPerson) {
		this.myPerson = myPerson;
		
		gui = new HomeRoleGui(this);
		myPerson.home.getPanel().addGui(gui);
		
		inventory.add(new Item("Cooking Ingredient",2));
		inventory.add(new Item("Cleaning supply", 2));
		
		features.add(new HomeFeature("Sink"));
	}
	
	public String getNameOfRole() {
		return "HomeRole";
	}
	
	public boolean canGoGetFood() {
		return true;
	}
	
	// Messages
	
	public void msgRentDue (int amount) {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "I just got charged rent.");
		rentOwed = amount;
		stateChanged();
	}
	public void msgTired() { //called by timer
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "I'm tired.");
		state = AgentState.Sleeping;
		stateChanged();
	}
	public void msgRestockItem (String itemName, int quantity) {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "Just received " + quantity + " " + itemName + "s.");
		event = AgentEvent.none;
		for(Item i : inventory) {
			if(i.name == itemName) {
				i.quantity = i.quantity + quantity;
			}
		}
		stateChanged();
	}
	public void msgFixedFeature (String name) {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), name + " fixed.");
		for (HomeFeature hf : features) {
			if (name == hf.name) {
				hf.working = true;
			}
		}
		stateChanged();
	}
	public void msgMakeFood() {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "I'm eating at home.");
		state = AgentState.Cooking;
		stateChanged();
	}
	public void msgLeaveBuilding() {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "Leaving home.");
		event = AgentEvent.leaving;
		leaveHome = true;
		stateChanged();
	}
	public void msgEnterBuilding() {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "Going home.");
		event = AgentEvent.none;
		enterHome = true;
		stateChanged();
	}
	
	public void msgAtKitchen() {
		atKitchen.release();
	}
	public void msgAtBedroom() {
		atBedroom.release();
	}
	public void msgAtBed() {
		atBed.release();
	}
	public void msgAtFrontDoor() {
		deactivate();
		event = AgentEvent.none;
		atFrontDoor.release();
	}
	public void msgAtTable() {
		atTable.release();
	}
	public void msgAtCenter() {
		atCenter.release();
	}
	
	/**
	 * Scheduler.  Determine what action is called for, and do it.
	 */
	public boolean pickAndExecuteAction() {
		if (leaveHome == true) {
			leaveHome();
			return true;
		}
		if (enterHome == true) {
			enterHome();
			return true;
		}
		if (rentOwed > 0) {
			payRent();
			return true;
		}
		if (state == AgentState.Cooking && event == AgentEvent.none) {
			cook();
			return true;
		}
		/*if (Person.stateOfNourishment == hungry) {
			eat()
			return true;
		}*/
		if (state == AgentState.Sleeping && event == AgentEvent.none) {
			goToSleep();
			return true;
		}
		for(Item i : inventory) {
			if(i.quantity < 2 && state == AgentState.DoingNothing && event == AgentEvent.none) {
				goToMarket(i);
			}
			return true;
		}
		for (HomeFeature hf : features) {
			if(!hf.working) {
				fileWorkOrder(hf);
				return true;
			}
		}
		return false;
	}
	

	// Actions

	private void cook() {
		event = AgentEvent.cooking;
		gui.DoGoToCenter();
		try {
			atCenter.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		gui.DoGoToKitchen();
		try {
			atKitchen.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		timer.schedule(new TimerTask() {
			public void run() {
				goEat();
				state = AgentState.DoingNothing;
				event = AgentEvent.eating;
			}
		},
		5000);
		for(Item i : inventory) {
			if(i.name == "Cooking Ingredient") {
				i.quantity--;
			}
		}
	}
	private void goEat() {
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "Dinner's ready! Eating.");
		gui.DoGoToTable();
		try {
			atTable.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		timer.schedule(new TimerTask() {
			public void run() {
				AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "Done eating.");
				state = AgentState.DoingNothing;
				event = AgentEvent.none;
			}
		},
		5000);
	}
	private void goToMarket (Item item) {
		event = AgentEvent.leaving;
		state = AgentState.Leaving;
		gui.DoGoToCenter();
		try {
			atCenter.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		AlertLog.getInstance().logMessage(AlertTag.HOME_ROLE, myPerson.getName(), "Low on " + item.name + ". I'm going to the market.");
		gui.DoGoToFrontDoor();
		try {
			atFrontDoor.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		myPerson.msgGoToMarket(item.name);
	}
	private void fileWorkOrder (HomeFeature brokenFeature) {
		landlord.msgBrokenFeature(brokenFeature.name, this);
	}
{'fraction_non_alphanumeric': 0.13152892876149927, 'fraction_numerical': 0.003041131300843914, 'mean_word_length': 3.880890538033395, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '7362607', 'n_tokens_mistral': 4836, 'n_tokens_neox': 4250, 'n_words': 1037}