View Javadoc

1   /*
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le Berre
3    * 
4    * Based on the original minisat specification from:
5    * 
6    * An extensible SAT solver. Niklas E?n and Niklas S?rensson. Proceedings of the
7    * Sixth International Conference on Theory and Applications of Satisfiability
8    * Testing, LNCS 2919, pp 502-518, 2003.
9    * 
10   * This library is free software; you can redistribute it and/or modify it under
11   * the terms of the GNU Lesser General Public License as published by the Free
12   * Software Foundation; either version 2.1 of the License, or (at your option)
13   * any later version.
14   * 
15   * This library is distributed in the hope that it will be useful, but WITHOUT
16   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18   * details.
19   * 
20   * You should have received a copy of the GNU Lesser General Public License
21   * along with this library; if not, write to the Free Software Foundation, Inc.,
22   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23   * 
24   */
25  
26  package org.sat4j;
27  
28  import java.io.BufferedReader;
29  import java.io.FileReader;
30  import java.io.FileWriter;
31  import java.io.IOException;
32  import java.io.InputStreamReader;
33  import java.io.Reader;
34  import java.net.MalformedURLException;
35  import java.net.URL;
36  import java.util.Calendar;
37  import java.util.HashMap;
38  import java.util.Map;
39  import java.util.StringTokenizer;
40  
41  public class ResultsManager {
42  
43      public static final String SEPARATOR = "=";
44  
45      public static final String EXT_JU = "WXP";
46  
47      public static final String COMMENT = "#";
48  
49      private final String wxpFileName;
50  
51      private final Map<String, ExitCode> files;
52  
53      private final boolean save;
54  
55      public ResultsManager(final String wxpFileName, final boolean save)
56              throws MalformedURLException, IOException {
57  
58          this.wxpFileName = wxpFileName;
59          this.save = save;
60          files = getInformations(wxpFileName);
61      }
62  
63      public final ResultCode compare(final String fileName,
64              final ExitCode newCode) {
65  
66          final ExitCode tmp = files.get(fileName);
67          final ExitCode oldCode = tmp == null ? ExitCode.UNKNOWN : tmp;
68          final ResultCode resultCode = computeResultCode(oldCode, newCode);
69  
70          if (save && resultCode.equals(ResultCode.UPDATED)) {
71              files.put(fileName, newCode);
72          }
73  
74          return resultCode;
75      }
76  
77      public final void save() {
78  
79          save(wxpFileName);
80      }
81  
82      public String[] getFiles() {
83          return files.keySet().toArray(new String[files.keySet().size()]);
84      }
85  
86      public final void save(final String wxpFileName) {
87          FileWriter fw = null;
88          try {
89              fw = new FileWriter(wxpFileName);
90              fw.write(getFileDescription());
91             
92          } catch (IOException e) {
93              e.printStackTrace();
94          } finally {
95              if (fw!=null) {
96                  try {
97                      fw.close();
98                  } catch (IOException e) {
99                      e.printStackTrace();
100                 }
101             }
102         }
103     }
104 
105     private final String getFileDescription() {
106         final StringBuffer sb = new StringBuffer("#Evaluation : ");
107 
108         sb.append("\n\n");
109 
110         for (String file : files.keySet()) {
111             sb.append(file);
112             sb.append(ResultsManager.SEPARATOR);
113             sb.append(files.get(file));
114             sb.append('\n');
115         }
116 
117         sb.append("\n\n#Evaluation END");
118         return sb.toString();
119     }
120 
121     private final ResultCode computeResultCode(final ExitCode oldS,
122             final ExitCode newS) {
123         if (oldS.equals(newS)) {
124             return ResultCode.OK;
125         }
126 
127         if ((ExitCode.UNKNOWN.equals(oldS))
128                 && ((ExitCode.UNSATISFIABLE.equals(newS) || (ExitCode.SATISFIABLE
129                         .equals(newS))))) {
130             return ResultCode.UPDATED;
131         }
132 
133         if (((ExitCode.UNSATISFIABLE.equals(oldS) || (ExitCode.SATISFIABLE
134                 .equals(oldS))))
135                 && (ExitCode.UNKNOWN.equals(newS))) {
136             return ResultCode.WARNING;
137         }
138 
139         if (((ExitCode.SATISFIABLE.equals(oldS)) && (ExitCode.UNSATISFIABLE
140                 .equals(newS)))
141                 || ((ExitCode.UNSATISFIABLE.equals(oldS)) && (ExitCode.SATISFIABLE
142                         .equals(newS)))) {
143             return ResultCode.KO;
144         }
145 
146         return ResultCode.UNKNOWN;
147     }
148 
149     public static final Map<String, ExitCode> getInformations(final URL path)
150             throws IOException {
151 
152         return getInformations(new InputStreamReader(path.openStream()));
153     }
154 
155     public static final Map<String, ExitCode> getInformations(final String path)
156             throws MalformedURLException, IOException {
157 
158         if (path.startsWith("http://")) {
159             return getInformations(new URL(path));
160         }
161         return getInformations(new FileReader(path));
162     }
163 
164     public static final Map<String, ExitCode> getInformations(final Reader in) {
165         final BufferedReader br = new BufferedReader(in);
166 
167         final Map<String, ExitCode> ci = new HashMap<String, ExitCode>();
168         StringTokenizer tokens;
169         String line = null;
170         int cpt = 1;
171 
172         try {
173 
174             line = br.readLine();
175             for (; line != null; cpt++) {
176 
177                 if ((!"".equals(line.trim()))
178                         && (!(line.trim()).startsWith(COMMENT))) {
179                     tokens = new StringTokenizer(line, SEPARATOR);
180 
181                     if (tokens.countTokens() == 2) {
182                         ci.put(tokens.nextToken(), ExitCode.valueOf(tokens
183                                 .nextToken()));
184                     } else {
185                         throw new IllegalArgumentException();
186                     }
187 
188                 }
189                 line = br.readLine();
190             }
191         } catch (IllegalArgumentException i) {
192             System.err.println("File Parsing Error in line " + cpt
193                     + "\nError caused by : " + line);
194         } catch (IOException e) {
195             e.printStackTrace();
196             return null;
197         }
198 
199         return ci;
200     }
201 
202     public static final String printLine(final String fileName,
203             final ExitCode exitCode, final ResultCode resultCode) {
204         return fileName + SEPARATOR + exitCode.toString() + " ["
205                 + resultCode.toString() + "]";
206     }
207 
208     public static final String createPath() {
209         final StringBuffer sb = new StringBuffer("Eval_");
210         sb.append(Calendar.getInstance().getTime().toString().replace(" ", "_")
211                 .replace(":", "_"));
212         // sb.append('.');
213         // sb.append(EXT_JU.toLowerCase());
214         return sb.toString();
215     }
216 
217 }