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.simulation.assignments.exhaustive;
17  
18  import java.util.Map;
19  
20  import junit.framework.TestCase;
21  import p3j.misc.MatrixDimension;
22  import p3j.pppm.SubPopulation;
23  import p3j.pppm.parameters.Parameter;
24  import p3j.pppm.parameters.ParameterAssignment;
25  import p3j.pppm.parameters.ParameterInstance;
26  import p3j.pppm.parameters.ParameterType;
27  import p3j.pppm.parameters.Population;
28  import p3j.pppm.sets.Set;
29  import p3j.pppm.sets.SetType;
30  
31  /**
32   * Test for the {@link SetManager}.
33   * 
34   * @author Christina Bohk
35   * @author Roland Ewald
36   */
37  public class TestSetManager extends TestCase {
38  
39    /** The set manager. */
40    SetManager setManager;
41  
42    /**
43     * The probability of the first assignment. Has to be larger or equal than
44     * 0.5.
45     */
46    protected final static double probFirst = 0.8;
47  
48    /** The probability of the second assignment (have to sum up to 1). */
49    protected final static double probSecond = 1 - probFirst;
50  
51    /** The first parameter instance. */
52    public static final ParameterInstance instance1MigF = new ParameterInstance(
53        0, new Parameter(0, false,
54            ParameterType.MIGRATION.getFemaleLabelFor(new SubPopulation()),
55            MatrixDimension.AGES, MatrixDimension.YEARS, Population.CUSTOM));
56  
57    /** The second parameter instance. */
58    public static final ParameterInstance instance2MigM = new ParameterInstance(
59        1, new Parameter(1, false,
60            ParameterType.MIGRATION.getMaleLabelFor(new SubPopulation()),
61            MatrixDimension.AGES, MatrixDimension.YEARS, Population.CUSTOM));
62  
63    /** The first assignment for the first inst. */
64    final ParameterAssignment assignment1Inst1 = new ParameterAssignment(
65        instance1MigF, "#1-1", "", probFirst, 0.0, null);
66  
67    /** The second assignment for the first instance. */
68    final ParameterAssignment assignment2Inst1 = new ParameterAssignment(
69        instance1MigF, "#1-2", "", probSecond, 0.0, null);
70  
71    /** The first assignment1 for the second instance. */
72    final ParameterAssignment assignment1Inst2 = new ParameterAssignment(
73        instance2MigM, "#2-1", "", probFirst, 0.0, null);
74  
75    /** The second assignment for the second instance. */
76    final ParameterAssignment assignment2Inst2 = new ParameterAssignment(
77        instance2MigM, "#2-2", "", probSecond, 0.0, null);
78  
79    /** The test Settype. */
80    protected final SetType setType = new SetType("Test Settype",
81        "This is just a Settype of testing.");
82    {
83      setType.addInstance(instance1MigF);
84      setType.addInstance(instance2MigM);
85    }
86  
87    /** The test set. */
88    protected final Set set = setType.createSet("Test Set",
89        "This is just a set for testing.", 1.0);
90    {
91      set.addParameterAssignment(assignment1Inst1);
92      set.addParameterAssignment(assignment1Inst2);
93      set.addParameterAssignment(assignment2Inst1);
94      set.addParameterAssignment(assignment2Inst2);
95    }
96  
97    /*
98     * (non-Javadoc)
99     * 
100    * @see junit.framework.TestCase#setUp()
101    */
102   @Override
103   public void setUp() {
104     setManager = new SetManager(set, setType);
105   }
106 
107   /**
108    * Test set manager.
109    */
110   public void testSetManager() {
111     assertEquals(probFirst * probFirst, setManager.getCurrentAssignmentProb());
112     testMapping(setManager.getCurrentMapping(), assignment1Inst1,
113         assignment1Inst2);
114 
115     assertTrue(setManager.nextAssignment());
116     assertEquals(probFirst * probSecond, setManager.getCurrentAssignmentProb());
117 
118     assertTrue(setManager.nextAssignment());
119     assertEquals(probFirst * probSecond, setManager.getCurrentAssignmentProb());
120 
121     assertTrue(setManager.nextAssignment());
122     assertEquals(probSecond * probSecond, setManager.getCurrentAssignmentProb());
123     testMapping(setManager.getCurrentMapping(), assignment2Inst1,
124         assignment2Inst2);
125 
126     assertFalse(setManager.nextAssignment());
127   }
128 
129   /**
130    * Test mapping.
131    * 
132    * @param currentMapping
133    *          the current mapping
134    * @param assignmentFirstInst
135    *          the assignment for the first instance
136    * @param assignmentSecondInst
137    *          the assignment for the second instance
138    */
139   private void testMapping(
140       Map<ParameterInstance, ParameterAssignment> currentMapping,
141       ParameterAssignment assignmentFirstInst,
142       ParameterAssignment assignmentSecondInst) {
143     assertEquals(2, currentMapping.size());
144     assertEquals(assignmentFirstInst, currentMapping.get(instance1MigF));
145     assertEquals(assignmentSecondInst, currentMapping.get(instance2MigM));
146   }
147 
148 }