src/main/java/org/thegalactic/context/io/ContextSerializerText.java
public void read(Context context, BufferedReader file) throws IOException {
        /*
         * First line : All observations separated by a space.
         * A StringTokenizer is used to divide the line into different observations considering spaces as separator.
         */
        StringTokenizer tokenizer = new StringTokenizer(file.readLine());

        // First token corresponds to the string "Observations:"
        if (!tokenizer.nextToken().equals("Observations:")) {
            throw new IOException("Invalid declaration of observations");
        }

        // Add all the observations
        while (tokenizer.hasMoreTokens()) {
            if (!context.addToObservations(tokenizer.nextToken())) {
                throw new IOException("Duplicated observation");
            }
        }

        /*
         * Second line : All attributes separated by a space
         * A StringTokenizer is used to divide the line into different token considering spaces as separator.
         */
        tokenizer = new StringTokenizer(file.readLine());

        // first token corresponds to the string "Attributes:"
        if (!tokenizer.nextToken().equals("Attributes:")) {
            throw new IOException("Invalid declaration of attributes");
        }

        // Add all the attributes
        while (tokenizer.hasMoreTokens()) {
            if (!context.addToAttributes(tokenizer.nextToken())) {
                throw new IOException("Duplicated attribute");
            }
        }

        /*
         * Next lines : All intents of observations, one on each line:
         * observation: list of attributes
         * a StringTokenizer is used to divide each intent.
         */
        String line = file.readLine();
        while (line != null && !line.isEmpty()) {
            tokenizer = new StringTokenizer(line);

            // Get the observation identifier
            String identifier = tokenizer.nextToken();

            // Detect invalid format
            if (!identifier.substring(identifier.length() - 1).equals(":")) {
                throw new IOException("Observation name must be followed by a semi-colon");
            }
            identifier = identifier.substring(0, identifier.length() - 1);

            // Verify existence of this observation
            if (!context.containsObservation(identifier)) {
                throw new IOException("Unexisting observation");
            }

            // Add intents for this observation
            while (tokenizer.hasMoreTokens()) {
                String attribute = tokenizer.nextToken();

                // Detect invalid format
                if (!context.containsAttribute(attribute)) {
                    throw new IOException("Unexisting attribute");
                }

                // Add the extent/intent for the current identifier and current attribute
                context.addExtentIntent(identifier, attribute);
            }
            line = file.readLine();
        }
        context.setBitSets();
    }
/**
     * Read a context from a file.
     *
     * The following format is respected:
     *
     * The list of observations separated by a space on the first line ; the
     * list of attrbutes separated by a space on the second line ; then, for
     * each observations o, the list of its intent on a line, written like o a1
     * a2 ...
     *
     * ~~~
     * Observations: 1 2 3
     * Attributes: a b c d e
     * 1: a c
     * 2: a b
     * 3: b d e
     * 4: c e
     * ~~~
     *
     * @param context a context to read
     * @param file    a file
     *
     * @throws IOException When an IOException occurs
     *
     * @todo use Scanner or StreamTokenizer
     */
Read a context from a file.
The following format is respected.

The list of observations separated by a space on the first line ; the
list of attrbutes separated by a space on the second line ; then, for
each observations o, the list of its intent on a line, written like o a1
a2



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



