1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
42
43
44
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
91
92
93
94
95
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
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
133
134
135
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
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 }