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 * 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 *******************************************************************************/
28 package org.sat4j.core;
29
30 import java.io.Serializable;
31 import java.lang.reflect.InvocationTargetException;
32 import java.lang.reflect.Method;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import org.sat4j.specs.ISolver;
37
38 /**
39 * A solver factory is responsible for providing prebuilt solvers to the end user.
40 *
41 * @author bourgeois
42 */
43 public abstract class ASolverFactory<T extends ISolver> implements Serializable {
44
45 /**
46 *
47 */
48 private static final long serialVersionUID = 1L;
49
50 /**
51 * This methods returns names of solvers to be used with the method
52 * getSolverByName().
53 *
54 * @return an array containing the names of all the solvers available in the
55 * library.
56 * @see #createSolverByName(String)
57 */
58 public String[] solverNames() {
59 List<String> l = new ArrayList<String>();
60 Method[] solvers = this.getClass().getDeclaredMethods();
61 for (int i = 0; i < solvers.length; i++) {
62 if (solvers[i].getParameterTypes().length == 0
63 && solvers[i].getName().startsWith("new")) { //$NON-NLS-1$
64 l.add(solvers[i].getName().substring(3));
65 }
66 }
67 String[] names = new String[l.size()];
68 l.toArray(names);
69 return names;
70 }
71
72 /**
73 * create a solver from its String name. the solvername Xxxx must map one of
74 * the newXxxx methods.
75 *
76 * @param solvername
77 * the name of the solver
78 * @return an ISolver built using newSolvername. <code>null</code> if the
79 * solvername doesn't map one of the method of the factory.
80 */
81 @SuppressWarnings("unchecked")
82 public T createSolverByName(String solvername) {
83 try {
84 Class<?>[] paramtypes = {};
85 Method m = this.getClass()
86 .getMethod("new" + solvername, paramtypes); //$NON-NLS-1$
87 return (T) m.invoke(null, (Object[]) null);
88 } catch (SecurityException e) {
89 e.printStackTrace();
90 } catch (IllegalArgumentException e) {
91 e.printStackTrace();
92 } catch (NoSuchMethodException e) {
93 e.printStackTrace();
94 } catch (IllegalAccessException e) {
95 e.printStackTrace();
96 } catch (InvocationTargetException e) {
97 e.printStackTrace();
98 }
99 return null;
100 }
101
102 /**
103 * To obtain the default solver of the library. The solver is suitable to
104 * solve huge SAT benchmarks. It should reflect state-of-the-art SAT
105 * technologies.
106 *
107 * For solving small/easy SAT benchmarks, use lightSolver() instead.
108 *
109 * @return a solver from the factory
110 * @see #lightSolver()
111 */
112 public abstract T defaultSolver();
113
114 /**
115 * To obtain a solver that is suitable for solving many small instances of
116 * SAT problems.
117 *
118 * The solver is not using sophisticated but costly reasoning and avoids to
119 * allocate too much memory.
120 *
121 * For solving bigger SAT benchmarks, use defaultSolver() instead.
122 *
123 * @return a solver from the factory
124 * @see #defaultSolver()
125 */
126 public abstract T lightSolver();
127 }