source: orange/source/orange/vars.hpp @ 7667:460a67ca7a5a

Revision 7667:460a67ca7a5a, 7.9 KB checked in by markotoplak, 3 years ago (diff)

Replaced allVariables (a list) with allVariablesMap.

Line 
1/*
2    This file is part of Orange.
3   
4    Copyright 1996-2010 Faculty of Computer and Information Science, University of Ljubljana
5    Contact: janez.demsar@fri.uni-lj.si
6
7    Orange is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    Orange is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Orange.  If not, see <http://www.gnu.org/licenses/>.
19*/
20
21
22#ifndef __VARS_HPP
23#define __VARS_HPP
24
25#include <string>
26#include <list>
27#include <set>
28#include <map>
29#include "orvector.hpp"
30
31using namespace std;
32
33#include "root.hpp"
34
35class ORANGE_API TValue;
36class ORANGE_API TExample;
37class ORANGE_API TTransformValue;
38class ORANGE_API TDistribution;
39
40WRAPPER(Domain);
41WRAPPER(ExampleGenerator)
42WRAPPER(Classifier);
43WRAPPER(RandomGenerator)
44
45//#include "getarg.hpp"
46#include "stladdon.hpp"
47
48#ifdef _MSC_VER
49 #pragma warning (disable : 4786 4114 4018 4267 4251)
50#endif
51
52WRAPPER(Variable);
53WRAPPER(Classifier)
54
55
56class ORANGE_API TVariable : public TOrange {
57public:
58  __REGISTER_ABSTRACT_CLASS
59
60  CLASSCONSTANTS(Type: None=(int)TValue::NONE; Discrete=(int)TValue::INTVAR; Continuous=(int)TValue::FLOATVAR; Other=(int)(TValue::FLOATVAR+1); String=(int)STRINGVAR);
61 
62  //string name; //variable's name - hiding it!
63
64  int  varType; //P(&Variable_Type) variable type
65  bool ordered; //P variable values are ordered
66  bool distributed; //P variable values are distributions
67  int defaultMetaId; //P default (proposed, suggested...) meta id for this variable
68
69  PRandomGenerator randomGenerator; //P random generator for random values (initialized when first needed)
70
71  PVariable sourceVariable; //P The attribute that this attribute is computed from (when applicable)
72  PClassifier getValueFrom; //P Function to compute the value from values of other variables
73  bool getValueFromLocked;
74
75protected:
76  TValue DC_value;
77  TValue DK_value;
78
79private:
80  string name;
81
82  void registerVariable();
83  void removeVariable();
84
85public:
86
87  string get_name() const {
88    return name;
89  }
90
91  void set_name(const string &a) {
92    //update the map
93    removeVariable();
94    name = a;
95    registerVariable();
96  }
97
98  static multimap<string, TVariable *> allVariablesMap;
99
100  void operator delete(void *t);
101
102  /* Status codes for getExisting and make. The codes refer to the difference between
103     the requested and the existing variable.
104     OK                  the new variable contains at least one of the existing values,
105                         and no new values; there is no problem with their order
106     MissingValues       the new variable contains at least one of the existing values,
107                         and some new oness; there is no problem with their order
108     NoRecognizedValues  the new variable contains no existing values
109     Incompatible        the new variable prescribes an order of values which is
110                         incompatible with the existing
111     NotFound            the variable with that name and type doesn't exist yet
112  */
113  CLASSCONSTANTS(MakeStatus) enum { OK, MissingValues, NoRecognizedValues, Incompatible, NotFound };
114 
115  /* This will search for an existing variable and return it unless the status (above)
116     equals or exceeds the failOn argument, Incompatible or NotFound.
117     The status is return if status!=NULL */
118  static TVariable *getExisting(const string &name, const int &varType, TStringList *fixedOrderValues = NULL, set<string> *values = NULL,
119                                const int failOn = Incompatible, int *status = NULL);
120                               
121  /* Gets an existing variable or makes a new one. A new one is made if there is no
122     existing variable by that name or its status (above) equals or exceeds createNewOne.
123     The returned status equals to the result of the search for an existing variable,
124     except if createNewOn==OK, in which case status is always OK.  */
125  static TVariable *make(const string &name, const int &varType, TStringList *fixedOrderValues = NULL, set<string> *value = NULL,
126                                const int createNewOn = Incompatible, int *status = NULL);
127                 
128  virtual bool isEquivalentTo(const TVariable &old) const;
129 
130  TVariable(const int &avarType = TValue::NONE, const bool &ordered = false);
131  TVariable(const string &aname, const int &avarType=TValue::NONE, const bool &ordered = false);
132
133  virtual const TValue &DC() const;
134  virtual const TValue &DK() const;
135  virtual TValue specialValue(int) const;
136
137  bool str2special(const string &valname, TValue &valu) const;  // Those two functions convert ? to DK, ~ to DC and vice-versa
138  bool special2str(const TValue &val, string &str) const;
139
140  virtual void val2str(const TValue &val, string &str) const =0;
141  virtual void str2val(const string &valname, TValue &valu) =0;
142  virtual bool str2val_try(const string &valname, TValue &valu);
143  virtual void str2val_add(const string &valname, TValue &valu);
144
145  virtual void val2filestr(const TValue &val, string &str, const TExample &) const;
146  virtual void filestr2val(const string &valname, TValue &valu, TExample &);
147
148  virtual bool   firstValue(TValue &val) const;
149  virtual bool   nextValue(TValue &val) const;
150  virtual TValue randomValue(const int &rand=-1);
151
152  // Returns the number of different values, -1 if it cannot be done (for example, if variable is continuous)
153  virtual int  noOfValues() const =0;
154
155  virtual TValue computeValue(const TExample &);
156};
157
158WRAPPER(Variable)
159
160#define TVarList TOrangeVector<PVariable>
161VWRAPPER(VarList)
162
163#define TVarListList TOrangeVector<PVarList>
164VWRAPPER(VarListList)
165
166class ORANGE_API TEnumVariable : public TVariable {
167public:
168  __REGISTER_CLASS
169
170  PStringList values; //P attribute's values
171  int baseValue;      //P the index of the base value
172
173  TEnumVariable();
174  TEnumVariable(const string &aname);
175  TEnumVariable(const string &aname, PStringList val);
176  TEnumVariable(const TEnumVariable &);
177
178  virtual bool isEquivalentTo(const TVariable &old) const;
179
180  void addValue(const string &);
181  bool hasValue(const string &);
182
183  virtual bool   firstValue(TValue &val) const;
184  virtual bool   nextValue(TValue &val) const;
185  virtual TValue randomValue(const int &rand=-1);
186
187  virtual int  noOfValues() const;
188
189  virtual void val2str(const TValue &val, string &str) const;
190  virtual void str2val(const string &valname, TValue &valu);
191  virtual bool str2val_try(const string &valname, TValue &valu);
192  virtual void str2val_add(const string &valname, TValue &valu);
193
194  bool checkValuesOrder(const TStringList &refValues);
195  static void presortValues(const set<string> &unsorted, vector<string> &sorted);
196
197private:
198  map<string, int> valuesTree;
199  void createValuesTree();
200};
201
202
203
204
205class ORANGE_API TFloatVariable : public TVariable {
206public:
207  __REGISTER_CLASS
208
209  float startValue; //P lowest value
210  float endValue;   //P highest value
211  float stepValue;  //P step
212 
213  int numberOfDecimals; //P number of digits after decimal point
214  bool scientificFormat; //P use scientific format in output
215  int adjustDecimals; //P adjust number of decimals according to the values converted (0 - no, 1 - yes, 2 - yes, but haven't seen any yet)
216
217  TFloatVariable();
218  TFloatVariable(const string &aname);
219
220  virtual bool isEquivalentTo(const TVariable &old) const;
221
222  virtual bool   firstValue(TValue &val) const;
223  virtual bool   nextValue(TValue &val) const;
224  virtual TValue randomValue(const int &rand=-1);
225
226  virtual int  noOfValues() const;
227 
228  virtual void val2str(const TValue &val, string &str) const;
229  virtual void str2val(const string &valname, TValue &valu);
230  virtual bool str2val_try(const string &valname, TValue &valu);
231  int str2val_low(const string &valname, TValue &valu);
232};
233
234#endif
Note: See TracBrowser for help on using the repository browser.