Ejemplo n.º 1
0
  /**
   * Test the given attribute, whether it can be processed by the handler, given its capabilities.
   *
   * @param att the attribute to test
   * @param isClass whether this attribute is the class attribute
   * @return true if all the tests succeeded
   * @see #m_AttributeTest
   */
  public boolean test(Attribute att, boolean isClass) {
    boolean result;
    Capability cap;
    Capability capBinary;
    Capability capUnary;
    Capability capEmpty;
    String errorStr;

    result = true;

    // shall we test the data?
    if (!m_AttributeTest) return result;

    // for exception
    if (isClass) errorStr = "class";
    else errorStr = "attributes";

    switch (att.type()) {
      case Attribute.NOMINAL:
        if (isClass) {
          cap = Capability.NOMINAL_CLASS;
          capBinary = Capability.BINARY_CLASS;
          capUnary = Capability.UNARY_CLASS;
          capEmpty = Capability.EMPTY_NOMINAL_CLASS;
        } else {
          cap = Capability.NOMINAL_ATTRIBUTES;
          capBinary = Capability.BINARY_ATTRIBUTES;
          capUnary = Capability.UNARY_ATTRIBUTES;
          capEmpty = Capability.EMPTY_NOMINAL_ATTRIBUTES;
        }

        if (handles(cap) && (att.numValues() > 2)) break;
        else if (handles(capBinary) && (att.numValues() == 2)) break;
        else if (handles(capUnary) && (att.numValues() == 1)) break;
        else if (handles(capEmpty) && (att.numValues() == 0)) break;

        if (att.numValues() == 0) {
          m_FailReason =
              new UnsupportedAttributeTypeException(
                  createMessage("Cannot handle empty nominal " + errorStr + "!"));
          result = false;
        }
        if (att.numValues() == 1) {
          m_FailReason =
              new UnsupportedAttributeTypeException(
                  createMessage("Cannot handle unary " + errorStr + "!"));
          result = false;
        } else if (att.numValues() == 2) {
          m_FailReason =
              new UnsupportedAttributeTypeException(
                  createMessage("Cannot handle binary " + errorStr + "!"));
          result = false;
        } else {
          m_FailReason =
              new UnsupportedAttributeTypeException(
                  createMessage("Cannot handle multi-valued nominal " + errorStr + "!"));
          result = false;
        }
        break;

      case Attribute.NUMERIC:
        if (isClass) cap = Capability.NUMERIC_CLASS;
        else cap = Capability.NUMERIC_ATTRIBUTES;

        if (!handles(cap)) {
          m_FailReason =
              new UnsupportedAttributeTypeException(
                  createMessage("Cannot handle numeric " + errorStr + "!"));
          result = false;
        }
        break;

      case Attribute.DATE:
        if (isClass) cap = Capability.DATE_CLASS;
        else cap = Capability.DATE_ATTRIBUTES;

        if (!handles(cap)) {
          m_FailReason =
              new UnsupportedAttributeTypeException(
                  createMessage("Cannot handle date " + errorStr + "!"));
          result = false;
        }
        break;

      case Attribute.STRING:
        if (isClass) cap = Capability.STRING_CLASS;
        else cap = Capability.STRING_ATTRIBUTES;

        if (!handles(cap)) {
          m_FailReason =
              new UnsupportedAttributeTypeException(
                  createMessage("Cannot handle string " + errorStr + "!"));
          result = false;
        }
        break;

      case Attribute.RELATIONAL:
        if (isClass) cap = Capability.RELATIONAL_CLASS;
        else cap = Capability.RELATIONAL_ATTRIBUTES;

        if (!handles(cap)) {
          m_FailReason =
              new UnsupportedAttributeTypeException(
                  createMessage("Cannot handle relational " + errorStr + "!"));
          result = false;
        }
        // attributes in the relation of this attribute must be tested
        // separately with a different Capabilites object
        break;

      default:
        m_FailReason =
            new UnsupportedAttributeTypeException(
                createMessage("Cannot handle unknown attribute type '" + att.type() + "'!"));
        result = false;
    }

    return result;
  }