1 /*
2 * Copyright (C) 2002-2003, Mark Diggory
3 *
4 * This file is part of the CAGN model project.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. License
19 * information is also available at http://www.gnu.org.
20 *
21 */
22 package org.mrd.repast.landscape;
23
24 import java.io.*;
25 import java.util.*;
26 import uchicago.src.sim.space.*;
27 import org.apache.commons.logging.*;
28 import org.apache.commons.pool.*;
29
30 /*** Landscape Encapsulates much of the Internal Datastructure management of a
31 * spatial simulation. It takes care of traking agents that are dead and need to be
32 * added or removed from the simulation landscape. It is composed of Three primary
33 * data structures:
34 * 1.) LandscapeAgent Grid - used to track nieghorhood relationships
35 * 2.) Resource Grid - represents available resources in an area
36 * 3.) LandscapeAgent List - A randomizable or sortable list that is iterated over to
37 * update the agents.
38 *
39 * There is one secondary datastructure, a birth queue, this
40 * store new agents that need to be added to the model at the end of
41 * a iteration.
42 *
43 * @author Mark Diggory <mdiggory@latte.harvard.edu>;
44 */
45
46 public class Landscape{
47
48 protected Log log;
49
50 /*** Grid that holds the Agents.
51 */
52 protected Object2DGrid agentGrid;
53
54 /*** Grid that holds the Habitat Resources
55 */
56 protected Object2DGrid habitatGrid;
57
58 /***
59 * A list of all the agents. Convenient for any method that
60 * iterates through a list of agents. A least one list like this is common
61 * to most simulations. See below for examples of its use.
62 **/
63 protected ArrayList agents = new ArrayList();
64
65 /*** This queue holds new agents that need to be added to the simulation at the end
66 * of the iteration.
67 */
68 protected Vector birthQueue = new Vector();
69
70 /*** Holds value of property pool. */
71 private ObjectPool pool;
72
73 /*** Creates a new Landscape
74 * @param model The model this landscape exists within.
75 * @param title Title that will be shown on the Landscape Display
76 * @param habitatFile The Habitat File that will initialize the resources in this landscape.
77 */
78 public Landscape(String habitatFile) {
79
80 log = LogFactory.getLog(this.getClass().getName());
81
82 /* Initialize the Grids */
83 try{
84 habitatFile = new File(habitatFile).getCanonicalPath();
85 log.debug("HabitatFile: "+habitatFile);
86 }catch(Exception e){
87 log.error("failed to locate habitatGrid file.");
88 }
89 habitatGrid = new Object2DTorus(habitatFile, Object2DGrid.PGM_ASCII);
90 agentGrid = new Object2DTorus(this.getSizeX(), this.getSizeY());
91 }
92
93 /*** This will update the landscape by adding the new agents to the Grid and the agent
94 * list.
95 */
96 public void birthAgents() {
97 agents.addAll(birthQueue);
98 birthQueue.clear();
99 }
100
101 /*** This will update the landscape by removing the dead agents from the Grid and the agent
102 * list.
103 */
104 public void reapAgents() {
105 try{
106 for (java.util.Iterator iter = agents.iterator(); iter.hasNext();) {
107 LandscapeAgent agent = (LandscapeAgent)iter.next();
108 if(agent.getStage() == LandscapeAgent.DEAD){
109 iter.remove();
110 agentGrid.putObjectAt(agent.getX(), agent.getY(),null);
111 if(pool != null){
112 pool.returnObject(agent);
113 }
114 }
115 }
116 }catch(Exception e){
117 log.error(e.getMessage(),e);
118 }
119 }
120
121 /*** This gets the Resource Object available at a (x, y) coordinate
122 * @param x coordinate
123 * @param y coordinate
124 * @return Object The resource object at the coordinate of the LandscapeAgent
125 */
126 public Object getResourceAt(int x, int y){
127 try{
128 return habitatGrid.getObjectAt(x, y);
129 }catch(java.lang.ArrayIndexOutOfBoundsException aobe){
130 System.err.println("Landscape.getResourceAt(): Bad coordinate( x=" +x+" y=" +y+")");
131 aobe.printStackTrace();
132 }
133 return null;
134 }
135
136 /*** This gets the Resource double value available at a (x, y) coordinate
137 * @param x coordinate
138 * @param y coordinate
139 * @return double The value of the resource object at that coordinate.
140 */
141 public double getResourceValueAt(int x, int y){
142
143 double value = Double.NaN;
144 try{
145 value = habitatGrid.getValueAt(x, y);
146 }catch(java.lang.ArrayIndexOutOfBoundsException aobe){
147 System.err.println("Landscape.getResourceValueAt(): Bad coordinate( x=" +x+" y=" +y+")");
148 aobe.printStackTrace();
149 }
150
151 return value;
152 }
153
154 /***
155 * @param x
156 * @param y
157 * @return */
158 public LandscapeAgent getAgentAt(int x, int y){
159 return (LandscapeAgent)agentGrid.getObjectAt(x,y);
160 }
161
162 /***
163 * @param x
164 * @param y
165 * @param agent */
166 public void moveAgentTo(LandscapeAgent agent, int x, int y) throws Exception{
167
168 /* make sure new location is empty */
169 if(agentGrid.getObjectAt(x,y) != null){
170 throw new Exception("Not Empty");
171 }
172
173 /* get agent in old location, make sure new location is empty */
174 LandscapeAgent exists = (LandscapeAgent)agentGrid.getObjectAt(agent.getX(),agent.getY());
175
176 if(exists != null){
177 agentGrid.putObjectAt(exists.getX(),exists.getY(),null);
178 agentGrid.putObjectAt(x,y,exists);
179 exists.setX(x);
180 exists.setY(y);
181 }
182
183 }
184
185 /***
186 * @param x
187 * @param y
188 * @param agent */
189 public void moveAgentTo(int old_x, int old_y, int x, int y) throws Exception{
190
191 /* make sure new location is empty */
192 if(agentGrid.getObjectAt(x,y) != null){
193 throw new Exception("Not Empty");
194 }
195
196 /* get agent in old location, make sure new location is empty */
197 LandscapeAgent exists = (LandscapeAgent)agentGrid.getObjectAt(old_x,old_y);
198
199 if(exists != null){
200 agentGrid.putObjectAt(exists.getX(),exists.getY(),null);
201 agentGrid.putObjectAt(x,y,exists);
202 exists.setX(x);
203 exists.setY(y);
204 }
205 }
206
207 /***
208 * @param x
209 * @param y
210 * @param agent */
211 public void putAgentAt(int x, int y, LandscapeAgent agent) throws Exception{
212
213 /* make sure new location is empty */
214 if(agentGrid.getObjectAt(x,y) != null){
215 throw new Exception("Not Empty");
216 }
217 /* set Agents characteristic determined by Landscape */
218 agent.setX(x);
219 agent.setY(y);
220 /* put the agent in the landscape */
221 birthQueue.add(agent);
222 agentGrid.putObjectAt(x,y,agent);
223
224 }
225
226 /*** Getter for property sizeX.
227 * @return Value of property sizeX.
228 */
229 public int getSizeX() {
230 return habitatGrid.getSizeX();
231 }
232
233 /*** Getter for property sizeY.
234 * @return Value of property sizeY.
235 */
236 public int getSizeY() {
237 return habitatGrid.getSizeY();
238 }
239
240 /*** Getter for property agents.
241 * @return Value of property agents.
242 */
243 public ArrayList getAgents() {
244 return this.agents;
245 }
246
247 /*** Getter for property habitatGrid.
248 * @return Value of property habitatGrid.
249 */
250 public Object2DGrid getHabitatGrid() {
251 return this.habitatGrid;
252 }
253
254 /*** Getter for property agentGrid.
255 * @return Value of property agentGrid.
256 */
257 public Object2DGrid getAgentGrid() {
258 return this.agentGrid;
259 }
260
261 /*** Getter for property pool.
262 * @return Value of property pool.
263 */
264 public ObjectPool getPool() {
265 return this.pool;
266 }
267
268 /*** Setter for property pool.
269 * @param pool New value of property pool.
270 */
271 public void setPool(ObjectPool pool) {
272 this.pool = pool;
273 }
274
275 }
This page was automatically generated by Maven