View Javadoc

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