View Javadoc

1   /*******************************************************************************
2   * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2008 Daniel Le Berre
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  package org.sat4j.maxsat.reader;
20  
21  import java.io.IOException;
22  import java.io.LineNumberReader;
23  import java.util.StringTokenizer;
24  
25  import org.sat4j.maxsat.WeightedMaxSatDecorator;
26  import org.sat4j.pb.IPBSolver;
27  import org.sat4j.reader.DimacsReader;
28  import org.sat4j.reader.ParseFormatException;
29  
30  /**
31   * Simple reader for the weighted maxsat problem.
32   * 
33   * @author daniel
34   *
35   */
36  public class WDimacsReader extends DimacsReader {
37  
38      /**
39       * 
40       */
41      private static final long serialVersionUID = 1L;
42  
43      public WDimacsReader(IPBSolver solver) {
44          super(solver,"wcnf");
45      }
46  
47      public WDimacsReader(IPBSolver solver, String format) {
48          super(solver, format);
49      }
50  
51      @Override
52      protected void readProblemLine(LineNumberReader in) throws IOException,
53              ParseFormatException {
54          String line = in.readLine();
55  
56          if (line == null) {
57              throw new ParseFormatException(
58                      "premature end of file: <p cnf ...> expected  on line "
59                              + in.getLineNumber());
60          }
61          StringTokenizer stk = new StringTokenizer(line);
62  
63          if (!(stk.hasMoreTokens() && stk.nextToken().equals("p")
64                  && stk.hasMoreTokens() && stk.nextToken().equals(formatString))) {
65              throw new ParseFormatException(
66                      "problem line expected (p cnf ...) on line "
67                              + in.getLineNumber());
68          }
69  
70          int vars;
71  
72          // reads the max var id
73          vars = Integer.parseInt(stk.nextToken());
74          assert vars > 0;
75          solver.newVar(vars);
76          // reads the number of clauses
77          expectedNbOfConstr = Integer.parseInt(stk.nextToken());
78          assert expectedNbOfConstr > 0;
79          solver.setExpectedNumberOfClauses(expectedNbOfConstr);
80  
81          if ("wcnf".equals(formatString)) {
82              // assume we are in weighted MAXSAT mode
83              int top;
84              if (stk.hasMoreTokens()) {
85                  top = Integer.parseInt(stk.nextToken());
86              } else {
87                  top = Integer.MAX_VALUE;
88              }
89              ((WeightedMaxSatDecorator)solver).setTopWeight(top);
90          }
91      }
92  
93      
94  }