-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRReal.java
More file actions
536 lines (490 loc) · 13.9 KB
/
Copy pathRReal.java
File metadata and controls
536 lines (490 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package sysmlinjava.valuetypes;
import java.io.Serializable;
import sysmlinjava.annotations.Attribute;
import sysmlinjava.probability.SysMLProbabilityDistribution;
import sysmlinjava.units.SysMLinJavaUnits;
/**
* SysMLinJava value type for the SysML Real represented by a java
* {@code double} value
* <p>
* This representation of the real number type is for a generic real as used in
* SysML. Hence the use of a Java {@code double} for its value. This SysML real
* is not the same as the java float, double, Float or Double types as SysML
* does not discriminate between bit-lengths of real or floating- point numeric
* types as the Java language does.
* <p>
* Note that the {@code Real} name is used by SysML/UML to represent real
* numbers. To prevent any confusion between this SysML {@code Real} and its
* representation in SysMLinJava, the double R is used in the {@code RReal}
* name, consistent with the double letters used for SysMLinJava's
* {@code BBoolean}, {@code IInteger}, and {@code SString} value types.
* <p>
* <b>Note:</b> as an {@code ObservableValue} this value type can be "observed"
* by other objects. {@code ValueObserver}s call the {@code addValueObserver()}
* operation to be notiified (called-back) by the {@code RReal} object of any
* change in its value. This notification will only occur if and when the
* {@code RReal} {@code value} is changed by a call to the {@code setValue()}
* operation. So, while the {@code RReal.value} is publicly accessible and can
* be changed by direct assignment, the {@code setValue()} operation must be
* used if {@code ValueObserver}s are to be automatically notified of the
* change.
*
* @author ModelerOne
*
*/
public class RReal extends SysMLValueType implements Serializable
{
/** Serializable ID */
private static final long serialVersionUID = -1566489011777283872L;
/**
* Constant double that can be used to assign an initial zero value that also
* indicates the actual value is assigned dynamically, i.e. sometime after
* initial creation.
*/
@Attribute
public static final double dynamicallyAssignedValue = 0;
/**
* Current value of the RReal
*/
public double value;
/**
* Constructor - initial value. Units are assumed to be simple numeric units,
* i.e. simply a real value.
*
* @param value double value of the RReal
*/
public RReal(double value)
{
super();
this.value = value;
}
/**
* Constructor - copy
*
* @param copied RReal instance whose value is to be the initial value of this
* copy
*/
public RReal(RReal copied)
{
super(copied);
this.value = copied.value;
}
/**
* Constructor specifying the real's value and a probability distribution for
* the value being a random variable. Units are assumed to be simple numeric
* units, i.e. simply a real value.
*
* @param value double value of the RReal
* @param distribution probability distribution for the value as a random
* variable
*/
public RReal(double value, SysMLProbabilityDistribution distribution)
{
super(distribution);
this.value = value;
}
/**
* Sets the value to the specified double value and notifies all
* {@code ValueChangeObservers} of the change.
*
* @param value double value that this value is to be set to
*/
public void setValue(double value)
{
this.value = value;
notifyValueChangeObservers();
}
/**
* Sets the value to the specified RReal value and notifies all
* {@code ValueChangeObservers} of the change.
*
* @param value RReal whose value this value is to be set to
*/
public void setValue(RReal value)
{
setValue(value.value);
}
/**
* Returns this value
*
* @return this value
*/
public double getValue()
{
if (probabilityDistribution.isPresent())
value = probabilityDistribution.get().nextRandom();
return value;
}
/**
* Returns this value changed to its value added to specified real value
*
* @param value double value to add to this value
*/
public void add(double value)
{
this.value += value;
}
/**
* Returns this value changed to its value added to specified real value
*
* @param real RReal value to add to this value
*/
public void add(RReal real)
{
this.value += real.value;
}
/**
* Returns RReal for this value added to specified RReal value
*
* @param real RReal value to be added to this value
* @return RReal for this value added to specified RReal value
*/
public RReal added(RReal real)
{
return new RReal(this.value + real.value);
}
/**
* Returns RReal for specified RReal value subtracted from this value
*
* @param real RReal value to be subtracted from this value
* @return RReal for specified RReal value subtracted from this value
*/
public RReal subtracted(RReal real)
{
return new RReal(this.value - real.value);
}
/**
* Changes this value to its value subtracted by specified double value
*
* @param value double value to subtract from this value
*/
public void subtract(double value)
{
this.value -= value;
}
/**
* Changes this value to its value multiplied by specified double value
*
* @param value double value to multiply this value by
*/
public void multiply(double value)
{
this.value *= value;
}
/**
* Changes this value to its value multiplied by specified real value
*
* @param real RReal value to multiply this value by
*/
public void multiplyBy(RReal real)
{
this.value *= real.value;
}
/**
* Returns new instance that is this value multiplied by specified real value
*
* @param real RReal value to multiply this value by
* @return instance of this value multiplied by specified value
*/
public RReal multipliedBy(RReal real)
{
return new RReal(this.value * real.value);
}
/**
* Returns new instance that is this value multiplied by specified double value
*
* @param value double value to multiply this value by
* @return instance of this value multiplied by specified value
*/
public RReal multipliedBy(double value)
{
return new RReal(this.value * value);
}
/**
* Returns new instance that is this value divided by specified double value
*
* @param value double value to divide this value by
* @return instance of this value divided by specified value
*/
public RReal dividedBy(double value)
{
return new RReal(this.value / value);
}
/**
* Returns new instance that is this value divided by specified real value
*
* @param real RReal value to divide this value by
* @return instance of this value divided by specified value
*/
public RReal dividedBy(RReal real)
{
return new RReal(this.value / real.value);
}
/**
* This value changed to its value divided by specified real value
*
* @param real RReal value with which this value is to be divided by
*/
public void divide(RReal real)
{
this.value /= real.value;
}
/**
* This value changed to its value divided by specified real value
*
* @param value double value with which this value is to be divided by
*/
public void divide(double value)
{
this.value /= value;
}
/**
* Returns new instance that is this value negated
*
* @return instance of this value megated
*/
public RReal negated()
{
return new RReal(this.value *= -1.0);
}
/**
* This value changed to its absolute value
*/
public void abs()
{
value = Math.abs(value);
}
/**
* Returns new instance that is this value changed to its absolute value
*
* @return instance that is this value changed to its absolute value
*/
public RReal absed()
{
return new RReal(Math.abs(value));
}
/**
* This value changed to its negtive value
*/
public void negate()
{
value *= -1.0;
}
/**
* Sets this value to zero
*/
public void zero()
{
value = 0.0;
}
/**
* Returns whether this value is less than specified value
*
* @param real value to compare
* @return whether this value is less than specified value
*/
public boolean lessThan(RReal real)
{
return this.value < real.value;
}
/**
* Returns whether this value is less than specified value
*
* @param value double value to compare
* @return whether this value is less than specified value
*/
public boolean lessThan(double value)
{
return this.value < value;
}
/**
* Returns whether this value is greater than specified value
*
* @param real value to compare
* @return whether this value is greater than specified value
*/
public boolean greaterThan(RReal real)
{
return this.value > real.value;
}
/**
* Returns whether this value is greater than specified value
*
* @param value double value to compare
* @return whether this value is greater than specified value
*/
public boolean greaterThan(double value)
{
return this.value > value;
}
/**
* Returns whether this value is less than or equal than specified value
*
* @param real value to compare
* @return whether this value is less than or equal than specified value
*/
public boolean lessThanOrEqualTo(RReal real)
{
return this.value <= real.value;
}
/**
* Returns whether this value is greater than or equal than specified value
*
* @param other double value to compare
* @return whether this value is greater than or equal than specified value
*/
public boolean greaterThanOrEqualTo(double other)
{
return this.value >= other;
}
/**
* Returns whether this value is greater than or equal than specified value
*
* @param real RReal value to compare
* @return whether this value is greater than or equal than specified value
*/
public boolean greaterThanOrEqualTo(RReal real)
{
return this.value >= real.value;
}
/**
* Returns new instance that is the absolute value of this value
*
* @return instance that is the absolute value of this value
*/
public RReal absoluted()
{
return new RReal(Math.abs(value));
}
/**
* Returns new instance that is the modulus value of this value for the
* specified value
*
* @param value value of the divisor for the modulus
* @return instance that is the modulus value of this value for the specified
* value
*/
public RReal moduloOf(RReal value)
{
return new RReal(this.value % value.value);
}
/**
* Returns whether this value is zero
*
* @return true if this value is zero, false otherwise
*/
public boolean isZero()
{
return value == 0.0;
}
/**
* Returns new instance whose initial value is the specified value
*
* @param value value to be the initial value
* @return instance whose initial value is the specified value
*/
public static RReal of(double value)
{
return new RReal(value);
}
/**
* Returns the numeric length of hypotenuse for right triangle of specified
* opposite and adjacent sides
*
* @param opposite length of one of the right sides
* @param adjacent length of the other of the right sides
* @return length of hypotenuse
*/
public static RReal hypotenuseOf(RReal opposite, RReal adjacent)
{
return new RReal(Math.sqrt(Math.pow(opposite.value, 2) + Math.pow(adjacent.value, 2)));
}
/**
* Returns the numeric length of the opposite side for right triangle of
* specified hypotenuse and angle
*
* @param hypotenuse length of the hypotenuse
* @param angle radian angle opposite from side to be returned
* @return length of side opposite the angle
*/
public static RReal oppositeOf(RReal hypotenuse, RReal angle)
{
return new RReal(hypotenuse.value * Math.sin(angle.value));
}
/**
* Returns the numeric length of the adjacent side for right triangle of
* specified hypotenuse and angle
*
* @param hypotenuse length of the hypotenuse
* @param angle radian angle adjacent to side to be returned
* @return length of side adjacent to the angle
*/
public static RReal adjacentOf(RReal hypotenuse, RReal angle)
{
return new RReal(hypotenuse.value * Math.cos(angle.value));
}
/**
* Returns the radian angle for a right triangle with specified opposite and
* adjacent sides
*
* @param opposite length of side opposite the angle
* @param adjacent length of side adjacent to the angle
* @return radian angle
*/
public static RReal tanAngleOf(RReal opposite, RReal adjacent)
{
return new RReal(Math.atan(opposite.dividedBy(adjacent).value));
}
/**
* Returns the radian angle for a right triangle with specified opposite side
* and hypotenuse
*
* @param opposite length of side opposite the angle
* @param hypotenuse length of hypotenuse
* @return radian angle
*/
public static RReal sinAngleOf(RReal opposite, RReal hypotenuse)
{
return new RReal(Math.asin(opposite.dividedBy(hypotenuse).value));
}
/**
* Returns the radian angle for a right triangle with specified adjacent side
* and hypotenuse
*
* @param adjacent length of side adjacent to the angle
* @param hypotenuse length of hypotenuse
* @return radian angle
*/
public static RReal cosAngleOf(RReal adjacent, RReal hypotenuse)
{
return new RReal(Math.acos(adjacent.dividedBy(hypotenuse).value));
}
@Override
protected void createUnits()
{
units = SysMLinJavaUnits.Numeric;
}
@Override
public boolean equals(Object obj)
{
if (obj instanceof RReal)
return value == ((RReal)obj).value;
else
return false;
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("RReal [value=");
builder.append(value);
builder.append(", units=");
builder.append(units);
builder.append(", name=");
builder.append(name);
builder.append(", id=");
builder.append(id);
builder.append("]");
return builder.toString();
}
}