SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
ListWithStat.java
1/*
2 * Class: ListWithStat
3 * Description: Implements a list with integrated statistical probes to
4 provide automatic collection of statistics on the sojourn
5 times of objects in the list and on the size of the list
6 * Environment: Java
7 * Software: SSJ
8 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
9 * Organization: DIRO, Universite de Montreal
10 * @author
11 * @since
12 *
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 *
26 */
27package umontreal.ssj.simevents;
28
29import java.util.Collection;
30import java.util.List;
31import java.util.Iterator;
32import java.util.ListIterator;
33import java.util.NoSuchElementException;
34import umontreal.ssj.stat.Tally;
35import umontreal.ssj.util.TransformingList;
36import umontreal.ssj.util.PrintfFormat;
37
50public class ListWithStat<E> extends TransformingList<E, ListWithStat.Node<E>> {
51 private boolean stats; // true si on a appele setStatCollecting
52 private double initTime; // temps de la derniere initialisation
53 private Accumulate blockSize; // block stat. sur la longueur de la liste
54 private Tally blockSojourn; // block stat. sur les durees de sejour
55 private String name;
56 private Simulator sim;
57
63 public ListWithStat(List<Node<E>> nodeList) {
64 super(nodeList);
65 nodeList.clear();
67 stats = false;
68 }
69
76 public ListWithStat(Simulator inSim, List<Node<E>> nodeList) {
77 super(nodeList);
78 if (inSim == null || nodeList == null)
79 throw new NullPointerException();
80 nodeList.clear();
81 sim = inSim;
82 stats = false;
83 }
84
92 public ListWithStat(List<Node<E>> nodeList, Collection<? extends E> c) {
93 this(Simulator.getDefaultSimulator(), nodeList);
94 addAll(c);
95 }
96
105 public ListWithStat(Simulator inSim, List<Node<E>> nodeList, Collection<? extends E> c) {
106 this(inSim, nodeList);
107 addAll(c);
108 }
109
119 public ListWithStat(List<Node<E>> nodeList, String name) {
120 this(Simulator.getDefaultSimulator(), nodeList);
121 this.name = name;
122 }
123
133 public ListWithStat(Simulator inSim, List<Node<E>> nodeList, String name) {
134 this(inSim, nodeList);
135 this.name = name;
136 }
137
147 public ListWithStat(List<Node<E>> nodeList, Collection<? extends E> c, String name) {
148 this(Simulator.getDefaultSimulator(), nodeList);
149 this.name = name;
150 addAll(c);
151 }
152
163 public ListWithStat(Simulator inSim, List<Node<E>> nodeList, Collection<? extends E> c, String name) {
164 this(inSim, nodeList);
165 this.name = name;
166 addAll(c);
167 }
168
169 public E convertFromInnerType(Node<E> node) {
170 return node.getElement();
171 }
172
173 public Node<E> convertToInnerType(E element) {
174 return new Node<E>(element, sim.time());
175 }
176
183 return sim;
184 }
185
192 public void setSimulator(Simulator sim) {
193 if (sim == null)
194 throw new NullPointerException();
195 this.sim = sim;
196 if (blockSize != null)
197 blockSize.setSimulator(sim);
198 }
199
200 @Override
201 public void clear() {
202 if (stats)
203 initStat();
204 super.clear();
205 }
206
207 @Override
208 public void add(int index, E obj) {
209 super.add(index, obj);
210 if (stats)
211 blockSize.update(size());
212 }
213
214 @Override
215 public E remove(int index) {
216 Node<E> node = getInnerList().get(index);
217 if (stats)
218 blockSojourn.add(sim.time() - node.getInsertionTime());
219 E e = super.remove(index);
220 if (stats)
221 blockSize.update(size());
222 return e;
223 }
224
225 @Override
226 public Iterator<E> iterator() {
227 return new IteratorWithStat(getInnerList().iterator());
228 }
229
230 @Override
231 public ListIterator<E> listIterator() {
232 return new ListIteratorWithStat(getInnerList().listIterator());
233 }
234
235 @Override
236 public ListIterator<E> listIterator(int index) {
237 return new ListIteratorWithStat(getInnerList().listIterator(index));
238 }
239
240 @Override
241 public E set(int index, E element) {
242 Node<E> oldNode = getInnerList().get(index);
243 E oldElement = oldNode.getElement();
244 boolean equal;
245 if (oldElement == null || element == null)
246 equal = oldElement == element;
247 else
248 equal = oldElement.equals(element);
249 if (equal) {
250 getInnerList().set(index, new Node<E>(element, oldNode.getInsertionTime()));
251 return oldElement;
252 } else {
253 if (stats)
254 blockSojourn.add(sim.time() - oldNode.getInsertionTime());
255 getInnerList().set(index, new Node<E>(element, sim.time()));
256 return oldElement;
257 }
258 }
259
260 private class IteratorWithStat implements Iterator<E> {
261 private Iterator<Node<E>> itr;
262 private Node<E> lastRet;
263
264 public IteratorWithStat(Iterator<Node<E>> itr) {
265 this.itr = itr;
266 }
267
268 public boolean hasNext() {
269 return itr.hasNext();
270 }
271
272 public E next() {
273 lastRet = itr.next();
274 return lastRet.getElement();
275 }
276
277 public void remove() {
278 itr.remove();
279 if (stats) {
280 blockSize.update(size());
281 blockSojourn.add(sim.time() - lastRet.getInsertionTime());
282 }
283 lastRet = null;
284 }
285 }
286
287 private class ListIteratorWithStat implements ListIterator<E> {
288 private ListIterator<Node<E>> itr;
289 private Node<E> lastRet;
290
291 public ListIteratorWithStat(ListIterator<Node<E>> itr) {
292 this.itr = itr;
293 }
294
295 public void add(E o) {
296 itr.add(new Node<E>(o, sim.time()));
297 lastRet = null;
298 if (stats)
299 blockSize.update(size());
300 }
301
302 public boolean hasNext() {
303 return itr.hasNext();
304 }
305
306 public boolean hasPrevious() {
307 return itr.hasPrevious();
308 }
309
310 public E next() {
311 lastRet = itr.next();
312 return lastRet.getElement();
313 }
314
315 public int nextIndex() {
316 return itr.nextIndex();
317 }
318
319 public E previous() {
320 lastRet = itr.previous();
321 return lastRet.getElement();
322 }
323
324 public int previousIndex() {
325 return itr.previousIndex();
326 }
327
328 public void remove() {
329 itr.remove();
330 if (stats) {
331 blockSize.update(size());
332 blockSojourn.add(sim.time() - lastRet.getInsertionTime());
333 }
334 lastRet = null;
335 }
336
337 public void set(E element) {
338 if (lastRet == null)
339 throw new NoSuchElementException();
340 Node<E> oldNode = lastRet;
341 E oldElement = oldNode.getElement();
342 boolean equal;
343 if (oldElement == null || element == null)
344 equal = oldElement == element;
345 else
346 equal = oldElement.equals(element);
347 if (equal) {
348 lastRet = new Node<E>(element, oldNode.getInsertionTime());
349 itr.set(lastRet);
350 } else {
351 if (stats)
352 blockSojourn.add(sim.time() - oldNode.getInsertionTime());
353 lastRet = new Node<E>(element, sim.time());
354 itr.set(lastRet);
355 }
356 }
357 }
358
362
370 public boolean getStatCollecting() {
371 return stats;
372 }
373
390 public void setStatCollecting(boolean b) {
391 if (b && !stats) {
392 if (blockSize == null)
393 blockSize = new Accumulate(sim, "List Size " + name);
394 if (blockSojourn == null)
395 blockSojourn = new Tally("List Sojourn " + name);
396 blockSize.update(size());
397 stats = true;
398 initStat();
399 } else
400 stats = false;
401 }
402
410 public void initStat() {
411 if (!stats)
412 throw new IllegalStateException("initStat for a list that did not call setStatCollecting (true).");
413 blockSize.init();
414 blockSojourn.init();
415 blockSize.update(size());
416 initTime = sim.time();
417 }
418
424 public double getInitTime() {
425 return initTime;
426 }
427
437 return blockSize;
438 }
439
448 return blockSojourn;
449 }
450
463 public String report() {
464 if (blockSojourn == null || blockSize == null)
465 throw new IllegalStateException("Calling report when no statistics were collected");
466
467 PrintfFormat str = new PrintfFormat();
468 str.append(PrintfFormat.NEWLINE + "REPORT ON LIST : ").append(name).append(PrintfFormat.NEWLINE);
469 str.append(" From time: ").append(7, 2, 2, initTime);
470 str.append(" to time: ").append(10, 2, 2, sim.time());
471 str.append(" min max average ");
472 str.append("standard dev. nb. Obs");
473
474 str.append(" Size ");
475 str.append(9, (int) (blockSize.min() + 0.5));
476 str.append(11, (int) (blockSize.max() + 0.5));
477 str.append(14, 3, 2, blockSize.average()).append(PrintfFormat.NEWLINE);
478
479 str.append(" Sojourn ");
480 str.append(12, 3, 2, blockSojourn.min()).append(" ");
481 str.append(10, 3, 2, blockSojourn.max()).append(" ");
482 str.append(10, 3, 2, blockSojourn.average()).append(" ");
483 str.append(10, 3, 2, blockSojourn.standardDeviation()).append(" ");
484 str.append(11, blockSojourn.numberObs()).append(PrintfFormat.NEWLINE);
485
486 return str.toString();
487 }
488
494 public String getName() {
495 return name;
496 }
497
501
505
509 public static class Node<E> {
510 private E element;
511 private double insertionTime;
512
516
524 public Node(E element, double insertionTime) {
525 this.element = element;
526 this.insertionTime = insertionTime;
527 }
528
534 public E getElement() {
535 return element;
536 }
537
543 public double getInsertionTime() {
544 return insertionTime;
545 }
546
547 public String toString() {
548 String str = element == null ? "null" : element.toString();
549 str += " (inserted at time " + insertionTime + ")";
550 return str;
551 }
552 }
553
554}
A subclass of umontreal.ssj.stat.StatProbe, for collecting statistics on a variable that evolves in s...
void update()
Updates the accumulator using the last value passed to update(double).
Represents a node that can be part of a list with statistical collecting.
Node(E element, double insertionTime)
Constructs a new node containing element element inserted into the list at time insertionTime.
E getElement()
Returns the element stored into this node.
double getInsertionTime()
Returns the insertion time of the element in this node.
void setStatCollecting(boolean b)
Starts or stops collecting statistics on this list.
Simulator simulator()
Returns the simulator associated with this list.
ListWithStat(Simulator inSim, List< Node< E > > nodeList)
Constructs a new list with internal data structure implemented by nodeList.
ListWithStat(List< Node< E > > nodeList, String name)
Constructs a new list with name name, internal list nodeList, and using the default simulator.
String report()
Returns a string containing a statistical report on the list, provided that setStatCollecting(true) h...
double getInitTime()
Returns the last simulation time initStat was called.
boolean getStatCollecting()
Returns true if the list collects statistics about its size and sojourn times of elements,...
ListWithStat(List< Node< E > > nodeList, Collection<? extends E > c)
Constructs a list containing the elements of the specified collection, whose elements are stored into...
ListWithStat(List< Node< E > > nodeList, Collection<? extends E > c, String name)
Constructs a new list containing the elements of the specified collection c, with name name,...
String getName()
Returns the name associated to this list, or null if no name was assigned.
ListWithStat(Simulator inSim, List< Node< E > > nodeList, String name)
Constructs a new list with name name, and internal list nodeList.
Tally statSojourn()
Returns the statistical probe on the sojourn times of the objects in the list.
void initStat()
Reinitializes the two statistical probes created by setStatCollecting(true) and makes an update for t...
ListWithStat(Simulator inSim, List< Node< E > > nodeList, Collection<? extends E > c)
Constructs a list containing the elements of the specified collection, whose elements are stored into...
ListWithStat(List< Node< E > > nodeList)
Constructs a new list with internal data structure using the default simulator and implemented by nod...
ListWithStat(Simulator inSim, List< Node< E > > nodeList, Collection<? extends E > c, String name)
Constructs a new list containing the elements of the specified collection c, with name name,...
Accumulate statSize()
Returns the statistical probe on the evolution of the size of the list as a function of the simulatio...
void setSimulator(Simulator sim)
Sets the simulator associated with this list.
Represents the executive of a discrete-event simulator.
static Simulator getDefaultSimulator()
Returns the default simulator instance used by the deprecated class.
double time()
Returns the current value of the simulation clock.
A subclass of StatProbe.
Definition Tally.java:47
void add(double x)
Gives a new observation x to the statistical collector.
Definition Tally.java:109
This class acts like a StringBuffer which defines new types of append methods.
String toString()
Converts the buffer into a String.
PrintfFormat append(String str)
Appends str to the buffer.
static final String NEWLINE
End-of-line symbol or line separator.
TransformingList(List< IE > fromList)
Creates a new transforming list wrapping the inner list fromList.