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 package org.sat4j.reader;
26
27 import java.io.IOException;
28 import java.io.LineNumberReader;
29 import java.util.Scanner;
30 import java.util.StringTokenizer;
31
32 import org.sat4j.specs.ContradictionException;
33 import org.sat4j.specs.ISolver;
34 import org.sat4j.specs.IVecInt;
35 import org.sat4j.tools.GateTranslator;
36
37
38
39
40
41
42
43
44 public class ExtendedDimacsReader extends DimacsReader {
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
77
78
79
80 private static final long serialVersionUID = 1L;
81
82 public ExtendedDimacsReader(ISolver solver) {
83 super(new GateTranslator(solver));
84 }
85
86
87
88
89
90
91
92
93
94 @Override
95 protected void readProblemLine(LineNumberReader in) throws IOException,
96 ParseFormatException {
97
98 String line = in.readLine();
99
100 if (line == null) {
101 throw new ParseFormatException(
102 "premature end of file: <p noncnf ...> expected on line "
103 + in.getLineNumber());
104 }
105 StringTokenizer stk = new StringTokenizer(line);
106
107 if (!(stk.hasMoreTokens() && stk.nextToken().equals("p")
108 && stk.hasMoreTokens() && stk.nextToken().equals("noncnf"))) {
109 throw new ParseFormatException(
110 "problem line expected (p noncnf ...) on line "
111 + in.getLineNumber());
112 }
113
114 int vars;
115
116
117 vars = Integer.parseInt(stk.nextToken());
118 assert vars > 0;
119 solver.newVar(vars);
120 try {
121 ((GateTranslator)solver).gateTrue(vars);
122 } catch (ContradictionException e) {
123 assert false;
124 System.err.println("Contradiction when asserting root variable?");
125 }
126 disableNumberOfConstraintCheck();
127 }
128
129
130
131
132
133
134
135 @Override
136 protected boolean handleConstr(String line, IVecInt literals)
137 throws ContradictionException {
138 boolean added = true;
139 assert literals.size() == 0;
140 Scanner scan = new Scanner(line);
141 GateTranslator gater = (GateTranslator)solver;
142 while (scan.hasNext()) {
143 int gateType = scan.nextInt();
144 assert gateType > 0;
145 int nbparam = scan.nextInt();
146 assert nbparam != 0;
147 assert nbparam == -1 || gateType >= ATLEAST;
148 for (int i = 0; i < nbparam; i++) {
149 scan.nextInt();
150 }
151
152 int y = scan.nextInt();
153 int x;
154 while ((x = scan.nextInt()) != 0) {
155 literals.push(x);
156 }
157 switch (gateType) {
158 case FALSE:
159 assert literals.size()==0;
160 gater.gateFalse(y);
161 break;
162 case TRUE:
163 assert literals.size()==0;
164 gater.gateTrue(y);
165 break;
166 case OR:
167 gater.or(y, literals);
168 break;
169 case NOT:
170 assert literals.size()==1;
171 gater.not(y, literals.get(0));
172 break;
173 case AND:
174 gater.and(y, literals);
175 break;
176 case XOR:
177 gater.xor(y, literals);
178 break;
179 case IFF:
180 gater.iff(y, literals);
181 break;
182 case IFTHENELSE:
183 assert literals.size()==3;
184 gater.ite(y, literals.get(0),literals.get(1),literals.get(2));
185 break;
186 default:
187 throw new UnsupportedOperationException("Gate type " + gateType
188 + " not handled yet");
189 }
190 }
191 literals.clear();
192 return added;
193 }
194
195
196 }