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