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  
18  
19  
20  
21  
22  
23  
24  
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      
38      @XStreamOmitField
39      private double startTime = Double.NaN;
40  
41      
42      private double elapsedTime = Double.NaN;
43  
44      
45      @XStreamOmitField
46      private double lastValue = Double.NaN;
47  
48      
49  
50  
51  
52      public XPersistent(String description, DEVSSimulatorInterface.TimeDoubleUnit simulator)
53      {
54          super(description);
55          this.simulator = simulator;
56      }
57  
58      
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      
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      
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      
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 
110 
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 
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 }