1 package org.thegalactic.context.io; 2 3 /* 4 * ContextSerializerFIMI.java 5 * 6 * Copyright: 2010-2015 Karell Bertet, France 7 * Copyright: 2015-2016 The Galactic Organization, France 8 * 9 * License: http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html CeCILL-B license 10 * 11 * This file is part of java-lattices. 12 * You can redistribute it and/or modify it under the terms of the CeCILL-B license. 13 */ 14 import java.io.BufferedReader; 15 import java.io.BufferedWriter; 16 import java.io.IOException; 17 import java.util.HashMap; 18 19 import org.thegalactic.context.Context; 20 import org.thegalactic.io.Reader; 21 import org.thegalactic.io.Writer; 22 23 /** 24 * This class defines the way for reading a context from a text file. 25 * 26 * ![ContextSerializerFIMI](ContextSerializerFIMI.png) 27 * 28 * @uml ContextSerializerFIMI.png 29 * !include resources/org/thegalactic/context/io/ContextSerializerFIMI.iuml 30 * !include resources/org/thegalactic/io/Reader.iuml 31 * !include resources/org/thegalactic/io/Writer.iuml 32 * 33 * hide members 34 * show ContextSerializerFIMI members 35 * class ContextSerializerFIMI #LightCyan 36 * title ContextSerializerFIMI UML graph 37 */ 38 public final class ContextSerializerFIMI implements Reader<Context>, Writer<Context> { 39 40 /** 41 * String extension. 42 */ 43 private static final String EXTENSION = "dat"; 44 45 /** 46 * The singleton instance. 47 */ 48 private static final ContextSerializerFIMI INSTANCE = new ContextSerializerFIMI(); 49 50 /** 51 * Return the singleton instance of this class. 52 * 53 * @return the singleton instance 54 */ 55 public static ContextSerializerFIMI getInstance() { 56 return INSTANCE; 57 } 58 59 /** 60 * Register this class for reading and writing .dat files. 61 */ 62 public static void register() { 63 ContextIOFactory.getInstance().registerReader(ContextSerializerFIMI.getInstance(), EXTENSION); 64 ContextIOFactory.getInstance().registerWriter(ContextSerializerFIMI.getInstance(), EXTENSION); 65 } 66 67 /** 68 * This class is not designed to be publicly instantiated. 69 */ 70 private ContextSerializerFIMI() { 71 } 72 73 /** 74 * Read a context from a file. 75 * 76 * The FIMI dat format file is respected: 77 * 78 * The file format is structured as follows: 79 * 80 * Each line corresponds to an observation Each line is made of a list of 81 * integers corresponding to attributes separated by a space 82 * 83 * ~~~ 84 * 1 3 85 * 2 4 5 86 * 1 2 87 * 3 4 5 88 * ~~~ 89 * 90 * For reading convinience, observations are labelled with 'O' + LineNumber. 91 * 92 * Be careful when using a downloaded file: an empty line at the end of the 93 * file gives an observation with no attributes 94 * 95 * @param context a context to read 96 * @param file a file 97 * 98 * @throws IOException When an IOException occurs 99 */ 100 public void read(final Context context, final BufferedReader file) throws IOException { 101 // Initialize the line number 102 int lineNumber = 0; 103 104 // Loop on the file 105 while (file.ready()) { 106 // Increment the line number 107 lineNumber++; 108 109 // Get the next identifier 110 final String identifier = "O" + lineNumber; 111 context.addToObservations(identifier); 112 113 // Get the current line 114 final String str = file.readLine(); 115 116 // Tokenize the line 117 for (final String token : str.split(" +")) { 118 final Integer attribute = Integer.parseInt(token); 119 if (!context.containsAttribute(attribute)) { 120 context.addToAttributes(attribute); 121 } 122 123 // Add the extent/intent for the current identifier and current attribute 124 context.addExtentIntent(identifier, attribute); 125 } 126 } 127 context.setBitSets(); 128 } 129 130 /** 131 * Write a context to a file. 132 * 133 * The FIMI dat format file is respected : 134 * 135 * The file format is structured as follows: 136 * 137 * Each line corresponds to an observation Each line is made of a list of 138 * integers corresponding to attributes separated by a space 139 * 140 * ~~~ 141 * 1 3 142 * 2 4 5 143 * 1 2 144 * 3 4 5 145 * ~~~ 146 * 147 * @param context a context to write 148 * @param file a file 149 * 150 * @throws IOException When an IOException occurs 151 */ 152 @Override 153 public void write(final Context context, final BufferedWriter file) throws IOException { 154 final HashMap<Comparable, Integer> map = new HashMap(); 155 Integer count = 0; 156 for (final Comparable att : context.getAttributes()) { 157 count++; 158 map.put(att, count); 159 } 160 for (final Comparable obs : context.getObservations()) { 161 for (final Comparable att : context.getIntent(obs)) { 162 file.write(map.get(att) + " "); 163 } 164 file.newLine(); 165 } 166 } 167 }