View Javadoc
1   package org.djutils.stats.summarizers.event;
2   
3   import java.io.Serializable;
4   import java.util.Calendar;
5   
6   import org.djutils.event.Event;
7   import org.djutils.event.EventInterface;
8   import org.djutils.event.EventListenerInterface;
9   import org.djutils.event.EventProducer;
10  import org.djutils.event.TimedEvent;
11  import org.djutils.stats.summarizers.TimestampTallyInterface;
12  import org.djutils.stats.summarizers.TimestampWeightedTally;
13  
14  /**
15   * The TimestampWeightedTally class defines a time-weighted tally based on timestamped data. The difference with a normal
16   * time-weighed tally is that the weight of a value is only known at the occurrence of the next timestamp. Furthermore, a last
17   * timestamp needs to be specified to determine the weight of the last value. This EventBased version of the tally can be
18   * notified with timestamps and values using the EventListenerInterface. It also produces events when values are tallied and
19   * when the tally is initialized. Timestamps can be Number based or Calendar based.
20   * <p>
21   * Copyright (c) 2020-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
22   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
23   * project is distributed under a three-clause BSD-style license, which can be found at
24   * <a href="https://simulation.tudelft.nl/dsol/3.0/license.html" target="_blank">
25   * https://simulation.tudelft.nl/dsol/3.0/license.html</a>. <br>
26   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank"> Alexander Verbraeck</a>
27   */
28  public class EventBasedTimestampWeightedTally extends EventProducer implements EventListenerInterface, TimestampTallyInterface
29  {
30      /** */
31      private static final long serialVersionUID = 20200228L;
32  
33      /** the wrapped timestamp weighted tally. */
34      private TimestampWeightedTally wrappedTimestampWeightedTally;
35  
36      /**
37       * constructs a new EventBasefdTimestampWeightedTally with a description.
38       * @param description String; the description of this EventBasedTimestampWeightedTally
39       */
40      public EventBasedTimestampWeightedTally(final String description)
41      {
42          this.wrappedTimestampWeightedTally = new TimestampWeightedTally(description);
43          initialize();
44      }
45  
46      /** {@inheritDoc} */
47      @Override
48      public Serializable getSourceId()
49      {
50          return this;
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public void initialize()
56      {
57          this.wrappedTimestampWeightedTally.initialize();
58          fireEvent(new Event(StatisticsEvents.INITIALIZED_EVENT, this, null));
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public final boolean isActive()
64      {
65          return this.wrappedTimestampWeightedTally.isActive();
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public void endObservations(final Number timestamp)
71      {
72          this.wrappedTimestampWeightedTally.endObservations(timestamp);
73          fireEvents(timestamp.doubleValue(), this.wrappedTimestampWeightedTally.getLastValue());
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public void endObservations(final Calendar timestamp)
79      {
80          this.wrappedTimestampWeightedTally.endObservations(timestamp);
81          fireEvents(timestamp.getTimeInMillis(), this.wrappedTimestampWeightedTally.getLastValue());
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public void notify(final EventInterface event)
87      {
88          if (event instanceof TimedEvent<?>)
89          {
90              TimedEvent<?> timedEvent = (TimedEvent<?>) event;
91              double value = 0.0;
92              if (event.getContent() instanceof Number)
93              {
94                  value = ((Number) event.getContent()).doubleValue();
95              }
96              else
97              {
98                  throw new IllegalArgumentException(
99                          "EventBasedTimestampWeightedTally.notify: Content " + event.getContent() + " should be a Number");
100             }
101             Object timestamp = timedEvent.getTimeStamp();
102             if (timestamp instanceof Number)
103             {
104                 register(((Number) timestamp).doubleValue(), value);
105             }
106             else if (timestamp instanceof Calendar)
107             {
108                 register(((Calendar) timestamp).getTimeInMillis(), value);
109             }
110             else
111             {
112                 throw new IllegalArgumentException(
113                         "EventBasedTimestampWeightedTally.notify: timestamp should be a Number or Calendar");
114             }
115         }
116         else
117         {
118             throw new IllegalArgumentException("EventBasedTimestampWeightedTally.notify: Event should be a TimedEvent");
119         }
120     }
121 
122     /**
123      * Process one observed value.
124      * @param timestamp Calendar; the Calendar object representing the timestamp
125      * @param value double; the value to process
126      * @return double; the value
127      */
128     public double register(final Calendar timestamp, final double value)
129     {
130         this.wrappedTimestampWeightedTally.register(timestamp, value);
131         fireEvents(timestamp, value);
132         return value;
133     }
134 
135     /**
136      * Process one observed value.
137      * @param <T> a type for the timestamp that extends Number and is Comparable, e.g., a Double, a Long, or a djunits Time
138      * @param timestamp T; the object representing the timestamp
139      * @param value double; the value to process
140      * @return double; the value
141      */
142     public <T extends Number & Comparable<T>> double register(final T timestamp, final double value)
143     {
144         this.wrappedTimestampWeightedTally.register(timestamp, value);
145         fireEvents(timestamp, value);
146         return value;
147     }
148 
149     /**
150      * Fire the events to potential listeners with the timestamp and value.
151      * @param <T> a type for the timestamp that is Serializable and Comparable
152      * @param timestamp T; Number or Calendar timestamp
153      * @param value double; observation value
154      */
155     private <T extends Serializable & Comparable<T>> void fireEvents(final T timestamp, final double value)
156     {
157         if (hasListeners())
158         {
159             this.fireEvent(
160                     new Event(StatisticsEvents.TIMESTAMPED_OBSERVATION_ADDED_EVENT, this, new Object[] {timestamp, value}));
161             fireEvents(timestamp);
162         }
163     }
164 
165     /**
166      * Method that can be overridden to fire own events or additional events when ingesting an observation.
167      * @param <T> a type for the timestamp that is Serializable and Comparable
168      * @param timestamp T; the timestamp to use in the TimedEvents
169      */
170     protected <T extends Serializable & Comparable<T>> void fireEvents(final T timestamp)
171     {
172         fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_N_EVENT, this, getN(), timestamp));
173         fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_MIN_EVENT, this, getMin(), timestamp));
174         fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_MAX_EVENT, this, getMax(), timestamp));
175         fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_POPULATION_MEAN_EVENT, this,
176                 getWeightedPopulationMean(), timestamp));
177         fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_POPULATION_VARIANCE_EVENT, this,
178                 getWeightedPopulationVariance(), timestamp));
179         fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_POPULATION_STDEV_EVENT, this,
180                 getWeightedPopulationStDev(), timestamp));
181         fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_SUM_EVENT, this, getWeightedSum(), timestamp));
182         fireTimedEvent(
183                 new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_SAMPLE_MEAN_EVENT, this, getWeightedSampleMean(), timestamp));
184         fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_SAMPLE_VARIANCE_EVENT, this,
185                 getWeightedSampleVariance(), timestamp));
186         fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_SAMPLE_STDEV_EVENT, this, getWeightedSampleStDev(),
187                 timestamp));
188     }
189 
190     /** {@inheritDoc} */
191     @Override
192     public final String getDescription()
193     {
194         return this.wrappedTimestampWeightedTally.getDescription();
195     }
196 
197     /** {@inheritDoc} */
198     @Override
199     public final long getN()
200     {
201         return this.wrappedTimestampWeightedTally.getN();
202     }
203 
204     /** {@inheritDoc} */
205     @Override
206     public final double getMax()
207     {
208         return this.wrappedTimestampWeightedTally.getMax();
209     }
210 
211     /** {@inheritDoc} */
212     @Override
213     public final double getMin()
214     {
215         return this.wrappedTimestampWeightedTally.getMin();
216     }
217 
218     /** {@inheritDoc} */
219     @Override
220     public final double getWeightedSampleMean()
221     {
222         return this.wrappedTimestampWeightedTally.getWeightedSampleMean();
223     }
224 
225     /** {@inheritDoc} */
226     @Override
227     public final double getWeightedSampleStDev()
228     {
229         return this.wrappedTimestampWeightedTally.getWeightedSampleStDev();
230     }
231 
232     /** {@inheritDoc} */
233     @Override
234     public final double getWeightedPopulationStDev()
235     {
236         return this.wrappedTimestampWeightedTally.getWeightedPopulationStDev();
237     }
238 
239     /** {@inheritDoc} */
240     @Override
241     public final double getWeightedSampleVariance()
242     {
243         return this.wrappedTimestampWeightedTally.getWeightedSampleVariance();
244     }
245 
246     /** {@inheritDoc} */
247     @Override
248     public final double getWeightedPopulationVariance()
249     {
250         return this.wrappedTimestampWeightedTally.getWeightedPopulationVariance();
251     }
252 
253     /** {@inheritDoc} */
254     @Override
255     public final double getWeightedSum()
256     {
257         return this.wrappedTimestampWeightedTally.getWeightedSum();
258     }
259 
260     /** {@inheritDoc} */
261     @Override
262     @SuppressWarnings("checkstyle:designforextension")
263     public String toString()
264     {
265         return this.wrappedTimestampWeightedTally.toString();
266     }
267 
268 }