1 /*
2 * Copyright (C) 2002-2003, Mark Diggory
3 *
4 * This file is part of the Repast Taglibrary Package for use with Jelly.
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 * * Copyright (c) 1999, Trustees of the University of Chicago
23 * All rights reserved.
24 *
25 * Redistribution and use in source and binary forms, with
26 * or without modification, are permitted provided that the following
27 * conditions are met:
28 *
29 * Redistributions of source code must retain the above copyright notice,
30 * this list of conditions and the following disclaimer.
31 *
32 * Redistributions in binary form must reproduce the above copyright notice,
33 * this list of conditions and the following disclaimer in the documentation
34 * and/or other materials provided with the distribution.
35 *
36 * Neither the name of the University of Chicago nor the names of its
37 * contributors may be used to endorse or promote products derived from
38 * this software without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
43 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
44 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
47 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
50 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 *
52 * Nick Collier
53 * nick@src.uchicago.edu
54 *
55 * packages cern.jet.random.*
56 * Copyright (c) 1999 CERN - European Laboratory for Particle
57 * Physics. Permission to use, copy, modify, distribute and sell this
58 * software and its documentation for any purpose is hereby granted without
59 * fee, provided that the above copyright notice appear in all copies
60 * and that both that copyright notice and this permission notice appear in
61 * supporting documentation. CERN makes no representations about the
62 * suitability of this software for any purpose. It is provided "as is"
63 * without expressed or implied warranty.
64 *
65 * Wolfgang Hoschek
66 * wolfgang.hoschek@cern.ch
67 *
68 * This file has been copied and modified from the Repast Sourcecode to add
69 * features support minimal threading control over the model.
70 *
71 * Provides minimal (thin) controller support for running a simulation.
72 * ThinController is intended to provide batch-like non-gui programmatic control over
73 * a simulation for those who want to do batch type runs without using
74 * RePast's batch run mechansim. Thin controller thus provides methods to
75 * start, stop, pause and exit a simulation, but virtually nothing else. In
76 * particular loading parameters into a model is not done here. Loading
77 * parameters using some sort of custom parameter file or mechanism will have
78 * to be done elsewhere.<p>
79 *
80 * ThinController can be used in conjuntion with a tool like Drone (drone.sf.net)
81 * where an instance of the model and a controller are created for each run
82 * of the model. The main method of a model might then look like:
83 * <pre><code>
84 * public static void main( String[] args ) {
85 * MyModel model = new MyModel();
86 * ThinController controller = new ThinController();
87 * model.setController(controller);
88 * controller.setExitOnExit(true);
89 * controller.setModel(model);
90 * model.addSimEventListener(controller);
91 * // custom parameter loading here ...
92 * ...
93 * control.startSimulation();
94 * }
95 * </code></pre>
96 *
97 */
98 package org.mrd.repast.control;
99
100 import uchicago.src.sim.engine.BaseController;
101 import uchicago.src.sim.engine.SimEvent;
102 import org.apache.commons.logging.*;
103
104 /***
105 * @author Administrator
106 *
107 * To change the template for this generated type comment go to
108 * Window>Preferences>Java>Code Generation>Code and Comments
109 */
110
111 public class ThinController extends BaseController {
112
113 protected Log log = LogFactory.getLog(this.getClass());
114
115 /*** Holds value of property batch. */
116 private boolean batch = false;
117
118 /*** Holds value of property gUI. */
119 private boolean gUI = false;
120
121 /*** Holds value of property runCount. */
122 private int runCount = 0;
123
124 public ThinController() {
125 super();
126 }
127
128 /***
129 * Creates a ThinController, specifying whether this is a graphical controller
130 * or not. The isGui parameter only determines what value is returned by the
131 * isGUI method, nothing more.
132 *
133 * @param isGui whether or not this ThinController will identify itself as
134 * a gui controller or not
135 */
136 public ThinController(boolean gUI) {
137 super();
138 this.gUI = gUI;
139 }
140
141 public void initSim() {
142 model.setup();
143 runThread = null;
144 runFinished = false;
145 }
146
147
148 public void startSim() {
149 super.startSim();
150 synchronized (monitor) {
151 while (!runFinished) {
152 try {
153 monitor.wait();
154 } catch (InterruptedException ex) {
155 }
156 }
157 }
158 runCount++;
159 stopSim();
160 }
161
162 /***
163 * Exits a simulation.
164 */
165 public void exitSim() {
166 stopSim();
167 if (runThread != null) {
168 runThread.interrupt();
169 try {
170 runThread.join();
171 } catch (InterruptedException ex) {
172 System.out.println("Interrupted");
173 }
174 }
175
176 fireSimEvent(new SimEvent(this, SimEvent.END_EVENT));
177
178 }
179
180 protected void onTickCountUpdate() {
181 }
182
183 /***
184 * Listens for SimEvents and responds accordingly.
185 * @param evt
186 */
187 public void simEventPerformed(SimEvent evt) {
188 if (evt.getId() == SimEvent.STOP_EVENT) {
189 stopSim();
190 } else if (evt.getId() == SimEvent.END_EVENT) {
191 exitSim();
192 } else if (evt.getId() == SimEvent.PAUSE_EVENT) {
193 pauseSim();
194 }
195 }
196
197 /*** Getter for property batch.
198 * @return Value of property batch.
199 */
200 public boolean isBatch() {
201 return this.batch;
202 }
203
204 /*** Setter for property batch.
205 * @param batch New value of property batch.
206 */
207 public void setBatch(boolean batch) {
208 this.batch = batch;
209 }
210
211 public long getRunCount() {
212 return runCount;
213 }
214
215 /*** Getter for property gUI.
216 * @return Value of property gUI.
217 */
218 public boolean isGUI() {
219 return this.gUI;
220 }
221
222 /*** Setter for property gUI.
223 * @param gUI New value of property gUI.
224 */
225 public void setGUI(boolean gUI) {
226 this.gUI = gUI;
227 }
228
229 }
This page was automatically generated by Maven