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.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.PrintWriter;
33 import java.net.URL;
34 import java.util.Locale;
35
36 import org.sat4j.specs.ContradictionException;
37 import org.sat4j.specs.IProblem;
38 import org.sat4j.specs.ISolver;
39
40
41
42
43
44
45
46 public class InstanceReader extends Reader {
47
48 private AAGReader aag;
49
50 private AIGReader aig;
51
52 private DimacsReader ezdimacs;
53
54 private LecteurDimacs dimacs;
55
56 private Reader reader = null;
57
58 private final ISolver solver;
59
60 public InstanceReader(ISolver solver) {
61
62 this.solver = solver;
63 }
64
65 private Reader getDefaultSATReader() {
66 if (dimacs == null) {
67 dimacs = new LecteurDimacs(solver);
68 }
69 return dimacs;
70 }
71
72 private Reader getEZSATReader() {
73 if (ezdimacs == null) {
74 ezdimacs = new DimacsReader(solver);
75 }
76 return ezdimacs;
77 }
78
79 private Reader getAIGReader() {
80 if (aig == null) {
81 aig = new AIGReader(solver);
82 }
83 return aig;
84 }
85
86 private Reader getAAGReader() {
87 if (aag == null) {
88 aag = new AAGReader(solver);
89 }
90 return aag;
91 }
92
93 @Override
94 public IProblem parseInstance(String filename)
95 throws FileNotFoundException, ParseFormatException, IOException,
96 ContradictionException {
97 String fname;
98 boolean isHttp = false;
99 String tempFileName = "";
100 String prefix = "";
101
102 if (filename.startsWith("http://")) {
103 isHttp = true;
104 tempFileName = filename;
105 filename = filename.substring(filename.lastIndexOf('/'), filename
106 .length() - 1);
107 }
108
109 if (filename.indexOf(':') != -1) {
110
111 String[] parts = filename.split(":");
112 filename = parts[1];
113 prefix = parts[0].toUpperCase(Locale.getDefault());
114
115 }
116
117 if (filename.endsWith(".gz")) {
118 fname = filename.substring(0, filename.lastIndexOf('.'));
119 } else {
120 fname = filename;
121 }
122 if ("EZCNF".equals(prefix)) {
123 reader = getEZSATReader();
124 } else if (fname.endsWith(".aag")) {
125 reader = getAAGReader();
126 } else if (fname.endsWith(".aig")) {
127 reader = getAIGReader();
128
129 } else {
130 reader = getDefaultSATReader();
131 }
132
133 if (isHttp) {
134 return reader.parseInstance((new URL(tempFileName)).openStream());
135 }
136 return reader.parseInstance(filename);
137 }
138
139 @Override
140 @Deprecated
141 public String decode(int[] model) {
142 return reader.decode(model);
143 }
144
145 @Override
146 public void decode(int[] model, PrintWriter out) {
147 reader.decode(model, out);
148 }
149
150 @Override
151 public IProblem parseInstance(java.io.Reader in)
152 throws ParseFormatException, ContradictionException, IOException {
153 throw new UnsupportedOperationException();
154 }
155 }