1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.pppm.parameters;
17
18 import james.SimSystem;
19 import james.core.serialization.IConstructorParameterProvider;
20 import james.core.serialization.SerialisationUtils;
21
22 import java.io.IOException;
23 import java.io.Serializable;
24
25 import p3j.misc.Misc;
26 import p3j.misc.math.Matrix;
27 import p3j.misc.math.Matrix2D;
28 import p3j.pppm.IStochasticOccurrence;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class ParameterAssignment implements Serializable, IStochasticOccurrence {
49 static {
50 SerialisationUtils.addDelegateForConstructor(ParameterAssignment.class,
51 new IConstructorParameterProvider<ParameterAssignment>() {
52 @Override
53 public Object[] getParameters(ParameterAssignment assignment) {
54 Object[] params = null;
55 try {
56 params = new Object[] { assignment.getMatrixBinary(),
57 assignment.getParamInstance(), assignment.getName(),
58 assignment.getDescription(), assignment.getProbability(),
59 assignment.getDeviation() };
60 } catch (IOException e) {
61 SimSystem.report(e);
62 }
63 return params;
64 }
65 });
66 }
67
68
69 private static final long serialVersionUID = 5672796759108189718L;
70
71
72 private int id;
73
74
75 private ParameterInstance paramInstance;
76
77
78 private Matrix matrix;
79
80
81 private String name = "";
82
83
84 private String description = "";
85
86
87 private double probability;
88
89
90
91
92
93
94 private double deviation;
95
96
97
98
99
100
101
102 public ParameterAssignment(ParameterInstance instance) {
103 paramInstance = instance;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 public ParameterAssignment(ParameterInstance instance, String assignName,
123 String desc, double prob, double dev, Matrix val) {
124 paramInstance = instance;
125 name = assignName;
126 description = desc;
127 probability = prob;
128 deviation = dev;
129 matrix = val;
130 }
131
132 public ParameterAssignment() {
133 }
134
135
136
137
138
139
140
141 public ParameterAssignment(String matrixString, ParameterInstance instance,
142 String assignName, String desc, double prob, double dev)
143 throws ClassNotFoundException, IOException {
144 this(instance, assignName, desc, prob, dev, (Matrix) SerialisationUtils
145 .deserializeFromB64String(matrixString));
146 }
147
148
149
150
151
152
153
154
155
156 public void setMatrixBinary(String matrixString)
157 throws ClassNotFoundException, IOException {
158 matrix = (Matrix) SerialisationUtils.deserializeFromB64String(matrixString);
159 }
160
161
162
163
164
165
166
167 public String getMatrixBinary() throws IOException {
168 return SerialisationUtils.serializeToB64String(matrix);
169 }
170
171
172
173
174
175
176
177 public ParameterAssignment getCopy() {
178
179 ParameterAssignment returnAssignment = new ParameterAssignment(
180 getParamInstance());
181 returnAssignment.description = description;
182 returnAssignment.name = name;
183 returnAssignment.probability = probability;
184 returnAssignment.matrix = matrix.copy();
185
186 return returnAssignment;
187 }
188
189 @Override
190 public String toString() {
191 return getName() + " (" + Misc.round(getProbability(), 2) + ")";
192 }
193
194
195
196 @Override
197 public double getProbability() {
198 return probability;
199 }
200
201 @Override
202 public void setProbability(double probability) {
203 this.probability = probability;
204 }
205
206 public String getDescription() {
207 return description;
208 }
209
210 public void setDescription(String description) {
211 this.description = description;
212 }
213
214 public String getName() {
215 return name;
216 }
217
218 public void setName(String name) {
219 this.name = name;
220 }
221
222 public Matrix getMatrix() {
223 return matrix;
224 }
225
226 public void setMatrix(Matrix matrix) {
227 this.matrix = matrix;
228 }
229
230
231
232
233
234
235
236 public void setMatrixValue(Matrix2D matrix2D) {
237
238
239 if (matrix2D.rows() == 0 && matrix2D.columns() == 0) {
240 return;
241 }
242
243 if (matrix == null) {
244 matrix = new Matrix(matrix2D);
245 } else {
246 matrix.setValue(matrix2D);
247 }
248 }
249
250
251
252
253
254
255 public Matrix2D getMatrixValue() {
256 return matrix.getValue();
257 }
258
259 public ParameterInstance getParamInstance() {
260 return paramInstance;
261 }
262
263 public void setParamInstance(ParameterInstance paramInstance) {
264 this.paramInstance = paramInstance;
265 }
266
267 public int getID() {
268 return id;
269 }
270
271 public void setID(int uniqueID) {
272 id = uniqueID;
273 }
274
275 public double getDeviation() {
276 return deviation;
277 }
278
279
280
281
282
283
284
285
286
287
288 public void setDeviation(Double newDeviation) {
289 if (newDeviation == null || newDeviation < 0) {
290 this.deviation = 0;
291 } else {
292 this.deviation = newDeviation;
293 }
294 }
295
296 }