| 
 1 | 
  
 | package org.sat4j; | 
| 
 2 | 
  
 |  | 
| 
 3 | 
  
 | import java.io.BufferedReader; | 
| 
 4 | 
  
 | import java.io.FileReader; | 
| 
 5 | 
  
 | import java.io.FileWriter; | 
| 
 6 | 
  
 | import java.io.IOException; | 
| 
 7 | 
  
 | import java.io.InputStreamReader; | 
| 
 8 | 
  
 | import java.io.Reader; | 
| 
 9 | 
  
 | import java.net.MalformedURLException; | 
| 
 10 | 
  
 | import java.net.URL; | 
| 
 11 | 
  
 | import java.util.Calendar; | 
| 
 12 | 
  
 | import java.util.HashMap; | 
| 
 13 | 
  
 | import java.util.Map; | 
| 
 14 | 
  
 | import java.util.StringTokenizer; | 
| 
 15 | 
  
 |  | 
| 
 16 | 
  
 | import org.sat4j.ExitCode; | 
| 
 17 | 
  
 |  | 
| 
 18 | 
  
 | public class ResultsManager { | 
| 
 19 | 
  
 |  | 
| 
 20 | 
  
 |     public static final String SEPARATOR = "="; | 
| 
 21 | 
  
 |  | 
| 
 22 | 
  
 |     public static final String EXT_JU = "WXP"; | 
| 
 23 | 
  
 |  | 
| 
 24 | 
  
 |     public static final String COMMENT = "#"; | 
| 
 25 | 
  
 |  | 
| 
 26 | 
  
 |     private final String wxpFileName; | 
| 
 27 | 
  
 |  | 
| 
 28 | 
  
 |     private final Map<String, ExitCode> files; | 
| 
 29 | 
  
 |  | 
| 
 30 | 
  
 |     private final transient boolean save; | 
| 
 31 | 
  
 |  | 
| 
 32 | 
 0
 |     public ResultsManager(final String wxpFileName, final boolean save)
 | 
| 
 33 | 
  
 |             throws MalformedURLException, IOException { | 
| 
 34 | 
  
 |  | 
| 
 35 | 
 0
 |         this.wxpFileName = wxpFileName;
 | 
| 
 36 | 
 0
 |         this.save = save;
 | 
| 
 37 | 
 0
 |         files = getInformations(wxpFileName);
 | 
| 
 38 | 
  
 |     } | 
| 
 39 | 
  
 |  | 
| 
 40 | 
 0
 |     public final ResultCode compare(final String fileName,
 | 
| 
 41 | 
  
 |             final ExitCode newCode) { | 
| 
 42 | 
  
 |  | 
| 
 43 | 
 0
 |         final ExitCode tmp = files.get(fileName);
 | 
| 
 44 | 
 0
 |         final ExitCode oldCode = tmp == null ? ExitCode.UNKNOWN : tmp;
 | 
| 
 45 | 
 0
 |         final ResultCode resultCode = computeResultCode(oldCode, newCode);
 | 
| 
 46 | 
  
 |  | 
| 
 47 | 
 0
 |         if (save && resultCode.equals(ResultCode.UPDATED)) {
 | 
| 
 48 | 
 0
 |             files.put(fileName, newCode);
 | 
| 
 49 | 
  
 |         } | 
| 
 50 | 
  
 |  | 
| 
 51 | 
 0
 |         return resultCode;
 | 
| 
 52 | 
  
 |     } | 
| 
 53 | 
  
 |  | 
| 
 54 | 
 0
 |     public final void save() throws IOException {
 | 
| 
 55 | 
  
 |  | 
| 
 56 | 
 0
 |         save(wxpFileName);
 | 
| 
 57 | 
  
 |     } | 
| 
 58 | 
  
 |  | 
| 
 59 | 
 0
 |     public String[] getFiles() {
 | 
| 
 60 | 
 0
 |         return (String[]) files.keySet().toArray(new String[0]);
 | 
| 
 61 | 
  
 |     } | 
| 
 62 | 
  
 |  | 
| 
 63 | 
 0
 |     public final void save(final String wxpFileName) throws IOException {
 | 
| 
 64 | 
  
 |  | 
| 
 65 | 
 0
 |         final FileWriter fw = new FileWriter(wxpFileName);
 | 
| 
 66 | 
 0
 |         fw.write(getFileDescription());
 | 
| 
 67 | 
 0
 |         fw.close();
 | 
| 
 68 | 
  
 |     } | 
| 
 69 | 
  
 |  | 
| 
 70 | 
 0
 |     private final String getFileDescription() {
 | 
| 
 71 | 
 0
 |         final StringBuffer sb = new StringBuffer("#Evaluation : ");
 | 
| 
 72 | 
  
 |  | 
| 
 73 | 
 0
 |         sb.append("\n\n");
 | 
| 
 74 | 
  
 |  | 
| 
 75 | 
 0
 |         for (String file : files.keySet()) {
 | 
| 
 76 | 
 0
 |             sb.append(file);
 | 
| 
 77 | 
 0
 |             sb.append(ResultsManager.SEPARATOR);
 | 
| 
 78 | 
 0
 |             sb.append(files.get(file));
 | 
| 
 79 | 
 0
 |             sb.append('\n');
 | 
| 
 80 | 
  
 |         } | 
| 
 81 | 
  
 |  | 
| 
 82 | 
 0
 |         sb.append("\n\n#Evaluation END");
 | 
| 
 83 | 
 0
 |         return sb.toString();
 | 
| 
 84 | 
  
 |     } | 
| 
 85 | 
  
 |  | 
| 
 86 | 
 0
 |     private final ResultCode computeResultCode(final ExitCode oldS,
 | 
| 
 87 | 
  
 |             final ExitCode newS) { | 
| 
 88 | 
 0
 |         if (oldS.equals(newS)) {
 | 
| 
 89 | 
 0
 |             return ResultCode.OK;
 | 
| 
 90 | 
  
 |         } | 
| 
 91 | 
  
 |  | 
| 
 92 | 
 0
 |         if ((ExitCode.UNKNOWN.equals(oldS))
 | 
| 
 93 | 
  
 |                 && ((ExitCode.UNSATISFIABLE.equals(newS) || (ExitCode.SATISFIABLE | 
| 
 94 | 
  
 |                         .equals(newS))))) { | 
| 
 95 | 
 0
 |             return ResultCode.UPDATED;
 | 
| 
 96 | 
  
 |         } | 
| 
 97 | 
  
 |  | 
| 
 98 | 
 0
 |         if (((ExitCode.UNSATISFIABLE.equals(oldS) || (ExitCode.SATISFIABLE
 | 
| 
 99 | 
  
 |                 .equals(oldS)))) | 
| 
 100 | 
  
 |                 && (ExitCode.UNKNOWN.equals(newS))) { | 
| 
 101 | 
 0
 |             return ResultCode.WARNING;
 | 
| 
 102 | 
  
 |         } | 
| 
 103 | 
  
 |  | 
| 
 104 | 
 0
 |         if (((ExitCode.SATISFIABLE.equals(oldS)) && (ExitCode.UNSATISFIABLE
 | 
| 
 105 | 
  
 |                 .equals(newS))) | 
| 
 106 | 
  
 |                 || ((ExitCode.UNSATISFIABLE.equals(oldS)) && (ExitCode.SATISFIABLE | 
| 
 107 | 
  
 |                         .equals(newS)))) { | 
| 
 108 | 
 0
 |             return ResultCode.KO;
 | 
| 
 109 | 
  
 |         } | 
| 
 110 | 
  
 |  | 
| 
 111 | 
 0
 |         return ResultCode.UNKNOWN;
 | 
| 
 112 | 
  
 |     } | 
| 
 113 | 
  
 |  | 
| 
 114 | 
 0
 |     public static final Map<String, ExitCode> getInformations(final URL path)
 | 
| 
 115 | 
  
 |             throws IOException { | 
| 
 116 | 
  
 |  | 
| 
 117 | 
 0
 |         return getInformations(new InputStreamReader(path.openStream()));
 | 
| 
 118 | 
  
 |     } | 
| 
 119 | 
  
 |  | 
| 
 120 | 
 0
 |     public static final Map<String, ExitCode> getInformations(final String path)
 | 
| 
 121 | 
  
 |             throws MalformedURLException, IOException { | 
| 
 122 | 
  
 |  | 
| 
 123 | 
 0
 |         if (path.startsWith("http://")) {
 | 
| 
 124 | 
 0
 |             return getInformations(new URL(path));
 | 
| 
 125 | 
  
 |         } | 
| 
 126 | 
 0
 |         return getInformations(new FileReader(path));
 | 
| 
 127 | 
  
 |     } | 
| 
 128 | 
  
 |  | 
| 
 129 | 
 0
 |     public static final Map<String, ExitCode> getInformations(final Reader in) {
 | 
| 
 130 | 
 0
 |         final BufferedReader br = new BufferedReader(in);
 | 
| 
 131 | 
  
 |  | 
| 
 132 | 
 0
 |         final Map<String, ExitCode> ci = new HashMap<String, ExitCode>();
 | 
| 
 133 | 
 0
 |         StringTokenizer tokens;
 | 
| 
 134 | 
 0
 |         String line = null;
 | 
| 
 135 | 
 0
 |         int cpt = 1;
 | 
| 
 136 | 
  
 |  | 
| 
 137 | 
 0
 |         try {
 | 
| 
 138 | 
  
 |  | 
| 
 139 | 
 0
 |             line = br.readLine();
 | 
| 
 140 | 
 0
 |             for (; line != null; cpt++) {
 | 
| 
 141 | 
  
 |  | 
| 
 142 | 
 0
 |                 if ((!"".equals(line.trim()))
 | 
| 
 143 | 
  
 |                         && (!(line.trim()).startsWith(COMMENT))) { | 
| 
 144 | 
 0
 |                     tokens = new StringTokenizer(line, SEPARATOR);
 | 
| 
 145 | 
  
 |  | 
| 
 146 | 
 0
 |                     if (tokens.countTokens() == 2) {
 | 
| 
 147 | 
 0
 |                         ci.put(tokens.nextToken(), ExitCode.valueOf(tokens
 | 
| 
 148 | 
  
 |                                 .nextToken())); | 
| 
 149 | 
  
 |                     } else { | 
| 
 150 | 
 0
 |                         throw new IllegalArgumentException();
 | 
| 
 151 | 
  
 |                     } | 
| 
 152 | 
  
 |  | 
| 
 153 | 
  
 |                 } | 
| 
 154 | 
 0
 |                 line = br.readLine();
 | 
| 
 155 | 
  
 |             } | 
| 
 156 | 
  
 |         } catch (IllegalArgumentException i) { | 
| 
 157 | 
 0
 |             System.err.println("File Parsing Error in line " + cpt
 | 
| 
 158 | 
  
 |                     + "\nError caused by : " + line); | 
| 
 159 | 
  
 |         } catch (IOException e) { | 
| 
 160 | 
 0
 |             e.printStackTrace();
 | 
| 
 161 | 
 0
 |             return null;
 | 
| 
 162 | 
  
 |         } | 
| 
 163 | 
  
 |  | 
| 
 164 | 
 0
 |         return ci;
 | 
| 
 165 | 
  
 |     } | 
| 
 166 | 
  
 |  | 
| 
 167 | 
 0
 |     public static final String printLine(final String fileName,
 | 
| 
 168 | 
  
 |             final ExitCode exitCode, final ResultCode resultCode) { | 
| 
 169 | 
 0
 |         return fileName + SEPARATOR + exitCode.toString() + " ["
 | 
| 
 170 | 
  
 |                 + resultCode.toString() + "]"; | 
| 
 171 | 
  
 |     } | 
| 
 172 | 
  
 |  | 
| 
 173 | 
 0
 |     public static final String createPath() {
 | 
| 
 174 | 
 0
 |         final StringBuffer sb = new StringBuffer("Eval_");
 | 
| 
 175 | 
 0
 |         sb.append(Calendar.getInstance().getTime().toString().replace(" ", "_")
 | 
| 
 176 | 
  
 |                 .replace(":", "_")); | 
| 
 177 | 
  
 |          | 
| 
 178 | 
  
 |          | 
| 
 179 | 
 0
 |         return sb.toString();
 | 
| 
 180 | 
  
 |     } | 
| 
 181 | 
  
 |  | 
| 
 182 | 
  
 | } |