Source

orange / source / orange / domaindepot.cpp

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
    This file is part of Orange.
    
    Copyright 1996-2010 Faculty of Computer and Information Science, University of Ljubljana
    Contact: janez.demsar@fri.uni-lj.si

    Orange is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Orange is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Orange.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "vars.hpp"
#include "domain.hpp"
#include "getarg.hpp"

#include "stringvars.hpp"

#include "domaindepot.hpp"


TDomainDepot domainDepot;

extern TOrangeType PyOrVariable_Type;
extern TOrangeType PyOrPythonVariable_Type;
#include "pythonvars.hpp"
#include "c2py.hpp"

typedef vector<pair<string, PyObject *> > TPythonVariablesRegistry;

TPythonVariablesRegistry pythonVariables;

void registerVariableType(PyObject *variable)
{
  const char *varname;

  if (!PyType_IsSubtype((PyTypeObject *)variable, (PyTypeObject *)&PyOrPythonVariable_Type))
    raiseErrorWho("registerVariableType", "variable type must be derived from PythonVariable");

  PyObject *pyclassname = PyObject_GetAttrString(variable, "__name__");
  if (!pyclassname)
    raiseErrorWho("registerVariableType", "variable type misses the '__name__'");
  varname = PyString_AsString(pyclassname);

  TPythonVariablesRegistry::iterator bi(pythonVariables.begin()), be(pythonVariables.end());
  for(; (bi != be) && ((*bi).first != varname); bi++);

  Py_INCREF(variable);
  if (bi==be)
    pythonVariables.push_back(make_pair(string(varname), variable));
  else {
    Py_DECREF((*bi).second);
    (*bi).second = variable;
  }

  Py_DECREF(pyclassname);
}

void pythonVariables_unsafeInitializion()
{
  pythonVariables.push_back(make_pair(string("PythonVariable"), (PyObject *)&PyOrPythonVariable_Type));
}

bool pythonDeclarationMatches(const string &declaration, PVariable var)
{
  PyObject *classname = PyObject_GetAttrString((PyObject *)(var.counter), "__class__");
  PyObject *typenamee = PyObject_GetAttrString(classname, "__name__");
  bool res = !strcmp(PyString_AsString(typenamee), (declaration.size()>6) ? declaration.c_str()+7 : "PythonVariable");
  Py_DECREF(classname);
  Py_DECREF(typenamee);
  return res;
}



PDomain combineDomains(PDomainList sources, TDomainMultiMapping &mapping)
{
  PVariable classVar;
  // I would use reverse iterators, but don't have them
  TDomainList::const_iterator cri(sources->end()), cre(sources->begin());
  while(!(*--cri)->classVar && (cri!=cre));
  classVar = (*cri)->classVar; // might have stopped at the classvar and reached cre which has none...
      
  TVarList variables;

  mapping.clear();
  vector<pair<int, int> > classMapping;

  int domainIdx = 0;
  TDomainList::const_iterator di(sources->begin()), de(sources->end());
  for(; di!=de; di++, domainIdx++) {

    int varIdx = 0;
    TVarList::const_iterator vi((*di)->variables->begin()), ve((*di)->variables->end());
    for(; vi!=ve; vi++, varIdx++) {
      if (*vi == classVar)
        classMapping.push_back(make_pair(domainIdx, varIdx));
      else {
        TDomainMultiMapping::iterator dmmi(mapping.begin());
        TVarList::const_iterator hvi(variables.begin()), hve(variables.end());
        for(; (hvi!=hve) && (*hvi != *vi); hvi++, dmmi++);
        if (hvi==hve) {
          variables.push_back(*vi);
          mapping.push_back(vector<pair<int, int> >());
          mapping.back().push_back(make_pair(domainIdx, varIdx));
        }
        else
          (*dmmi).push_back(make_pair(domainIdx, varIdx));
      }
    }
  }

  if (classVar)
    mapping.push_back(classMapping);

  TDomain *newDomain = mlnew TDomain(classVar, variables);
  PDomain wdomain = newDomain;

  for(domainIdx = 0, di = sources->begin(); di!=de; domainIdx++, di++)
    const_ITERATE(TMetaVector, mi, (*di)->metas) {
      PVariable metavar = newDomain->getMetaVar((*mi).id, false);
      if (!metavar)
        newDomain->metas.push_back(*mi);
      else
        if (metavar != (*mi).variable)
          raiseError("Id %i represents two different attributes ('%s' and '%s')", (*mi).id, metavar->get_name().c_str(), (*mi).variable->get_name().c_str());
    }

  return wdomain;
}


void computeMapping(PDomain destination, PDomainList sources, TDomainMultiMapping &mapping)
{
  mapping.clear();
  const_PITERATE(TVarList, vi, destination->variables) {
    mapping.push_back(vector<pair<int, int> >());
    int domainIdx = 0;
    TDomainList::const_iterator si(sources->begin()), se(sources->end());
    for(; si!=se; si++, domainIdx++) {
      int pos = (*si)->getVarNum(*vi, false);
      if (pos != ILLEGAL_INT)
        mapping.back().push_back(make_pair(domainIdx, pos));
    }
  }
}



