Clover Coverage Report
Coverage timestamp: mer. juin 27 2007 07:27:16 CEST
64   217   25   5,33
22   147   0,56   12
12     3  
1    
 
  ResultsManager       Line # 41 64 25 0% 0.0
 
No Tests
 
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  0 toggle public ResultsManager(final String wxpFileName, final boolean save)
56    throws MalformedURLException, IOException {
57   
58  0 this.wxpFileName = wxpFileName;
59  0 this.save = save;
60  0 files = getInformations(wxpFileName);
61    }
62   
 
63  0 toggle public final ResultCode compare(final String fileName,
64    final ExitCode newCode) {
65   
66  0 final ExitCode tmp = files.get(fileName);
67  0 final ExitCode oldCode = tmp == null ? ExitCode.UNKNOWN : tmp;
68  0 final ResultCode resultCode = computeResultCode(oldCode, newCode);
69   
70  0 if (save && resultCode.equals(ResultCode.UPDATED)) {
71  0 files.put(fileName, newCode);
72    }
73   
74  0 return resultCode;
75    }
76   
 
77  0 toggle public final void save() {
78   
79  0 save(wxpFileName);
80    }
81   
 
82  0 toggle public String[] getFiles() {
83  0 return files.keySet().toArray(new String[files.keySet().size()]);
84    }
85   
 
86  0 toggle public final void save(final String wxpFileName) {
87  0 FileWriter fw = null;
88  0 try {
89  0 fw = new FileWriter(wxpFileName);
90  0 fw.write(getFileDescription());
91   
92    } catch (IOException e) {
93  0 e.printStackTrace();
94    } finally {
95  0 if (fw!=null) {
96  0 try {
97  0 fw.close();
98    } catch (IOException e) {
99  0 e.printStackTrace();
100    }
101    }
102    }
103    }
104   
 
105  0 toggle private final String getFileDescription() {
106  0 final StringBuffer sb = new StringBuffer("#Evaluation : ");
107   
108  0 sb.append("\n\n");
109   
110  0 for (String file : files.keySet()) {
111  0 sb.append(file);
112  0 sb.append(ResultsManager.SEPARATOR);
113  0 sb.append(files.get(file));
114  0 sb.append('\n');
115    }
116   
117  0 sb.append("\n\n#Evaluation END");
118  0 return sb.toString();
119    }
120   
 
121  0 toggle private final ResultCode computeResultCode(final ExitCode oldS,
122    final ExitCode newS) {
123  0 if (oldS.equals(newS)) {
124  0 return ResultCode.OK;
125    }
126   
127  0 if ((ExitCode.UNKNOWN.equals(oldS))
128    && ((ExitCode.UNSATISFIABLE.equals(newS) || (ExitCode.SATISFIABLE
129    .equals(newS))))) {
130  0 return ResultCode.UPDATED;
131    }
132   
133  0 if (((ExitCode.UNSATISFIABLE.equals(oldS) || (ExitCode.SATISFIABLE
134    .equals(oldS))))
135    && (ExitCode.UNKNOWN.equals(newS))) {
136  0 return ResultCode.WARNING;
137    }
138   
139  0 if (((ExitCode.SATISFIABLE.equals(oldS)) && (ExitCode.UNSATISFIABLE
140    .equals(newS)))
141    || ((ExitCode.UNSATISFIABLE.equals(oldS)) && (ExitCode.SATISFIABLE
142    .equals(newS)))) {
143  0 return ResultCode.KO;
144    }
145   
146  0 return ResultCode.UNKNOWN;
147    }
148   
 
149  0 toggle public static final Map<String, ExitCode> getInformations(final URL path)
150    throws IOException {
151   
152  0 return getInformations(new InputStreamReader(path.openStream()));
153    }
154   
 
155  0 toggle public static final Map<String, ExitCode> getInformations(final String path)
156    throws MalformedURLException, IOException {
157   
158  0 if (path.startsWith("http://")) {
159  0 return getInformations(new URL(path));
160    }
161  0 return getInformations(new FileReader(path));
162    }
163   
 
164  0 toggle public static final Map<String, ExitCode> getInformations(final Reader in) {
165  0 final BufferedReader br = new BufferedReader(in);
166   
167  0 final Map<String, ExitCode> ci = new HashMap<String, ExitCode>();
168  0 StringTokenizer tokens;
169  0 String line = null;
170  0 int cpt = 1;
171   
172  0 try {
173   
174  0 line = br.readLine();
175  0 for (; line != null; cpt++) {
176   
177  0 if ((!"".equals(line.trim()))
178    && (!(line.trim()).startsWith(COMMENT))) {
179  0 tokens = new StringTokenizer(line, SEPARATOR);
180   
181  0 if (tokens.countTokens() == 2) {
182  0 ci.put(tokens.nextToken(), ExitCode.valueOf(tokens
183    .nextToken()));
184    } else {
185  0 throw new IllegalArgumentException();
186    }
187   
188    }
189  0 line = br.readLine();
190    }
191    } catch (IllegalArgumentException i) {
192  0 System.err.println("File Parsing Error in line " + cpt
193    + "\nError caused by : " + line);
194    } catch (IOException e) {
195  0 e.printStackTrace();
196  0 return null;
197    }
198   
199  0 return ci;
200    }
201   
 
202  0 toggle public static final String printLine(final String fileName,
203    final ExitCode exitCode, final ResultCode resultCode) {
204  0 return fileName + SEPARATOR + exitCode.toString() + " ["
205    + resultCode.toString() + "]";
206    }
207   
 
208  0 toggle public static final String createPath() {
209  0 final StringBuffer sb = new StringBuffer("Eval_");
210  0 sb.append(Calendar.getInstance().getTime().toString().replace(" ", "_")
211    .replace(":", "_"));
212    // sb.append('.');
213    // sb.append(EXT_JU.toLowerCase());
214  0 return sb.toString();
215    }
216   
217    }