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.IOException;
33 import java.io.PrintWriter;
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 (this.dimacs == null) {
67 this.dimacs = new LecteurDimacs(this.solver);
68
69 }
70 return this.dimacs;
71 }
72
73 private Reader getEZSATReader() {
74 if (this.ezdimacs == null) {
75 this.ezdimacs = new DimacsReader(this.solver);
76
77 }
78 return this.ezdimacs;
79 }
80
81 private Reader getAIGReader() {
82 if (this.aig == null) {
83 this.aig = new AIGReader(this.solver);
84 }
85 return this.aig;
86 }
87
88 private Reader getAAGReader() {
89 if (this.aag == null) {
90 this.aag = new AAGReader(this.solver);
91 }
92 return this.aag;
93 }
94
95 @Override
96 public IProblem parseInstance(String filename)
97 throws ParseFormatException, IOException,
98 ContradictionException {
99 String fname;
100 String prefix = "";
101
102 if (filename.startsWith("http://")) {
103 filename = filename.substring(filename.lastIndexOf('/'),
104 filename.length() - 1);
105 }
106
107 if (filename.indexOf(':') != -1) {
108 String[] parts = filename.split(":");
109 filename = parts[1];
110 prefix = parts[0].toUpperCase(Locale.getDefault());
111
112 }
113
114 if (filename.endsWith(".gz") || filename.endsWith(".bz2")) {
115 fname = filename.substring(0, filename.lastIndexOf('.'));
116 } else {
117 fname = filename;
118 }
119 this.reader = handleFileName(fname, prefix);
120 return this.reader.parseInstance(filename);
121 }
122
123 protected Reader handleFileName(String fname, String prefix) {
124 if ("EZCNF".equals(prefix)) {
125 return getEZSATReader();
126 }
127 if (fname.endsWith(".aag")) {
128 return getAAGReader();
129 }
130 if (fname.endsWith(".aig")) {
131 return getAIGReader();
132 }
133 return getDefaultSATReader();
134 }
135
136 @Override
137 @Deprecated
138 public String decode(int[] model) {
139 return this.reader.decode(model);
140 }
141
142 @Override
143 public void decode(int[] model, PrintWriter out) {
144 this.reader.decode(model, out);
145 }
146
147 @Override
148 public IProblem parseInstance(java.io.InputStream in)
149 throws ParseFormatException, ContradictionException, IOException {
150 throw new UnsupportedOperationException(
151 "Use a domain specific Reader (LecteurDimacs, AIGReader, etc.) for stream input ");
152 }
153
154 }