View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    *  http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   *
19   * Based on the original MiniSat specification from:
20   *
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   *
27   * Contributors:
28   *   CRIL - initial API and implementation
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   * An reader having the responsability to choose the right reader according to
42   * the input.
43   * 
44   * @author leberre
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          // dimacs = new DimacsReader(solver);
62          this.solver = solver;
63      }
64  
65      private Reader getDefaultSATReader() {
66          if (this.dimacs == null) {
67              this.dimacs = new LecteurDimacs(this.solver);// new
68                                                           // LecteurDimacs(solver);
69          }
70          return this.dimacs;
71      }
72  
73      private Reader getEZSATReader() {
74          if (this.ezdimacs == null) {
75              this.ezdimacs = new DimacsReader(this.solver);// new
76                                                            // LecteurDimacs(solver);
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 }