src/main/java/org/thegalactic/context/io/ContextSerializerCsv.java
public void read(Context context, BufferedReader file) throws IOException {
        // Parse the file
        CSVParser parser = CSVFormat.RFC4180.parse(file);

        // Get the records and record size
        List<CSVRecord> records = parser.getRecords();
        int length = records.size();

        // Verify length
        if (length == 0) {
            throw new IOException("CSV cannot be empty");
        }

        // Get the attributes and the attribute size
        CSVRecord attributes = records.get(0);
        int size = attributes.size();

        // Detect invalid attribute size
        if (size == 1 && attributes.get(0).equals("")) {
            throw new IOException("Attribute size cannot be 0");
        }

        // Index of the first attribute
        int first = 0;
        if (attributes.get(0).equals("")) {
            first = 1;
        }

        // Get the attributes
        for (int i = first; i < size; i++) {
            String attribute = attributes.get(i);

            // Detect duplicated attribute
            if (!context.addToAttributes(attribute)) {
                throw new IOException("Duplicated attribute");
            }

            // Detect empty attribute
            if ("".equals(attribute)) {
                throw new IOException("Empty attribute");
            }
        }

        // Get the data
        for (int j = 1; j < length; j++) {
            // Get the current record
            CSVRecord record = records.get(j);

            // Detect incorrect size
            if (record.size() != size) {
                throw new IOException("Line does not have the correct number of attributes");
            }

            // Get the observation identifier
            String identifier;
            if (first == 1) {
                identifier = record.get(0);
            } else {
                identifier = String.valueOf(j);
            }

            // Detect duplicated identifier
            if (!context.addToObservations(identifier)) {
                throw new IOException("Duplicated identifier");
            }

            // Add the extent/intent for the current identifier and current attribute
            for (int i = first; i < size; i++) {
                if (record.get(i).equals("1")) {
                    context.addExtentIntent(identifier, attributes.get(i));
                }
            }
        }

        // Close the parser
        parser.close();
        context.setBitSets();
    }
/**
     * Read a context from a csv file.
     *
     * The following format is respected:
     *
     * The first line contains the attribute names, the other lines contains the
     * observations identifier followed by boolean values
     *
     * ~~~
     * "",a,b,c,d,e
     * 1,1,0,1,0,0
     * 2,1,1,0,0,0
     * 3,0,1,0,1,1
     * 4,0,0,1,0,1
     * ~~~
     *
     * If the first attribute is the empty string, the first column corresponds
     * to the individual identifiers. In the other case, the individual
     * identifiers will be generated by successive integers.
     *
     * ~~~
     * a,b,c,d,e
     * 1,0,1,0,0
     * 1,1,0,0,0
     * 0,1,0,1,1
     * 0,0,1,0,1
     * ~~~
     *
     * @param context a context to read
     * @param file    a file
     *
     * @throws IOException When an IOException occurs
     */
Read a context from a csv file.
The following format is respected.

The first line contains the attribute names, the other lines contains the
observations identifier followed by boolean values



If the first attribute is the empty string, the first column corresponds
to the individual identifiers. In the other case, the individual
identifiers will be generated by successive integers.



@param context a context to read
@param file    a file


Read a context from a csv file . The following format is respected . The first line contains the attribute names the other lines contains the observations identifier followed by boolean values If the first attribute is the empty string the first column corresponds to the individual identifiers . In the other case the individual identifiers will be generated by successive integers . @param context a context to read @param file a file
public void read(Context context, BufferedReader file) throws IOException {
        CSVParser parser = CSVFormat.RFC4180.parse(file);
        List<CSVRecord> records = parser.getRecords();
        int length = records.size();
        if (length == 0) {
            throw new IOException("CSV cannot be empty");
        }
        CSVRecord attributes = records.get(0);
        int size = attributes.size();
        if (size == 1 && attributes.get(0).equals("")) {
            throw new IOException("Attribute size cannot be 0");
        }
        int first = 0;
        if (attributes.get(0).equals("")) {
            first = 1;
        }
        for (int i = first; i < size; i++) {
            String attribute = attributes.get(i);
            if (!context.addToAttributes(attribute)) {
                throw new IOException("Duplicated attribute");
            }
            if ("".equals(attribute)) {
                throw new IOException("Empty attribute");
            }
        }
        for (int j = 1; j < length; j++) {
            CSVRecord record = records.get(j);
            if (record.size() != size) {
                throw new IOException("Line does not have the correct number of attributes");
            }
            String identifier;
            if (first == 1) {
                identifier = record.get(0);
            } else {
                identifier = String.valueOf(j);
            }
            if (!context.addToObservations(identifier)) {
                throw new IOException("Duplicated identifier");
            }
            for (int i = first; i < size; i++) {
                if (record.get(i).equals("1")) {
                    context.addExtentIntent(identifier, attributes.get(i));
                }
            }
        }
        parser.close();
        context.setBitSets();
    }
public void read ( Context context , BufferedReader file ) throws IOException { CSVParser parser = CSVFormat . RFC4180 . parse ( file ) ; List < CSVRecord > records = parser . getRecords ( ) ; int length = records . size ( ) ; if ( length == 0 ) { throw new IOException ( "CSV cannot be empty" ) ; } CSVRecord attributes = records . get ( 0 ) ; int size = attributes . size ( ) ; if ( size == 1 && attributes . get ( 0 ) . equals ( "" ) ) { throw new IOException ( "Attribute size cannot be 0" ) ; } int first = 0 ; if ( attributes . get ( 0 ) . equals ( "" ) ) { first = 1 ; } for ( int i = first ; i < size ; i ++ ) { String attribute = attributes . get ( i ) ; if ( ! context . addToAttributes ( attribute ) ) { throw new IOException ( "Duplicated attribute" ) ; } if ( "" . equals ( attribute ) ) { throw new IOException ( "Empty attribute" ) ; } } for ( int j = 1 ; j < length ; j ++ ) { CSVRecord record = records . get ( j ) ; if ( record . size ( ) != size ) { throw new IOException ( "Line does not have the correct number of attributes" ) ; } String identifier ; if ( first == 1 ) { identifier = record . get ( 0 ) ; } else { identifier = String . valueOf ( j ) ; } if ( ! context . addToObservations ( identifier ) ) { throw new IOException ( "Duplicated identifier" ) ; } for ( int i = first ; i < size ; i ++ ) { if ( record . get ( i ) . equals ( "1" ) ) { context . addExtentIntent ( identifier , attributes . get ( i ) ) ; } } } parser . close ( ) ; context . setBitSets ( ) ; }
Read a context from a csv file.
Read a context from a csv file .
// Parse the file // Get the records and record size // Verify length // Get the attributes and the attribute size // Detect invalid attribute size // Index of the first attribute // Get the attributes // Detect duplicated attribute // Detect empty attribute // Get the data // Get the current record // Detect incorrect size // Get the observation identifier // Detect duplicated identifier // Add the extent/intent for the current identifier and current attribute // Close the parser