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