View Javadoc

1   /*
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le Berre
3    * 
4    * Based on the original minisat specification from:
5    * 
6    * An extensible SAT solver. Niklas E?n and Niklas S?rensson. Proceedings of the
7    * Sixth International Conference on Theory and Applications of Satisfiability
8    * Testing, LNCS 2919, pp 502-518, 2003.
9    * 
10   * This library is free software; you can redistribute it and/or modify it under
11   * the terms of the GNU Lesser General Public License as published by the Free
12   * Software Foundation; either version 2.1 of the License, or (at your option)
13   * any later version.
14   * 
15   * This library is distributed in the hope that it will be useful, but WITHOUT
16   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18   * details.
19   * 
20   * You should have received a copy of the GNU Lesser General Public License
21   * along with this library; if not, write to the Free Software Foundation, Inc.,
22   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23   * 
24   */
25  
26  package org.sat4j.core;
27  
28  import java.lang.reflect.InvocationTargetException;
29  import java.lang.reflect.Method;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import org.sat4j.specs.ISolver;
34  
35  /**
36   * A solver factory is responsible to provide prebuilt solvers to the end user.
37   * 
38   * @author bourgeois
39   */
40  public abstract class ASolverFactory {
41  
42      /**
43       * This methods returns names of solvers to be used with the method
44       * getSolverByName().
45       * 
46       * @return an array containing the names of all the solvers available in the
47       *         library.
48       * @see #createSolverByName(String)
49       */
50      public String[] solverNames() {
51          List<String> l = new ArrayList<String>();
52          Method[] solvers = this.getClass().getDeclaredMethods();
53          for (int i = 0; i < solvers.length; i++) {
54              if (solvers[i].getParameterTypes().length == 0
55                      && solvers[i].getName().startsWith("new")) { //$NON-NLS-1$
56                  l.add(solvers[i].getName().substring(3));
57              }
58          }
59          String[] names = new String[l.size()];
60          l.toArray(names);
61          return names;
62      }
63  
64      /**
65       * create a solver from its String name. the solvername Xxxx must map one of
66       * the newXxxx methods.
67       * 
68       * @param solvername
69       *            the name of the solver
70       * @return an ISolver built using newSolvername. <code>null</code> if the
71       *         solvername doesn't map one of the method of the factory.
72       */
73      public ISolver createSolverByName(String solvername) {
74          try {
75              Class<?>[] paramtypes = {};
76                  Method m = this.getClass()
77                          .getMethod("new" + solvername, paramtypes); //$NON-NLS-1$
78                  return (ISolver) m.invoke(null, (Object[]) null);
79          } catch (SecurityException e) {
80              e.printStackTrace();
81          } catch (IllegalArgumentException e) {
82               e.printStackTrace();
83          } catch (NoSuchMethodException e) {
84              e.printStackTrace();
85          } catch (IllegalAccessException e) {
86              e.printStackTrace();
87          } catch (InvocationTargetException e) {
88              e.printStackTrace();
89          }
90           return null;
91      }
92  
93      /**
94       * To obtain the default solver of the library. The solver is suitable to
95       * solve huge SAT benchmarks. It should reflect state-of-the-art SAT
96       * technologies.
97       * 
98       * For solving small/easy SAT benchmarks, use lightSolver() instead.
99       * 
100      * @return a solver from the factory
101      * @see #lightSolver()
102      */
103     public abstract ISolver defaultSolver();
104 
105     /**
106      * To obtain a solver that is suitable for solving many small instances of
107      * SAT problems.
108      * 
109      * The solver is not using sophisticated but costly reasoning and avoids to
110      * allocate too much memory.
111      * 
112      * For solving bigger SAT benchmarks, use defaultSolver() instead.
113      * 
114      * @return a solver from the factory
115      * @see #defaultSolver()
116      */
117     public abstract ISolver lightSolver();
118 }