TDomainDepot::TAttributeDescription::TAttributeDescription(const string &n, const int &vt, const string &td, bool ord)
: name(n),
  varType(vt),
  typeDeclaration(td),
  ordered(ord)
{}


TDomainDepot::TAttributeDescription::TAttributeDescription(const string &n, const int &vt)
: name(n),
  varType(vt),
  typeDeclaration(),
  ordered(false)
{}


TDomainDepot::TAttributeDescription::TAttributeDescription(PVariable pvar)
: preparedVar(pvar)
{}


void TDomainDepot::TAttributeDescription::addValue(const string &s)
{
  fixedOrderValues.push_back(s);
  values[s] = 1;
}

TDomainDepot::~TDomainDepot()
{
  ITERATE(list<TDomain *>, di, knownDomains) {
    // this could be done by some remove_if, but I don't want to fight
    //   all various implementations of STL
    list<TDomain::TDestroyNotification>::iterator src((*di)->destroyNotifiers.begin()), end((*di)->destroyNotifiers.end());
    for(; (src!=end) && ((const TDomainDepot *)((*src).second) != this); src++);
    (*di)->destroyNotifiers.erase(src);
  }
}


void TDomainDepot::destroyNotifier(TDomain *domain, void *data)
{ 
  ((TDomainDepot *)(data))->knownDomains.remove(domain);
}


 
inline bool checkValueOrder(PVariable var, const TDomainDepot::TAttributeDescription &desc)
{
  return    desc.varType != TValue::INTVAR
         || dynamic_cast<TEnumVariable &>(var.getReference()).checkValuesOrder(desc.fixedOrderValues);
}

void augmentVariableValues(PVariable var, const TDomainDepot::TAttributeDescription &desc)
{
  if (desc.varType != TValue::INTVAR)
    return;
    
  TEnumVariable &evar = dynamic_cast<TEnumVariable &>(var.getReference());
  const_ITERATE(TStringList, fvi, desc.fixedOrderValues)
    evar.addValue(*fvi);
  vector<string> sorted;
  set<string> values;
  for(map<string, int>::const_iterator dvi(desc.values.begin()), dve(desc.values.end()); dvi != dve; dvi++)
    values.insert(values.end(), dvi->first);
  TEnumVariable::presortValues(values, sorted);
  const_ITERATE(vector<string>, ssi, sorted)
    evar.addValue(*ssi);
}


bool TDomainDepot::checkDomain(const TDomain *domain, 
                               const TPAttributeDescriptions *attributes, bool hasClass,
                               const TPAttributeDescriptions *metas,
                               int *metaIDs)
{
  // check the number of attributes and meta attributes, and the presence of class attribute
  if (    (domain->variables->size() != attributes->size())
       || (bool(domain->classVar) != hasClass)
       || (metas ? (metas->size() != domain->metas.size()) : domain->metas.size() )
     )
    return false;

  // check the names and types of attributes
  TVarList::const_iterator vi(domain->variables->begin());
  TPAttributeDescriptions::const_iterator ai(attributes->begin()), ae(attributes->end());
  for(; ai != ae; ai++, vi++)
    if (    ((**ai).name != (*vi)->get_name())
         || ((**ai).varType>0) && ((**ai).varType != (*vi)->varType)
         || (((**ai).varType==PYTHONVAR) && !pythonDeclarationMatches((**ai).typeDeclaration, *vi))
         || !checkValueOrder(*vi, **ai)
       )
      return false;

  // check the meta attributes if they exist
  TPAttributeDescriptions::const_iterator mi, me;
  if (metas) {
    for(mi = metas->begin(), me = metas->end(); mi != me; mi++) {
      PVariable var = domain->getMetaVar((**mi).name, false);
      if (    !var
           || (((**mi).varType > 0) && ((**mi).varType != var->varType))
           || (((**mi).varType==PYTHONVAR) && !pythonDeclarationMatches((**mi).typeDeclaration, var))
           || !checkValueOrder(var, **mi)
         )
        return false;
      if (metaIDs)
        *(metaIDs++) = domain->getMetaNum((**mi).name, false);
    }
  }

  for(ai = attributes->begin(), vi = domain->variables->begin(); ai != ae; ai++, vi++)
    augmentVariableValues(*vi, **ai);
  for(mi = metas->begin(); mi != me; mi++)
    if ((**mi).varType == TValue::INTVAR)
      augmentVariableValues(domain->getMetaVar((**mi).name), **mi);
      
  return true;
}


