SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
StatProbe.java
1/*
2 * Class: StatProbe
3 * Description: statistical probe
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.stat;
26
27import java.util.List;
28import java.util.ArrayList;
29import umontreal.ssj.util.PrintfFormat;
30
60public abstract class StatProbe {
61
62 private List<ObservationListener> listeners = new ArrayList<ObservationListener>();
63 protected String name;
64 protected double maxValue;
65 protected double minValue;
66 protected double sumValue;
67 protected boolean collect = true;
68 protected boolean broadcast = false;
69 protected boolean showNobs = true;
70
74 abstract public void init();
75
79 public void setName(String name) {
80 this.name = name;
81 }
82
89 public String getName() {
90 return name;
91 }
92
100 public double min() {
101 return minValue;
102 }
103
111 public double max() {
112 return maxValue;
113 }
114
123 public double sum() {
124 return sumValue;
125 }
126
133 abstract public double average();
134
142 abstract public String report();
143
154 abstract public String shortReport();
155
173 abstract public String shortReportHeader();
174
191 public static String report(String globalName, StatProbe[] probes) {
192 int maxn = 0;
193 StatProbe firstProbe = null;
194 for (StatProbe probe : probes) {
195 if (firstProbe == null)
196 firstProbe = probe;
197 String s = probe.getName();
198 if (s != null && s.length() > maxn)
199 maxn = s.length();
200 }
201 if (firstProbe == null)
202 return "";
203 StringBuffer sb = new StringBuffer("Report for ");
204 sb.append(globalName).append(PrintfFormat.NEWLINE);
205 for (int i = 0; i < maxn; i++)
206 sb.append(' ');
207 sb.append(" ");
208 sb.append(firstProbe.shortReportHeader()).append(PrintfFormat.NEWLINE);
209 for (StatProbe probe : probes) {
210 sb.append(PrintfFormat.s(-maxn, probe.getName()));
211 sb.append(" ");
212 sb.append(probe.shortReport()).append(PrintfFormat.NEWLINE);
213 }
214 return sb.toString();
215 }
216
227 public static String report(String globalName, Iterable<? extends StatProbe> probes) {
228 int maxn = 0;
229 StatProbe firstProbe = null;
230 for (StatProbe probe : probes) {
231 if (firstProbe == null)
232 firstProbe = probe;
233 String s = probe.getName();
234 int sl = s == null ? 4 : s.length();
235 if (sl > maxn)
236 maxn = sl;
237 }
238 if (firstProbe == null)
239 return "";
240 StringBuffer sb = new StringBuffer("Report for ");
241 sb.append(globalName).append(PrintfFormat.NEWLINE);
242 for (int i = 0; i < maxn; i++)
243 sb.append(' ');
244 sb.append(" ");
245 sb.append(firstProbe.shortReportHeader()).append(PrintfFormat.NEWLINE);
246 for (StatProbe probe : probes) {
247 sb.append(PrintfFormat.s(-maxn, probe.getName()));
248 sb.append(" ");
249 sb.append(probe.shortReport()).append(PrintfFormat.NEWLINE);
250 }
251 return sb.toString();
252 }
253
260 public boolean isBroadcasting() {
261 return broadcast;
262 }
263
272 public void setBroadcasting(boolean b) {
273 broadcast = b;
274 }
275
282 public boolean isCollecting() {
283 return collect;
284 }
285
294 public void setCollecting(boolean b) {
295 collect = b;
296 }
297
306 if (l == null)
307 throw new NullPointerException();
308 if (!listeners.contains(l))
309 listeners.add(l);
310 }
311
319 listeners.remove(l);
320 }
321
327 listeners.clear();
328 }
329
334 public void notifyListeners(double x) {
335 if (!broadcast)
336 return;
337 // We could also use the enhanced for loop here, but this is less efficient.
338 final int nl = listeners.size();
339 for (int i = 0; i < nl; i++)
340 listeners.get(i).newObservation(this, x);
341 }
342
343 public StatProbe clone() throws CloneNotSupportedException {
344 StatProbe s = (StatProbe) super.clone();
345 s.listeners = new ArrayList<ObservationListener>(listeners);
346 return s;
347 }
348}
The objects of this class are statistical probes or collectors, which are elementary devices for coll...
void notifyListeners(double x)
Notifies the observation x to all registered observers if broadcasting is ON.
abstract String shortReportHeader()
Returns a string containing the name of the values returned in the report strings.
abstract String shortReport()
Formats and returns a short, one-line report about this statistical probe.
boolean isCollecting()
Determines if this statistical probe is collecting values.
void removeObservationListener(ObservationListener l)
Removes the observation listener l from the list of observers of this statistical probe.
void addObservationListener(ObservationListener l)
Adds the observation listener l to the list of observers of this statistical probe.
abstract double average()
Returns the average for this collector.
abstract void init()
Initializes the statistical collector.
String getName()
Returns the name associated with this probe, or null if no name was specified upon construction.
boolean isBroadcasting()
Determines if this statistical probe is broadcasting observations to registered observers.
void setBroadcasting(boolean b)
Instructs the probe to turn its broadcasting ON or OFF.
double min()
Returns the smallest value taken by the variable since the last initialization of this probe.
static String report(String globalName, Iterable<? extends StatProbe > probes)
Equivalent to report(String,StatProbe[]), except that probes is an Iterable object instead of an arra...
void setName(String name)
Sets the name of this statistical collector to name.
void setCollecting(boolean b)
Turns ON or OFF the collection of statistical observations.
abstract String report()
Returns a string containing a report for this statistical collector.
double sum()
Returns the sum cumulated so far for this probe.
double max()
Returns the largest value taken by the variable since the last initialization of this probe.
void clearObservationListeners()
Removes all observation listeners from the list of observers of this statistical probe.
static String report(String globalName, StatProbe[] probes)
Formats short reports for each statistical probe in the array probes while aligning the probes’ names...
This class acts like a StringBuffer which defines new types of append methods.
static String s(String str)
Same as s(0, str).
static final String NEWLINE
End-of-line symbol or line separator.
Represents an object that can listen to observations broadcast by statistical probes.