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  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
11  
12  import com.thoughtworks.xstream.annotations.XStreamAlias;
13  import com.thoughtworks.xstream.annotations.XStreamOmitField;
14  
15  /**
16   * <br>
17   * Copyright (c) 2011-2013 TU Delft, Faculty of TBM, Systems and Simulation <br>
18   * This software is licensed without restrictions to Nederlandse Organisatie voor Toegepast Natuurwetenschappelijk
19   * Onderzoek TNO (TNO), Erasmus University Rotterdam, Delft University of Technology, Panteia B.V., Stichting Projecten
20   * Binnenvaart, Ab Ovo Nederland B.V., Modality Software Solutions B.V., and Rijkswaterstaat - Dienst Water, Verkeer en
21   * Leefomgeving, including the right to sub-license sources and derived products to third parties. <br>
22   * 
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 OTSDEVSSimulatorInterface 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 description of the statistic
50       * @param simulator the simulator
51       */
52      public XPersistent(String description, OTSDEVSSimulatorInterface 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      * 
111      * @param value value
112      */
113     public void persist(final double value)
114     {
115         if (!Double.isNaN(value))
116         {
117             synchronized (this.semaphore)
118             {
119                 double simTime = this.simulator.getSimulatorTime().getTime().si;
120                 super.n++;
121                 if (value < super.min)
122                     super.min = value;
123                 if (value > super.max)
124                     super.max = value;
125 
126                 if (super.n == 1)
127                     this.startTime = simTime;
128                 else
129                 {
130                     double deltaTime = simTime - (this.elapsedTime + this.startTime);
131                     if (deltaTime > 0.0)
132                     {
133                         super.sum += lastValue * deltaTime;
134                         super.varianceSum += lastValue * lastValue * deltaTime;
135                         this.elapsedTime = this.elapsedTime + deltaTime;
136                     }
137                 }
138                 this.lastValue = value;
139             }
140         }
141     }
142 
143     /**
144      * @see org.opentrafficsim.water.statistics.XTally#writeToExcel(org.apache.poi.ss.usermodel.Sheet, int)
145      */
146     @Override
147     public int writeToExcel(final Sheet sheet, final int startRow)
148     {
149         DataFormat format = sheet.getWorkbook().createDataFormat();
150         CellStyle style = sheet.getWorkbook().createCellStyle();
151         style.setDataFormat(format.getFormat("0.00"));
152 
153         int rownr = startRow;
154         Row row = sheet.createRow(rownr);
155 
156         row.createCell(1).setCellValue(description);
157         row.createCell(2).setCellValue("tijdgewogen [n, gem, stdev, min, max]");
158         row.createCell(3).setCellValue(getN());
159         if (getN() > 0)
160         {
161             row.createCell(4).setCellValue(getSampleMean());
162             row.getCell(4).setCellStyle(style);
163             if (getN() > 1 && !Double.isNaN(getStdDev()))
164             {
165                 row.createCell(5).setCellValue(getStdDev());
166                 row.getCell(5).setCellStyle(style);
167             }
168             row.createCell(6).setCellValue(getMin());
169             row.getCell(6).setCellStyle(style);
170             row.createCell(7).setCellValue(getMax());
171             row.getCell(7).setCellStyle(style);
172         }
173 
174         return rownr + 1;
175     }
176 
177 }