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