1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.pppm;
17
18 import james.core.model.Model;
19
20 import java.util.ArrayList;
21 import java.util.Calendar;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import p3j.pppm.parameters.ParameterAssignment;
27 import p3j.pppm.parameters.ParameterAssignmentSet;
28 import p3j.pppm.parameters.ParameterInstance;
29 import p3j.pppm.sets.Set;
30 import p3j.pppm.sets.SetType;
31 import p3j.simulation.calculation.deterministic.Constants;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class ProjectionModel extends Model implements IProjectionModel {
49
50
51 private static final long serialVersionUID = -1984182268566219449L;
52
53
54 private int id;
55
56
57 private String name;
58
59
60 private String description;
61
62
63
64
65
66 private SetType defaultType = new SetType("Default Settype",
67 "This is the default Settype.");
68
69
70 private Set defaultSet = defaultType.createSet("Default Set",
71 "This is the default set.", 1);
72
73
74 private List<SetType> userDefinedTypes = new ArrayList<SetType>();
75
76
77 private List<ParameterInstance> allParameterInstances = new ArrayList<ParameterInstance>();
78
79
80 private Map<ParameterInstance, SetType> instanceSetTypes = new HashMap<ParameterInstance, SetType>();
81
82
83
84
85
86 private int generations = Constants.DEFAULT_NUM_GENERATIONS;
87
88
89
90
91
92 private int years = Constants.DEFAULT_NUM_YEARS;
93
94
95
96
97
98 private int maximumAge = Constants.DEFAULT_MAXIMUM_AGE;
99
100
101 private int jumpOffYear = Calendar.getInstance().get(Calendar.YEAR);
102
103
104
105
106 public ProjectionModel() {
107
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public ProjectionModel(String scenName, String desc, int numOfGenerations,
127 int predYears, int maxAge, int jumpOffYear) {
128 this.name = scenName;
129 this.description = desc;
130 this.generations = numOfGenerations;
131 this.years = predYears;
132 this.maximumAge = maxAge;
133 this.jumpOffYear = jumpOffYear;
134 }
135
136
137
138
139
140 @Override
141 public void init() {
142 for (int i = 0; i < allParameterInstances.size(); i++) {
143 instanceSetTypes.put(allParameterInstances.get(i), defaultType);
144 defaultType.addInstance(allParameterInstances.get(i));
145 }
146 }
147
148
149
150
151
152
153
154
155
156
157
158 public SetType createSetType(String stName, String stDesc) {
159 SetType setType = new SetType(stName, stDesc);
160 userDefinedTypes.add(setType);
161 return setType;
162 }
163
164
165
166
167
168
169
170
171
172 public List<ParameterInstance> getUnassignedParameterInstances() {
173 return this.defaultType.getDefinedParameters();
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191 public boolean assignParameterInstance(ParameterInstance instance,
192 SetType type, boolean migrate) {
193
194 if (type.equals(defaultType)) {
195 removeParameterInstanceAssignment(instance);
196 }
197
198 SetType associatedSetType = instanceSetTypes.get(instance);
199
200 if (!associatedSetType.equals(defaultType)) {
201 return false;
202 }
203
204 type.addInstance(instance);
205 if (migrate) {
206 migrateAssignmentsFromDefaultType(instance, type);
207 }
208 defaultType.removeInstance(instance);
209 instanceSetTypes.put(instance, type);
210 return true;
211 }
212
213
214
215
216
217
218
219
220
221 private void migrateAssignmentsFromDefaultType(ParameterInstance instance,
222 SetType newType) {
223 Set set = newType.getNumOfSets() == 0 ? newType.createSet(
224 "Migrated parameter assignments",
225 "Automatically created during Settype definition.", 0.0) : newType
226 .getSets().get(0);
227 ParameterAssignmentSet paramAssignmentSet = getDefaultSet()
228 .getParameterAssignments(instance);
229 for (ParameterAssignment pa : paramAssignmentSet.getAssignments()) {
230 set.addParameterAssignment(pa);
231 }
232 }
233
234
235
236
237
238
239
240
241
242
243
244 public boolean removeParameterInstanceAssignment(ParameterInstance instance) {
245 SetType associatedSetType = instanceSetTypes.get(instance);
246
247 if (associatedSetType.equals(defaultType)) {
248 return false;
249 }
250
251 associatedSetType.removeInstance(instance);
252 defaultType.addInstance(instance);
253 instanceSetTypes.put(instance, defaultType);
254 return true;
255 }
256
257
258
259
260
261
262
263
264
265 public SetType getSetType(int index) {
266 return userDefinedTypes.get(index);
267 }
268
269
270
271
272
273
274
275
276
277 public boolean removeSetType(int index) {
278 SetType removedSetType = userDefinedTypes.remove(index);
279
280 if (removedSetType == null) {
281 return false;
282 }
283
284 List<ParameterInstance> parameterInstances = new ArrayList<ParameterInstance>(
285 removedSetType.getDefinedParameters());
286 for (ParameterInstance instance : parameterInstances) {
287 removeParameterInstanceAssignment(instance);
288 }
289 return true;
290 }
291
292 @Override
293 public List<SetType> getAllSetTypes() {
294 List<SetType> setTypes = new ArrayList<SetType>();
295 setTypes.add(defaultType);
296 setTypes.addAll(userDefinedTypes);
297 return setTypes;
298 }
299
300
301
302
303
304
305
306
307 public Set getDefaultSet() {
308 return defaultSet;
309 }
310
311
312
313
314
315
316
317 public void setDefaultSet(Set defaultSet) {
318 this.defaultSet = defaultSet;
319 }
320
321
322
323
324
325
326 public SetType getDefaultType() {
327 return defaultType;
328 }
329
330
331
332
333
334
335
336 public void setDefaultType(SetType defaultType) {
337 this.defaultType = defaultType;
338 }
339
340
341
342
343
344
345 public SetType getDefaultSetType() {
346 return defaultType;
347 }
348
349 @Override
350 public int getGenerations() {
351 return generations;
352 }
353
354
355
356
357
358
359
360 public void setGenerations(int generations) {
361
362 this.generations = generations;
363 }
364
365 @Override
366 public int getMaximumAge() {
367 return maximumAge;
368 }
369
370
371
372
373
374
375
376 public void setMaximumAge(int maximumAge) {
377
378 this.maximumAge = maximumAge;
379 }
380
381 @Override
382 public int getYears() {
383 return years;
384 }
385
386
387
388
389
390
391
392 public void setYears(int years) {
393
394 this.years = years;
395 }
396
397
398
399
400
401
402 public List<SetType> getUserDefinedTypes() {
403 return userDefinedTypes;
404 }
405
406
407
408
409
410
411
412 public void setUserDefinedTypes(List<SetType> userDefinedTypes) {
413 this.userDefinedTypes = userDefinedTypes;
414 }
415
416
417
418
419
420
421 public List<ParameterInstance> getAllParameterInstances() {
422 return this.allParameterInstances;
423 }
424
425
426
427
428
429
430
431 public void setAllParameterInstances(
432 List<ParameterInstance> allParameterInstances) {
433 this.allParameterInstances = allParameterInstances;
434 }
435
436 @Override
437 public Map<ParameterInstance, SetType> getInstanceSetTypes() {
438 return instanceSetTypes;
439 }
440
441
442
443
444
445
446
447 public void setInstanceSetTypes(
448 Map<ParameterInstance, SetType> instanceSetTypes) {
449 this.instanceSetTypes = instanceSetTypes;
450 }
451
452
453
454
455
456
457
458
459
460 public boolean removeSetType(SetType setType) {
461 return removeSetType(userDefinedTypes.indexOf(setType));
462 }
463
464
465
466
467
468
469 public int getNumOfSetTypes() {
470 return userDefinedTypes.size();
471 }
472
473
474
475
476
477
478 public int getID() {
479 return id;
480 }
481
482
483
484
485
486
487
488 public void setID(int uniqueID) {
489 id = uniqueID;
490 }
491
492 @Override
493 public String getName() {
494 return name;
495 }
496
497 @Override
498 public void setName(String name) {
499 this.name = name;
500 }
501
502
503
504
505
506
507 public String getDescription() {
508 return description;
509 }
510
511
512
513
514
515
516
517 public void setDescription(String description) {
518 this.description = description;
519 }
520
521
522
523
524
525
526
527 public int getNumberOfAgeClasses() {
528 return maximumAge + 1;
529 }
530
531
532
533
534
535
536 public int getJumpOffYear() {
537 return jumpOffYear;
538 }
539
540
541
542
543
544
545
546 public void setJumpOffYear(int jumpOffYear) {
547 this.jumpOffYear = jumpOffYear;
548 }
549
550
551
552
553
554
555 public int countNumberOfParameterAssignments() {
556 int numOfAssignments = 0;
557 for (SetType setType : getAllSetTypes())
558 for (Set set : setType.getSets())
559 for (ParameterAssignmentSet assignSet : set.getSetData().values())
560 numOfAssignments += assignSet.size();
561 return numOfAssignments;
562 }
563 }