1 /*
2 * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le Berre
3 *
4 * Based on the original minisat specification from:
5 *
6 * An extensible SAT solver. Niklas E?n and Niklas S?rensson. Proceedings of the
7 * Sixth International Conference on Theory and Applications of Satisfiability
8 * Testing, LNCS 2919, pp 502-518, 2003.
9 *
10 * This library is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 2.1 of the License, or (at your option)
13 * any later version.
14 *
15 * This library is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25 package org.sat4j.tools;
26
27 import org.sat4j.core.VecInt;
28 import org.sat4j.specs.ContradictionException;
29 import org.sat4j.specs.ISolver;
30 import org.sat4j.specs.IVecInt;
31
32 /**
33 * Reader for the Extended Dimacs format proposed by Fahiem Bacchus and Toby
34 * Walsh in array representation (without the terminating 0).
35 *
36 * Adaptation of org.sat4j.reader.ExtendedDimacsReader.
37 *
38 * @author leberre
39 * @author fuhs
40 */
41 public class ExtendedDimacsArrayReader extends DimacsArrayReader {
42
43 public static final int FALSE = 1;
44
45 public static final int TRUE = 2;
46
47 public static final int NOT = 3;
48
49 public static final int AND = 4;
50
51 public static final int NAND = 5;
52
53 public static final int OR = 6;
54
55 public static final int NOR = 7;
56
57 public static final int XOR = 8;
58
59 public static final int XNOR = 9;
60
61 public static final int IMPLIES = 10;
62
63 public static final int IFF = 11;
64
65 public static final int IFTHENELSE = 12;
66
67 public static final int ATLEAST = 13;
68
69 public static final int ATMOST = 14;
70
71 public static final int COUNT = 15;
72
73 private static final long serialVersionUID = 1L;
74
75 private final GateTranslator gater;
76
77 public ExtendedDimacsArrayReader(ISolver solver) {
78 super(solver);
79 gater = new GateTranslator(solver);
80 }
81
82 /**
83 * Handles a single constraint (constraint == Extended Dimacs circuit gate).
84 *
85 * @param gateType
86 * the type of the gate in question
87 * @param output
88 * the number of the output of the gate in question
89 * @param inputs
90 * the numbers of the inputs of the gates in question
91 * @return true
92 */
93 @Override
94 protected boolean handleConstr(int gateType, int output, int[] inputs)
95 throws ContradictionException {
96 IVecInt literals = new VecInt(inputs);
97 switch (gateType) {
98 case FALSE:
99 gater.gateFalse(output);
100 break;
101 case TRUE:
102 gater.gateTrue(output);
103 break;
104 case OR:
105 gater.or(output, literals);
106 break;
107 case NOT:
108 assert literals.size()==1;
109 gater.not(output, inputs[0]);
110 break;
111 case AND:
112 gater.and(output, literals);
113 break;
114 case XOR:
115 gater.xor(output, literals);
116 break;
117 case IFF:
118 gater.iff(output, literals);
119 break;
120 case IFTHENELSE:
121 assert literals.size()==3;
122 gater.ite(output, inputs[0],inputs[1],inputs[2]);
123 break;
124 default:
125 throw new UnsupportedOperationException("Gate type " + gateType
126 + " not handled yet");
127 }
128 return true;
129 }
130 }