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.sets;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import p3j.pppm.parameters.ParameterInstance;
23  import p3j.pppm.parameters.ParameterInstanceComparator;
24  
25  /**
26   * Represents a Settype. Basically, a Settype defines a list of
27   * {@link ParameterInstance} objects. This list represents parameter instances
28   * that loosely depend on each other. Each element of this list is exclusive to
29   * one Settype (by default, this is the default Settype, see
30   * {@link p3j.pppm.ProjectionModel} ), i.e., the parameter instance lists of all
31   * Settypes are disjunct. Each {@link Set} of a {@link SetType} contains a list
32   * of {@link p3j.pppm.parameters.ParameterAssignment} objects for each instances
33   * and has an occurrence probability itself. Hence, the Monte-Carlo simulation
34   * firstly selects a {@link Set} for each {@link SetType} randomly, and then
35   * selects a {@link p3j.pppm.parameters.ParameterAssignment} from the options
36   * offered by the chosen {@link Set}, for each {@link ParameterInstance} that is
37   * defined in the {@link SetType}.
38   * 
39   * Basically, {@link Set} and {@link SetType} serve the purpose of avoiding
40   * implausible combinations of input matrices for the PPPM (e.g., high fertility
41   * of native population and immigrants, but low fertility of emigrants could be
42   * such a combination). Each Settype manages the list of {@link Set} instances
43   * that are associated with it. Hence, sets need to be added/removed from the
44   * Settype.
45   * 
46   * Created on August 7, 2006
47   * 
48   * @see Set
49   * @see p3j.pppm.ProjectionModel
50   * @see p3j.pppm.parameters.ParameterAssignment
51   * 
52   * @author Christina Bohk
53   * @author Roland Ewald
54   * 
55   */
56  public class SetType implements Serializable {
57  
58  	/** Serialization ID. */
59  	private static final long serialVersionUID = 3111687701869447727L;
60  
61  	/** ID of this Settype. */
62  	private int id;
63  
64  	/** List of defined parameters. */
65  	private List<ParameterInstance> definedParameters = new ArrayList<ParameterInstance>();
66  
67  	/** List of all sets defined for this type. */
68  	private List<Set> sets = new ArrayList<Set>();
69  
70  	/** Name of Settype. */
71  	private String name = "";
72  
73  	/** Description of Settype. */
74  	private String description = "";
75  
76  	/**
77  	 * Default constructor.
78  	 * 
79  	 * @param typeName
80  	 *          name of the Settype
81  	 * @param typeDescription
82  	 *          description for this Settype
83  	 */
84  	public SetType(String typeName, String typeDescription) {
85  		this.name = typeName;
86  		this.description = typeDescription;
87  	}
88  
89  	/**
90  	 * Constructor for bean compatibility.
91  	 */
92  	public SetType() {
93  	}
94  
95  	/**
96  	 * Creates a new set for a type.
97  	 * 
98  	 * @param setName
99  	 *          name of the set to be created
100 	 * @param setDescription
101 	 *          description of the set to be created
102 	 * @param probability
103 	 *          probability of the set to be created
104 	 * @return the newly created set
105 	 */
106 	public Set createSet(String setName, String setDescription, double probability) {
107 		Set set = new Set(getDefinedParameters(), setName, setDescription,
108 		    probability);
109 		sets.add(set);
110 		return set;
111 	}
112 
113 	/**
114 	 * Removes a set from this Settype.
115 	 * 
116 	 * @param set
117 	 *          the set to be removed
118 	 * @return true, if set could be removed; otherwise false
119 	 */
120 	public boolean removeSet(Set set) {
121 		return sets.remove(set);
122 	}
123 
124 	/**
125 	 * Removes a parameter instance from this Settype. Removes all
126 	 * {@link p3j.pppm.parameters.ParameterAssignment} objects from all
127 	 * {@link Set} objects associated with this Settype.
128 	 * 
129 	 * @param instance
130 	 *          the instance to be removed
131 	 */
132 	public void removeInstance(ParameterInstance instance) {
133 		definedParameters.remove(instance);
134 		for (Set set : sets) {
135 			set.removeParameterInstance(instance);
136 		}
137 	}
138 
139 	/**
140 	 * Adds a parameter instance for this Settype. Updates all associated
141 	 * {@link Set} objects as well.
142 	 * 
143 	 * @param instance
144 	 *          the instance to be added
145 	 */
146 	public void addInstance(ParameterInstance instance) {
147 		ParameterInstanceComparator.insert(definedParameters, instance);
148 		for (Set set : sets) {
149 			set.addParameterInstance(instance);
150 		}
151 	}
152 
153 	@Override
154 	public String toString() {
155 		return name;
156 	}
157 
158 	// Getter/Setter for bean compatibility
159 
160 	public void setDefinedParameters(List<ParameterInstance> definedParameters) {
161 		this.definedParameters = definedParameters;
162 	}
163 
164 	public void setSets(List<Set> sets) {
165 		this.sets = sets;
166 	}
167 
168 	public String getDescription() {
169 		return description;
170 	}
171 
172 	public void setDescription(String description) {
173 		this.description = description;
174 	}
175 
176 	public String getName() {
177 		return name;
178 	}
179 
180 	public void setName(String name) {
181 		this.name = name;
182 	}
183 
184 	public List<ParameterInstance> getDefinedParameters() {
185 		return definedParameters;
186 	}
187 
188 	public List<Set> getSets() {
189 		return sets;
190 	}
191 
192 	/**
193 	 * Gets the number of sets for this Settype.
194 	 * 
195 	 * @return the number of sets
196 	 */
197 	public int getNumOfSets() {
198 		return sets.size();
199 	}
200 
201 	public int getID() {
202 		return id;
203 	}
204 
205 	public void setID(int uniqueID) {
206 		id = uniqueID;
207 	}
208 
209 }