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 org.sat4j.core.VecInt;
31  import org.sat4j.specs.ContradictionException;
32  import org.sat4j.specs.ISolver;
33  import org.sat4j.specs.IVecInt;
34  
35  /**
36   * Reader for the Extended Dimacs format proposed by Fahiem Bacchus and Toby
37   * Walsh in array representation (without the terminating 0).
38   * 
39   * Adaptation of org.sat4j.reader.ExtendedDimacsReader.
40   * 
41   * @author leberre
42   * @author fuhs
43   */
44  public class ExtendedDimacsArrayReader extends DimacsArrayReader {
45  
46      public static final int FALSE = 1;
47  
48      public static final int TRUE = 2;
49  
50      public static final int NOT = 3;
51  
52      public static final int AND = 4;
53  
54      public static final int NAND = 5;
55  
56      public static final int OR = 6;
57  
58      public static final int NOR = 7;
59  
60      public static final int XOR = 8;
61  
62      public static final int XNOR = 9;
63  
64      public static final int IMPLIES = 10;
65  
66      public static final int IFF = 11;
67  
68      public static final int IFTHENELSE = 12;
69  
70      public static final int ATLEAST = 13;
71  
72      public static final int ATMOST = 14;
73  
74      public static final int COUNT = 15;
75  
76      private static final long serialVersionUID = 1L;
77  
78      private final GateTranslator gater;
79      
80      public ExtendedDimacsArrayReader(ISolver solver) {
81          super(solver);
82          gater = new GateTranslator(solver);
83      }
84  
85      /**
86       * Handles a single constraint (constraint == Extended Dimacs circuit gate).
87       * 
88       * @param gateType
89       *            the type of the gate in question
90       * @param output
91       *            the number of the output of the gate in question
92       * @param inputs
93       *            the numbers of the inputs of the gates in question; the array
94       *            must have the corresponding length for the gate type unless
95       *            arbitrary lengths are allowed (i.e., 0 for TRUE and FALSE,
96       *            1 for NOT, or 3 for ITE)
97       * @return true
98       */
99      @Override
100     protected boolean handleConstr(int gateType, int output, int[] inputs)
101             throws ContradictionException {
102         IVecInt literals;
103         switch (gateType) {
104         case FALSE:
105             assert inputs.length == 0;
106             gater.gateFalse(output);
107             break;
108         case TRUE:
109             assert inputs.length == 0;
110             gater.gateTrue(output);
111             break;
112         case OR:
113             literals = new VecInt(inputs);
114             gater.or(output, literals);
115             break;
116         case NOT:
117             assert inputs.length == 1;
118             gater.not(output, inputs[0]);
119             break;
120         case AND:
121             literals = new VecInt(inputs);
122             gater.and(output, literals);
123             break;
124         case XOR:
125             literals = new VecInt(inputs);
126             gater.xor(output, literals);
127             break;
128         case IFF:
129             literals = new VecInt(inputs);
130             gater.iff(output, literals);
131             break;
132         case IFTHENELSE:
133             assert inputs.length == 3;
134             gater.ite(output, inputs[0], inputs[1], inputs[2]);
135             break;
136         default:
137             throw new UnsupportedOperationException("Gate type " + gateType
138                     + " not handled yet");
139         }
140         return true;
141     }
142 }