Statistics
| Branch: | Revision:

root / src / scave / customfilter.h @ e1750c09

History | View | Annotate | Download (3.15 KB)

1 01873262 Georg Kunz
//=========================================================================
2
//  CUSTOMFILTER.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 _CUSTOMFILTER_H_
19
#define _CUSTOMFILTER_H_
20
21
#include "commonnodes.h"
22
#include "expression.h"
23
24
NAMESPACE_BEGIN
25
26
27
/**
28
 * Processing node which evaluates an arbitrary expression entered
29
 * by the user.
30
 *
31
 * TODO:
32
 *  - support parameters: $windowsize or $(windowsize) in the expression
33
 *    would prompt user to enter a value for windowsize;
34
 *  - extend the expression syntax into a mini imperative language to allow
35
 *    for writing more powerful filters: introduce variables and assigment operator
36
 *    (=, +=, *=, ++, --, etc); allow several expressions separated by semicolon;
37
 *    implement 'for', 'while', 'if'; filternode to keep a variables table;
38
 *    "init-expression" attr where user could initialize variables;
39
 *    possibly implement array variables as well.
40
 */
41
class SCAVE_API ExpressionFilterNode : public FilterNode
42
{
43
    public:
44
        /**
45
         * Implements a variable in the expression. Currently just delegates
46
         * to ExpressionFilterNode::getVariable().
47
         */
48
        class NodeVar : public Expression::Variable
49
        {
50
          private:
51
            ExpressionFilterNode *hostnode;
52
            std::string varname;
53
          public:
54
            NodeVar(ExpressionFilterNode *node, const char *name)
55
                {hostnode = node; varname = name; hostnode->skipFirstDatum |= (varname=="tprev" || varname=="yprev"); }
56
            virtual ~NodeVar() {}
57
            virtual Expression::Functor *dup() const {return new NodeVar(hostnode, varname.c_str());}
58
            virtual const char *getName() const {return varname.c_str();}
59
            virtual char getReturnType() const {return Expression::Value::DBL;}
60
            virtual Expression::Value evaluate(Expression::Value args[], int numargs)
61
                {return hostnode->getVariable(varname.c_str());}
62
        };
63
    private:
64
        Expression *expr;
65
        Datum currentDatum, prevDatum;
66
        bool skipFirstDatum;
67
    public:
68
        ExpressionFilterNode(const char *expression);
69
        virtual ~ExpressionFilterNode();
70
        virtual bool isReady() const;
71
        virtual void process();
72
        double getVariable(const char *varname);
73
};
74
75
class SCAVE_API ExpressionFilterNodeType : public FilterNodeType
76
{
77
    public:
78
        virtual const char *getName() const {return "expression";}
79
        virtual const char *getDescription() const;
80
        virtual void getAttributes(StringMap& attrs) const;
81
        virtual Node *create(DataflowManager *mgr, StringMap& attrs) const;
82
        virtual void mapVectorAttributes(/*inout*/StringMap &attrs) const;
83
};
84
85
NAMESPACE_END
86
87
88
#endif
89