1- package  aima .core .agent .impl ;
2- 
3- import  java .util .ArrayList ;
4- import  java .util .LinkedHashMap ;
5- import  java .util .LinkedHashSet ;
6- import  java .util .List ;
7- import  java .util .Map ;
8- import  java .util .Set ;
9- 
10- import  aima .core .agent .Action ;
11- import  aima .core .agent .Agent ;
12- import  aima .core .agent .Environment ;
13- import  aima .core .agent .EnvironmentObject ;
14- import  aima .core .agent .EnvironmentView ;
15- import  aima .core .agent .EnvironmentViewNotifier ;
16- import  aima .core .agent .Percept ;
17- 
18- /** 
19-  * @author Ravi Mohan 
20-  * @author Ciaran O'Reilly 
21-  */ 
22- public  abstract  class  AbstractEnvironment  implements  Environment ,
23- 		EnvironmentViewNotifier  {
24- 
25- 	// Note: Use LinkedHashSet's in order to ensure order is respected as 
26- 	// provide 
27- 	// access to these elements via List interface. 
28- 	protected  Set <EnvironmentObject > envObjects  = new  LinkedHashSet <EnvironmentObject >();
29- 
30- 	protected  Set <Agent > agents  = new  LinkedHashSet <Agent >();
31- 
32- 	protected  Set <EnvironmentView > views  = new  LinkedHashSet <EnvironmentView >();
33- 
34- 	protected  Map <Agent , Double > performanceMeasures  = new  LinkedHashMap <Agent , Double >();
35- 
36- 	// 
37- 	// PRUBLIC METHODS 
38- 	// 
39- 	
40- 	// 
41- 	// Methods to be implemented by subclasses. 
42- 
43- 	public  abstract  void  executeAction (Agent  agent , Action  action );
44- 
45- 	public  abstract  Percept  getPerceptSeenBy (Agent  anAgent );
46- 
47- 	/** 
48- 	 * Method for implementing dynamic environments in which not all changes are 
49- 	 * directly caused by agent action execution. The default implementation 
50- 	 * does nothing. 
51- 	 */ 
52- 	public  void  createExogenousChange () {
53- 	}
54- 
55- 	// 
56- 	// START-Environment 
57- 	public  List <Agent > getAgents () {
58- 		// Return as a List but also ensures the caller cannot modify 
59- 		return  new  ArrayList <Agent >(agents );
60- 	}
61- 
62- 	public  void  addAgent (Agent  a ) {
63- 		addEnvironmentObject (a );
64- 	}
65- 
66- 	public  void  removeAgent (Agent  a ) {
67- 		removeEnvironmentObject (a );
68- 	}
69- 
70- 	public  List <EnvironmentObject > getEnvironmentObjects () {
71- 		// Return as a List but also ensures the caller cannot modify 
72- 		return  new  ArrayList <EnvironmentObject >(envObjects );
73- 	}
74- 
75- 	public  void  addEnvironmentObject (EnvironmentObject  eo ) {
76- 		envObjects .add (eo );
77- 		if  (eo  instanceof  Agent ) {
78- 			Agent  a  = (Agent ) eo ;
79- 			if  (!agents .contains (a )) {
80- 				agents .add (a );
81- 				this .updateEnvironmentViewsAgentAdded (a );
82- 			}
83- 		}
84- 	}
85- 
86- 	public  void  removeEnvironmentObject (EnvironmentObject  eo ) {
87- 		envObjects .remove (eo );
88- 		agents .remove (eo );
89- 	}
90- 
91- 	/** 
92- 	 * Central template method for controlling agent simulation. The concrete 
93- 	 * behavior is determined by the primitive operations 
94- 	 * {@link #getPerceptSeenBy(Agent)}, {@link #executeAction(Agent, Action)}, 
95- 	 * and {@link #createExogenousChange()}. 
96- 	 */ 
97- 	public  void  step () {
98- 		for  (Agent  agent  : agents ) {
99- 			if  (agent .isAlive ()) {
100- 				Action  anAction  = agent .execute (getPerceptSeenBy (agent ));
101- 				executeAction (agent , anAction );
102- 				updateEnvironmentViewsAgentActed (agent , anAction );
103- 			}
104- 		}
105- 		createExogenousChange ();
106- 	}
107- 
108- 	public  void  step (int  n ) {
109- 		for  (int  i  = 0 ; i  < n ; i ++) {
110- 			step ();
111- 		}
112- 	}
113- 
114- 	public  void  stepUntilDone () {
115- 		while  (!isDone ()) {
116- 			step ();
117- 		}
118- 	}
119- 
120- 	public  boolean  isDone () {
121- 		for  (Agent  agent  : agents ) {
122- 			if  (agent .isAlive ()) {
123- 				return  false ;
124- 			}
125- 		}
126- 		return  true ;
127- 	}
128- 
129- 	public  double  getPerformanceMeasure (Agent  forAgent ) {
130- 		Double  pm  = performanceMeasures .get (forAgent );
131- 		if  (null  == pm ) {
132- 			pm  = new  Double (0 );
133- 			performanceMeasures .put (forAgent , pm );
134- 		}
135- 
136- 		return  pm ;
137- 	}
138- 
139- 	public  void  addEnvironmentView (EnvironmentView  ev ) {
140- 		views .add (ev );
141- 	}
142- 
143- 	public  void  removeEnvironmentView (EnvironmentView  ev ) {
144- 		views .remove (ev );
145- 	}
146- 
147- 	public  void  notifyViews (String  msg ) {
148- 		for  (EnvironmentView  ev  : views ) {
149- 			ev .notify (msg );
150- 		}
151- 	}
152- 
153- 	// END-Environment 
154- 	// 
155- 
156- 	// 
157- 	// PROTECTED METHODS 
158- 	// 
159- 
160- 	protected  void  updatePerformanceMeasure (Agent  forAgent , double  addTo ) {
161- 		performanceMeasures .put (forAgent , getPerformanceMeasure (forAgent )
162- 				+ addTo );
163- 	}
164- 
165- 	protected  void  updateEnvironmentViewsAgentAdded (Agent  agent ) {
166- 		for  (EnvironmentView  view  : views ) {
167- 			view .agentAdded (agent , this );
168- 		}
169- 	}
170- 
171- 	protected  void  updateEnvironmentViewsAgentActed (Agent  agent , Action  action ) {
172- 		for  (EnvironmentView  view  : views ) {
173- 			view .agentActed (agent , action , this );
174- 		}
175- 	}
1+ package  aima .core .agent .impl ;
2+ 
3+ import  java .util .ArrayList ;
4+ import  java .util .LinkedHashMap ;
5+ import  java .util .LinkedHashSet ;
6+ import  java .util .List ;
7+ import  java .util .Map ;
8+ import  java .util .Set ;
9+ 
10+ import  aima .core .agent .Action ;
11+ import  aima .core .agent .Agent ;
12+ import  aima .core .agent .Environment ;
13+ import  aima .core .agent .EnvironmentObject ;
14+ import  aima .core .agent .EnvironmentView ;
15+ import  aima .core .agent .EnvironmentViewNotifier ;
16+ import  aima .core .agent .Percept ;
17+ 
18+ /** 
19+  * @author Ravi Mohan 
20+  * @author Ciaran O'Reilly 
21+  */ 
22+ public  abstract  class  AbstractEnvironment  implements  Environment ,
23+ 		EnvironmentViewNotifier  {
24+ 
25+ 	// Note: Use LinkedHashSet's in order to ensure order is respected as 
26+ 	// provide 
27+ 	// access to these elements via List interface. 
28+ 	protected  Set <EnvironmentObject > envObjects  = new  LinkedHashSet <EnvironmentObject >();
29+ 
30+ 	protected  Set <Agent > agents  = new  LinkedHashSet <Agent >();
31+ 
32+ 	protected  Set <EnvironmentView > views  = new  LinkedHashSet <EnvironmentView >();
33+ 
34+ 	protected  Map <Agent , Double > performanceMeasures  = new  LinkedHashMap <Agent , Double >();
35+ 
36+ 	// 
37+ 	// PRUBLIC METHODS 
38+ 	// 
39+ 	
40+ 	// 
41+ 	// Methods to be implemented by subclasses. 
42+ 
43+ 	public  abstract  void  executeAction (Agent  agent , Action  action );
44+ 
45+ 	public  abstract  Percept  getPerceptSeenBy (Agent  anAgent );
46+ 
47+ 	/** 
48+ 	 * Method for implementing dynamic environments in which not all changes are 
49+ 	 * directly caused by agent action execution. The default implementation 
50+ 	 * does nothing. 
51+ 	 */ 
52+ 	public  void  createExogenousChange () {
53+ 	}
54+ 
55+ 	// 
56+ 	// START-Environment 
57+ 	public  List <Agent > getAgents () {
58+ 		// Return as a List but also ensures the caller cannot modify 
59+ 		return  new  ArrayList <Agent >(agents );
60+ 	}
61+ 
62+ 	public  void  addAgent (Agent  a ) {
63+ 		addEnvironmentObject (a );
64+ 	}
65+ 
66+ 	public  void  removeAgent (Agent  a ) {
67+ 		removeEnvironmentObject (a );
68+ 	}
69+ 
70+ 	public  List <EnvironmentObject > getEnvironmentObjects () {
71+ 		// Return as a List but also ensures the caller cannot modify 
72+ 		return  new  ArrayList <EnvironmentObject >(envObjects );
73+ 	}
74+ 
75+ 	public  void  addEnvironmentObject (EnvironmentObject  eo ) {
76+ 		envObjects .add (eo );
77+ 		if  (eo  instanceof  Agent ) {
78+ 			Agent  a  = (Agent ) eo ;
79+ 			if  (!agents .contains (a )) {
80+ 				agents .add (a );
81+ 				this .notifyEnvironmentViews (a );
82+ 			}
83+ 		}
84+ 	}
85+ 
86+ 	public  void  removeEnvironmentObject (EnvironmentObject  eo ) {
87+ 		envObjects .remove (eo );
88+ 		agents .remove (eo );
89+ 	}
90+ 
91+ 	/** 
92+ 	 * Central template method for controlling agent simulation. The concrete 
93+ 	 * behavior is determined by the primitive operations 
94+ 	 * {@link #getPerceptSeenBy(Agent)}, {@link #executeAction(Agent, Action)}, 
95+ 	 * and {@link #createExogenousChange()}. 
96+ 	 */ 
97+ 	public  void  step () {
98+ 		for  (Agent  agent  : agents ) {
99+ 			if  (agent .isAlive ()) {
100+ 				Action  anAction  = agent .execute (getPerceptSeenBy (agent ));
101+ 				executeAction (agent , anAction );
102+ 				notifyEnvironmentViews (agent , anAction );
103+ 			}
104+ 		}
105+ 		createExogenousChange ();
106+ 	}
107+ 
108+ 	public  void  step (int  n ) {
109+ 		for  (int  i  = 0 ; i  < n ; i ++) {
110+ 			step ();
111+ 		}
112+ 	}
113+ 
114+ 	public  void  stepUntilDone () {
115+ 		while  (!isDone ()) {
116+ 			step ();
117+ 		}
118+ 	}
119+ 
120+ 	public  boolean  isDone () {
121+ 		for  (Agent  agent  : agents ) {
122+ 			if  (agent .isAlive ()) {
123+ 				return  false ;
124+ 			}
125+ 		}
126+ 		return  true ;
127+ 	}
128+ 
129+ 	public  double  getPerformanceMeasure (Agent  forAgent ) {
130+ 		Double  pm  = performanceMeasures .get (forAgent );
131+ 		if  (null  == pm ) {
132+ 			pm  = new  Double (0 );
133+ 			performanceMeasures .put (forAgent , pm );
134+ 		}
135+ 
136+ 		return  pm ;
137+ 	}
138+ 
139+ 	public  void  addEnvironmentView (EnvironmentView  ev ) {
140+ 		views .add (ev );
141+ 	}
142+ 
143+ 	public  void  removeEnvironmentView (EnvironmentView  ev ) {
144+ 		views .remove (ev );
145+ 	}
146+ 
147+ 	public  void  notifyViews (String  msg ) {
148+ 		for  (EnvironmentView  ev  : views ) {
149+ 			ev .notify (msg );
150+ 		}
151+ 	}
152+ 
153+ 	// END-Environment 
154+ 	// 
155+ 
156+ 	// 
157+ 	// PROTECTED METHODS 
158+ 	// 
159+ 
160+ 	protected  void  updatePerformanceMeasure (Agent  forAgent , double  addTo ) {
161+ 		performanceMeasures .put (forAgent , getPerformanceMeasure (forAgent )
162+ 				+ addTo );
163+ 	}
164+ 
165+ 	protected  void  notifyEnvironmentViews (Agent  agent ) {
166+ 		for  (EnvironmentView  view  : views ) {
167+ 			view .agentAdded (agent , this );
168+ 		}
169+ 	}
170+ 
171+ 	protected  void  notifyEnvironmentViews (Agent  agent , Action  action ) {
172+ 		for  (EnvironmentView  view  : views ) {
173+ 			view .agentActed (agent , action , this );
174+ 		}
175+ 	}
176176}
0 commit comments