Clover coverage report -
Coverage timestamp: jeu. sept. 29 2005 23:57:39 CEST
file stats: LOC: 208   Methods: 9
NCLOC: 138   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LecteurDimacs.java 71,4% 85% 88,9% 80,9%
coverage coverage
 1    /*
 2    * SAT4J: a SATisfiability library for Java
 3    * Copyright (C) 2004 Daniel Le Berre
 4    *
 5    * Based on the original minisat specification from:
 6    *
 7    * An extensible SAT solver. Niklas E?n and Niklas S?rensson.
 8    * Proceedings of the Sixth International Conference on Theory
 9    * and Applications of Satisfiability Testing, LNCS 2919,
 10    * pp 502-518, 2003.
 11    *
 12    * This library is free software; you can redistribute it and/or
 13    * modify it under the terms of the GNU Lesser General Public
 14    * License as published by the Free Software Foundation; either
 15    * version 2.1 of the License, or (at your option) any later version.
 16    *
 17    * This library is distributed in the hope that it will be useful,
 18    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 19    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 20    * Lesser General Public License for more details.
 21    *
 22    * You should have received a copy of the GNU Lesser General Public
 23    * License along with this library; if not, write to the Free Software
 24    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 25    *
 26    */
 27   
 28    package org.sat4j.reader;
 29   
 30    import java.io.BufferedInputStream;
 31    import java.io.FileInputStream;
 32    import java.io.IOException;
 33    import java.io.Serializable;
 34    import java.util.zip.GZIPInputStream;
 35   
 36    import org.sat4j.core.VecInt;
 37    import org.sat4j.specs.ContradictionException;
 38    import org.sat4j.specs.IProblem;
 39    import org.sat4j.specs.ISolver;
 40    import org.sat4j.specs.IVecInt;
 41   
 42    /**
 43    * Dimacs Reader written by Frederic Laihem. It is much faster that
 44    * DimacsReader.
 45    *
 46    * @author leberre
 47    *
 48    */
 49    public class LecteurDimacs implements Reader, Serializable {
 50   
 51    private static final long serialVersionUID = 1L;
 52   
 53    /* taille du buffer */
 54    private final static int TAILLE_BUF = 16384;
 55   
 56    private ISolver s;
 57   
 58    private transient BufferedInputStream in;
 59   
 60    /* nombre de literaux dans le fichier */
 61    private int nbLit = 0;
 62   
 63    private static final char EOF = (char) -1;
 64   
 65    /*
 66    * nomFichier repr?sente le nom du fichier ? lire
 67    */
 68  2026 public LecteurDimacs(ISolver s) {
 69  2026 this.s = s;
 70    }
 71   
 72    /**
 73    * lit la base de clauses et la met dans le vecteur donn? en param?tre
 74    */
 75  1646 public IProblem parseInstance(String nomFichier) throws IOException,
 76    ContradictionException {
 77  1646 if (nomFichier.endsWith(".gz")) {
 78  0 in = new BufferedInputStream(new GZIPInputStream(
 79    new FileInputStream(nomFichier), TAILLE_BUF));
 80    } else {
 81  1646 in = new BufferedInputStream(new FileInputStream(nomFichier),
 82    TAILLE_BUF);
 83    }
 84  1646 s.reset();
 85  1646 char car = passerCommentaire();
 86  1646 if (nbLit == 0)
 87  0 throw new IOException(
 88    "DIMACS non valide (nombre de Literaux non valide)");
 89  1646 s.newVar(nbLit);
 90  1646 car = passerEspaces();
 91  1646 if (car == EOF)
 92  0 throw new IOException("DIMACS non valide (o? sont les clauses ?)");
 93  1646 ajouterClauses(car);
 94  1646 in.close();
 95  1646 return s;
 96    }
 97   
 98    /** on passe les commentaires et on lit le nombre de literaux */
 99  1646 private char passerCommentaire() throws IOException {
 100  1646 char car;
 101  1646 for (;;) {
 102  22406 car = passerEspaces();
 103  22406 if (car == 'p') {
 104  1646 car = lectureNombreLiteraux();
 105    }
 106  22406 if (car != 'c' && car != 'p')
 107  1646 break; /* fin des commentaires */
 108  20760 car = nextLine(); /* on passe la ligne de commentaire */
 109  20760 if (car == EOF)
 110  0 break;
 111    }
 112  1646 return car;
 113    }
 114   
 115    /** lit le nombre repr?sentant le nombre de literaux */
 116  1646 private char lectureNombreLiteraux() throws IOException {
 117  1646 char car = nextChiffre(); /* on lit le prchain chiffre */
 118  1646 if (car != EOF) {
 119  1646 nbLit = car - '0';
 120  1646 for (;;) { /* on lit le chiffre repr?sentant le nombre de literaux */
 121  4556 car = (char) in.read();
 122  4556 if (car < '0' || car > '9')
 123  1646 break;
 124  2910 nbLit = 10 * nbLit + (car - '0');
 125    }
 126  1646 if (car != EOF)
 127  1646 nextLine(); /* on lit la fin de la ligne */
 128    }
 129  1646 return car;
 130    }
 131   
 132    /** lit les clauses et les ajoute dans le vecteur donn? en param?tre */
 133  1646 private void ajouterClauses(char car) throws IOException,
 134    ContradictionException {
 135  1646 final IVecInt lit = new VecInt();
 136  1646 int val = 0;
 137  1646 boolean neg = false;
 138  1646 for (;;) {
 139    /* on lit le signe du literal */
 140  16306090 if (car == '-') {
 141  7207708 neg = true;
 142  7207708 car = (char) in.read();
 143  9098382 } else if (car == '+')
 144  0 car = (char) in.read();
 145    else /* on le 1er chiffre du literal */
 146  9098382 if (car >= '0' && car <= '9') {
 147  9098382 val = car - '0';
 148  9098382 car = (char) in.read();
 149    } else
 150  0 break;
 151    /* on lit la suite du literal */
 152  16306090 while (car >= '0' && car <= '9') {
 153  25361960 val = (val * 10) + car - '0';
 154  25361960 car = (char) in.read();
 155    }
 156  16306090 if (val == 0) { // on a lu toute la clause
 157  4000706 s.addClause(lit);
 158  4000706 lit.clear();
 159    } else {
 160    /* on ajoute le literal au vecteur */
 161    // s.newVar(val-1);
 162  12305384 lit.push(neg ? -val : val);
 163  12305384 neg = false;
 164  12305384 val = 0; /* on reinitialise les variables */
 165    }
 166  16306090 if (car != EOF)
 167  16305706 car = passerEspaces();
 168  16306090 if (car == EOF)
 169  1646 break; /* on a lu tout le fichier */
 170    }
 171    }
 172   
 173    /** passe tout les caract?res d'espacement (espace ou \n) */
 174  16329758 private char passerEspaces() throws IOException {
 175  16329758 char car;
 176  ? while ((car = (char) in.read()) == ' ' || car == '\n')
 177    ;
 178  16329758 return car;
 179    }
 180   
 181    /** passe tout les caract?res jusqu? rencontrer une fin de la ligne */
 182  22406 private char nextLine() throws IOException {
 183  22406 char car;
 184  22406 do {
 185  555738 car = (char) in.read();
 186  555738 } while ((car != '\n') && (car != EOF));
 187  22406 return car;
 188    }
 189   
 190    /** passe tout les caract?re jusqu'? rencontrer un chiffre */
 191  1646 private char nextChiffre() throws IOException {
 192  1646 char car;
 193  1646 do {
 194  9876 car = (char) in.read();
 195  9876 } while ((car < '0') || (car > '9') && (car != EOF));
 196  1646 return car;
 197    }
 198   
 199  0 public String decode(int[] model) {
 200  0 StringBuffer stb = new StringBuffer();
 201  0 for (int i = 0; i < model.length; i++) {
 202  0 stb.append(model[i]);
 203  0 stb.append(" ");
 204    }
 205  0 stb.append("0");
 206  0 return stb.toString();
 207    }
 208    }