View Javadoc
1   package org.thegalactic.lattice.io;
2   
3   /*
4    * ImplicationalSystemSerializerText.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.StringTokenizer;
18  
19  import org.thegalactic.io.Reader;
20  import org.thegalactic.io.Writer;
21  import org.thegalactic.rule.ImplicationalSystem;
22  import org.thegalactic.rule.Rule;
23  
24  /**
25   * This class defines the way for reading an implicational system from a text
26   * file.
27   *
28   * ![ImplicationalSystemSerializerText](ImplicationalSystemSerializerText.png)
29   *
30   * @uml
31   *
32   * ImplicationalSystemSerializerText.png
33   *
34   * !include
35   * resources/org/thegalactic/lattice/io/ImplicationalSystemSerializerText.iuml
36   * !include resources/org/thegalactic/io/Reader.iuml
37   * !include resources/org/thegalactic/io/Writer.iuml
38   *
39   * hide members
40   * show ImplicationalSystemSerializerText members
41   * class ImplicationalSystemSerializerText #LightCyan
42   * title ImplicationalSystemSerializerText UML graph
43   */
44  public final class ImplicationalSystemSerializerText implements Reader<ImplicationalSystem>, Writer<ImplicationalSystem> {
45  
46      /**
47       * File extension.
48       */
49      private static final String EXTENSION = "txt";
50  
51      /**
52       * The singleton instance.
53       */
54      private static ImplicationalSystemSerializerText instance = null;
55  
56      /**
57       * Return the singleton instance of this class.
58       *
59       * @return the singleton instance
60       */
61      public static ImplicationalSystemSerializerText getInstance() {
62          if (instance == null) {
63              instance = new ImplicationalSystemSerializerText();
64          }
65          return instance;
66      }
67  
68      /**
69       * Register this class for reading and writing .txt files.
70       */
71      public static void register() {
72          ImplicationalSystemIOFactory.getInstance().registerReader(ImplicationalSystemSerializerText.getInstance(), EXTENSION);
73          ImplicationalSystemIOFactory.getInstance().registerWriter(ImplicationalSystemSerializerText.getInstance(), EXTENSION);
74      }
75  
76      /**
77       * This class is not designed to be publicly instantiated.
78       */
79      private ImplicationalSystemSerializerText() {
80      }
81  
82      /**
83       * Saves this component in a file.
84       *
85       * The following format is used:
86       *
87       * An implicational system can be instancied from and save to a text file in
88       * the following format: A list of elements separated by a space in the
89       * first line ; then, each rule on a line, written like [premise] ->
90       * [conclusion] where elements are separated by a space.
91       *
92       * ~~~
93       * a b c d e
94       * a b -> c d
95       * c d -> e
96       * ~~~
97       *
98       * @param system a system to read
99       * @param file   a file
100      *
101      * @throws IOException When an IOException occurs
102      */
103     public void read(ImplicationalSystem system, BufferedReader file) throws IOException {
104         // first line : All elements of S separated by a space
105         // a StringTokenizer is used to divide the line into different token,
106         // considering spaces as separator.
107         StringTokenizer st = new StringTokenizer(file.readLine());
108         while (st.hasMoreTokens()) {
109             String n = new String(st.nextToken());
110             system.addElement(n);
111         }
112         // next lines : [elements of the premise separated by a space] -> [elements of the conclusion separated by a space]
113         // a StringTokenizer is used to divide each rule.
114         String line = file.readLine();
115         while (line != null && !line.isEmpty()) {
116             st = new StringTokenizer(line);
117             Rule r = new Rule();
118             boolean prem = true;
119             while (st.hasMoreTokens()) {
120                 String word = st.nextToken();
121                 if ("->".equals(word)) {
122                     prem = false;
123                 } else {
124                     String x = null;
125                     // search of x in S
126                     for (Comparable e : system.getSet()) {
127                         if (((String) e).equals(word)) {
128                             x = (String) e;
129                         }
130                     }
131                     if (x != null) {
132                         if (prem) {
133                             r.addToPremise(x);
134                         } else {
135                             r.addToConclusion(x);
136                         }
137                     }
138                 }
139             }
140             if (!r.getConclusion().isEmpty()) {
141                 system.addRule(r);
142             }
143             line = file.readLine();
144         }
145     }
146 
147     /**
148      * Saves this component in a file.
149      *
150      * The following format is used:
151      *
152      * An implicational system can be instancied from and save to a text file in
153      * the following format: A list of elements separated by a space in the
154      * first line ; then, each rule on a line, written like [premise] ->
155      * [conclusion] where elements are separated by a space.
156      *
157      * ~~~
158      * a b c d e
159      * a b -> c d
160      * c d -> e
161      * ~~~
162      *
163      * @param system a system to write
164      * @param file   a file
165      *
166      * @throws IOException When an IOException occurs
167      */
168     public void write(ImplicationalSystem system, BufferedWriter file) throws IOException {
169         file.write(system.toString());
170     }
171 }