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.reader;
29  
30  import java.io.IOException;
31  import java.io.LineNumberReader;
32  import java.util.Scanner;
33  import java.util.StringTokenizer;
34  
35  import org.sat4j.specs.ContradictionException;
36  import org.sat4j.specs.ISolver;
37  import org.sat4j.specs.IVecInt;
38  import org.sat4j.tools.GateTranslator;
39  
40  /**
41   * Reader for the Extended Dimacs format proposed by Fahiem Bacchus and Toby
42   * Walsh.
43   * 
44   * @author leberre
45   * 
46   */
47  public class ExtendedDimacsReader extends DimacsReader {
48  
49      public static final int FALSE = 1;
50  
51      public static final int TRUE = 2;
52  
53      public static final int NOT = 3;
54  
55      public static final int AND = 4;
56  
57      public static final int NAND = 5;
58  
59      public static final int OR = 6;
60  
61      public static final int NOR = 7;
62  
63      public static final int XOR = 8;
64  
65      public static final int XNOR = 9;
66  
67      public static final int IMPLIES = 10;
68  
69      public static final int IFF = 11;
70  
71      public static final int IFTHENELSE = 12;
72  
73      public static final int ATLEAST = 13;
74  
75      public static final int ATMOST = 14;
76  
77      public static final int COUNT = 15;
78  
79      /**
80       * 
81       * 
82       */
83      private static final long serialVersionUID = 1L;
84  
85      public ExtendedDimacsReader(ISolver solver) {
86          super(new GateTranslator(solver));
87      }
88  
89      /**
90       * @param in
91       *            the input stream
92       * @throws IOException
93       *             iff an IO occurs
94       * @throws ParseFormatException
95       *             if the input stream does not comply with the DIMACS format.
96       */
97      @Override
98      protected void readProblemLine(LineNumberReader in) throws IOException,
99              ParseFormatException {
100 
101         String line = in.readLine();
102 
103         if (line == null) {
104             throw new ParseFormatException(
105                     "premature end of file: <p noncnf ...> expected  on line "
106                             + in.getLineNumber());
107         }
108         StringTokenizer stk = new StringTokenizer(line);
109 
110         if (!(stk.hasMoreTokens() && stk.nextToken().equals("p")
111                 && stk.hasMoreTokens() && stk.nextToken().equals("noncnf"))) {
112             throw new ParseFormatException(
113                     "problem line expected (p noncnf ...) on line "
114                             + in.getLineNumber());
115         }
116 
117         int vars;
118 
119         // reads the max var id
120         vars = Integer.parseInt(stk.nextToken());
121         assert vars > 0;
122         solver.newVar(vars);
123         try {
124             ((GateTranslator)solver).gateTrue(vars);
125         } catch (ContradictionException e) {
126             throw new ParseFormatException("Contradiction when asserting root variable?");
127         }
128         disableNumberOfConstraintCheck();
129     }
130 
131     /*
132      * (non-Javadoc)
133      * 
134      * @see org.sat4j.reader.DimacsReader#handleConstr(java.lang.String,
135      *      org.sat4j.specs.IVecInt)
136      */
137     @Override
138     protected boolean handleConstr(String line, IVecInt literals)
139             throws ContradictionException {
140         boolean added = true;
141         assert literals.size() == 0;
142         Scanner scan = new Scanner(line);
143         GateTranslator gater = (GateTranslator)solver;
144         while (scan.hasNext()) {
145             int gateType = scan.nextInt();
146             assert gateType > 0;
147             int nbparam = scan.nextInt();
148             assert nbparam != 0;
149             assert nbparam == -1 || gateType >= ATLEAST;
150             for (int i = 0; i < nbparam; i++) {
151                 scan.nextInt();
152             }
153             // readI/O until reaching ending 0
154             int y = scan.nextInt();
155             int x;
156             while ((x = scan.nextInt()) != 0) {
157                 literals.push(x);
158             }
159             switch (gateType) {
160             case FALSE:
161                 assert literals.size()==0;
162                 gater.gateFalse(y);
163                 break;
164             case TRUE:
165                 assert literals.size()==0;
166                 gater.gateTrue(y);
167                 break;
168             case OR:
169                 gater.or(y, literals);
170                 break;
171             case NOT:
172                 assert literals.size()==1;
173                 gater.not(y, literals.get(0));
174                 break;
175             case AND:
176                 gater.and(y, literals);
177                 break;
178             case XOR:
179                 gater.xor(y, literals);
180                 break;
181             case IFF:
182                 gater.iff(y, literals);
183                 break;
184             case IFTHENELSE:
185                 assert literals.size()==3;
186                 gater.ite(y, literals.get(0),literals.get(1),literals.get(2));
187                 break;
188             default:
189                 throw new UnsupportedOperationException("Gate type " + gateType
190                         + " not handled yet");
191             }
192         }
193         literals.clear();
194         return added;
195     }
196 
197 
198 }