View Javadoc
1   package org.djutils.stats.summarizers.event;
2   
3   import org.djutils.event.Event;
4   import org.djutils.event.EventListener;
5   import org.djutils.event.EventListenerMap;
6   import org.djutils.event.EventProducer;
7   import org.djutils.event.LocalEventProducer;
8   import org.djutils.exceptions.Throw;
9   import org.djutils.stats.summarizers.WeightedTally;
10  
11  /**
12   * The EventBasedWeightedTally class defines a time-weighted tally that can be notified with weights and values using the
13   * EventListener. It also produces events when values are tallied and when the tally is initialized. It embeds an EventProducer
14   * so it can keep listeners informed about new observations.
15   * <p>
16   * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
18   * project is distributed under a three-clause BSD-style license, which can be found at
19   * <a href="https://simulation.tudelft.nl/dsol/3.0/license.html" target="_blank">
20   * https://simulation.tudelft.nl/dsol/3.0/license.html</a>. <br>
21   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank"> Alexander Verbraeck</a>
22   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
23   */
24  public class EventBasedWeightedTally extends WeightedTally implements EventProducer, EventListener
25  {
26      /** The embedded EventProducer. */
27      private EventProducer eventProducer = null;
28  
29      /**
30       * Construct a new WeightedTally with a description.
31       * @param description the description of this WeightedTally
32       */
33      public EventBasedWeightedTally(final String description)
34      {
35          this(description, new LocalEventProducer());
36      }
37  
38      /**
39       * Construct a new WeightedTally with a description.
40       * @param description the description of this WeightedTally
41       * @param eventProducer the EventProducer to embed and use in this statistic
42       */
43      public EventBasedWeightedTally(final String description, final EventProducer eventProducer)
44      {
45          super(description);
46          Throw.whenNull(eventProducer, "eventProducer cannot be null");
47          this.eventProducer = eventProducer;
48      }
49  
50      @Override
51      public EventListenerMap getEventListenerMap()
52      {
53          return this.eventProducer.getEventListenerMap();
54      }
55  
56      @Override
57      public void initialize()
58      {
59          super.initialize();
60          if (this.eventProducer != null)
61          {
62              this.eventProducer.fireEvent(StatisticsEvents.INITIALIZED_EVENT);
63          }
64      }
65  
66      @Override
67      public void notify(final Event event)
68      {
69          // metadata descriptor checks content
70          Object[] content = (Object[]) event.getContent();
71          double weight = ((Number) content[0]).doubleValue();
72          double value = ((Number) content[1]).doubleValue();
73          register(weight, value);
74      }
75  
76      /**
77       * Process one observed weighted value.
78       * @param weight the weight of the value to process
79       * @param value the value to process
80       * @return the value
81       */
82      @Override
83      public double register(final double weight, final double value)
84      {
85          super.register(weight, value);
86          if (hasListeners())
87          {
88              this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_OBSERVATION_ADDED_EVENT, new Object[] {weight, value});
89              fireEvents();
90          }
91          return value;
92      }
93  
94      /**
95       * Method that can be overridden to fire own events or additional events when registering an observation.
96       */
97      protected void fireEvents()
98      {
99          this.eventProducer.fireEvent(StatisticsEvents.N_EVENT, getN());
100         this.eventProducer.fireEvent(StatisticsEvents.MIN_EVENT, getMin());
101         this.eventProducer.fireEvent(StatisticsEvents.MAX_EVENT, getMax());
102         this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_POPULATION_MEAN_EVENT, getWeightedPopulationMean());
103         this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_POPULATION_VARIANCE_EVENT, getWeightedPopulationVariance());
104         this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_POPULATION_STDEV_EVENT, getWeightedPopulationStDev());
105         this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_SUM_EVENT, getWeightedSum());
106         this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_SAMPLE_MEAN_EVENT, getWeightedSampleMean());
107         this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_SAMPLE_VARIANCE_EVENT, getWeightedSampleVariance());
108         this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_SAMPLE_STDEV_EVENT, getWeightedSampleStDev());
109     }
110 
111     @Override
112     public String toString()
113     {
114         return super.toString();
115     }
116 
117 }