Statistics
| Branch: | Revision:

root / src / envir / scenario.h @ master

History | View | Annotate | Download (3.64 KB)

1
//==========================================================================
2
//  SCENARIO.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 SCENARIO_H_
19
#define SCENARIO_H_
20

    
21
#include <map>
22
#include <vector>
23
#include <set>
24
#include <string>
25
#include "envirdefs.h"
26
#include "expression.h"
27
#include "sectionbasedconfig.h"
28
#include "valueiterator.h"
29

    
30
NAMESPACE_BEGIN
31

    
32

    
33
/**
34
 * Helper class for SectionBasedConfiguration: Resolves iterations in a scenario.
35
 * May be used as an iterator as well.
36
 */
37
class ENVIR_API Scenario
38
{
39
  public:
40
    typedef SectionBasedConfiguration::IterationVariable IterationVariable;
41

    
42
  private:
43
    // the input
44
    const std::vector<IterationVariable>& vars;
45
    Expression *constraint;
46

    
47
    // the iteration variables, named (${x=1..5}) and unnamed (${1..5})
48
    std::vector<ValueIterator> variterators; // indices correspond to vars[]
49
    std::map<std::string, ValueIterator*> varmap; // varid-to-iterator
50

    
51
  private:
52
    bool inc();
53
    bool evaluateConstraint();
54
    Expression::Value getIterationVariable(const char *varname);
55

    
56
  public:
57
    /**
58
     * Used in resolving and generating scenarios, it implements a $x iteration variable.
59
     * Currently just delegates to SectionBasedConfiguration::getIterationVariable().
60
     */
61
    class VariableReference : public Expression::Variable
62
    {
63
      private:
64
        Scenario *hostnode;
65
        std::string varname;
66
      public:
67
        VariableReference(Scenario *node, const char *name) {hostnode = node; varname = name;}
68
        virtual ~VariableReference() {}
69
        virtual Expression::Functor *dup() const {return new VariableReference(hostnode, varname.c_str());}
70
        virtual const char *getName() const {return varname.c_str();}
71
        virtual char getReturnType() const {return Expression::Value::DBL;}
72
        virtual Expression::Value evaluate(Expression::Value args[], int numargs)
73
            {return hostnode->getIterationVariable(varname.c_str());}
74
    };
75

    
76
  public:
77
    Scenario(const std::vector<IterationVariable>& iterationVariables, const char *constraint);
78
    ~Scenario();
79

    
80
    /**
81
     * Returns the constructor parameter, unchanged.
82
     */
83
    const std::vector<IterationVariable>& getIterationVariables() const {return vars;}
84

    
85
    /**
86
     * Counts the number of runs this scenario generates.
87
     *
88
     * The current iteration state is NOT preserved.
89
     */
90
    int getNumRuns();
91

    
92
    /**
93
     * Spins the iteration variables to the given run.
94
     */
95
    void gotoRun(int runNumber);
96

    
97
    /**
98
     * Restarts the iteration. Returns false if there is no valid config at all,
99
     * that is, get() and next() may not be called.
100
     */
101
    bool restart();
102

    
103
    /**
104
     * Advances to the next valid run. When it returns false, that means the
105
     * the iteration finished, and get() and next() may not be invoked any more.
106
     */
107
    bool next();
108

    
109
    /**
110
     * XXX
111
     */
112
    std::string getVariable(const char *varid) const;
113

    
114
    /**
115
     * XXX
116
     */
117
    int getIteratorPosition(const char *varid) const;
118

    
119
    /**
120
     * Returns the current iteration state as a string ("$x=100, $y=3, $2=.4").
121
     * Used for the value of ${iterationvars} as well.
122
     */
123
    std::string str() const;
124
};
125

    
126
NAMESPACE_END
127

    
128

    
129
#endif
130

    
131