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