1 | package desmoj.core.statistic; |
2 | |
3 | import desmoj.core.simulator.Model; |
4 | |
5 | /** |
6 | * <code>StatisticObject</code> belongs to the <code>desmoj.statistic</code> |
7 | * package. The <code>StatisticObject</code> class is the super class of all |
8 | * other classes collecting statistical data. <br> |
9 | * It extends the <code>desmoj.core.simulator.Reportable</code> class so that it can provide |
10 | * a reporter to represent the statistical data in the report. <br> |
11 | * It also implements the <code>java.util.Observer</code> interface. So this |
12 | * is the observer part of the observer pattern as described in [Gamm95] that is |
13 | * observing the <code>desmoj.statistic.ValueSupplier</code>. Whenever the |
14 | * <code>ValueSupplier</code> is changing it calls the |
15 | * <code>update(Observable, Object)</code> method of this |
16 | * <code>StatisticObject</code>. Which happens inside the |
17 | * <code>notifyObservers()</code> method of the observable |
18 | * <code>ValueSupplier</code>. |
19 | * <p> |
20 | * |
21 | * The virtual method <code>update(Observable, Object)</code> has to be |
22 | * implemented in all derived classes so that the <code>StatisticObject</code> |
23 | * is updated when the observed <code>ValueSupplier</code> has changed and its |
24 | * <code>notifyObservers()</code> method is called from its |
25 | * <code>notifyStatistics()</code> method. |
26 | * <p> |
27 | * |
28 | * @see desmoj.core.statistic.ValueSupplier |
29 | * |
30 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
31 | * @author Soenke Claassen |
32 | * @author based on DESMO-C from Thomas Schniewind, 1998 |
33 | * |
34 | * Licensed under the Apache License, Version 2.0 (the "License"); |
35 | * you may not use this file except in compliance with the License. You |
36 | * may obtain a copy of the License at |
37 | * http://www.apache.org/licenses/LICENSE-2.0 |
38 | * |
39 | * Unless required by applicable law or agreed to in writing, software |
40 | * distributed under the License is distributed on an "AS IS" |
41 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
42 | * or implied. See the License for the specific language governing |
43 | * permissions and limitations under the License. |
44 | * |
45 | */ |
46 | |
47 | public abstract class StatisticObject extends desmoj.core.simulator.Reportable |
48 | implements java.util.Observer { |
49 | |
50 | // ****** attributes ****** |
51 | |
52 | /** |
53 | * Represents the value returned in case of an error. If no valid value can |
54 | * be returned. |
55 | */ |
56 | public static final double UNDEFINED = -1.0; |
57 | |
58 | /** |
59 | * The number of digits after the decimal point which will be displayed for |
60 | * the numbers in the reports. A precision of more than that is obsolete. |
61 | */ |
62 | protected static final double FRACTION_DIGITS = 5.0; |
63 | |
64 | /** |
65 | * The number needed for rounding the results to the desired precision. |
66 | */ |
67 | protected static final double PRECISION = java.lang.Math.pow(10.0, |
68 | FRACTION_DIGITS); |
69 | |
70 | // ****** methods ****** |
71 | |
72 | /** |
73 | * Constructor for a StatisticObject |
74 | * |
75 | * @param ownerModel |
76 | * Model : The model this StatisticObject is associated to. |
77 | * @param name |
78 | * java.lang.String : The name of this StatisticObject |
79 | * @param showInReport |
80 | * boolean : Flag for showing the report about this |
81 | * StatisticObject. |
82 | * @param showInTrace |
83 | * boolean : Flag for showing this StatisticObject in trace |
84 | * files. |
85 | */ |
86 | public StatisticObject(Model ownerModel, String name, boolean showInReport, |
87 | boolean showInTrace) { |
88 | super(ownerModel, name, showInReport, showInTrace); |
89 | } |
90 | |
91 | /** |
92 | * Converts an <code>Object</code> to a <code>double</code> -value. If |
93 | * the given object is not an instance of a number wrapper class a warning |
94 | * is produced and the UNDEFINED-value (-1.0) is returned. <br> |
95 | * So the <code>StatisticObject</code> can be notified by a |
96 | * <code>ValueSupplier</code> with the |
97 | * <code>notifyStatistics(Object arg)</code> method, where the value is |
98 | * passed as an object in this method. |
99 | * |
100 | * @return double : The double value the given object is converted to. |
101 | * @param obj |
102 | * Object : The Object which will be converted to a double value. |
103 | */ |
104 | protected double convertToDouble(Object obj) { |
105 | if (obj == null) // the given object is a null pointer |
106 | { |
107 | sendWarning("Attempt to convert a null pointer to a double value." |
108 | + " Zero (0.0) is returned!", "StatisticObject: " |
109 | + getName() + " Method: double convertToDouble" |
110 | + "(Object obj)", |
111 | "A null pointer can not be converted to a double value. Zero is " |
112 | + "assumed and will be returned.", |
113 | "Make sure not to pass a null pointer where an Object is expected."); |
114 | |
115 | return 0.0; // that makes sense (doesn't it?) |
116 | } |
117 | |
118 | if (obj instanceof Byte) { |
119 | return ((Byte) obj).doubleValue(); |
120 | } |
121 | |
122 | if (obj instanceof Short) { |
123 | return ((Short) obj).doubleValue(); |
124 | } |
125 | |
126 | if (obj instanceof Integer) { |
127 | return ((Integer) obj).doubleValue(); |
128 | } |
129 | |
130 | if (obj instanceof Long) { |
131 | return ((Long) obj).doubleValue(); |
132 | } |
133 | |
134 | if (obj instanceof Float) { |
135 | return ((Float) obj).doubleValue(); |
136 | } |
137 | |
138 | if (obj instanceof Double) { |
139 | return ((Double) obj).doubleValue(); |
140 | } |
141 | |
142 | // the given object is not an instance of a number wrapper class |
143 | sendWarning( |
144 | "Attempt to convert an object which is not a number wrapper" |
145 | + " class to a double value. The UNDEFINED value (-1.0) is returned!", |
146 | "StatisticObject: " + getName() |
147 | + " Method: double convertToDouble" + "(Object obj)", |
148 | "The given Object is an instance of the class: " |
149 | + obj.getClass().toString() |
150 | + ". This can not be converted to a double.", |
151 | "Make sure to use a number wrapper class for numeric values."); |
152 | |
153 | return UNDEFINED; // return (-1.0) in case of an error |
154 | } |
155 | |
156 | /** |
157 | * Leaves a message in the trace that this StatisticObject has been updated. |
158 | */ |
159 | protected void traceUpdate() { |
160 | if (currentlySendTraceNotes()) { |
161 | sendTraceNote("updates " + this.getQuotedName()); |
162 | } // tell in the trace which StatisticObject is updated |
163 | } |
164 | |
165 | /** |
166 | * Rounds a double value with repect to the <code>PRECISION</code>. |
167 | * |
168 | * @return double : The rounded value. |
169 | * @param d |
170 | * double : The value to be rounded |
171 | */ |
172 | public static double round(double d) { |
173 | return java.lang.Math.rint(PRECISION * d) / PRECISION; |
174 | } |
175 | } // end class StatisticObject |