Project

General

Profile

Statistics
| Branch: | Revision:

root / src / envir / resultfilters.cc @ e26d3d25

History | View | Annotate | Download (4.84 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  RESULTFILTERS.CC - 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
#include "resultfilters.h"
19
#include "cmessage.h"  // PacketBytesFilter
20
21
22
// note: we don't register WarmupPeriodFilter and ExpressionFilter
23
Register_ResultFilter("count", CountFilter);
24
Register_ResultFilter("constant0", Constant0Filter);
25
Register_ResultFilter("constant1", Constant1Filter);
26
Register_ResultFilter("last", LastValueFilter);
27
Register_ResultFilter("sum", SumFilter);
28
Register_ResultFilter("mean", MeanFilter);
29
Register_ResultFilter("min", MinFilter);
30
Register_ResultFilter("max", MaxFilter);
31
Register_ResultFilter("timeavg", TimeAverageFilter);
32
Register_ResultFilter("packetBytes", PacketBytesFilter);
33
Register_ResultFilter("packetBits", PacketBitsFilter);
34
Register_ResultFilter("sumPerDuration", SumPerDurationFilter);
35
36
37
void WarmupPeriodFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, long l)
38
{
39
    if (t >= getEndWarmupPeriod())
40
        fire(this, t, l);
41
}
42
43
void WarmupPeriodFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, unsigned long l)
44
{
45
    if (t >= getEndWarmupPeriod())
46
        fire(this, t, l);
47
}
48
49
void WarmupPeriodFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, double d)
50
{
51
    if (t >= getEndWarmupPeriod())
52
        fire(this, t, d);
53
}
54
55
void WarmupPeriodFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, const SimTime& v)
56
{
57
    if (t >= getEndWarmupPeriod())
58
        fire(this, t, v);
59
}
60
61
void WarmupPeriodFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, const char *s)
62
{
63
    if (t >= getEndWarmupPeriod())
64
        fire(this, t, s);
65
}
66
67
void WarmupPeriodFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, cObject *obj)
68
{
69
    // note: cITimestampedValue stuff was already dispatched to (simtime_t,double) method in base class
70
    if (t >= getEndWarmupPeriod())
71
        fire(this, t, obj);
72
}
73
74
//---
75
76
bool TimeAverageFilter::process(simtime_t& t, double& value)
77
{
78
    bool isFirstValue = startTime < SIMTIME_ZERO;
79
    if (isFirstValue)
80
        startTime = t;
81
    else
82
        weightedSum += lastValue * SIMTIME_DBL(t - lastTime);  // always forward-sample-hold
83
    lastTime = t;
84
    lastValue = value;
85
86
    if (isFirstValue || t==startTime)
87
        return false;  // suppress initial 0/0 = NaNs
88
89
    double interval = SIMTIME_DBL(t - startTime);
90
    value = weightedSum / interval;
91
    return true;
92
}
93
94
//---
95
96
class ValueVariable : public Expression::Variable
97
{
98
  private:
99
    ExpressionFilter *owner;
100
  public:
101
    ValueVariable(ExpressionFilter *filter) {owner = filter;}
102
    virtual Functor *dup() const {return new ValueVariable(owner);}
103
    virtual const char *getName() const {return "<signalvalue>";}
104
    virtual char getReturnType() const {return Expression::Value::DBL;}
105
    virtual Expression::Value evaluate(Expression::Value args[], int numargs) {return owner->currentValue;}
106
};
107
108
//XXX currently unused
109
class TimeVariable : public Expression::Variable
110
{
111
  private:
112
    ExpressionFilter *owner;
113
  public:
114
    TimeVariable(ExpressionFilter *filter) {owner = filter;}
115
    virtual Functor *dup() const {return new TimeVariable(owner);}
116
    virtual const char *getName() const {return "<signaltime>";}
117
    virtual char getReturnType() const {return Expression::Value::DBL;}
118
    virtual Expression::Value evaluate(Expression::Value args[], int numargs) {return SIMTIME_DBL(owner->currentTime);}
119
};
120
121
bool ExpressionFilter::process(simtime_t& t, double& value)
122
{
123
    currentTime = t;
124
    currentValue = value;
125
    value = expr.doubleValue();
126
    return true;
127
}
128
129
Expression::Functor *ExpressionFilter::makeValueVariable()
130
{
131
    return new ValueVariable(this);
132
}
133
134
Expression::Functor *ExpressionFilter::makeTimeVariable()
135
{
136
    return new TimeVariable(this);
137
}
138
139
//---
140
141
void PacketBytesFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, cObject *object)
142
{
143
    if (dynamic_cast<cPacket *>(object))
144
    {
145
        cPacket *pk = (cPacket *)object;
146
        fire(this, t, (double)pk->getByteLength());
147
    }
148
}
149
150
//---
151
152
void PacketBitsFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, cObject *object)
153
{
154
    if (dynamic_cast<cPacket *>(object))
155
    {
156
        cPacket *pk = (cPacket *)object;
157
        fire(this, t, (double)pk->getBitLength());
158
    }
159
}
160
161
//---
162
163
bool SumPerDurationFilter::process(simtime_t& t, double& value)
164
{
165
    sum += value;
166
    value = sum / (simTime() - simulation.getWarmupPeriod());
167
    return true;
168
}