PageRenderTime 195ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 1ms

/src/es/uned/genTest/ComputationalLogic/wrapperProver9Mace4/SingletonProver9.java

https://bitbucket.org/jcampillom/gentestgenerator
Java | 398 lines | 343 code | 19 blank | 36 comment | 79 complexity | 108c5b79225766c1671d2224bec47ff9 MD5 | raw file
  1. package es.uned.genTest.ComputationalLogic.wrapperProver9Mace4;
  2. import java.io.*;
  3. import java.util.*;
  4. import es.uned.genTest.ComputationalLogic.PCLogicClausifyFormula;
  5. import es.uned.genTest.ComputationalLogic.PCLogicFormula;
  6. import es.uned.genTest.ComputationalLogic.PCLogicListClausifyFormula;
  7. import es.uned.genTest.ComputationalLogic.PCLogicListProofs;
  8. /**
  9. * Implementa la gestión de fórmulas lógicas mediante la aplicación externa
  10. * Prover9
  11. *
  12. * @author Joaquín Campillo Molina - jcampillomolina@hotmail.com, Febrero/2012
  13. * @version 1.0
  14. */
  15. public class SingletonProver9 {
  16. static private SingletonProver9 singletonProver9 = new SingletonProver9();
  17. static private String time = "";
  18. private final int EXIT_FALSE = 0;
  19. private final int EXIT_TRUE = 1;
  20. private final int EXIT_ERROR = 2;
  21. private PCLogicListClausifyFormula listClausifyFormula = new PCLogicListClausifyFormula();
  22. private PCLogicListProofs proofs = new PCLogicListProofs();
  23. private SingletonProver9() {
  24. }
  25. static public SingletonProver9 getSingletonProver9() {
  26. return singletonProver9;
  27. }
  28. public boolean exec(List<PCLogicFormula> assumptions,
  29. List<PCLogicFormula> goals, String path) {
  30. readPropertiesFile();
  31. boolean result = false;
  32. int resultProver9 = EXIT_ERROR;
  33. while (resultProver9 == EXIT_ERROR) {
  34. // Modificación 25/02/2012 para evitar mostrar resoluciones
  35. // repetidas
  36. this.listClausifyFormula.clear();
  37. this.proofs.clear();
  38. resultProver9 = execProver9(assumptions, goals, path);
  39. }
  40. if (resultProver9 == EXIT_FALSE)
  41. result = false;
  42. else
  43. result = true;
  44. return result;
  45. }
  46. private int execProver9(List<PCLogicFormula> assumptions,
  47. List<PCLogicFormula> goals, String path) {
  48. Iterator<PCLogicFormula> iter = null;
  49. StringBuffer out = new StringBuffer();
  50. try {
  51. boolean findProof = false;
  52. String formulas = "formulas(assumptions).\n";
  53. iter = assumptions.iterator();
  54. while (iter.hasNext())
  55. formulas = formulas + iter.next().getFormula() + ".\n";
  56. formulas = formulas + "end_of_list.\n";
  57. formulas = formulas + "formulas(goals).\n";
  58. iter = goals.iterator();
  59. while (iter.hasNext())
  60. formulas = formulas + iter.next().getFormula() + ".\n";
  61. formulas = formulas + "end_of_list.\n";
  62. File file = new File(path + "input.in");
  63. if (!file.exists()) {
  64. file.createNewFile();
  65. }
  66. FileWriter inputProver9 = new FileWriter(file.getName());
  67. BufferedWriter bufferedWriter = new BufferedWriter(inputProver9);
  68. bufferedWriter.write(formulas);
  69. bufferedWriter.close();
  70. Process p = Runtime.getRuntime().exec(
  71. path + "prover9.exe -t " + time + " -f " + path
  72. + "input.in");
  73. // Se obtiene el stream de salida del programa
  74. InputStream is = p.getInputStream();
  75. /*
  76. * Se prepara un bufferedReader para poder leer la salida más
  77. * comodamente.
  78. */
  79. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  80. // Se lee la primera linea
  81. String aux = br.readLine();
  82. if (aux == null) {
  83. return EXIT_ERROR;
  84. }
  85. out.append(aux + "\n");
  86. String clausify = "";
  87. String order = "";
  88. String charac = "";
  89. String references = "";
  90. Boolean findOrder;
  91. PCLogicClausifyFormula pcLogicClausifyFormula = null;
  92. // Mientras se haya leido alguna linea
  93. // while (!aux.contains("exit")) { -- Esta es otra opción a tener en
  94. // cuenta
  95. while (!aux.contains("proofs=")) {
  96. // Descomentar la siguiente linea para visualizar resultados
  97. // prover9
  98. // System.out.println (aux);
  99. if (aux.contains("PROCESS NON-CLAUSAL FORMULAS")) {
  100. aux = br.readLine();
  101. while (!aux.contains("end of process non-clausal formulas")) {
  102. aux = br.readLine();
  103. if (aux.contains("assumption")) {
  104. findOrder = false;
  105. order = "";
  106. int i = 0;
  107. clausify = "";
  108. while (i < aux.trim().length()
  109. && !aux.trim().substring(i, i + 1).equals(
  110. "#")) {
  111. charac = aux.trim().substring(i, i + 1);
  112. while (Character.isDigit(aux.charAt(i))
  113. && !findOrder) {
  114. order = order + charac;
  115. i++;
  116. charac = aux.trim().substring(i, i + 1);
  117. }
  118. if (!order.equals("") && !findOrder)
  119. findOrder = true;
  120. clausify = clausify + charac;
  121. i++;
  122. }
  123. this.proofs.putProof(Integer.parseInt(order),
  124. clausify.trim(), "Premisa");
  125. }
  126. }
  127. }
  128. if (aux.contains("PROCESS INITIAL CLAUSES")) {
  129. aux = br.readLine();
  130. String assumptionTreated;
  131. String assumptionInCurse;
  132. while (!aux.contains("formulas(sos)."))
  133. aux = br.readLine();
  134. aux = br.readLine();
  135. while (!aux.contains("end_of_list.")) {
  136. pcLogicClausifyFormula = new PCLogicClausifyFormula();
  137. if (aux.contains("assumption")) {
  138. int i = 0;
  139. clausify = "";
  140. while (i < aux.trim().length()
  141. && !aux.trim().substring(i, i + 1).equals(
  142. ".")) {
  143. clausify = clausify
  144. + aux.trim().substring(i, i + 1);
  145. i++;
  146. }
  147. pcLogicClausifyFormula.putClausify(clausify);
  148. pcLogicClausifyFormula.putAssumption(true);
  149. aux = br.readLine();
  150. } else {
  151. assumptionTreated = aux.trim().substring(
  152. aux.trim().length() - 4,
  153. aux.trim().length() - 3);
  154. assumptionInCurse = assumptionTreated;
  155. while (assumptionInCurse.equals(assumptionTreated)
  156. && !aux.contains("end_of_list.")) {
  157. int i = 0;
  158. clausify = "";
  159. while (i < aux.trim().length()
  160. && !aux.trim().substring(i, i + 1)
  161. .equals(".")) {
  162. clausify = clausify
  163. + aux.trim().substring(i, i + 1);
  164. i++;
  165. }
  166. pcLogicClausifyFormula.putClausify(clausify);
  167. aux = br.readLine();
  168. assumptionInCurse = aux.trim().substring(
  169. aux.trim().length() - 4,
  170. aux.trim().length() - 3);
  171. }
  172. }
  173. this.listClausifyFormula
  174. .putClausifyFormula(pcLogicClausifyFormula);
  175. }
  176. }
  177. if (aux.contains("CLAUSES FOR SEARCH")) {
  178. aux = br.readLine();
  179. while (!aux.contains("end_of_list")) {
  180. aux = br.readLine();
  181. if (aux.contains("clausify")) {
  182. findOrder = false;
  183. order = "";
  184. int i = 0;
  185. clausify = "";
  186. while (i < aux.trim().length()
  187. && !aux.trim().substring(i, i + 1).equals(
  188. ".")) {
  189. charac = aux.trim().substring(i, i + 1);
  190. while (Character.isDigit(aux.charAt(i))
  191. && !findOrder) {
  192. order = order + charac;
  193. i++;
  194. charac = aux.trim().substring(i, i + 1);
  195. }
  196. if (!order.equals("") && !findOrder)
  197. findOrder = true;
  198. clausify = clausify + charac;
  199. i++;
  200. }
  201. this.proofs.putProof(Integer.parseInt(order),
  202. clausify.trim(), "Claúsula de la premisa "
  203. + aux.trim().substring(
  204. aux.trim().length() - 4,
  205. aux.trim().length() - 3));
  206. }
  207. }
  208. }
  209. if (aux.contains("PROOF") && !findProof) {
  210. findProof = true; // Por si hubiera más de una prueba se
  211. // elige la primera
  212. aux = br.readLine();
  213. while (!aux.contains("end of proof")) {
  214. aux = br.readLine();
  215. if (aux.contains("clausify")) {
  216. findOrder = false;
  217. order = "";
  218. int i = 0;
  219. clausify = "";
  220. while (i < aux.trim().length()
  221. && !aux.trim().substring(i, i + 1).equals(
  222. ".")) {
  223. charac = aux.trim().substring(i, i + 1);
  224. while (Character.isDigit(aux.charAt(i))
  225. && !findOrder) {
  226. order = order + charac;
  227. i++;
  228. charac = aux.trim().substring(i, i + 1);
  229. }
  230. if (!order.equals("") && !findOrder)
  231. findOrder = true;
  232. clausify = clausify + charac;
  233. i++;
  234. }
  235. this.proofs.putProof(Integer.parseInt(order),
  236. clausify.trim(), "Claúsula de la premisa "
  237. + aux.trim().substring(
  238. aux.trim().length() - 4,
  239. aux.trim().length() - 3));
  240. } else if (aux.contains("$F")) {
  241. findOrder = false;
  242. order = "";
  243. int i = 0;
  244. clausify = "";
  245. references = "";
  246. while (i < aux.trim().length()
  247. && !aux.trim().substring(i, i + 1).equals(
  248. ".")) {
  249. charac = aux.trim().substring(i, i + 1);
  250. while (Character.isDigit(aux.charAt(i))
  251. && !findOrder) {
  252. order = order + charac;
  253. i++;
  254. charac = aux.trim().substring(i, i + 1);
  255. }
  256. if (!order.equals("") && !findOrder)
  257. findOrder = true;
  258. clausify = clausify + charac;
  259. i++;
  260. }
  261. i++;
  262. // Se buscan las referencias a la dedución en curso
  263. while (i < aux.trim().length()
  264. && !aux.trim().substring(i, i + 1).equals(
  265. ".")) {
  266. charac = aux.trim().substring(i, i + 1);
  267. while (Character.isDigit(aux.charAt(i))) {
  268. references = references + charac;
  269. i++;
  270. charac = aux.trim().substring(i, i + 1);
  271. if (!Character.isDigit(aux.charAt(i)))
  272. references = references + ",";
  273. }
  274. i++;
  275. }
  276. this.proofs.putProof(Integer.parseInt(order),
  277. "[ ]", "Claúsula vacía ("
  278. + references.substring(0,
  279. references.length() - 1)
  280. + ")");
  281. } else if ((aux.contains("[") && !aux
  282. .contains("assumption"))) {
  283. findOrder = false;
  284. order = "";
  285. int i = 0;
  286. clausify = "";
  287. references = "";
  288. while (i < aux.trim().length()
  289. && !aux.trim().substring(i, i + 1).equals(
  290. ".")) {
  291. charac = aux.trim().substring(i, i + 1);
  292. while (Character.isDigit(aux.charAt(i))
  293. && !findOrder) {
  294. order = order + charac;
  295. i++;
  296. charac = aux.trim().substring(i, i + 1);
  297. }
  298. if (!order.equals("") && !findOrder)
  299. findOrder = true;
  300. clausify = clausify + charac;
  301. i++;
  302. }
  303. i++;
  304. // Se buscan las referencias a la dedución en curso
  305. while (i < aux.trim().length()
  306. && !aux.trim().substring(i, i + 1).equals(
  307. ".")) {
  308. charac = aux.trim().substring(i, i + 1);
  309. while (Character.isDigit(aux.charAt(i))) {
  310. references = references + charac;
  311. i++;
  312. charac = aux.trim().substring(i, i + 1);
  313. if (!Character.isDigit(aux.charAt(i)))
  314. references = references + ",";
  315. }
  316. i++;
  317. }
  318. this.proofs.putProof(Integer.parseInt(order),
  319. clausify.trim(), " ("
  320. + references.substring(0,
  321. references.length() - 1)
  322. + ")");
  323. }
  324. }
  325. }
  326. // La siguiente línea se puede descomentar si se quiere
  327. // visualizar la salida de prover9
  328. // Interesante para hacer depuraciones
  329. // System.out.println(aux);
  330. aux = br.readLine();
  331. if (aux == null) {
  332. return EXIT_ERROR;
  333. }
  334. out.append(aux + "\n");
  335. }
  336. } catch (IOException e) {
  337. System.out.println(e.getMessage());
  338. if (e.getMessage().contains("Cannot run program \"prover9.exe\"")) {
  339. System.out
  340. .println("Finalizada la ejecucion. Revise la instalación.");
  341. System.exit(-1);
  342. }
  343. } catch (Exception e) {
  344. // Excepciones si hay algún problema al arrancar el ejecutable o al
  345. // leer su salida.*/
  346. System.out.println(e.toString());
  347. // e.printStackTrace();
  348. // return EXIT_ERROR;
  349. }
  350. // System.out.println(out);
  351. // if (out.toString().contains("Exiting with failure.")) {-- Es otra
  352. // posibilidad que se utilizó pero se ha demostrado más eficiente
  353. // consultar por proofs
  354. if (out.toString().contains("proofs=0")) { // Es satisfacible
  355. return EXIT_FALSE;
  356. } else { // Es insatisfacible
  357. return EXIT_TRUE;
  358. }
  359. }
  360. public PCLogicListProofs getProofs() {
  361. this.proofs.reNumberProofs();
  362. return this.proofs;
  363. }
  364. public PCLogicListClausifyFormula getClausifyFormulas() {
  365. return this.listClausifyFormula;
  366. }
  367. private static void readPropertiesFile() {
  368. final String PROP_FILE = "./properties/configuration.properties";
  369. try {
  370. InputStream is = new FileInputStream(PROP_FILE);
  371. Properties prop = new Properties();
  372. prop.load(is);
  373. time = prop.getProperty("tiempoProver9");
  374. is.close();
  375. } catch (Exception e) {
  376. System.out.println("No se ha podido abrir el fichero " + PROP_FILE);
  377. }
  378. }
  379. }