Пример #1
0
TCostMatrix::TCostMatrix(PVariable acv, const float &inside)
: classVar(acv),
  dimension(0),
  costs(NULL)
{ 
  TEnumVariable *dcv = classVar.AS(TEnumVariable);
  if (!dcv)
    raiseError("attribute '%s' is not discrete", classVar->get_name().c_str());

  dimension = dcv->noOfValues();
  if (!dimension)
    raiseError("attribute '%s' has no values", classVar->get_name().c_str());

  init(inside);
}
Пример #2
0
bool convertFromPythonExisting(PyObject *lst, TExample &example)
{
  PDomain dom=example.domain;

  if (PyOrExample_Check(lst)) {
    const TExample &orex = PyExample_AS_ExampleReference(lst);
    if (orex.domain != dom)
      dom->convert(example, orex);
    else
      example = orex;
    return true;
  }

  if (!PyList_Check(lst)) {
    PyErr_Format(PyExc_TypeError, "invalid argument type (expected list, got '%s)", lst ? lst->ob_type->tp_name : "None");
    return false;
  }

  int const nvars = dom->variables->size() + dom->classVars->size();
  if (Py_ssize_t(nvars) != PyList_Size(lst)) {
    PyErr_Format(PyExc_IndexError, "invalid list size (got %i, expected %i items)",
        PyList_Size(lst), nvars);
    return false;
  }

  Py_ssize_t pos = 0;
  TExample::iterator ei(example.begin());
  TVarList::iterator vi(dom->variables->begin());
  TVarList::const_iterator const ve(dom->variables->end());
  TVarList::const_iterator const ce(dom->classVars->end());
  while(vi != ce && vi != ve) {
    PyObject *li=PyList_GetItem(lst, pos++);
    if (!li)
      PYERROR(PyExc_SystemError, "can't read the list", false);

    if (PyOrValue_Check(li))
      if (PyValue_AS_Variable(li) ? (PyValue_AS_Variable(li) != *vi) : (PyValue_AS_Value(li).varType=!(*vi)->varType) ) {
        PyErr_Format(PyExc_TypeError, "wrong value type for attribute no. %i (%s)", pos, (*vi)->get_name().c_str());
        return false;
      }
      else
        *(ei++)=PyValue_AS_Value(li);

    else {
      if (li == Py_None) {
        *(ei++) = (*vi)->DK();
      } else if (PyString_Check(li)) {
          (*vi)->str2val(string(PyString_AsString(li)), *(ei++));
      } else if ((*vi)->varType==TValue::INTVAR) {
        if (PyInt_Check(li)) {
          TEnumVariable * enumvar = dynamic_cast<TEnumVariable *>(vi->getUnwrappedPtr());
          int value = int(PyInt_AsLong(li));
          if (value < 0 || value >= enumvar->noOfValues()) {
            PyErr_Format(PyExc_ValueError,
                         "value index %i out of range (0 - %i) at attribute no %i (%s)",
                         value, enumvar->noOfValues() - 1, pos, enumvar->get_name().c_str());
            return false;
          }
          *(ei++) = TValue(value);
        } else {
          PyErr_Format(PyExc_TypeError, "attribute no. %i (%s) is ordinal, string or int value expected", pos, (*vi)->get_name().c_str());
          return false;
        }
      }
      else if ((*vi)->varType==TValue::FLOATVAR) {
        float f;
        if (PyNumber_ToFloat(li, f))
          *(ei++) = TValue(f);
        else {
          PyErr_Format(PyExc_TypeError, "attribute no. %i (%s) is continuous, float value expected", pos, (*vi)->get_name().c_str());
          return false;
        }
      }
      else
        ei++;
    }
    if (++vi == ve) {
        vi = dom->classVars->begin();
    }
  }

  return true;
}