@todo use Scanner or StreamTokenizer
Read a context from a file . The following format is respected . The list of observations separated by a space on the first line ; the list of attrbutes separated by a space on the second line ; then for each observations o the list of its intent on a line written like o a1 a2 @param context a context to read @param file a file @todo use Scanner or StreamTokenizer
public void read(Context context, BufferedReader file) throws IOException {
        /*
         * First line : All observations separated by a space.
         * A StringTokenizer is used to divide the line into different observations considering spaces as separator.
         */
        StringTokenizer tokenizer = new StringTokenizer(file.readLine());
        if (!tokenizer.nextToken().equals("Observations:")) {
            throw new IOException("Invalid declaration of observations");
        }
        while (tokenizer.hasMoreTokens()) {
            if (!context.addToObservations(tokenizer.nextToken())) {
                throw new IOException("Duplicated observation");
            }
        }
        /*
         * Second line : All attributes separated by a space
         * A StringTokenizer is used to divide the line into different token considering spaces as separator.
         */
        tokenizer = new StringTokenizer(file.readLine());
        if (!tokenizer.nextToken().equals("Attributes:")) {
            throw new IOException("Invalid declaration of attributes");
        }
        while (tokenizer.hasMoreTokens()) {
            if (!context.addToAttributes(tokenizer.nextToken())) {
                throw new IOException("Duplicated attribute");
            }
        }
        /*
         * Next lines : All intents of observations, one on each line:
         * observation: list of attributes
         * a StringTokenizer is used to divide each intent.
         */
        String line = file.readLine();
        while (line != null && !line.isEmpty()) {
            tokenizer = new StringTokenizer(line);
            String identifier = tokenizer.nextToken();
            if (!identifier.substring(identifier.length() - 1).equals(":")) {
                throw new IOException("Observation name must be followed by a semi-colon");
            }
            identifier = identifier.substring(0, identifier.length() - 1);
            if (!context.containsObservation(identifier)) {
                throw new IOException("Unexisting observation");
            }
            while (tokenizer.hasMoreTokens()) {
                String attribute = tokenizer.nextToken();
                if (!context.containsAttribute(attribute)) {
                    throw new IOException("Unexisting attribute");
                }
                context.addExtentIntent(identifier, attribute);
            }
            line = file.readLine();
        }
        context.setBitSets();
    }
public void read ( Context context , BufferedReader file ) throws IOException { /*
         * First line : All observations separated by a space.
         * A StringTokenizer is used to divide the line into different observations considering spaces as separator.
         */ StringTokenizer tokenizer = new StringTokenizer ( file . readLine ( ) ) ; if ( ! tokenizer . nextToken ( ) . equals ( "Observations:" ) ) { throw new IOException ( "Invalid declaration of observations" ) ; } while ( tokenizer . hasMoreTokens ( ) ) { if ( ! context . addToObservations ( tokenizer . nextToken ( ) ) ) { throw new IOException ( "Duplicated observation" ) ; } } /*
         * Second line : All attributes separated by a space
         * A StringTokenizer is used to divide the line into different token considering spaces as separator.
         */ tokenizer = new StringTokenizer ( file . readLine ( ) ) ; if ( ! tokenizer . nextToken ( ) . equals ( "Attributes:" ) ) { throw new IOException ( "Invalid declaration of attributes" ) ; } while ( tokenizer . hasMoreTokens ( ) ) { if ( ! context . addToAttributes ( tokenizer . nextToken ( ) ) ) { throw new IOException ( "Duplicated attribute" ) ; } } /*
         * Next lines : All intents of observations, one on each line:
         * observation: list of attributes
         * a StringTokenizer is used to divide each intent.
         */ String line = file . readLine ( ) ; while ( line != null && ! line . isEmpty ( ) ) { tokenizer = new StringTokenizer ( line ) ; String identifier = tokenizer . nextToken ( ) ; if ( ! identifier . substring ( identifier . length ( ) - 1 ) . equals ( ":" ) ) { throw new IOException ( "Observation name must be followed by a semi-colon" ) ; } identifier = identifier . substring ( 0 , identifier . length ( ) - 1 ) ; if ( ! context . containsObservation ( identifier ) ) { throw new IOException ( "Unexisting observation" ) ; } while ( tokenizer . hasMoreTokens ( ) ) { String attribute = tokenizer . nextToken ( ) ; if ( ! context . containsAttribute ( attribute ) ) { throw new IOException ( "Unexisting attribute" ) ; } context . addExtentIntent ( identifier , attribute ) ; } line = file . readLine ( ) ; } context . setBitSets ( ) ; }
Read a context from a file.
Read a context from a file .
// First token corresponds to the string "Observations:" // Add all the observations // first token corresponds to the string "Attributes:" // Add all the attributes // Get the observation identifier // Detect invalid format // Verify existence of this observation // Add intents for this observation // Detect invalid format // Add the extent/intent for the current identifier and current attribute