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