Project

General

Profile

Statistics
| Branch: | Revision:

root / src / scave / fields.h @ a3be1d55

History | View | Annotate | Download (8.27 KB)

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

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

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

    
17
#ifndef _FIELDS_H_
18
#define _FIELDS_H_
19

    
20
#include <functional>
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <assert.h>
25
#include "resultfilemanager.h"
26
#include "commonutil.h"
27
#include "patternmatcher.h"
28
#include "stringutil.h"
29
#include "scaveutils.h"
30

    
31
NAMESPACE_BEGIN
32

    
33

    
34
class SCAVE_API ResultItemField
35
{
36
    public:
37
        static const char * const FILE;
38
        static const char * const RUN;
39
        static const char * const MODULE;
40
        static const char * const NAME;
41

    
42
        static const int FILE_ID        = 1;
43
        static const int RUN_ID         = 2;
44
        static const int MODULE_ID      = 3;
45
        static const int NAME_ID        = 4;
46
        static const int ATTR_ID        = 5;
47
        static const int RUN_ATTR_ID    = 6;
48
        static const int RUN_PARAM_ID   = 7;
49
    private:
50
        int id;
51
        std::string name; // constant names above + attribute/param names
52
        static int getFieldID(const std::string fieldName);
53
    public:
54
        ResultItemField(const std::string fieldName);
55
        int getID() const { return id; }
56
        const std::string getName() const { return name; };
57
        std::string getFieldValue(const ResultItem &d) const ;
58
        bool equal(const ResultItem &d1, const ResultItem &d2) const;
59
        int compare(const ResultItem &d1, const ResultItem &d2) const;
60
};
61

    
62
class SCAVE_API ResultItemAttribute
63
{
64
    public:
65
        static const char * const TYPE;
66
        static const char * const ENUM;
67

    
68
        static StringVector getAttributeNames();
69
        static bool isAttributeName(const std::string name);
70
};
71

    
72

    
73
class SCAVE_API RunAttribute
74
{
75
    public:
76
        static const char * const INIFILE;
77
        static const char * const CONFIGNAME;
78
        static const char * const RUNNUMBER;
79
        static const char * const NETWORK;
80
        static const char * const EXPERIMENT;
81
        static const char * const MEASUREMENT;
82
        static const char * const REPLICATION;
83
        static const char * const DATETIME;
84
        static const char * const PROCESSID;
85
        static const char * const RESULTDIR;
86
        static const char * const REPETITION;
87
        static const char * const SEEDSET;
88
        static const char * const ITERATIONVARS;
89
        static const char * const ITERATIONVARS2;
90

    
91
        static StringVector getAttributeNames();
92
        static bool isAttributeName(const std::string name);
93
};
94

    
95
inline const char *getAttribute(const ResultItem &d, const std::string attrName)
96
{
97
    const char *value = d.getAttribute(attrName.c_str());
98
    return value ? value : "";
99
}
100

    
101
inline const char *getRunAttribute(const ResultItem &d, const std::string attrName)
102
{
103
    const char *value = d.fileRunRef->runRef->getAttribute(attrName.c_str());
104
    return value ? value : "";
105
}
106

    
107
inline const char *getRunParam(const ResultItem &d, const std::string paramName)
108
{
109
    const char *value = d.fileRunRef->runRef->getModuleParam(paramName.c_str());
110
    return value ? value : "";
111
}
112

    
113
inline std::string ResultItemField::getFieldValue(const ResultItem &d) const
114
{
115
    switch(id)
116
    {
117
    case FILE_ID:       return d.fileRunRef->fileRef->filePath;
118
    case RUN_ID:        return d.fileRunRef->runRef->runName;
119
    case MODULE_ID:     return *d.moduleNameRef;
120
    case NAME_ID:       return *d.nameRef;
121
    case ATTR_ID:       return getAttribute(d, name);
122
    case RUN_ATTR_ID:   return getRunAttribute(d, name);
123
    case RUN_PARAM_ID:  return getRunParam(d, name);
124
    default:            return "";
125
    }
126
}
127

    
128
inline int strcmpFIXME(const char *str1, const char *str2)
129
{
130
    return strcmp(str1, str2);
131
}
132

    
133

    
134
inline bool ResultItemField::equal(const ResultItem &d1, const ResultItem &d2) const
135
{
136
    switch (id)
137
    {
138
    case FILE_ID:       return d1.fileRunRef->fileRef == d2.fileRunRef->fileRef;
139
    case RUN_ID:        return d1.fileRunRef->runRef == d2.fileRunRef->runRef;
140
    case MODULE_ID:     return d1.moduleNameRef == d2.moduleNameRef;
141
    case NAME_ID:       return d1.nameRef == d2.nameRef;
142
                        // KLUDGE using strcmp() here causes an INTERNAL COMPILER ERROR with MSVC71, i don't know why
143
    case ATTR_ID:       return strcmpFIXME(getAttribute(d1, name), getAttribute(d2, name)) == 0;
144
    case RUN_ATTR_ID:   return strcmpFIXME(getRunAttribute(d1, name), getRunAttribute(d2, name)) == 0;
145
    case RUN_PARAM_ID:  return strcmpFIXME(getRunParam(d1, name), getRunParam(d2, name)) == 0;
146
    }
147
    // not reached
148
    return true;
149
}
150

    
151
inline int ResultItemField::compare(const ResultItem &d1, const ResultItem &d2) const
152
{
153
    switch (id)
154
    {
155
    case FILE_ID:
156
        return strdictcmp(d1.fileRunRef->fileRef->filePath.c_str(),
157
                          d2.fileRunRef->fileRef->filePath.c_str());
158
    case RUN_ID:
159
        return strdictcmp(d1.fileRunRef->runRef->runName.c_str(),
160
                          d2.fileRunRef->runRef->runName.c_str());
161
    case MODULE_ID:
162
        return strdictcmp(d1.moduleNameRef->c_str(),
163
                          d2.moduleNameRef->c_str());
164
    case NAME_ID:
165
        return strdictcmp(d1.nameRef->c_str(), d2.nameRef->c_str());
166
    case ATTR_ID:
167
        return strdictcmp(getAttribute(d1, name), getAttribute(d2, name));
168
    case RUN_ATTR_ID:
169
        return strdictcmp(getRunAttribute(d1, name), getRunAttribute(d2, name));
170
    case RUN_PARAM_ID:
171
        return strdictcmp(getRunParam(d1, name), getRunParam(d2, name));
172
    default:
173
        return false;
174
    }
175
}
176

    
177
class SCAVE_API ResultItemFields
178
{
179
    private:
180
        std::vector<ResultItemField> fields;
181
    public:
182
        typedef std::vector<ResultItemField>::const_iterator const_iterator;
183

    
184
        static StringVector getFieldNames();
185

    
186
        ResultItemFields() {};
187
        ResultItemFields(ResultItemField field);
188
        ResultItemFields(const std::string fieldName);
189
        ResultItemFields(const StringVector &fieldNames);
190

    
191
        ResultItemFields complement() const;
192
        bool hasField(ResultItemField field) const;
193
        bool hasField(const std::string fieldName) const;
194
        const_iterator begin() const { return fields.begin(); };
195
        const_iterator end() const { return fields.end(); };
196
        bool less(ID id1, ID id2, ResultFileManager *manager) const;
197
        bool less(const ResultItem &d1, const ResultItem &d2) const;
198
        bool equal(ID id1, ID id2, ResultFileManager *manager) const;
199
        bool equal(const ResultItem& d1, const ResultItem& d2) const;
200
};
201

    
202
#ifndef SWIG
203
/*
204
 * Equality test of ResultItems that compares the specified fields.
205
 */
