You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
5.4 KiB

  1. /*
  2. * This file is part of the program ltl2dstar (http://www.ltl2dstar.de/).
  3. * Copyright (C) 2005-2007 Joachim Klein <j.klein@ltl2dstar.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifndef LTL2NBA_HPP
  19. #define LTL2NBA_HPP
  20. /** @file
  21. * Provides wrapper classes for external LTL-to-Buechi translators.
  22. */
  23. #include "NBA.hpp"
  24. #include "LTLFormula.hpp"
  25. #include "common/RunProgram.hpp"
  26. #include "parsers/parser_interface.hpp"
  27. #include <cstdio>
  28. /**
  29. * Virtual base class for wrappers to external LTL-to-Buechi translators.
  30. */
  31. template <class NBA_t>
  32. class LTL2NBA {
  33. public:
  34. /** Constructor */
  35. LTL2NBA() {}
  36. /** Destructor */
  37. virtual ~LTL2NBA() {}
  38. /** Convert an LTL formula to an NBA */
  39. virtual NBA_t *ltl2nba(LTLFormula& ltl) = 0;
  40. };
  41. /**
  42. * Wrapper for external LTL-to-Buechi translators using the SPIN interface.
  43. */
  44. template <class NBA_t>
  45. class LTL2NBA_SPIN : public LTL2NBA<NBA_t> {
  46. public:
  47. /**
  48. * Constructor
  49. * @param path path to the executable
  50. * @param arguments vector of command line arguments to be passed to the external translator
  51. */
  52. LTL2NBA_SPIN(std::string path,
  53. std::vector<std::string> arguments=std::vector<std::string>()) :
  54. _path(path), _arguments(arguments) {}
  55. /** Destructor */
  56. virtual ~LTL2NBA_SPIN() {}
  57. /**
  58. * Convert an LTL formula to an NBA
  59. * @param ltl
  60. * @return a pointer to the created NBA (caller gets ownership).
  61. */
  62. virtual
  63. NBA_t *ltl2nba(LTLFormula& ltl) {
  64. // Create canonical APSet (with 'p0', 'p1', ... as AP)
  65. LTLFormula_ptr ltl_canonical=ltl.copy();
  66. APSet_cp canonical_apset(ltl.getAPSet()->createCanonical());
  67. ltl_canonical->switchAPSet(canonical_apset);
  68. AnonymousTempFile spin_outfile;
  69. std::vector<std::string> arguments;
  70. arguments.push_back("-f");
  71. arguments.push_back(ltl_canonical->toStringInfix());
  72. arguments.insert(arguments.end(),
  73. _arguments.begin(),
  74. _arguments.end());
  75. const char *program_path=_path.c_str();
  76. RunProgram spin(program_path,
  77. arguments,
  78. false,
  79. 0,
  80. &spin_outfile,
  81. 0);
  82. int rv=spin.waitForTermination();
  83. if (rv==0) {
  84. NBA_t *result_nba(new NBA_t(canonical_apset));
  85. FILE *f=spin_outfile.getInFILEStream();
  86. if (f==NULL) {
  87. throw Exception("");
  88. }
  89. int rc=nba_parser_promela::parse(f, result_nba);
  90. fclose(f);
  91. if (rc!=0) {
  92. throw Exception("Couldn't parse PROMELA file!");
  93. }
  94. // switch back to original APSet
  95. result_nba->switchAPSet(ltl.getAPSet());
  96. return result_nba;
  97. } else {
  98. // There was an error, return null ptr
  99. return (NBA_t *)0;
  100. }
  101. }
  102. private:
  103. /** The path */
  104. std::string _path;
  105. /** The arguments */
  106. std::vector<std::string> _arguments;
  107. };
  108. /**
  109. * Wrapper for external LTL-to-Buechi translators using the LBTT interface.
  110. */
  111. template <class NBA_t>
  112. class LTL2NBA_LBTT : public LTL2NBA<NBA_t> {
  113. public:
  114. /**
  115. * Constructor
  116. * @param path path to the executable
  117. * @param arguments vector of command line arguments to be passed to the external translator
  118. */
  119. LTL2NBA_LBTT(std::string path,
  120. std::vector<std::string> arguments=std::vector<std::string>()) :
  121. _path(path), _arguments(arguments) {}
  122. /** Destructor */
  123. virtual ~LTL2NBA_LBTT() {}
  124. /**
  125. * Convert an LTL formula to an NBA
  126. * @param ltl
  127. * @return a pointer to the created NBA (caller gets ownership).
  128. */
  129. virtual NBA_t *ltl2nba(LTLFormula& ltl) {
  130. // Create canonical APSet (with 'p0', 'p1', ... as AP)
  131. LTLFormula_ptr ltl_canonical=ltl.copy();
  132. APSet_cp canonical_apset(ltl.getAPSet()->createCanonical());
  133. ltl_canonical->switchAPSet(canonical_apset);
  134. NamedTempFile infile(true);
  135. NamedTempFile outfile(true);
  136. std::ostream& o=infile.getOStream();
  137. o << ltl_canonical->toStringPrefix() << std::endl;
  138. o.flush();
  139. std::vector<std::string> arguments(_arguments);
  140. arguments.push_back(infile.getFileName());
  141. arguments.push_back(outfile.getFileName());
  142. const char *program_path=_path.c_str();
  143. RunProgram ltl2nba_lbtt(program_path,
  144. arguments,
  145. false,
  146. 0,
  147. 0,
  148. 0);
  149. int rv=ltl2nba_lbtt.waitForTermination();
  150. if (rv==0) {
  151. NBA_t *result_nba=new NBA_t(ltl_canonical->getAPSet());
  152. FILE *f=outfile.getInFILEStream();
  153. if (f==NULL) {
  154. throw Exception("");
  155. }
  156. int rc=nba_parser_lbtt::parse(f, result_nba);
  157. fclose(f);
  158. if (rc!=0) {
  159. throw Exception("Couldn't parse LBTT file!");
  160. }
  161. // result_nba->print(std::cerr);
  162. // switch back to original APSet
  163. result_nba->switchAPSet(ltl.getAPSet());
  164. return result_nba;
  165. } else {
  166. // There was an error, return null ptr
  167. return (NBA_t *)0;
  168. }
  169. }
  170. private:
  171. /** The path */
  172. std::string _path;
  173. /** The arguments */
  174. std::vector<std::string> _arguments;
  175. };
  176. #endif