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