Statistics
| Branch: | Revision:

root / src / envir / resultrecorders.h @ 81ad8b66

History | View | Annotate | Download (6.13 KB)

1
//==========================================================================
2
//  RESULTRECORDERS.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17

    
18
#ifndef __ENVIR_RESULTRECORDERS_H
19
#define __ENVIR_RESULTRECORDERS_H
20

    
21
#include "envirdefs.h"
22
#include "resultrecorder.h"
23
#include "expression.h"
24
#include "cstatistic.h"
25

    
26

    
27
/**
28
 * Listener for recording a signal to an output vector
29
 */
30
class ENVIR_API VectorRecorder : public NumericResultRecorder
31
{
32
    protected:
33
        void *handle;        // identifies output vector for the output vector manager
34
        simtime_t lastTime;  // to ensure increasing timestamp order
35
    protected:
36
        virtual void collect(simtime_t_cref t, double value);
37
    public:
38
        VectorRecorder() {handle = NULL; lastTime = 0;}
39
        virtual void subscribedTo(ResultFilter *prev);
40
};
41

    
42
/**
43
 * Listener for recording the count of signal values. Signal values do not need
44
 * to be numeric to be counted.
45
 */
46
class ENVIR_API CountRecorder : public ResultRecorder
47
{
48
    protected:
49
        long count;
50
    public:
51
        CountRecorder() {count = 0;}
52
        virtual void receiveSignal(ResultFilter *prev, simtime_t_cref t, long l) {count++;}
53
        virtual void receiveSignal(ResultFilter *prev, simtime_t_cref t, unsigned long l) {count++;}
54
        virtual void receiveSignal(ResultFilter *prev, simtime_t_cref t, double d) {count++;}
55
        virtual void receiveSignal(ResultFilter *prev, simtime_t_cref t, const SimTime& v) {count++;}
56
        virtual void receiveSignal(ResultFilter *prev, simtime_t_cref t, const char *s) {count++;}
57
        virtual void receiveSignal(ResultFilter *prev, simtime_t_cref t, cObject *obj) {count++;}
58
        virtual void finish(ResultFilter *prev);
59
};
60

    
61
/**
62
 * Listener for recording the last signal value
63
 */
64
class ENVIR_API LastValueRecorder : public NumericResultRecorder
65
{
66
    protected:
67
        double lastValue;
68
    protected:
69
        virtual void collect(simtime_t_cref t, double value) {lastValue = value;}
70
    public:
71
        LastValueRecorder() {lastValue = NaN;}
72
        virtual void finish(ResultFilter *prev);
73
};
74

    
75
/**
76
 * Listener for recording the sum of signal values
77
 */
78
class ENVIR_API SumRecorder : public NumericResultRecorder
79
{
80
    protected:
81
        double sum;
82
    protected:
83
        virtual void collect(simtime_t_cref t, double value) {sum += value;}
84
    public:
85
        SumRecorder() {sum = 0;}
86
        virtual void finish(ResultFilter *prev);
87
};
88

    
89
/**
90
 * Listener for recording the mean of signal values
91
 */
92
class ENVIR_API MeanRecorder : public NumericResultRecorder
93
{
94
    protected:
95
        long count;
96
        double sum;
97
    protected:
98
        virtual void collect(simtime_t_cref t, double value) {count++; sum += value;}
99
    public:
100
        MeanRecorder() {count = 0; sum = 0;}
101
        virtual void finish(ResultFilter *prev);
102
};
103

    
104
/**
105
 * Listener for recording the minimum of signal values
106
 */
107
class ENVIR_API MinRecorder : public NumericResultRecorder
108
{
109
    protected:
110
        double min;
111
    protected:
112
        virtual void collect(simtime_t_cref t, double value) {if (value < min) min = value;}
113
    public:
114
        MinRecorder() {min = POSITIVE_INFINITY;}
115
        virtual void finish(ResultFilter *prev);
116
};
117

    
118
/**
119
 * Listener for recording the maximum of signal values
120
 */
121
class ENVIR_API MaxRecorder : public NumericResultRecorder
122
{
123
    protected:
124
        double max;
125
    protected:
126
        virtual void collect(simtime_t_cref t, double value) {if (value > max) max = value;}
127
    public:
128
        MaxRecorder() {max = NEGATIVE_INFINITY;}
129
        virtual void finish(ResultFilter *prev);
130
};
131

    
132
/**
133
 * Listener for recording the time average of signal values
134
 */
135
class ENVIR_API TimeAverageRecorder : public NumericResultRecorder
136
{
137
    protected:
138
        simtime_t startTime;
139
        simtime_t lastTime;
140
        double lastValue;
141
        double weightedSum;
142
    protected:
143
        virtual void collect(simtime_t_cref t, double value);
144
    public:
145
        TimeAverageRecorder() {startTime = lastTime = -1; lastValue = weightedSum = 0;}
146
        virtual void finish(ResultFilter *prev);
147
};
148

    
149
/**
150
 * Listener for recording signal values via a cStatistic
151
 */
152
class ENVIR_API StatisticsRecorder : public NumericResultRecorder, private cObject /*so it can own the statistic object*/
153
{
154
    protected:
155
        cStatistic *statistic;
156
    protected:
157
        virtual void collect(double value) {statistic->collect(value);}
158
        virtual void collect(simtime_t_cref t, double value) {statistic->collect(value);}
159
    public:
160
        StatisticsRecorder(cStatistic *stat) {statistic = stat; take(statistic);}
161
        ~StatisticsRecorder() {drop(statistic); delete statistic;}
162
        virtual void finish(ResultFilter *prev);
163
};
164

    
165
class ENVIR_API StatsRecorder : public StatisticsRecorder
166
{
167
    public:
168
        StatsRecorder();
169
};
170

    
171
class ENVIR_API HistogramRecorder : public StatisticsRecorder
172
{
173
    public:
174
        HistogramRecorder();
175
};
176

    
177
/**
178
 * Result recorder that remembers the last value of a signal,
179
 * and in finish() it evaluates an expression and saves the result.
180
 */
181
//TODO: make variants that eat 2 signals, and N signals
182
class ENVIR_API ExpressionRecorder : public NumericResultRecorder
183
{
184
    protected:
185
        Expression expr;
186
    public:
187
        // current values, valid inside process() only
188
        double lastValue;
189
    protected:
190
        virtual void collect(simtime_t_cref t, double value) {lastValue = value;}
191
    public:
192
        ExpressionRecorder() {lastValue=NaN;}
193
        virtual std::string str() const {return expr.str()+" (ExpressionRecorder)";}
194
        virtual Expression& getExpression() {return expr;}
195
        virtual Expression::Functor *makeValueVariable();
196
        virtual Expression::Functor *makeTimeVariable();
197
        virtual void finish(ResultFilter *prev);
198
};
199

    
200
#endif
201