Package org.integratedmodelling.riskwiz.learning.data

Examples of org.integratedmodelling.riskwiz.learning.data.FastVector


            } else {
                errorMessage("keyword " + Instances.ARFF_RELATION + " expected");
            }

            // Create vectors to hold information temporarily.
            FastVector attributes = new FastVector();
  
            // Get attribute declarations.
            getFirstToken();
            if (m_Tokenizer.ttype == StreamTokenizer.TT_EOF) {
                errorMessage("premature end of file");
            }

            while (Attribute.ARFF_ATTRIBUTE.equalsIgnoreCase(m_Tokenizer.sval)) {
                attributes = parseAttribute(attributes);
            }

            // Check if data part follows. We can't easily check for EOL.
            if (!Instances.ARFF_DATA.equalsIgnoreCase(m_Tokenizer.sval)) {
                errorMessage("keyword " + Instances.ARFF_DATA + " expected");
            }
     
            // Check if any attributes have been declared.
            if (attributes.size() == 0) {
                errorMessage("no attributes declared");
            }
     
            m_Data = new Instances(relationName, attributes, capacity);
        }
View Full Code Here


         * @throws IOException   if the information is not read
         *         successfully
         */
        protected FastVector parseAttribute(FastVector attributes) throws IOException {
            String attributeName;
            FastVector attributeValues;

            // Get attribute name.
            getNextToken();
            attributeName = m_Tokenizer.sval;
            getNextToken();
     
            // Check if attribute is nominal.
            if (m_Tokenizer.ttype == StreamTokenizer.TT_WORD) {
       
                // Attribute is real, integer, or string.
                if (m_Tokenizer.sval.equalsIgnoreCase(
                        Attribute.ARFF_ATTRIBUTE_REAL)
                                || m_Tokenizer.sval.equalsIgnoreCase(
                                        Attribute.ARFF_ATTRIBUTE_INTEGER)
                                        || m_Tokenizer.sval.equalsIgnoreCase(
                                                Attribute.ARFF_ATTRIBUTE_NUMERIC)) {
                    attributes.addElement(
                            new Attribute(attributeName, attributes.size()));
                    readTillEOL();
                } else if (m_Tokenizer.sval.equalsIgnoreCase(
                        Attribute.ARFF_ATTRIBUTE_STRING)) {
                    attributes.addElement(
                            new Attribute(attributeName, (FastVector) null,
                            attributes.size()));
                    readTillEOL();
                } else if (m_Tokenizer.sval.equalsIgnoreCase(
                        Attribute.ARFF_ATTRIBUTE_DATE)) {
                    String format = null;

                    if (m_Tokenizer.nextToken() != StreamTokenizer.TT_EOL) {
                        if ((m_Tokenizer.ttype != StreamTokenizer.TT_WORD)
                                && (m_Tokenizer.ttype != '\'')
                                && (m_Tokenizer.ttype != '\"')) {
                            errorMessage("not a valid date format");
                        }
                        format = m_Tokenizer.sval;
                        readTillEOL();
                    } else {
                        m_Tokenizer.pushBack();
                    }
                    attributes.addElement(
                            new Attribute(attributeName, format,
                            attributes.size()));
         
                } else if (m_Tokenizer.sval.equalsIgnoreCase(
                        Attribute.ARFF_ATTRIBUTE_RELATIONAL)) {
                    readTillEOL();
         
                    // Read attributes for subrelation
                    // First, save current set of attributes
                    FastVector atts = attributes;

                    attributes = new FastVector();
         
                    // Now, read attributes until we hit end of declaration of relational value
                    getFirstToken();
                    if (m_Tokenizer.ttype == StreamTokenizer.TT_EOF) {
                        errorMessage("premature end of file");
                    }
                    do {
                        if (Attribute.ARFF_ATTRIBUTE.equalsIgnoreCase(
                                m_Tokenizer.sval)) {
                            attributes = parseAttribute(attributes);
                        } else if (Attribute.ARFF_END_SUBRELATION.equalsIgnoreCase(
                                m_Tokenizer.sval)) {
                            getNextToken();
                            if (!attributeName.equalsIgnoreCase(m_Tokenizer.sval)) {
                                errorMessage(
                                        "declaration of subrelation "
                                                + attributeName
                                                + " must be terminated by "
                                                + "@end " + attributeName);
                            }
                            break;
                        } else {
                            errorMessage(
                                    "declaration of subrelation "
                                            + attributeName
                                            + " must be terminated by "
                                            + "@end " + attributeName);
                        }
                    } while (true);
         
                    // Make relation and restore original set of attributes
                    Instances relation = new Instances(attributeName, attributes,
                            0);

                    attributes = atts;
                    attributes.addElement(
                            new Attribute(attributeName, relation,
                            attributes.size()));
                } else {
                    errorMessage(
                            "no valid attribute type or invalid "
                                    + "enumeration");
                }
            } else {
       
                // Attribute is nominal.
                attributeValues = new FastVector();
                m_Tokenizer.pushBack();
       
                // Get values for nominal attribute.
                if (m_Tokenizer.nextToken() != '{') {
                    errorMessage("{ expected at beginning of enumeration");
View Full Code Here

        }
        if (m_Password != null) {
            m_DataBaseConnection.setPassword(m_Password);
        }

        m_orderBy = new FastVector();
        // don't lose previously set key columns!
        if (m_Keys != null) {
            setKeys(m_Keys);
        }
     
View Full Code Here

                        if (m_DataBaseConnection.getUpperCase()) {
                            columnName = columnName.toUpperCase();
                        }
                        m_nominalIndexes[i - 1] = new Hashtable();
                        m_nominalStrings[i - 1] = new FastVector();
                        String query = "SELECT COUNT(DISTINCT( " + columnName
                                + " )) FROM " + end;

                        if (m_DataBaseConnection.execute(query) == true) {
                            rs1 = m_DataBaseConnection.getResultSet();
                            rs1.next();
                            int count = rs1.getInt(1);

                            rs1.close();
                            if (count > m_nominalToStringLimit
                                    || m_DataBaseConnection.execute(
                                            "SELECT DISTINCT ( " + columnName
                                            + " ) FROM " + end)
                                                    == false) {
                                attributeTypes[i - 1] = Attribute.STRING;
                                break;
                            }
                            rs1 = m_DataBaseConnection.getResultSet();
                        } else {
                            // System.err.println("Count for nominal values cannot be calculated. Attribute "+columnName+" treated as String.");
                            attributeTypes[i - 1] = Attribute.STRING;
                            break;
                        }
                        attributeTypes[i - 1] = Attribute.NOMINAL;
                        stringToNominal(rs1, i);
                        rs1.close();
                        break;

                    case DatabaseUtils.TEXT:
                        // System.err.println("boolean --> string");
                        columnName = md.getColumnName(i);
                        if (m_DataBaseConnection.getUpperCase()) {
                            columnName = columnName.toUpperCase();
                        }
                        m_nominalIndexes[i - 1] = new Hashtable();
                        m_nominalStrings[i - 1] = new FastVector();
                        query = "SELECT COUNT(DISTINCT( " + columnName
                                + " )) FROM " + end;
                        if (m_DataBaseConnection.execute(query) == true) {
                            rs1 = m_DataBaseConnection.getResultSet();
                            stringToNominal(rs1, i);
                            rs1.close();
                        }
                        attributeTypes[i - 1] = Attribute.STRING;
                        break;

                    case DatabaseUtils.BOOL:
                        // System.err.println("boolean --> nominal");
                        attributeTypes[i - 1] = Attribute.NOMINAL;
                        m_nominalIndexes[i - 1] = new Hashtable();
                        m_nominalIndexes[i - 1].put("false", new Double(0));
                        m_nominalIndexes[i - 1].put("true", new Double(1));
                        m_nominalStrings[i - 1] = new FastVector();
                        m_nominalStrings[i - 1].addElement("false");
                        m_nominalStrings[i - 1].addElement("true");
                        break;

                    case DatabaseUtils.DOUBLE:
                        // System.err.println("BigDecimal --> numeric");
                        attributeTypes[i - 1] = Attribute.NUMERIC;
                        break;

                    case DatabaseUtils.BYTE:
                        // System.err.println("byte --> numeric");
                        attributeTypes[i - 1] = Attribute.NUMERIC;
                        break;

                    case DatabaseUtils.SHORT:
                        // System.err.println("short --> numeric");
                        attributeTypes[i - 1] = Attribute.NUMERIC;
                        break;

                    case DatabaseUtils.INTEGER:
                        // System.err.println("int --> numeric");
                        attributeTypes[i - 1] = Attribute.NUMERIC;
                        break;

                    case DatabaseUtils.LONG:
                        // System.err.println("long --> numeric");
                        attributeTypes[i - 1] = Attribute.NUMERIC;
                        break;

                    case DatabaseUtils.FLOAT:
                        // System.err.println("float --> numeric");
                        attributeTypes[i - 1] = Attribute.NUMERIC;
                        break;

                    case DatabaseUtils.DATE:
                        attributeTypes[i - 1] = Attribute.DATE;
                        break;

                    default:
                        // System.err.println("Unknown column type");
                        attributeTypes[i - 1] = Attribute.STRING;
                    }
                }
                FastVector attribInfo = new FastVector();

                for (int i = 0; i < numAttributes; i++) {

                    /* Fix for databases that uppercase column names */
                    // String attribName = attributeCaseFix(md.getColumnName(i + 1));
                    String attribName = md.getColumnName(i + 1);

                    switch (attributeTypes[i]) {
                    case Attribute.NOMINAL:
                        attribInfo.addElement(
                                new Attribute(attribName, m_nominalStrings[i]));
                        break;

                    case Attribute.NUMERIC:
                        attribInfo.addElement(new Attribute(attribName));
                        break;

                    case Attribute.STRING:
                        Attribute att = new Attribute(attribName,
                                (FastVector) null);

                        for (int n = 0; n < m_nominalStrings[i].size(); n++) {
                            att.addStringValue(
                                    (String) m_nominalStrings[i].elementAt(n));
                        }
                        attribInfo.addElement(att);
                        break;

                    case Attribute.DATE:
                        attribInfo.addElement(
                                new Attribute(attribName, (String) null));
                        break;

                    default:
                        throw new IOException("Unknown attribute type");
View Full Code Here

                        columnName = columnName.toUpperCase();
                    }
                    String end = endOfQuery(false);

                    m_nominalIndexes[i - 1] = new Hashtable();
                    m_nominalStrings[i - 1] = new FastVector();
                    if (m_DataBaseConnection.execute(
                            "SELECT DISTINCT ( " + columnName + " ) FROM " + end)
                                    == false) {
                        throw new Exception("Nominal values cannot be retrieved");
                    }
                    rs1 = m_DataBaseConnection.getResultSet();
                    attributeTypes[i - 1] = Attribute.NOMINAL;
                    stringToNominal(rs1, i);
                    rs1.close()
                    break;

                case DatabaseUtils.TEXT:
                    columnName = md.getColumnName(i);
                    if (m_DataBaseConnection.getUpperCase()) {
                        columnName = columnName.toUpperCase();
                    }
                    end = endOfQuery(false);
                    m_nominalIndexes[i - 1] = new Hashtable();
                    m_nominalStrings[i - 1] = new FastVector();
                    if (m_DataBaseConnection.execute(
                            "SELECT DISTINCT ( " + columnName + " ) FROM " + end)
                                    == false) {
                        throw new Exception("Nominal values cannot be retrieved");
                    }
                    rs1 = m_DataBaseConnection.getResultSet();
                    attributeTypes[i - 1] = Attribute.STRING;
                    stringToNominal(rs1, i);
                    rs1.close()
                    break;

                case DatabaseUtils.BOOL:
                    // System.err.println("boolean --> nominal");
                    attributeTypes[i - 1] = Attribute.NOMINAL;
                    m_nominalIndexes[i - 1] = new Hashtable();
                    m_nominalIndexes[i - 1].put("false", new Double(0));
                    m_nominalIndexes[i - 1].put("true", new Double(1));
                    m_nominalStrings[i - 1] = new FastVector();
                    m_nominalStrings[i - 1].addElement("false");
                    m_nominalStrings[i - 1].addElement("true");
                    break;

                case DatabaseUtils.DOUBLE:
                    // System.err.println("BigDecimal --> numeric");
                    attributeTypes[i - 1] = Attribute.NUMERIC;
                    break;

                case DatabaseUtils.BYTE:
                    // System.err.println("byte --> numeric");
                    attributeTypes[i - 1] = Attribute.NUMERIC;
                    break;

                case DatabaseUtils.SHORT:
                    // System.err.println("short --> numeric");
                    attributeTypes[i - 1] = Attribute.NUMERIC;
                    break;

                case DatabaseUtils.INTEGER:
                    // System.err.println("int --> numeric");
                    attributeTypes[i - 1] = Attribute.NUMERIC;
                    break;

                case DatabaseUtils.LONG:
                    // System.err.println("long --> numeric");
                    attributeTypes[i - 1] = Attribute.NUMERIC;
                    break;

                case DatabaseUtils.FLOAT:
                    // System.err.println("float --> numeric");
                    attributeTypes[i - 1] = Attribute.NUMERIC;
                    break;

                case DatabaseUtils.DATE:
                    attributeTypes[i - 1] = Attribute.DATE;
                    break;

                default:
                    // System.err.println("Unknown column type");
                    attributeTypes[i - 1] = Attribute.STRING;
                }
            }

            // Step through the tuples
            // System.err.println("Creating instances...");
            FastVector instances = new FastVector();

            while (rs.next()) {
                double[] vals = new double[numAttributes];

                for (int i = 1; i <= numAttributes; i++) {
                    switch (m_DataBaseConnection.translateDBColumnType(
                            md.getColumnTypeName(i))) {
                    case DatabaseUtils.STRING:
                        String str = rs.getString(i);
   
                        if (rs.wasNull()) {
                            vals[i - 1] = Instance.missingValue();
                        } else {
                            Double index = (Double) m_nominalIndexes[i - 1].get(
                                    str);

                            if (index == null) {
                                index = new Double(
                                        m_structure.attribute(i - 1).addStringValue(
                                                str));
                            }
                            vals[i - 1] = index.doubleValue();
                        }
                        break;

                    case DatabaseUtils.TEXT:
                        str = rs.getString(i);

                        if (rs.wasNull()) {
                            vals[i - 1] = Instance.missingValue();
                        } else {
                            Double index = (Double) m_nominalIndexes[i - 1].get(
                                    str);

                            if (index == null) {
                                index = new Double(
                                        m_structure.attribute(i - 1).addStringValue(
                                                str));
                            }
                            vals[i - 1] = index.doubleValue();
                        }
                        break;

                    case DatabaseUtils.BOOL:
                        boolean boo = rs.getBoolean(i);

                        if (rs.wasNull()) {
                            vals[i - 1] = Instance.missingValue();
                        } else {
                            vals[i - 1] = (boo ? 1.0 : 0.0);
                        }
                        break;

                    case DatabaseUtils.DOUBLE:
                        double dd = rs.getDouble(i);

                        if (rs.wasNull()) {
                            vals[i - 1] = Instance.missingValue();
                        } else {
                            vals[i - 1] = dd;
                        }
                        break;

                    case DatabaseUtils.BYTE:
                        byte by = rs.getByte(i);

                        if (rs.wasNull()) {
                            vals[i - 1] = Instance.missingValue();
                        } else {
                            vals[i - 1] = by;
                        }
                        break;

                    case DatabaseUtils.SHORT:
                        short sh = rs.getByte(i);

                        if (rs.wasNull()) {
                            vals[i - 1] = Instance.missingValue();
                        } else {
                            vals[i - 1] = sh;
                        }
                        break;

                    case DatabaseUtils.INTEGER:
                        int in = rs.getInt(i);

                        if (rs.wasNull()) {
                            vals[i - 1] = Instance.missingValue();
                        } else {
                            vals[i - 1] = in;
                        }
                        break;

                    case DatabaseUtils.LONG:
                        long lo = rs.getLong(i);

                        if (rs.wasNull()) {
                            vals[i - 1] = Instance.missingValue();
                        } else {
                            vals[i - 1] = lo;
                        }
                        break;

                    case DatabaseUtils.FLOAT:
                        float fl = rs.getFloat(i);

                        if (rs.wasNull()) {
                            vals[i - 1] = Instance.missingValue();
                        } else {
                            vals[i - 1] = fl;
                        }
                        break;

                    case DatabaseUtils.DATE:
                        Date date = rs.getDate(i);

                        if (rs.wasNull()) {
                            vals[i - 1] = Instance.missingValue();
                        } else {
                            // TODO: Do a value check here.
                            vals[i - 1] = date.getTime();
                        }
                        break;

                    default:
                        vals[i - 1] = Instance.missingValue();
                    }
                }
                Instance newInst;

                newInst = new Instance(1.0, vals);
                instances.addElement(newInst);
            }  
   
            // Create the header and add the instances to the dataset
            // System.err.println("Creating header...");
            FastVector attribInfo = new FastVector();

            for (int i = 0; i < numAttributes; i++) {

                /* Fix for databases that uppercase column names */
                // String attribName = attributeCaseFix(md.getColumnName(i + 1));
                String attribName = md.getColumnName(i + 1);

                switch (attributeTypes[i]) {
                case Attribute.NOMINAL:
                    attribInfo.addElement(
                            new Attribute(attribName, m_nominalStrings[i]));
                    break;

                case Attribute.NUMERIC:
                    attribInfo.addElement(new Attribute(attribName));
                    break;

                case Attribute.STRING:
                    Attribute att = new Attribute(attribName, (FastVector) null);

                    attribInfo.addElement(att);
                    for (int n = 0; n < m_nominalStrings[i].size(); n++) {
                        att.addStringValue(
                                (String) m_nominalStrings[i].elementAt(n));
                    }
                    break;

                case Attribute.DATE:
                    attribInfo.addElement(
                            new Attribute(attribName, (String) null));
                    break;

                default:
                    throw new IOException("Unknown attribute type");
View Full Code Here

     * @return an enumeration of the available options
     */ 
    @Override
  public java.util.Enumeration listOptions() {
     
        FastVector newVector = new FastVector();

        newVector.addElement(
                new Option(
                        "\tThe JDBC URL to connect to.\n"
                                + "\t(default: from DatabaseUtils.props file)",
                                "url",
                                1,
                                "-url <JDBC URL>"));
    
        newVector.addElement(
                new Option(
                        "\tThe user to connect with to the database.\n"
                                + "\t(default: none)",
                                "user",
                                1,
                                "-user <name>"));
    
        newVector.addElement(
                new Option(
                        "\tThe password to connect with to the database.\n"
                                + "\t(default: none)",
                                "password",
                                1,
                                "-password <password>"));
    
        newVector.addElement(
                new Option(
                        "\tSQL query of the form\n"
                                + "\t\tSELECT <list of columns>|* FROM <table> [WHERE]\n"
                                + "\tto execute.\n"
                                + "\t(default: Select * From Results0)",
                                "Q",
                                1,
                                "-Q <query>"));
    
        newVector.addElement(
                new Option(
                        "\tList of column names uniquely defining a DB row\n"
                                + "\t(separated by ', ').\n"
                                + "\tUsed for incremental loading.\n"
                                + "\tIf not specified, the key will be determined automatically,\n"
                                + "\tif possible with the used JDBC driver.\n"
                                + "\tThe auto ID column created by the DatabaseSaver won't be loaded.",
                                "P",
                                1,
                                "-P <list of column names>"));

        newVector.addElement(
                new Option("\tSets incremental loading", "I", 0, "-I"));
    
        return  newVector.elements();
    }
View Full Code Here

     * @param parent  the (nominal) attribute node
     * @return    the label vector
     * @throws Exception  if generation fails
     */
    protected FastVector createLabels(Element parent) throws Exception {
        FastVector    result;
        Vector    list;
        Element    node;
        Element    labelsnode;
        int      i;
   
        result = new FastVector();
   
        // find labels node directly underneath this attribute, but not in
        // deeper nested attributes (e.g., within relational attributes)
        labelsnode = null;
        list = getChildTags(parent, TAG_LABELS);
        if (list.size() > 0) {
            labelsnode = (Element) list.get(0);
        }
   
        // retrieve all labels
        if (labelsnode != null) {
            list = getChildTags(labelsnode, TAG_LABEL);
            for (i = 0; i < list.size(); i++) {
                node = (Element) list.get(i);
                result.addElement(getContent(node));
            }
        }
   
        return result;
    }
View Full Code Here

    protected Attribute createAttribute(Element node) throws Exception {
        String    typeStr;
        String    name;
        int      type;
        Attribute    result;
        FastVector    values;
        ProtectedProperties  metadata;
        Vector    list;
        FastVector    atts;
   
        result = null;
   
        // name
        name = node.getAttribute(ATT_NAME);
View Full Code Here

     * @return    the vector with the generated attributes
     * @throws Exception  if generation fails, e.g., due to unknown attribute type
     */
    protected FastVector createAttributes(Element parent, int[] classIndex) throws Exception {
        Vector  list;
        FastVector  result;
        int    i;
        Element  node;
        Attribute  att;

        result = new FastVector();
        classIndex[0] = -1;
   
        list = getChildTags(parent, TAG_ATTRIBUTE);
        for (i = 0; i < list.size(); i++) {
            node = (Element) list.get(i);
            att = createAttribute(node);
            if (node.getAttribute(ATT_CLASS).equals(VAL_YES)) {
                classIndex[0] = i;
            }
            result.addElement(att);
        }
   
        return result;
    }
View Full Code Here

    protected Instances headerFromXML() throws Exception {
        Instances  result;
        Element  root;
        Element  node;
        Vector  list;
        FastVector  atts;
        Version  version;
        int[]  classIndex;

        root = m_Document.getDocumentElement();
   
View Full Code Here

TOP

Related Classes of org.integratedmodelling.riskwiz.learning.data.FastVector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.