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 java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Set;
23  
24  import p3j.misc.errors.GeneratorError;
25  import p3j.misc.math.RandomNumberChecks;
26  
27  /**
28   * Stores a set of parameter assignments. This is a somewhat artificial class
29   * that is solely required for a clean object-relational mapping.
30   * 
31   * Created: August 18, 2008
32   * 
33   * @author Christina Bohk
34   * @author Roland Ewald
35   * 
36   */
37  public class ParameterAssignmentSet implements Serializable {
38  
39  	/** Serialization ID. */
40  	private static final long serialVersionUID = -1138437071774695491L;
41  
42  	/** The ID of the set. */
43  	private int id;
44  
45  	/** The set of assignments. */
46  	private Set<ParameterAssignment> assignments = new HashSet<ParameterAssignment>();
47  
48  	/**
49  	 * Sums up the probabilities of all assignments.
50  	 * 
51  	 * @return sum of assignment probabilities
52  	 */
53  	public double getProbabilitySum() {
54  		double sum = 0;
55  		for (ParameterAssignment assignment : assignments) {
56  			sum += assignment.getProbability();
57  		}
58  		return sum;
59  	}
60  
61  	/**
62  	 * Adjusts probability using the {@link RandomNumberChecks} singleton.
63  	 * 
64  	 * @return error log
65  	 */
66  	public List<GeneratorError> adjustProbability() {
67  		List<GeneratorError> errorLog = new ArrayList<GeneratorError>();
68  		if (assignments.size() == 0) {
69  			return errorLog;
70  		}
71  		RandomNumberChecks.getInstance().checkProbabilitySetting(
72  		    assignments.iterator().next().getParamInstance().toString(),
73  		    assignments, errorLog);
74  		return errorLog;
75  	}
76  
77  	public Set<ParameterAssignment> getAssignments() {
78  		return assignments;
79  	}
80  
81  	public void setAssignments(Set<ParameterAssignment> assignments) {
82  		this.assignments = assignments;
83  	}
84  
85  	public int getID() {
86  		return id;
87  	}
88  
89  	public void setID(int uniqueID) {
90  		id = uniqueID;
91  	}
92  
93  	/**
94  	 * Adds a parameter assignment to the set.
95  	 * 
96  	 * @param paramAssignment
97  	 *          the parameter assignment to be added
98  	 */
99  	public void add(ParameterAssignment paramAssignment) {
100 		assignments.add(paramAssignment);
101 	}
102 
103 	/**
104 	 * Removes the parameter assignment from the set.
105 	 * 
106 	 * @param paramAssignment
107 	 *          the parameter assignment to be removed
108 	 */
109 	public void remove(ParameterAssignment paramAssignment) {
110 		assignments.remove(paramAssignment);
111 	}
112 
113 	/**
114 	 * Get size of the set.
115 	 * 
116 	 * @return the size of the set
117 	 */
118 	public int size() {
119 		return assignments.size();
120 	}
121 
122 }