206
struct ResultItemFieldsEqual : public std::binary_function<ResultItem, ResultItem, bool>
207
{
208
    ResultItemFields fields;
209
    ResultItemFieldsEqual(const ResultItemFields fields) : fields(fields) {}
210
    bool operator()(const ResultItem &d1, const ResultItem &d2) const { return fields.equal(d1, d2); }
211
};
212

    
213
struct IDFieldsEqual : public std::binary_function<ID,ID,bool>
214
{
215
    ResultItemFields fields;
216
    ResultFileManager *manager;
217
    IDFieldsEqual(ResultItemFields fields, ResultFileManager *manager)
218
        : fields(fields), manager(manager) {}
219
    bool operator()(ID id1, ID id2) const { return fields.equal(id1, id2, manager); }
220
};
221

    
222
/*
223
 * Comparator of ResultItems that compares the specified fields.
224
 */
225
struct ResultItemFieldsLess : public std::binary_function<ResultItem, ResultItem, bool>
226
{
227
    ResultItemFields fields;
228
    ResultItemFieldsLess() {}
229
    ResultItemFieldsLess(const ResultItemFields fields) : fields(fields) {}
230
    bool operator()(const ResultItem &d1, const ResultItem &d2) const { return fields.less(d1, d2); }
231
};
232

    
233
struct IDFieldsLess : public std::binary_function<ID,ID,bool>
234
{
235
    ResultItemFields fields;
236
    ResultFileManager *manager;
237
    IDFieldsLess(ResultItemFields fields, ResultFileManager *manager)
238
        : fields(fields), manager(manager) {}
239
    bool operator()(ID id1, ID id2) const { return fields.less(id1, id2, manager); }
240
};
241
#endif
242

    
243
NAMESPACE_END
244

    
245

    
246
#endif
247