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.tools;
29  
30  import java.io.ObjectInputStream;
31  import java.io.PrintWriter;
32  
33  import org.sat4j.specs.ContradictionException;
34  import org.sat4j.specs.IConstr;
35  import org.sat4j.specs.IVecInt;
36  import org.sat4j.specs.IteratorInt;
37  
38  /**
39   * Solver used to display in a writer the CNF instance in Dimacs format.
40   * 
41   * That solver is useful to produce CNF files to be used by third party solvers.
42   * 
43   * @author leberre
44   * 
45   */
46  public class DimacsOutputSolver extends AbstractOutputSolver {
47  
48  	/**
49       * 
50       */
51  	private static final long serialVersionUID = 1L;
52  
53  	private transient PrintWriter out;
54  
55  	public DimacsOutputSolver() {
56  		this(new PrintWriter(System.out, true));
57  	}
58  
59  	public DimacsOutputSolver(PrintWriter pw) {
60  		out = pw;
61  	}
62  
63  	private void readObject(ObjectInputStream stream) {
64  		out = new PrintWriter(System.out, true);
65  	}
66  
67  	public int newVar() {
68  		return 0;
69  	}
70  
71  	public int newVar(int howmany) {
72  		out.print("p cnf " + howmany);
73  		nbvars = howmany;
74  		return 0;
75  	}
76  
77  	public void setExpectedNumberOfClauses(int nb) {
78  		out.println(" " + nb);
79  		nbclauses = nb;
80  		fixedNbClauses = true;
81  	}
82  
83  	public IConstr addClause(IVecInt literals) throws ContradictionException {
84  		if (firstConstr) {
85  			if (!fixedNbClauses) {
86  				out.println(" XXXXXX");
87  			}
88  			firstConstr = false;
89  		}
90  		for (IteratorInt iterator = literals.iterator(); iterator.hasNext();) {
91  			out.print(iterator.next() + " ");
92  		}
93  		out.println("0");
94  		return null;
95  	}
96  
97  	public IConstr addAtMost(IVecInt literals, int degree)
98  			throws ContradictionException {
99  		if (degree > 1) {
100 			throw new UnsupportedOperationException(
101 					"Not a clausal problem! degree " + degree);
102 		}
103 		assert degree == 1;
104 		if (firstConstr) {
105 			if (!fixedNbClauses) {
106 				out.println("XXXXXX");
107 			}
108 			firstConstr = false;
109 		}
110 		for (int i = 0; i <= literals.size(); i++) {
111 			for (int j = i + 1; j < literals.size(); j++) {
112 				out.println("" + (-literals.get(i)) + " " + (-literals.get(j))
113 						+ " 0");
114 			}
115 		}
116 		return null;
117 	}
118 
119 	public IConstr addAtLeast(IVecInt literals, int degree)
120 			throws ContradictionException {
121 		if (degree > 1) {
122 			throw new UnsupportedOperationException(
123 					"Not a clausal problem! degree " + degree);
124 		}
125 		assert degree == 1;
126 		return addClause(literals);
127 	}
128 
129 	public IConstr addExactly(IVecInt literals, int n)
130 			throws ContradictionException {
131 		if (n > 1) {
132 			throw new UnsupportedOperationException(
133 					"Not a clausal problem! degree " + n);
134 		}
135 		assert n == 1;
136 		addAtMost(literals, n);
137 		addAtLeast(literals, n);
138 		return null;
139 	}
140 
141 	public void reset() {
142 		fixedNbClauses = false;
143 		firstConstr = true;
144 
145 	}
146 
147 	public String toString(String prefix) {
148 		return "Dimacs output solver";
149 	}
150 
151 	public int nConstraints() {
152 		return nbclauses;
153 	}
154 
155 	public int nVars() {
156 		return nbvars;
157 	}
158 
159 	/**
160 	 * @since 2.1
161 	 */
162 	public int nextFreeVarId(boolean reserve) {
163 		if (reserve) {
164 			return ++nbvars;
165 		}
166 		return nbvars + 1;
167 	}
168 
169 	/**
170 	 * @since 2.3.1
171 	 */
172 	public int[] modelWithInternalVariables() {
173 		throw new UnsupportedOperationException();
174 	}
175 
176 	/**
177 	 * @since 2.3.1
178 	 */
179 	public int realNumberOfVariables() {
180 		return nbvars;
181 	}
182 
183 	/**
184 	 * @since 2.3.1
185 	 */
186 	public void registerLiteral(int p) {
187 		throw new UnsupportedOperationException();
188 	}
189 }