View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.pppm.parameters;
17  
18  import james.SimSystem;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import p3j.database.DatabaseFactory;
26  import p3j.database.IP3MDatabase;
27  import p3j.misc.MatrixDimension;
28  
29  /**
30   * Central parameters directory. Contains all {@link Parameter} definitions
31   * necessary for the PPPM.
32   * 
33   * Created on August 7, 2006
34   * 
35   * @author Christina Bohk
36   * @author Roland Ewald
37   */
38  public final class Parameters {
39  
40  	/** Counter for the IDs. */
41  	private static int idCounter = 1;
42  
43  	/**
44  	 * The survivors at age x. Since this string is later used to check whether a
45  	 * given parameter refers to mortality, it should be constant over all
46  	 * {@link Parameter} instances.
47  	 * 
48  	 * TODO: Add an enumeration to define the type of a {@link Parameter}.
49  	 */
50  	public static final String SURVIVORS_AGE_X = "Survivors at age x";
51  
52  	/** List with all parameters. */
53  	private List<Parameter> params = new ArrayList<Parameter>();
54  
55  	/** Mapping from id => parameter. */
56  	private final Map<Integer, Parameter> idToParameter = new HashMap<Integer, Parameter>();
57  
58  	/** Natives: Survival probability of open-end age class (male). */
59  	public static final Parameter NAT_SURV_PROB_O100_M = new Parameter(nextID(),
60  	    false, "Natives: Survival probability of open-end age class (male)",
61  	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.NATIVES);
62  
63  	/** Natives: Survival probability of open-end age class (female). */
64  	public static final Parameter NAT_SURV_PROB_O100_F = new Parameter(nextID(),
65  	    false, "Natives: Survival probability of open-end age class (female)",
66  	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.NATIVES);
67  
68  	/** Natives: Proportion of infant deaths dying in the first 6 months (male). */
69  	public static final Parameter NAT_DEATHPROB_INFANT_1STHALF_M = new Parameter(
70  	    nextID(),
71  	    false,
72  	    "Natives: Proportion of infant deaths dying in the first 6 months (male)",
73  	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.NATIVES);
74  
75  	/** Natives: Proportion of infant deaths dying in the first 6 months (female). */
76  	public static final Parameter NAT_DEATHPROB_INFANT_1STHALF_F = new Parameter(
77  	    nextID(),
78  	    false,
79  	    "Natives: Proportion of infant deaths dying in the first 6 months (female)",
80  	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.NATIVES);
81  
82  	/** Natives: Proportion of male live births. */
83  	public static final Parameter NAT_PROP_LIVEBIRTH_M = new Parameter(nextID(),
84  	    false, "Natives: Proportion of male live births", MatrixDimension.SINGLE,
85  	    MatrixDimension.YEARS, Population.NATIVES);
86  
87  	/** Natives: Mortality (male). */
88  	public static final Parameter NAT_MORT_X_M = new Parameter(nextID(), false,
89  	    "Natives: " + SURVIVORS_AGE_X + " (male)", MatrixDimension.AGES,
90  	    MatrixDimension.YEARS, Population.NATIVES);
91  
92  	/** Natives: Mortality (female). */
93  	public static final Parameter NAT_MORT_X_F = new Parameter(nextID(), false,
94  	    "Natives: " + SURVIVORS_AGE_X + " (female)", MatrixDimension.AGES,
95  	    MatrixDimension.YEARS, Population.NATIVES);
96  
97  	/** Natives: Fertility. */
98  	public static final Parameter NAT_FERT = new Parameter(nextID(), false,
99  	    "Natives: Fertility", MatrixDimension.AGES, MatrixDimension.YEARS,
100 	    Population.NATIVES);
101 
102 	/** Natives: Jump-off population (male). */
103 	public static final Parameter NAT_P_END_SY_M = new Parameter(nextID(), false,
104 	    "Natives: Jump-off population (male)", MatrixDimension.AGES,
105 	    MatrixDimension.SINGLE, Population.NATIVES);
106 
107 	/** Natives: Jump-off population (female). */
108 	public static final Parameter NAT_P_END_SY_F = new Parameter(nextID(), false,
109 	    "Natives: Jump-off population (female)", MatrixDimension.AGES,
110 	    MatrixDimension.SINGLE, Population.NATIVES);
111 
112 	// EMIGRANTS
113 
114 	/** Emigrants (male). */
115 	public static final Parameter EMIGRANTS_M = new Parameter(nextID(), false,
116 	    "Emigrants (male)", MatrixDimension.AGES, MatrixDimension.YEARS,
117 	    Population.EMIGRANTS);
118 
119 	/** Emigrants (female). */
120 	public static final Parameter EMIGRANTS_F = new Parameter(nextID(), false,
121 	    "Emigrants (female)", MatrixDimension.AGES, MatrixDimension.YEARS,
122 	    Population.EMIGRANTS);
123 
124 	/** Emigrants: Survival probability of open-end age class (male). */
125 	public static final Parameter EMIG_SURV_PROB_O100_M = new Parameter(nextID(),
126 	    true, "Emigrants: Survival probability of open-end age class (male)",
127 	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.EMIGRANTS);
128 
129 	/** Emigrants: Survival probability of open-end age class (female). */
130 	public static final Parameter EMIG_SURV_PROB_O100_F = new Parameter(nextID(),
131 	    true, "Emigrants: Survival probability of open-end age class (female)",
132 	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.EMIGRANTS);
133 
134 	/** Emigrants: Proportion of infant deaths dying in the first 6 months (male). */
135 	public static final Parameter EMIG_DEATHPROB_INFANT_1STHALF_M = new Parameter(
136 	    nextID(),
137 	    true,
138 	    "Emigrants: Proportion of infant deaths dying in the first 6 months (male)",
139 	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.EMIGRANTS);
140 
141 	/**
142 	 * Emigrants: Proportion of infant deaths dying in the first 6 months
143 	 * (female).
144 	 */
145 	public static final Parameter EMIG_DEATHPROB_INFANT_1STHALF_F = new Parameter(
146 	    nextID(),
147 	    true,
148 	    "Emigrants: Proportion of infant deaths dying in the first 6 months (female)",
149 	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.EMIGRANTS);
150 
151 	/** Emigrants: Proportion of male live births. */
152 	public static final Parameter EMIG_PROP_LIVEBIRTH_M = new Parameter(nextID(),
153 	    true, "Emigrants: Proportion of male live births",
154 	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.EMIGRANTS);
155 
156 	/** Emigrants: Mortality (male). */
157 	public static final Parameter EMIG_MORT_X_M = new Parameter(nextID(), true,
158 	    "Emigrants: " + SURVIVORS_AGE_X + " (male)", MatrixDimension.AGES,
159 	    MatrixDimension.YEARS, Population.EMIGRANTS);
160 
161 	/** Emigrants: Mortality (female). */
162 	public static final Parameter EMIG_MORT_X_F = new Parameter(nextID(), true,
163 	    "Emigrants: " + SURVIVORS_AGE_X + " (female)", MatrixDimension.AGES,
164 	    MatrixDimension.YEARS, Population.EMIGRANTS);
165 
166 	/** Emigrants: Fertility. */
167 	public static final Parameter EMIG_FERT = new Parameter(nextID(), true,
168 	    "Emigrants: Fertility", MatrixDimension.AGES, MatrixDimension.YEARS,
169 	    Population.EMIGRANTS);
170 
171 	// IMMIGRANTS
172 
173 	/** Immigrants (male). */
174 	public static final Parameter IMMIG_M = new Parameter(nextID(), false,
175 	    "Immigrants (male)", MatrixDimension.AGES, MatrixDimension.YEARS,
176 	    Population.IMMIGRANTS);
177 
178 	/** Immigrants (female). */
179 	public static final Parameter IMMIG_F = new Parameter(nextID(), false,
180 	    "Immigrants (female)", MatrixDimension.AGES, MatrixDimension.YEARS,
181 	    Population.IMMIGRANTS);
182 
183 	/** Immigrants: Survival probability of open-end age class (male). */
184 	public static final Parameter IMMIG_SURV_PROB_O100_M = new Parameter(
185 	    nextID(), true,
186 	    "Immigrants: Survival probability of open-end age class (male)",
187 	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.IMMIGRANTS);
188 
189 	/** Immigrants: Survival probability of open-end age class (female). */
190 	public static final Parameter IMMIG_SURV_PROB_O100_F = new Parameter(
191 	    nextID(), true,
192 	    "Immigrants: Survival probability of open-end age class (female)",
193 	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.IMMIGRANTS);
194 
195 	/**
196 	 * Immigrants: Proportion of infant deaths dying in the first 6 months (male).
197 	 */
198 	public static final Parameter IMMIG_DEATHPROB_INFANT_1STHALF_M = new Parameter(
199 	    nextID(),
200 	    true,
201 	    "Immigrants: Proportion of infant deaths dying in the first 6 months (male)",
202 	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.IMMIGRANTS);
203 
204 	/**
205 	 * Immigrants: Proportion of infant deaths dying in the first 6 months
206 	 * (female).
207 	 */
208 	public static final Parameter IMMIG_DEATHPROB_INFANT_1STHALF_F = new Parameter(
209 	    nextID(),
210 	    true,
211 	    "Immigrants: Proportion of infant deaths dying in the first 6 months (female)",
212 	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.IMMIGRANTS);
213 
214 	/** Immigrants: Proportion of male live births. */
215 	public static final Parameter IMMIG_PROP_LIVEBIRTH_M = new Parameter(
216 	    nextID(), true, "Immigrants: Proportion of male live births",
217 	    MatrixDimension.SINGLE, MatrixDimension.YEARS, Population.IMMIGRANTS);
218 
219 	/** Immigrants: Mortality (male). */
220 	public static final Parameter IMMIG_MORT_X_M = new Parameter(nextID(), true,
221 	    "Immigrants: " + SURVIVORS_AGE_X + " (male)", MatrixDimension.AGES,
222 	    MatrixDimension.YEARS, Population.IMMIGRANTS);
223 
224 	/** Immigrants: Mortality (female). */
225 	public static final Parameter IMMIG_MORT_X_F = new Parameter(nextID(), true,
226 	    "Immigrants: " + SURVIVORS_AGE_X + " (female)", MatrixDimension.AGES,
227 	    MatrixDimension.YEARS, Population.IMMIGRANTS);
228 
229 	/** Immigrants: Fertility. */
230 	public static final Parameter IMMIG_FERT = new Parameter(nextID(), true,
231 	    "Immigrants: Fertility", MatrixDimension.AGES, MatrixDimension.YEARS,
232 	    Population.IMMIGRANTS);
233 
234 	/** The singleton. */
235 	private static Parameters singleton;
236 
237 	/**
238 	 * Returns singleton.
239 	 * 
240 	 * @return the parameters singleton
241 	 */
242 	public static Parameters getInstance() {
243 		if (singleton == null) {
244 			singleton = new Parameters();
245 		}
246 		return singleton;
247 	}
248 
249 	/**
250 	 * Generates a unique id.
251 	 * 
252 	 * @return a unique id
253 	 */
254 	protected static int nextID() {
255 		return ++idCounter;
256 	}
257 
258 	/**
259 	 * This class shall not be instantiated.
260 	 */
261 	private Parameters() {
262 
263 		addNatives();
264 		addEmigrants();
265 		addImmigrants();
266 
267 		// Is there a database
268 		IP3MDatabase db = DatabaseFactory.getDatabaseSingleton();
269 
270 		List<Parameter> registeredParams = new ArrayList<Parameter>();
271 
272 		// Set IDs
273 		for (Parameter p : params) {
274 			if (db != null) {
275 				Parameter registeredParameter = null;
276 				try {
277 					registeredParameter = db.newParameter(p.getName(),
278 					    p.isGenerationDependent(), p.getValueHeight(), p.getValueWidth(),
279 					    p.getPopulation());
280 				} catch (Exception ex) {
281 					SimSystem.report(ex);
282 				}
283 				if (registeredParameter != null) {
284 					registeredParams.add(registeredParameter);
285 				}
286 			} else {
287 				registeredParams.add(p);
288 			}
289 		}
290 
291 		params = registeredParams;
292 
293 		for (Parameter p : params) {
294 			idToParameter.put(p.getID(), p);
295 		}
296 
297 	}
298 
299 	/**
300 	 * Adds emigrant fields to the parameter mapping.
301 	 */
302 	private void addImmigrants() {
303 		params.add(IMMIG_SURV_PROB_O100_M);
304 		params.add(IMMIG_SURV_PROB_O100_F);
305 		params.add(IMMIG_DEATHPROB_INFANT_1STHALF_M);
306 		params.add(IMMIG_DEATHPROB_INFANT_1STHALF_F);
307 		params.add(IMMIG_PROP_LIVEBIRTH_M);
308 		params.add(IMMIG_MORT_X_M);
309 		params.add(IMMIG_MORT_X_F);
310 		params.add(IMMIG_FERT);
311 		params.add(IMMIG_M);
312 		params.add(IMMIG_F);
313 	}
314 
315 	/**
316 	 * Adds the emigrant fields to parameter mapping.
317 	 */
318 	private void addEmigrants() {
319 		params.add(EMIG_SURV_PROB_O100_M);
320 		params.add(EMIG_SURV_PROB_O100_F);
321 		params.add(EMIG_DEATHPROB_INFANT_1STHALF_M);
322 		params.add(EMIG_DEATHPROB_INFANT_1STHALF_F);
323 		params.add(EMIG_PROP_LIVEBIRTH_M);
324 		params.add(EMIG_MORT_X_M);
325 		params.add(EMIG_MORT_X_F);
326 		params.add(EMIG_FERT);
327 		params.add(EMIGRANTS_M);
328 		params.add(EMIGRANTS_F);
329 	}
330 
331 	/**
332 	 * Adds the native fields to parameter mapping.
333 	 */
334 	private void addNatives() {
335 		params.add(NAT_SURV_PROB_O100_M);
336 		params.add(NAT_SURV_PROB_O100_F);
337 		params.add(NAT_DEATHPROB_INFANT_1STHALF_M);
338 		params.add(NAT_DEATHPROB_INFANT_1STHALF_F);
339 		params.add(NAT_PROP_LIVEBIRTH_M);
340 		params.add(NAT_MORT_X_M);
341 		params.add(NAT_MORT_X_F);
342 		params.add(NAT_FERT);
343 		params.add(NAT_P_END_SY_M);
344 		params.add(NAT_P_END_SY_F);
345 	}
346 
347 	/**
348 	 * Gets the params.
349 	 * 
350 	 * @return the params
351 	 */
352 	public List<Parameter> getParams() {
353 		return params;
354 	}
355 
356 	/**
357 	 * Sets the params.
358 	 * 
359 	 * @param params
360 	 *          the new params
361 	 */
362 	public void setParams(List<Parameter> params) {
363 		this.params = params;
364 	}
365 
366 }