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  package org.sat4j;
26  
27  import java.io.FileNotFoundException;
28  import java.io.IOException;
29  import java.io.PrintWriter;
30  
31  import org.sat4j.minisat.SolverFactory;
32  import org.sat4j.reader.InstanceReader;
33  import org.sat4j.reader.ParseFormatException;
34  import org.sat4j.reader.Reader;
35  import org.sat4j.specs.ContradictionException;
36  import org.sat4j.specs.IProblem;
37  import org.sat4j.specs.ISolver;
38  import org.sat4j.specs.IVecInt;
39  import org.sat4j.specs.TimeoutException;
40  import org.sat4j.tools.RemiUtils;
41  import org.sat4j.tools.SolutionCounter;
42  
43  /**
44   * This is an example of use of the SAT4J library for computing the backbone of
45   * a CNF or to compute the number of solutions of a CNF. We do not claim that
46   * those tools are very efficient: they were simple to write and they helped us
47   * on small examples.
48   * 
49   * @author leberre
50   */
51  public class MoreThanSAT {
52  
53      /**
54       * This constructor is private to prevent people to use instances of that
55       * class.
56       * 
57       */
58      private MoreThanSAT() {
59          // to silent PMD audit
60      }
61  
62      public static void main(final String[] args) {
63          final ISolver solver = SolverFactory.newMiniLearning();
64          final SolutionCounter sc = new SolutionCounter(solver);
65          solver.setTimeout(3600); // 1 hour timeout
66          Reader reader = new InstanceReader(solver);
67  
68          // filename is given on the command line
69          try {
70              final IProblem problem = reader.parseInstance(args[0]);
71              if (problem.isSatisfiable()) {
72                  System.out.println(Messages.getString("MoreThanSAT.0")); //$NON-NLS-1$
73                  reader.decode(problem.model(), new PrintWriter(System.out));
74                  IVecInt backbone = RemiUtils.backbone(solver);
75                  System.out
76                          .println(Messages.getString("MoreThanSAT.1") + backbone); //$NON-NLS-1$
77                  System.out.println(Messages.getString("MoreThanSAT.2")); //$NON-NLS-1$
78                  System.out.println(Messages.getString("MoreThanSAT.3") //$NON-NLS-1$
79                          + sc.countSolutions());
80              } else {
81                  System.out.println(Messages.getString("MoreThanSAT.4")); //$NON-NLS-1$
82              }
83          } catch (FileNotFoundException e) {
84              e.printStackTrace();
85          } catch (ParseFormatException e) {
86              e.printStackTrace();
87          } catch (IOException e) {
88              e.printStackTrace();
89          } catch (ContradictionException e) {
90              System.out.println(Messages.getString("MoreThanSAT.5")); //$NON-NLS-1$
91          } catch (TimeoutException e) {
92              System.out.println(Messages.getString("MoreThanSAT.6")); //$NON-NLS-1$
93          }
94      }
95  }