PDomain TDomainDepot::prepareDomain(TPAttributeDescriptions *attributes, bool hasClass,
                                    TPAttributeDescriptions *classDescriptions,
                                    TPAttributeDescriptions *metas, const int createNewOn,
                                    vector<int> &status, vector<pair<int, int> > &metaStatus)
{ 
  int tStatus;

  status.clear();
  TVarList attrList;
  PITERATE(TPAttributeDescriptions, ai, attributes) {
    attrList.push_back(makeVariable(**ai, tStatus, createNewOn));
    status.push_back(tStatus);
  }
  
  PDomain newDomain;
  PVariable classVar;
  if (hasClass) {
    classVar = attrList.back();
    attrList.erase(attrList.end()-1);
  }
  
  newDomain = mlnew TDomain(classVar, attrList);

  if (classDescriptions) {
      PITERATE(TPAttributeDescriptions, ai, classDescriptions) {
          newDomain->classVars->push_back(makeVariable(**ai, tStatus, createNewOn));
      }
  }

  metaStatus.clear();
  if (metas)
    PITERATE(TPAttributeDescriptions, mi, metas) {
      PVariable var = makeVariable(**mi, tStatus, createNewOn);
      int id = var->defaultMetaId;
      if (!id)
        id = getMetaID();
      newDomain->metas.push_back(TMetaDescriptor(id, var));
      metaStatus.push_back(make_pair(id, tStatus));
    }
    
  return newDomain;
}

PVariable TDomainDepot::createVariable_Python(const string &typeDeclaration, const string &name)
{
  if (typeDeclaration.size() == 6) {
    TPythonVariable *var = mlnew TPythonVariable();
    var->set_name(name);
    return var;
  }


  char *vartypename = const_cast<char *>(typeDeclaration.c_str()+7);
  char *parpos = strchr(vartypename, '(');
  PyObject *var = NULL;

  if (!parpos) {
    TPythonVariablesRegistry::iterator bi(pythonVariables.begin()), be(pythonVariables.end());
    for(; (bi != be) && strcmp((*bi).first.c_str(), vartypename); bi++);

    if (bi!=be) {
      var = PyObject_CallFunction((*bi).second, NULL);
      if (!var)
        throw pyexception();
    }
  }

  if (!var) {
    PyObject *globals = PyEval_GetGlobals();
    PyObject *locals = PyEval_GetLocals();

    var = PyRun_String(vartypename, Py_eval_input, globals, locals);
    if (!parpos && (!var || PyType_Check(var))) {
      PyErr_Clear();
      const int slen = strlen(vartypename);
      char *wPar = strcpy(new char [slen + 3], vartypename);
      strcpy(wPar+slen, "()");
      var = PyRun_String(wPar, Py_eval_input, globals, locals);
    }
  
    if (!var)
      // if parentheses were given, this is the exception from the first call, else from the second
      throw pyexception();
  }

  if (!PyObject_TypeCheck(var, (PyTypeObject *)&PyOrVariable_Type)) {
    Py_DECREF(var);
    ::raiseErrorWho("make_variable", "PythonVariable's constructor is expected to return a 'PythonVariable', not '%s'", var->ob_type->tp_name);
  }

  PVariable pvar = GCPtr<TOrange>((TPyOrange *)var, true);
  Py_DECREF(var);

  pvar->set_name(name);
  return pvar;
}


int Orange_setattrDictionary(TPyOrange *self, const char *name, PyObject *args, bool warn);

PVariable TDomainDepot::makeVariable(TAttributeDescription &desc, int &status, const int &createNewOn)
{
  set<string> values;
  for(map<string, int>::const_iterator dvi(desc.values.begin()), dve(desc.values.end()); dvi != dve; dvi++)
    values.insert(values.end(), dvi->first);
    
  PVariable var = TVariable::make(desc.name, desc.varType, &desc.fixedOrderValues, &values, createNewOn, &status);
  
  if (!var) {
    if (desc.varType == PYTHONVAR) {
      var = createVariable_Python(desc.typeDeclaration, desc.name);
      status = TVariable::NotFound;
    }

    if (!var)
      ::raiseErrorWho("make_variable", "unknown type for attribute '%s'", desc.name.c_str());
  }
  
  if (var && desc.ordered)
    var->ordered = true;
    
  if (desc.userFlags.size()) {
    PyObject *attrdict = PyDict_New();
    ITERATE(TMultiStringParameters, si, desc.userFlags) {
      PyObject *value = PyString_FromString((*si).second.c_str());
      PyDict_SetItemString(attrdict, (*si).first.c_str(), value);
      Py_DECREF(value);
    }
      
    Orange_setattrDictionary((TPyOrange *)(var.counter), "attributes", attrdict, false);
    Py_DECREF(attrdict);
    PyErr_Clear();
  }
  
  return var;
}


void TDomainDepot::pattrFromtAttr(TDomainDepot::TAttributeDescriptions &descs, TDomainDepot::TPAttributeDescriptions &pdescs) {
  pdescs.clear();
  ITERATE(TDomainDepot::TAttributeDescriptions, adi, descs) {
    pdescs.push_back(&*adi);
  }
}