View Javadoc
1   package org.djutils.stats.summarizers.event;
2   
3   import java.rmi.RemoteException;
4   
5   import org.djutils.event.Event;
6   import org.djutils.event.EventListener;
7   import org.djutils.event.EventListenerMap;
8   import org.djutils.event.EventProducer;
9   import org.djutils.event.LocalEventProducer;
10  import org.djutils.exceptions.Throw;
11  import org.djutils.stats.summarizers.Tally;
12  import org.djutils.stats.summarizers.quantileaccumulator.NoStorageAccumulator;
13  import org.djutils.stats.summarizers.quantileaccumulator.QuantileAccumulator;
14  
15  /**
16   * The EventBasedTally class registers a series of values and provides mean, standard deviation, etc. of the registered values.
17   * It embeds an EventProducer so it can keep listeners informed about new observations, and it listens to external events to be
18   * able to receive observations, in addition to the register(...) method.
19   * <p>
20   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
21   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
22   * project is distributed under a three-clause BSD-style license, which can be found at
23   * <a href="https://simulation.tudelft.nl/dsol/3.0/license.html" target="_blank">
24   * https://simulation.tudelft.nl/dsol/3.0/license.html</a>. <br>
25   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank"> Alexander Verbraeck</a>
26   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
27   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
28   */
29  public class EventBasedTally extends Tally implements EventProducer, EventListener
30  {
31      /** */
32      private static final long serialVersionUID = 20200228L;
33  
34      /** The embedded EventProducer. */
35      private EventProducer eventProducer = null;
36  
37      /**
38       * Constructs a new EventBasedTally.
39       * @param description String; the description of this tally
40       * @param quantileAccumulator QuantileAccumulator; the input series accumulator that can approximate or compute quantiles.
41       */
42      public EventBasedTally(final String description, final QuantileAccumulator quantileAccumulator)
43      {
44          this(description, quantileAccumulator, new LocalEventProducer());
45      }
46  
47      /**
48       * Convenience constructor that uses a NoStorageAccumulator to estimate quantiles.
49       * @param description String; the description of this tally
50       */
51      public EventBasedTally(final String description)
52      {
53          this(description, new NoStorageAccumulator());
54      }
55  
56      /**
57       * Construct a new EventBasedCounter with a specific EventProducer, e.g. a remote one. The Tally uses uses a
58       * NoStorageAccumulator to estimate quantiles.
59       * @param description String; the description for this counter
60       * @param eventProducer EventProducer; the EventProducer to embed and use in this statistic
61       */
62      public EventBasedTally(final String description, final EventProducer eventProducer)
63      {
64          this(description, new NoStorageAccumulator(), eventProducer);
65      }
66  
67      /**
68       * Construct a new EventBasedCounter with a specific EventProducer, e.g. a remote one.
69       * @param description String; the description for this counter
70       * @param quantileAccumulator QuantileAccumulator; the input series accumulator that can approximate or compute quantiles.
71       * @param eventProducer EventProducer; the EventProducer to embed and use in this statistic
72       */
73      public EventBasedTally(final String description, final QuantileAccumulator quantileAccumulator,
74              final EventProducer eventProducer)
75      {
76          super(description, quantileAccumulator);
77          Throw.whenNull(eventProducer, "eventProducer cannot be null");
78          this.eventProducer = eventProducer;
79      }
80  
81      @Override
82      public EventListenerMap getEventListenerMap() throws RemoteException
83      {
84          return this.eventProducer.getEventListenerMap();
85      }
86  
87      @Override
88      public void initialize()
89      {
90          super.initialize();
91          if (this.eventProducer != null)
92          {
93              try
94              {
95                  this.eventProducer.fireEvent(StatisticsEvents.INITIALIZED_EVENT);
96              }
97              catch (RemoteException exception)
98              {
99                  throw new RuntimeException(exception);
100             }
101         }
102     }
103 
104     @Override
105     @SuppressWarnings("checkstyle:designforextension")
106     public void notify(final Event event)
107     {
108         if (!(event.getContent() instanceof Number))
109         {
110             throw new IllegalArgumentException("Tally does not accept " + event);
111         }
112         double value = ((Number) event.getContent()).doubleValue();
113         register(value);
114     }
115 
116     @Override
117     public double register(final double value)
118     {
119         super.register(value);
120         try
121         {
122             if (hasListeners())
123             {
124                 this.eventProducer.fireEvent(StatisticsEvents.OBSERVATION_ADDED_EVENT, value);
125                 fireEvents();
126             }
127         }
128         catch (RemoteException exception)
129         {
130             throw new RuntimeException(exception);
131         }
132         return value;
133     }
134 
135     /**
136      * Method that can be overridden to fire own events or additional events when registering an observation.
137      * @throws RemoteException on network error
138      */
139     protected void fireEvents() throws RemoteException
140     {
141         this.eventProducer.fireEvent(StatisticsEvents.N_EVENT, getN());
142         this.eventProducer.fireEvent(StatisticsEvents.MIN_EVENT, getMin());
143         this.eventProducer.fireEvent(StatisticsEvents.MAX_EVENT, getMax());
144         this.eventProducer.fireEvent(StatisticsEvents.POPULATION_MEAN_EVENT, getPopulationMean());
145         this.eventProducer.fireEvent(StatisticsEvents.POPULATION_VARIANCE_EVENT, getPopulationVariance());
146         this.eventProducer.fireEvent(StatisticsEvents.POPULATION_SKEWNESS_EVENT, getPopulationSkewness());
147         this.eventProducer.fireEvent(StatisticsEvents.POPULATION_KURTOSIS_EVENT, getPopulationKurtosis());
148         this.eventProducer.fireEvent(StatisticsEvents.POPULATION_EXCESS_KURTOSIS_EVENT, getPopulationExcessKurtosis());
149         this.eventProducer.fireEvent(StatisticsEvents.POPULATION_STDEV_EVENT, getPopulationStDev());
150         this.eventProducer.fireEvent(StatisticsEvents.SUM_EVENT, getSum());
151         this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_MEAN_EVENT, getSampleMean());
152         this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_VARIANCE_EVENT, getSampleVariance());
153         this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_SKEWNESS_EVENT, getSampleSkewness());
154         this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_KURTOSIS_EVENT, getSampleKurtosis());
155         this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_EXCESS_KURTOSIS_EVENT, getSampleExcessKurtosis());
156         this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_STDEV_EVENT, getSampleStDev());
157     }
158 
159     @Override
160     @SuppressWarnings("checkstyle:designforextension")
161     public String toString()
162     {
163         return "EventBasedTally" + super.toString().substring(5);
164     }
165 
166 }