View Javadoc
1   /**
2    * 
3    */
4   package org.opentrafficsim.water.statistics;
5   
6   import org.apache.poi.ss.usermodel.CellStyle;
7   import org.apache.poi.ss.usermodel.DataFormat;
8   import org.apache.poi.ss.usermodel.Row;
9   import org.apache.poi.ss.usermodel.Sheet;
10  
11  import com.thoughtworks.xstream.annotations.XStreamAlias;
12  import com.thoughtworks.xstream.annotations.XStreamOmitField;
13  
14  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
15  
16  /**
17   * <br>
18   * Copyright (c) 2011-2013 TU Delft, Faculty of TBM, Systems and Simulation <br>
19   * This software is licensed without restrictions to Nederlandse Organisatie voor Toegepast Natuurwetenschappelijk Onderzoek TNO
20   * (TNO), Erasmus University Rotterdam, Delft University of Technology, Panteia B.V., Stichting Projecten Binnenvaart, Ab Ovo
21   * Nederland B.V., Modality Software Solutions B.V., and Rijkswaterstaat - Dienst Water, Verkeer en Leefomgeving, including the
22   * right to sub-license sources and derived products to third parties. <br>
23   * @version Mar 24, 2013 <br>
24   * @author <a href="http://tudelft.nl/averbraeck">Alexander Verbraeck </a>
25   */
26  @XStreamAlias("persistent")
27  public class XPersistent extends XTally
28  {
29      /** */
30      @XStreamOmitField
31      private static final long serialVersionUID = 1L;
32  
33      /** */
34      @XStreamOmitField
35      private DEVSSimulatorInterface.TimeDoubleUnit simulator;
36  
37      /** startTime defines the time of the first event */
38      @XStreamOmitField
39      private double startTime = Double.NaN;
40  
41      /** elapsedTime tracks the elapsed time */
42      private double elapsedTime = Double.NaN;
43  
44      /** lastvalue tracks the last value */
45      @XStreamOmitField
46      private double lastValue = Double.NaN;
47  
48      /**
49       * @param description String; description of the statistic
50       * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the simulator
51       */
52      public XPersistent(String description, DEVSSimulatorInterface.TimeDoubleUnit simulator)
53      {
54          super(description);
55          this.simulator = simulator;
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public double getSampleMean()
61      {
62          if (super.n > 1 && this.elapsedTime > 0)
63              return super.sum / elapsedTime;
64          else
65              return Double.NaN;
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public double getStdDev()
71      {
72          synchronized (this.semaphore)
73          {
74              if (super.n > 1)
75              {
76                  return Math.sqrt(super.varianceSum / this.elapsedTime);
77              }
78              return Double.NaN;
79          }
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public double getSampleVariance()
85      {
86          synchronized (this.semaphore)
87          {
88              if (super.n > 1)
89              {
90                  return super.varianceSum / this.elapsedTime;
91              }
92              return Double.NaN;
93          }
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public void initialize()
99      {
100         synchronized (this.semaphore)
101         {
102             super.initialize();
103             this.elapsedTime = 0.0;
104             this.lastValue = 0.0;
105         }
106     }
107 
108     /**
109      * dstat
110      * @param value double; value
111      */
112     public void persist(final double value)
113     {
114         if (!Double.isNaN(value))
115         {
116             synchronized (this.semaphore)
117             {
118                 double simTime = this.simulator.getSimulatorTime().si;
119                 super.n++;
120                 if (value < super.min)
121                     super.min = value;
122                 if (value > super.max)
123                     super.max = value;
124 
125                 if (super.n == 1)
126                     this.startTime = simTime;
127                 else
128                 {
129                     double deltaTime = simTime - (this.elapsedTime + this.startTime);
130                     if (deltaTime > 0.0)
131                     {
132                         super.sum += lastValue * deltaTime;
133                         super.varianceSum += lastValue * lastValue * deltaTime;
134                         this.elapsedTime = this.elapsedTime + deltaTime;
135                     }
136                 }
137                 this.lastValue = value;
138             }
139         }
140     }
141 
142     /**
143      * @see org.opentrafficsim.water.statistics.XTally#writeToExcel(org.apache.poi.ss.usermodel.Sheet, int)
144      */
145     @Override
146     public int writeToExcel(final Sheet sheet, final int startRow)
147     {
148         DataFormat format = sheet.getWorkbook().createDataFormat();
149         CellStyle style = sheet.getWorkbook().createCellStyle();
150         style.setDataFormat(format.getFormat("0.00"));
151 
152         int rownr = startRow;
153         Row row = sheet.createRow(rownr);
154 
155         row.createCell(1).setCellValue(description);
156         row.createCell(2).setCellValue("tijdgewogen [n, gem, stdev, min, max]");
157         row.createCell(3).setCellValue(getN());
158         if (getN() > 0)
159         {
160             row.createCell(4).setCellValue(getSampleMean());
161             row.getCell(4).setCellStyle(style);
162             if (getN() > 1 && !Double.isNaN(getStdDev()))
163             {
164                 row.createCell(5).setCellValue(getStdDev());
165                 row.getCell(5).setCellStyle(style);
166             }
167             row.createCell(6).setCellValue(getMin());
168             row.getCell(6).setCellStyle(style);
169             row.createCell(7).setCellValue(getMax());
170             row.getCell(7).setCellStyle(style);
171         }
172 
173         return rownr + 1;
174     }
175 
176 }