package agent;

import utilities.EventLog;
import utilities.StringUtil;
import city.Place;
import city.interfaces.Person;

public abstract class Role
{
	// --------------------------------------- DATA ------------------------------------
	protected Person _person;
	public boolean active = false;
	public EventLog log = new EventLog(); // unit testing
	
	// ----------------------------- CONSTRUCTOR & PROPERTIES ------------------------------------
	public Role(Person person) { _person = person; }
	public void setPerson(Person person) { _person = person; }
	public abstract Place place();
	public String toString() { return StringUtil.shortName(getClass()); }
	
	// ------------------------------------- METHODS ------------------------------------
	protected void stateChanged()
	{
		if(_person != null) _person.stateChanged(); // this checking for _person != null is necessary in cases like when the cook sends a message to a market whose cashier role isn't yet filled 
		// note: it should be okay to perform extra scheduler calls in the person (i.e. if active == false) (since they all take place in one thread anyway)
	}
	
	// --------- ABSTRACT ---------
	public abstract boolean pickAndExecuteAnAction();
	public abstract void cmdFinishAndLeave(); // from PersonAgent
	
	// --------- UTILITIES ---------
	/**
	 * Print message with this agent's name and exception stack trace
	 */
	protected void print(String msg, Throwable e)
	{
		log.add(msg);
		StringBuffer sb = new StringBuffer();
		sb.append(_person.name());
		sb.append(": ");
		sb.append(msg);
		sb.append("\n");
		if (e != null) sb.append(StringUtil.stackTraceString(e));
		System.out.print(sb.toString());
	}
	protected void print(String msg)
	{
		print(msg, null);
	}
}

@@ -1049,16 +1049,33 @@
 on (i.e.
+ no need to check
  if acti
@@ -1084,16 +1084,14 @@
  == 
-fals
+tru
 e) 
-(
 sinc
@@ -1132,17 +1132,16 @@
 d anyway
-)
 %0A%09%7D%0A%09%0A%09/

Comment in Role class