View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    *  http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   *
19   * Based on the original MiniSat specification from:
20   *
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   *
27   * Contributors:
28   *   CRIL - initial API and implementation
29   *******************************************************************************/
30  package org.sat4j;
31  
32  import java.io.FileNotFoundException;
33  import java.io.IOException;
34  import java.io.PrintWriter;
35  
36  import org.sat4j.minisat.SolverFactory;
37  import org.sat4j.reader.InstanceReader;
38  import org.sat4j.reader.ParseFormatException;
39  import org.sat4j.reader.Reader;
40  import org.sat4j.specs.ContradictionException;
41  import org.sat4j.specs.IProblem;
42  import org.sat4j.specs.ISolver;
43  import org.sat4j.specs.IVecInt;
44  import org.sat4j.specs.TimeoutException;
45  import org.sat4j.tools.RemiUtils;
46  import org.sat4j.tools.SolutionCounter;
47  
48  /**
49   * This is an example of use of the SAT4J library for computing the backbone of
50   * a CNF or to compute the number of solutions of a CNF. We do not claim that
51   * those tools are very efficient: they were simple to write and they helped us
52   * on small examples.
53   * 
54   * @author leberre
55   */
56  public final class MoreThanSAT {
57  
58      /**
59       * This constructor is private to prevent people to use instances of that
60       * class.
61       * 
62       */
63      private MoreThanSAT() {
64          // to silent PMD audit
65      }
66  
67      public static void main(final String[] args) {
68          final ISolver solver = SolverFactory.newDefault();
69          final SolutionCounter sc = new SolutionCounter(solver);
70          solver.setTimeout(3600); // 1 hour timeout
71          Reader reader = new InstanceReader(solver);
72  
73          // filename is given on the command line
74          try {
75              final IProblem problem = reader.parseInstance(args[0]);
76              if (problem.isSatisfiable()) {
77                  System.out.println(Messages.getString("MoreThanSAT.0")); //$NON-NLS-1$
78                  reader.decode(problem.model(), new PrintWriter(System.out));
79                  IVecInt backbone = RemiUtils.backbone(solver);
80                  System.out
81                          .println(Messages.getString("MoreThanSAT.1") + backbone); //$NON-NLS-1$
82                  System.out.println(Messages.getString("MoreThanSAT.2")); //$NON-NLS-1$
83                  System.out.println(Messages.getString("MoreThanSAT.3") //$NON-NLS-1$
84                          + sc.countSolutions());
85              } else {
86                  System.out.println(Messages.getString("MoreThanSAT.4")); //$NON-NLS-1$
87              }
88          } catch (FileNotFoundException e) {
89              e.printStackTrace();
90          } catch (ParseFormatException e) {
91              e.printStackTrace();
92          } catch (IOException e) {
93              e.printStackTrace();
94          } catch (ContradictionException e) {
95              System.out.println(Messages.getString("MoreThanSAT.5")); //$NON-NLS-1$
96          } catch (TimeoutException e) {
97              System.out.println(Messages.getString("MoreThanSAT.6")); //$NON-NLS-1$
98          }
99      }
100 }